|
29 | 29 | fmlogger = logging.getLogger("filemanip") |
30 | 30 |
|
31 | 31 |
|
| 32 | +related_filetype_sets = [ |
| 33 | + ('.hdr', '.img', '.mat'), |
| 34 | + ('.BRIK', '.HEAD'), |
| 35 | +] |
| 36 | + |
| 37 | + |
32 | 38 | class FileNotFoundError(Exception): |
33 | 39 | pass |
34 | 40 |
|
@@ -314,38 +320,30 @@ def copyfile(originalfile, newfile, copy=False, create_new=False, |
314 | 320 | fmlogger.warn(e.message) |
315 | 321 |
|
316 | 322 | # 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) |
332 | 333 |
|
333 | 334 | return newfile |
334 | 335 |
|
335 | 336 |
|
336 | 337 | def get_related_files(filename): |
337 | 338 | """Returns a list of related files for Nifti-Pair, Analyze (SPM) and AFNI |
338 | | - files |
| 339 | + files |
339 | 340 | """ |
340 | 341 | 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)) |
349 | 347 | if not len(related_files): |
350 | 348 | related_files = [filename] |
351 | 349 | return related_files |
|
0 commit comments