33from sklearn .mixture import GaussianMixture
44from .common_func import extract_single_image , df_from_cellpose_mask
55import numpy as np
6+ import matplotlib
7+
8+ matplotlib .use ("agg" )
9+
610import matplotlib .pyplot as plt
711
812labels_predict = {1 : "fiber type 1" , 2 : "fiber type 2" }
913np .random .seed (42 )
1014
1115
12- def get_all_intensity (image_array , df_cellpose ):
16+ def get_all_intensity (
17+ image_array , df_cellpose , intensity_method = "median" , erosion = False
18+ ):
1319 all_cell_median_intensity = []
1420 for index in range (len (df_cellpose )):
15- single_cell_img = extract_single_image (image_array , df_cellpose , index )
21+ single_cell_img = extract_single_image (image_array , df_cellpose , index , erosion )
1622
1723 # Calculate median pixel intensity of the cell but ignore 0 values
18- single_cell_median_intensity = np .median (single_cell_img [single_cell_img > 0 ])
24+ if intensity_method == "median" :
25+ single_cell_median_intensity = np .median (
26+ single_cell_img [single_cell_img > 0 ]
27+ )
28+ elif intensity_method == "mean" :
29+ single_cell_median_intensity = np .mean (single_cell_img [single_cell_img > 0 ])
1930 all_cell_median_intensity .append (single_cell_median_intensity )
2031 return all_cell_median_intensity
2132
2233
23- def estimate_threshold (intensity_list ):
34+ def estimate_threshold (intensity_list , n_classes = 2 ):
2435 density = gaussian_kde (intensity_list )
2536 density .covariance_factor = lambda : 0.25
2637 density ._compute_covariance ()
2738
2839 # Create a vector of 256 values going from 0 to 256:
2940 xs = np .linspace (0 , 255 , 256 )
3041 density_xs_values = density (xs )
31- gmm = GaussianMixture (n_components = 2 ).fit (np .array (intensity_list ).reshape (- 1 , 1 ))
42+ gmm = GaussianMixture (n_components = n_classes ).fit (
43+ np .array (intensity_list ).reshape (- 1 , 1 )
44+ )
3245
3346 # Find the x values of the two peaks
3447 peaks_x = np .sort (gmm .means_ .flatten ())
3548 # Find the minimum point between the two peaks
36- min_index = np .argmin (density_xs_values [(xs > peaks_x [0 ]) & (xs < peaks_x [1 ])])
37- threshold = peaks_x [0 ] + xs [min_index ]
3849
39- return threshold
50+ threshold_list = []
51+ length = len (peaks_x )
52+ for index , peaks in enumerate (peaks_x ):
53+ if index == length - 1 :
54+ break
55+ min_index = np .argmin (
56+ density_xs_values [(xs > peaks ) & (xs < peaks_x [index + 1 ])]
57+ )
58+ threshold_list .append (peaks + xs [min_index ])
59+ return threshold_list
4060
4161
42- def plot_density (all_cell_median_intensity , intensity_threshold ):
62+ def plot_density (all_cell_median_intensity , intensity_threshold , n_classes = 2 ):
4363 if intensity_threshold == 0 :
44- intensity_threshold = estimate_threshold (all_cell_median_intensity )
64+ intensity_threshold = estimate_threshold (all_cell_median_intensity , n_classes )
4565 fig , ax = plt .subplots (figsize = (10 , 5 ))
4666 density = gaussian_kde (all_cell_median_intensity )
4767 density .covariance_factor = lambda : 0.25
@@ -51,22 +71,44 @@ def plot_density(all_cell_median_intensity, intensity_threshold):
5171 xs = np .linspace (0 , 255 , 256 )
5272 density_xs_values = density (xs )
5373 ax .plot (xs , density_xs_values , label = "Estimated Density" )
54- ax .axvline (x = intensity_threshold , color = "red" , label = "Threshold" )
74+ for values in intensity_threshold :
75+ ax .axvline (x = values , color = "red" , label = "Threshold" )
5576 ax .set_xlabel ("Pixel Intensity" )
5677 ax .set_ylabel ("Density" )
5778 ax .legend ()
5879 return fig
5980
6081
61- def predict_all_cells (histo_img , cellpose_df , intensity_threshold ):
62- all_cell_median_intensity = get_all_intensity (histo_img , cellpose_df )
82+ def merge_peaks_too_close (peak_list ):
83+ pass
84+
85+
86+ def classify_cells_intensity (all_cell_median_intensity , intensity_threshold ):
87+ muscle_fiber_type_all = []
88+ for intensity in all_cell_median_intensity :
89+ class_cell = np .searchsorted (intensity_threshold , intensity , side = "right" )
90+ muscle_fiber_type_all .append (class_cell )
91+ return muscle_fiber_type_all
92+
93+
94+ def predict_all_cells (
95+ histo_img ,
96+ cellpose_df ,
97+ intensity_threshold ,
98+ n_classes = 2 ,
99+ intensity_method = "median" ,
100+ erosion = False ,
101+ ):
102+ all_cell_median_intensity = get_all_intensity (
103+ histo_img , cellpose_df , intensity_method , erosion
104+ )
63105 if intensity_threshold is None :
64- intensity_threshold = estimate_threshold (all_cell_median_intensity )
106+ intensity_threshold = estimate_threshold (all_cell_median_intensity , n_classes )
65107
66- muscle_fiber_type_all = [
67- 1 if x > intensity_threshold else 2 for x in all_cell_median_intensity
68- ]
69- return muscle_fiber_type_all , all_cell_median_intensity
108+ muscle_fiber_type_all = classify_cells_intensity (
109+ all_cell_median_intensity , intensity_threshold
110+ )
111+ return muscle_fiber_type_all , all_cell_median_intensity , intensity_threshold
70112
71113
72114def paint_full_image (image_atp , df_cellpose , class_predicted_all ):
@@ -76,24 +118,46 @@ def paint_full_image(image_atp, df_cellpose, class_predicted_all):
76118 # for index in track(range(len(df_cellpose)), description="Painting cells"):
77119 for index in range (len (df_cellpose )):
78120 single_cell_mask = df_cellpose .iloc [index , 9 ].copy ()
79- if class_predicted_all [index ] == 1 :
80- image_atp_paint [
81- df_cellpose .iloc [index , 5 ] : df_cellpose .iloc [index , 7 ],
82- df_cellpose .iloc [index , 6 ] : df_cellpose .iloc [index , 8 ],
83- ][single_cell_mask ] = 1
84- elif class_predicted_all [index ] == 2 :
85- image_atp_paint [
86- df_cellpose .iloc [index , 5 ] : df_cellpose .iloc [index , 7 ],
87- df_cellpose .iloc [index , 6 ] : df_cellpose .iloc [index , 8 ],
88- ][single_cell_mask ] = 2
121+ image_atp_paint [
122+ df_cellpose .iloc [index , 5 ] : df_cellpose .iloc [index , 7 ],
123+ df_cellpose .iloc [index , 6 ] : df_cellpose .iloc [index , 8 ],
124+ ][single_cell_mask ] = (
125+ class_predicted_all [index ] + 1
126+ )
89127 return image_atp_paint
90128
91129
92- def run_atp_analysis (image_array , mask_cellpose , intensity_threshold = None ):
130+ def label_list_from_threhsold (threshold_list ):
131+ label_list = []
132+ length = len (threshold_list )
133+ for index , threshold in enumerate (threshold_list ):
134+ if index == 0 :
135+ label_list .append (f"<{ threshold } " )
136+ if index == length - 1 :
137+ label_list .append (f">{ threshold } " )
138+ else :
139+ label_list .append (f">{ threshold } & <{ threshold_list [index + 1 ]} " )
140+ return label_list
141+
142+
143+ def run_atp_analysis (
144+ image_array ,
145+ mask_cellpose ,
146+ intensity_threshold = None ,
147+ n_classes = 2 ,
148+ intensity_method = "median" ,
149+ erosion = False ,
150+ ):
93151 df_cellpose = df_from_cellpose_mask (mask_cellpose )
94- class_predicted_all , intensity_all = predict_all_cells (
95- image_array , df_cellpose , intensity_threshold
152+ class_predicted_all , intensity_all , intensity_threshold = predict_all_cells (
153+ image_array ,
154+ df_cellpose ,
155+ intensity_threshold ,
156+ n_classes ,
157+ intensity_method ,
158+ erosion ,
96159 )
160+ fig = plot_density (intensity_all , intensity_threshold , n_classes )
97161 df_cellpose ["muscle_cell_type" ] = class_predicted_all
98162 df_cellpose ["cell_intensity" ] = intensity_all
99163 count_per_label = np .unique (class_predicted_all , return_counts = True )
@@ -102,15 +166,16 @@ def run_atp_analysis(image_array, mask_cellpose, intensity_threshold=None):
102166 headers = ["Feature" , "Raw Count" , "Proportion (%)" ]
103167 data = []
104168 data .append (["Muscle Fibers" , len (class_predicted_all ), 100 ])
169+ label_list = label_list_from_threhsold (intensity_threshold )
105170 for index , elem in enumerate (count_per_label [0 ]):
106171 data .append (
107172 [
108- labels_predict [int (elem )],
173+ label_list [int (elem )],
109174 count_per_label [1 ][int (index )],
110175 100 * count_per_label [1 ][int (index )] / len (class_predicted_all ),
111176 ]
112177 )
113178 result_df = pd .DataFrame (columns = headers , data = data )
114179 # Paint The Full Image
115180 full_label_map = paint_full_image (image_array , df_cellpose , class_predicted_all )
116- return result_df , full_label_map , df_cellpose
181+ return result_df , full_label_map , df_cellpose , fig
0 commit comments