Skip to content

Commit 4ab5554

Browse files
committed
copyfile uses get_related_files, tests for get_related_files
1 parent dcf560d commit 4ab5554

File tree

2 files changed

+59
-24
lines changed

2 files changed

+59
-24
lines changed

nipype/utils/filemanip.py

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,8 @@ def hash_timestamp(afile):
207207

208208

209209
def copyfile(originalfile, newfile, copy=False, create_new=False,
210-
hashmethod=None, use_hardlink=False):
210+
hashmethod=None, use_hardlink=False,
211+
copy_related_files=True):
211212
"""Copy or link ``originalfile`` to ``newfile``.
212213
213214
If ``use_hardlink`` is True, and the file can be hard-linked, then a
@@ -228,6 +229,9 @@ def copyfile(originalfile, newfile, copy=False, create_new=False,
228229
use_hardlink : Bool
229230
specifies whether to hard-link files, when able
230231
(Default=False), taking precedence over copy
232+
copy_related_files : Bool
233+
specifies whether to also operate on related files, as defined in
234+
``related_filetype_sets``
231235
232236
Returns
233237
-------
@@ -320,30 +324,36 @@ def copyfile(originalfile, newfile, copy=False, create_new=False,
320324
fmlogger.warn(e.message)
321325

322326
# Associated files
323-
_, _, this_type = split_filename(originalfile)
324-
for type_set in related_filetype_sets:
325-
if this_type in type_set:
326-
for alt_type in type_set:
327-
alt_ofile = originalfile[:-len(this_type)] + alt_type
328-
alt_nfile = newfile[:-len(this_type)] + alt_type
329-
if os.path.exists(alt_ofile) and not os.path.exists(alt_nfile):
330-
copyfile(alt_ofile, alt_nfile, copy,
331-
hashmethod=hashmethod,
332-
use_hardlink=use_hardlink)
327+
if copy_related_files:
328+
related_file_pairs = (get_related_files(f, include_this_file=False)
329+
for f in (originalfile, newfile))
330+
for alt_ofile, alt_nfile in zip(*related_file_pairs):
331+
if os.path.exists(alt_ofile):
332+
copyfile(alt_ofile, alt_nfile, copy, hashmethod=hashmethod,
333+
use_hardlink=use_hardlink, copy_related_files=False)
333334

334335
return newfile
335336

336337

337-
def get_related_files(filename):
338-
"""Returns a list of related files for Nifti-Pair, Analyze (SPM) and AFNI
339-
files
338+
def get_related_files(filename, include_this_file=True):
339+
"""Returns a list of related files, as defined in
340+
``related_filetype_sets``, for a filename. (e.g., Nifti-Pair, Analyze (SPM)
341+
and AFNI files).
342+
343+
Parameters
344+
----------
345+
filename : str
346+
File name to find related filetypes of.
347+
include_this_file : bool
348+
If true, output includes the input filename.
340349
"""
341350
related_files = []
342-
path, name, ext = split_filename(filename)
351+
path, name, this_type = split_filename(filename)
343352
for type_set in related_filetype_sets:
344-
if ext in type_set:
345-
for new_ext in type_set:
346-
related_files.append(os.path.join(path, name + new_ext))
353+
if this_type in type_set:
354+
for related_type in type_set:
355+
if include_this_file or related_type != this_type:
356+
related_files.append(os.path.join(path, name + related_type))
347357
if not len(related_files):
348358
related_files = [filename]
349359
return related_files

nipype/utils/tests/test_filemanip.py

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,14 @@
88
from tempfile import mkstemp, mkdtemp
99
import warnings
1010

11-
from ...testing import assert_equal, assert_true, assert_false, TempFATFS
11+
from ...testing import (assert_equal, assert_true, assert_false,
12+
assert_in, assert_not_in, TempFATFS)
1213
from ...utils.filemanip import (save_json, load_json,
13-
fname_presuffix, fnames_presuffix,
14-
hash_rename, check_forhash,
15-
copyfile, copyfiles,
16-
filename_to_list, list_to_filename,
17-
split_filename, get_related_files)
14+
fname_presuffix, fnames_presuffix,
15+
hash_rename, check_forhash,
16+
copyfile, copyfiles,
17+
filename_to_list, list_to_filename,
18+
split_filename, get_related_files)
1819

1920
import numpy as np
2021

@@ -249,6 +250,30 @@ def test_copyfallback():
249250
os.unlink(orig_hdr)
250251

251252

253+
def test_get_related_files():
254+
orig_img, orig_hdr = _temp_analyze_files()
255+
256+
related_files = get_related_files(orig_img)
257+
yield assert_in, orig_img, related_files
258+
yield assert_in, orig_hdr, related_files
259+
260+
related_files = get_related_files(orig_hdr)
261+
yield assert_in, orig_img, related_files
262+
yield assert_in, orig_hdr, related_files
263+
264+
265+
def test_get_related_files_noninclusive():
266+
orig_img, orig_hdr = _temp_analyze_files()
267+
268+
related_files = get_related_files(orig_img, include_this_file=False)
269+
yield assert_not_in, orig_img, related_files
270+
yield assert_in, orig_hdr, related_files
271+
272+
related_files = get_related_files(orig_hdr, include_this_file=False)
273+
yield assert_in, orig_img, related_files
274+
yield assert_not_in, orig_hdr, related_files
275+
276+
252277
def test_filename_to_list():
253278
x = filename_to_list('foo.nii')
254279
yield assert_equal, x, ['foo.nii']

0 commit comments

Comments
 (0)