Skip to content

Commit bd28c90

Browse files
committed
updated
1 parent 5fb0d44 commit bd28c90

2 files changed

Lines changed: 66 additions & 54 deletions

File tree

src/DAB_Analysis_Functions.py

Lines changed: 66 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -134,27 +134,27 @@ def chop_and_analyse_svs(
134134

135135
start = time.time()
136136

137-
for i in np.arange(n_samples_forthresh):
138-
data_tothresh = IO.imread(random_tiffs[i])
139-
maskname = os.path.split(random_tiffs[i])[-1]
137+
for n in np.arange(n_samples_forthresh):
138+
data_tothresh = IO.imread(random_tiffs[n])
139+
maskname = os.path.split(random_tiffs[n])[-1]
140140
maskname = maskname.split("_")
141141
maskname.insert(1, "mask")
142142
maskname = "_".join(maskname)
143143
maskname = os.path.join(temp_folder, maskname)
144144
mask = IO.imread(maskname)
145145
(
146-
thresh_asyn_variance[i],
147-
thresh_nuclei_variance[i],
146+
thresh_asyn_variance[n],
147+
thresh_nuclei_variance[n],
148148
) = self.analyse_DAB_and_cells(
149149
data_tothresh,
150150
mask=mask,
151-
filename=os.path.split(random_tiffs[i])[-1],
151+
filename=os.path.split(random_tiffs[n])[-1],
152152
give_NN=False,
153153
justthresh=True,
154154
)
155155
print(
156156
"Thresholding; tiff {}/{} Time elapsed: {:.2f} minutes".format(
157-
i + 1,
157+
n + 1,
158158
n_samples_forthresh,
159159
(time.time() - start) / 60,
160160
),
@@ -169,6 +169,10 @@ def chop_and_analyse_svs(
169169
thresh_asyn = np.percentile(thresh_asyn_variance, percentile)
170170
thresh_nuclei = np.percentile(thresh_nuclei_variance, percentile)
171171

172+
del thresh_asyn_variance
173+
del thresh_nuclei_variance
174+
del random_tiffs
175+
172176
to_save = {"thresh_protein": thresh_asyn, "thresh_nuclei": thresh_nuclei}
173177
with open(
174178
os.path.join(folder, slice_name + "_thresholds.json"),
@@ -180,6 +184,9 @@ def chop_and_analyse_svs(
180184
pixel_size_um = pixel_size * 1e6
181185

182186
start = time.time()
187+
188+
table_asyn = []
189+
table_nuclei = []
183190

184191
for i in np.arange(len(tiffs)):
185192
data_toanalyse = IO.imread(tiffs[i])
@@ -214,56 +221,61 @@ def chop_and_analyse_svs(
214221
pixel_size=pixel_size_um,
215222
)
216223

217-
xc = pl.Series(
218-
"centroid-x", table_asyn_temp["centroid-0"].to_numpy() + xpos
219-
)
220-
yc = pl.Series(
221-
"centroid-y", table_asyn_temp["centroid-1"].to_numpy() + ypos
222-
)
223-
table_asyn_temp = table_asyn_temp.replace_column(0, xc)
224-
table_asyn_temp = table_asyn_temp.replace_column(1, yc)
225-
226-
xnc = pl.Series(
227-
"centroid-x", table_nuclei_temp["centroid-0"].to_numpy() + xpos
228-
)
229-
ync = pl.Series(
230-
"centroid-y", table_nuclei_temp["centroid-1"].to_numpy() + ypos
231-
)
232-
table_nuclei_temp = table_nuclei_temp.replace_column(0, xnc)
233-
table_nuclei_temp = table_nuclei_temp.replace_column(1, ync)
234-
235-
if save_figs == True:
236-
import matplotlib
237-
238-
matplotlib.use("Agg")
239-
fig, axs = self.plot_masks(
240-
IO.imread(tiffs[i]),
241-
np.dstack([image_mask_asyn, image_mask_nuclei]),
224+
if len(table_asyn_temp) > 0:
225+
xc = pl.Series(
226+
"centroid-x", table_asyn_temp["centroid-0"].to_numpy() + xpos
242227
)
243-
figname = (
244-
str(int(os.path.split(tiffs[i])[-1].split("_")[1]))
245-
+ "_"
246-
+ str(
247-
int(
248-
os.path.split(tiffs[i])[-1]
249-
.split("_")[-1]
250-
.split(".tiff")[0]
251-
)
252-
)
253-
+ "_segmentation_example.svg"
228+
yc = pl.Series(
229+
"centroid-y", table_asyn_temp["centroid-1"].to_numpy() + ypos
254230
)
255-
plt.savefig(
256-
os.path.join(fig_folder, figname), format="svg", dpi=600
231+
table_asyn_temp = table_asyn_temp.replace_column(0, xc)
232+
table_asyn_temp = table_asyn_temp.replace_column(1, yc)
233+
234+
xnc = pl.Series(
235+
"centroid-x", table_nuclei_temp["centroid-0"].to_numpy() + xpos
257236
)
258-
259-
if i == 0:
260-
table_asyn = table_asyn_temp
261-
table_nuclei = table_nuclei_temp
262-
else:
263-
table_asyn = pl.concat([table_asyn, table_asyn_temp], rechunk=True)
264-
table_nuclei = pl.concat(
265-
[table_nuclei, table_nuclei_temp], rechunk=True
237+
ync = pl.Series(
238+
"centroid-y", table_nuclei_temp["centroid-1"].to_numpy() + ypos
266239
)
240+
table_nuclei_temp = table_nuclei_temp.replace_column(0, xnc)
241+
table_nuclei_temp = table_nuclei_temp.replace_column(1, ync)
242+
243+
if save_figs == True:
244+
import matplotlib
245+
246+
matplotlib.use("Agg")
247+
fig, axs = self.plot_masks(
248+
IO.imread(tiffs[i]),
249+
np.dstack([image_mask_asyn, image_mask_nuclei]),
250+
)
251+
figname = (
252+
str(int(os.path.split(tiffs[i])[-1].split("_")[1]))
253+
+ "_"
254+
+ str(
255+
int(
256+
os.path.split(tiffs[i])[-1]
257+
.split("_")[-1]
258+
.split(".tiff")[0]
259+
)
260+
)
261+
+ "_segmentation_example.svg"
262+
)
263+
plt.savefig(
264+
os.path.join(fig_folder, figname), format="svg", dpi=600
265+
)
266+
267+
if isinstance(table_asyn_temp, list):
268+
table_asyn = table_asyn_temp
269+
else:
270+
table_asyn = pl.concat([table_asyn, table_asyn_temp], rechunk=True)
271+
272+
if isinstance(table_nuclei_temp, list):
273+
table_nuclei = table_nuclei_temp
274+
else:
275+
if len(table_nuclei_temp) > 0:
276+
table_nuclei = pl.concat(
277+
[table_nuclei, table_nuclei_temp], rechunk=True
278+
)
267279

268280
print(
269281
"Analysing tiffs; tiff {}/{} Time elapsed: {:.2f} minutes".format(
@@ -800,7 +812,7 @@ def analyse_DAB_and_cells(
800812
else:
801813
image_mask_asyn_raw = dab_image <= thresh
802814

803-
bin_blur = ski.filters.gaussian(image_mask_asyn_raw, sigma=2)
815+
bin_blur = ski.filters.gaussian(image_mask_asyn_raw, sigma=1)
804816
bin_blur[bin_blur > 0] = 1
805817
bin_blur = np.asarray(bin_blur, dtype=bool)
806818

84 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)