Skip to content

Commit 0fa7f17

Browse files
author
Corentin
committed
--export-stats for nuclei and cell tables
1 parent d466e78 commit 0fa7f17

File tree

2 files changed

+55
-10
lines changed

2 files changed

+55
-10
lines changed

myoquant/HE_analysis.py

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,18 @@ def single_cell_analysis(
4242
df_nuc_single,
4343
x_fiber,
4444
y_fiber,
45+
cell_label,
4546
internalised_threshold=0.75,
4647
):
4748
n_nuc, n_nuc_intern, n_nuc_periph = 0, 0, 0
49+
new_row_lst = []
50+
new_col_names = df_nuc_single.columns.tolist()
51+
new_col_names.append("internalised")
52+
new_col_names.append("score_eccentricity")
53+
new_col_names.append("cell label")
4854
for _, value in df_nuc_single.iterrows():
4955
n_nuc += 1
56+
value = value.tolist()
5057
# Extend line and find closest point
5158

5259
# Handling of the case where the nucleus is at the exact center of the fiber
@@ -78,8 +85,15 @@ def single_cell_analysis(
7885
ratio_dist = dist_nuc_cent / dist_out_of_fiber
7986
if ratio_dist <= internalised_threshold:
8087
n_nuc_intern += 1
88+
value.append(True)
89+
value.append(ratio_dist)
90+
value.append(cell_label)
8191
else:
8292
n_nuc_periph += 1
93+
value.append(False)
94+
value.append(ratio_dist)
95+
value.append(cell_label)
96+
new_row_lst.append(value)
8397
break
8498
except IndexError:
8599
coords = list(zip(rr, cc))[index3 - 1]
@@ -90,17 +104,24 @@ def single_cell_analysis(
90104
ratio_dist = dist_nuc_cent / dist_out_of_fiber
91105
if ratio_dist < internalised_threshold:
92106
n_nuc_intern += 1
107+
value.append(True)
108+
value.append(ratio_dist)
109+
value.append(cell_label)
93110
else:
94111
n_nuc_periph += 1
112+
value.append(True)
113+
value.append(ratio_dist)
114+
value.append(cell_label)
115+
new_row_lst.append(value)
95116
break
96-
97-
return n_nuc, n_nuc_intern, n_nuc_periph
117+
df_nuc_single_stats = pd.DataFrame(new_row_lst, columns=new_col_names)
118+
return n_nuc, n_nuc_intern, n_nuc_periph, df_nuc_single_stats
98119

99120

100121
def predict_all_cells(
101122
histo_img, cellpose_df, mask_stardist, internalised_threshold=0.75
102123
):
103-
list_n_nuc, list_n_nuc_intern, list_n_nuc_periph = [], [], []
124+
list_n_nuc, list_n_nuc_intern, list_n_nuc_periph, list_nuc_df = [], [], [], []
104125
for index in range(len(cellpose_df)):
105126
(
106127
single_cell_img,
@@ -110,22 +131,26 @@ def predict_all_cells(
110131
) = extract_ROIs(histo_img, index, cellpose_df, mask_stardist)
111132
x_fiber = cellpose_df.iloc[index, 3] - cellpose_df.iloc[index, 6]
112133
y_fiber = cellpose_df.iloc[index, 2] - cellpose_df.iloc[index, 5]
113-
n_nuc, n_nuc_intern, n_nuc_periph = single_cell_analysis(
134+
n_nuc, n_nuc_intern, n_nuc_periph, df_nuc_single_stats = single_cell_analysis(
114135
single_cell_img,
115136
single_cell_mask,
116137
df_nuc_single,
117138
x_fiber,
118139
y_fiber,
140+
index + 1,
119141
internalised_threshold,
120142
)
121143
list_n_nuc.append(n_nuc)
122144
list_n_nuc_intern.append(n_nuc_intern)
123145
list_n_nuc_periph.append(n_nuc_periph)
146+
list_nuc_df.append(df_nuc_single_stats)
124147
df_nuc_analysis = pd.DataFrame(
125148
list(zip(list_n_nuc, list_n_nuc_intern, list_n_nuc_periph)),
126149
columns=["N° Nuc", "N° Nuc Intern", "N° Nuc Periph"],
127150
)
128-
return df_nuc_analysis
151+
all_nuc_df_stats = pd.concat(list_nuc_df, ignore_index=True)
152+
cellpose_df_stat = pd.concat([cellpose_df, df_nuc_analysis], axis=1)
153+
return cellpose_df_stat, all_nuc_df_stats
129154

130155

131156
def paint_histo_img(histo_img, cellpose_df, prediction_df):
@@ -159,7 +184,7 @@ def run_he_analysis(image_ndarray, mask_cellpose, mask_stardist, eccentricity_th
159184
],
160185
)
161186
df_cellpose = pd.DataFrame(props_cellpose)
162-
df_nuc_analysis = predict_all_cells(
187+
df_nuc_analysis, all_nuc_df_stats = predict_all_cells(
163188
image_ndarray, df_cellpose, mask_stardist, eccentricity_thresh
164189
)
165190

@@ -197,4 +222,4 @@ def run_he_analysis(image_ndarray, mask_cellpose, mask_stardist, eccentricity_th
197222

198223
result_df = pd.DataFrame(columns=headers, data=data)
199224
label_map_he = paint_histo_img(image_ndarray, df_cellpose, df_nuc_analysis)
200-
return result_df, label_map_he
225+
return result_df, label_map_he, df_nuc_analysis, all_nuc_df_stats

myoquant/__main__.py

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,7 @@ def he_analysis(
338338
mask_stardist = imread(stardist_path)
339339

340340
console.print("Calculating all nuclei eccentricity scores... !", style="blue")
341-
result_df, full_label_map = run_he_analysis(
341+
result_df, full_label_map, df_nuc_analysis, all_nuc_df_stats = run_he_analysis(
342342
image_ndarray, mask_cellpose, mask_stardist, eccentricity_thresh
343343
)
344344
console.print("Analysis completed ! ", style="green")
@@ -352,14 +352,34 @@ def he_analysis(
352352
str(row[2]),
353353
)
354354
console.print(table)
355-
csv_name = image_path.stem + "_results.csv"
355+
csv_name = image_path.stem + "_results_summary.csv"
356+
cell_details_name = image_path.stem + "_cell_details.csv"
357+
nuc_details_name = image_path.stem + "_nuc_details.csv"
356358
result_df.to_csv(
357359
output_path / csv_name,
358360
index=False,
359361
)
360362
console.print(
361-
f"Table saved as a .csv file named {output_path/csv_name}", style="green"
363+
f"Summary Table saved as a .csv file named {output_path/csv_name}",
364+
style="green",
362365
)
366+
if export_stats:
367+
df_nuc_analysis.drop("image", axis=1).to_csv(
368+
output_path / cell_details_name,
369+
index=False,
370+
)
371+
console.print(
372+
f"Cell Table saved as a .csv file named {output_path/cell_details_name}",
373+
style="green",
374+
)
375+
all_nuc_df_stats.drop("image", axis=1).to_csv(
376+
output_path / nuc_details_name,
377+
index=False,
378+
)
379+
console.print(
380+
f"Nuclei Table saved as a .csv file named {output_path/nuc_details_name}",
381+
style="green",
382+
)
363383
label_map_name = image_path.stem + "_label_map.tiff"
364384
Image.fromarray(full_label_map).save(output_path / label_map_name)
365385
console.print(

0 commit comments

Comments
 (0)