def edit_session_date(xnat, session_info): date = session_info['label'].split('_')[-1] 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)
def sessions_delete(): """Delete sessions on XNAT.""" for session in LI_SESSIONS: print ' - deleting session: %s' % session['label'] session_obj = XnatUtils.get_full_object(XNAT, session) if session_obj.exists(): try: session_obj.delete() except: print "exception but let's continue"
def set_session_age(session_info): """Set age for session.""" if not session_info['age'] or ARGS.force: session_obj = XnatUtils.get_full_object(XNAT, session_info) xsitype_sess = session_obj.attrs.get('xsiType') yob = session_obj.parent().attrs.get('yob') if not yob: print 'Skipping the session because yob not set.' else: yob = "%s-01-01" % yob years = years_between(yob, session_info['date']) age = '%.2f' % years session_obj.attrs.set('%s/%s' % (xsitype_sess, 'age'), age) print 'Age set to %s for %s.' % (age, session['label']) else: print 'Already set to %s for %s' % (session_info['age'], session['label'])
def zip_resources(xnat, args): """ Loop through the project scans to zip files. :param xnat: interface from dax related to pyxnat :param args: arguments from parse_args """ # set a directory where the files are download directory = os.path.abspath(XnatUtils.makedir(args.directory)) list_scans = XnatUtils.list_project_scans(xnat, args.project) print("INFO: Filtering list of scans to keep scans with resources.") if not args.force: for resource in args.resources.split(','): list_scans = filter(lambda x: resource in x['resources'], list_scans) # if sessions, filter: if args.sessions: list_scans = filter( lambda x: x['session_label'] in args.sessions.split(','), list_scans) # filtering last sessions: list_scans = sorted(list_scans, key=lambda k: k['session_label']) if args.lastsess: list_scans = remove_sessions_processed(list_scans, args.lastsess) number_scans = len(list_scans) print("INFO: Converting the %s scans found." % (number_scans)) for index, scan in enumerate(list_scans): message = ' * {ind}/{tot} -- Session: {sess} -- Scan: {scan}' print(message.format(ind=index + 1, tot=number_scans, sess=scan['session_label'], scan=scan['ID'])) scan_obj = XnatUtils.get_full_object(xnat, scan) if scan_obj.exists(): for resource in args.resources.split(','): zip_resource(scan_obj.resource(resource), directory, resource, args.no_delete, args.no_big)
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: tmp_dir = os.path.join(args.directory, scan_d['session_label'], scan_d['ID']) if not os.path.isdir(tmp_dir): os.makedirs(tmp_dir) scan_obj = XnatUtils.get_full_object(xnat, scan_d) # Download: files = XnatUtils.download_files_from_obj( tmp_dir, scan_obj.resource('IMG')) if len(files) > 1 and args.zip_fdf: zipping_files(scan_obj, os.path.dirname(files[0])) if len(files) == 1 and files[0].endswith('.zip'): fdf_dir = os.path.dirname(files[0]) os.system('unzip -d %s -j %s > /dev/null' % (fdf_dir, files[0])) os.remove(files[0]) files = get_files_list(fdf_dir, '.fdf', add_procpar=True) # Convert:
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 if old_res.exists(): files = XnatUtils.download_files_from_obj(TMP_DIR, old_res) if files: new_res = assessors.resource(NEW_RESOURCE) XnatUtils.upload_files_to_obj(files, new_res) if new_res.exists(): old_res.delete() sys.stdout.write(' renamed.\n') for f in files: os.remove(f) print "DONE -- See y'all" except Exception as e:
def make_preview_nifti(xnat, options): """generate previewe for a project on XNAT.""" # make directory if not os.path.exists(options.directory): os.makedirs(options.directory) # list of scans for the project list_scans = XnatUtils.list_project_scans(xnat, options.project) # filter the list to keep scans with DICOM and no NIFTI if not options.force: print "Filtering scans to keep scans with NIFTI but no SNAPSHOTS." list_scans = filter(lambda x: 'NIFTI' in x['resources'] and 'SNAPSHOTS' not in x['resources'], list_scans) # if sessions, filter: if options.sessions: list_scans = filter( lambda x: x['session_label'] in options.sessions.split(','), list_scans) number_scans = len(list_scans) for index, scan in enumerate(sorted(list_scans, key=lambda k: k['session_label'])): message = ' * {index}/{total} -- Session: {session} -- Scan: {scan}' print message.format(index=index+1, total=number_scans, session=scan['session_label'], scan=scan['ID']) scan_obj = XnatUtils.get_full_object(xnat, scan) res_obj = scan_obj.resource(options.resource) if res_obj.exists() and len(res_obj.files().get()) > 0: if options.force and scan_obj.resource("SNAPSHOTS").exists(): scan_obj.resource("SNAPSHOTS").delete() print " --> downloading "+options.resource+" ..." filename = XnatUtils.download_biggest_file_from_obj( options.directory, res_obj) if not filename: print ' - warning: '+options.resource+' -- no files.' else: if filename.endswith('.nii.gz'): os.system('gzip -d '+filename) filename = filename[:-3] fname = os.path.basename(filename).split('.')[0] + "_sm.gif" smgif = os.path.join(options.directory, fname) fname = os.path.basename(filename).split('.')[0] + "_lg.gif" lggif = os.path.join(options.directory, fname) status = generate_preview(filename, smgif, lggif) if not status: print ' --> GIF FAILED' else: if os.path.isfile(smgif): print ' --> GIF Made / upload to XNAT...' scan_obj.resource('SNAPSHOTS').file('snap_t.gif')\ .put(smgif, 'GIF', 'THUMBNAIL') scan_obj.resource('SNAPSHOTS').file('snap.gif')\ .put(lggif, 'GIF', 'ORIGINAL') else: print ' --> GIF FAILED' if os.path.isfile(smgif): os.remove(smgif) os.remove(lggif) if os.path.isfile(filename): os.remove(filename) else: print(" - ERROR : issue with resource %s: no file or resource. " % (options.resource))
def convert_DICOM(): """Loop through the project scans to convert DICOM to NIFTI.""" list_scans = XnatUtils.list_project_scans(XNAT, OPTIONS.project) # filter the list to keep scans with DICOM and no NIFTI if not OPTIONS.force: print "Filtering list of scans to keep scans with DICOM but no NIFTI." list_scans = filter( lambda x: 'DICOM' in x['resources'] and 'NIFTI' not in x['resources'], list_scans) else: print "Filtering list of scans to keep scans with DICOM." list_scans = filter(lambda x: 'DICOM' in x['resources'], list_scans) # if sessions, filter: if OPTIONS.sessions: list_scans = filter( lambda x: x['session_label'] in OPTIONS.sessions.split(','), list_scans) number_scans = len(list_scans) print "Converting the %s scans found." % (number_scans) for index, scan in enumerate(sorted(list_scans, key=lambda k: k['session_label'])): message = ' * {index}/{total} -- Session: {session} -- Scan: {scan}' print message.format(index=index+1, total=number_scans, session=scan['session_label'], scan=scan['ID']) need_to_zip = False scan_obj = XnatUtils.get_full_object(XNAT, scan) if scan_obj.exists() and \ len(scan_obj.resource('DICOM').files().get()) > 0: print " --> downloading DICOM ..." fpaths = XnatUtils.download_files_from_obj( OPTIONS.directory, scan_obj.resource("DICOM")) if not fpaths: print ' - warning: DICOM -- no files.' else: if OPTIONS.force and scan_obj.resource('NIFTI').exists(): scan_obj.resource('NIFTI').delete() dcm_dir = os.path.join(OPTIONS.directory, 'DICOM') if len(fpaths) > 1: need_to_zip = True if len(fpaths) == 1 and fpaths[0].endswith('.zip'): if not os.path.exists(dcm_dir): os.makedirs(dcm_dir) os.system('unzip -d %s -j %s > /dev/null' % (dcm_dir, fpaths[0])) os.remove(fpaths[0]) dicom_files = get_dicom_list(dcm_dir) if dicom_files: # Check for duplicate dicoms: if OPTIONS.check_dcm: check_duplicate_slices_dicom(dicom_files) # convert dcm to nii conversion_status = dcm2nii(dicom_files[0]) if not conversion_status: # Convert dcm via dcmdjpeg dcmdjpeg(dcm_dir) # try again dcm2nii dcm_fpath = os.path.join(dcm_dir, 'final_1.dcm') conversion_status = dcm2nii(dcm_fpath) # Check if Nifti created: nii_li = [f for f in os.listdir(dcm_dir) if f.endswith('.nii.gz') or f.endswith('.nii')] if not nii_li: print " - warning: dcm to nii failed with \ conversion dcmjpeg. no upload." else: # UPLOADING THE RESULTS upload_converted_images(dicom_files, dcm_dir, scan_obj, need_to_zip) # clean tmp folder XnatUtils.clean_directory(OPTIONS.directory) else: print " - ERROR : no proper DICOM files \ found from the resource on XNAT. " else: print " - ERROR : issue with resource DICOM: \
li_scans = [scan for scan in li_scans if scan['type'] == '1946_PET_NAC_DYNAMIC_0_60MIN'] row = list() for scan in li_scans: if scan['session_label'] not in li_sessions: msg = 'Download scan number %s for session %s' print msg % (scan['ID'], scan['session_label']) # data_dir = os.path.join(args.directory, scan['subject_label'], # scan['session_label'], 'scans', # '1946_PET_NAC_DYNAMIC_0_60MIN') data_dir = os.path.join(args.directory, scan['session_label']) if os.path.exists(data_dir): print ' --> Folder present. Already downloaded?' else: scan_obj = XnatUtils.get_full_object(XNAT, scan) res_obj = scan_obj.resource('DICOM') if not res_obj.exists(): print ' Error: No DICOM Resources found for scan.' else: if len(res_obj.files().get()) > 0: print ' 1) start download...' os.makedirs(data_dir) dl_files = XnatUtils.download_files_from_obj(data_dir, res_obj) print ' 2) Files downloaded' else: os.makedirs(data_dir) curl = CCMD.format(path=data_dir, host=HOST, project='1946',
__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 if '_' in labels[2]: fin = '_'+'_'.join(labels[2].split('_')[1:]) labels[2] = labels[2].split('_')[0] else:
'Olin': 5, 'Pitt': 0, 'SBL': 0, 'SDSU': 0, 'Stanford': 0, 'Trinity': 0, 'UCLA': 0, 'UM': 0, 'USM': 0, 'Yale': 5} XNAT = XnatUtils.get_interface() SCANS = XnatUtils.list_project_scans(XNAT, 'ABIDE') for SCAN in SCANS: if SCAN_TYPE in SCAN['type']: OBJECT = XnatUtils.get_full_object(XNAT, SCAN) dropvols_num = get_num_dropvols(SCAN['session_label']) sys.stdout.write('Setting dropvols to {dropvols} and slorder ' 'to {slorder} for scan id {scanid} for ' 'session {session}\n'.format(dropvols=dropvols_num, slorder='[]', scanid=SCAN['ID'], session=SCAN['session_label'])) OBJECT.attrs.set('xnat:mrScanData/parameters/addParam[name=dropvols]/addField', str(dropvols_num)) OBJECT.attrs.set('xnat:mrScanData/parameters/addParam[name=slorder]/addField', '[]') XNAT.disconnect()
print 'INFO: connection to xnat <%s>:' % (HOST) XNAT = XnatUtils.get_interface(host=ARGS.host, user=ARGS.username, pwd=PWD) li_assessors = XnatUtils.list_project_assessors(XNAT, ARGS.project) li_assessors = XnatUtils.filter_list_dicts_regex( li_assessors, 'proctype', ['GIF_Parcellation_v2']) li_assessors = XnatUtils.filter_list_dicts_regex( li_assessors, 'procstatus', ['COMPLETE']) li_assessors = filter(lambda x: 'WHOLEBRAIN' not in x['resources'], li_assessors) li_assessors = sorted(li_assessors, key=lambda k: k['session_label']) start = False for assessor in li_assessors: if ARGS.session: if assessor['session_label'] == ARGS.session: start = True else: start = True if start: print '\n<-- Assessor: %s -->' % assessor['label'] assessor_obj = XnatUtils.get_full_object(XNAT, assessor) brain = generate_bmask_whole(assessor_obj, assessor, ARGS.tmpdir) XnatUtils.upload_file_to_obj( brain, assessor_obj.resource('WHOLEBRAIN'), removeall=True) finally: XNAT.disconnect()
XNAT = XnatUtils.get_interface() ASSESSORS = XnatUtils.list_project_assessors(XNAT, 'WOODWARD_TCP') if len(sys.argv) != 4: print len(sys.argv) sys.stderr.write('ERROR: args must be <ASSESSOR_LABEL> <STATUS> <COMMENTS>') sys.exit() MY_ASSESSOR = sys.argv[1] STATUS = sys.argv[2] COMMENTS = sys.argv[3] if STATUS not in XNAT_STATUS_LIST: sys.stderr.write('ERROR: Status "%s" not in known list: %s\n' %(STATUS, ','.join(XNAT_STATUS_LIST))) for ASSESSOR in ASSESSORS: if ASSESSOR['label'] == MY_ASSESSOR: print "Setting QC of assessor %s to %s" % (MY_ASSESSOR, STATUS) OBJECT = XnatUtils.get_full_object(XNAT, ASSESSOR) if not MY_ASSESSOR.endswith('-x-FS'): OBJECT.attrs.set('proc:genProcData/validation/status', STATUS) OBJECT.attrs.set('proc:genProcData/validation/notes', COMMENTS) else: OBJECT.attrs.set('fs:fsData/validation/status', STATUS) OBJECT.attrs.set('fs:fsData/validation/notes', COMMENTS) XNAT.disconnect()