def main(): header_msg = '%s - v%s' % (os.path.basename(sys.argv[0]), __version__) if len(sys.argv) != 5: print '[error] No valid arguments supplied (%s)' % header_msg sys.exit(1) usr_pwd = sys.argv[1] hostname = sys.argv[2] expID = sys.argv[3] in_directory = sys.argv[4] #basic checkings if not os.path.isdir(in_directory): print '[error] Input data location specified is not a valid directory (%s)' % in_directory sys.exit(1) try: with xnatLibrary.XNAT(hostname, usr_pwd) as xnat_connection: get_scans_details(xnat_connection, expID, in_directory) except xnatLibrary.XNATException as xnatErr: print '[error] XNAT-related issue(%s): %s' % (header_msg, xnatErr) sys.exit(1) except Exception as e: print '[Error]', e print(traceback.format_exc()) sys.exit(1)
def main(argument_list): header_msg = '%s - v%s' % (os.path.basename(argument_list[0]), __version__) if len(argument_list) != 7: print '[error] No valid arguments supplied (%s)' % header_msg sys.exit(1) # input argument list usr_pwd = argument_list[1] hostname = argument_list[2] project = argument_list[3] subject = argument_list[4] session = argument_list[5] csv_input_file = argument_list[6] # check the main XML element type (XNAT datatype) xml_element_type = 'AMCZ0:qMRIData' # parse out data from CSV file containing results generated by qMRI run parsed_results = parse_csv_file(csv_input_file) #parsed_results = fix_results_type(parsed_results) # connect to XNAT try: with xnatLibrary.XNAT(hostname, usr_pwd) as xnat_connection: # for each entry (row) in the results file, create an XML object instantiating an XNAT assessment/experiment for scan_results in parsed_results: xml_element = create_xml_obj(scan_results, xml_element_type) # Do some magic :: upload xml object into XNAT (instantiate a new dataType object) assessment_label = normalize_string( session + '_' + xml_element_type.split(':')[1]) assessment_uid = upload_to_XNAT(xnat_connection, project, subject, session, assessment_label, etree.tostring(xml_element), xml_element_type) # quality control image upload output_directory = os.path.dirname( os.path.realpath(csv_input_file)) qc_image_file = os.path.join(output_directory, 'qmri_fits.png') if os.path.isfile(qc_image_file): upload_snapshot_resource(xnat_connection, project, subject, session, assessment_uid, qc_image_file) except xnatLibrary.XNATException as xnatErr: print '[error] XNAT-related issue(%s): %s' % (header_msg, xnatErr) sys.exit(1) except Exception as e: print '[error]', e print(traceback.format_exc()) sys.exit(1)
def main(): header_msg = '%s - v%s' % (os.path.basename(sys.argv[0]), __version__) if len(sys.argv) != 8: print '[error] No valid arguments supplied (%s)' % header_msg sys.exit(1) usr_pwd = sys.argv[1] hostname = sys.argv[2] project = sys.argv[3] subjectID = sys.argv[4] experimentID = sys.argv[5] required_type = sys.argv[6] output_directory = sys.argv[7] resource_format = 'DICOM,NIFTI' #basic checkings on output directory status if not os.path.isdir(output_directory): print '[error] Download location specified is not a valid directory (%s)' % header_msg sys.exit(1) # connect to XNAT try: with xnatLibrary.XNAT(hostname, usr_pwd) as xnat_connection: # get a list of affected scans for the given imaging session process_scan_list = get_scans_list(xnat_connection, project, subjectID, experimentID, required_type) # if the list is empty, no valid scans found for the given imaging session, close and exit if not process_scan_list: print '[error] Unable to find a suitable %s scan for %s (%s)' % ( required_type, experimentID, header_msg) sys.exit(1) # retrieve the scans from XNAT download_scan_list_files(xnat_connection, experimentID, process_scan_list, resource_format, output_directory) except xnatLibrary.XNATException as xnatErr: print '[error] XNAT-related issue(%s): %s' % (header_msg, xnatErr) sys.exit(1) except Exception as e: print '[Error]', e print(traceback.format_exc()) sys.exit(1)
# compose the HTTP basic authentication credentials string if args['password'] is None : password = getpass.getpass('Password for user %s:' %args['username']) else : password = args['password'] usr_pwd = args['username']+':'+password print '' try: #if args['csvFile'] is not None and not os.path.isfile(args['csvFile']) : # raise Exception('CSV file %s not found' %args['csvFile']) # connect to XNAT with xnatLibrary.XNAT(args['hostname'],usr_pwd) as XNAT : if args['verbose'] : print ' [Info] session %s opened' %XNAT.jsession # check if XNAT project exists if XNAT.resourceExist('%s/data/projects/%s' %(XNAT.host,args['project'])).status != 200 : raise xnatLibrary.XNATException('project ("%s") is unreachable at: %s' % (args['project'], XNAT.host) ) # if all went OK, proceed to the main processing stage main(XNAT,args) # disconnect from XNAT if args['verbose'] : print ' [Info] session %s closed' %XNAT.jsession except xnatLibrary.XNATException as xnatErr: print ' [Error] XNAT-related issue:', xnatErr except Exception as e:
parser.add_argument('-t','--type', dest="e_type", help='Entity type/level where to create resource; can either be: {projects,subjects,experiments}', required=True) parser.add_argument('-id','--identifier', dest="e_name", help='Entity name/identifier where to create resource', required=True) parser.add_argument('-i','--input', dest="input", help='Input file/directory location', required=True) parser.add_argument('-rc','--resource_collection', dest="resource_collection", default=None, help='Resource collection name (optional)', required=False) parser.add_argument('-v','--verbose', dest="verbose", action='store_true', default=False, help='Display verbosal information (optional)', required=False) args = vars(parser.parse_args()) # compose the HTTP basic authentication credentials string password = getpass.getpass('Password for user %s:' %args['username']) usr_pwd = args['username']+':'+password print '' try: # connect to XNAT with xnatLibrary.XNAT(args['hostname'],usr_pwd) as xnat_connection : if args['verbose'] : print '[Info] session %s opened' %xnat_connection.jsession main(xnat_connection,args) if args['verbose'] : print '[Info] session %s closed' %xnat_connection.jsession # disconnect from XNAT except xnatLibrary.XNATException as xnatErr: print '[Error] XNAT-related issue:', xnatErr sys.exit(1) except Exception as e: print '[Error]', e print(traceback.format_exc()) sys.exit(1)
def main(argument_list): header_msg = '%s - v%s' % (os.path.basename(argument_list[0]), __version__) if len(argument_list) != 7: print '[error] No valid arguments supplied (%s)' % header_msg sys.exit(1) # input argument list usr_pwd = argument_list[1] hostname = argument_list[2] project = argument_list[3] csv_input_file = argument_list[4] qap_analysis_type = argument_list[5] # either {temporal, spatial} scan_type = argument_list[6] # either {anat, func} # check the main XML element type (XNAT datatype) if 'anat' in scan_type.lower(): xml_element_type = 'AMCZ0:anatQA' elif 'func' in scan_type.lower(): if qap_analysis_type.lower() == 'spatial': xml_element_type = 'AMCZ0:fspatQA' elif qap_analysis_type.lower() == 'temporal': xml_element_type = 'AMCZ0:ftempQA' if xml_element_type not in [ 'AMCZ0:anatQA', 'AMCZ0:fspatQA', 'AMCZ0:ftempQA' ]: print '[error] No valid QAP or scan type recognized (%s)' % header_msg sys.exit(1) # parse out data from CSV file containing results generated by QAP run parsed_results = parse_csv_file(csv_input_file) parsed_results = fix_results_type(parsed_results) # connect to XNAT try: with xnatLibrary.XNAT(hostname, usr_pwd) as xnat_connection: # for each entry (row) in the results file, create an XML object instantiating an XNAT assessment/experiment for scan_results in parsed_results: scan_id = scan_results['scan'].split('_')[1] scan_type = xnat_scan_type = get_scan_type_xnat( xnat_connection, project, scan_results['subject'], scan_results['session'], scan_id) xml_element = create_xml_obj(scan_results, xml_element_type, scan_id, scan_type) # Do some magic :: upload xml object into XNAT (instantiate a new dataType object) #assessment_label = normalize_string( scan_results['subject'] + '_' + scan_results['session'] + '_s' + scan_results['scan'].split('_')[1] + '_' + xml_element_type.split(':')[1] ) #let's shorten it, subject info is not required (session label should suffice) assessment_label = normalize_string( scan_results['session'] + '_s' + scan_results['scan'].split('_')[1] + '_' + xml_element_type.split(':')[1]) assessment_uid = upload_to_XNAT(xnat_connection, project, scan_results['subject'], scan_results['session'], assessment_label, etree.tostring(xml_element), xml_element_type) # snapshot images generation and upload qap_output_directory = os.path.dirname( os.path.realpath(csv_input_file)) matched_results = qap_snapshot_creator.find_pdf_files( qap_output_directory, scan_results['scan']) if matched_results: for pdf_file in matched_results: png_file = qap_snapshot_creator.convert_to_img( pdf_file) qap_snapshot_creator.upload_snapshot_resource( xnat_connection, project, scan_results['subject'], scan_results['session'], assessment_uid, png_file) os.remove(png_file) except xnatLibrary.XNATException as xnatErr: print '[error] XNAT-related issue(%s): %s' % (header_msg, xnatErr) sys.exit(1) except Exception as e: print '[error]', e print(traceback.format_exc()) sys.exit(1)