orthancDatabaseBrowser.py 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. import orthancInterface
  2. import json
  3. import chardet
  4. import io
  5. def extractJSON(data):
  6. encoding=chardet.detect(data)["encoding"]
  7. try:
  8. return json.loads(data.decode(encoding))
  9. except UnicodeDecodeError:
  10. print(f'Failed to decode with [{encoding}]: {data}')
  11. return None
  12. class orthancDB:
  13. def __init__(self,net):
  14. self.net=net
  15. def getList(self,category):
  16. url=self.net.getCoreURL()
  17. url+='/'+category
  18. response=self.net.get(url)
  19. return extractJSON(response.data)
  20. def getPatients(self):
  21. return self.getList('patients')
  22. def getStudies(self):
  23. return self.getList('studies')
  24. def getPatientData(self, orthancId):
  25. return self.getData('patients',orthancId)
  26. def getStudyData(self, studyOrthancId):
  27. return self.getData('studies',studyOrthancId)
  28. def getSeriesData(self, seriesOrthancId):
  29. return self.getData('series',seriesOrthancId)
  30. def getNumpy(self,level,orthancId):
  31. url=self.net.getCoreURL()
  32. url+='/'+level+'/'+orthancId+'/numpy'
  33. response=self.net.get(url,binary=True)
  34. return io.BytesIO(response.data)
  35. def getData(self, level, orthancId):
  36. url=self.net.getCoreURL()
  37. url+='/'+level+'/'+orthancId
  38. response=self.net.get(url)
  39. return extractJSON(response.data)
  40. def getDicomField(self,orthancInstanceId,dicomField):
  41. url=self.net.getCoreURL()
  42. url+='/instances/'+orthancInstanceId+'/content/'+dicomField
  43. response=self.net.get(url)
  44. try:
  45. encoding=chardet.detect(response.data)["encoding"]
  46. return response.data.decode(encoding)
  47. except TypeError:
  48. return ''
  49. def selectRows(self, qfilter):
  50. obj={}
  51. obj["Level"]="Instance"
  52. obj["Query"]=qfilter
  53. jsonData=json.dumps(obj)
  54. url=self.net.getCoreURL()
  55. url+='/tools/find'
  56. response=self.net.post(url,'json',jsonData)
  57. encoding=chardet.detect(response.data)["encoding"]
  58. return json.loads(response.data.decode(encoding))
  59. def upload(self, f) :
  60. url=self.net.getCoreURL()
  61. url+='/instances'
  62. fobj=open (f, 'rb')
  63. response=self.net.post(url,'octet-stream',fobj.read())
  64. try:
  65. encoding=chardet.detect(response.data)["encoding"]
  66. except AttributeError:
  67. print(response)
  68. return json.loads('{"status":"BAD"}')
  69. return json.loads(response.data.decode(encoding))
  70. def remove(self,level,orthancId):
  71. url=self.net.getCoreURL()
  72. url+='/'+level+'/'+orthancId
  73. response=self.net.delete(url)
  74. print(response.data)