import os import unittest from __main__ import vtk, qt, ctk, slicer from slicer.ScriptedLoadableModule import * import json import datetime import sys import nixModule import pathlib # # labkeySlicerPythonExtension # class imageBrowser(ScriptedLoadableModule): """Uses ScriptedLoadableModule base class, available at: https://github.com/Slicer/Slicer/blob/master/Base/Python/slicer/ScriptedLoadableModule.py """ def __init__(self, parent): ScriptedLoadableModule.__init__(self, parent) self.parent.title = "image Browser" # TODO make this more human readable by adding spaces self.parent.categories = ["LabKey"] self.parent.dependencies = [] self.parent.contributors = ["Andrej Studen (UL/FMF)"] # replace with "Firstname Lastname (Organization)" self.parent.helpText = """ Interface to irAEMM files in LabKey """ self.parent.acknowledgementText = """ Developed within the medical physics research programme of the Slovenian research agency. """ # replace with organization, grant and thanks. # # labkeySlicerPythonExtensionWidget # class imageBrowserWidget(ScriptedLoadableModuleWidget): """Uses ScriptedLoadableModuleWidget base class, available at: https://github.com/Slicer/Slicer/blob/master/Base/Python/slicer/ScriptedLoadableModule.py """ def setup(self): print("Setting up imageBrowserWidget") ScriptedLoadableModuleWidget.setup(self) # Instantiate and connect widgets ... self.logic=imageBrowserLogic(self) self.addInfoSection() self.addSetupSection() self.addPatientsSelector() def addInfoSection(self): #a python overview of json settings infoCollapsibleButton = ctk.ctkCollapsibleButton() infoCollapsibleButton.text = "Info" self.layout.addWidget(infoCollapsibleButton) infoLayout = qt.QFormLayout(infoCollapsibleButton) self.participantField=qt.QLabel("PatientId") infoLayout.addRow("Participant field:",self.participantField) self.ctField=qt.QLabel("ctResampled") infoLayout.addRow("Data field (CT):",self.ctField) self.petField=qt.QLabel("petResampled") infoLayout.addRow("Data field (PET):",self.petField) self.idField=qt.QLabel("Loading") infoLayout.addRow("ID",self.idField) #Add logic at some point #self.logic=imageBrowserLogic(self) def updatePatientList(self,ids): self.patientList.clear() for id in ids: self.patientList.addItem(id) def addPatientsSelector(self): # # Patients Area # patientsCollapsibleButton = ctk.ctkCollapsibleButton() patientsCollapsibleButton.text = "Patients" #don't add it yet self.layout.addWidget(patientsCollapsibleButton) patientsFormLayout = qt.QFormLayout(patientsCollapsibleButton) self.patientList=qt.QComboBox() self.patientList.currentIndexChanged.connect(self.onPatientListChanged) patientsFormLayout.addRow("Patient:",self.patientList) self.visitList=qt.QComboBox() self.visitList.currentIndexChanged.connect(self.onVisitListChanged) patientsFormLayout.addRow("Visit:",self.visitList) self.ctCode=qt.QLabel("ctCode") patientsFormLayout.addRow("CT:",self.ctCode) self.petCode=qt.QLabel("petCode") patientsFormLayout.addRow("PET:",self.petCode) self.patientLoad=qt.QPushButton("Load") self.patientLoad.clicked.connect(self.onPatientLoadButtonClicked) patientsFormLayout.addRow("Load patient",self.patientLoad) self.patientClear=qt.QPushButton("Clear") self.patientClear.clicked.connect(self.onPatientClearButtonClicked) patientsFormLayout.addRow("Clear patient",self.patientClear) self.keepCached=qt.QCheckBox("keep Cached") self.keepCached.setChecked(1) patientsFormLayout.addRow("Keep cached",self.keepCached) def addSetupSection(self): setupCollapsibleButton = ctk.ctkCollapsibleButton() setupCollapsibleButton.text = "Setup" self.layout.addWidget(setupCollapsibleButton) #Form layout (maybe one can think of more intuitive layouts) setupFormLayout = qt.QFormLayout(setupCollapsibleButton) self.serverList=qt.QComboBox() self.serverList.addItem('') self.setupList.addItem("limfomiPET_iBrowser.json") self.setupList.currentIndexChanged.connect(self.onSetupListChanged) setupFormLayout.addRow("Setup:",self.setupList) def addReviewSection(self): # # Review Area # reviewCollapsibleButton = ctk.ctkCollapsibleButton() reviewCollapsibleButton.text = "Review" self.layout.addWidget(reviewCollapsibleButton) self.reviewBoxLayout = qt.QVBoxLayout(reviewCollapsibleButton) self.reviewFormLayout = qt.QFormLayout() self.reviewSegment=qt.QComboBox() self.reviewSegment.currentIndexChanged.connect(\ self.onReviewSegmentChanged) self.reviewFormLayout.addRow("Selected region:",self.reviewSegment) self.reviewResult=qt.QComboBox() sLabel="What do you think about the segmentation:" self.reviewFormLayout.addRow(sLabel,self.reviewResult) reviewOptions=['Select','Excellent','Minor deficiencies',\ 'Major deficiencies','Unusable'] for opt in reviewOptions: self.reviewResult.addItem(opt) self.aeResult=qt.QComboBox() aeLabel="Is organ suffering from adverse effect?" self.reviewFormLayout.addRow(aeLabel,self.aeResult) aeOptions=['Select','Yes','No'] for opt in aeOptions: self.aeResult.addItem(opt) #self.aeResult.setCurrentIndex(0) self.updateReview=qt.QPushButton("Save") saLabel="Save segmentation and AE decision for current segment" self.reviewFormLayout.addRow(saLabel,self.updateReview) self.updateReview.clicked.connect(self.onUpdateReviewButtonClicked) self.reviewBoxLayout.addLayout(self.reviewFormLayout) submitFrame=qt.QGroupBox("Submit data") self.submitFormLayout=qt.QFormLayout() self.reviewComment=qt.QTextEdit("this is a test") self.submitFormLayout.addRow("Comments (optional)",self.reviewComment) self.submitReviewButton=qt.QPushButton("Submit") self.submitFormLayout.addRow("Submit to database",\ self.submitReviewButton) self.submitReviewButton.clicked.connect(\ self.onSubmitReviewButtonClicked) submitFrame.setLayout(self.submitFormLayout) submitFrame.setFlat(1) #submitFrame.setFrameShape(qt.QFrame.StyledPanel) #submitFrame.setFrameShadow(qt.QFrame.Sunken) submitFrame.setStyleSheet("background-color:rgba(220,215,180,45)") self.reviewBoxLayout.addWidget(submitFrame) def onSetupListChanged(self,i): status=self.logic.setConfig(self.setupList.currentText) try: if status['error']=='FILE NOT FOUND': print('File {} not found.'.format(self.setupList.currentText)) return except KeyError: pass self.updatePatientList(status['ids']) self.onPatientListChanged(0) def onServerListChanged(self,i): status=self.logic.setServer(self.serverList.currentText) try: if status['error']=='KEY ERROR': self.serverList.setStyleSheet('background-color: violet') if status['error']=='ID ERROR': self.serverList.setStyleSheet('background-color: red') return except KeyError: pass self.idField.setText(status['id']) self.serverList.setStyleSheet('background-color: green') def onPatientListChanged(self,i): idFilter={'variable':'PatientId', 'value':self.patientList.currentText, 'oper':'eq'} ds=self.logic.getDataset([idFilter]) seq=[int(row['SequenceNum']) for row in ds['rows']] self.visitList.clear() for s in seq: self.visitList.addItem("Visit "+str(s)) self.onVisitListChanged(0) def onVisitListChanged(self,i): try: s=self.visitList.currentText.split(' ')[1] except IndexError: return print("Visit: Selected item: {}->{}".format(i,s)) idFilter={'variable':'PatientId',\ 'value':self.patientList.currentText,'oper':'eq'} sFilter={'variable':'SequenceNum','value':s,'oper':'eq'} ds=self.logic.getDataset([idFilter,sFilter]) if not len(ds['rows'])==1: print("Found incorrect number {} of matches for [{}]/[{}]".\ format(len(ds['rows']),\ self.patientList.currentText,s)) row=ds['rows'][0] #copy row properties for data access self.currentRow=row self.petCode.setText(row[self.petField.text]) self.ctCode.setText(row[self.ctField.text]) #self.segmentationCode.setText(row[self.segmentationField.text]) def onPatientLoadButtonClicked(self): print("Load") #delegate loading to logic self.logic.loadImages(self.currentRow,self.keepCached.isChecked()) #self.logic.loadReview(self.currentRow) #self.logic.loadAE(self.currentRow) #self.onReviewSegmentChanged() def onReviewSegmentChanged(self): pass def onPatientClearButtonClicked(self): self.logic.clearVolumesAndSegmentations() #self.reviewSegment.clear() #self.removeCompletedSegments() #self.reviewComment.clear() def cleanup(self): pass def loadLibrary(name): #utility function to load nix library from git fwrapper=nixModule.getWrapper('nixWrapper.py') p=pathlib.Path(fwrapper) sys.path.append(str(p.parent)) import nixWrapper return nixWrapper.loadLibrary(name) # # imageBrowserLogic # class imageBrowserLogic(ScriptedLoadableModuleLogic): """This class should implement all the actual computation done by your module. The interface should be such that other python code can import this class and make use of the functionality without requiring an instance of the Widget. Uses ScriptedLoadableModuleLogic base class, available at: https://github.com/Slicer/Slicer/blob/master/Base/Python/slicer/ScriptedLoadableModule.py """ def __init__(self,parent=None): ScriptedLoadableModuleLogic.__init__(self, parent) print('imageBrowserLogic loading') if not parent==None: #use layout and data from parent widget self.parent=parent fhome=os.path.expanduser('~') fsetup=os.path.join(fhome,'.labkey','setup.json') try: with open(fsetup) as f: self.setup=json.load(f) except FileNotFoundError: self.setup={} try: pt=self.setup['paths'] except KeyError: self.setup['paths']={} lName='labkeyInterface' loadLibrary(lName) import labkeyInterface import labkeyDatabaseBrowser import labkeyFileBrowser self.network=labkeyInterface.labkeyInterface() self.dbBrowser=labkeyDatabaseBrowser self.fBrowser=labkeyFileBrowser print('imageBrowserLogic setup complete') def setServer(self,serverName): #additional way of setting the labkey network interface #if no parent was provided in logic initialization (stand-alone mode) status={} fileName="NONE" if serverName=="astuden": fileName="astuden.json" if fileName=="NONE": print("No path was associated with server {}".format(serverName)) status['error']='KEY ERROR' return status fconfig=os.path.join(os.path.expanduser('~'),'.labkey',fileName) self.network.init(fconfig) remoteId=self.network.getUserId() if remoteId==None: status['error']='ID ERROR' return status status['id']=remoteId['displayName'] #reset db and fb (they are thin classes anyhow) self.db=self.dbBrowser.labkeyDB(self.network) self.fb=self.fBrowser.labkeyFileBrowser(self.network) return status def setConfig(self,configName): status={} fileName=os.path.join(os.path.expanduser('~'),'.labkey',configName) if not os.path.isfile(fileName): status['error']='FILE NOT FOUND' return status with open(fileName,'r') as f: self.isetup=json.load(f) self.project=self.isetup['project'] #"iPNUMMretro/Study" self.schema='study' self.dataset=self.isetup['query'] ds=self.getDataset([]) ids=[row[self.isetup['participantField']] for row in ds['rows']] status['ids']=list(set(ids)) return status def getDataset(self,dbFilter): project=self.project schema=self.schema query=self.dataset try: return self.db.selectRows(project,schema,query, \ dbFilter,self.isetup['view']) except KeyError: return self.db.selectRows(project,schema,query,dbFilter) def loadImage(self,idx,path,tempDir,keepCached): localPath=os.path.join(tempDir,path[-1]) if not os.path.isfile(localPath): #download from server remotePath=self.fb.formatPathURL(self.project,'/'.join(path)) if not self.fb.entryExists(remotePath): print("Failed to get {}".format(remotePath)) return self.fb.readFileToFile(remotePath,localPath) properties={} #make sure segmentation gets loaded as a labelmap if idx=="Segmentation": properties["labelmap"]=1 self.volumeNode[idx]=slicer.util.loadNodeFromFile(localPath, filetype='VolumeFile',properties=properties) if not keepCached: os.remove(localPath) def loadImages(self,row,keepCached): tempDir=os.path.join(os.path.expanduser('~'),'temp') if not os.path.isdir(tempDir): os.mkdir(tempDir) #fields={'ctResampled':True,'petResampled':False} fields={"CT":self.parent.ctField.text,\ "PET":self.parent.petField.text} path=['preprocessedImages',row['patientCode'],row['visitCode']] relativePaths={x:path+[row[y]] for (x,y) in fields.items()} self.volumeNode={} for f in relativePaths: p=relativePaths[f] self.loadImage(f,p,tempDir,keepCached) #mimic abdominalCT standardized window setting self.volumeNode['CT'].GetScalarVolumeDisplayNode().\ SetWindowLevel(1400, -500) #set colormap for PET to PET-Heat (this is a verbatim setting from #the Volumes->Display->Lookup Table colormap identifier) self.volumeNode['PET'].GetScalarVolumeDisplayNode().\ SetAndObserveColorNodeID(\ slicer.util.getNode('PET-Heat').GetID()) slicer.util.setSliceViewerLayers(background=self.volumeNode['CT'],\ foreground=self.volumeNode['PET'],foregroundOpacity=0.1,fit=True) def clearVolumesAndSegmentations(self): nodes=slicer.util.getNodesByClass("vtkMRMLVolumeNode") #nodes.extend(slicer.util.getNodesByClass("vtkMRMLSegmentationNode")) res=[slicer.mrmlScene.RemoveNode(f) for f in nodes] #self.segmentationNode=None #self.reviewResult={} #self.aeList={} class imageBrowserTest(ScriptedLoadableModuleTest): """ This is the test case for your scripted module. Uses ScriptedLoadableModuleTest base class, available at: https://github.com/Slicer/Slicer/blob/master/Base/Python/slicer/ScriptedLoadableModule.py """ def setup(self): """ Do whatever is needed to reset the state - typically a scene clear will be enough. """ slicer.mrmlScene.Clear(0) def runTest(self): """Run as few or as many tests as needed here. """ self.setUp() self.test_irAEMMBrowser() def test_irAEMMBrowser(self): """ Ideally you should have several levels of tests. At the lowest level tests sould exercise the functionality of the logic with different inputs (both valid and invalid). At higher levels your tests should emulate the way the user would interact with your code and confirm that it still works the way you intended. One of the most important features of the tests is that it should alert other developers when their changes will have an impact on the behavior of your module. For example, if a developer removes a feature that you depend on, your test should break so they know that the feature is needed. """ self.delayDisplay("Starting the test") # # first, get some data #