Skip to content

Commit

Permalink
Merge pull request #37 from samarthshetty09/dev
Browse files Browse the repository at this point in the history
step7, step8
  • Loading branch information
samarthshetty09 authored Jul 26, 2024
2 parents 55cfb26 + 4c530c2 commit bd8d002
Show file tree
Hide file tree
Showing 2 changed files with 141 additions and 62 deletions.
18 changes: 13 additions & 5 deletions step7.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def resolve_h5py_reference(data, f):
#Matmasks = mat['Matmasks']
cell_data = mat['cell_data']

print("Keys in MAT:", mat.keys())
# print("Keys in MAT:", mat.keys())

# =============================================================================
# with h5py.File(os.path.join(path, file_list[0]), 'r') as f:
Expand Down Expand Up @@ -88,13 +88,19 @@ def resolve_h5py_reference(data, f):
arr = [1]
# for iv in arr:
for iv in range(int(mat['no_obj'][0, 0])):
# iv = 0
indx_remov = []
final_indx_remov = []
# TODO! remove Hardcode range (check git changes)
# for its in range(241, 777): # check for 10 time points
for its in range(int(mat['cell_data'][0, iv]), int(mat['cell_data'][1, iv])):
for its in range(int(mat['cell_data'][0, iv])-1, int(mat['cell_data'][1, iv])):
# its = 240
M = Matmasks[its].T

# sio.savemat(os.path.join(sav_path, 'M2_py.mat'), {
# "M2_py": M2
# })

# =============================================================================
# plt.figure()
# plt.imshow(M, cmap='gray')
Expand Down Expand Up @@ -156,11 +162,13 @@ def resolve_h5py_reference(data, f):
indx_remov_inter = np.unique(indx_remov)
final_indx_remov = np.unique(indx_remov)
for itt1 in indx_remov_inter:
# itt1 = 1.0
dist_data = -1 * np.ones(len(Mask3))
for its1 in range(int(mat['cell_data'][0, iv]), int(art['cell_exists'][int(itt1)-1, 1])):
for its1 in range(int(mat['cell_data'][0, iv]) - 1, int(art['cell_exists'][int(itt1)-1, 1])):
# its1 = 240
if its1 >= art['cell_exists'][int(itt1)-1, 0]:
M6 = (Mask3[its1] == itt1)
M7 = (Matmasks[its1] == iv + 1)
M6 = (Mask3[its1] == itt1).T
M7 = (Matmasks[its1] == iv + 1).T
dist_data[its1] = np.sum(M6 * M7) / np.sum(M6)

if np.any(dist_data != -1):
Expand Down
185 changes: 128 additions & 57 deletions step8.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,32 +58,34 @@ def cal_allob1(ccel, TETC, rang):

for iv in range(ccel): # Adjusted to 1-based index
for its in rang:
if TETC[its] is not None: # Check if the array is not None and not empty
all_obj[iv, its] = np.sum(TETC[its] == iv) # Adjusted for 1-based index logic
if np.sum(TETC[its]) > 0: # Check if the array is not None and not empty
all_obj[iv, its] = np.sum(TETC[its] == iv + 1) # Adjusted for 1-based index logic
else:
all_obj[iv, its] = -1

return all_obj

# Function to process ART Tracks
def process_art_tracks(files, last_tp, end_inter, st_inter, suffix):
# file = ART1files[0]
# suffix = 'DS'
for file in files:
art_path = os.path.join(path, file)
data = load_mat(art_path)

Mask3 = []
with h5py.File(os.path.join(path, files[0]), 'r') as f:
with h5py.File(os.path.join(path, files[0]), 'r') as f: #files[0]
for i in range(len(f['Mask3'])):
masks_refs = f['Mask3'][i]
for ref in masks_refs:
mask = resolve_h5py_reference(ref, f)
Mask3.append(mask)

# Mask3 = np.array(Mask3)

no_obj = data['no_obj'][0, 0]


all_ob1 = cal_allob1(int(no_obj), Mask3, range(len(Mask3)))
# all_ob1 = cal_allob1(int(no_obj), Mask3, range(len(Mask3)))

# all_ob1 = binar(all_ob1)
# plt.figure()
Expand All @@ -92,18 +94,18 @@ def process_art_tracks(files, last_tp, end_inter, st_inter, suffix):
# plt.show()


endi = len(Mask3) - (last_tp - end_inter)
endi = len(Mask3) - (last_tp - end_inter) - 1
tr1 = Mask3[:st_inter-1]
tr2 = Mask3[st_inter:endi-1]
tr2 = Mask3[st_inter-1:endi]
tr3 = Mask3[endi:]

