scanOrthancLocal.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. #labkey/orthanc interface
  2. #scans the orthanc internal database and fills labkey table
  3. #"Orthanc" section expected in the configuration file, with
  4. #"queryName": query to be filled for each image
  5. #"demographicsQuery": query to be filled for every patient
  6. #"schemaName": the schema both queries are part of
  7. #"project": name of the project under labkey that collects Orthanc data,
  8. # typically named Orthanc or similar
  9. #"participantField": sorting participant field label in labkey study
  10. import os
  11. import json
  12. import re
  13. import sys
  14. def main(parameterFile):
  15. fhome=os.path.expanduser('~')
  16. fsetup=os.path.join(fhome,'.labkey','setup.json')
  17. with open(fsetup,'r') as f:
  18. setup=json.load(f)
  19. sys.path.insert(0,setup['paths']['nixWrapper'])
  20. sys.path.insert(0,setup['paths']['labkeyInterface'])
  21. sys.path.insert(0,setup['paths']['orthancInterface'])
  22. import nixWrapper
  23. #nixWrapper.loadLibrary("labkeyInterface")
  24. import labkeyInterface
  25. import labkeyDatabaseBrowser
  26. #nixWrapper.loadLibrary('orthancInterface')
  27. import orthancInterface
  28. import orthancDatabaseBrowser
  29. fconfig=os.path.join(fhome,'.labkey','network.json')
  30. net=labkeyInterface.labkeyInterface()
  31. net.init(fconfig)
  32. db=labkeyDatabaseBrowser.labkeyDB(net)
  33. onet=orthancInterface.orthancInterface()
  34. onet.init(fconfig)
  35. odb=orthancDatabaseBrowser.orthancDB(onet)
  36. with open(parameterFile) as f:
  37. pars=json.load(f)
  38. i=0
  39. opars=pars['Orthanc']
  40. project=opars['project']
  41. participantField=opars['participantField']
  42. patients=odb.getPatients()
  43. n=len(patients)
  44. for p in patients:
  45. pdata=odb.getPatientData(p)
  46. dicom=pdata['MainDicomTags']
  47. patientId=dicom['PatientID']
  48. print("[{}/{}] Patient: {} ID: {}".format(i,n,p,patientId))
  49. qfilter={'variable':participantField,'value':patientId,'oper':'eq'}
  50. ds=db.selectRows(project,opars['schemaName'],\
  51. opars['demographicsQuery'],[qfilter])
  52. if len(ds['rows'])==0:
  53. row={}
  54. row[participantField]=patientId
  55. row['birthDate']=dicom['PatientBirthDate']
  56. row['PatientName']=dicom['PatientName']
  57. row['OrthancId']=p
  58. db.modifyRows('insert',project,opars['schemaName'],\
  59. opars['demographicsQuery'],[row])
  60. for s in pdata['Studies']:
  61. sdata=odb.getStudyData(s)
  62. sdicom=sdata['MainDicomTags']
  63. sid=sdicom['StudyInstanceUID']
  64. #print('Data: {}'.format(sdata))
  65. #this is try/except protetcted in previous version...
  66. sdate="19700101"
  67. try:
  68. sdate=sdicom['StudyDate']
  69. except KeyError:
  70. pass
  71. #continue
  72. print('\tStudy[{}]: {}/{}'.format(sdate,s,sid))
  73. for se in sdata['Series']:
  74. sedata=odb.getSeriesData(se)
  75. sedicom=sedata['MainDicomTags']
  76. seid=sedicom['SeriesInstanceUID']
  77. #print('Data: {}'.format(sedata))
  78. seDesc="NONE"
  79. try:
  80. seDesc=sedicom['SeriesDescription']
  81. except KeyError:
  82. pass
  83. #replace letters that might trip the database
  84. spanishOAcute=''.join([chr(3619),chr(3603)])
  85. spanishAAcute=''.join([chr(3619),chr(3585)])
  86. seDesc=re.sub(spanishOAcute,'o',seDesc)
  87. seDesc=re.sub(spanishAAcute,'a',seDesc)
  88. print('\t\tSeries[{}]: {}/{}'.format(seDesc,se,seid))
  89. qfilter={'variable':'orthancSeries','value':se,'oper':'eq'}
  90. ds=db.selectRows(project,opars['schemaName'],\
  91. opars['queryName'],[qfilter])
  92. mode='insert'
  93. if len(ds['rows'])>0:
  94. mode='update'
  95. #use existing row data
  96. row=ds['rows'][0]
  97. else:
  98. #count existing entries for patient
  99. qfilter={'variable':participantField,'value':patientId,'oper':'eq'}
  100. ds=db.selectRows(project,opars['schemaName'],\
  101. opars['queryName'],[qfilter])
  102. seqNum=len(ds['rows'])
  103. #create new row
  104. row={}
  105. row[participantField]=patientId
  106. row['sequenceNum']=seqNum
  107. row['orthancSeries']=se
  108. row['dicomStudy']=sid
  109. row['orthancStudy']=s
  110. row['dicomSeries']=seid
  111. row['studyDate']=sdate
  112. row['seriesDescription']=seDesc
  113. db.modifyRows(mode,project,opars['schemaName'],\
  114. opars['queryName'],[row])
  115. i+=1
  116. print("Done")
  117. if __name__=="__main__":
  118. main(sys.argv[1])