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 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: tmp_dir = os.path.join(args.directory,
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: \
help="Directory where the json files are stored.", required=True) argp.add_argument('-c', dest='csv', help="Csv file containing the subject that where already\ downloaded.", required=True) return argp.parse_args() if __name__ == '__main__': args = parse_args() row_excel = read_csv(args.csv, header=['subject', 'session']) li_sessions = [row['session'].strip() for row in row_excel] XNAT = XnatUtils.get_interface(host=HOST) li_scans = XnatUtils.list_project_scans(XNAT, '1946') 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:
'MaxMun': 0, 'NYU': 5, 'OHSU': 0, '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()
if not os.path.isfile(file_not_zipped): sys.stderr.write('ERROR: could not find file %s to gzip' % file_not_zipped) else: file_name_out = '%s.gz' % file_not_zipped content = open(file_not_zipped, 'rb') content = content.read() fout = gzip.open(file_name_out, 'wb') fout.write(content) fout.close() os.remove(file_not_zipped) return file_name_out if __name__ == '__main__': XNAT = XnatUtils.get_interface() SCANS = XnatUtils.list_project_scans(XNAT, 'SMITH_DOD') FID = open('/tmp/SMITH_DOD.csv', 'w') NEEDS_UPLOAD = False TMP_DIR_LIST = list() for SCAN in SCANS: NEED_PAR = False NEED_REC = True sys.stdout.write('Checking scan %s in session %s\n' % (SCAN['ID'], SCAN['session_label'])) if 'DICOM' not in SCAN['resources'] and 'secondary' in SCAN['resources'] and SCAN['ID'] not in KNOWN_IGNORE_LIST \ and 'superpacs' not in SCAN['type'].lower(): sys.stdout.write(' -DICOM not found. Checking to see if PAR and REC do\n') if 'PAR' not in SCAN['resources']: sys.stdout.write(' -PAR not found. Marked for upload\n') NEED_PAR = True if 'REC' not in SCAN['resources']:
__author__ = "damons" __purpose__ = "set the scan quality of a scan on xnat" from dax import XnatUtils import sys if __name__ == "__main__": XNAT = XnatUtils.get_interface() SCANS = XnatUtils.list_project_scans(XNAT, "WOODWARD_TCP") if len(sys.argv) != 4: print len(sys.argv) sys.stderr.write("ERROR: args must be <SESSION> <SCAN_ID> <STATUS>") sys.exit(1) MY_SESSION = sys.argv[1] MY_SCAN = sys.argv[2] STATUS = sys.argv[3] for SCAN in SCANS: if SCAN["session_label"] == MY_SESSION and SCAN["ID"] == MY_SCAN: print "Setting QC of scan %s in session %s to %s" % (MY_SCAN, MY_SESSION, STATUS) OBJECT = XnatUtils.get_full_object(XNAT, SCAN) OBJECT.attrs.set("quality", STATUS) XNAT.disconnect()
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']]))
__author__ = 'damons' from dax import XnatUtils if __name__ == '__main__': XNAT = XnatUtils.get_interface() SCANS = XnatUtils.list_project_scans(XNAT,'PHOTON') print "There are %s scans in the PHOTON project" % str(len(SCANS)) count = 0 for SCAN in SCANS: if 'SNAPSHOTS' in SCAN['resources']: count +=1 print "There are %s scans in the PHOTON project with SNAPSHOTS" % str(count) print "There are %s scans in the PHOTON project WITHOUT SNAPSHOTS" % str((len(SCANS) - count)) XNAT.disconnect()
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()[:]: # fpath = XnatUtils.download_file_from_obj(TMP_DIR, res_obj, # fname=OLD_FILE)