ground_truth = []
for i in range(len(tr2) - 1):
for i in range(len(tr2) - 2):
if i % 16 == 0:
ground_truth.append(tr2[i])

tr_final = tr1 + ground_truth + tr3
Mask3 = tr_final
Mask3 = tr_final.copy()

new_art_name = file.replace('.mat', f'_{suffix}.mat')

Expand All @@ -120,14 +122,14 @@ def process_art_tracks(files, last_tp, end_inter, st_inter, suffix):

for itt2 in range(all_ob.shape[0]):
if np.all(all_ob[itt2,:] == 0):
cell_artifacts.append(itt2)
cell_artifacts.append(itt2 + 1)
else:
cell_exists[0, itt2] = np.argmax(all_ob[itt2,:] > 0)
cell_exists[1, itt2] = len(all_ob[itt2, :]) - 1 - np.argmax((all_ob[itt2,:][::-1] > 0))
cell_exists[0, itt2] = np.argmax(all_ob[itt2,:] > 0) + 1
cell_exists[1, itt2] = len(all_ob[itt2, :]) - 1 - np.argmax((all_ob[itt2,:][::-1] > 0)) + 1

if cell_artifacts:
if len(cell_artifacts) > 0:
all_ccel = np.arange(1, no_obj + 1)
good_cells = np.setdiff1d(all_ccel, np.array(cell_artifacts) + 1)
good_cells = np.setdiff1d(all_ccel, np.array(cell_artifacts))
good_cells = np.sort(good_cells)

for iv in range(good_cells.size):
Expand All @@ -137,39 +139,62 @@ def process_art_tracks(files, last_tp, end_inter, st_inter, suffix):

no_obj = good_cells.size
all_ob = cal_allob1(no_obj, Mask3, range(len(Mask3)))


x_scale = 300
y_scale = 50
all_obj_new = all_ob
aspect_ratio = (x_scale / all_obj_new.shape[1]) / (y_scale / all_obj_new.shape[0])

# Display the image with the adjusted scales
plt.imshow(all_obj_new, extent=[0, x_scale, 0, y_scale], aspect='auto')
plt.colorbar()

# Set the ticks to reflect the scales accurately
plt.xticks(np.linspace(0, x_scale, num=5))
plt.yticks(np.linspace(0, y_scale, num=5))

plt.show()

sio.savemat(os.path.join(sav_path, "art_py.mat"), {
'all_ob_py': all_ob
})


cell_exists = np.zeros((2, all_ob.shape[0]))

for itt2 in range(all_ob.shape[0]):
cell_exists[0, itt2] = np.argmax(all_ob[itt2,:] > 0)
cell_exists[1, itt2] = len(all_ob[itt2, :]) - 1 - np.argmax(all_ob[itt2, ::-1] > 0)
cell_exists[0, itt2] = np.argmax(all_ob[itt2,:] > 0) + 1
cell_exists[1, itt2] = len(all_ob[itt2, :]) - 1 - np.argmax(all_ob[itt2, ::-1] > 0) + 1

#TODO! recreate remove cell artifacts once more to remove inturrupted time series.

cell_artifacts = []
cell_exists = np.zeros((2, all_ob.shape[0]))
# cell_artifacts = []
# cell_exists = np.zeros((2, all_ob.shape[0]))

for itt2 in range(all_ob.shape[0]):
if np.all(all_ob[itt2,:] == 0):
cell_artifacts.append(itt2)
else:
cell_exists[0, itt2] = np.argmax(all_ob[itt2,:] > 0)
cell_exists[1, itt2] = len(all_ob[itt2, :]) - 1 - np.argmax((all_ob[itt2,:][::-1] > 0))
# for itt2 in range(all_ob.shape[0]):
# if np.all(all_ob[itt2,:] == 0):
# cell_artifacts.append(itt2)
# else:
# cell_exists[0, itt2] = np.argmax(all_ob[itt2,:] > 0)
# cell_exists[1, itt2] = len(all_ob[itt2, :]) - 1 - np.argmax((all_ob[itt2,:][::-1] > 0))

if cell_artifacts:
all_ccel = np.arange(1, no_obj + 1)
good_cells = np.setdiff1d(all_ccel, np.array(cell_artifacts) + 1)
good_cells = np.sort(good_cells)
# if cell_artifacts:
# all_ccel = np.arange(1, no_obj + 1)
# good_cells = np.setdiff1d(all_ccel, np.array(cell_artifacts) + 1)
# good_cells = np.sort(good_cells)

for iv in range(good_cells.size):
for its in range(len(Mask3)):
pix = np.where(Mask3[its] == good_cells[iv])
Mask3[its][pix] = iv + 1
# for iv in range(good_cells.size):
# for its in range(len(Mask3)):
# pix = np.where(Mask3[its] == good_cells[iv])
# Mask3[its][pix] = iv + 1

