def pre_run(self): """Method to download data from XNAT. :param argument_parse: argument parser object return by parse_args() """ resource = 'ACQ' # resource to download from the scan on XNAT folder = os.path.join(self.jobdir, 'inputs') os.makedirs(folder) assessor_label = '-x-'.join([self.xnat_project, self.xnat_subject, self.xnat_session, self.proctype]) xnat = XnatUtils.get_interface() a = xnat.select('/projects/%s/subjects/%s/experiments/%s/assessors/%s' % (self.xnat_project, self.xnat_subject, self.xnat_session, assessor_label)) for nb_acq in range(1, self.nb_acquisition+1): res_name = '%s%d' % (resource, nb_acq) self.time_writer('Download resource: %s' % res_name) file_gzip = XnatUtils.download_file_from_obj( folder, a.out_resource(res_name)) self.time_writer('Unzip file: %s' % file_gzip) XnatUtils.gunzip_file(file_gzip) self.inputs[nb_acq] = file_gzip[:-3] sc = XnatUtils.get_good_scans(a.parent(), DICOM_SCAN_TYPE)[0] dcm_file = XnatUtils.download_file_from_obj( folder, sc.resource('DICOM')) self.inputs['dcm'] = dcm_file xnat.disconnect()
def prerun(self, settings_filename=''): """ prerun function overridden from base-class """ LOGGER.info('loading XNAT...') self.xnat = XnatUtils.get_interface() LOGGER.info('loading REDCap...') self.load_redcap() LOGGER.info('checking Projects of Prearchive Session..') self.load_prearchive() moving = self.check_projects() if moving: # wait for moves to complete LOGGER.info('sessions moving projects, waiting '+str(MOVING_SLEEP_SECS)+' secs') time.sleep(MOVING_SLEEP_SECS) else: LOGGER.info('no sessions changed, continuing to archiving') LOGGER.info('do archiving...') self.load_prearchive() self.do_archiving() LOGGER.info('crosscheck redcap...') self.crosscheck_redcap() self.xnat.disconnect() if self.send_an_email: LOGGER.info('sending email with report of errors') self.send_report() LOGGER.info('DONE')
def copy_inputs(self): self.run_inputs = self.src_inputs for _input in self.copy_list: src = self.src_inputs[_input] if src.startswith('xnat://'): src = src[len('xnat:/'):] _res, _file = src.split('/files/') dst = os.path.join(self.input_dir, _file) print('DEBUG:downloading from XNAT:'+src+' to '+dst) try: xnat = XnatUtils.get_interface(self.host, self.user, self.pwd) res = xnat.select(_res) result = res.file(_file).get(dst) except: print('ERROR:downloading from XNAT') return else: dst = os.path.join(self.input_dir, os.path.basename(src)) print('DEBUG:copying src:'+src+' to '+dst) copyfile(src, dst) self.run_inputs[_input] = dst return self.run_inputs
def prerun(self, settings_filename=''): """ prerun function overridden from base-class """ LOGGER.info('loading XNAT...') self.xnat = XnatUtils.get_interface() LOGGER.info('loading REDCap...') self.load_redcap() LOGGER.info('checking Projects of Prearchive Scans..') self.load_prearchive() self.check_projects() LOGGER.info('do archiving...') self.load_prearchive() self.do_archiving() LOGGER.info('crosscheck redcap...') self.crosscheck_redcap() self.xnat.disconnect() if self.send_an_email: LOGGER.info('sending email with report of errors') self.send_report() LOGGER.info('DONE')
def test_setup_old_assessors(self): intf = XnatUtils.get_interface(host) proj_id = 'proj1' subj_id = 'subj1' sess_ids = ['sess2', 'sess1'] project = intf.select_project(proj_id) if not project.exists(): self.assertTrue(False, 'proj1 should be pre-created for this test') subject = intf.select_subject(proj_id, subj_id) if not subject.exists(): self.assertTrue(False, 'subj1 should be pre-created for this test') for s in sess_ids: session = intf.select_experiment(proj_id, subj_id, s) if not session.exists(): self.assertTrue(False, 'sess1 should be pre-created for this test') # delete and recreate scans scan_descriptors = [('1', 't1'), ('2', 't1'), ('11', 'flair')] ComponentTestBuild._setup_scans(session, scan_descriptors) # delete and recreate old assessors ComponentTestBuild._setup_assessors(proj_id, subj_id, session)
def download(self, obj_label, resource, folder): """ Return a python list of the files downloaded for the resource on the scan example: download(scan_id, "DICOM", "/Users/test") or download(assessor_label, "DATA", "/Users/test") :param obj_label: xnat object label (scan ID or assessor label) :param resource: folder name under the xnat object :param folder: download directory :return: python list of files downloaded """ # Open connection to XNAT xnat = XnatUtils.get_interface(host=self.host, user=self.user, pwd=self.pwd) resource_obj = self.select_obj(intf=xnat, obj_label=obj_label, resource=resource) list_files = XnatUtils.download_files_from_obj( directory=folder, resource_obj=resource_obj) # close connection xnat.disconnect() return list_files
def test_setup_old_assessors(self): intf = XnatUtils.get_interface(host) proj_id = 'proj1' subj_id = 'subj1' sess_ids = ['sess2', 'sess1'] project = intf.select_project(proj_id) if not project.exists(): self.assertTrue(False, 'proj1 should be pre-created for this test') subject = intf.select_subject(proj_id, subj_id) if not subject.exists(): self.assertTrue(False, 'subj1 should be pre-created for this test') for s in sess_ids: session = intf.select_experiment(proj_id, subj_id, s) if not session.exists(): self.assertTrue(False, 'sess1 should be pre-created for this test') # delete and recreate scans scan_descriptors = [('1', 't1'), ('2', 't1'), ('11', 'flair')] ComponentTestBuild._setup_scans(session, scan_descriptors) # delete and recreate old assessors ComponentTestBuild._setup_assessors(proj_id, subj_id, session)
def test_assessor_out_resources(self): with XnatUtils.get_interface(host=host) as intf: cisess = XnatUtils.CachedImageSession(intf, proj_id, subj_id, sess_id) for asr in cisess.assessors(): asrobj = intf.select_assessor(proj_id, subj_id, sess_id, asr.label()) print((asrobj.out_resources()))
def test_xnat_get_full_object(self): with XnatUtils.get_interface(host=host) as intf: intf.connect() print(map(lambda x: x['name'], intf.get_projects())) print(map(lambda x: x['label'], intf.get_subjects(proj_id))) subj = intf.select(XnatUtils.InterfaceTemp.S_XPATH.format(project=proj_id, subject=subj_id)) print(subj.label()) print(subj.parent().label())
def test_xnat_parse_session(self): with XnatUtils.get_interface(host=host) as intf: intf.connect() SanityChecks.__prep_project(intf) yamldoc = YamlDoc().from_string(yamls.proc_a) csess = XnatUtils.CachedImageSession(intf, proj_id, subj_id, sess_id) ap = AutoProcessor(XnatUtils, yamldoc) ap.parse_session(ap)
def test_xnat_parse_session(self): with XnatUtils.get_interface(host=host) as intf: intf.connect() SanityChecks.__prep_project(intf) yamldoc = YamlDoc().from_string(yamls.proc_a) csess = XnatUtils.CachedImageSession( intf, proj_id, subj_id, sess_id) ap = AutoProcessor(XnatUtils, yamldoc) ap.parse_session(ap)
def test_setup_session(self): proj_id = 'proj1' subj_id = 'subj1' sess_ids = ['sess1', 'sess2'] intf = XnatUtils.get_interface(host=host) scan_descriptors = [('1', 't1'), ('2', 't1'), ('11', 'flair'), ('12', 'flair'), ('21', 't2')] for sess_id in sess_ids: session = intf.select_experiment(proj_id, subj_id, sess_id) if not session.exists(): self.assertTrue(False, "no such session") ComponentTestBuild._setup_scans(session, scan_descriptors)
def pre_run(self): """Method to download data from XNAT. :param argument_parse: argument parser object return by parse_args() """ # Make directory input_folder = XnatUtils.makedir(os.path.join(self.jobdir, 'inputs'), subdir=False) # Download scans: self.time_writer('Connection to XNAT') xnat = XnatUtils.get_interface(host=self.host, user=self.user, pwd=self.pwd) # Download NIFTIs index = 1 for scan_id in self.scans_id: scan_info = {} scan_dir = XnatUtils.makedir(os.path.join(input_folder, scan_id), subdir=False) self.time_writer('Downloading scan ID %s ...' % scan_id) scan = XnatUtils.select_obj(xnat, self.xnat_project, self.xnat_subject, self.xnat_session, scan_id) snii_obj = scan.resource('NIFTI') scan_info['4D'] = XnatUtils.download_file_from_obj(scan_dir, snii_obj) # Download DICOMs sdcm_obj = scan.resource('DICOM') self.time_writer('Downloading DICOM for scan ID %s ...' % scan_id) scan_info['dicom'] = XnatUtils.download_file_from_obj(scan_dir, sdcm_obj) scan_info['type'] = scan.attrs.get('type') scan_info['ID'] = scan_id if 'b3000' in scan_info['type'].lower() and \ len([o for o in self.acquisitions.get(index, list()) if 'b3000' in o['type'].lower()]) > 0: index += 1 if index in self.acquisitions: self.acquisitions[index].append(scan_info) else: self.acquisitions[index] = [scan_info] xnat.disconnect() self.time_writer('Disconnection of XNAT')
def get_voxel_size(self): """Method to get the voxel size from XNAT if define using default value if not. :return: voxel size [x, y, z] """ xnat = XnatUtils.get_interface(host=self.host, user=self.user, pwd=self.pwd) scan_obj = self.select_obj(xnat, self.xnat_scan, None) vsize = scan_obj.attrs.mget(['xnat:mrScanData/parameters/voxelRes/x', 'xnat:mrScanData/parameters/voxelRes/y', 'xnat:mrScanData/parameters/voxelRes/z']) xnat.disconnect() return vsize
def test_clean_scans_from_test_session(self): proj_id = 'proj1' subj_id = 'subj1' sess_ids = ['sess1', 'sess2'] # subj_id = 'subj2' # sess_ids = ['sess3', 'sess4'] intf = XnatUtils.get_interface(host=host) for sess_id in sess_ids: session = intf.select_experiment(proj_id, subj_id, sess_id) if not session.exists(): self.assertTrue(False, "no such session") for scn in session.scans(): scn.delete()
def test_setup_session(self): proj_id = 'proj1' subj_id = 'subj1' sess_ids = ['sess1', 'sess2'] intf = XnatUtils.get_interface(host=host) scan_descriptors = [('1', 't1'), ('2', 't1'), ('11', 'flair'), ('12', 'flair'), ('21', 't2')] for sess_id in sess_ids: session = intf.select_experiment(proj_id, subj_id, sess_id) if not session.exists(): self.assertTrue(False, "no such session") ComponentTestBuild._setup_scans(session, scan_descriptors)
def test_clean_scans_from_test_session(self): proj_id = 'proj1' subj_id = 'subj1' sess_ids = ['sess1', 'sess2'] # subj_id = 'subj2' # sess_ids = ['sess3', 'sess4'] intf = XnatUtils.get_interface(host=host) for sess_id in sess_ids: session = intf.select_experiment(proj_id, subj_id, sess_id) if not session.exists(): self.assertTrue(False, "no such session") for scn in session.scans(): scn.delete()
def test_clean_assessors_from_test_session(self): proj_id = 'proj1' subj_id = 'subj1' sess_ids = ['sess1', 'sess2'] # subj_id = 'subj2' # sess_ids = ['sess3', 'sess4'] intf = XnatUtils.get_interface(host=host) for sess_id in sess_ids: session = intf.select_experiment(proj_id, subj_id, sess_id) if not session.exists(): self.assertTrue(False, "no such session") for asr in session.assessors(): print((assessor_utils.full_label_from_assessor(asr))) asr.delete()
def test_clean_assessors_from_test_session(self): proj_id = 'proj1' subj_id = 'subj1' sess_ids = ['sess1', 'sess2'] # subj_id = 'subj2' # sess_ids = ['sess3', 'sess4'] intf = XnatUtils.get_interface(host=host) for sess_id in sess_ids: session = intf.select_experiment(proj_id, subj_id, sess_id) if not session.exists(): self.assertTrue(False, "no such session") for asr in session.assessors(): print assessor_utils.full_label_from_assessor(asr) asr.delete()
def test_create_assessor_with_no_input(self): with XnatUtils.get_interface(host=host) as intf: intf.connect() e = intf.select_experiment(proj_id, subj_id, sess_id) if not e.exists(): self.assertTrue( False, "Unexpected: {}//{}//{} does not exist".format( proj_id, subj_id, sess_id)) proc_a_params = { 'xsitype': asrxsitype, 'proctype': 'Proc_A_v1', 'files': [('SEG', ['seg.gz'])] } SessionTools.add_assessor(e, 'proc1-x-subj1-x-sess1-x-2-Proc_A_v1', proc_a_params, 'no_inputs')
def download_xnat_resource(self, src, dst): result = None try: xnat = XnatUtils.get_interface(self.host, self.user, self.pwd) try: res = xnat.select(src) res.get(dst, extract=True) result = dst except: print('ERROR:downloading from XNAT') except: print('ERROR:FAILED to get XNAT connection') finally: xnat.disconnect() return result
def test_xnat_has_inputs(self): with XnatUtils.get_interface(host=host) as intf: intf.connect() print yamls.proc_a SanityChecks.__prep_project(intf) yamldoc = YamlDoc().from_string(yamls.proc_a) ap = AutoProcessor(XnatUtils, yamldoc) csess = XnatUtils.CachedImageSession( intf, proj_id, subj_id, sess_id) results = [] for cassr in csess.assessors(): has, errors = ap.has_inputs(cassr) results.append((has, errors)) print results
def test_xnat_has_inputs(self): with XnatUtils.get_interface(host=host) as intf: intf.connect() print((yamls.proc_a)) SanityChecks.__prep_project(intf) yamldoc = YamlDoc().from_string(yamls.proc_a) ap = AutoProcessor(XnatUtils, yamldoc) csess = XnatUtils.CachedImageSession(intf, proj_id, subj_id, sess_id) results = [] for cassr in csess.assessors(): has, errors = ap.has_inputs(cassr) results.append((has, errors)) print(results)
def download_xnat_file(self, src, dst): result = None try: xnat = XnatUtils.get_interface(self.host, self.user, self.pwd) try: _res, _file = src.split('/files/') res = xnat.select(_res) result = res.file(_file).get(dst) except: print('ERROR:downloading from XNAT') except: print('ERROR:FAILED to get XNAT connection') finally: xnat.disconnect() return result
def upload_data(args): """ Main function to upload data. :param args: arguments from argparser """ biomarkers_info = XnatUtils.read_csv(args.csv_file) if args.host: host = args.host else: host = os.environ['XNAT_HOST'] with XnatUtils.get_interface(host=host, user=args.username) as xnat: print('INFO: connection to xnat <%s>:' % (host)) print('INFO: setting information to XNAT') for biomarker_dict in biomarkers_info: # Create create_biomarker(xnat, biomarker_dict)
def test_xnat_get_cached_image_session(self): with XnatUtils.get_interface(host=host) as intf: cisess = XnatUtils.CachedImageSession(intf, proj_id, subj_id, sess_id) print(cisess) print(cisess.info()) for ciscan in cisess.scans(): print(ciscan.info()) scanobj = ciscan.full_object() print(scanobj) for ciassr in cisess.assessors(): print(ciassr.info()) for cirsrc in ciassr.out_resources(): print(cirsrc.info()) asrinfo = ciassr.info() rsrcobj = intf.select_assessor_resource(asrinfo['project_id'], asrinfo['subject_id'], asrinfo['session_id'], asrinfo['assessor_label'], cirsrc.info()['label']) print(rsrcobj) sessobj = cisess.full_object() print(sessobj)
def test_xnat_get_cached_image_session(self): with XnatUtils.get_interface(host=host) as intf: cisess = XnatUtils.CachedImageSession(intf, proj_id, subj_id, sess_id) print(cisess) print((cisess.info())) for ciscan in cisess.scans(): print((ciscan.info())) scanobj = ciscan.full_object() print(scanobj) for ciassr in cisess.assessors(): print((ciassr.info())) for cirsrc in ciassr.out_resources(): print((cirsrc.info())) asrinfo = ciassr.info() rsrcobj = intf.select_assessor_resource( asrinfo['project_id'], asrinfo['subject_id'], asrinfo['session_id'], asrinfo['assessor_label'], cirsrc.info()['label']) print(rsrcobj) sessobj = cisess.full_object() print(sessobj)
def test_create_assessor_with_no_input(self): with XnatUtils.get_interface(host=host) as intf: intf.connect() e = intf.select_experiment(proj_id, subj_id, sess_id) if not e.exists(): self.assertTrue( False, "Unexpected: {}//{}//{} does not exist".format( proj_id, subj_id, sess_id )) proc_a_params = { 'xsitype': asrxsitype, 'proctype': 'Proc_A_v1', 'files': [ ('SEG', ['seg.gz']) ] } SessionTools.add_assessor(e, 'proc1-x-subj1-x-sess1-x-2-Proc_A_v1', proc_a_params, 'no_inputs')
def get_cmds(self, assessor, job_dir_path): """ This function generates the spider command for the cluster job """ project = assessor.parent().parent().parent().label() subject = assessor.parent().parent().label() session = assessor.parent().label() csess = XnatUtils.CachedImageSession(assessor._intf, project, subject, session) xnat = XnatUtils.get_interface() scan = csess.scans()[0].label() scan_data = get_scan_resource_uri(xnat, project, subject, session, scan) cmd = csai_CMD.format(spider_path=self.spider_path, job_dir_path=job_dir_path, assessor_label=assessor.label(), project=project, subject=subject, session=session, scan_data=scan_data, suffix_proc=self.suffix_proc, matlab_utils=self.matlab_utils, matlab_bin=self.matlab_path) return [cmd]
def download(self, obj_label, resource, folder): """ Return a python list of the files downloaded for the resource on the scan example: download(scan_id, "DICOM", "/Users/test") or download(assessor_label, "DATA", "/Users/test") :param obj_label: xnat object label (scan ID or assessor label) :param resource: folder name under the xnat object :param folder: download directory :return: python list of files downloaded """ # Open connection to XNAT xnat = XnatUtils.get_interface(host=self.host, user=self.user, pwd=self.pwd) resource_obj = self.select_obj(intf=xnat, obj_label=obj_label, resource=resource) list_files = XnatUtils.download_files_from_obj(directory=folder, resource_obj=resource_obj) # close connection xnat.disconnect() return list_files
:param list_scans: list of scans :param lastsess: last session processed """ new_list_scans = list() good = False for scan in list_scans: if scan['session_label'] == lastsess: good = True if good: new_list_scans.append(scan) return new_list_scans if __name__ == '__main__': args = parse_args() print('Zipping files in %s for Project:\t%s' % (args.resources.split(','), args.project)) print('Time: %s' % str(datetime.now())) if args.host: host = args.host else: host = os.environ['XNAT_HOST'] with XnatUtils.get_interface(host=host, user=args.username) as xnat: print('INFO: connection to xnat <%s>:' % (host)) print('INFO: extracting information from XNAT') zip_resources(xnat, args) print('================================================================\n')
import os import sys from dax import XnatUtils ASSESSOR_TYPE = 'GIF_Parcellation_v1' HOST = 'http://cmic-xnat.cs.ucl.ac.uk' PROJECT = 'Prisma_upgrade' OLD_RESOURCE = 'VOLUME' NEW_RESOURCE = 'STATS' ASSESSOR = None TMP_DIR = '/Users/byvernault/data/tempGIF/' try: xnat = XnatUtils.get_interface(host=HOST) if ASSESSOR: li_assessors = [XnatUtils.select_assessor(xnat, ASSESSOR)] sys.stdout.write('Renaming resources %s to %s for assessor %s' % (OLD_RESOURCE, NEW_RESOURCE, ASSESSOR)) else: li_assessors = XnatUtils.list_project_assessors(xnat, PROJECT) sys.stdout.write('Renaming resources %s to %s for assessor type %s\n' % (OLD_RESOURCE, NEW_RESOURCE, ASSESSOR_TYPE)) for assessor in li_assessors: sys.stdout.write(' - assessor: %s ...\n' % assessor['label']) assessors = XnatUtils.get_full_object(xnat, assessor) old_res = assessors.resource(OLD_RESOURCE) files = None
# TODO: daxlauncher.upload() import logging from dax import bin, XnatUtils # Create the launcher settingspath = '/Users/boydb1/.dax/settings/settings-subjgenproc.yaml' daxlauncher = bin.read_yaml_settings(settingspath, logging.getLogger()) # Get xnat connection xnat = XnatUtils.get_interface(host=daxlauncher.xnat_host) # Build print('building') project = 'DepMIND2' daxlauncher.build_project_subjgenproc(xnat, project) print('All Done!')
date = '{}-{}-{}'.format(date[:4], date[4:6], date[6:]) print("%s - %s" % (session_info['label'], date)) if session_info['date'] == date: print ' already set up.' else: session_obj = XnatUtils.get_full_object(xnat, session_info) session_obj.attrs.set('date', date) if __name__ == '__main__': ARGS = parse_args() try: if ARGS.host: HOST = ARGS.host else: HOST = os.environ['XNAT_HOST'] if ARGS.username: MSG = ("Please provide the password for user <%s> on xnat(%s):" % (ARGS.username, HOST)) PWD = getpass.getpass(prompt=MSG) else: PWD = None print 'INFO: connection to xnat <%s>:' % (HOST) XNAT = XnatUtils.get_interface(host=ARGS.host, user=ARGS.username, pwd=PWD) for session in XnatUtils.list_sessions(XNAT, ARGS.project): edit_session_date(XNAT, session) finally: XNAT.disconnect()
if found and not sess['subject_label'] in subject_done: subject_done.append(sess['subject_label']) print "%s == %s " % (sess['subject_label'], subject_dict['Subject ID']) for subject_dict in info_list: subject = subject_dict['Hospital ID'] session = subject +'_'+ get_date(subject_dict['Date of scan']) sess_obj = XnatUtils.select_obj(XNAT, 'prion', subject, session) if not sess_obj.exists(): print '%s not found on XNAT. (%s)' % (session, subject_dict['Subject ID']) if __name__ == '__main__': OPTIONS = parse_args() try: XNAT = XnatUtils.get_interface(host='http://cmic-xnat.cs.ucl.ac.uk') INFO_SUBJ = read_excel_2() #CSV_LIST = create_upload_data_csv() #CSV_LIST = create_upload2_data_csv() CSV_LIST = create_upload_demo_csv() #missing_data() print '\n\nCSV results for upload:' print 'project_id,subject_label,session_label,gender,age,yob,education,genetic,codon129,mrc,geneticstate' #print 'project_id,subject_label,DateOfFirstSymptoms' print '\n'.join(CSV_LIST) #info_list = read_excel() #Read the xlsx file: """ for idict in info_list:
import json import sys import pandas import pyxnat import argparse import time from dax import XnatUtils from dax import utilities # Specify and parse command line arguments parser = argparse.ArgumentParser() parser.add_argument('project', help='Project name') args = parser.parse_args() print('Project: {}'.format(args.project)) xnat = XnatUtils.get_interface() Assrs = XnatUtils.list_project_assessors(xnat, args.project) xnat.disconnect() timestamp = time.strftime("%Y%m%d%H%M%S") outfile1 = 'status_by_assessor_{}_{}.csv'.format(args.project, timestamp) outfile2 = 'status_by_session_{}_{}.csv'.format(args.project, timestamp) R = list() for assr in Assrs: #print(assr['assessor_label']) # Get desired fields thisR = {} for key in ('project_label', 'subject_label', 'session_label', 'proctype',
from dax import XnatUtils import os if __name__ == "__main__": # Variables: project = 'DIAN' types = {'': 'PhoenixZIPReport'} with XnatUtils.get_interface(host=os.environ['DPUK']) as xnat: scans = XnatUtils.list_project_scans(xnat, project) scans = XnatUtils.filter_list_dicts_regex(scans, 'type', types.keys()) for scan in sorted(scans, key=lambda k: k['session_label']): print "%s - %s - %s - %s" % (scan["session_label"], scan["ID"], scan["type"], scan["series_description"]) scan_obj = XnatUtils.get_full_object(xnat, scan) scan_obj.attrs.set('type', types[scan['type']]) print(' new type: {}'.format(types[scan['type']]))
def prerun(self, settings_filename=''): self.xnat = XnatUtils.get_interface()
def test_assessor_out_resources(self): with XnatUtils.get_interface(host=host) as intf: cisess = XnatUtils.CachedImageSession(intf, proj_id, subj_id, sess_id) for asr in cisess.assessors(): asrobj = intf.select_assessor(proj_id, subj_id, sess_id, asr.label()) print(asrobj.out_resources())
# Min and Max a_max = data.max() a_min = data.min() # Rescale the data between 0.0 - 1.0 rescale_array = (data-a_min)/(a_max/2-a_min) rescale_array[rescale_array > 1.0] = 1.0 return rescale_array if __name__ == '__main__': OPTIONS = parse_args() print('Making Preview from NIFTI format images for Project:\t%s' % OPTIONS.project) print 'Time: ', str(datetime.now()) # Connection to Xnat try: if OPTIONS.username: msg = """password for user <%s>:""" % OPTIONS.username pwd = getpass.getpass(prompt=msg) else: pwd = None XNAT = XnatUtils.get_interface(host=OPTIONS.host, user=OPTIONS.username, pwd=pwd) make_preview_nifti(XNAT, OPTIONS) finally: XNAT.disconnect() print '================================================================\n'
def pre_run(self): """Method to download data from XNAT. :param argument_parse: argument parser object return by parse_args() """ # Make directory input_folder = XnatUtils.makedir(os.path.join(self.jobdir, 'inputs'), subdir=False) # Target target_folder = XnatUtils.makedir(os.path.join(input_folder, ARGS.target_id), subdir=False) target_dcm = XnatUtils.makedir(os.path.join(target_folder, 'DICOM'), subdir=False) self.time_writer('Connection to XNAT') xnat = XnatUtils.get_interface(host=self.host, user=self.user, pwd=self.pwd) self.time_writer('Downloading target %s ...' % ARGS.target_id) target_scan = XnatUtils.select_obj(xnat, ARGS.proj_label, ARGS.subj_label, ARGS.sess_label, ARGS.target_id) tnii_obj = target_scan.resource('NIFTI') self.target['nii'] = XnatUtils.download_file_from_obj(target_folder, tnii_obj) tdcm_obj = target_scan.resource('DICOM') self.target['dcm'] = XnatUtils.download_files_from_obj(target_dcm, tdcm_obj) self.target['type'] = target_scan.attrs.get('type') self.target['ID'] = ARGS.target_id # Sources sources_list = XnatUtils.get_input_list(ARGS.sources_id, list()) self.time_writer('Downloading sources %s ...' % sources_list) for scan_id in sources_list: # Make directories spath = os.path.join(input_folder, scan_id) source_folder = XnatUtils.makedir(spath, subdir=False) dpath = os.path.join(source_folder, 'DICOM') source_dcm = XnatUtils.makedir(dpath, subdir=False) source_scan = XnatUtils.select_obj(xnat, ARGS.proj_label, ARGS.subj_label, ARGS.sess_label, scan_id) snii_obj = source_scan.resource('NIFTI') nii_list = XnatUtils.download_file_from_obj(source_folder, snii_obj) sdcm_obj = source_scan.resource('DICOM') dcm_list = XnatUtils.download_file_from_obj(source_dcm, sdcm_obj) self.sources[scan_id] = dict() self.sources[scan_id]['nii'] = nii_list self.sources[scan_id]['dcm'] = dcm_list self.sources[scan_id]['type'] = source_scan.attrs.get('type') self.sources[scan_id]['ID'] = scan_id xnat.disconnect() self.time_writer('Disconnection of XNAT')
# Set the Image pixel array if pixel_array2.dtype != np.uint16: pixel_array2 = pixel_array2.astype(np.uint16) print pixel_array2.max() print pixel_array2.min() ds.PixelData = pixel_array2.tostring() # Save the image ds.save_as(filename) if __name__ == '__main__': args = parse_args() # Scans on XNAT: try: xnat = XnatUtils.get_interface() li_scans = XnatUtils.list_project_scans(xnat, args.project) # Filter: if args.sessions: li_scans = XnatUtils.filter_list_dicts_regex( li_scans, 'session_label', args.sessions.split(',')) if args.subjects: li_scans = XnatUtils.filter_list_dicts_regex( li_scans, 'subject_label', args.subjects.split(',')) li_scans = sorted(li_scans, key=lambda k: k['session_label']) for scan_d in li_scans: if scan_d['ID'] == 'n004': print (' -> converting session: %s /scan: %s' % (scan_d['session_label'], scan_d['ID'])) if 'IMG' in scan_d['resources']: if 'NIFTI' not in scan_d['resources'] or args.force:
def get_connection(host): return XnatUtils.get_interface(host=host)
""" Remove the flagfile to unlock the process :param lock_file: flag file use to lock the process :return: None """ if os.path.exists(lock_file): os.remove(lock_file) def check_dir(dir_path): try: os.makedirs(dir_path) except OSError: if not os.path.isdir(dir_path): raise if __name__ == '__main__': d = Module_baxter_redcap_sync(directory='/Users/yuqian', mod_name=DEFAULT_MODULE_NAME, text_report=DEFAULT_TEXT_REPORT, resources='STATS', proctypes='naleg-roi_v1') xnat = XnatUtils.get_interface("http://xnat.vanderbilt.edu:8080/xnat", "masispider", "xnatisawesome!!") csess = XnatUtils.CachedImageSession(xnat, 'IKIZLER', '133511', '133511') d.prerun() d.needs_run(csess, xnat) d.run(csess.info(), csess.full_object())
from argparse import ArgumentParser, RawDescriptionHelpFormatter argp = ArgumentParser(prog='Set_types_from_dicom', description=__purpose__, formatter_class=RawDescriptionHelpFormatter) argp.add_argument('-s', '--subj', dest='suject_to_do', help='Sessions label on XNAT.') argp.add_argument('-d', '--date', dest='dateb', help='Date of birth.') argp.add_argument('-g', '--gender', dest='gender', help='Gender.') return argp.parse_args() if __name__ == '__main__': args = parse_args() xnat = XnatUtils.get_interface(host='https://qs-controls.cs.ucl.ac.uk') subj = xnat.select('/project/control_PRION/subject/%s' % args.suject_to_do) if subj.exists(): dic = {'yob': args.dateb, 'gender': args.gender} subj.attrs.mset(dic) """li_subjects = XnatUtils.list_subjects(xnat, 'prion') li_subjects = sorted(li_subjects, key=lambda k: k['label']) li_su = [] for subject in li_subjects: subj_obj = XnatUtils.get_full_object(xnat, subject) genetic = subj_obj.attrs.get('xnat:subjectData/fields/field\ [name=genetic]/field') # print genetic
p.add_argument('-c', '--cred', dest='password', help='Database username password', required=True) return p.parse_args() if __name__ == '__main__': ARGS = parse_args() # Try to connect and exit if we catch the most common OperationalError # will add more as they are encountered. try: conn = psycopg2.connect(dbname=ARGS.dbname, password=ARGS.password, user=ARGS.user) except OperationalError as operr: print "FATAL: Caught an OperationalError. Please check your dbname, " \ "host ip address, username, and password" sys.exit(1) cur = conn.cursor() XNAT = XnatUtils.get_interface() PROJECTS = XNAT.select('/projects').get() for PROJECT in PROJECTS: sessions = XnatUtils.list_experiments(XNAT, PROJECT) for session in sessions: if '-x-' in session['session_label']: sys.stdout.write('Found corrupt assessors %s for project %s\n' % (session['session_label'], PROJECT)) cur.execute("""DELETE FROM xnat_experimentdata where label=(%s)""",(session['session_label'],)) conn.commit() XNAT.disconnect() conn.close()
from dax import XnatUtils __author__ = 'byvernault' __email__ = '*****@*****.**' __purpose__ = "Renaming attrs for xnat object" __version__ = '1.0.0' __modifications__ = '11 September 2015 - Original write' if __name__ == '__main__': host = os.environ['XNAT_HOST'] user = os.environ['XNAT_USER'] pwd = os.environ['XNAT_PASS'] subject_type = False session_type = True xnat = XnatUtils.get_interface(host=host, user=user, pwd=pwd) if subject_type: for subject in XnatUtils.list_subjects(xnat, projectid='INNOVATE'): subj_obj = XnatUtils.get_full_object(xnat, subject) if subj_obj.exists(): labels = subject['label'].split('-') if subject['label'].startswith('INN_'): sub = subject['label'].replace('_', '-') labels = sub.split('-') if len(labels) < 2: print 'stop - wrong label' continue if labels[1].isdigit(): print 'stop - already good label' continue
def __get_connection(): return XnatUtils.get_interface(host=host)
"""Renaming a file in scans.""" import os import sys from dax import XnatUtils SCAN = 'T2 Axial' HOST = 'https://prostate-xnat.cs.ucl.ac.uk' PROJECT = 'PICTURE' RESOURCE = 'OsiriX' OLD_FILE = 'mantonelli_Unnamed.rois_series' NEW_FILE = 'Mrishta_Unnamed.rois_series' TMP_DIR = '/Users/byvernault/data/temp_roi_upgrade/' with XnatUtils.get_interface(host=HOST) as xnat: li_scans = XnatUtils.list_project_scans(xnat, PROJECT) li_scans = XnatUtils.filter_list_dicts_regex(li_scans, 'type', SCAN) li_scans = filter(lambda x: RESOURCE in x['resources'], li_scans) sys.stdout.write('Renaming file %s to %s for scan type %s\n' % (OLD_FILE, NEW_FILE, SCAN)) for scan in li_scans: sys.stdout.write(' - scan: %s / %s ...\n' % (scan['session_label'], scan['ID'])) scan_obj = XnatUtils.get_full_object(xnat, scan) res_obj = scan_obj.resource(RESOURCE) files = None if res_obj.exists() and OLD_FILE in res_obj.files().get()[:]: