orthancDatabaseBrowser.py 2.5 KB

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