loadPatient.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. #load all relevant patient data
  2. #requires labkeyInterface through labkeyDatabrowser db, labkeyFileBrowser fb, fbDMR, importDicom iDicom and loadDicom lDicom
  3. def getOICode(db, project, label):
  4. idFilter={"variable":"EMBRACE_ID",
  5. "oper":"eq",
  6. "value":label}
  7. fSet=db.selectRows(project,'study','PopisneStevilke',[idFilter])
  8. try:
  9. row=fSet["rows"][0]
  10. except:
  11. print("OI code for {} not found").format(label)
  12. return None
  13. return row["OICode"]
  14. def load(db,fb,fbDMR,iDicom,lDicom,project,label):
  15. print('loadPatient.load ver: 1.0.3')
  16. lDicom.clearNodes()
  17. idFilter={'variable':'EMBRACE_ID','value':label,'oper':'eq'}
  18. ctFilter={'variable':'type','value':'CT','oper':'eq'}
  19. dsCT=db.selectRows(project,'study','ImagingVisitsManaged',[idFilter,ctFilter])
  20. returnObj={}
  21. try:
  22. ctRow=dsCT['rows'][0]
  23. except IndexError:
  24. return returnObj
  25. oiCode=getOICode(db,project,label)
  26. #load ct
  27. seqNum=ctRow['pseudoTransverseID']
  28. oiFilter={'variable':'PatientId','value':oiCode,'oper':'eq'}
  29. sFilter={'variable':'SequenceNum','value':'{:.0f}'.format(seqNum),'oper':'eq'}
  30. dsDicom=db.selectRows('Test/Transfer','study','Imaging',[oiFilter,sFilter])
  31. try:
  32. rDicom=dsDicom['rows'][0]
  33. except IndexError:
  34. return returnObj
  35. iPath=['dicom',rDicom['Study'],rDicom['Series']]
  36. dicomReadFilter={'seriesNumber':"SeriesLabel",
  37. 'studyInstanceUid':None,
  38. 'frameOfReferenceInstanceUid':None}
  39. ctVolumes=iDicom.loadVolumes(fb,'Test/Transfer',iPath,dicomReadFilter)
  40. ctNode=ctVolumes[0]['node']
  41. ctNode.SetName(label+'_CT')
  42. #load dmr
  43. #uses different server!!!
  44. dmrFilter={'variable':'type','value':'DMR','oper':'eq'}
  45. dsDMR=db.selectRows(project,'study','ImagingVisitsManaged',[idFilter,dmrFilter])
  46. try:
  47. dmrRow=dsDMR['rows'][0]
  48. except IndexError:
  49. return returnObj
  50. dmrName=dmrRow['Name']
  51. iPath=['PRETVORJENE',dmrName,'DICOM']
  52. dicomReadFilter={'seriesNumber':"SeriesLabel",
  53. 'sequenceName':"*tse2d1_3",
  54. 'percentPhaseFieldOfView':90,
  55. 'studyInstanceUid':None,
  56. 'frameOfReferenceInstanceUid':None,
  57. 'scanningSequence':None,
  58. 'sequenceVariant':None}
  59. dmrVolumes=iDicom.loadVolumes(fbDMR,project,iPath,dicomReadFilter)
  60. try:
  61. dmrNode=dmrVolumes[0]['node']
  62. dmrNode.SetName(label+'_DMR')
  63. except IndexError:
  64. return returnObj
  65. #load CT-RS
  66. ctrsFilter={'variable':'type','value':'CT-RS','oper':'eq'}
  67. dsCTRS=db.selectRows(project,'study','ImagingVisitsManaged',[idFilter,ctrsFilter])
  68. try:
  69. ctrsRow=dsCTRS['rows'][0]
  70. print('CTRS: {}'.format(len(dsCTRS['rows'])))
  71. except IndexError:
  72. return returnObj
  73. seqNum=ctrsRow['pseudoTransverseID']
  74. sFilter={'variable':'SequenceNum','value':'{:.0f}'.format(seqNum),'oper':'eq'}
  75. dsDicomRT=db.selectRows('Test/Transfer','study','Imaging',[oiFilter,sFilter])
  76. try:
  77. rDicomRT=dsDicomRT['rows'][0]
  78. print('CT-RS Dicom: {}'.format(len(dsDicomRT['rows'])))
  79. print('ID: {}'.format(rDicomRT['PatientId']))
  80. except IndexError:
  81. return returnObj
  82. iPath=['dicom',rDicomRT['Study'],rDicomRT['Series']]
  83. dicomReadFilter={'seriesNumber':"SeriesLabel",
  84. 'studyInstanceUid':None,
  85. 'frameOfReferenceInstanceUid':None}
  86. rtSeg=lDicom.loadSegmentations(fb,'Test/Transfer',iPath,dicomReadFilter)
  87. rtNode=rtSeg[0]['node']
  88. rtNode.SetName(label+'_CTRS')
  89. return {'CT':ctNode,'DMR':dmrNode,'CTRS':rtNode,
  90. 'metadataCT':ctVolumes[0]['metadata'],
  91. 'metadataDMR':dmrVolumes[0]['metadata'],
  92. 'metadataCTRS':rtSeg[0]['metadata']
  93. }