From d83349efcbdcfda1f0db08c16da012da5c608a2c Mon Sep 17 00:00:00 2001 From: Samarth Shetty Date: Wed, 31 Jul 2024 18:13:47 -0400 Subject: [PATCH] step3 nan correction --- step3.py | 169 ++++++++++++++++--------------------------------------- 1 file changed, 48 insertions(+), 121 deletions(-) diff --git a/step3.py b/step3.py index afe58ca..43b58fa 100644 --- a/step3.py +++ b/step3.py @@ -5,6 +5,7 @@ @author: samarth """ import os +import math import numpy as np import scipy.io as sio from skimage.io import imread @@ -17,13 +18,14 @@ # Define thresholds and parameters pos = 'Pos0_2' -path = f'/Users/samarth/Documents/MATLAB/Full_Life_Cycle_tracking/Python_Track_Test/MAT/' +path = f'/Users/samarth/Documents/MATLAB/Full_Life_Cycle_tracking/Python_Track_Test/Pos32_1_B/' +# path = f'/Users/samarth/Documents/MATLAB/Full_Life_Cycle_tracking/Python_Track_Test/MAT/' sav_path = '/Users/samarth/Documents/MATLAB/Full_Life_Cycle_tracking/saved_res/py_res/' shock_period = [122, 134] # Load image file names -file_names = [f for f in os.listdir(path) if f.endswith('_Ph3_000_cp_masks.tif')] -file_numbers = [int(f.split('img_')[1].split('_Ph3_000_cp_masks.tif')[0]) for f in file_names] +file_names = [f for f in os.listdir(path) if f.endswith('_Ph3_000_MAT16_masks.tif')] +file_numbers = [int(f.split('img_')[1].split('_Ph3_000_MAT16_masks.tif')[0]) for f in file_names] sorted_indices = np.argsort(file_numbers) sorted_numbers = np.array(file_numbers)[sorted_indices] @@ -40,14 +42,14 @@ # Remove shock-induced timepoints mat_masks_original = mat_masks.copy() -# for start, end in [shock_period]: -# for i in range(start, end + 1): -# mat_masks[i] = None +for start, end in [shock_period]: + for i in range(start-1, end): + mat_masks[i] = None start = -1 for its in range(len(mat_masks)): # if mat_masks[its] is not None and np.sum(mat_masks[its]) > 0: - if np.sum(mat_masks[its]) > 0: + if mat_masks[its] is not None and np.sum(mat_masks[its]) > 0: start = its break @@ -79,161 +81,82 @@ while xx != -1: for im_no in rang2: - #I2 = mat_masks[im_no] if ccel == 1 else MATC[1][im_no] + if ccel == 1: I2 = mat_masks[im_no] else: I2 = MATC[1][im_no] - if I2 is None: + if I2 is None or I2.size == 0: continue - - - # plt.figure() - # plt.imshow(np.uint16(I2), cmap='gray') - # plt.title('I2') - # plt.show() - - # if(im_no < 280): - # print(im_no, min(rang2)) if im_no == min(rang2): - #TODO! ind1 = np.unique(I2)[1:] # Exclude background I3 = (I2 == ind1[0]) I3A = I3.copy() - # plt.figure() - # plt.imshow(np.uint16(I3A), cmap='gray') - # plt.title('I3A') - # plt.show() else: - I3A = IS6.copy() - # plt.figure() - # plt.imshow(np.uint16(I3A), cmap='gray') - # plt.title('I3A') - # plt.show() + I3A = np.copy(IS6) - - - I3A = skeletonize(I3A) + I3A = skeletonize(I3A > 0) I2A = np.copy(I2) - I3B = np.uint16(I3A) * np.uint16(I2A) - - - # plt.figure() - # plt.imshow(np.uint16(I3A), cmap='gray') - # plt.title('I3A') - # plt.show() - - - # plt.figure() - # plt.imshow(np.uint16(I3B), cmap='gray') - # plt.title('I3B') - # plt.show() - + I3B = I3A.astype(np.uint16) * I2A.astype(np.uint16) ind = mode(I3B[I3B != 0])[0] -# ============================================================================= -# if I3B[I3B != 0].size == 0: -# if ccel == 1: -# MATC[0][im_no] = I3B -# MATC[1][im_no] = I2A -# continue -# -# ind = np.argmax(np.bincount(I3B[I3B != 0])) -# ============================================================================= - - - if ind == 0 and ccel == 1: - MATC[0][im_no] = I3B.copy() - MATC[1][im_no] = I2A.copy() + if (ind == 0 or math.isnan(ind)) and ccel == 1: + MATC[0][im_no] = I3B + MATC[1][im_no] = I2A continue - elif ind == 0 and ccel != 1: + elif (ind == 0 or math.isnan(ind)) and ccel != 1: continue pix = np.where(I2A == ind) pix0 = np.where(I2A != ind) - # pix = np.flatnonzero(I2A == ind) - # pix0 = np.flatnonzero(I2A != ind) - - # I2A.flat[pix] = ccel - # I2A.flat[pix0] = 0 - # IS6 = I2A.copy() - # I22 = np.zeros_like(I2) - # pix1 = np.flatnonzero(IS6 == ccel) - # I2.flat[pix1] = 0 - # pix2 = np.unique(I2) - # pix2 = pix2[1:] - I2A[pix] = ccel I2A[pix0] = 0 - IS6 = I2A.copy() - - """ - plt.figure() - plt.imshow(np.uint16(IS6), cmap='gray') - plt.title('IS6') - plt.show() - """ - + IS6 = np.copy(I2A) + I22 = np.zeros_like(I2) pix1 = np.where(IS6 == ccel) I2[pix1] = 0 - # #TODO! - pix2 = np.unique(I2)[1:] # Exclude background + pix2 = np.unique(I2) + pix2 = pix2[1:] # Exclude background if ccel == 1: - for ity in range(len(pix2)): - pix4 = np.where(I2 == pix2[ity]) + # for ity in range(len(pix2)): + # pix4 = np.where(I2 == pix2[ity]) + # I22[pix4] = ity + 1' + for ity, p2 in enumerate(pix2): + pix4 = np.where(I2 == p2) I22[pix4] = ity + 1 - MATC[0][im_no] = IS6.copy() + MATC[0][im_no] = np.copy(IS6) else: - if pix2.size > 0: - for ity in pix2: - pix4 = np.where(I2 == ity) - I22[pix4] = ity + if len(pix2) > 0: + # for ity in range(len(pix2)): + # pix4 = np.where(I2 == pix2[ity]) + # I22[pix4] = ity + 1 + for ity, p2 in enumerate(pix2): + pix4 = np.where(I2 == p2) + I22[pix4] = ity + 1 else: - I22 = I2 - IS61 = (MATC[0][im_no]).copy() + I22 = I2.copy() + IS61 = np.copy(MATC[0][im_no]) IS61[pix] = ccel - MATC[0][im_no] = np.uint16(IS61.copy()) - - MATC[1][im_no] = I22.copy() - # if ccel == 1: - # for ity in range(len(pix2)): - # pix4 = np.flatnonzero(I2 == pix2[ity]) - # I22.flat[pix4] = ity + 1 # Adjust for 0-based indexing in Python - # MATC[0][im_no - 1] = IS6.copy() - # else: - # if len(pix2) > 0: - # for ity in range(len(pix2)): - # pix4 = np.flatnonzero(I2 == pix2[ity]) - # I22.flat[pix4] = ity + 1 # Adjust for 0-based indexing in Python - # else: - # I22 = I2 - # IS61 = (MATC[0][im_no - 1]).copy() - # IS61.flat[pix] = ccel - # MATC[0][im_no - 1] = (IS61.copy()).astype(np.uint16) - # MATC[1][im_no - 1] = I22.copy() - - - # plt.figure() - # plt.imshow(np.uint16(IS6), cmap='gray') - # plt.title('IS6') - # plt.show() - + MATC[0][im_no] = IS61.astype(np.uint16) + MATC[1][im_no] = np.copy(I22) + xx = -1 for i in rang: - if MATC[1][i] is not None and np.sum(MATC[1][i]) > 0: + if MATC[1][i] is not None and MATC[1][i].size > 0 and np.sum(MATC[1][i]) > 0: xx = i break ccel += 1 rang2 = range(xx, len(mat_masks)) - print(xx) + + print(xx + 1) ccel -= 1 # number of cells tracked @@ -241,7 +164,7 @@ # Removing the shock-induced points from rang rang3 = list(rang) for start, end in [shock_period]: - for i in range(start, end + 1): + for i in range(start-1, end): if i in rang3: rang3.remove(i) @@ -249,6 +172,10 @@ all_obj = cal_allob(ccel, MATC, rang) cell_data = cal_celldata(all_obj, ccel) +sio.savemat('st3_allob.mat', { + "all_obj_py": all_obj +}) + for iv in range(ccel): if np.any(all_obj[iv, min(rang):shock_period[-1]] > 0): if all_obj[iv, shock_period[-1] + 1] != 0: