瀏覽代碼

Merge branch 'master' of https://git0.fmf.uni-lj.si/studen/irAEMM

Andrej Studen 3 年之前
父節點
當前提交
bbed7b3e28
共有 1 個文件被更改,包括 88 次插入58 次删除
  1. 88 58
      pythonScripts/populateImagingFromTransferList.py

+ 88 - 58
pythonScripts/populateImagingFromTransferList.py

@@ -94,72 +94,58 @@ def main(parameterFile):
             #just the one match, fine
             #just the one match, fine
             print('Entry for {}/{} already resolved'.\
             print('Entry for {}/{} already resolved'.\
                     format(im[dbParticipantField],seqNum))
                     format(im[dbParticipantField],seqNum))
-            continue
+            
+            entry=ds1['rows'][0]
+            try:
+                if len(entry['PETWB_orthancId'])>0 and len(entry['CT_orthancId'])>0:
+                    continue
+            except:
+                pass
+            print('Debug mode for missing cases')
 
 
         #have to convert from datetime to %Y%m%d format
         #have to convert from datetime to %Y%m%d format
         #dateFilter={'variable':'imageDate','value':im['imageDate'],'oper':'eq'}
         #dateFilter={'variable':'imageDate','value':im['imageDate'],'oper':'eq'}
 
 
         dsOrthanc=db.selectRows(projectOrthanc,inputSchema,inputQuery,[inputIdFilter])
         dsOrthanc=db.selectRows(projectOrthanc,inputSchema,inputQuery,[inputIdFilter])
+
         #what if dsOrthanc['rows'] is empty?
         #what if dsOrthanc['rows'] is empty?
         #this is part of QA and is reported in missingImages Schema/Query
         #this is part of QA and is reported in missingImages Schema/Query
-
-        uploadStatus="FAILED"
-        imageDateMismatch=[]
-        for im1 in dsOrthanc['rows']:
-
-            date=datetime.datetime.strptime(im1['studyDate'],'%Y/%m/%d %H:%M:%S') 
-            #convert date to %Y%m%d notation
-            dateYMD=date.strftime('%Y%m%d')
-            if dateYMD!=im['imageDate']:
-                print('Rejecting mismatch: {}/{}'.format(dateYMD,im['imageDate']))
-                imageDateMismatch.append(dateYMD)
-                continue
-        
-            outvar='NONE'
-            sd=im1['seriesDescription']
-            if sd=='PET WB':
-                outvar='PETWB_orthancId'
-                print('Found PET: {}'.format(im1['orthancSeries']))
-            if sd.find('CT WB')==0:
-                if sd.find('fov')<0:
-                    outvar='CT_orthancId'
-                    print('Found CT: {}'.format(im1['orthancSeries']))
-
-            #skip irrelevant series
-            if outvar=='NONE':
-                continue
-
-            #figure out which row in output study to update
-            filters=[]
-            print('Participant {} Sequence number {}'.\
-                    format(im[dbParticipantField],str(seqNum)))
     
     
+        #convert dates
+        sDates={r['orthancSeries']:datetime.datetime.strptime(r['studyDate'],'%Y/%m/%d %H:%M:%S') 
+                for r in dsOrthanc['rows']}
 
 
-            mode='update'
-            outRow={}
-            if len(ds1['rows'])==0:
-                mode='insert'
+        sDates={x:sDates[x].strftime('%Y%m%d') for x in sDates}
+
+        #unique dates (for date mismatch)
+        dates=list(set(sDates.values()))
         
         
-                outRow[dbParticipantField]=im[dbParticipantField]
-                outRow['SequenceNum']=seqNum
-                outRow['dicomStudy']=im1['dicomStudy']
+        #select dates
+        sDatesMatch={x:sDates[x] for x in sDates if sDates[x]==im['imageDate']}
         
         
-            else:
-                #never happens if we check for sd1 before matches are found
-                outRow=ds1['rows'][0]
+        #select 
+        rowsMatch=[r for r in dsOrthanc['rows'] if r['orthancSeries'] in list(sDatesMatch.keys())]
+
+        #select CT matches
+        rowsCT=[r for r in rowsMatch if r['seriesDescription'].find('CT WB')==0]
+        #should not include fov
+        rowsCT=[r for r in rowsCT if r['seriesDescription'].find('fov')<0]
+        print('entry[{}/{}] rowsCT : {}'.format(im[dbParticipantField],seqNum,rowsCT))
         
         
-            
-            outRow[outvar]=im1['orthancSeries']
-            outRow['studyDate']=im1['studyDate']
-            outRow['imagingVisitId']=im['imagingVisitId']
-            outRow['visitCode']='VISIT_'+str(im['imagingVisitId'])
-
-            modifyStatus=db.modifyRows(mode,projectStudy,outputSchema,\
-                    outputQuery,[outRow])
-            print('{}'.format(modifyStatus))
-            uploadStatus="LOADED"
+        #should be one and one only
+        rowsPET=[r for r in rowsMatch if r['seriesDescription']=='PET WB']
+        print('entry[{}/{}] rowsPET: {}'.format(im[dbParticipantField],seqNum,rowsPET))
         
         
-        if uploadStatus=="FAILED":
+        dataRow={}
+        if len(rowsCT)==1:
+            dataRow=rowsCT[0]
+        if len(dataRow)==0:
+            if len(rowsPET)==1:
+                dataRows=rowsPET[0]
+        
+
+        #deal with erroneous outcomes (ie- no CT, more then 1 CT, no PET, more than 1 PET)
+        if len(rowsPET)!=1 or len(rowsCT)!=1:
             #standard spiel - find if already present; if so, skip, if not, add
             #standard spiel - find if already present; if so, skip, if not, add
             imFilter={'variable':'imagingVisitId',
             imFilter={'variable':'imagingVisitId',
                     'value':'{}'.format(im['imagingVisitId']),
                     'value':'{}'.format(im['imagingVisitId']),
@@ -168,7 +154,6 @@ def main(parameterFile):
                     missingQuery,[idFilter,imFilter])
                     missingQuery,[idFilter,imFilter])
 
 
             #already recorded
             #already recorded
