fsl.utils.path
This module contains a few utility functions for working with file system paths.
Finds the deepest directory which ends with one of the given sequence of suffixes, or returns |
|
Finds the shallowest directory which ends with one of the given sequence of suffixes, or returns |
|
Return a list containing all files which exist underneath the specified |
|
Convenience function which returns |
|
Adds a file extension to the given file |
|
Returns the base name of the given file name. |
|
Returns the extension of the given file name. |
|
Returns the base name and the extension from the given file name. |
|
If the given |
|
Reduces the list of |
|
Return the longest prefix for the given file name which unambiguously identifies it, relative to the other files in the same directory. |
|
Identifies the deepest common base directory shared by all files in |
|
Convert Windows path (or a command line argument containing a Windows path) to the equivalent WSL path (e.g. |
|
Convert a WSL-local filepath (for example |
- exception fsl.utils.path.PathError[source]
Bases:
ExceptionExceptionclass raised by the functions defined in this module when something goes wrong.- __annotations__ = {}
- __module__ = 'fsl.utils.path'
- __weakref__
list of weak references to the object (if defined)
- fsl.utils.path.deepest(path, suffixes)[source]
Finds the deepest directory which ends with one of the given sequence of suffixes, or returns
Noneif no directories end with any of the suffixes.
- fsl.utils.path.shallowest(path, suffixes)[source]
Finds the shallowest directory which ends with one of the given sequence of suffixes, or returns
Noneif no directories end with any of the suffixes.
- fsl.utils.path.allFiles(root)[source]
Return a list containing all files which exist underneath the specified
rootdirectory.
- fsl.utils.path.hasExt(path: str | Path, allowedExts: Sequence[str]) bool[source]
Convenience function which returns
Trueif the givenpathends with any of the givenallowedExts,Falseotherwise.
- fsl.utils.path.addExt(prefix: str | Path, allowedExts: Sequence[str] | None = None, mustExist: bool = True, defaultExt: str | None = None, fileGroups: Sequence[Sequence[str]] | None = None, unambiguous: bool = True) Sequence[str] | str[source]
Adds a file extension to the given file
prefix.If
mustExistis False, and the file does not already have a supported extension, the default extension is appended and the new file name returned. If the prefix already has a supported extension, it is returned unchanged.If
mustExistisTrue(the default), the function checks to see if any files exist that have the given prefix, and a supported file extension. APathErroris raised if:No files exist with the given prefix and a supported extension.
fileGroups is Noneandunambiguous is True, and more than one file exists with the given prefix, and a supported extension.
Otherwise the full file name is returned.
- Parameters:
prefix – The file name prefix to modify.
allowedExts – List of allowed file extensions.
mustExist – Whether the file must exist or not.
defaultExt – Default file extension to use.
fileGroups – Recognised file groups - see
getFileGroup().unambiguous – If
True(the default), and more than one file exists with the specifiedprefix, aPathErroris raised. Otherwise, a list containing all matching files is returned.
- fsl.utils.path.removeExt(filename: str | Path, allowedExts: Sequence[str] | None = None, firstDot: bool = False) str[source]
Returns the base name of the given file name. See
splitExt().
- fsl.utils.path.getExt(filename: str | Path, allowedExts: Sequence[str] | None = None, firstDot: bool = False) str[source]
Returns the extension of the given file name. See
splitExt().
- fsl.utils.path.splitExt(filename: str | Path, allowedExts: Sequence[str] | None = None, firstDot: bool = False) Tuple[str, str][source]
Returns the base name and the extension from the given file name.
If
allowedExtsisNoneandfirstDotisFalse, this function is equivalent to using:os.path.splitext(filename)
If
allowedExtsisNoneandfirstDotisTrue, the file name is split on the first period that is found, rather than the last period. For example:splitExt('image.nii.gz') # -> ('image.nii', '.gz') splitExt('image.nii.gz', firstDot=True) # -> ('image', '.nii.gz')
If
allowedExtsis provided,firstDotis ignored. In this case, if the file does not end with an allowed extension, a tuple containing(filename, '')is returned.- Parameters:
filename – The file name to split.
allowedExts – Allowed/recognised file extensions.
firstDot – Split the file name on the first period, rather than the last period. Ignored if
allowedExtsis specified.
- fsl.utils.path.getFileGroup(path, allowedExts=None, fileGroups=None, fullPaths=True, unambiguous=False)[source]
If the given
pathis part of afileGroup, returns a list containing the paths to all other files in the group (including thepathitself).If the
pathdoes not appear to be part of a file group, or appears to be part of an incomplete file group, a list containing only thepathis returned.If the
pathdoes not exist, or appears to be part of more than one file group, aPathErroris raised.File groups can be used to specify a collection of file suffixes which should always exist alongside each other. This can be used to resolve ambiguity when multiple files exist with the same
prefixand supported extensions (e.g.file.hdrandfile.img). The file groups are specified as a list of sequences, for example:[('.img', '.hdr'), ('.img.gz', '.hdr.gz')]
If you specify
fileGroups=[('.img', '.hdr')]andprefix='file', and bothfile.imgandfile.hdrexist, theaddExt()function would returnfile.img(i.e. the file which matches the first extension in the group).Similarly, if you call the
imcp.imcp()orimcp.immv()functions with the above parameters, bothfile.imgandfile.hdrwill be moved.Note
The primary use-case of file groups is to resolve ambiguity with respect to NIFTI and ANALYSE75 image pairs. By specifying
fileGroups=[('.img', '.hdr'), ('.img.gz', '.hdr.gz')], theaddExt(),imcp.immv()andimcp.imcp()functions are able to figure out what you mean when you specifyfile, and bothfile.hdrandfile.img(orfile.hdr.gzandfile.img.gz) exist.- Parameters:
path – Path to the file. Must contain the file extension.
allowedExts – Allowed/recognised file extensions.
fileGroups – Recognised file groups.
fullPaths – If
True(the default), full file paths (relative to thepath) are returned. Otherwise, only the file extensions in the group are returned.unambiguous – Defaults to
False. IfTrue, and the path is not unambiguously part of one group, or part of no groups, aPathErroris raised. Otherwise, the path is returned.
- fsl.utils.path.removeDuplicates(paths, allowedExts=None, fileGroups=None)[source]
Reduces the list of
pathsdown to those which are unique with respect to the specifiedfileGroups.For example, if you have a directory containing:
001.hdr 001.img 002.hdr 002.img 003.hdr 003.img
And you call
removeDuplicateslike so:paths = ['001.img', '001.hdr', '002.img', '002.hdr', '003.img', '003.hdr'] allowedExts = ['.img', '.hdr'] fileGroups = [('.img', '.hdr')] removeDuplicates(paths, allowedExts, fileGroups)
The returned list will be:
['001.img', '002.img', '003.img']
If you provide
allowedExts, you may specify incompletepaths(i.e. without extensions), as long as there are no path ambiguities.A
PathErrorwill be raised if any of thepathsdo not exist, or if there are any ambiguities with respect to incomplete paths.- Parameters:
paths – List of paths to reduce.
allowedExts – Allowed/recognised file extensions.
fileGroups – Recognised file groups - see
getFileGroup().
- fsl.utils.path.uniquePrefix(path)[source]
Return the longest prefix for the given file name which unambiguously identifies it, relative to the other files in the same directory.
Raises a
PathErrorif a unique prefix could not be found (which will never happen if the path is valid).
- fsl.utils.path.commonBase(paths)[source]
Identifies the deepest common base directory shared by all files in
paths.Raises a
PathErrorif the paths have no common base. This will never happen for absolute paths (as the base will be e.g.'/').
- fsl.utils.path.wslpath(path)[source]
Convert Windows path (or a command line argument containing a Windows path) to the equivalent WSL path (e.g.
c:\Users->/mnt/c/Users). Also supports paths in the form\wsl$\(distro)\users\...- Parameters:
winpath – Command line argument which may (or may not) contain a Windows path. It is assumed to be either of the form <windows path> or –<arg>=<windows path>. Note that we don’t need to handle –arg <windows path> or -a <windows path> since in these cases the argument and the path will be parsed as separate entities.
- Returns:
If
winpathmatches a Windows path, the converted argument (including the –<arg>= portion). Otherwise returnswinpathunchanged.
- fsl.utils.path.winpath(path)[source]
Convert a WSL-local filepath (for example
/usr/local/fsl/) into a path that can be used from Windows.If
self.fslwslisFalse, simply returnswslpathunmodified Otherwise, usesFSLDIRto deduce the WSL distro in use for FSL. This requires WSL2 which supports the\wsl$\network path. wslpath is assumed to be an absolute path.