-
-
Save bdunnette/cec9d7ab044314f343c291eb1371d522 to your computer and use it in GitHub Desktop.
Save Aperio annotations to XML files
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
USERNAME="user" | |
PASSWORD="pass" | |
IMAGE_ENDPOINT="https://images.pathology.umn.edu/dataserver/Aperio.Images/Image" | |
SECURITY_ENDPOINT="https://images.pathology.umn.edu/dataserver/Aperio.Security/Security2" | |
XML_PATH='G:\My Drive\Projects\Metzger\AnnotationBackup' | |
PROJECT_ID=288 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
import time | |
import os | |
import xml.etree.ElementTree as ET | |
from decouple import config | |
IMAGE_ENDPOINT = config( | |
'IMAGE_ENDPOINT', default='https://images.pathology.umn.edu/dataserver/Aperio.Images/Image') | |
SECURITY_ENDPOINT = config( | |
'SECURITY_ENDPOINT', default='https://images.pathology.umn.edu/dataserver/Aperio.Security/Security2') | |
USERNAME = config('USERNAME') | |
PASSWORD = config('PASSWORD') | |
XML_PATH = config('XML_PATH') | |
projectId = config('PROJECT_ID') | |
ns = {'aperio': 'http://www.aperio.com/webservices/'} | |
def getAnnotations(imageId, accessionNumber): | |
headers['SOAPAction'] = 'GetAnnotations' | |
data = '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns0="http://www.aperio.com/webservices/">\ | |
<soapenv:Body>\ | |
<ns0:GetAnnotations>\ | |
<ns0:Token>%s</ns0:Token>\ | |
<ns0:ImageId>%s</ns0:ImageId>\ | |
</ns0:GetAnnotations>\ | |
</soapenv:Body>\ | |
</soapenv:Envelope>' % (token, imageId) | |
# print(data) | |
r = requests.post(IMAGE_ENDPOINT, data=data, headers=headers) | |
# print(r.text) | |
annotations_xml = r.text.split('<AnnotationsNode>')[ | |
1].split('</AnnotationsNode>')[0] | |
# print(annotations_xml) | |
t = time.strftime("%Y%m%d-%H%M", time.localtime()) | |
# print(t) | |
filename = '%s-annotations-%s.xml' % (imageId, t) | |
# print(filename) | |
filepath = os.path.join(XML_PATH, filename) | |
print(filepath) | |
with open(filepath, 'w') as f: | |
f.write(annotations_xml) | |
headers = {'Content-Type': 'text/xml', | |
'SOAPAction': 'Logon'} | |
data = '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns0="http://www.aperio.com/webservices/">\ | |
<soapenv:Body>\ | |
<ns0:Logon>\ | |
<ns0:UserName>%s</ns0:UserName>\ | |
<ns0:PassWord>%s</ns0:PassWord>\ | |
</ns0:Logon>\ | |
</soapenv:Body>\ | |
</soapenv:Envelope>' % (USERNAME, PASSWORD) | |
r = requests.post(SECURITY_ENDPOINT, data=data, headers=headers) | |
resp_xml = ET.fromstring(r.text) | |
token = resp_xml.find('.//aperio:LogonResponse/aperio:Token', ns).text | |
if token is not None: | |
print(token) | |
else: | |
print(r.text) | |
headers['SOAPAction'] = 'GetChildList' | |
data = '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns0="http://www.aperio.com/webservices/">\ | |
<soapenv:Body>\ | |
<ns0:GetChildList>\ | |
<ns0:Token>%s</ns0:Token>\ | |
<ns0:ParentTableName>Project</ns0:ParentTableName>\ | |
<ns0:ParentId>%i</ns0:ParentId>\ | |
<ns0:ChildTableName>Specimen</ns0:ChildTableName>\ | |
</ns0:GetChildList>\ | |
</soapenv:Body>\ | |
</soapenv:Envelope>' % (token, projectId) | |
print(data) | |
r = requests.post(IMAGE_ENDPOINT, data=data, headers=headers) | |
# print(r.text) | |
specimens_xml = ET.fromstring(r.text) | |
specimens = specimens_xml.findall( | |
'.//aperio:GetChildListResponse/aperio:GenericDataSet/aperio:DataRow', ns) | |
for specimen in specimens: | |
# print(s) | |
# print(dir(s)) | |
accessionNumber = specimen.find('./aperio:AccessionNumber', ns).text | |
print(accessionNumber) | |
specimenId = int(specimen.find('./aperio:Id', ns).text) | |
headers['SOAPAction'] = 'GetChildList' | |
data = '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns0="http://www.aperio.com/webservices/">\ | |
<soapenv:Body>\ | |
<ns0:GetChildList>\ | |
<ns0:Token>%s</ns0:Token>\ | |
<ns0:ParentTableName>Specimen</ns0:ParentTableName>\ | |
<ns0:ParentId>%i</ns0:ParentId>\ | |
<ns0:ChildTableName>Slide</ns0:ChildTableName>\ | |
</ns0:GetChildList>\ | |
</soapenv:Body>\ | |
</soapenv:Envelope>' % (token, specimenId) | |
print(data) | |
r = requests.post(IMAGE_ENDPOINT, data=data, headers=headers) | |
print(r.text) | |
slides_xml = ET.fromstring(r.text) | |
slides = slides_xml.findall( | |
'.//aperio:GetChildListResponse/aperio:GenericDataSet/aperio:DataRow', ns) | |
print(len(slides)) | |
for slide in slides: | |
imageId = int(slide.find('./aperio:ImageId', ns).text) | |
print(imageId) | |
getAnnotations(imageId, accessionNumber) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment