2929fmlogger = logging .getLogger ("filemanip" )
3030
3131
32+ related_filetype_sets = [
33+ ('.hdr' , '.img' , '.mat' ),
34+ ('.BRIK' , '.HEAD' ),
35+ ]
36+
37+
3238class FileNotFoundError (Exception ):
3339 pass
3440
@@ -215,7 +221,8 @@ def hash_timestamp(afile):
215221
216222
217223def copyfile (originalfile , newfile , copy = False , create_new = False ,
218- hashmethod = None , use_hardlink = False ):
224+ hashmethod = None , use_hardlink = False ,
225+ copy_related_files = True ):
219226 """Copy or link ``originalfile`` to ``newfile``.
220227
221228 If ``use_hardlink`` is True, and the file can be hard-linked, then a
@@ -236,6 +243,9 @@ def copyfile(originalfile, newfile, copy=False, create_new=False,
236243 use_hardlink : Bool
237244 specifies whether to hard-link files, when able
238245 (Default=False), taking precedence over copy
246+ copy_related_files : Bool
247+ specifies whether to also operate on related files, as defined in
248+ ``related_filetype_sets``
239249
240250 Returns
241251 -------
@@ -328,38 +338,36 @@ def copyfile(originalfile, newfile, copy=False, create_new=False,
328338 fmlogger .warn (e .message )
329339
330340 # Associated files
331- if originalfile .endswith (".img" ):
332- hdrofile = originalfile [:- 4 ] + ".hdr"
333- hdrnfile = newfile [:- 4 ] + ".hdr"
334- matofile = originalfile [:- 4 ] + ".mat"
335- if os .path .exists (matofile ):
336- matnfile = newfile [:- 4 ] + ".mat"
337- copyfile (matofile , matnfile , copy , hashmethod = hashmethod ,
338- use_hardlink = use_hardlink )
339- copyfile (hdrofile , hdrnfile , copy , hashmethod = hashmethod ,
340- use_hardlink = use_hardlink )
341- elif originalfile .endswith (".BRIK" ):
342- hdrofile = originalfile [:- 5 ] + ".HEAD"
343- hdrnfile = newfile [:- 5 ] + ".HEAD"
344- copyfile (hdrofile , hdrnfile , copy , hashmethod = hashmethod ,
345- use_hardlink = use_hardlink )
341+ if copy_related_files :
342+ related_file_pairs = (get_related_files (f , include_this_file = False )
343+ for f in (originalfile , newfile ))
344+ for alt_ofile , alt_nfile in zip (* related_file_pairs ):
345+ if os .path .exists (alt_ofile ):
346+ copyfile (alt_ofile , alt_nfile , copy , hashmethod = hashmethod ,
347+ use_hardlink = use_hardlink , copy_related_files = False )
346348
347349 return newfile
348350
349351
350- def get_related_files (filename ):
351- """Returns a list of related files for Nifti-Pair, Analyze (SPM) and AFNI
352- files
352+ def get_related_files (filename , include_this_file = True ):
353+ """Returns a list of related files, as defined in
354+ ``related_filetype_sets``, for a filename. (e.g., Nifti-Pair, Analyze (SPM)
355+ and AFNI files).
356+
357+ Parameters
358+ ----------
359+ filename : str
360+ File name to find related filetypes of.
361+ include_this_file : bool
362+ If true, output includes the input filename.
353363 """
354364 related_files = []
355- if filename .endswith (".img" ) or filename .endswith (".hdr" ):
356- path , name , ext = split_filename (filename )
357- for ext in ['.hdr' , '.img' , '.mat' ]:
358- related_files .append (os .path .join (path , name + ext ))
359- elif filename .endswith (".BRIK" ) or filename .endswith (".HEAD" ):
360- path , name , ext = split_filename (filename )
361- for ext in ['.BRIK' , '.HEAD' ]:
362- related_files .append (os .path .join (path , name + ext ))
365+ path , name , this_type = split_filename (filename )
366+ for type_set in related_filetype_sets :
367+ if this_type in type_set :
368+ for related_type in type_set :
369+ if include_this_file or related_type != this_type :
370+ related_files .append (os .path .join (path , name + related_type ))
363371 if not len (related_files ):
364372 related_files = [filename ]
365373 return related_files
0 commit comments