Skip to content

Commit

Permalink
perf: use collections.deque with maxlen=5 for autosave worker q
Browse files Browse the repository at this point in the history
  • Loading branch information
ElpadoCan committed Sep 8, 2023
1 parent b3c3229 commit cb5ad45
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 41 deletions.
34 changes: 3 additions & 31 deletions cellacdc/gui.py
Original file line number Diff line number Diff line change
Expand Up @@ -6835,14 +6835,8 @@ def gui_mousePressEventImg1(self, event):
and not is_right_click_custom_ON and not copyContourON
)

# printl(
# f'{isOnlyRightClick = }\n'
# f'{self.isDoubleRightClick = }\n'
# f'{self.doubleRightClickTimeElapsed = }\n'
# f'{self.countRightClicks = }'
# )

if isOnlyRightClick:
# Start timer or check if it is a double-right-click
if self.countRightClicks == 0:
self.isDoubleRightClick = False
self.countRightClicks = 1
Expand Down Expand Up @@ -11385,32 +11379,10 @@ def retainSpaceSlidersToggled(self, checked):
@exception_handler
def keyPressEvent(self, ev):
if ev.key() == Qt.Key_Q and self.debug:
# printl(self.xHoverImg, self.yHoverImg)
# printl(self.img1.mapToData(QCursor().pos()))
# x, y = QCursor().pos().x(), QCursor().pos().y()
# printl(self.graphLayout.mapToScene(x, y))
posData = self.data[self.pos_i]
is_segm_3D = self.isSegm3D
# all_metrics_names = measurements.get_all_metrics_names(
# posData, self.user_ch_name, is_segm_3D
# )
# printl(self.metricsToSave)
self.initMetricsToSave(posData)
for worker, thread in self.autoSaveActiveWorkers:
printl(worker.isAutoSaveON)

# printl(self.bkgr_metrics_params, pretty=True)
printl(self.foregr_metrics_params, pretty=True)
printl(self.custom_metrics_params, pretty=True)

# 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)

printl(self.setMeasWinState, pretty=True)

if not self.dataIsLoaded:
self.logger.info(
'[WARNING]: Data not loaded yet. '
Expand Down
22 changes: 12 additions & 10 deletions cellacdc/workers.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import os
import time
import json
from collections import deque

from typing import Union, List

Expand Down Expand Up @@ -493,7 +494,7 @@ def __init__(self, mutex, waitCond, savedSegmData):
self.abortSaving = False
self.isSaving = False
self.isPaused = False
self.dataQ = queue.Queue()
self.dataQ = deque(maxlen=5)
self.isAutoSaveON = False

def pause(self):
Expand All @@ -514,8 +515,9 @@ def enqueue(self, posData):
def _enqueue(self, posData):
if DEBUG:
self.logger.log('Enqueing posData autosave...')
self.dataQ.put(posData)
if self.dataQ.qsize() == 1:
self.dataQ.append(posData)
if len(self.dataQ) == 1:
# Wake worker upon inserting first element
self.abortSaving = False
self.waitCond.wakeAll()

Expand All @@ -525,8 +527,8 @@ def _stop(self):

def abort(self):
self.abortSaving = True
while not self.dataQ.empty():
data = self.dataQ.get()
while not len(self.dataQ) == 0:
data = self.dataQ.pop()
del data
self._stop()

Expand All @@ -536,18 +538,18 @@ def run(self):
if self.exit:
self.logger.log('Closing autosaving worker...')
break
elif not self.dataQ.empty():
elif not len(self.dataQ) == 0:
if DEBUG:
self.logger.log('Autosaving...')
data = self.dataQ.get()
data = self.dataQ.pop()
try:
self.saveData(data)
except Exception as e:
error = traceback.format_exc()
print('*'*40)
self.logger.log(error)
print('='*40)
if self.dataQ.empty():
if len(self.dataQ) == 0:
self.sigDone.emit()
else:
self.pause()
Expand Down Expand Up @@ -585,7 +587,7 @@ def saveData(self, posData):
segm_npz_path = posData.segm_npz_temp_path

end_i = self.getLastTrackedFrame(posData)

if self.isAutoSaveON:
if end_i < len(posData.segm_data):
saved_segm_data = posData.segm_data
Expand Down Expand Up @@ -628,7 +630,7 @@ def saveData(self, posData):
if self.abortSaving:
break

if not self.abortSaving:
if not self.abortSaving:
if self.isAutoSaveON:
segm_data = np.squeeze(saved_segm_data)
self._saveSegm(segm_npz_path, posData.segm_npz_path, segm_data)
Expand Down

0 comments on commit cb5ad45

Please sign in to comment.