Created
December 1, 2020 15:34
-
-
Save micabe/06220b46c669ec19e3af18fcf674e55d to your computer and use it in GitHub Desktop.
This file contains 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
#!/usr/bin/env python | |
import argparse | |
import os | |
import tempfile | |
import shutil | |
import json | |
import dicom | |
import requests | |
from soma.archive import pack, unpack | |
from commons.helpers.analyses import get_analyses_dir | |
from commons.helpers.api_calls import ApiInformation | |
from qynapse.dicom.dataset import Dataset | |
from qynapse.dicom.clinical_info import compute_clinical_info | |
def add_qyscore_analysis(directory, storescu, aec, user, apiUrl, apiHost): | |
application = 'QyScore' | |
patient = None | |
acquisition = None | |
clinicalInfos = None | |
for root, dirs, files in os.walk(directory): | |
for filename in files: | |
try: | |
ds = dicom.read_file(os.path.join(root, filename)) | |
patient = Dataset.get_patient_name(ds) | |
chars_to_remove = ['\x00', '\x09', '\x20'] | |
patient = patient.translate(None, ''.join(chars_to_remove)) | |
acquisition = Dataset.get_study_date_and_time(ds) | |
clinicalInfos = compute_clinical_info(ds) | |
except: | |
continue | |
if patient is None or acquisition is None: | |
raise ValueError('Could not find required data in input files.') | |
inputDict = { | |
'analysis': { | |
'inputs': { | |
'subject_name': patient, | |
'subject_age': clinicalInfos['subject'][ | |
'subject_age'], | |
'subject_sex': clinicalInfos['subject'][ | |
'subject_sex'], | |
'acquisition': acquisition, | |
'user': user, | |
'aec': aec, | |
'storescu': storescu | |
} | |
}, | |
'mode': 'single', | |
'application': application, | |
'patient': patient, | |
'acquisition': acquisition, | |
} | |
analysisResponse = requests.post( | |
apiUrl + '/analyses', json=inputDict, headers={'API-Host': apiHost}, verify=False) | |
analysis = json.loads(analysisResponse.text.encode('utf-8'))['analysis'] | |
try: | |
analysisId = analysis['id'] | |
analysisPath = os.path.join(get_analyses_dir(), str(analysisId)) | |
pack(os.path.join(analysisPath, 'input_dicom.zip'), | |
os.path.abspath(directory)) | |
except Exception as e: | |
raise e | |
def main(): | |
parser = argparse.ArgumentParser(description='''Run QyScore.''') | |
parser.add_argument('-i', '--input', help='ZIP input') | |
parser.add_argument('-d', '--directory', help='Directory input') | |
parser.add_argument('-s', '--storescu', help='Request storescu', | |
action='store_true', dest='storescu', default=False) | |
parser.add_argument('-c', '--aec', help='Called AE') | |
parser.add_argument('-u', '--user', help='User') | |
parser.add_argument('-e', '--envPath', help='QyScore env path', | |
required=True) | |
args = parser.parse_args() | |
directory = args.directory | |
deleteDirectory = False | |
if args.input is not None: | |
directory = tempfile.mktemp() | |
unpack(args.input, directory) | |
deleteDirectory = True | |
apiInfo = ApiInformation() | |
add_qyscore_analysis(directory, | |
args.storescu, args.aec, args.user, | |
apiInfo.get_url(), apiInfo.get_ip_address()) | |
if deleteDirectory: | |
shutil.rmtree(directory) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment