scanOrthanc.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. #scans orthanc database and updates Demographics datataset and
  2. #series list in project Imaging dataset
  3. import os
  4. import json
  5. import re
  6. import sys
  7. fhome=os.path.expanduser('~')
  8. fsetup=os.path.join(fhome,'.labkey','setup.json')
  9. with open(fsetup,'r') as f:
  10. setup=json.load(f)
  11. sys.path.insert(0,setup['paths']['labkeyInterface'])
  12. import labkeyInterface
  13. import labkeyDatabaseBrowser
  14. sys.path.insert(0,setup['paths']['orthancInterface'])
  15. import orthancInterface
  16. import orthancDatabaseBrowser
  17. fconfig=os.path.join(fhome,'.labkey','network.json')
  18. net=labkeyInterface.labkeyInterface()
  19. net.init(fconfig)
  20. db=labkeyDatabaseBrowser.labkeyDB(net)
  21. onet=orthancInterface.orthancInterface()
  22. onet.init(fconfig)
  23. odb=orthancDatabaseBrowser.orthancDB(onet)
  24. i=0
  25. project='Orthanc/Database'
  26. patients=odb.getPatients()
  27. for p in patients:
  28. print("Extracting patient: {}".format(p))
  29. pdata=odb.getPatientData(p)
  30. dicom=pdata['MainDicomTags']
  31. patientId=dicom['PatientID']
  32. queryPatientId=re.sub(r' ',r'%20',patientId)
  33. print("Patient: {} ID: {}".format(p,patientId))
  34. qfilter={'variable':'ParticipantId','value':queryPatientId,'oper':'eq'}
  35. ds=db.selectRows(project,'study','Demographics',[qfilter])
  36. if len(ds['rows'])==0:
  37. row={}
  38. row['ParticipantId']=patientId
  39. try:
  40. row['birthDate']=dicom['PatientBirthDate']
  41. row['PatientName']=dicom['PatientName']
  42. except KeyError:
  43. pass
  44. row['OrthancId']=p
  45. db.modifyRows('insert',project,'study','Demographics',[row])
  46. for s in pdata['Studies']:
  47. sdata=odb.getStudyData(s)
  48. sdicom=sdata['MainDicomTags']
  49. sdate='19700101'
  50. try:
  51. sid=sdicom['StudyInstanceUID']
  52. sdate=sdicom['StudyDate']
  53. print('Study: {}/{}'.format(s,sid))
  54. except KeyError:
  55. pass
  56. #print('Data: {}'.format(sdata))
  57. #continue
  58. for se in sdata['Series']:
  59. qfilter={'variable':'orthancSeries','value':se,'oper':'eq'}
  60. ds=db.selectRows(project,'study','Imaging',[qfilter])
  61. if len(ds['rows'])>0:
  62. continue
  63. #count existing entries for patient
  64. qfilter={'variable':'ParticipantId','value':queryPatientId,'oper':'eq'}
  65. ds=db.selectRows(project,'study','Imaging',[qfilter])
  66. seqNum=len(ds['rows'])
  67. sedata=odb.getSeriesData(se)
  68. sedicom=sedata['MainDicomTags']
  69. seid=sedicom['SeriesInstanceUID']
  70. print('Series: {}/{}'.format(se,seid))
  71. #print('Data: {}'.format(sedata))
  72. seDesc="NONE"
  73. try:
  74. seDesc=sedicom['SeriesDescription']
  75. except KeyError:
  76. pass
  77. #remove strange characters
  78. spanishOAcute=''.join([chr(3619),chr(3603)])
  79. spanishAAcute=''.join([chr(3619),chr(3585)])
  80. seDesc=re.sub(spanishOAcute,'o',seDesc)
  81. seDesc=re.sub(spanishAAcute,'a',seDesc)
  82. #print("seDesc")
  83. #fc=[ord(c) for c in seDesc]
  84. #for f in fc:
  85. # print("{}".format(f))
  86. #
  87. print('ID: {}.'.format(seDesc))
  88. row={}
  89. row['ParticipantId']=patientId
  90. row['sequenceNum']=seqNum
  91. row['dicomStudy']=sid
  92. row['orthancStudy']=s
  93. row['dicomSeries']=seid
  94. row['orthancSeries']=se
  95. row['studyDate']=sdate
  96. row['seriesDescription']=seDesc
  97. db.modifyRows('insert',project,'study','Imaging',[row])
  98. print("Done")