#scans orthanc database and updates Demographics datataset and #series list in project Imaging dataset import os import json import re import sys fhome=os.path.expanduser('~') fsetup=os.path.join(fhome,'.labkey','setup.json') 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']['orthancInterface']) import orthancInterface import orthancDatabaseBrowser fconfig=os.path.join(fhome,'.labkey','network.json') net=labkeyInterface.labkeyInterface() net.init(fconfig) db=labkeyDatabaseBrowser.labkeyDB(net) onet=orthancInterface.orthancInterface() onet.init(fconfig) odb=orthancDatabaseBrowser.orthancDB(onet) i=0 project='Orthanc/Database' patients=odb.getPatients() for p in patients: print("Extracting patient: {}".format(p)) pdata=odb.getPatientData(p) dicom=pdata['MainDicomTags'] patientId=dicom['PatientID'] queryPatientId=re.sub(r' ',r'%20',patientId) print("Patient: {} ID: {}".format(p,patientId)) qfilter={'variable':'ParticipantId','value':queryPatientId,'oper':'eq'} ds=db.selectRows(project,'study','Demographics',[qfilter]) if len(ds['rows'])==0: row={} row['ParticipantId']=patientId try: row['birthDate']=dicom['PatientBirthDate'] row['PatientName']=dicom['PatientName'] except KeyError: pass row['OrthancId']=p db.modifyRows('insert',project,'study','Demographics',[row]) for s in pdata['Studies']: sdata=odb.getStudyData(s) sdicom=sdata['MainDicomTags'] sdate='19700101' try: sid=sdicom['StudyInstanceUID'] sdate=sdicom['StudyDate'] print('Study: {}/{}'.format(s,sid)) except KeyError: pass #print('Data: {}'.format(sdata)) #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':'ParticipantId','value':queryPatientId,'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 #remove strange characters spanishOAcute=''.join([chr(3619),chr(3603)]) spanishAAcute=''.join([chr(3619),chr(3585)]) seDesc=re.sub(spanishOAcute,'o',seDesc) seDesc=re.sub(spanishAAcute,'a',seDesc) #print("seDesc") #fc=[ord(c) for c in seDesc] #for f in fc: # print("{}".format(f)) # print('ID: {}.'.format(seDesc)) row={} row['ParticipantId']=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")