|
| 1 | +import numpy as np |
| 2 | +import pandas as pd |
| 3 | +from skimage.draw import line |
| 4 | +from skimage.measure import regionprops_table |
| 5 | + |
| 6 | +from .draw_line import * |
| 7 | + |
| 8 | + |
| 9 | +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() |
| 18 | + 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 | + ], |
| 34 | + ) |
| 35 | + df_nuc_single = pd.DataFrame(props_nuc_single) |
| 36 | + return single_cell_img, nucleus_single_cell_img, single_cell_mask, df_nuc_single |
| 37 | + |
| 38 | + |
| 39 | +def single_cell_analysis( |
| 40 | + single_cell_img, |
| 41 | + single_cell_mask, |
| 42 | + df_nuc_single, |
| 43 | + x_fiber, |
| 44 | + y_fiber, |
| 45 | + internalised_threshold=0.75, |
| 46 | +): |
| 47 | + n_nuc, n_nuc_intern, n_nuc_periph = 0, 0, 0 |
| 48 | + for _, value in df_nuc_single.iterrows(): |
| 49 | + n_nuc += 1 |
| 50 | + # Extend line and find closest point |
| 51 | + m, b = line_equation(x_fiber, y_fiber, value[3], value[2]) |
| 52 | + |
| 53 | + intersections_lst = calculate_intersection( |
| 54 | + m, b, (single_cell_img.shape[0], single_cell_img.shape[1]) |
| 55 | + ) |
| 56 | + border_point = calculate_closest_point(value[3], value[2], intersections_lst) |
| 57 | + rr, cc = line( |
| 58 | + int(y_fiber), |
| 59 | + int(x_fiber), |
| 60 | + int(border_point[1]), |
| 61 | + int(border_point[0]), |
| 62 | + ) |
| 63 | + for index3, coords in enumerate(list(zip(rr, cc))): |
| 64 | + try: |
| 65 | + if single_cell_mask[coords] == 0: |
| 66 | + dist_nuc_cent = calculate_distance( |
| 67 | + x_fiber, y_fiber, value[3], value[2] |
| 68 | + ) |
| 69 | + dist_out_of_fiber = calculate_distance( |
| 70 | + x_fiber, y_fiber, coords[1], coords[0] |
| 71 | + ) |
| 72 | + ratio_dist = dist_nuc_cent / dist_out_of_fiber |
| 73 | + if ratio_dist < internalised_threshold: |
| 74 | + n_nuc_intern += 1 |
| 75 | + else: |
| 76 | + n_nuc_periph += 1 |
| 77 | + break |
| 78 | + except IndexError: |
| 79 | + coords = list(zip(rr, cc))[index3 - 1] |
| 80 | + dist_nuc_cent = calculate_distance(x_fiber, y_fiber, value[3], value[2]) |
| 81 | + dist_out_of_fiber = calculate_distance( |
| 82 | + x_fiber, y_fiber, coords[1], coords[0] |
| 83 | + ) |
| 84 | + ratio_dist = dist_nuc_cent / dist_out_of_fiber |
| 85 | + if ratio_dist < internalised_threshold: |
| 86 | + n_nuc_intern += 1 |
| 87 | + else: |
| 88 | + n_nuc_periph += 1 |
| 89 | + break |
| 90 | + |
| 91 | + return n_nuc, n_nuc_intern, n_nuc_periph |
| 92 | + |
| 93 | + |
| 94 | +def predict_all_cells( |
| 95 | + histo_img, cellpose_df, mask_stardist, internalised_threshold=0.75 |
| 96 | +): |
| 97 | + list_n_nuc, list_n_nuc_intern, list_n_nuc_periph = [], [], [] |
| 98 | + for index in range(len(cellpose_df)): |
| 99 | + ( |
| 100 | + single_cell_img, |
| 101 | + _, |
| 102 | + single_cell_mask, |
| 103 | + df_nuc_single, |
| 104 | + ) = extract_ROIs(histo_img, index, cellpose_df, mask_stardist) |
| 105 | + x_fiber = cellpose_df.iloc[index, 3] - cellpose_df.iloc[index, 6] |
| 106 | + y_fiber = cellpose_df.iloc[index, 2] - cellpose_df.iloc[index, 5] |
| 107 | + n_nuc, n_nuc_intern, n_nuc_periph = single_cell_analysis( |
| 108 | + single_cell_img, single_cell_mask, df_nuc_single, x_fiber, y_fiber |
| 109 | + ) |
| 110 | + list_n_nuc.append(n_nuc) |
| 111 | + list_n_nuc_intern.append(n_nuc_intern) |
| 112 | + list_n_nuc_periph.append(n_nuc_periph) |
| 113 | + df_nuc_analysis = pd.DataFrame( |
| 114 | + list(zip(list_n_nuc, list_n_nuc_intern, list_n_nuc_periph)), |
| 115 | + columns=["N° Nuc", "N° Nuc Intern", "N° Nuc Periph"], |
| 116 | + ) |
| 117 | + return df_nuc_analysis |
| 118 | + |
| 119 | + |
| 120 | +def paint_histo_img(histo_img, cellpose_df, prediction_df): |
| 121 | + paint_img = np.zeros((histo_img.shape[0], histo_img.shape[1]), dtype=np.uint8) |
| 122 | + for index in range(len(cellpose_df)): |
| 123 | + single_cell_mask = cellpose_df.iloc[index, 9].copy() |
| 124 | + if prediction_df.iloc[index, 1] == 0: |
| 125 | + paint_img[ |
| 126 | + cellpose_df.iloc[index, 5] : cellpose_df.iloc[index, 7], |
| 127 | + cellpose_df.iloc[index, 6] : cellpose_df.iloc[index, 8], |
| 128 | + ][single_cell_mask] = 1 |
| 129 | + elif prediction_df.iloc[index, 1] > 0: |
| 130 | + paint_img[ |
| 131 | + cellpose_df.iloc[index, 5] : cellpose_df.iloc[index, 7], |
| 132 | + cellpose_df.iloc[index, 6] : cellpose_df.iloc[index, 8], |
| 133 | + ][single_cell_mask] = 2 |
| 134 | + return paint_img |
| 135 | + |
| 136 | + |
| 137 | +def run_he_analysis(image_ndarray, mask_cellpose, mask_stardist, eccentricity_thresh): |
| 138 | + props_cellpose = regionprops_table( |
| 139 | + mask_cellpose, |
| 140 | + properties=[ |
| 141 | + "label", |
| 142 | + "area", |
| 143 | + "centroid", |
| 144 | + "eccentricity", |
| 145 | + "bbox", |
| 146 | + "image", |
| 147 | + "perimeter", |
| 148 | + ], |
| 149 | + ) |
| 150 | + df_cellpose = pd.DataFrame(props_cellpose) |
| 151 | + df_nuc_analysis = predict_all_cells(image_ndarray, df_cellpose, mask_stardist) |
| 152 | + |
| 153 | + # Result table dict |
| 154 | + headers = ["Feature", "Raw Count", "Proportion (%)"] |
| 155 | + data = [] |
| 156 | + data.append(["N° Nuclei", df_nuc_analysis["N° Nuc"].sum(), 100]) |
| 157 | + data.append( |
| 158 | + [ |
| 159 | + "N° Intern. Nuclei", |
| 160 | + df_nuc_analysis["N° Nuc Intern"].sum(), |
| 161 | + 100 |
| 162 | + * df_nuc_analysis["N° Nuc Intern"].sum() |
| 163 | + / df_nuc_analysis["N° Nuc"].sum(), |
| 164 | + ] |
| 165 | + ) |
| 166 | + data.append( |
| 167 | + [ |
| 168 | + "N° Periph. Nuclei", |
| 169 | + df_nuc_analysis["N° Nuc Periph"].sum(), |
| 170 | + 100 |
| 171 | + * df_nuc_analysis["N° Nuc Periph"].sum() |
| 172 | + / df_nuc_analysis["N° Nuc"].sum(), |
| 173 | + ] |
| 174 | + ) |
| 175 | + data.append( |
| 176 | + [ |
| 177 | + "N° Cells with 1+ intern. nuc.", |
| 178 | + df_nuc_analysis["N° Nuc Intern"].astype(bool).sum(axis=0), |
| 179 | + 100 |
| 180 | + * df_nuc_analysis["N° Nuc Intern"].astype(bool).sum(axis=0) |
| 181 | + / len(df_nuc_analysis), |
| 182 | + ] |
| 183 | + ) |
| 184 | + |
| 185 | + result_df = pd.DataFrame(columns=headers, data=data) |
| 186 | + label_map_he = paint_histo_img(image_ndarray, df_cellpose, df_nuc_analysis) |
| 187 | + return result_df, label_map_he |
0 commit comments