Skip to content

Commit a674003

Browse files
committed
RF: Added brikhead.filespec_to_file_map
Edited filespec_to_file_map in AFNIImage to handle BRIK/HEAD compression business more smoothly.
1 parent 6bd34fe commit a674003

File tree

1 file changed

+23
-15
lines changed

1 file changed

+23
-15
lines changed

nibabel/brikhead.py

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
import numpy as np
2222

2323
from .arrayproxy import ArrayProxy
24+
from .fileholders import FileHolder
25+
from .filename_parser import (types_filenames, TypesFilenamesError)
2426
from .fileslice import strided_scalar
2527
from .keywordonly import kw_only_meth
2628
from .spatialimages import (
@@ -527,21 +529,27 @@ def filespec_to_file_map(klass, filespec):
527529
If `filespec` is not recognizable as being a filename for this
528530
image type.
529531
"""
530-
file_map = super(AFNIImage, klass).filespec_to_file_map(filespec)
531-
# only BRIK can be compressed; remove potential compression suffixes from HEAD
532-
head_fname = file_map['header'].filename
533-
if not os.path.exists(head_fname):
534-
for ext in klass._compressed_suffixes:
535-
head_fname = head_fname[:-len(ext)] if head_fname.endswith(ext) else head_fname
536-
file_map['header'].filename = head_fname
537-
# if HEAD is read in and BRIK is compressed, function won't detect the
538-
# compressed format; check for these cases
539-
if not os.path.exists(file_map['image'].filename):
540-
for ext in klass._compressed_suffixes:
541-
im_ext = file_map['image'].filename + ext
542-
if os.path.exists(im_ext):
543-
file_map['image'].filename = im_ext
544-
break
532+
# copied from filebasedimages.py
533+
try:
534+
filenames = types_filenames(
535+
filespec, klass.files_types,
536+
trailing_suffixes=klass._compressed_suffixes)
537+
except TypesFilenamesError:
538+
raise ImageFileError(
539+
'Filespec "{0}" does not look right for class {1}'.format(
540+
filespec, klass))
541+
file_map = {}
542+
# check for AFNI-specific BRIK/HEAD compression idiosyncracies
543+
for key, fname in filenames.items():
544+
if key == 'header' and not os.path.exists(fname):
545+
for ext in klass._compressed_suffixes:
546+
fname = fname[:-len(ext)] if fname.endswith(ext) else fname
547+
elif key == 'image' and not os.path.exists(fname):
548+
for ext in klass._compressed_suffixes:
549+
if os.path.exists(fname + ext):
550+
fname += ext
551+
break
552+
file_map[key] = FileHolder(filename=fname)
545553
return file_map
546554

547555
load = from_filename

0 commit comments

Comments
 (0)