-
Notifications
You must be signed in to change notification settings - Fork 0
/
step5.py
474 lines (369 loc) · 13.1 KB
/
step5.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Mon Jul 8 19:17:47 2024
@author: samarth
"""
import numpy as np
from skimage import io, morphology
from skimage.morphology import thin, disk, binary_opening, dilation, opening
from scipy.ndimage import binary_opening, binary_dilation
from scipy.io import savemat
import os
from glob import glob
import time
import cv2
import matplotlib.pyplot as plt
from skimage import img_as_uint
from scipy import stats
import scipy.io as sio
import h5py
Arti_v = 11
cell_prob = 0.5
flow_threshold = 0.9
disk_size = 3
def OAM_231216_bina(IS1):
IS1B = IS1.copy()
IS1B[IS1 != 0] = 1
return IS1B
def OAM_230919_remove_artif(I2A,disk_size): # I2A = IS2 % disk radius is 3 for ~500x~1000, 6 for larger images
# we need a function to define the disk size base in the average cell size
I2AA=np.copy(I2A) # plt.imshow(IS2)
# Applying logical operation and morphological opening
I2A1 =OAM_231216_bina(I2A);#binar(I2A) plt.imshow(I2A1) plt.imshow(I2A)
# Create a disk-shaped structuring element with radius 3
selem = disk(disk_size)
# Perform morphological opening
I2B = opening(I2A1, selem)
# Morphological dilation plt.imshow(I2B)
I2C = dilation(I2B, disk(disk_size)) # Adjust the disk size as needed
I3 = I2AA * I2C # plt.imshow(I3)
# Extract unique objects
objs = np.unique(I3)
objs = objs[1:len(objs)]
# Initialize an image of zeros with the same size as I2A
I4 = np.uint16(np.zeros((I3.shape[0], I3.shape[1])))
# Mapping the original image values where they match the unique objects
AZ=1
for obj in objs:
I4[I2A == obj] = AZ
AZ=AZ+1
return I4
def OAM_23121_tp3(M, cel, no_obj1, A):
tp3 = M.copy()
tp3[tp3 == cel] = no_obj1 + A
return tp3
pos = 'Pos0_2'
# path to the segmented tet masks
path = '/Users/samarth/Documents/MATLAB/Full_Life_Cycle_tracking/Python_Track_Test/Pro/'
sav_path = '/Users/samarth/Documents/MATLAB/Full_Life_Cycle_tracking/saved_res/py_res/'
art_mask_path = path
file_list = sorted(glob(os.path.join(art_mask_path, '*_Ph3_000_cp_masks.tif')))
# file_list = file_list[:25]
mm = range(len(file_list)) # time points to track
# Load the first mask that begins the indexing for all the cells; IS1 is updated to most recently processed tracked mask at the end of it0
# Start tracking at first timepoint
IS1 = io.imread(file_list[0]).astype(np.uint16)
# Remove artifacts and start tracking at first timepoint
IS1 = OAM_230919_remove_artif(IS1, disk_size)
# plt.imshow(IS1)
# Contains the re-labeled masks according to labels in the last tp mask
masks = np.zeros((IS1.shape[0], IS1.shape[1], len(mm)), dtype=np.uint16)
# First timepoint defines indexing; IS1 is first segmentation output
masks[:, :, mm[0]] = IS1
# Allocate a mask for cells where there's a gap in the segmentation; IblankG is updated within the loops it1 & itG
IblankG = np.zeros_like(IS1)
tic = time.time()
for it0 in mm:
print(f'it0={it0}')
IS2 = io.imread(file_list[it0]).astype(np.uint16)
IS22 = IS2.copy()
IS2 = OAM_230919_remove_artif(IS2, disk_size) # Remove artifacts
IS2C = IS2.copy()
IS1B = OAM_231216_bina(IS1)
IS3 = IS1B.astype(np.uint16) * IS2
tr_cells = np.unique(IS1[IS1 != 0])
gap_cells = np.unique(IblankG[IblankG != 0])
cells_tr = np.concatenate([tr_cells, gap_cells])
# Allocate space for the re-indexed IS2 according to tracking
Iblank0 = np.zeros_like(IS1)
if cells_tr.sum() != 0: # This is required in case the mask goes blank because cells mate immediately during germination
for it1 in sorted(cells_tr):
IS5 = (IS1 == it1)
IS5A = IS5.copy()
# plt.imshow(IS5A)
IS6AA = thin(IS5A, 1).astype(np.uint16)
IS6A = np.multiply(IS6AA, IS3)
if IS5A.sum() == 0: # If the cell was missing in past mask; look at the gaps in segmentation otherwise continue to look at the past mask
IS5A = (IblankG == it1)
IS5AA = IS5A.copy()
# plt.imshow(IS5A)
IS5AB = thin(IS5AA, 1).astype(np.uint16)
IS6A = np.multiply(IS5AB, IS2C)
# Remove the cell from segmentation gap mask - it'll be in the updated past mask for next round of processing
IblankG[IblankG == it1] = 0
if IS6A.sum() != 0:
IS2ind = np.uint16((np.bincount(IS6A[IS6A != 0]).argmax()))
# is2ind_arr.append(IS2ind);
Iblank0[IS2 == IS2ind] = it1
# plt.imshow(Iblank0)
# print(sum(sum(Iblank0)))
IS3[IS3 == IS2ind] = 0
# plt.imshow(IS3)
IS2C[IS2 == IS2ind] = 0
seg_gap = np.setdiff1d(tr_cells, np.unique(Iblank0))
# seg_gap = seg_gap[1:]
if seg_gap.size > 0:
for itG in seg_gap:
# itG = 16
IblankG[IS1 == itG] = itG
# plt.imshow(IblankG)
Iblank0B = Iblank0.copy()
Iblank0B[Iblank0 != 0] = 1
# plt.imshow(Iblank0B)
ISB = IS2 * (1 - Iblank0B).astype(np.uint16)
# plt.imshow(ISB)
newcells = np.unique(ISB[ISB != 0])
# newcells = newcells[1:]
Iblank = Iblank0.copy()
A = 1
if newcells.size > 0:
for it2 in newcells:
Iblank[IS2 == it2] = max(cells_tr) + A
A += 1
# plt.imshow(Iblank);
masks[:, :, mm[it0]] = Iblank.astype(np.uint16)
# plt.imshow(masks[:, :, mm[it0]])
IS1 = masks[:, :, mm[it0]]
else:
masks[:, :, mm[it0]] = IS2
IS1 = IS2
toc = time.time()
print(f'Elapsed time is {toc - tic} seconds.')
"""
Vizualize All Ob
"""
# obj = np.unique(masks)
# no_obj = int(np.max(obj))
# im_no = masks.shape[2]
# all_ob = np.zeros((no_obj, im_no))
# tic = time.time()
# for ccell in range(1, no_obj + 1):
# Maa = (masks == ccell)
# for i in range(im_no):
# pix = np.sum(Maa[:, :, i])
# all_ob[ccell-1, i] = pix
# plt.figure()
# plt.imshow(all_ob, aspect='auto', cmap='viridis')
# plt.title("all_obj")
# plt.xlabel("Time")
# plt.ylabel("Cells")
# plt.show()
"""
Tracks as a tensor
"""
im_no = masks.shape[2]
# Find all unique non-zero cell identifiers across all time points
ccell2 = np.unique(masks[masks != 0])
# Initialize Mask2 with zeros of the same shape as masks
Mask2 = np.zeros((masks.shape[0], masks.shape[1], masks.shape[2]))
# TODO: instead of np use cpypy
# Process each unique cell ID
for itt3 in range(len(ccell2)): # cells
pix3 = np.where(masks == ccell2[itt3])
Mask2[pix3] = itt3 + 1 # ID starts from 1
"""
Get Cell Presence
"""
# Get cell presence
Mask3 = Mask2.copy()
numbM = im_no
obj = np.unique(Mask3)
no_obj1 = int(obj.max())
A = 1
tp_im = np.zeros((no_obj1, im_no))
for cel in range(1, no_obj1+1):
Ma = (Mask3 == cel)
for ih in range(numbM):
if Ma[:, :, ih].sum() != 0:
tp_im[cel-1, ih] = 1
# plt.figure()
# plt.imshow(tp_im, aspect='auto')
# plt.title("Cell Presence Over Time")
# plt.xlabel("Time")
# plt.ylabel("Cells")
# plt.show()
"""
Split Inturrupted time series
"""
tic = time.time()
for cel in range(1, no_obj1+1):
tp_im2 = np.diff(tp_im[cel-1, :])
tp1 = np.where(tp_im2 == 1)[0]
tp2 = np.where(tp_im2 == -1)[0]
maxp = (Mask3[:, :, numbM - 1] == cel).sum()
if len(tp1) == 1 and len(tp2) == 1 and maxp != 0: # has one interruption
for itx in range(tp1[0], numbM):
tp3 = OAM_23121_tp3(Mask3[:, :, itx], cel, no_obj1, A)
Mask3[:, :, itx] = tp3.copy()
no_obj1 += A
elif len(tp1) == 1 and len(tp2) == 1 and maxp == 0: # has one interruption
pass
elif len(tp1) == len(tp2) + 1 and maxp != 0:
tp2 = np.append(tp2, numbM-1)
for itb in range(1, len(tp1)): # starts at 2 because the first cell index remains unchanged
for itx in range(tp1[itb] + 1, tp2[itb] + 1):
tp3 = OAM_23121_tp3(Mask3[:, :, itx], cel, no_obj1, A)
Mask3[:, :, itx] = tp3.copy()
no_obj1 += A
elif len(tp2) == 0 or len(tp1) == 0: # it's a normal cell, it's born and stays until the end
pass
elif len(tp1) == len(tp2):
if tp1[0] > tp2[0]:
tp2 = np.append(tp2, numbM-1) #check this throughly
for itb in range(len(tp1)):
for itx in range(tp1[itb]+1, tp2[itb + 1] + 1):
tp3 = OAM_23121_tp3(Mask3[:, :, itx], cel, no_obj1, A) #+1 here
Mask3[:, :, itx] = tp3.copy()
no_obj1 += A
elif tp1[0] < tp2[0]:
for itb in range(1, len(tp1)):
for itx in range(tp1[itb] + 1, tp2[itb] + 1): # Inclusive range
tp3 = OAM_23121_tp3(Mask3[:, :, itx], cel, no_obj1, A)
Mask3[:, :, itx] = tp3.copy()
no_obj1 += A
elif len(tp2) > 1:
for itb in range(1, len(tp1)):
for itx in range(tp1[itb] + 1, tp2[itb] + 1):
tp3 = OAM_23121_tp3(Mask3[:, :, itx], cel, no_obj1, A)
Mask3[:, :, itx] = tp3.copy()
no_obj1 += A
toc = time.time()
print(f'Elapsed time is {toc - tic} seconds.')
for it4 in range(len(file_list)):
plt.imshow(Mask3[:, :, it4])
numbM = im_no
obj = np.unique(Mask3)
# Get cell presence 2
tp_im = np.zeros((int(max(obj)), im_no))
for cel in range(1, int(max(obj)) + 1):
Ma = (Mask3 == cel)
for ih in range(numbM):
if Ma[:, :, ih].sum() != 0:
tp_im[cel-1, ih] = 1
# plt.figure()
# plt.imshow(tp_im, aspect='auto')
# plt.title("Cell Presence Over Time")
# plt.xlabel("Time")
# plt.ylabel("Cells")
# plt.show()
# Get good cells
cell_artifacts = np.zeros(tp_im.shape[0])
for it05 in range(tp_im.shape[0]):
arti = np.where(np.diff(tp_im[it05, :]) == -1)[0] # Find artifacts in the time series
if arti.size > 0:
cell_artifacts[it05] = it05 + 1 # Mark cells with artifacts
goodcells = np.setdiff1d(np.arange(1, tp_im.shape[0] + 1), cell_artifacts[cell_artifacts != 0]) # Identify good cells
# display tp_im
plt.figure()
plt.imshow(tp_im, aspect='auto')
plt.title("Cell Presence Over Time")
plt.xlabel("Time")
plt.ylabel("Cells")
plt.show()
# Tracks as a tensor 2
im_no = Mask3.shape[2]
Mask4 = np.zeros((masks.shape[0], masks.shape[1], masks.shape[2]))
for itt3 in range(goodcells.size):
pix3 = np.where(Mask3 == goodcells[itt3])
Mask4[pix3] = itt3 + 1 # IDs start from 1
# Get cell presence 3
Mask5 = Mask4.copy()
numbM = im_no
obj = np.unique(Mask4)
no_obj1 = int(obj.max())
A = 1
tp_im = np.zeros((no_obj1, im_no))
for cel in range(1, no_obj1+1):
Ma = (Mask5 == cel)
for ih in range(numbM):
if Ma[:, :, ih].sum() != 0:
tp_im[cel-1, ih] = 1
plt.figure()
plt.imshow(tp_im, aspect='auto')
plt.title("Cell Presence Over Time")
plt.xlabel("Time")
plt.ylabel("Cells")
plt.show()
cell_exists0 = np.zeros((2, tp_im.shape[0]))
######
for itt2 in range(tp_im.shape[0]):
# Find indices of non-zero elements
non_zero_indices = np.where(tp_im[itt2, :] != 0)[0]
# If there are non-zero elements, get first and last
if non_zero_indices.size > 0:
first_non_zero = non_zero_indices[0]
last_non_zero = non_zero_indices[-1]
else:
first_non_zero = -1 # Or any placeholder value for rows without non-zero elements
last_non_zero = -1 # Or any placeholder value for rows without non-zero elements
cell_exists0[:, itt2] = [first_non_zero, last_non_zero]
sortOrder = sorted(range(cell_exists0.shape[1]), key=lambda i: cell_exists0[0, i])
########
# Reorder the array based on the sorted indices
cell_exists = cell_exists0[:, sortOrder]
# Re-label
Mask6 = np.zeros_like(Mask5)
for itt3 in range(len(sortOrder)):
pix3 = np.where(Mask5 == sortOrder[itt3] + 1) # here
Mask6[pix3] = itt3 + 1# reassign
# Get cell presence 3
Mask7 = Mask6.copy()
numbM = im_no
obj = np.unique(Mask6)
no_obj1 = int(obj.max())
A = 1
tic = time.time()
tp_im = np.zeros((no_obj1, im_no))
for cel in range(1, no_obj1 + 1):
tp_im[cel - 1, :] = ((Mask7 == cel).sum(axis=(0, 1)) != 0).astype(int)
toc = time.time()
print(f'Elapsed time is {toc - tic} seconds.')
plt.figure()
plt.imshow(tp_im, aspect='auto')
plt.title("Cell Presence Over Time")
plt.xlabel("Time")
plt.ylabel("Cells")
plt.show()
# Calculate size
obj = np.unique(Mask7)
no_obj = int(np.max(obj))
im_no = Mask7.shape[2]
all_ob = np.zeros((no_obj, im_no))
tic = time.time()
for ccell in range(1, no_obj + 1):
Maa = (Mask7 == ccell)
for i in range(im_no):
pix = np.sum(Maa[:, :, i])
all_ob[ccell-1, i] = pix
toc = time.time()
print(f'Elapsed time is {toc - tic} seconds.')
plt.figure()
plt.imshow(all_ob, aspect='auto', cmap='viridis')
plt.title("Cell Sizes Over Time")
plt.xlabel("Time")
plt.ylabel("Cells")
plt.show()
save_path = os.path.join(sav_path, f'ART_Track0_{im_no}.mat')
savemat(save_path, {
'all_ob_py': all_ob,
'Mask7_py': Mask7,
'no_obj_py': no_obj,
'im_no_py': im_no,
'ccell2_py': ccell2,
'cell_exists': cell_exists,
'cell_prob': cell_prob,
'Arti_v': Arti_v,
'flow_threshold': flow_threshold
})
print(pos)