Skip to content
Snippets Groups Projects
Commit 0aa6585f authored by Paul McCarthy's avatar Paul McCarthy :mountain_bicyclist:
Browse files

Make imglob work.

parent 67538f10
No related branches found
No related tags found
No related merge requests found
...@@ -4,16 +4,15 @@ ...@@ -4,16 +4,15 @@
# #
# Author: Paul McCarthy <pauldmccarthy@gmail.com> # Author: Paul McCarthy <pauldmccarthy@gmail.com>
# #
"""This module defines the ``imglob`` application, for expanding glob patterns """This module defines the ``imglob`` application, which identifies unique
involving NIFTI/ANALYZE image file name. NIFTI/ANALYZE image files.
""" """
from __future__ import print_function from __future__ import print_function
import os.path as op
import sys import sys
import glob import itertools as it
import fsl.utils.path as fslpath import fsl.utils.path as fslpath
import fsl.data.image as fslimage import fsl.data.image as fslimage
...@@ -29,44 +28,50 @@ groups = fslimage.FILE_GROUPS ...@@ -29,44 +28,50 @@ groups = fslimage.FILE_GROUPS
def main(argv=None): def main(argv=None):
"""The ``imglob`` utility. Prints image files which match one or more """The ``imglob`` utility. Given a list of file names, identifies and prints
shell-style wildcard patterns. the unique NIFTI/ANALYZE image files.
""" """
if argv is None: if argv is None:
argv = sys.argv[1:] argv = sys.argv[1:]
if len(argv) < 1: if len(argv) < 1:
raise RuntimeError(usage) print(usage)
return 1
if argv[0] == '-extension': output = 'primary' if argv[0] == '-extension': output = 'primary'
elif argv[0] == '-extensions': output = 'all' elif argv[0] == '-extensions': output = 'all'
else: output = 'prefix' else: output = 'prefix'
if output == 'prefix': patterns = argv if output == 'prefix': paths = argv
else: patterns = argv[1:] else: paths = argv[1:]
paths = [] imgfiles = []
for pattern in patterns: # Build a list of all image files (both
# hdr and img and otherwise) that match
for path in paths:
try:
imgfiles.extend(fslimage.addExt(path, unambiguous=False))
except fslpath.PathError:
continue
hits = glob.glob(pattern) if output == 'prefix':
hits = [fslimage.looksLikeImage(h) for h in hits] imgfiles = fslpath.removeDuplicates(imgfiles,
if output == 'prefix':
hits = fslpath.removeDuplicates(hits,
allowedExts=exts, allowedExts=exts,
fileGroups=groups) fileGroups=groups)
hits = [fslpath.removeExt(h, exts) for h in hits] imgfiles = [fslpath.removeExt(f, exts) for f in imgfiles]
elif output == 'primary':
hits = fslpath.removeDuplicates(hits, elif output == 'primary':
imgfiles = fslpath.removeDuplicates(imgfiles,
allowedExts=exts, allowedExts=exts,
fileGroups=groups) fileGroups=groups)
paths.extend(hits) imgfiles = sorted(set(imgfiles))
print(' '.join(imgfiles))
print(' '.join(paths)) return 0
if __name__ == '__main__': if __name__ == '__main__':
......
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