loadPatient.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. idFilter={'variable':'EMBRACE_ID','value':label,'oper':'eq'}
  17. ctFilter={'variable':'type','value':'CT','oper':'eq'}
  18. dsCT=db.selectRows(project,'study','ImagingVisitsManaged',[idFilter,ctFilter])
  19. returnObj={}
  20. try:
  21. ctRow=dsCT['rows'][0]
  22. except IndexError:
  23. return returnObj
  24. oiCode=getOICode(db,project,label)
  25. #load ct
  26. seqNum=ctRow['pseudoTransverseID']
  27. oiFilter={'variable':'PatientId','value':oiCode,'oper':'eq'}
  28. sFilter={'variable':'SequenceNum','value':'{:.0f}'.format(seqNum),'oper':'eq'}
  29. dsDicom=db.selectRows('Test/Transfer','study','Imaging',[oiFilter,sFilter])
  30. try:
  31. rDicom=dsDicom['rows'][0]
  32. except IndexError:
  33. return returnObj
  34. iPath=['dicom',rDicom['Study'],rDicom['Series']]
  35. dicomReadFilter={'seriesNumber':"SeriesLabel",
  36. 'studyInstanceUid':None,
  37. 'frameOfReferenceInstanceUid':None}
  38. ctVolumes=iDicom.loadVolumes(fb,'Test/Transfer',iPath,dicomReadFilter)
  39. ctNode=ctVolumes[0]['node']
  40. ctNode.SetName(label+'_CT')
  41. #load dmr
  42. #uses different server!!!
  43. dmrFilter={'variable':'type','value':'DMR','oper':'eq'}
  44. dsDMR=db.selectRows(project,'study','ImagingVisitsManaged',[idFilter,dmrFilter])
  45. try:
  46. dmrRow=dsDMR['rows'][0]
  47. except IndexError:
  48. return returnObj
  49. dmrName=dmrRow['Name']
  50. iPath=['PRETVORJENE',dmrName,'DICOM']
  51. dicomReadFilter={'seriesNumber':"SeriesLabel",
  52. 'sequenceName':"*tse2d1_3",
  53. 'percentPhaseFieldOfView':90,
  54. 'studyInstanceUid':None,
  55. 'frameOfReferenceInstanceUid':None,
  56. 'scanningSequence':None,
  57. 'sequenceVariant':None}
  58. dmrVolumes=iDicom.loadVolumes(fbDMR,project,iPath,dicomReadFilter)
  59. try:
  60. dmrNode=dmrVolumes[0]['node']
  61. dmrNode.SetName(label+'_DMR')
  62. except IndexError:
  63. return returnObj
  64. #load CT-RS
  65. ctrsFilter={'variable':'type','value':'CT-RS','oper':'eq'}
  66. dsCTRS=db.selectRows(project,'study','ImagingVisitsManaged',[idFilter,ctrsFilter])
  67. try:
  68. ctrsRow=dsCTRS['rows'][0]
  69. print('CTRS: {}'.format(len(dsCTRS['rows'])))
  70. except IndexError:
  71. return returnObj
  72. seqNum=ctrsRow['pseudoTransverseID']
  73. sFilter={'variable':'SequenceNum','value':'{:.0f}'.format(seqNum),'oper':'eq'}
  74. dsDicomRT=db.selectRows('Test/Transfer','study','Imaging',[oiFilter,sFilter])
  75. try:
  76. rDicomRT=dsDicomRT['rows'][0]
  77. print('CT-RS Dicom: {}'.format(len(dsDicomRT['rows'])))
  78. print('ID: {}'.format(rDicomRT['PatientId']))
  79. except IndexError:
  80. return returnObj
  81. iPath=['dicom',rDicomRT['Study'],rDicomRT['Series']]
  82. dicomReadFilter={'seriesNumber':"SeriesLabel",
  83. 'studyInstanceUid':None,
  84. 'frameOfReferenceInstanceUid':None}
  85. rtSeg=lDicom.loadSegmentations(fb,'Test/Transfer',iPath,dicomReadFilter)
  86. rtNode=rtSeg[0]['node']
  87. rtNode.SetName(label+'_CTRS')
  88. return {'CT':ctNode,'DMR':dmrNode,'CTRS':rtNode,
  89. 'metadataCT':ctVolumes[0]['metadata'],
  90. 'metadataDMR':dmrVolumes[0]['metadata'],
  91. 'metadataCTRS':rtSeg[0]['metadata']
  92. }