Browse Source

Changing labeling of visits to match lookup values in labkey; adding imaging date to labels

Andrej Studen 2 years ago
parent
commit
4c7ec34d82
1 changed files with 80 additions and 17 deletions
  1. 80 17
      slicerModules/imageBrowser.py

+ 80 - 17
slicerModules/imageBrowser.py

@@ -81,11 +81,6 @@ class imageBrowserWidget(ScriptedLoadableModuleWidget):
         #self.logic=imageBrowserLogic(self)
 
 
-    def updatePatientList(self,ids):
-        self.patientList.clear()
-
-        for id in ids:
-            self.patientList.addItem(id)
 
     def addPatientsSelector(self):
         #
@@ -267,23 +262,47 @@ class imageBrowserWidget(ScriptedLoadableModuleWidget):
 
     def onPatientListChanged(self,i):
         ds=self.logic.getDataset(dbFilter={'participant':self.patientList.currentText})
+       
+        visitVar=self.logic.getVarName(var='visitField')
+        dt=datetime.datetime
+        
+        #label is a combination of sequence number and date of imaging
+        seq={row['SequenceNum']:
+                {'label':row[visitVar],
+                'date': dt.strptime(row['studyDate'],'%Y/%m/%d %H:%M:%S')}
+            for row in ds['rows']}
+
+        #apply lookup to visitVar if available
+        try:
+            seq={x:
+                    {'label':ds['lookups'][visitVar][seq[x]['label']],
+                    'date':seq[x]['date']}
+                for x in seq}
+        except KeyError:
+            pass
+
+        #format label
+        seq={x:'{} ({})'.format(seq[x]['label'],dt.strftime(seq[x]['date'],'%d-%b-%Y')) 
+            for x in seq}
 
-        seq=[int(row['SequenceNum']) for row in ds['rows']]
-        seq=['Visit {}'.format(s) for s in seq]
-        print(seq)
         self.visitList.clear()   
             
         for s in seq:
-            self.visitList.addItem(s)
-        self.onVisitListChanged(0)
+            #onVisitListChanged is called for every addItem
+            self.visitList.addItem(seq[s],s)
+
+        #self.onVisitListChanged(0)
 
     def onVisitListChanged(self,i):
-        try:
-            s=self.visitList.currentText.split(' ')[1]
-        except IndexError:
+    
+        #ignore calls on empty list
+        if self.visitList.count==0:
             return
 
-        print("Visit: Selected item: {}->{}".format(i,s))
+        #get sequence num
+        s=self.visitList.itemData(i)
+
+        print("Visit: SequenceNum:{}, label{}".format(s,self.visitList.currentText))
         dbFilter={'participant':self.patientList.currentText,
             'seqNum':s}
         ds=self.logic.getDataset(dbFilter=dbFilter)
@@ -299,6 +318,12 @@ class imageBrowserWidget(ScriptedLoadableModuleWidget):
         self.ctCode.setText(row[self.ctField.text])
         #self.segmentationCode.setText(row[self.segmentationField.text])
 
+    def updatePatientList(self,ids):
+        self.patientList.clear()
+
+        for id in ids:
+            self.patientList.addItem(id)
+
     def onPatientLoadButtonClicked(self):
         print("Load")
         #delegate loading to logic
@@ -386,8 +411,9 @@ class imageBrowserLogic(ScriptedLoadableModuleLogic):
         self.tempDir=os.path.join(os.path.expanduser('~'),'temp')
         if not os.path.isdir(self.tempDir):
             os.mkdir(self.tempDir)
+        self.lookups={}
         print('imageBrowserLogic setup complete')
-
+    
 
 
     def setServer(self,serverName):
@@ -443,6 +469,15 @@ class imageBrowserLogic(ScriptedLoadableModuleLogic):
         ids=[row[self.isetup['participantField']] for row in ds['rows']]
         status['ids']=list(set(ids))
         return status
+
+    def getVarName(self,name="Imaging",var="visitField"):
+        dset=self.isetup['datasets'][name]
+        defaults={"visitField":"imagingVisitId"}
+        try:
+            return dset[var]
+        except KeyError:
+            return defaults[var]
+
         
     def getDataset(self,name="Imaging",dbFilter=[]):
         dset=self.isetup['datasets'][name]
@@ -463,10 +498,38 @@ class imageBrowserLogic(ScriptedLoadableModuleLogic):
             qFilter.append({'variable':f,'value':dbFilter[f],'oper':'eq'})
 
         try:
-            return self.db.selectRows(project,schema,query, \
+            ds=self.db.selectRows(project,schema,query, \
                 qFilter,dset['view'])
         except KeyError:
-            return self.db.selectRows(project,schema,query,qFilter)
+            ds=self.db.selectRows(project,schema,query,qFilter)
+
+        #get lookups as well 
+        lookups={}
+        for f in ds['metaData']['fields']:
+            try:
+                lookup=f['lookup']
+            except KeyError:
+                continue
+            var=f['name']
+            lookupCode='{}:{}'.format(lookup['schema'],lookup['queryName'])
+            try:
+                lookups[var]=self.lookups[lookupCode]
+            except KeyError:
+                self.lookups[lookupCode]=self.loadLookup(project,lookup)
+                lookups[var]=self.lookups[lookupCode]
+            
+        return {'rows':ds['rows'],'lookups':lookups}
+
+    def loadLookup(self,project,lookup):
+
+        qData={}
+        key=lookup['keyColumn']
+        value=lookup['displayColumn']
+
+        fSet=self.db.selectRows(project,lookup['schema'],lookup['queryName'],[])
+        for q in fSet['rows']:
+            qData[q[key]]=q[value]
+        return qData
 
     def loadImage(self,iData):