populateImagingFromOrthanc.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. sys.path.insert(0,setup['paths']['analysisInterface'])
  16. import analysisInterface
  17. fconfig=os.path.join(fhome,'.labkey','network.json')
  18. net=labkeyInterface.labkeyInterface()
  19. net.init(fconfig)
  20. db=labkeyDatabaseBrowser.labkeyDB(net)
  21. fb=labkeyFileBrowser.labkeyFileBrowser(net)
  22. parameterFile=sys.argv[1]
  23. runid=sys.argv[2];
  24. ana=analysisInterface.analysisInterface(db,fb,runid)
  25. ana.updateStatus(2)
  26. pars=ana.getParameters(parameterFile)
  27. if pars==None:
  28. sys.exit()
  29. i=0
  30. #from orthancDatabase/Imaging dataset
  31. projectOrthanc=pars['Orthanc']['project']
  32. inputQuery=pars['Orthanc']['queryName']
  33. inputSchema=pars['Orthanc']['schemaName']
  34. inputParticipantField=pars['Orthanc']['participantField']
  35. #to target project dataset
  36. projectStudy=pars['Database']['project']
  37. #'iPNUMMretro/Study'
  38. #for prospective, set
  39. #projectStudy='IPNUMMprospektiva/Study'
  40. outputQuery=pars['Database']['queryName']
  41. outputSchema=pars['Database']['schemaName']
  42. #select patientId that are contained in the demographics dataset
  43. listQuery=pars['Database']['listQuery']
  44. dbParticipantField=pars['Database']['participantField']
  45. #make a list of patients
  46. dsDemo=db.selectRows(projectStudy,outputSchema,listQuery,[])
  47. patients=[row[dbParticipantField] for row in dsDemo['rows']]
  48. patients=list(set(patients))
  49. patientListStr=""
  50. for p in patients:
  51. if len(patientListStr)>0:
  52. patientListStr+=";"
  53. patientListStr+=p
  54. patientFilter={'variable':inputParticipantField,
  55. 'value':patientListStr,'oper':'in'}
  56. #takes orthanc as the baseline, selects from patient list
  57. ds=db.selectRows(projectOrthanc,inputSchema,inputQuery,[patientFilter])
  58. #single entry for the patientId/dicomStudy pair
  59. selectVars={dbParticipantField:inputParticipantField,\
  60. 'dicomStudy':'dicomStudy'}
  61. dates=[datetime.datetime.strptime(row['studyDate'],'%Y/%m/%d %H:%M:%S') \
  62. for row in ds['rows']]
  63. #date sorted entries
  64. idx=sorted(range(len(dates)),key=lambda k:dates[k])
  65. #historical traverse of all studies from inputDataset
  66. for j in range(len(dates)):
  67. row=ds['rows'][idx[j]]
  68. #skip series which don't match selected filters
  69. outvar='NONE'
  70. sd=row['seriesDescription']
  71. if sd=='PET WB':
  72. outvar='PETWB_orthancId'
  73. if sd.find('CT WB')==0:
  74. if sd.find('fov')<0:
  75. outvar='CT_orthancId'
  76. #skip irrelevant series
  77. if outvar=='NONE':
  78. continue
  79. filters=[]
  80. for v in selectVars:
  81. filters.append({'variable':v,\
  82. 'value':row[selectVars[v]],'oper':'eq'})
  83. #ds2 are all studies by patient from sorted dataset
  84. ds2=db.selectRows(projectStudy,outputSchema,outputQuery,
  85. [{'variable':dbParticipantField,\
  86. 'value':row[inputParticipantField],'oper':'eq'}])
  87. #ds1 is the matching row from output dataset
  88. ds1=db.selectRows(projectStudy,outputSchema,outputQuery,filters)
  89. if len(ds1['rows'])>1:
  90. print('ERROR: too many matches for {}/{}'.\
  91. format(row[inputParticipantField],row['dicomStudy']))
  92. continue
  93. mode='update'
  94. outRow={}
  95. if len(ds1['rows'])==0:
  96. mode='insert'
  97. outRow[dbParticipantField]=row[inputParticipantField]
  98. #setting sequence number to length of already included studies
  99. #sorted by date makes it historically incremental
  100. outRow['SequenceNum']=len(ds2['rows'])
  101. outRow['dicomStudy']=row['dicomStudy']
  102. else:
  103. outRow=ds1['rows'][0]
  104. outRow[outvar]=row['orthancSeries']
  105. outRow['studyDate']=row['studyDate']
  106. status=db.modifyRows(mode,projectStudy,outputSchema,outputQuery,[outRow])
  107. print('{}'.format(status))
  108. if j==50:
  109. break
  110. print("Done")