no_obj = good_cells.size
all_ob = cal_allob1(no_obj, Mask3, range(len(Mask3)))
cell_exists = np.zeros((2, all_ob.shape[0]))
for itt2 in range(all_ob.shape[0]):
cell_exists[0, itt2] = np.argmax(all_ob[itt2,:] > 0)
cell_exists[1, itt2] = len(all_ob[itt2, :]) - 1 - np.argmax(all_ob[itt2, ::-1] > 0)
# no_obj = good_cells.size
# all_ob = cal_allob1(no_obj, Mask3, range(len(Mask3)))
# cell_exists = np.zeros((2, all_ob.shape[0]))
# for itt2 in range(all_ob.shape[0]):
# cell_exists[0, itt2] = np.argmax(all_ob[itt2,:] > 0)
# cell_exists[1, itt2] = len(all_ob[itt2, :]) - 1 - np.argmax(all_ob[itt2, ::-1] > 0)

#####

Expand All @@ -183,13 +208,18 @@ def process_art_tracks(files, last_tp, end_inter, st_inter, suffix):
'Mask3': Mask3, 'all_ob': all_ob, 'ccell2': data['ccell2'],
'cell_exists': cell_exists, 'no_obj': no_obj
}, do_compression=True)



# Process ART Tracks
process_art_tracks(ARTfiles, last_tp, end_inter, st_inter, 'DS')


process_art_tracks(ART1files, last_tp, end_inter, st_inter, 'DS')


# Function to process MAT Tracks
def process_mat_tracks(files, last_tp, end_inter, st_inter):
# file = MATfiles[0]
for file in files:
mat_path = os.path.join(path, file)
data = load_mat(mat_path)
Expand All @@ -202,17 +232,23 @@ def process_mat_tracks(files, last_tp, end_inter, st_inter):
mask = resolve_h5py_reference(ref, f)
Matmasks.append(mask)

# MATC = Matmasks.copy()
# for itx1 in range(1, last_tp_int + 1):
# if itx1 > len(Matmasks):
# MATC[itx1 - 1] = []

# Matmasks = MATC

no_obj = data['no_obj'][0, 0]
shock_period = data['shock_period']

endi = len(Matmasks) - (last_tp - end_inter)
endi = len(Matmasks) - (last_tp - end_inter) - 1
tr1 = Matmasks[:st_inter-1]
tr2 = Matmasks[st_inter:endi-1]
tr2 = Matmasks[st_inter-1:endi]
tr3 = Matmasks[endi:]

ground_truth = []
for i in range(len(tr2) - 1):
for i in range(len(tr2) - 2):
if i % 16 == 0:
ground_truth.append(tr2[i])

Expand All @@ -223,17 +259,36 @@ def process_mat_tracks(files, last_tp, end_inter, st_inter):

all_obj = cal_allob1(int(no_obj), Matmasks, range(len(Matmasks)))

# x_scale = 300
# y_scale = 1.5
# all_obj_new = all_obj
# aspect_ratio = (x_scale / all_obj_new.shape[1]) / (y_scale / all_obj_new.shape[0])

# # Display the image with the adjusted scales
# plt.imshow(all_obj_new, extent=[0, x_scale, 0, y_scale], aspect='auto')
# plt.colorbar()

# # Set the ticks to reflect the scales accurately
# plt.xticks(np.linspace(0, x_scale, num=5))
# plt.yticks(np.linspace(0, y_scale, num=5))

sio.savemat(os.path.join(sav_path, "mat_ob_py.mat"), {
'all_obj_py': all_obj
})



cell_artifacts = []
cell_data = np.zeros((all_obj.shape[0], 2))

for itt2 in range(all_obj.shape[0]):
if np.all(all_obj[itt2, :] == 0):
cell_artifacts.append(itt2)
cell_artifacts.append(itt2 + 1)
else:
cell_data[itt2, 0] = np.argmax(all_obj[itt2, :] > 0)
cell_data[itt2, 1] = len(all_obj[itt2, :]) - 1 - np.argmax(all_obj[itt2, ::-1] > 0)
cell_data[itt2, 0] = np.argmax(all_obj[itt2, :] > 0) + 1
cell_data[itt2, 1] = len(all_obj[itt2, :]) - 1 - np.argmax(all_obj[itt2, ::-1] > 0) + 1

