1
1
import numpy as np
2
2
import pandas as pd
3
3
from skimage .draw import line
4
- from skimage .measure import regionprops_table
5
-
6
- from .draw_line import *
4
+ from .common_func import (
5
+ extract_single_image ,
6
+ df_from_cellpose_mask ,
7
+ df_from_stardist_mask ,
8
+ )
9
+ from .draw_line import (
10
+ line_equation ,
11
+ calculate_intersection ,
12
+ calculate_closest_point ,
13
+ calculate_distance ,
14
+ )
15
+ import matplotlib .pyplot as plt
7
16
8
17
9
18
def extract_ROIs (histo_img , index , cellpose_df , mask_stardist ):
10
- single_cell_img = histo_img [
11
- cellpose_df .iloc [index , 5 ] : cellpose_df .iloc [index , 7 ],
12
- cellpose_df .iloc [index , 6 ] : cellpose_df .iloc [index , 8 ],
13
- ].copy ()
14
- nucleus_single_cell_img = mask_stardist [
15
- cellpose_df .iloc [index , 5 ] : cellpose_df .iloc [index , 7 ],
16
- cellpose_df .iloc [index , 6 ] : cellpose_df .iloc [index , 8 ],
17
- ].copy ()
19
+ single_cell_img = extract_single_image (histo_img , cellpose_df , index )
20
+ nucleus_single_cell_img = extract_single_image (mask_stardist , cellpose_df , index )
18
21
single_cell_mask = cellpose_df .iloc [index , 9 ]
19
- single_cell_img [~ single_cell_mask ] = 0
20
- nucleus_single_cell_img [~ single_cell_mask ] = 0
21
-
22
- props_nuc_single = regionprops_table (
23
- nucleus_single_cell_img ,
24
- intensity_image = single_cell_img ,
25
- properties = [
26
- "label" ,
27
- "area" ,
28
- "centroid" ,
29
- "eccentricity" ,
30
- "bbox" ,
31
- "image" ,
32
- "perimeter" ,
33
- "feret_diameter_max" ,
34
- ],
22
+ df_nuc_single = df_from_stardist_mask (
23
+ nucleus_single_cell_img , intensity_image = single_cell_img
35
24
)
36
- df_nuc_single = pd .DataFrame (props_nuc_single )
37
25
return single_cell_img , nucleus_single_cell_img , single_cell_mask , df_nuc_single
38
26
39
27
@@ -47,13 +35,20 @@ def single_cell_analysis(
47
35
internalised_threshold = 0.75 ,
48
36
draw_and_return = False ,
49
37
):
38
+ if draw_and_return :
39
+ fig_size = (5 , 5 )
40
+ f , ax = plt .subplots (figsize = fig_size )
41
+ ax .scatter (x_fiber , y_fiber , color = "white" )
42
+
50
43
n_nuc , n_nuc_intern , n_nuc_periph = 0 , 0 , 0
51
44
new_row_lst = []
52
45
new_col_names = df_nuc_single .columns .tolist ()
53
46
new_col_names .append ("internalised" )
54
47
new_col_names .append ("score_eccentricity" )
55
48
new_col_names .append ("cell label" )
56
49
for _ , value in df_nuc_single .iterrows ():
50
+ if draw_and_return :
51
+ ax .scatter (value [3 ], value [2 ], color = "red" )
57
52
n_nuc += 1
58
53
value = value .tolist ()
59
54
# Extend line and find closest point
@@ -69,6 +64,21 @@ def single_cell_analysis(
69
64
m , b , (single_cell_img .shape [0 ], single_cell_img .shape [1 ])
70
65
)
71
66
border_point = calculate_closest_point (value [3 ], value [2 ], intersections_lst )
67
+ if draw_and_return :
68
+ ax .plot (
69
+ (x_fiber , border_point [0 ]),
70
+ (y_fiber , border_point [1 ]),
71
+ "ro--" ,
72
+ linewidth = 1 ,
73
+ markersize = 1 ,
74
+ )
75
+ ax .plot (
76
+ (x_fiber , value [3 ]),
77
+ (y_fiber , value [2 ]),
78
+ "go--" ,
79
+ linewidth = 1 ,
80
+ markersize = 1 ,
81
+ )
72
82
rr , cc = line (
73
83
int (y_fiber ),
74
84
int (x_fiber ),
@@ -96,6 +106,8 @@ def single_cell_analysis(
96
106
value .append (ratio_dist )
97
107
value .append (cell_label )
98
108
new_row_lst .append (value )
109
+ if draw_and_return :
110
+ ax .scatter (coords [1 ], coords [0 ], color = "red" , s = 10 )
99
111
break
100
112
except IndexError :
101
113
coords = list (zip (rr , cc ))[index3 - 1 ]
@@ -115,8 +127,13 @@ def single_cell_analysis(
115
127
value .append (ratio_dist )
116
128
value .append (cell_label )
117
129
new_row_lst .append (value )
130
+ if draw_and_return :
131
+ ax .scatter (coords [1 ], coords [0 ], color = "red" , s = 10 )
132
+ ax .axis ("off" )
118
133
break
119
134
df_nuc_single_stats = pd .DataFrame (new_row_lst , columns = new_col_names )
135
+ if draw_and_return :
136
+ return n_nuc , n_nuc_intern , n_nuc_periph , df_nuc_single_stats , ax
120
137
return n_nuc , n_nuc_intern , n_nuc_periph , df_nuc_single_stats
121
138
122
139
@@ -173,20 +190,7 @@ def paint_histo_img(histo_img, cellpose_df, prediction_df):
173
190
174
191
175
192
def run_he_analysis (image_ndarray , mask_cellpose , mask_stardist , eccentricity_thresh ):
176
- props_cellpose = regionprops_table (
177
- mask_cellpose ,
178
- properties = [
179
- "label" ,
180
- "area" ,
181
- "centroid" ,
182
- "eccentricity" ,
183
- "bbox" ,
184
- "image" ,
185
- "perimeter" ,
186
- "feret_diameter_max" ,
187
- ],
188
- )
189
- df_cellpose = pd .DataFrame (props_cellpose )
193
+ df_cellpose = df_from_cellpose_mask (mask_cellpose )
190
194
df_nuc_analysis , all_nuc_df_stats = predict_all_cells (
191
195
image_ndarray , df_cellpose , mask_stardist , eccentricity_thresh
192
196
)
0 commit comments