-            imageDateMismatch=list(set(imageDateMismatch))
             vals=[dbParticipantField,'imagingVisitId','imageDate']
             vals=[dbParticipantField,'imagingVisitId','imageDate']
             mode='insert'
             mode='insert'
             if len(dsMissing['rows'])>0:
             if len(dsMissing['rows'])>0:
@@ -176,12 +161,57 @@ def main(parameterFile):
                 orow=dsMissing['rows'][0]
                 orow=dsMissing['rows'][0]
             else:
             else:
                 orow={v:im[v] for v in vals}
                 orow={v:im[v] for v in vals}
-            orow['imageDateMismatch']=','.join(imageDateMismatch)
-            orow['failureDescription']='MISSING DICOM'
+            orow['imageDateMismatch']=','.join(dates)
+            failDesc=''
+            if len(rowsPET)==0:
+                failDesc+='[MISSSING PET]'
+            if len(rowsPET)>1:
+                failDesc+='[MULTIPLE PET]'
+            if len(rowsCT)==0:
+                failDesc+='[MISSSING CT]'
+            if len(rowsCT)>1:
+                failDesc+='[MULTIPLE CT]'
+            orow['failureDescription']=failDesc
             db.modifyRows(mode,projectStudy,missingSchema,\
             db.modifyRows(mode,projectStudy,missingSchema,\
                     missingQuery,[orow])
                     missingQuery,[orow])
-            
-       
+            #don't break, but fill only for singular outcomes
+         
+
+        #figure out which row in output study to update
+        filters=[]
+        print('Participant {} Sequence number {}'.\
+               format(im[dbParticipantField],str(seqNum)))
+        #refresh the matching study list (after CT we have found PET)
+        ds1=db.selectRows(projectStudy,outputSchema,outputQuery,\
+                [idFilter,seqFilter])
+
+        mode='update'
+        outRow={}
+        if len(ds1['rows'])==0:
+            mode='insert'
+        
+            outRow[dbParticipantField]=im[dbParticipantField]
+            outRow['SequenceNum']=seqNum
+            if len(dataRow)==1:
+                outRow['dicomStudy']=dataRow['dicomStudy']
+        
+        else:
+            #never happens if we check for sd1 before matches are found
+            outRow=ds1['rows'][0]
+        
+        if len(rowsPET)==1:    
+            outRow['PET_orthancId']=rowsPET[0]['orthancSeries']
+        if len(rowsCT)==1:
+            outRow['CT_orthancId']=rowsCT[0]['orthancSeries']
+        if len(dataRow)==1:
+            outRow['studyDate']=dataRow['studyDate']
+        outRow['imagingVisitId']=im['imagingVisitId']
+        outRow['visitCode']='VISIT_'+str(im['imagingVisitId'])
+
+        modifyStatus=db.modifyRows(mode,projectStudy,outputSchema,\
+                outputQuery,[outRow])
+        print('{}'.format(modifyStatus))
+        
     print("Done")
     print("Done")
 
 
 if __name__=='__main__':
 if __name__=='__main__':