if cell_artifacts:
if cell_artifacts.size>0:
all_ccel = np.arange(1, no_obj + 1)
good_cells = np.setdiff1d(all_ccel, cell_artifacts)
good_cells = np.sort(good_cells)
Expand All @@ -247,8 +302,8 @@ def process_mat_tracks(files, last_tp, end_inter, st_inter):
all_obj = cal_allob1(no_obj, Matmasks, range(len(Matmasks)))
cell_data = np.zeros((all_obj.shape[0], 2))
for itt2 in range(all_obj.shape[0]):
cell_data[itt2, 0] = np.argmax(all_obj[itt2, :] > 0)
cell_data[itt2, 1] = len(all_obj[itt2, :]) - 1 - np.argmax(all_obj[itt2, ::-1] > 0)
cell_data[itt2, 0] = np.argmax(all_obj[itt2, :] > 0) + 1
cell_data[itt2, 1] = len(all_obj[itt2, :]) - 1 - np.argmax(all_obj[itt2, ::-1] > 0) + 1

sio.savemat(os.path.join(sav_path, new_mat_name), {
'Matmasks': Matmasks, 'cell_data': cell_data, 'no_obj': no_obj,
Expand All @@ -260,6 +315,7 @@ def process_mat_tracks(files, last_tp, end_inter, st_inter):

# Function to process TET Tracks
def process_tet_tracks(files, last_tp, end_inter, st_inter):
# file = TETfiles[0]
for file in files:
tet_path = os.path.join(path, file)
data = load_mat(tet_path)
Expand All @@ -282,13 +338,13 @@ def process_tet_tracks(files, last_tp, end_inter, st_inter):
TETC.append([])
TETmasks = TETC

endi = len(TETmasks) - (last_tp - end_inter)
endi = len(TETmasks) - (last_tp - end_inter) - 1
tr1 = TETmasks[:st_inter-1]
tr2 = TETmasks[st_inter:endi-1]
tr2 = TETmasks[st_inter-1:endi]
tr3 = TETmasks[endi:]

ground_truth = []
for i in range(len(tr2) - 1):
for i in range(len(tr2) - 2):
if i % 16 == 0:
ground_truth.append(tr2[i])

Expand All @@ -302,14 +358,29 @@ def process_tet_tracks(files, last_tp, end_inter, st_inter):
cell_artifacts = []
TET_exists = np.zeros((TET_Size.shape[0], 2))

# x_scale = 300
# y_scale = 1.5
# all_obj_new = TET_Size
# aspect_ratio = (x_scale / all_obj_new.shape[1]) / (y_scale / all_obj_new.shape[0])

# # Display the image with the adjusted scales
# plt.imshow(all_obj_new, extent=[0, x_scale, 0, y_scale], aspect='auto')
# plt.colorbar()

# # Set the ticks to reflect the scales accurately
# plt.xticks(np.linspace(0, x_scale, num=5))
# plt.yticks(np.linspace(0, y_scale, num=5))

# plt.show()

for itt2 in range(TET_Size.shape[0]):
if np.all(TET_Size[itt2, :] == 0):
cell_artifacts.append(itt2)
cell_artifacts.append(itt2 + 1)
else:
TET_exists[itt2, 0] = np.argmax(TET_Size[itt2, :] > 0)
TET_exists[itt2, 1] = len(TET_Size[itt2, :]) - 1 - np.argmax(TET_Size[itt2, ::-1] > 0)
TET_exists[itt2, 0] = np.argmax(TET_Size[itt2, :] > 0) + 1
TET_exists[itt2, 1] = len(TET_Size[itt2, :]) - 1 - np.argmax(TET_Size[itt2, ::-1] > 0) + 1

if cell_artifacts:
if cell_artifacts.size > 0:
all_ccel = np.arange(1, TET_obj + 1)
good_cells = np.setdiff1d(all_ccel, cell_artifacts)
good_cells = np.sort(good_cells)
Expand All @@ -323,8 +394,8 @@ def process_tet_tracks(files, last_tp, end_inter, st_inter):
TET_Size = cal_allob1(TET_obj, TETmasks, range(len(TETmasks)))
TET_exists = np.zeros((TET_Size.shape[0], 2))
for itt2 in range(TET_Size.shape[0]):
TET_exists[itt2, 0] = np.argmax(TET_Size[itt2, :] > 0)
TET_exists[itt2, 1] = len(TET_Size[itt2, :]) - 1 - np.argmax(TET_Size[itt2, ::-1] > 0)
TET_exists[itt2, 0] = np.argmax(TET_Size[itt2, :] > 0) + 1
TET_exists[itt2, 1] = len(TET_Size[itt2, :]) - 1 - np.argmax(TET_Size[itt2, ::-1] > 0) + 1

sio.savemat(os.path.join(sav_path, new_tet_name), {
'TETmasks': TETmasks, 'shock_period': shock_period, 'TET_exists': TET_exists,
Expand Down

0 comments on commit bd8d002

Please sign in to comment.