Andrej vor 4 Jahren
Ursprung
Commit
404538e450
4 geänderte Dateien mit 173 neuen und 6 gelöschten Zeilen
  1. 26 2
      orthancDatabaseBrowser.py
  2. 14 2
      orthancFileBrowser.py
  3. 15 2
      orthancInterface.py
  4. 118 0
      pythonScripts/scanOrthanc.py

+ 26 - 2
orthancDatabaseBrowser.py

@@ -11,12 +11,18 @@ class orthancDB:
     def __init__(self,net):
         self.net=net
 
-    def getPatients(self):
+    def getList(self,category):
         url=self.net.getCoreURL()
-        url+='/patients'
+        url+='/'+category
         response=self.net.get(url)
         return extractJSON(response.data)
+    
+    def getPatients(self):
+        return self.getList('patients')
 
+    def getStudies(self):
+        return self.getList('studies')
+    
     def getPatientData(self, orthancId):
         return self.getData('patients',orthancId)
 
@@ -33,6 +39,16 @@ class orthancDB:
         return extractJSON(response.data)
 
 
+    def getDicomField(self,orthancInstanceId,dicomField):
+        url=self.net.getCoreURL()
+        url+='/instances/'+orthancInstanceId+'/content/'+dicomField
+        response=self.net.get(url)
+        try:
+            encoding=chardet.detect(response.data)["encoding"]
+            return response.data.decode(encoding)
+        except TypeError:
+            return ''
+
     def selectRows(self, qfilter):
         obj={}
         obj["Level"]="Instance"
@@ -54,5 +70,13 @@ class orthancDB:
         encoding=chardet.detect(response.data)["encoding"]
         return json.loads(response.data.decode(encoding))
 
+    def remove(self,level,orthancId):
+        url=self.net.getCoreURL()
+        url+='/'+level+'/'+orthancId
+        response=self.net.delete(url)
+        print(response.data)
+
+
+
 
 

+ 14 - 2
orthancFileBrowser.py

@@ -9,14 +9,24 @@ class orthancFileBrowser:
 
     def getZip(self,retrieveLevel, dataId, path):
         url=self.net.getCoreURL()
-        url+="/"+retrieveLevel+"/"+dataId+"/archive";
+        url+="/"+retrieveLevel+"/"+dataId+"/archive"
         print("Using: {}".format(url))
-        response=self.net.get(url,binary=True);
+        response=self.net.get(url,binary=True)
         with open(path,'wb') as out_file:
             shutil.copyfileobj(response,out_file)
         response.release_conn()
         #response.data is a byte array -> is the same as file
 
+    def getInstance(self, instanceId, localFile):
+        url=self.net.getCoreURL()
+        url+='/instances/'+instanceId+'/file'
+        print('Using {}'.format(url))
+        response=self.net.get(url,binary=True)
+        with open(localFile,'wb') as out_file:
+            shutil.copyfileobj(response,out_file)
+        response.release_conn()
+
+
     def upload(self,path):
         url=self.net.getCoreURL()
         url+="/instances"
@@ -24,3 +34,5 @@ class orthancFileBrowser:
             response=self.net.post(url,"octet-stream", f.read())
         print(response.data)
 
+    
+

+ 15 - 2
orthancInterface.py

@@ -43,7 +43,6 @@ class orthancInterface:
         debug=False
         if debug:
             print("GET: {0}").format(url)
-            print("as {0}").format(user)
         headers=urllib3.util.make_headers(basic_auth=self.getBasicAuth())
         try:
             if not binary:
@@ -89,7 +88,21 @@ class orthancInterface:
         #f contains json as a return value
         except urllib3.exceptions.HTTPError as e:
             print(e)
-      
+     
+
+    def delete(self,url):
+
+        debug=False
+        if debug:
+            print("DELETE: {0}").format(url)
+        
+        headers=urllib3.util.make_headers(basic_auth=self.getBasicAuth())
+        try:
+            return self.http.request('DELETE',url,headers=headers)
+        except urllib3.exceptions.HTTPError as e:
+            print(e)
+            return None
+ 
 
             
 

