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:
.. autosummary::
:nosignatures:
isSupported
looksLikeImage
removeExt
addExt
loadImage
......@@ -556,8 +556,8 @@ DEFAULT_EXTENSION = '.nii.gz'
"""The default file extension (TODO read this from ``$FSLOUTPUTTYPE``)."""
def isSupported(filename, allowedExts=None):
"""Returns ``True`` if the given file has a supported extension, ``False``
def looksLikeImage(filename, allowedExts=None):
"""Returns ``True`` if the given file looks like an image, ``False``
otherwise.
:arg filename: The file name to test.
......@@ -568,6 +568,9 @@ def isSupported(filename, allowedExts=None):
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))
......@@ -787,7 +790,8 @@ def saveImage(image, fromDir=None):
path = dlg.GetPath()
nibImage = image.nibImage
if not isSupported(path):
# Add a file extension if not specified
if not looksLikeImage(path):
path = addExt(path, False)
# this is an image which has been
......
......@@ -34,26 +34,46 @@ def getTensorDataPrefix(path):
fas = glob.glob(op.join(path, '*_FA.*'))
mds = glob.glob(op.join(path, '*_MD.*'))
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]
# Make sure that all of the above
# files have the same prefix
# Gather all of the existing file
# prefixes into a dictionary of
# prefix : [file list] mappings.
pattern = '^(.*)_(?:V1|V2|V3|L1|L2|L3|FA|MD).*$'
prefixes = [re.findall(pattern, f)[0] for f in files]
if any([p != prefixes[0] for p in prefixes]):
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
# 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])
......
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