Skip to content

Commit

Permalink
fix: apply del ROIs on switching to cca or saving
Browse files Browse the repository at this point in the history
  • Loading branch information
ElpadoCan committed Jun 19, 2023
1 parent f754fb1 commit 72d2027
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 16 deletions.
2 changes: 2 additions & 0 deletions cellacdc/__init__.py
Expand Up @@ -14,10 +14,12 @@
qrc_resources_light_path = os.path.join(cellacdc_path, 'qrc_resources_light.py')
qrc_resources_dark_path = os.path.join(cellacdc_path, 'qrc_resources_dark.py')

# Set default qrc resources
if not os.path.exists(qrc_resources_path):
# Load default light mode
shutil.copyfile(qrc_resources_light_path, qrc_resources_path)

# Replace 'from PyQt5' with 'from qtpy' in qrc_resources.py file
try:
with open(qrc_resources_path, 'r') as qrc_py:
text = qrc_py.read()
Expand Down
61 changes: 45 additions & 16 deletions cellacdc/gui.py
Expand Up @@ -9175,8 +9175,11 @@ def getDelROIlab(self):
delROIs_info['delIDsROI'][idx] = delIDsROI
return allDelIDs, DelROIlab

def getDelRoiMask(self, roi):
posData = self.data[self.pos_i]
def getDelRoiMask(self, roi, posData=None, z_slice=None):
if posData is None:
posData = self.data[self.pos_i]
if z_slice is None:
z_slice = self.z_lab()
ROImask = np.zeros(posData.lab.shape, bool)
if isinstance(roi, pg.PolyLineROI):
r, c = [], []
Expand All @@ -9193,7 +9196,7 @@ def getDelRoiMask(self, roi):
else:
rr, cc = skimage.draw.polygon(r, c, shape=self.currentLab2D.shape)
if self.isSegm3D:
ROImask[self.z_lab(), rr, cc] = True
ROImask[z_slice, rr, cc] = True
else:
ROImask[rr, cc] = True
elif isinstance(roi, pg.LineROI):
Expand All @@ -9204,14 +9207,14 @@ def getDelRoiMask(self, roi):
x2, y2 = int(point2.x()), int(point2.y())
rr, cc, val = skimage.draw.line_aa(y1, x1, y2, x2)
if self.isSegm3D:
ROImask[self.z_lab(), rr, cc] = True
ROImask[z_slice, rr, cc] = True
else:
ROImask[rr, cc] = True
else:
x0, y0 = [int(c) for c in roi.pos()]
w, h = [int(c) for c in roi.size()]
if self.isSegm3D:
ROImask[self.z_lab(), y0:y0+h, x0:x0+w] = True
ROImask[z_slice, y0:y0+h, x0:x0+w] = True
else:
ROImask[y0:y0+h, x0:x0+w] = True
return ROImask
Expand Down Expand Up @@ -9847,6 +9850,8 @@ def changeMode(self, idx):
self.store_cca_df()
elif mode == 'Cell cycle analysis':
proceed = self.initCca()
if proceed:
self.applyDelROIs()
self.modeToolBar.setVisible(True)
self.realTimeTrackingToggle.setDisabled(True)
self.realTimeTrackingToggle.label.setDisabled(True)
Expand Down Expand Up @@ -10773,17 +10778,13 @@ def keyPressEvent(self, ev):
if ev.key() == Qt.Key_Q:
# self.setAllIDs()
posData = self.data[self.pos_i]
roi = posData.allData_li[posData.frame_i]['delROIs_info']['rois'][0]
ROImask = self.getDelRoiMask(roi)
# self.pointsLayerDataToDf(posData)
# # posData.clickEntryPointsDfs
# printl(posData.clickEntryPointsDfs)
# self.drawPointsLayers(computePointsLayers=False)
# printl(posData.fluo_data_dict.keys())
# for key in posData.fluo_data_dict:
# printl(key, posData.fluo_data_dict[key].max())
# printl(f'{posData.binnedIDs = }')
# printl(f'{posData.ripIDs = }')
# from acdctools.plot import imshow
# delIDs = posData.allData_li[posData.frame_i]['delROIs_info']['delIDsROI']
# printl(delIDs)
# self.store_data()
# self.applyDelROIs()
# stored_lab = posData.allData_li[posData.frame_i]['labels']
# imshow(posData.lab, stored_lab, parent=self)

if not self.dataIsLoaded:
self.logger.info(
Expand Down Expand Up @@ -17816,6 +17817,32 @@ def applyDelROIimg1(self, roi, init=False, ax=0):
self.labelsLayerRightImg.setImage(lab, autoLevels=False)

self.setAllTextAnnotations(labelsToSkip={ID:True for ID in delIDs})

def applyDelROIs(self):
self.logger.info('Applying deletion ROIs (if present)...')

for posData in self.data:
self.current_frame_i = posData.frame_i
for frame_i in range(posData.SizeT):
lab = posData.allData_li[frame_i]['labels']
if lab is None:
break
delROIs_info = posData.allData_li[frame_i]['delROIs_info']
delIDs_rois = delROIs_info['delIDsROI']
if not delIDs_rois:
continue
for delIDs in delIDs_rois:
for delID in delIDs:
lab[lab==delID] = 0
posData.allData_li[frame_i]['labels'] = lab
# Get the rest of the metadata and store data based on the new lab
posData.frame_i = frame_i
self.get_data()
self.store_data(autosave=False)

# Back to current frame
posData.frame_i = self.current_frame_i
self.get_data()

def initTempLayerBrush(self, ID, ax=0):
if ax == 0:
Expand Down Expand Up @@ -21005,6 +21032,8 @@ def warnDifferentSegmChannel(

@exception_handler
def saveData(self, checked=False, finishedCallback=None, isQuickSave=False):
self.store_data(autosave=False)
self.applyDelROIs()
self.store_data()

# Wait autosave worker to finish
Expand Down

0 comments on commit 72d2027

Please sign in to comment.