Skip to content

Commit dcf560d

Browse files
committed
DRY, define related filetypes once.
1 parent bd3a7ef commit dcf560d

File tree

1 file changed

+22
-24
lines changed

1 file changed

+22
-24
lines changed

nipype/utils/filemanip.py

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,12 @@
2929
fmlogger = logging.getLogger("filemanip")
3030

3131

32+
related_filetype_sets = [
33+
('.hdr', '.img', '.mat'),
34+
('.BRIK', '.HEAD'),
35+
]
36+
37+
3238
class FileNotFoundError(Exception):
3339
pass
3440

@@ -314,38 +320,30 @@ def copyfile(originalfile, newfile, copy=False, create_new=False,
314320
fmlogger.warn(e.message)
315321

316322
# Associated files
317-
if originalfile.endswith(".img"):
318-
hdrofile = originalfile[:-4] + ".hdr"
319-
hdrnfile = newfile[:-4] + ".hdr"
320-
matofile = originalfile[:-4] + ".mat"
321-
if os.path.exists(matofile):
322-
matnfile = newfile[:-4] + ".mat"
323-
copyfile(matofile, matnfile, copy, hashmethod=hashmethod,
324-
use_hardlink=use_hardlink)
325-
copyfile(hdrofile, hdrnfile, copy, hashmethod=hashmethod,
326-
use_hardlink=use_hardlink)
327-
elif originalfile.endswith(".BRIK"):
328-
hdrofile = originalfile[:-5] + ".HEAD"
329-
hdrnfile = newfile[:-5] + ".HEAD"
330-
copyfile(hdrofile, hdrnfile, copy, hashmethod=hashmethod,
331-
use_hardlink=use_hardlink)
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)
332333

333334
return newfile
334335

335336

336337
def get_related_files(filename):
337338
"""Returns a list of related files for Nifti-Pair, Analyze (SPM) and AFNI
338-
files
339+
files
339340
"""
340341
related_files = []
341-
if filename.endswith(".img") or filename.endswith(".hdr"):
342-
path, name, ext = split_filename(filename)
343-
for ext in ['.hdr', '.img', '.mat']:
344-
related_files.append(os.path.join(path, name + ext))
345-
elif filename.endswith(".BRIK") or filename.endswith(".HEAD"):
346-
path, name, ext = split_filename(filename)
347-
for ext in ['.BRIK', '.HEAD']:
348-
related_files.append(os.path.join(path, name + ext))
342+
path, name, ext = split_filename(filename)
343+
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))
349347
if not len(related_files):
350348
related_files = [filename]
351349
return related_files

0 commit comments

Comments
 (0)