Skip to content

Commit

Permalink
time save fcn added
Browse files Browse the repository at this point in the history
  • Loading branch information
spreka committed Dec 5, 2023
1 parent cb04e9e commit ab2f592
Showing 1 changed file with 58 additions and 3 deletions.
61 changes: 58 additions & 3 deletions src/napari_annotatorj/_dock_widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
Replace code below according to your needs.
"""
from time import sleep
from time import sleep,time
from qtpy.QtWidgets import QWidget, QHBoxLayout,QVBoxLayout,QFormLayout,QPushButton,QCheckBox,QLabel,QMessageBox,QFileDialog,QDialog,QComboBox,QListWidget,QAbstractItemView,QLineEdit,QMenu,QRadioButton,QSlider,QFrame,QScrollArea,QButtonGroup,QTextEdit,QProgressBar,QSpinBox
import pyqtgraph
from magicgui import magic_factory
Expand Down Expand Up @@ -35,6 +35,7 @@
from tensorflow.python.keras.callbacks import Callback
import tensorflow.math
from tensorflow import convert_to_tensor
from pandas import DataFrame as DataFrame

# suppress numpy's FutureWarning: numpy\core\numeric.py:2449: FutureWarning: elementwise comparison failed; returning scalar instead, but in the future will perform elementwise comparison!
warnings.filterwarnings('ignore', category=FutureWarning)
Expand Down Expand Up @@ -196,6 +197,11 @@ def __init__(self, napari_viewer):
self.params=None
self.initParams()

# annot times
self.annotTimes=DataFrame({'#':[],'label':[],'time':[]})
self.annotCount=0
self.lastStartTime=time()

# for dock tabs
self.firstDockWidget=None
self.firstDockWidgetName=None
Expand Down Expand Up @@ -504,6 +510,12 @@ def openNew(self):
# moved to its own fcn
self.finishOpenNewInit()

# annot times option
if self.saveAnnotTimes:
self.annotTimes=DataFrame({'#':[],'label':[],'time':[]})
self.annotCount=0
self.lastStartTime=time()

if self.fileListWidget is not None and self.fileListWidget.fileFolder!=self.defDir:
# reinit the file list
self.fileListWidget.closeFileListWidget()
Expand Down Expand Up @@ -2338,6 +2350,27 @@ def setTestMode(self,mode=False):
self.defFile='img.png'

def saveData(self):
# check boolean if annot times should be saved to file
if self.saveAnnotTimes:
# save annot time in file
annotFolder=os.path.join(self.defDir,'annotTimes')
os.makedirs(annotFolder,exist_ok=True)
print(f'Created output folder: {annotFolder}')
annotFileNameRaw=os.path.splitext(os.path.basename(self.destNameRaw))[0]+'.csv' #'annotTimes.csv'
annotFileName=os.path.join(annotFolder,annotFileNameRaw)
if os.path.isfile(annotFileName):
annotFileName=os.path.join(annotFolder,annotFileNameRaw[:annotFileNameRaw.rfind('.')]+'_1.csv')
newFileNum2=1
while os.path.isfile(annotFileName):
newFileNum2+=1
annotFileName=os.path.join(annotFolder,annotFileNameRaw[:annotFileNameRaw.rfind('.')]+'_'+str(newFileNum2)+'.csv')

successfullySaved=self.saveAnnotTimes2file(self.annotTimes,annotFileName)
if successfullySaved:
print(f'Saved annotation times in file: {annotFileName}')
# TODO: delete this!!!!!! ^


# open a save dialog and save the rois to an imagej compatible roi.zip file
self.finishedSaving=False
if not self.started or (self.findImageLayer() is None or self.findImageLayer().data is None):
Expand Down Expand Up @@ -2477,6 +2510,11 @@ def saveData(self):
self.finishedSaving=True


def saveAnnotTimes2file(self,time,fileName):
success=time.to_csv(fileName)
return success



# add mouse event handler for free roi drawing on the shapes layer
def addFreeROIdrawing(self,shapesLayer=None):
Expand Down Expand Up @@ -3468,6 +3506,13 @@ def updateNewROIprops(self,event):
if n==1:
# empty shapes layer, init the props
roiLayer.properties={'name':['0001'],'class':[0],'nameInt':[1]}

if self.saveAnnotTimes:
# measure time
curTime=round((time()-self.lastStartTime)*1000) #ms time
print(self.annotTimes)
self.annotTimes.loc[0]=[self.annotCount,roiLayer.properties['name'][-1],curTime]
self.annotCount+=1
elif n==0:
# this should never happen
print('update ROI props function called on empty layer')
Expand All @@ -3480,6 +3525,13 @@ def updateNewROIprops(self,event):
roiLayer.properties['name'][-1]='{:04d}'.format(lastNumber+1)
# default class is 0 (no class)
roiLayer.properties['class'][-1]=0

if self.saveAnnotTimes:
# measure time
curTime=round((time()-self.lastStartTime)*1000) #ms time
print(self.annotTimes)
self.annotTimes.loc[len(self.annotTimes.index)]=[self.annotCount,roiLayer.properties['name'][-1],curTime]
self.annotCount+=1
elif self.roiCount>n:
self.roiCount=n-1
else:
Expand All @@ -3492,6 +3544,9 @@ def updateNewROIprops(self,event):
print(f'roiCount: {self.roiCount}')
self.roiLayer=roiLayer

if self.saveAnnotTimes:
self.lastStartTime=time()


def initROItextProps(self):
roiLayer=self.findROIlayer()
Expand Down Expand Up @@ -8250,8 +8305,8 @@ def __init__(self,napari_viewer,annotatorjObj):
self.saveAnnotTimesChkBx.setToolTip('Save annotation times to .csv file per object')
self.saveAnnotTimesChkBx.setChecked(self.annotatorjObj.saveAnnotTimes)
self.saveAnnotTimesChkBx.stateChanged.connect(self.setSaveAnnotTimes)
self.saveAnnotTimesChkBx.setEnabled(False)
self.saveAnnotTimesChkBx.setStyleSheet("color: gray")
#self.saveAnnotTimesChkBx.setEnabled(False)
#self.saveAnnotTimesChkBx.setStyleSheet("color: gray")

# enable roi auto roi load when stepping
self.autoROIloadChkBx=QCheckBox('Auto ROI load')
Expand Down

0 comments on commit ab2f592

Please sign in to comment.