+ 118 - 0
pythonScripts/scanOrthanc.py

@@ -0,0 +1,118 @@
+import os
+import json
+import re
+import sys
+
+
+fhome=os.path.expanduser('~')
+sys.path.insert(1,fhome+'/software/src/labkeyInterface')
+import labkeyInterface
+import labkeyDatabaseBrowser
+
+sys.path.insert(1,fhome+'/software/src/orthancInterface')
+import orthancInterface
+import orthancDatabaseBrowser
+
+fconfig=os.path.join(fhome,'.labkey','network.json')
+
+net=labkeyInterface.labkeyInterface()
+net.init(fconfig)
+db=labkeyDatabaseBrowser.labkeyDB(net)
+
+
+onet=orthancInterface.orthancInterface()
+onet.init(fconfig)
+odb=orthancDatabaseBrowser.orthancDB(onet)
+
+i=0
+project='Orthanc/Database'
+
+patients=odb.getPatients()
+
+for p in patients:
+    print("Extracting patient: {}".format(p))
+    pdata=odb.getPatientData(p)
+    dicom=pdata['MainDicomTags']
+    patientId=dicom['PatientID']
+
+    queryPatientId=re.sub(r' ',r'%20',patientId)
+    
+    print("Patient: {} ID: {}".format(p,patientId))
+
+    qfilter={'variable':'ParticipantId','value':queryPatientId,'oper':'eq'}
+    ds=db.selectRows(project,'study','Demographics',[qfilter])
+    if len(ds['rows'])==0:
+        row={}
+        row['ParticipantId']=patientId
+        try:
+            row['birthDate']=dicom['PatientBirthDate']
+            row['PatientName']=dicom['PatientName']
+        except KeyError:
+            pass
+        row['OrthancId']=p
+        db.modifyRows('insert',project,'study','Demographics',[row])
+
+    for s in pdata['Studies']:
+        sdata=odb.getStudyData(s)
+        sdicom=sdata['MainDicomTags']
+        sdate='19700101'
+        try:
+            sid=sdicom['StudyInstanceUID']
+            sdate=sdicom['StudyDate']
+            print('Study: {}/{}'.format(s,sid))
+        except KeyError:
+            pass
+        #print('Data: {}'.format(sdata))
+        #continue
+        
+        
+        for se in sdata['Series']:
+
+            qfilter={'variable':'orthancSeries','value':se,'oper':'eq'}
+            ds=db.selectRows(project,'study','Imaging',[qfilter])
+            if len(ds['rows'])>0:
+                continue
+
+            #count existing entries for patient
+            qfilter={'variable':'ParticipantId','value':queryPatientId,'oper':'eq'}
+            ds=db.selectRows(project,'study','Imaging',[qfilter])
+            seqNum=len(ds['rows'])
+
+            sedata=odb.getSeriesData(se)
+            sedicom=sedata['MainDicomTags']
+            seid=sedicom['SeriesInstanceUID']
+            print('Series: {}/{}'.format(se,seid))
+            #print('Data: {}'.format(sedata))
+            seDesc="NONE"
+            try:
+                seDesc=sedicom['SeriesDescription']
+            except KeyError:
+                pass
+
+            #remove strange characters
+            spanishOAcute=''.join([chr(3619),chr(3603)])
+            spanishAAcute=''.join([chr(3619),chr(3585)])
+            seDesc=re.sub(spanishOAcute,'o',seDesc)
+            seDesc=re.sub(spanishAAcute,'a',seDesc)
+
+            #print("seDesc")
+            #fc=[ord(c) for c in seDesc]
+            #for f in fc:
+            #    print("{}".format(f))
+            #
+            
+            print('ID: {}.'.format(seDesc))
+
+            row={}
+            row['ParticipantId']=patientId
+            row['sequenceNum']=seqNum
+            row['dicomStudy']=sid
+            row['orthancStudy']=s
+            row['dicomSeries']=seid
+            row['orthancSeries']=se
+            row['studyDate']=sdate
+            row['seriesDescription']=seDesc
+            db.modifyRows('insert',project,'study','Imaging',[row])
+
+
+print("Done")