-
Notifications
You must be signed in to change notification settings - Fork 535
ENH: Add "ExportFile" interface as simple alternative to "DataSink" #3054
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 16 commits
ee88157
b8d29a9
1b083f1
9bd195a
c870166
0d18f98
abe83dd
cba09b2
edcb73c
de9e299
b25c9c3
006cdcf
5737d21
47bb8e8
1ba9ca1
9897a73
ec023bf
1e0d7a1
4246b02
fe3dd07
7ed854f
7a3c7f6
1c26d1d
a23e16d
3695d36
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,12 +32,13 @@ | |
from .. import config, logging | ||
from ..utils.filemanip import ( | ||
copyfile, simplify_list, ensure_list, | ||
get_related_files) | ||
get_related_files, split_filename, | ||
FileExistsError) | ||
from ..utils.misc import human_order_sorted, str2bool | ||
from .base import ( | ||
TraitedSpec, traits, Str, File, Directory, BaseInterface, InputMultiPath, | ||
isdefined, OutputMultiPath, DynamicTraitedSpec, Undefined, BaseInterfaceInputSpec, | ||
LibraryBaseInterface) | ||
LibraryBaseInterface, SimpleInterface) | ||
|
||
iflogger = logging.getLogger('nipype.interface') | ||
|
||
|
@@ -2863,3 +2864,52 @@ def _list_outputs(self): | |
|
||
def _add_output_traits(self, base): | ||
return add_traits(base, list(self.inputs.output_query.keys())) | ||
|
||
|
||
class ExportFileInputSpec(BaseInterfaceInputSpec): | ||
in_file = File(exists=True, mandatory=True, desc='Input file name') | ||
out_file = File(mandatory=True, desc='Output file name') | ||
check_extension = traits.Bool(True, desc='Ensure that the input and output file extensions match') | ||
clobber = traits.Bool(desc='Permit overwriting existing files') | ||
|
||
|
||
class ExportFileOutputSpec(TraitedSpec): | ||
out_file = File(exists=True, desc='Output file name') | ||
|
||
|
||
class ExportFile(SimpleInterface): | ||
"""Copy in_file to out_file. | ||
stilley2 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
This interface copies an input file to a named output file. | ||
This is useful to save files to a specified location | ||
stilley2 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
(as opposed to using DataSink). | ||
stilley2 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
Examples | ||
-------- | ||
|
||
A trivial example that copies temporary_file.nii.gz | ||
to sub1_out.nii.gz. (A more realistic example would set | ||
in_file as the output of another Node.) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This feels like an unnecessary qualifier. I think you could probably just skip this text, unless you want to have multiple tests showing off |
||
|
||
>>> from nipype.interfaces.io import ExportFile | ||
>>> import os.path as op | ||
>>> ef = Node(ExportFile(), "export") | ||
stilley2 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
>>> ef.inputs.in_file = "temporary_file.nii.gz" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This file needs to exist. By default, all doctests are run in nipype/testing/data, so there are a lot of options to pick from. |
||
>>> ef.inputs.out_file = op.abspath("output_folder/sub1_out.nii.gz") | ||
effigies marked this conversation as resolved.
Show resolved
Hide resolved
|
||
>>> ef.run() | ||
stilley2 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
""" | ||
input_spec = ExportFileInputSpec | ||
output_spec = ExportFileOutputSpec | ||
|
||
def _run_interface(self, runtime): | ||
if not self.inputs.clobber and op.exists(self.inputs.out_file): | ||
stilley2 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
raise FileExistsError(self.inputs.out_file) | ||
if not op.isabs(self.inputs.out_file): | ||
raise ValueError('Out_file must be an absolute path.') | ||
if (self.inputs.check_extension and | ||
split_filename(self.inputs.in_file)[2] != split_filename(self.inputs.out_file)[2]): | ||
raise RuntimeError('%s and %s have different extensions' % (self.inputs.in_file, self.inputs.out_file)) | ||
shutil.copy(str(self.inputs.in_file), str(self.inputs.out_file)) | ||
self._results['out_file'] = self.inputs.out_file | ||
return runtime |
Uh oh!
There was an error while loading. Please reload this page.