populateImagingFromOrthanc.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. #date sorts studies from orthanc dataset into target study dataset
  2. import os
  3. import json
  4. import re
  5. import sys
  6. import datetime
  7. import re
  8. fhome=os.path.expanduser('~')
  9. fsetup=os.path.join(fhome,'.labkey','setup.json')
  10. with open(fsetup,'r') as f:
  11. setup=json.load(f)
  12. sys.path.insert(0,setup['paths']['labkeyInterface'])
  13. import labkeyInterface
  14. import labkeyDatabaseBrowser
  15. fconfig=os.path.join(fhome,'.labkey','network.json')
  16. net=labkeyInterface.labkeyInterface()
  17. net.init(fconfig)
  18. db=labkeyDatabaseBrowser.labkeyDB(net)
  19. i=0
  20. #from orthancDatabase/Imaging dataset
  21. projectOrthanc='Orthanc/Database'
  22. inputDataset='Imaging'
  23. #to target project dataset
  24. projectStudy='iPNUMMretro/Study'
  25. #for prospective, set
  26. projectStudy='IPNUMMprospektiva/Study'
  27. outputDataset='Imaging1'
  28. #select patientId that are contained in the demographics dataset
  29. demographicDataset='ClinicalData'
  30. #for prospective
  31. demographicDataset='demographicData'
  32. orthancParticipantField='PatientId'
  33. participantField='PatientId'
  34. #for prospective
  35. participantField='ParticipantId'
  36. #make a list of patients
  37. dsDemo=db.selectRows(projectStudy,'study',demographicDataset,[])
  38. patients=[row[participantField] for row in dsDemo['rows']]
  39. patients=list(set(patients))
  40. patientListStr=""
  41. for p in patients:
  42. if len(patientListStr)>0:
  43. patientListStr+=";"
  44. patientListStr+=p
  45. patientFilter={'variable':orthancParticipantField,
  46. 'value':patientListStr,'oper':'in'}
  47. #takes orthanc as the baseline, selects from patient list
  48. ds=db.selectRows(projectOrthanc,'study',inputDataset,[patientFilter])
  49. #single entry for the patientId/dicomStudy pair
  50. selectVars={participantField:orthancParticipantField,\
  51. 'dicomStudy':'dicomStudy'}
  52. dates=[datetime.datetime.strptime(row['studyDate'],'%Y/%m/%d %H:%M:%S') \
  53. for row in ds['rows']]
  54. #date sorted entries
  55. idx=sorted(range(len(dates)),key=lambda k:dates[k])
  56. #historical traverse of all studies from inputDataset
  57. for j in range(len(dates)):
  58. row=ds['rows'][idx[j]]
  59. #skip series which don't match selected filters
  60. outvar='NONE'
  61. sd=row['seriesDescription']
  62. if sd=='PET WB':
  63. outvar='PETWB_orthancId'
  64. if sd.find('CT WB')==0:
  65. if sd.find('fov')<0:
  66. outvar='CT_orthancId'
  67. #skip irrelevant series
  68. if outvar=='NONE':
  69. continue
  70. filters=[]
  71. for v in selectVars:
  72. filters.append({'variable':v,\
  73. 'value':row[selectVars[v]],'oper':'eq'})
  74. #ds2 are all studies by patient from sorted dataset
  75. ds2=db.selectRows(projectStudy,'study',outputDataset,
  76. [{'variable':participantField,\
  77. 'value':row[orthancParticipantField],'oper':'eq'}])
  78. #ds1 is the matching row from output dataset
  79. ds1=db.selectRows(projectStudy,'study',outputDataset,filters)
  80. if len(ds1['rows'])>1:
  81. print('ERROR: too many matches for {}/{}'.\
  82. format(row[orthancParticipantField],row['dicomStudy']))
  83. continue
  84. mode='update'
  85. outRow={}
  86. if len(ds1['rows'])==0:
  87. mode='insert'
  88. outRow[participantField]=row[orthancParticipantField]
  89. #setting sequence number to length of already included studies
  90. #sorted by date makes it historically incremental
  91. outRow['SequenceNum']=len(ds2['rows'])
  92. outRow['dicomStudy']=row['dicomStudy']
  93. else:
  94. outRow=ds1['rows'][0]
  95. outRow[outvar]=row['orthancSeries']
  96. outRow['studyDate']=row['studyDate']
  97. status=db.modifyRows(mode,projectStudy,'study',outputDataset,[outRow])
  98. print('{}'.format(status))
  99. if j==50:
  100. break
  101. print("Done")