Commit 37cdaf1f authored by Paul McCarthy's avatar Paul McCarthy
Browse files

Moved feat/melodic directory search routines to a separate generic

module. Hopefully fixed TimeSeries confusion w.r.t. nested
.feat/.ica directories
parent 15ba00ee
...@@ -48,6 +48,7 @@ import os.path as op ...@@ -48,6 +48,7 @@ import os.path as op
import numpy as np import numpy as np
import fsl.data.image as fslimage import fsl.data.image as fslimage
import fsl.utils.path as fslpath
import fsl.utils.transform as transform import fsl.utils.transform as transform
...@@ -120,49 +121,20 @@ def getAnalysisDir(path): ...@@ -120,49 +121,20 @@ def getAnalysisDir(path):
"""If the given path is contained within a FEAT directory, the path """If the given path is contained within a FEAT directory, the path
to that FEAT directory is returned. Otherwise, ``None`` is returned. to that FEAT directory is returned. Otherwise, ``None`` is returned.
""" """
featdir = fslpath.deepest(path, ['.feat', '.gfeat'])
# This is basically the opposite to if featdir is not None and isFEATDir(featdir):
# the getTopLevelAnalysisDir function return featdir
path = path.strip() return None
if path == op.sep or path == '':
return None
path = path.rstrip(op.sep)
sufs = ['.feat', '.gfeat']
if any([path.endswith(suf) for suf in sufs]):
return path
return getAnalysisDir(op.dirname(path))
def getTopLevelAnalysisDir(path): def getTopLevelAnalysisDir(path):
"""If the given path is contained within a FEAT directory, or a """If the given path is contained within a hierarchy of FEAT or MELODIC
MELODIC directory, the path to the latter directory is returned. directories, the path to the highest-level (i.e. the shallowest in the
Otherwise, ``None`` is returned. file system) directory is returned. Otherwise, ``None`` is returned.
""" """
return fslpath.shallowest(path, ['.ica', '.gica', '.feat', '.gfeat'])
path = path.strip()
# We've reached the root of the file system
if path == op.sep or path == '':
return None
path = path.rstrip(op.sep)
parent = getTopLevelAnalysisDir(op.dirname(path))
if parent is not None:
return parent
sufs = ['.ica', '.gica', '.feat', '.gfeat']
if any([path.endswith(suf) for suf in sufs]):
return path
return None
def loadDesign(featdir): def loadDesign(featdir):
......
...@@ -16,6 +16,7 @@ following functions are provided: ...@@ -16,6 +16,7 @@ following functions are provided:
isMelodicImage isMelodicImage
isMelodicDir isMelodicDir
getAnalysisDir
getTopLevelAnalysisDir getTopLevelAnalysisDir
getDataFile getDataFile
getICFile getICFile
...@@ -38,6 +39,7 @@ import numpy as np ...@@ -38,6 +39,7 @@ import numpy as np
import props import props
import fsl.utils.path as fslpath
import fsl.data.image as fslimage import fsl.data.image as fslimage
import fsl.data.featresults as featresults import fsl.data.featresults as featresults
...@@ -91,7 +93,27 @@ def isMelodicDir(path): ...@@ -91,7 +93,27 @@ def isMelodicDir(path):
return True return True
getTopLevelAnalysisDir = featresults.getTopLevelAnalysisDir def getAnalysisDir(path):
"""If the given path is contained within a MELODIC directory, the path
to that MELODIC directory is returned. Otherwise, ``None`` is returned.
"""
meldir = fslpath.deepest(path, ['.ica', '.gica'])
if meldir is not None and isMelodicDir(meldir):
return meldir
return None
def getTopLevelAnalysisDir(path):
"""If the given path is contained within a hierarchy of FEAT or MELODIC
directories, the path to the highest-level (i.e. the shallowest in the
file system) directory is returned. Otherwise, ``None`` is returned.
See :func:`.featresults.getTopLevelAnalysisDir`.
"""
return featresults.getTopLevelAnalysisDir(path)
def getDataFile(meldir): def getDataFile(meldir):
......
...@@ -17,8 +17,9 @@ import props ...@@ -17,8 +17,9 @@ import props
import plotpanel import plotpanel
import fsl.data.featimage as fslfeatimage import fsl.data.featimage as fslfeatimage
import fsl.data.featresults as featresults
import fsl.data.melodicimage as fslmelimage import fsl.data.melodicimage as fslmelimage
import fsl.data.featresults as featresults
import fsl.data.melodicresults as melresults
import fsl.data.image as fslimage import fsl.data.image as fslimage
import fsl.fsleyes.actions as actions import fsl.fsleyes.actions as actions
import fsl.fsleyes.plotting as plotting import fsl.fsleyes.plotting as plotting
...@@ -252,9 +253,18 @@ class TimeSeriesPanel(plotpanel.OverlayPlotPanel): ...@@ -252,9 +253,18 @@ class TimeSeriesPanel(plotpanel.OverlayPlotPanel):
if overlay.dataSource is not None: if overlay.dataSource is not None:
featPath = featresults.getAnalysisDir(overlay.dataSource) featPath = featresults.getAnalysisDir(overlay.dataSource)
melPath = melresults .getAnalysisDir(overlay.dataSource)
else: else:
featPath = None featPath = None
melPath = None
# If this overlay is from an .ica dir
# inside a .feat dir or vice versa,
# the longer/deeper path takes precedence.
if (featPath is not None) and (melPath is not None):
if len(melPath) > len(featPath): featPath = None
else: melPath = None
# Is this a FEAT filtered_func_data image, # Is this a FEAT filtered_func_data image,
# or an image in a FEAT directory? # or an image in a FEAT directory?
if isinstance(overlay, fslfeatimage.FEATImage) or featPath is not None: if isinstance(overlay, fslfeatimage.FEATImage) or featPath is not None:
......
#!/usr/bin/env python
#
# path.py - Utility functions for working with file/directory paths.
#
# Author: Paul McCarthy <pauldmccarthy@gmail.com>
#
"""This module contains a few utility functions for working with file system
paths.
.. autosummary::
:nosignatures:
deepest
shallowest
"""
import os.path as op
def deepest(path, suffixes):
"""Finds the deepest directory which ends with one of the given
sequence of suffixes, or returns ``None`` if no directories end
with any of the suffixes.
"""
path = path.strip()
if path == op.sep or path == '':
return None
path = path.rstrip(op.sep)
if any([path.endswith(s) for s in suffixes]):
return path
return deepest(op.dirname(path), suffixes)
def shallowest(path, suffixes):
"""Finds the shallowest directory which ends with one of the given
sequence of suffixes, or returns ``None`` if no directories end
with any of the suffixes.
"""
path = path.strip()
# We've reached the root of the file system
if path == op.sep or path == '':
return None
path = path.rstrip(op.sep)
parent = shallowest(op.dirname(path), suffixes)
if parent is not None:
return parent
if any([path.endswith(s) for s in suffixes]):
return path
return None
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment