-
Notifications
You must be signed in to change notification settings - Fork 0
/
step11b.py
225 lines (189 loc) · 7.71 KB
/
step11b.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Wed Jun 12 11:08:51 2024
@author: samarth
"""
import os
import numpy as np
import scipy.io as sio
import h5py
from skimage.transform import resize
import matplotlib.pyplot as plt
pos = 'Pos0_2'
path = '/Users/samarth/Documents/MATLAB/Full_Life_Cycle_tracking/Tracks2/'
sav_path = '/Users/samarth/Documents/MirandaLabs/tracking_algo/FungAi_tracking/Tracking_toydata_Tracks'
def load_mat(filename):
try:
return sio.loadmat(filename)
except NotImplementedError:
# Load using h5py for MATLAB v7.3 files
data = {}
with h5py.File(filename, 'r') as f:
for key in f.keys():
data[key] = np.array(f[key])
return data
def resolve_h5py_reference(data, f):
if isinstance(data, h5py.Reference):
return f[data][()]
return data
# Load tet track
tet_track_path = path
file_list_tet = [f for f in os.listdir(tet_track_path) if '_TET_Track_DS' in f]
file_list_tet = sorted(file_list_tet)
tet_track = load_mat(os.path.join(path, file_list_tet[0]))
# Load tet IDs
file_list = [f for f in os.listdir(tet_track_path) if '_TET_ID' in f]
file_list = sorted(file_list)
tet_ids = load_mat(os.path.join(path, file_list[0]))
# Load descendants information
desc_path = path
file_list_desc = [f for f in os.listdir(desc_path) if '_descendants_new_art' in f]
file_list_desc = sorted(file_list_desc)
descendants = load_mat(os.path.join(path, file_list_desc[0]))
# Load mat track
mat_track_path = path
file_list = [f for f in os.listdir(mat_track_path) if '_MAT_16_18_Track1_DS' in f]
file_list = sorted(file_list)
mat_track = load_mat(os.path.join(path, file_list[0]))
masks = tet_track['TETmasks']
TET_obj = tet_track['TET_obj']
TET_exists = tet_track['TET_exists']
alive_tets = descendants['alive_tets']
descendants_data = descendants["descendants_data"]
MTrack = mat_track['Matmasks']
shock_period = tet_track['shock_period']
no_obj = mat_track['no_obj']
I3 = descendants['I3']
cell_data = mat_track['cell_data']
with h5py.File(os.path.join(path, file_list[0]), 'r') as f:
masks = [resolve_h5py_reference(mask, f) for mask in masks]
MTrack = [resolve_h5py_reference(mtrack, f) for mtrack in MTrack]
descendants_data = [resolve_h5py_reference(desc, f) for desc in descendants_data]
shock_period = resolve_h5py_reference(shock_period, f)
no_obj = resolve_h5py_reference(no_obj, f)
I3 = resolve_h5py_reference(I3, f)
cell_data = resolve_h5py_reference(cell_data, f)
#resolving h5py
resolved_masks = []
with h5py.File(os.path.join(path, file_list_tet[0]), 'r') as f:
for i in range(len(f['TETmasks'])):
tet_masks_refs = f['TETmasks'][i]
for ref in tet_masks_refs:
mask = resolve_h5py_reference(ref, f)
resolved_masks.append(mask)
MTrack_res = []
with h5py.File(os.path.join(path, file_list[0]), 'r') as f:
for i in range(len(f['Matmasks'])):
tet_masks_refs = f['Matmasks'][i]
for ref in tet_masks_refs:
mask = resolve_h5py_reference(ref, f)
MTrack_res.append(mask)
resolved_desc = []
with h5py.File(os.path.join(path, file_list_desc[0]), 'r') as f:
for i in range(len(f['descendants_data'])):
tet_masks_refs = f['descendants_data'][i]
for ref in tet_masks_refs:
desc = resolve_h5py_reference(ref, f)
resolved_desc.append(desc)
masks = resolved_masks
MTrack = MTrack_res
descendants_data = resolved_desc
if no_obj != 0: # positive number of MAT Detections
masks = masks
MTrack = MTrack
shock_period = shock_period.T
cell_data = cell_data.T
for i in range(len(masks)):
if masks[i].size > 2:
masks[i] = resize(masks[i].astype(np.float64), I3.shape, order=0, preserve_range=True, anti_aliasing=False)
start = shock_period[0, 1] + 1
tp_end = len(MTrack)
int_range = range(int(min(cell_data[:, 0])), int(max(cell_data[:, 1])))
region = []
amt = []
k = 0
# TODO! remove hardcode for int_range check git changes before committing
# arr = [1]
# for iv in arr:
for iv in range(int(no_obj)):
I12 = np.zeros(MTrack[min(int_range)].shape, dtype=np.uint16)
kx = 0
for its in int_range:
I11 = (MTrack[its] == iv+1).astype(np.uint16)
# plt.figure()
# plt.imshow(I11, cmap='grey')
# plt.show()
if np.sum(I11) > 0:
kx += 1
if 1 <= kx <= 2:
I11 *= 1000
I12 += I11
I13 = (I12 > 0).astype(np.uint16) * I3.astype(np.uint16)
plt.figure()
plt.imshow(I13, cmap='grey')
plt.show()
I13 = I13.T
pix = np.unique(I13)
pix = pix[pix != 0]
if pix.size != 0:
amt_iv = []
for p in pix:
amt_iv.append(np.sum(I13 == p))
amt.append(amt_iv)
k += 1
val, ind = max((v, i) for i, v in enumerate(amt_iv))
region.append([iv, pix[ind]])
unique_regions = np.unique(np.array(region)[:, 1])
cell_arrays = [None] * len(unique_regions)
for i, current_region in enumerate(unique_regions):
cells_in_current_region = np.array(region)[np.array(region)[:, 1] == current_region, 1]
cell_arrays[int(current_region)-1] = cells_in_current_region.astype(np.uint16)
if alive_tets.size != 0:
TET_ind = np.zeros((len(unique_regions), 3))
common_indices = [] * TET_obj
amt_1 = []
for iv in range(int(TET_obj)):
if tet_ids["TET_ID"][0, iv] != -1:
if iv in alive_tets:
if TET_exists[iv, 1] >= shock_period[0, 1] + 1:
T1 = (masks[0, shock_period[0, 1] + 1] == iv).astype(np.uint16)
else:
T1 = (masks[0, TET_exists[iv, 1]] == iv).astype(np.uint16)
plt.figure()
plt.imshow(T1)
plt.show()
T2 = I3.astype(np.uint16) * T1
TET_ind[iv, 0] = iv
TET_ind[iv, 2] = tet_ids["TET_ID"][0, iv]
pix = np.unique(T2)
pix = pix[pix != 0]
if pix.size != 0:
amt_1_iv = []
for p in pix:
amt_1_iv.append(np.sum(T2 == p))
amt_1.append(amt_1_iv)
val, ind = max((v, i) for i, v in enumerate(amt_1_iv))
TET_ind[iv, 1] = pix[ind]
for ixx in range(len(cell_arrays)):
if cell_arrays[ixx] is not None:
tet_no = np.where(TET_ind[:,1] == ixx)[0]
descendants_data.append(cell_arrays[ixx][tet_no])
for i in range(len(descendants_data)):
for j in range(len(descendants_data[i])):
if isinstance(descendants_data[i][j], np.ndarray):
descendants_data[i][j] = descendants_data[i][j].tolist()
for i, element in enumerate(descendants_data):
if not isinstance(element, np.ndarray):
raise TypeError(f"Element at index {i} is not a numpy array")
# Convert list of numpy arrays to a numpy array with dtype=object
descendants_data_array = np.empty(len(descendants_data), dtype=object)
for i, array in enumerate(descendants_data):
descendants_data_array[i] = array
savemat_data = {
'I3': I3,
'descendants_data': descendants_data_array,
'alive_tets': alive_tets,
'TET_obj': TET_obj,
}
sio.savemat(os.path.join(sav_path, f'{pos}_final_descendants.mat'), savemat_data, do_compression=True, oned_as='row')