本文整理汇总了Python中nipype.utils.filemanip.copyfile函数的典型用法代码示例。如果您正苦于以下问题:Python copyfile函数的具体用法?Python copyfile怎么用?Python copyfile使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了copyfile函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_copyfallback
def test_copyfallback():
if os.name is not 'posix':
return
orig_img, orig_hdr = _temp_analyze_files()
pth, imgname = os.path.split(orig_img)
pth, hdrname = os.path.split(orig_hdr)
try:
fatfs = TempFATFS()
except IOError:
warnings.warn('Fuse mount failed. copyfile fallback tests skipped.')
else:
with fatfs as fatdir:
tgt_img = os.path.join(fatdir, imgname)
tgt_hdr = os.path.join(fatdir, hdrname)
for copy in (True, False):
for use_hardlink in (True, False):
copyfile(orig_img, tgt_img, copy=copy,
use_hardlink=use_hardlink)
yield assert_true, os.path.exists(tgt_img)
yield assert_true, os.path.exists(tgt_hdr)
yield assert_false, os.path.islink(tgt_img)
yield assert_false, os.path.islink(tgt_hdr)
yield assert_false, os.path.samefile(orig_img, tgt_img)
yield assert_false, os.path.samefile(orig_hdr, tgt_hdr)
os.unlink(tgt_img)
os.unlink(tgt_hdr)
finally:
os.unlink(orig_img)
os.unlink(orig_hdr)
示例2: sink_mask_file
def sink_mask_file(in_file, orig_file, out_dir):
import os
from nipype.utils.filemanip import fname_presuffix, copyfile
os.makedirs(out_dir, exist_ok=True)
out_file = fname_presuffix(orig_file, suffix='_mask', newpath=out_dir)
copyfile(in_file, out_file, copy=True, use_hardlink=True)
return out_file
示例3: _run_interface
def _run_interface(self, runtime):
_, _, ext = split_filename(self.inputs.max)
copyfile(self.inputs.max, os.path.abspath(self.inputs.input_data_prefix + "_max" + ext), copy=False)
_, _, ext = split_filename(self.inputs.ODF)
copyfile(self.inputs.ODF, os.path.abspath(self.inputs.input_data_prefix + "_odf" + ext), copy=False)
return super(ODFTracker, self)._run_interface(runtime)
示例4: test_copyfile
def test_copyfile():
orig_img, orig_hdr = _temp_analyze_files()
pth, fname = os.path.split(orig_img)
new_img = os.path.join(pth, 'newfile.img')
new_hdr = os.path.join(pth, 'newfile.hdr')
copyfile(orig_img, new_img)
yield assert_true, os.path.exists(new_img)
yield assert_true, os.path.exists(new_hdr)
os.unlink(new_img)
os.unlink(new_hdr)
# final cleanup
os.unlink(orig_img)
os.unlink(orig_hdr)
示例5: _list_outputs
def _list_outputs(self):
"""Execute this module.
"""
outdir = self.inputs.base_directory
if not isdefined(outdir):
outdir = '.'
outdir = os.path.abspath(outdir)
if isdefined(self.inputs.container):
outdir = os.path.join(outdir, self.inputs.container)
if not os.path.exists(outdir):
os.makedirs(outdir)
for key,files in self.inputs._outputs.items():
iflogger.debug("key: %s files: %s"%(key, str(files)))
files = filename_to_list(files)
outfiles = []
tempoutdir = outdir
for d in key.split('.'):
if d[0] == '@':
continue
tempoutdir = os.path.join(tempoutdir,d)
# flattening list
if isinstance(files, list):
if isinstance(files[0], list):
files = [item for sublist in files for item in sublist]
for src in filename_to_list(files):
src = os.path.abspath(src)
if os.path.isfile(src):
dst = self._get_dst(src)
dst = os.path.join(tempoutdir, dst)
dst = self._substitute(dst)
path,_ = os.path.split(dst)
if not os.path.exists(path):
os.makedirs(path)
iflogger.debug("copyfile: %s %s"%(src, dst))
copyfile(src, dst, copy=True)
elif os.path.isdir(src):
dst = self._get_dst(os.path.join(src,''))
dst = os.path.join(tempoutdir, dst)
dst = self._substitute(dst)
path,_ = os.path.split(dst)
if not os.path.exists(path):
os.makedirs(path)
if os.path.exists(dst):
iflogger.debug("removing: %s"%dst)
shutil.rmtree(dst)
iflogger.debug("copydir: %s %s"%(src, dst))
shutil.copytree(src, dst)
return None
示例6: _run_interface
def _run_interface(self, runtime):
if self.inputs.initialization == "PriorProbabilityImages":
priors_directory = os.path.join(os.getcwd(), "priors")
if not os.path.exists(priors_directory):
os.makedirs(priors_directory)
_, _, ext = split_filename(self.inputs.prior_probability_images[0])
for i, f in enumerate(self.inputs.prior_probability_images):
target = os.path.join(priors_directory,
'priorProbImages%02d' % (i + 1) + ext)
if not (os.path.exists(target) and os.path.realpath(target) == os.path.abspath(f)):
copyfile(os.path.abspath(f), os.path.join(priors_directory,
'priorProbImages%02d' % (i + 1) + ext))
runtime = super(Atropos, self)._run_interface(runtime)
return runtime
示例7: convert_rawdata
def convert_rawdata(base_directory, input_dir, out_prefix):
os.environ['UNPACK_MGH_DTI'] = '0'
file_list = os.listdir(input_dir)
# If RAWDATA folder contains one (and only one) gunzipped nifti file -> copy it
first_file = os.path.join(input_dir, file_list[0])
if len(file_list) == 1 and first_file.endswith('nii.gz'):
copyfile(first_file, os.path.join(base_directory, 'NIFTI', out_prefix+'.nii.gz'), False, False, 'content') # intelligent copy looking at input's content
else:
mem = Memory(base_dir=os.path.join(base_directory,'NIPYPE'))
mri_convert = mem.cache(fs.MRIConvert)
res = mri_convert(in_file=first_file, out_file=os.path.join(base_directory, 'NIFTI', out_prefix + '.nii.gz'))
if len(res.outputs.get()) == 0:
return False
return True
示例8: _copy_any
def _copy_any(src, dst):
src_isgz = src.endswith('.gz')
dst_isgz = dst.endswith('.gz')
if src_isgz == dst_isgz:
copyfile(src, dst, copy=True, use_hardlink=True)
return False # Make sure we do not reuse the hardlink later
# Unlink target (should not exist)
if os.path.exists(dst):
os.unlink(dst)
src_open = gzip.open if src_isgz else open
dst_open = gzip.open if dst_isgz else open
with src_open(src, 'rb') as f_in:
with dst_open(dst, 'wb') as f_out:
copyfileobj(f_in, f_out)
return True
示例9: _run_interface
def _run_interface(self, runtime):
out_file = self._gen_outfilename()
src_file = self.inputs.src_file
ref_file = self.inputs.ref_file
# Collect orientation infos
# "orientation" => 3 letter acronym defining orientation
src_orient = fs.utils.ImageInfo(in_file=src_file).run().outputs.orientation
ref_orient = fs.utils.ImageInfo(in_file=ref_file).run().outputs.orientation
# "convention" => RADIOLOGICAL/NEUROLOGICAL
src_conv = fsl.Orient(in_file=src_file, get_orient=True).run().outputs.orient
ref_conv = fsl.Orient(in_file=ref_file, get_orient=True).run().outputs.orient
if src_orient == ref_orient:
# no reorientation needed
copyfile(src_file, out_file, False, False, "content")
return runtime
else:
if src_conv != ref_conv:
# if needed, match convention (radiological/neurological) to reference
tmpsrc = os.path.join(os.path.dirname(src_file), "tmp_" + os.path.basename(src_file))
fsl.SwapDimensions(in_file=src_file, new_dims=("-x", "y", "z"), out_file=tmpsrc).run()
fsl.Orient(in_file=tmpsrc, swap_orient=True).run()
else:
# If conventions match, just use the original source
tmpsrc = src_file
tmp2 = os.path.join(os.path.dirname(src_file), "tmp.nii.gz")
map_orient = {"L": "RL", "R": "LR", "A": "PA", "P": "AP", "S": "IS", "I": "SI"}
fsl.SwapDimensions(
in_file=tmpsrc,
new_dims=(map_orient[ref_orient[0]], map_orient[ref_orient[1]], map_orient[ref_orient[2]]),
out_file=tmp2,
).run()
shutil.move(tmp2, out_file)
# Only remove the temporary file if the conventions did not match. Otherwise,
# we end up removing the output.
if tmpsrc != src_file:
os.remove(tmpsrc)
return runtime
示例10: inject_skullstripped
def inject_skullstripped(subjects_dir, subject_id, skullstripped):
mridir = op.join(subjects_dir, subject_id, 'mri')
t1 = op.join(mridir, 'T1.mgz')
bm_auto = op.join(mridir, 'brainmask.auto.mgz')
bm = op.join(mridir, 'brainmask.mgz')
if not op.exists(bm_auto):
img = nb.load(t1)
mask = nb.load(skullstripped)
bmask = new_img_like(mask, mask.get_data() > 0)
resampled_mask = resample_to_img(bmask, img, 'nearest')
masked_image = new_img_like(img, img.get_data() * resampled_mask.get_data())
masked_image.to_filename(bm_auto)
if not op.exists(bm):
copyfile(bm_auto, bm, copy=True, use_hardlink=True)
return subjects_dir, subject_id
示例11: index
def index(self, config):
fig_dir = 'figures'
subject_dir = self.root.split('/')[-1]
subject = re.search('^(?P<subject_id>sub-[a-zA-Z0-9]+)$', subject_dir).group()
svg_dir = os.path.join(self.out_dir, 'fmriprep', subject, fig_dir)
os.makedirs(svg_dir, exist_ok=True)
reportlet_list = list(sorted([str(f) for f in Path(self.root).glob('**/*.*')]))
for subrep_cfg in config:
reportlets = []
for reportlet_cfg in subrep_cfg['reportlets']:
rlet = Reportlet(**reportlet_cfg)
for src in reportlet_list:
ext = src.split('.')[-1]
if rlet.file_pattern.search(src):
contents = None
if ext == 'html':
with open(src) as fp:
contents = fp.read().strip()
elif ext == 'svg':
fbase = os.path.basename(src)
copyfile(src, os.path.join(svg_dir, fbase),
copy=True, use_hardlink=True)
contents = os.path.join(subject, fig_dir, fbase)
if contents:
rlet.source_files.append(src)
rlet.contents.append(contents)
if rlet.source_files:
reportlets.append(rlet)
if reportlets:
sub_report = SubReport(
subrep_cfg['name'], reportlets=reportlets,
title=subrep_cfg.get('title'))
self.sections.append(order_by_run(sub_report))
error_dir = os.path.join(self.out_dir, "fmriprep", subject, 'log', self.run_uuid)
if os.path.isdir(error_dir):
self.index_error_dir(error_dir)
示例12: _run_interface
def _run_interface(self, runtime):
out_file = self._gen_outfilename()
src_file = self.inputs.src_file
ref_file = self.inputs.ref_file
# Collect orientation infos
# "orientation" => 3 letter acronym defining orientation
src_orient = fs.utils.ImageInfo(in_file=src_file).run().outputs.orientation
ref_orient = fs.utils.ImageInfo(in_file=ref_file).run().outputs.orientation
# "convention" => RADIOLOGICAL/NEUROLOGICAL
src_conv = fsl.Orient(in_file=src_file, get_orient=True).run().outputs.orient
ref_conv = fsl.Orient(in_file=ref_file, get_orient=True).run().outputs.orient
if src_orient == ref_orient:
# no reorientation needed
copyfile(src_file,out_file,False, False, 'content')
return runtime
else:
if src_conv != ref_conv:
# if needed, match convention (radiological/neurological) to reference
tmpsrc = os.path.join(os.path.dirname(src_file), 'tmp_' + os.path.basename(src_file))
fsl.SwapDimensions(in_file=src_file, new_dims=('-x','y','z'), out_file=tmpsrc).run()
fsl.Orient(in_file=tmpsrc, swap_orient=True).run()
else:
# If conventions match, just use the original source
tmpsrc = src_file
tmp2 = os.path.join(os.path.dirname(src_file), 'tmp.nii.gz')
map_orient = {'L':'RL','R':'LR','A':'PA','P':'AP','S':'IS','I':'SI'}
fsl.SwapDimensions(in_file=tmpsrc, new_dims=(map_orient[ref_orient[0]],map_orient[ref_orient[1]],map_orient[ref_orient[2]]), out_file=tmp2).run()
shutil.move(tmp2, out_file)
# Only remove the temporary file if the conventions did not match. Otherwise,
# we end up removing the output.
if tmpsrc != src_file:
os.remove(tmpsrc)
return runtime
示例13: _run_interface
def _run_interface(self, runtime):
for i in range(1, len(self.inputs.thsamples) + 1):
_, _, ext = split_filename(self.inputs.thsamples[i - 1])
copyfile(self.inputs.thsamples[i - 1],
self.inputs.samples_base_name + "_th%dsamples" % i + ext,
copy=False)
_, _, ext = split_filename(self.inputs.thsamples[i - 1])
copyfile(self.inputs.phsamples[i - 1],
self.inputs.samples_base_name + "_ph%dsamples" % i + ext,
copy=False)
_, _, ext = split_filename(self.inputs.thsamples[i - 1])
copyfile(self.inputs.fsamples[i - 1],
self.inputs.samples_base_name + "_f%dsamples" % i + ext,
copy=False)
if isdefined(self.inputs.target_masks):
f = open("targets.txt", "w")
for target in self.inputs.target_masks:
f.write("%s\n" % target)
f.close()
if isinstance(self.inputs.seed, list):
f = open("seeds.txt", "w")
for seed in self.inputs.seed:
if isinstance(seed, list):
f.write("%s\n" % (" ".join([str(s) for s in seed])))
else:
f.write("%s\n" % seed)
f.close()
runtime = super(ProbTrackX, self)._run_interface(runtime)
if runtime.stderr:
self.raise_exception(runtime)
return runtime
示例14: test_linkchain
def test_linkchain():
if os.name is not 'posix':
return
orig_img, orig_hdr = _temp_analyze_files()
pth, fname = os.path.split(orig_img)
new_img1 = os.path.join(pth, 'newfile1.img')
new_hdr1 = os.path.join(pth, 'newfile1.hdr')
new_img2 = os.path.join(pth, 'newfile2.img')
new_hdr2 = os.path.join(pth, 'newfile2.hdr')
new_img3 = os.path.join(pth, 'newfile3.img')
new_hdr3 = os.path.join(pth, 'newfile3.hdr')
copyfile(orig_img, new_img1)
yield assert_true, os.path.islink(new_img1)
yield assert_true, os.path.islink(new_hdr1)
copyfile(new_img1, new_img2, copy=True)
yield assert_false, os.path.islink(new_img2)
yield assert_false, os.path.islink(new_hdr2)
yield assert_false, os.path.samefile(orig_img, new_img2)
yield assert_false, os.path.samefile(orig_hdr, new_hdr2)
copyfile(new_img1, new_img3, copy=True, use_hardlink=True)
yield assert_false, os.path.islink(new_img3)
yield assert_false, os.path.islink(new_hdr3)
yield assert_true, os.path.samefile(orig_img, new_img3)
yield assert_true, os.path.samefile(orig_hdr, new_hdr3)
os.unlink(new_img1)
os.unlink(new_hdr1)
os.unlink(new_img2)
os.unlink(new_hdr2)
os.unlink(new_img3)
os.unlink(new_hdr3)
# final cleanup
os.unlink(orig_img)
os.unlink(orig_hdr)
示例15: _run_interface
def _run_interface(self, runtime):
for i in range(1, len(self.inputs.thsamples) + 1):
_, _, ext = split_filename(self.inputs.thsamples[i - 1])
copyfile(self.inputs.thsamples[i - 1],
self.inputs.samples_base_name + "_th%dsamples" % i + ext,
copy=True)
_, _, ext = split_filename(self.inputs.thsamples[i - 1])
copyfile(self.inputs.phsamples[i - 1],
self.inputs.samples_base_name + "_ph%dsamples" % i + ext,
copy=True)
_, _, ext = split_filename(self.inputs.thsamples[i - 1])
copyfile(self.inputs.fsamples[i - 1],
self.inputs.samples_base_name + "_f%dsamples" % i + ext,
copy=True)
if isdefined(self.inputs.target_masks):
f = open("targets.txt", "w")
for target in self.inputs.target_masks:
f.write("%s\n" % target)
f.close()
runtime = super(mapped_ProbTrackX, self)._run_interface(runtime)
if runtime.stderr:
self.raise_exception(runtime)
return runtime