From e1afbc10417560b4559ac455f51216c9e28b08cd Mon Sep 17 00:00:00 2001 From: Paul McCarthy <pauld.mccarthy@gmail.com> Date: Fri, 18 Mar 2016 13:05:20 +0000 Subject: [PATCH] Made tensor data prefix function a bit more robust. --- fsl/data/image.py | 12 ++++++---- fsl/data/tensorimage.py | 52 ++++++++++++++++++++++++++++------------- 2 files changed, 44 insertions(+), 20 deletions(-) diff --git a/fsl/data/image.py b/fsl/data/image.py index fe179c4e7..449878b4e 100644 --- a/fsl/data/image.py +++ b/fsl/data/image.py @@ -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 diff --git a/fsl/data/tensorimage.py b/fsl/data/tensorimage.py index 838910c8a..0bc0627ac 100644 --- a/fsl/data/tensorimage.py +++ b/fsl/data/tensorimage.py @@ -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]) -- GitLab