本文整理汇总了Python中dax.XnatUtils.download_files_from_obj方法的典型用法代码示例。如果您正苦于以下问题:Python XnatUtils.download_files_from_obj方法的具体用法?Python XnatUtils.download_files_from_obj怎么用?Python XnatUtils.download_files_from_obj使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类dax.XnatUtils
的用法示例。
在下文中一共展示了XnatUtils.download_files_from_obj方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: generate_bmask_whole
# 需要导入模块: from dax import XnatUtils [as 别名]
# 或者: from dax.XnatUtils import download_files_from_obj [as 别名]
def generate_bmask_whole(assessor_obj, assessor, tmpdir):
""" Generating the bmask whole from the assessor outputs."""
jobsdir = os.path.join(tmpdir, assessor['label'])
if not os.path.exists(jobsdir):
os.makedirs(jobsdir)
else:
shutil.rmtree(jobsdir)
os.makedirs(jobsdir)
files = XnatUtils.download_files_from_obj(jobsdir,
assessor_obj.resource('LABELS'))
if len(files) != 1:
raise Exception('Too many niftis downloaded.')
else:
input_nii = files[0]
tiv_files = XnatUtils.download_files_from_obj(jobsdir,
assessor_obj.resource('TIV'))
basepath = os.path.dirname(input_nii)
basename = os.path.basename(input_nii[:-7])
final_seg_mats_sub_cmd = os.path.join(
basepath, "%s_ArtConstruction_0.nii.gz " % basename)
for index, value in enumerate(BM_ARRAY):
output_nii = os.path.join(
basepath, "%s_ArtConstruction_%d.nii.gz " % (basename, index))
val_min = float(value - 0.5)
val_max = float(value + 0.5)
cmd = '%s %s -thr %f -uthr %f -bin %s' % (ARGS.seg_maths_exe,
input_nii, val_min, val_max,
output_nii)
print cmd
os.system(cmd)
final_seg_mats_sub_cmd += "-add %s " % output_nii
# Seg maths add
to_remove_nii = "%s_ToRemove.nii.gz" % basepath
cmd = '%s %s -bin %s' % (ARGS.seg_maths_exe, final_seg_mats_sub_cmd,
to_remove_nii)
print cmd
os.system(cmd)
# Seg maths remove
final_brain = "%s_FinalBrain.nii.gz" % basepath
cmd = '%s %s -sub %s -bin %s' % (ARGS.seg_maths_exe,
' '.join(tiv_files),
to_remove_nii,
final_brain)
print cmd
os.system(cmd)
return final_brain
示例2: download
# 需要导入模块: from dax import XnatUtils [as 别名]
# 或者: from dax.XnatUtils import download_files_from_obj [as 别名]
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
示例3: sorted
# 需要导入模块: from dax import XnatUtils [as 别名]
# 或者: from dax.XnatUtils import download_files_from_obj [as 别名]
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:
fdf_files = filter(lambda x: x.endswith('.fdf'), files)
nii_file = os.path.join(
tmp_dir,
示例4:
# 需要导入模块: from dax import XnatUtils [as 别名]
# 或者: from dax.XnatUtils import download_files_from_obj [as 别名]
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:
print 'error: %s' % e
finally:
xnat.disconnect()
示例5: pre_run
# 需要导入模块: from dax import XnatUtils [as 别名]
# 或者: from dax.XnatUtils import download_files_from_obj [as 别名]
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')
示例6: convert_DICOM
# 需要导入模块: from dax import XnatUtils [as 别名]
# 或者: from dax.XnatUtils import download_files_from_obj [as 别名]
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: \
示例7: len
# 需要导入模块: from dax import XnatUtils [as 别名]
# 或者: from dax.XnatUtils import download_files_from_obj [as 别名]
# 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',
subject=scan['subject_label'],
session=scan['session_label'],
type='1946_PET_NAC_DYNAMIC_0_60MIN')
print 'Pyxnat failed. Trying curl: %s' % curl
try:
os.system(curl)
dcm_zip = os.path.join(data_dir, 'DICOM.zip')
if os.path.exists(dcm_zip):
os.system('unzip -o -d %s %s' %
示例8: zip_resource
# 需要导入模块: from dax import XnatUtils [as 别名]
# 或者: from dax.XnatUtils import download_files_from_obj [as 别名]
def zip_resource(res_obj, directory, resource, no_delete=False, no_big=False):
"""
Zip the files in the resource.
:param res_obj: resource Eobject from pyxnat
:param directory: directory to save temp files
:param resource: resource label
:param no_delete: do not delete the files zipped
:param no_big: do not zip big resources
"""
_warn = ' - warning: %s'
fzip = '%s.zip' % resource
if len(res_obj.files().get()) > 1:
print(" --> downloading %s ..." % resource)
fpaths = XnatUtils.download_files_from_obj(directory, res_obj)
if not fpaths:
msg = '%s -- no files.' % resource
print(_warn % msg)
else:
# If the resource.zip file exists, exit
if res_obj.file(fzip).exists():
msg = '%s -- zip file already on XNAT but zipped files not \
deleted.' % resource
print(_warn % msg)
else:
# Zip the resource if more than one
print(' --> zipping files.')
resource_dir = os.path.dirname(fpaths[0])
# Get init directory
init_dir = os.getcwd()
# Zip all the files in the directory
os.chdir(resource_dir)
os.system('zip -r %s * > /dev/null' % fzip)
# return to the initial directory:
os.chdir(init_dir)
# upload
_fzip = os.path.join(resource_dir, fzip)
if os.path.exists(_fzip):
# Check the size:
size_file = int(os.stat(_fzip).st_size) / (1024 * 1024)
if size_file >= LIMIT_SIZE:
msg = '%s too big.' % resource
print(_warn % msg)
if no_big and size_file >= LIMIT_SIZE:
msg = '%s too big. Skipping.' % resource
print(_warn % msg)
return
print(' --> uploading zip file')
res_obj.put_zip(_fzip, overwrite=True, extract=False)
if no_delete:
print(' --> not deleting original files(two copies).')
else:
print(' --> deleting original files and keeping zipfile.')
delete_resources(res_obj, fzip)
# clean tmp folder
XnatUtils.clean_directory(directory)
示例9:
# 需要导入模块: from dax import XnatUtils [as 别名]
# 或者: from dax.XnatUtils import download_files_from_obj [as 别名]
else:
sys.stdout.write(' + creating %s\n' % new_label)
assessor.create(assessors='proc:genProcData')
assessor_info = {
'proc:genProcData/procstatus': assessor['procstatus'],
'proc:genProcData/validation/status': assessor['qcstatus'],
'proc:genProcData/proctype': assessor['proctype'],
'proc:genProcData/jobstartdate': assessor['jobstartdate'],
'proc:genProcData/jobid': assessor['jobid'],
'proc:genProcData/date': assessor_obj.attrs.get('date')}
assessor.attrs.mset(assessor_info)
sys.stdout.write(' + copying %s\n' % new_label)
for resource_name in assessor['resources']:
asse_dir = os.path.join(TMP_DIR, new_label, resource_name)
if not os.path.isdir(asse_dir):
os.makedirs(asse_dir)
if resource_name != 'SNAPSHOTS':
files = XnatUtils.download_files_from_obj(
asse_dir, assessor_obj.resource(resource_name))
if files:
newa_res = new_assessor_obj.resource(resource_name)
XnatUtils.upload_files_to_obj(files, newa_res)
for f in files:
os.remove(f)
print "DONE -- See y'all"
except Exception as e:
print 'error: %s' % e
finally:
xnat.disconnect()
示例10: run
# 需要导入模块: from dax import XnatUtils [as 别名]
# 或者: from dax.XnatUtils import download_files_from_obj [as 别名]
def run(self, scan_info, scan_obj):
"""run function to convert dicom to parrec to nifti and upload data."""
# clean tmp folder
XnatUtils.clean_directory(self.directory)
if not len(scan_obj.resource('DICOM').files().get()) > 0:
LOGGER.debug('no DICOM files')
elif scan_info['type'] in AVOID_SCANTYPES:
LOGGER.info('avoid this scan type: {}'.format(scan_info['type']))
else:
LOGGER.debug('downloading all DICOMs...')
self.dicom_paths = XnatUtils.download_files_from_obj(
self.directory, scan_obj.resource('DICOM'))
if not self.dicom_paths:
msg = """dcm2nii -- %s -- No proper DICOM found in \
resource DICOM on XNAT"""
LOGGER.error(msg % scan_info['scan_id'])
msg = 'No proper DICOM found in resource DICOM on XNAT'
self.log_warning_error(msg, scan_info, error=True)
else:
# convert dcm to nii
dcm_dir = os.path.dirname(self.dicom_paths[0])
# ZIP the DICOM if more than one
if len(self.dicom_paths) > 1 and self.zip_dicoms:
self.zipping_dicoms(scan_obj, dcm_dir)
# if only one DICOM and it's a zip, unzip
if len(self.dicom_paths) == 1 and \
self.dicom_paths[0].endswith('.zip'):
dcm_dir = os.path.dirname(self.dicom_paths[0])
os.system('unzip -d %s -j %s > /dev/null'
% (dcm_dir, self.dicom_paths[0]))
os.remove(self.dicom_paths[0])
self.dicom_paths = self.get_dicom_list(dcm_dir)
if not self.dcm2nii(self.dicom_paths[0]):
# Convert dcm via dcmdjpeg
dicom_paths_djpeg = self.dcmdjpeg()
# try again dcm2nii
self.dcm2nii(dicom_paths_djpeg[0])
dcm_dir = os.path.dirname(dicom_paths_djpeg[0])
# Check if Nifti created:
nifti_list = [
f for f in os.listdir(dcm_dir)
if f.endswith('.nii.gz') or f.endswith('.nii')]
if not nifti_list:
msg = "dcm2nii -- %s -- DCM --> NII ( preprocess \
dicom with dcmdjpeg ) conversion failure"
LOGGER.warn(msg % scan_info['scan_id'])
msg = 'Fail to convert DICOM to NIFTI '
self.log_warning_error(msg, scan_info)
else:
# UPLOADING THE RESULTS
self.upload_converted_images(
dcm_dir, scan_obj, scan_info)
# clean tmp folder
LOGGER.debug('clean temp directory...')
XnatUtils.clean_directory(self.directory)
示例11: run
# 需要导入模块: from dax import XnatUtils [as 别名]
# 或者: from dax.XnatUtils import download_files_from_obj [as 别名]
def run(self, scan_info, scan_obj):
"""run function to convert dicom to parrec to nifti and upload data.
:param scan_info: dictionary with attrs for scan
:param scan_obj: pyxnat Eobject for scan
:return: None
"""
if not len(scan_obj.resource('DICOM').files().get()) > 0:
LOGGER.debug('no DICOM files')
else:
LOGGER.debug('downloading all DICOMs...')
self.dicom_paths = XnatUtils.download_files_from_obj(
self.directory,
scan_obj.resource('DICOM'))
if not self.dicom_paths:
msg = """dcm2nii -- %s -- No proper DICOM found in \
resource DICOM on XNAT"""
LOGGER.error(msg % scan_info['scan_id'])
msg = 'No proper DICOM found in resource DICOM on XNAT'
self.log_warning_error(msg, scan_info, error=True)
else:
# convert dcm to nii
dcm_dir = os.path.dirname(self.dicom_paths[0])
# ZIP the DICOM if more than one
if len(self.dicom_paths) > 1 and self.zip_dicoms:
self.zipping_dicoms(scan_obj, dcm_dir)
if len(self.dicom_paths) == 1 and \
self.dicom_paths[0].endswith('.zip'):
dcm_dir = os.path.dirname(self.dicom_paths[0])
os.system('unzip -d %s -j %s > /dev/null'
% (dcm_dir, self.dicom_paths[0]))
os.remove(self.dicom_paths[0])
self.dicom_paths = self.get_dicom_list(dcm_dir)
if not self.dicom_paths:
msg = """dcm2nii -- %s -- No proper DICOM found in \
resource DICOM on XNAT"""
LOGGER.error(msg % scan_info['scan_id'])
msg = 'No proper DICOM found in resource DICOM on XNAT'
self.log_warning_error(msg, scan_info, error=True)
else:
if scan_info['type'] in VERDICT_TYPE:
dcm_dir = self.verdict_dcm2nii()
else:
dcm_dir = self.default_dcm2nii()
# Check if Nifti created:
nifti_list = [f for f in os.listdir(dcm_dir)
if f.endswith('.nii.gz') or f.endswith('.nii')]
if not nifti_list:
msg = """dcm2nii -- %s -- DCM --> NII ( preprocess dicom \
with dcmdjpeg) conversion failure"""
LOGGER.warn(msg % scan_info['scan_id'])
self.log_warning_error('Fail to convert DICOM to NIFTI ',
scan_info)
else:
# UPLOADING THE RESULTS
self.upload_converted_images(dcm_dir, scan_obj, scan_info)
# clean tmp folder
self.clean_directory()