|
@@ -6,99 +6,116 @@ import json
|
|
import re
|
|
import re
|
|
import sys
|
|
import sys
|
|
|
|
|
|
|
|
+def main(parameterFile):
|
|
|
|
|
|
-fhome=os.path.expanduser('~')
|
|
|
|
-fsetup=os.path.join(fhome,'.labkey','setup.json')
|
|
|
|
|
|
+ fhome=os.path.expanduser('~')
|
|
|
|
+ fsetup=os.path.join(fhome,'.labkey','setup.json')
|
|
|
|
|
|
-with open(fsetup,'r') as f:
|
|
|
|
- setup=json.load(f)
|
|
|
|
|
|
+ with open(fsetup,'r') as f:
|
|
|
|
+ setup=json.load(f)
|
|
|
|
|
|
-sys.path.insert(0,setup['paths']['labkeyInterface'])
|
|
|
|
-import labkeyInterface
|
|
|
|
-import labkeyDatabaseBrowser
|
|
|
|
|
|
+ sys.path.insert(0,setup['paths']['labkeyInterface'])
|
|
|
|
+ import labkeyInterface
|
|
|
|
+ import labkeyDatabaseBrowser
|
|
|
|
|
|
-sys.path.insert(0,setup['paths']['orthancInterface'])
|
|
|
|
-import orthancInterface
|
|
|
|
-import orthancDatabaseBrowser
|
|
|
|
|
|
+ sys.path.insert(0,setup['paths']['orthancInterface'])
|
|
|
|
+ import orthancInterface
|
|
|
|
+ import orthancDatabaseBrowser
|
|
|
|
|
|
-fconfig=os.path.join(fhome,'.labkey','network.json')
|
|
|
|
|
|
+ fconfig=os.path.join(fhome,'.labkey','network.json')
|
|
|
|
|
|
-net=labkeyInterface.labkeyInterface()
|
|
|
|
-net.init(fconfig)
|
|
|
|
-db=labkeyDatabaseBrowser.labkeyDB(net)
|
|
|
|
|
|
+ net=labkeyInterface.labkeyInterface()
|
|
|
|
+ net.init(fconfig)
|
|
|
|
+ db=labkeyDatabaseBrowser.labkeyDB(net)
|
|
|
|
|
|
|
|
|
|
-onet=orthancInterface.orthancInterface()
|
|
|
|
-onet.init(fconfig)
|
|
|
|
-odb=orthancDatabaseBrowser.orthancDB(onet)
|
|
|
|
|
|
+ onet=orthancInterface.orthancInterface()
|
|
|
|
+ onet.init(fconfig)
|
|
|
|
+ odb=orthancDatabaseBrowser.orthancDB(onet)
|
|
|
|
|
|
-i=0
|
|
|
|
-project='Orthanc/Database'
|
|
|
|
|
|
|
|
-patients=odb.getPatients()
|
|
|
|
|
|
+ with open(parameterFile) as f:
|
|
|
|
+ pars=json.load(f)
|
|
|
|
|
|
-for p in patients:
|
|
|
|
- pdata=odb.getPatientData(p)
|
|
|
|
- dicom=pdata['MainDicomTags']
|
|
|
|
- patientId=dicom['PatientID']
|
|
|
|
|
|
+ i=0
|
|
|
|
+ opars=pars['Orthanc']
|
|
|
|
+ project=opars['project']
|
|
|
|
|
|
- print("Patient: {} ID: {}".format(p,patientId))
|
|
|
|
-
|
|
|
|
- qfilter={'variable':'PatientId','value':patientId,'oper':'eq'}
|
|
|
|
- ds=db.selectRows(project,'study','Demographics',[qfilter])
|
|
|
|
- if len(ds['rows'])==0:
|
|
|
|
- row={}
|
|
|
|
- row['PatientId']=patientId
|
|
|
|
- row['birthDate']=dicom['PatientBirthDate']
|
|
|
|
- row['PatientName']=dicom['PatientName']
|
|
|
|
- row['OrthancId']=p
|
|
|
|
- db.modifyRows('insert',project,'study','Demographics',[row])
|
|
|
|
-
|
|
|
|
- for s in pdata['Studies']:
|
|
|
|
- sdata=odb.getStudyData(s)
|
|
|
|
- sdicom=sdata['MainDicomTags']
|
|
|
|
- sid=sdicom['StudyInstanceUID']
|
|
|
|
- print('Study: {}/{}'.format(s,sid))
|
|
|
|
- #print('Data: {}'.format(sdata))
|
|
|
|
- sdate=sdicom['StudyDate']
|
|
|
|
- #continue
|
|
|
|
-
|
|
|
|
-
|
|
|
|
- for se in sdata['Series']:
|
|
|
|
-
|
|
|
|
- qfilter={'variable':'orthancSeries','value':se,'oper':'eq'}
|
|
|
|
- ds=db.selectRows(project,'study','Imaging',[qfilter])
|
|
|
|
- if len(ds['rows'])>0:
|
|
|
|
- continue
|
|
|
|
-
|
|
|
|
- #count existing entries for patient
|
|
|
|
- qfilter={'variable':'PatientId','value':patientId,'oper':'eq'}
|
|
|
|
- ds=db.selectRows(project,'study','Imaging',[qfilter])
|
|
|
|
- seqNum=len(ds['rows'])
|
|
|
|
-
|
|
|
|
- sedata=odb.getSeriesData(se)
|
|
|
|
- sedicom=sedata['MainDicomTags']
|
|
|
|
- seid=sedicom['SeriesInstanceUID']
|
|
|
|
- print('Series: {}/{}'.format(se,seid))
|
|
|
|
- #print('Data: {}'.format(sedata))
|
|
|
|
- seDesc="NONE"
|
|
|
|
- try:
|
|
|
|
- seDesc=sedicom['SeriesDescription']
|
|
|
|
- except KeyError:
|
|
|
|
- pass
|
|
|
|
-
|
|
|
|
- print('ID: {}.'.format(seDesc))
|
|
|
|
|
|
+ participantField=opars['participantField']
|
|
|
|
|
|
|
|
+ patients=odb.getPatients()
|
|
|
|
+ n=len(patients)
|
|
|
|
+
|
|
|
|
+ for p in patients:
|
|
|
|
+ pdata=odb.getPatientData(p)
|
|
|
|
+ dicom=pdata['MainDicomTags']
|
|
|
|
+ patientId=dicom['PatientID']
|
|
|
|
+
|
|
|
|
+ print("[{}/{}] Patient: {} ID: {}".format(i,n,p,patientId))
|
|
|
|
+
|
|
|
|
+ qfilter={'variable':participantField,'value':patientId,'oper':'eq'}
|
|
|
|
+ ds=db.selectRows(project,opars['schemaName'],\
|
|
|
|
+ opars['demographicsQuery'],[qfilter])
|
|
|
|
+ if len(ds['rows'])==0:
|
|
row={}
|
|
row={}
|
|
- row['PatientId']=patientId
|
|
|
|
- row['sequenceNum']=seqNum
|
|
|
|
- row['dicomStudy']=sid
|
|
|
|
- row['orthancStudy']=s
|
|
|
|
- row['dicomSeries']=seid
|
|
|
|
- row['orthancSeries']=se
|
|
|
|
- row['studyDate']=sdate
|
|
|
|
- row['seriesDescription']=seDesc
|
|
|
|
- db.modifyRows('insert',project,'study','Imaging',[row])
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-print("Done")
|
|
|
|
|
|
+ row[participantField]=patientId
|
|
|
|
+ row['birthDate']=dicom['PatientBirthDate']
|
|
|
|
+ row['PatientName']=dicom['PatientName']
|
|
|
|
+ row['OrthancId']=p
|
|
|
|
+ db.modifyRows('insert',project,opars['schemaName'],\
|
|
|
|
+ opars['demographicsQuery'],[row])
|
|
|
|
+
|
|
|
|
+ for s in pdata['Studies']:
|
|
|
|
+ sdata=odb.getStudyData(s)
|
|
|
|
+ sdicom=sdata['MainDicomTags']
|
|
|
|
+ sid=sdicom['StudyInstanceUID']
|
|
|
|
+ print('\tStudy: {}/{}'.format(s,sid))
|
|
|
|
+ #print('Data: {}'.format(sdata))
|
|
|
|
+ sdate=sdicom['StudyDate']
|
|
|
|
+ #continue
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ for se in sdata['Series']:
|
|
|
|
+
|
|
|
|
+ qfilter={'variable':'orthancSeries','value':se,'oper':'eq'}
|
|
|
|
+ ds=db.selectRows(project,opars['schemaName'],\
|
|
|
|
+ opars['queryName'],[qfilter])
|
|
|
|
+ if len(ds['rows'])>0:
|
|
|
|
+ continue
|
|
|
|
+
|
|
|
|
+ #count existing entries for patient
|
|
|
|
+ qfilter={'variable':participantField,'value':patientId,'oper':'eq'}
|
|
|
|
+ ds=db.selectRows(project,opars['schemaName'],\
|
|
|
|
+ opars['queryName'],[qfilter])
|
|
|
|
+ seqNum=len(ds['rows'])
|
|
|
|
+
|
|
|
|
+ sedata=odb.getSeriesData(se)
|
|
|
|
+ sedicom=sedata['MainDicomTags']
|
|
|
|
+ seid=sedicom['SeriesInstanceUID']
|
|
|
|
+ #print('Data: {}'.format(sedata))
|
|
|
|
+ seDesc="NONE"
|
|
|
|
+ try:
|
|
|
|
+ seDesc=sedicom['SeriesDescription']
|
|
|
|
+ except KeyError:
|
|
|
|
+ pass
|
|
|
|
+
|
|
|
|
+ print('\t\tSeries[{}]: {}/{}'.format(seDesc,se,seid))
|
|
|
|
+
|
|
|
|
+ row={}
|
|
|
|
+ row[participantField]=patientId
|
|
|
|
+ row['sequenceNum']=seqNum
|
|
|
|
+ row['dicomStudy']=sid
|
|
|
|
+ row['orthancStudy']=s
|
|
|
|
+ row['dicomSeries']=seid
|
|
|
|
+ row['orthancSeries']=se
|
|
|
|
+ row['studyDate']=sdate
|
|
|
|
+ row['seriesDescription']=seDesc
|
|
|
|
+ db.modifyRows('insert',project,opars['schemaName'],\
|
|
|
|
+ opars['queryName'],[row])
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ i+=1
|
|
|
|
+ print("Done")
|
|
|
|
+
|
|
|
|
+if __name__=="__main__":
|
|
|
|
+ main(sys.argv[1])
|