Skip to content
Snippets Groups Projects
Commit e1afbc10 authored by Paul McCarthy's avatar Paul McCarthy
Browse files

Made tensor data prefix function a bit more robust.

parent 344993ed
No related branches found
No related tags found
No related merge requests found
...@@ -23,7 +23,7 @@ file names: ...@@ -23,7 +23,7 @@ file names:
.. autosummary:: .. autosummary::
:nosignatures: :nosignatures:
isSupported looksLikeImage
removeExt removeExt
addExt addExt
loadImage loadImage
...@@ -556,8 +556,8 @@ DEFAULT_EXTENSION = '.nii.gz' ...@@ -556,8 +556,8 @@ DEFAULT_EXTENSION = '.nii.gz'
"""The default file extension (TODO read this from ``$FSLOUTPUTTYPE``).""" """The default file extension (TODO read this from ``$FSLOUTPUTTYPE``)."""
def isSupported(filename, allowedExts=None): def looksLikeImage(filename, allowedExts=None):
"""Returns ``True`` if the given file has a supported extension, ``False`` """Returns ``True`` if the given file looks like an image, ``False``
otherwise. otherwise.
:arg filename: The file name to test. :arg filename: The file name to test.
...@@ -568,6 +568,9 @@ def isSupported(filename, allowedExts=None): ...@@ -568,6 +568,9 @@ def isSupported(filename, allowedExts=None):
if allowedExts is None: allowedExts = ALLOWED_EXTENSIONS if allowedExts is None: allowedExts = ALLOWED_EXTENSIONS
# TODO A much more robust approach would be
# to try loading the file using nibabel.
return any(map(lambda ext: filename.endswith(ext), allowedExts)) return any(map(lambda ext: filename.endswith(ext), allowedExts))
...@@ -787,7 +790,8 @@ def saveImage(image, fromDir=None): ...@@ -787,7 +790,8 @@ def saveImage(image, fromDir=None):
path = dlg.GetPath() path = dlg.GetPath()
nibImage = image.nibImage nibImage = image.nibImage
if not isSupported(path): # Add a file extension if not specified
if not looksLikeImage(path):
path = addExt(path, False) path = addExt(path, False)
# this is an image which has been # this is an image which has been
......
...@@ -34,26 +34,46 @@ def getTensorDataPrefix(path): ...@@ -34,26 +34,46 @@ def getTensorDataPrefix(path):
fas = glob.glob(op.join(path, '*_FA.*')) fas = glob.glob(op.join(path, '*_FA.*'))
mds = glob.glob(op.join(path, '*_MD.*')) mds = glob.glob(op.join(path, '*_MD.*'))
files = [v1s, v2s, v3s, l1s, l2s, l3s, fas, mds] files = [v1s, v2s, v3s, l1s, l2s, l3s, fas, mds]
# Make sure there is exactly one
# of each of the above files
def lenone(l):
return len(l) == 1
if not all(map(lenone, files)):
return None
files = [f[0] for f in files] # Gather all of the existing file
# prefixes into a dictionary of
# Make sure that all of the above # prefix : [file list] mappings.
# files have the same prefix
pattern = '^(.*)_(?:V1|V2|V3|L1|L2|L3|FA|MD).*$' pattern = '^(.*)_(?:V1|V2|V3|L1|L2|L3|FA|MD).*$'
prefixes = [re.findall(pattern, f)[0] for f in files] prefixes = {}
if any([p != prefixes[0] for p in prefixes]): for f in [f for flist in files for f in flist]:
prefix = re.findall(pattern, f)[0]
if prefix not in prefixes: prefixes[prefix] = [f]
else: prefixes[prefix].append(f)
# Discard any prefixes which are
# not present for every file type.
for prefix, files in list(prefixes.items()):
if len(files) != 8:
prefixes.pop(prefix)
# Discard any prefixes which
# match any files that do
# not look like image files
for prefix, files in list(prefixes.items()):
if not all([fslimage.looksLikeImage(f) for f in files]):
prefixes.pop(prefix)
prefixes = list(prefixes.keys())
# No more prefixes remaining -
# this is probably not a dtifit
# directory
if len(prefixes) == 0:
return None return None
# And there's our prefix # If there's more than one remaining
# prefix, I don't know what to do -
# just return the first one.
if len(prefixes) > 1:
log.warning('Multiple dtifit prefixes detected: {}'.format(prefixes))
return op.basename(prefixes[0]) return op.basename(prefixes[0])
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment