populateImagingFromOrthanc.py 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. import os
  2. import json
  3. import re
  4. import sys
  5. import datetime
  6. import re
  7. fhome=os.path.expanduser('~')
  8. sys.path.insert(1,fhome+'/software/src/labkeyInterface')
  9. import labkeyInterface
  10. import labkeyDatabaseBrowser
  11. fconfig=os.path.join(fhome,'.labkey','network.json')
  12. net=labkeyInterface.labkeyInterface()
  13. net.init(fconfig)
  14. db=labkeyDatabaseBrowser.labkeyDB(net)
  15. i=0
  16. projectOrthanc='Orthanc/Database'
  17. inputDataset='Imaging'
  18. projectStudy='iPNUMMretro/Study'
  19. outputDataset='Imaging1'
  20. ds=db.selectRows(projectOrthanc,'study',inputDataset,[])
  21. #single entry for the patientId/dicomStudy pair
  22. selectVars=['PatientId','dicomStudy']
  23. dates=[datetime.datetime.strptime(row['studyDate'],'%Y/%m/%d %H:%M:%S') for row in ds['rows']]
  24. idx=sorted(range(len(dates)),key=lambda k:dates[k])
  25. for j in range(len(dates)):
  26. #row in ds['rows']:
  27. row=ds['rows'][idx[j]]
  28. #skip series which don't match selected filters
  29. outvar='NONE'
  30. sd=row['seriesDescription']
  31. if sd=='PET WB':
  32. outvar='PETWB'
  33. if sd.find('CT WB')==0:
  34. if sd.find('fov')<0:
  35. outvar='CT'
  36. if outvar=='NONE':
  37. continue
  38. filters=[]
  39. for v in selectVars:
  40. filters.append({'variable':v,'value':row[v],'oper':'eq'})
  41. ds2=db.selectRows(projectStudy,'study',outputDataset,
  42. [{'variable':'PatientId','value':row['PatientId'],'oper':'eq'}])
  43. ds1=db.selectRows(projectStudy,'study',outputDataset,filters)
  44. if len(ds1['rows'])>1:
  45. print('ERROR: too many matches for {}/{}'.format(row['PatientId'],row['dicomStudy']))
  46. continue
  47. mode='update'
  48. outRow={}
  49. if len(ds1['rows'])==0:
  50. mode='insert'
  51. outRow['PatientId']=row['PatientId']
  52. outRow['SequenceNum']=len(ds2['rows'])
  53. outRow['dicomStudy']=row['dicomStudy']
  54. else:
  55. outRow=ds1['rows'][0]
  56. outRow[outvar]=row['orthancSeries']
  57. outRow['studyDate']=row['studyDate']
  58. status=db.modifyRows(mode,projectStudy,'study',outputDataset,[outRow])
  59. print('{}'.format(status))
  60. if j==50:
  61. break
  62. print("Done")