Skip to content

Commit b44cfd2

Browse files
author
Corentin
committed
starting the merge with streamlit version
1 parent 6ecfe65 commit b44cfd2

File tree

7 files changed

+163
-96
lines changed

7 files changed

+163
-96
lines changed

myoquant/commands/run_sdh.py

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -164,21 +164,23 @@ def sdh_analysis(
164164
progress.add_task(description="Reading all inputs...", total=None)
165165
image_ndarray_sdh = imread(image_path)
166166

167-
if mask_path is not None:
168-
mask_ndarray = imread(mask_path)
169-
if np.unique(mask_ndarray).shape[0] != 2:
170-
console.print(
171-
"The mask image should be a binary image with only 2 values (0 and 1).",
172-
style="red",
173-
)
174-
raise ValueError
175-
if len(image_ndarray_sdh.shape) > 2:
176-
mask_ndarray = np.repeat(
177-
mask_ndarray.reshape(mask_ndarray.shape[0], mask_ndarray.shape[1], 1),
178-
image_ndarray_sdh.shape[2],
179-
axis=2,
180-
)
181-
image_ndarray_sdh = image_ndarray_sdh * mask_ndarray
167+
if mask_path is not None:
168+
mask_ndarray = imread(mask_path)
169+
if np.unique(mask_ndarray).shape[0] != 2:
170+
console.print(
171+
"The mask image should be a binary image with only 2 values (0 and 1).",
172+
style="red",
173+
)
174+
raise ValueError
175+
if len(image_ndarray_sdh.shape) > 2:
176+
mask_ndarray = np.repeat(
177+
mask_ndarray.reshape(
178+
mask_ndarray.shape[0], mask_ndarray.shape[1], 1
179+
),
180+
image_ndarray_sdh.shape[2],
181+
axis=2,
182+
)
183+
image_ndarray_sdh = image_ndarray_sdh * mask_ndarray
182184
if cellpose_path is not None:
183185
mask_cellpose = imread(cellpose_path)
184186

myoquant/src/ATP_analysis.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,25 @@ def estimate_threshold(intensity_list):
4444
return threshold
4545

4646

47+
def plot_density(all_cell_median_intensity, intensity_threshold):
48+
if intensity_threshold == 0:
49+
intensity_threshold = estimate_threshold(all_cell_median_intensity)
50+
fig, ax = plt.subplots(figsize=(10, 5))
51+
density = gaussian_kde(all_cell_median_intensity)
52+
density.covariance_factor = lambda: 0.25
53+
density._compute_covariance()
54+
55+
# Create a vector of 256 values going from 0 to 256:
56+
xs = np.linspace(0, 255, 256)
57+
density_xs_values = density(xs)
58+
ax.plot(xs, density_xs_values, label="Estimated Density")
59+
ax.axvline(x=intensity_threshold, color="red", label="Threshold")
60+
ax.set_xlabel("Pixel Intensity")
61+
ax.set_ylabel("Density")
62+
ax.legend()
63+
return fig
64+
65+
4766
def predict_all_cells(histo_img, cellpose_df, intensity_threshold):
4867
all_cell_median_intensity = get_all_intensity(histo_img, cellpose_df)
4968
if intensity_threshold is None:

myoquant/src/HE_analysis.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ def single_cell_analysis(
4545
y_fiber,
4646
cell_label,
4747
internalised_threshold=0.75,
48+
draw_and_return=False,
4849
):
4950
n_nuc, n_nuc_intern, n_nuc_periph = 0, 0, 0
5051
new_row_lst = []

myoquant/src/SDH_analysis.py

Lines changed: 16 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@
44

55
import pandas as pd
66
import tensorflow as tf
7-
from rich.progress import track
8-
from skimage.measure import regionprops_table
9-
7+
from .common_func import extract_single_image, df_from_cellpose_mask
108
from .gradcam import make_gradcam_heatmap, save_and_display_gradcam
119
import numpy as np
1210

@@ -32,14 +30,7 @@ def resize_batch_cells(histo_img, cellpose_df):
3230
img_array_full = np.empty((len(cellpose_df), 256, 256, 3))
3331
# for index in track(range(len(cellpose_df)), description="Resizing cells"):
3432
for index in range(len(cellpose_df)):
35-
single_cell_img = histo_img[
36-
cellpose_df.iloc[index, 5] : cellpose_df.iloc[index, 7],
37-
cellpose_df.iloc[index, 6] : cellpose_df.iloc[index, 8],
38-
].copy()
39-
40-
single_cell_mask = cellpose_df.iloc[index, 9].copy()
41-
single_cell_img[~single_cell_mask] = 0
42-
33+
single_cell_img = extract_single_image(histo_img, cellpose_df, index)
4334
img_array_full[index] = tf.image.resize(single_cell_img, (256, 256))
4435
return img_array_full
4536

@@ -54,7 +45,9 @@ def predict_all_cells(histo_img, cellpose_df, _model_SDH):
5445
predicted_class_array[index_counter] = prediction_result.argmax()
5546
predicted_proba_array[index_counter] = np.amax(prediction_result)
5647
index_counter += 1
57-
return predicted_class_array, predicted_proba_array
48+
cellpose_df["class_predicted"] = predicted_class_array
49+
cellpose_df["proba_predicted"] = predicted_proba_array
50+
return cellpose_df
5851

5952

6053
def paint_full_image(image_sdh, df_cellpose, class_predicted_all):
@@ -78,43 +71,28 @@ def paint_full_image(image_sdh, df_cellpose, class_predicted_all):
7871

7972

8073
def run_sdh_analysis(image_array, model_SDH, mask_cellpose):
81-
props_cellpose = regionprops_table(
82-
mask_cellpose,
83-
properties=[
84-
"label",
85-
"area",
86-
"centroid",
87-
"eccentricity",
88-
"bbox",
89-
"image",
90-
"perimeter",
91-
],
92-
)
93-
df_cellpose = pd.DataFrame(props_cellpose)
94-
class_predicted_all, proba_predicted_all = predict_all_cells(
95-
image_array, df_cellpose, model_SDH
96-
)
97-
df_cellpose["class_predicted"] = class_predicted_all
98-
df_cellpose["proba_predicted"] = proba_predicted_all
99-
count_per_label = np.unique(class_predicted_all, return_counts=True)
100-
class_and_proba_df = pd.DataFrame(
101-
list(zip(class_predicted_all, proba_predicted_all)),
102-
columns=["class", "proba"],
103-
)
74+
df_cellpose = df_from_cellpose_mask(mask_cellpose)
75+
76+
df_cellpose = predict_all_cells(image_array, df_cellpose, model_SDH)
77+
count_per_label = np.unique(df_cellpose["class_predicted"], return_counts=True)
10478

10579
# Result table dict
10680
headers = ["Feature", "Raw Count", "Proportion (%)"]
10781
data = []
108-
data.append(["Muscle Fibers", len(class_predicted_all), 100])
82+
data.append(["Muscle Fibers", len(df_cellpose["class_predicted"]), 100])
10983
for elem in count_per_label[0]:
11084
data.append(
11185
[
11286
labels_predict[int(elem)],
11387
count_per_label[1][int(elem)],
114-
100 * count_per_label[1][int(elem)] / len(class_predicted_all),
88+
100
89+
* count_per_label[1][int(elem)]
90+
/ len(df_cellpose["class_predicted"]),
11591
]
11692
)
11793
result_df = pd.DataFrame(columns=headers, data=data)
11894
# Paint The Full Image
119-
full_label_map = paint_full_image(image_array, df_cellpose, class_predicted_all)
95+
full_label_map = paint_full_image(
96+
image_array, df_cellpose, df_cellpose["class_predicted"]
97+
)
12098
return result_df, full_label_map, df_cellpose

myoquant/src/common_func.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,51 @@ def run_stardist(image, model_stardist, nms_thresh=0.4, prob_thresh=0.5):
130130
return mask_stardist
131131

132132

133+
def df_from_cellpose_mask(mask):
134+
props_cellpose = regionprops_table(
135+
mask,
136+
properties=[
137+
"label",
138+
"area",
139+
"centroid",
140+
"eccentricity",
141+
"bbox",
142+
"image",
143+
"perimeter",
144+
"feret_diameter_max",
145+
],
146+
)
147+
df_cellpose = pd.DataFrame(props_cellpose)
148+
return df_cellpose
149+
150+
151+
def df_from_stardist_mask(mask):
152+
props_stardist = regionprops_table(
153+
mask,
154+
properties=[
155+
"label",
156+
"area",
157+
"centroid",
158+
"eccentricity",
159+
"bbox",
160+
"image",
161+
"perimeter",
162+
],
163+
)
164+
df_stardist = pd.DataFrame(props_stardist)
165+
return df_stardist
166+
167+
168+
def extract_single_image(raw_image, df_props, index):
169+
single_entity_img = raw_image[
170+
df_props.iloc[index, 5] : df_props.iloc[index, 7],
171+
df_props.iloc[index, 6] : df_props.iloc[index, 8],
172+
].copy()
173+
single_entity_mask = df_props.iloc[index, 9]
174+
single_entity_img[~single_entity_mask] = 0
175+
return single_entity_img
176+
177+
133178
def label2rgb(img_ndarray, label_map):
134179
"""Convert a label map to RGB image
135180

poetry.lock

Lines changed: 64 additions & 42 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)