Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
F
fslpy
Manage
Activity
Members
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Analyze
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
FSL
fslpy
Commits
9f9668b0
Commit
9f9668b0
authored
9 years ago
by
Paul McCarthy
Browse files
Options
Downloads
Plain Diff
Merge branch 'oxford'
Conflicts: fsl/data/image.py
parents
690587cf
773b14b4
No related branches found
No related tags found
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
fsl/data/featresults.py
+37
-25
37 additions, 25 deletions
fsl/data/featresults.py
fsl/data/image.py
+13
-82
13 additions, 82 deletions
fsl/data/image.py
fsl/utils/path.py
+92
-1
92 additions, 1 deletion
fsl/utils/path.py
with
142 additions
and
108 deletions
fsl/data/featresults.py
+
37
−
25
View file @
9f9668b0
...
@@ -43,7 +43,6 @@ The following functions return the names of various files of interest:
...
@@ -43,7 +43,6 @@ The following functions return the names of various files of interest:
import
logging
import
logging
import
glob
import
os.path
as
op
import
os.path
as
op
import
numpy
as
np
import
numpy
as
np
...
@@ -104,11 +103,12 @@ def hasStats(featdir):
...
@@ -104,11 +103,12 @@ def hasStats(featdir):
for the given FEAT analysis, ``False`` otherwise.
for the given FEAT analysis, ``False`` otherwise.
"""
"""
statdir
=
op
.
join
(
featdir
,
'
stats
'
)
try
:
statfiles
=
glob
.
glob
(
op
.
join
(
statdir
,
'
*
'
))
getZStatFile
(
featdir
,
0
)
return
True
return
op
.
exists
(
statdir
)
and
len
(
statfiles
)
>
0
except
:
return
False
def
hasMelodicDir
(
featdir
):
def
hasMelodicDir
(
featdir
):
"""
Returns ``True`` if the data for the given FEAT directory has had
"""
Returns ``True`` if the data for the given FEAT directory has had
...
@@ -465,71 +465,83 @@ def getDataFile(featdir):
...
@@ -465,71 +465,83 @@ def getDataFile(featdir):
"""
Returns the name of the file in the FEAT directory which contains
"""
Returns the name of the file in the FEAT directory which contains
the model input data (typically called ``filtered_func_data.nii.gz``).
the model input data (typically called ``filtered_func_data.nii.gz``).
Raises a :exc:`ValueError` if the file does not exist.
:arg featdir: A FEAT directory.
:arg featdir: A FEAT directory.
"""
"""
datafile
=
op
.
join
(
featdir
,
'
filtered_func_data
'
)
# Assuming here that there is only
return
fslimage
.
addExt
(
datafile
,
mustExist
=
True
)
# one file called filtered_func_data.*
return
glob
.
glob
((
op
.
join
(
featdir
,
'
filtered_func_data.*
'
)))[
0
]
def
getMelodicFile
(
featdir
):
def
getMelodicFile
(
featdir
):
"""
Returns the name of the file in the FEAT results which contains the
"""
Returns the name of the file in the FEAT results which contains the melodic
melodic components. This file can be loaded as a :class:`.MelodicImage`.
components (if melodic ICA was performed as part of the FEAT
analysis). This file can be loaded as a :class:`.MelodicImage`.
Raises a :exc:`ValueError` if the file does not exist.
"""
"""
return
op
.
join
(
featdir
,
'
filtered_func_data.ica
'
,
'
melodic_IC.nii.gz
'
)
melfile
=
op
.
join
(
featdir
,
'
filtered_func_data.ica
'
,
'
melodic_IC
'
)
return
fslimage
.
addExt
(
melfile
,
mustExist
=
True
)
def
getResidualFile
(
featdir
):
def
getResidualFile
(
featdir
):
"""
Returns the name of the file in the FEAT results which contains
"""
Returns the name of the file in the FEAT results which contains
the model fit residuals (typically called ``res4d.nii.gz``).
the model fit residuals (typically called ``res4d.nii.gz``).
Raises a :exc:`ValueError` if the file does not exist.
:arg featdir: A FEAT directory.
:arg featdir: A FEAT directory.
"""
"""
resfile
=
op
.
join
(
featdir
,
'
stats
'
,
'
res4d
'
)
# Assuming here that there is only
return
fslimage
.
addExt
(
resfile
,
mustExist
=
True
)
# one file called stats/res4d.*
return
glob
.
glob
((
op
.
join
(
featdir
,
'
stats
'
,
'
res4d.*
'
)))[
0
]
def
getPEFile
(
featdir
,
ev
):
def
getPEFile
(
featdir
,
ev
):
"""
Returns the path of the PE file for the specified EV.
"""
Returns the path of the PE file for the specified EV.
Raises a :exc:`ValueError` if the file does not exist.
:arg featdir: A FEAT directory.
:arg featdir: A FEAT directory.
:arg ev: The EV number (0-indexed).
:arg ev: The EV number (0-indexed).
"""
"""
pefile
=
op
.
join
(
featdir
,
'
stats
'
,
'
pe{}
.*
'
.
format
(
ev
+
1
))
pefile
=
op
.
join
(
featdir
,
'
stats
'
,
'
pe{}
'
.
format
(
ev
+
1
))
return
glob
.
glob
(
pefile
)[
0
]
return
fslimage
.
addExt
(
pefile
,
mustExist
=
True
)
def
getCOPEFile
(
featdir
,
contrast
):
def
getCOPEFile
(
featdir
,
contrast
):
"""
Returns the path of the COPE file for the specified contrast.
"""
Returns the path of the COPE file for the specified contrast.
Raises a :exc:`ValueError` if the file does not exist.
:arg featdir: A FEAT directory.
:arg featdir: A FEAT directory.
:arg contrast: The contrast number (0-indexed).
:arg contrast: The contrast number (0-indexed).
"""
"""
copefile
=
op
.
join
(
featdir
,
'
stats
'
,
'
cope{}
.*
'
.
format
(
contrast
+
1
))
copefile
=
op
.
join
(
featdir
,
'
stats
'
,
'
cope{}
'
.
format
(
contrast
+
1
))
return
glob
.
glob
(
copefile
)[
0
]
return
fslimage
.
addExt
(
copefile
,
mustExist
=
True
)
def
getZStatFile
(
featdir
,
contrast
):
def
getZStatFile
(
featdir
,
contrast
):
"""
Returns the path of the Z-statistic file for the specified contrast.
"""
Returns the path of the Z-statistic file for the specified contrast.
Raises a :exc:`ValueError` if the file does not exist.
:arg featdir: A FEAT directory.
:arg featdir: A FEAT directory.
:arg contrast: The contrast number (0-indexed).
:arg contrast: The contrast number (0-indexed).
"""
"""
zfile
=
op
.
join
(
featdir
,
'
stats
'
,
'
zstat{}
.*
'
.
format
(
contrast
+
1
))
zfile
=
op
.
join
(
featdir
,
'
stats
'
,
'
zstat{}
'
.
format
(
contrast
+
1
))
return
glob
.
glob
(
zfile
)[
0
]
return
fslimage
.
addExt
(
zfile
,
mustExist
=
True
)
def
getClusterMaskFile
(
featdir
,
contrast
):
def
getClusterMaskFile
(
featdir
,
contrast
):
"""
Returns the path of the cluster mask file for the specified contrast.
"""
Returns the path of the cluster mask file for the specified contrast.
Raises a :exc:`ValueError` if the file does not exist.
:arg featdir: A FEAT directory.
:arg featdir: A FEAT directory.
:arg contrast: The contrast number (0-indexed).
:arg contrast: The contrast number (0-indexed).
"""
"""
mfile
=
op
.
join
(
featdir
,
'
cluster_mask_zstat{}
.*
'
.
format
(
contrast
+
1
))
mfile
=
op
.
join
(
featdir
,
'
cluster_mask_zstat{}
'
.
format
(
contrast
+
1
))
return
glob
.
glob
(
mfile
)[
0
]
return
fslimage
.
addExt
(
mfile
,
mustExist
=
True
)
def
getEVNames
(
settings
):
def
getEVNames
(
settings
):
...
...
This diff is collapsed.
Click to expand it.
fsl/data/image.py
+
13
−
82
View file @
9f9668b0
...
@@ -45,6 +45,8 @@ import props
...
@@ -45,6 +45,8 @@ import props
import
fsl.utils.transform
as
transform
import
fsl.utils.transform
as
transform
import
fsl.utils.status
as
status
import
fsl.utils.status
as
status
import
fsl.utils.path
as
fslpath
import
fsl.data.strings
as
fslstrings
import
fsl.data.constants
as
constants
import
fsl.data.constants
as
constants
...
@@ -604,97 +606,26 @@ def looksLikeImage(filename, allowedExts=None):
...
@@ -604,97 +606,26 @@ def looksLikeImage(filename, allowedExts=None):
return
any
(
map
(
lambda
ext
:
filename
.
endswith
(
ext
),
allowedExts
))
return
any
(
map
(
lambda
ext
:
filename
.
endswith
(
ext
),
allowedExts
))
def
removeExt
(
filename
,
allowedExts
=
None
):
def
removeExt
(
filename
):
"""
Removes the extension from the given file name. Returns the filename
"""
Removes the extension from the given file name. Returns the filename
unmodified if it does not have a supported extension.
unmodified if it does not have a supported extension.
:arg filename: The file name to strip.
See :func:`~fsl.utils.path.removeExt`.
:arg allowedExts: A list of strings containing the allowed file
extensions.
"""
if
allowedExts
is
None
:
allowedExts
=
ALLOWED_EXTENSIONS
# figure out the extension of the given file
extMatches
=
map
(
lambda
ext
:
filename
.
endswith
(
ext
),
allowedExts
)
# the file does not have a supported extension
if
not
any
(
extMatches
):
return
filename
# figure out the length of the matched extension
:arg filename: The file name to strip.
extIdx
=
extMatches
.
index
(
True
)
"""
extLen
=
len
(
allowedExts
[
extIdx
])
return
fslpath
.
removeExt
(
filename
,
ALLOWED_EXTENSIONS
)
# and trim it from the file name
return
filename
[:
-
extLen
]
def
addExt
(
prefix
,
mustExist
=
True
,
allowedExts
=
None
,
defaultExt
=
None
):
def
addExt
(
prefix
,
mustExist
=
True
):
"""
Adds a file extension to the given file ``prefix``.
"""
Adds a file extension to the given file ``prefix``.
If ``mustExist`` is False, and the file does not already have a
See :func:`~fsl.utils.path.addExt`.
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 ``mustExist`` is ``True`` (the default), the function checks to see
if any files exist that have the given prefix, and a supported file
extension. A :exc:`ValueError` is raised if:
- No files exist with the given prefix and a supported extension.
- More than one file exists with the given prefix, and a supported
extension.
Otherwise the full file name is returned.
:arg prefix: The file name refix to modify.
:arg mustExist: Whether the file must exist or not.
:arg allowedExts: List of allowed file extensions.
:arg defaultExt: Default file extension to use.
"""
"""
return
fslpath
.
addExt
(
prefix
,
if
allowedExts
is
None
:
allowedExts
=
ALLOWED_EXTENSIONS
ALLOWED_EXTENSIONS
,
if
defaultExt
is
None
:
defaultExt
=
DEFAULT_EXTENSION
mustExist
,
DEFAULT_EXTENSION
)
if
not
mustExist
:
# the provided file name already
# ends with a supported extension
if
any
(
map
(
lambda
ext
:
prefix
.
endswith
(
ext
),
allowedExts
)):
return
prefix
return
prefix
+
defaultExt
# If the provided prefix already ends with a
# supported extension , check to see that it exists
if
any
(
map
(
lambda
ext
:
prefix
.
endswith
(
ext
),
allowedExts
)):
extended
=
[
prefix
]
# Otherwise, make a bunch of file names, one per
# supported extension, and test to see if exactly
# one of them exists.
else
:
extended
=
map
(
lambda
ext
:
prefix
+
ext
,
allowedExts
)
exists
=
map
(
op
.
isfile
,
extended
)
# Could not find any supported file
# with the specified prefix
if
not
any
(
exists
):
raise
ValueError
(
'
Could not find a supported file with prefix {}
'
.
format
(
prefix
))
# Ambiguity! More than one supported
# file with the specified prefix
if
len
(
filter
(
bool
,
exists
))
>
1
:
raise
ValueError
(
'
More than one file with prefix {}
'
.
format
(
prefix
))
# Return the full file name of the
# supported file that was found
extIdx
=
exists
.
index
(
True
)
return
extended
[
extIdx
]
def
loadImage
(
filename
):
def
loadImage
(
filename
):
...
...
This diff is collapsed.
Click to expand it.
fsl/utils/path.py
+
92
−
1
View file @
9f9668b0
...
@@ -12,7 +12,9 @@ paths.
...
@@ -12,7 +12,9 @@ paths.
:nosignatures:
:nosignatures:
deepest
deepest
shallowest
shallowest
addExt
removeExt
"""
"""
...
@@ -60,3 +62,92 @@ def shallowest(path, suffixes):
...
@@ -60,3 +62,92 @@ def shallowest(path, suffixes):
return
path
return
path
return
None
return
None
def
addExt
(
prefix
,
allowedExts
,
mustExist
=
True
,
defaultExt
=
None
):
"""
Adds a file extension to the given file ``prefix``.
If ``mustExist`` is 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 ``mustExist`` is ``True`` (the default), the function checks to see
if any files exist that have the given prefix, and a supported file
extension. A :exc:`ValueError` is raised if:
- No files exist with the given prefix and a supported extension.
- More than one file exists with the given prefix, and a supported
extension.
Otherwise the full file name is returned.
:arg prefix: The file name refix to modify.
:arg mustExist: Whether the file must exist or not.
:arg allowedExts: List of allowed file extensions.
:arg defaultExt: Default file extension to use.
"""
if
not
mustExist
:
# the provided file name already
# ends with a supported extension
if
any
(
map
(
lambda
ext
:
prefix
.
endswith
(
ext
),
allowedExts
)):
return
prefix
if
defaultExt
is
not
None
:
return
prefix
+
defaultExt
else
:
return
None
# If the provided prefix already ends with a
# supported extension , check to see that it exists
if
any
(
map
(
lambda
ext
:
prefix
.
endswith
(
ext
),
allowedExts
)):
extended
=
[
prefix
]
# Otherwise, make a bunch of file names, one per
# supported extension, and test to see if exactly
# one of them exists.
else
:
extended
=
map
(
lambda
ext
:
prefix
+
ext
,
allowedExts
)
exists
=
map
(
op
.
isfile
,
extended
)
# Could not find any supported file
# with the specified prefix
if
not
any
(
exists
):
raise
ValueError
(
'
Could not find a supported file with prefix {}
'
.
format
(
prefix
))
# Ambiguity! More than one supported
# file with the specified prefix
if
len
(
filter
(
bool
,
exists
))
>
1
:
raise
ValueError
(
'
More than one file with prefix {}
'
.
format
(
prefix
))
# Return the full file name of the
# supported file that was found
extIdx
=
exists
.
index
(
True
)
return
extended
[
extIdx
]
def
removeExt
(
filename
,
allowedExts
):
"""
Removes the extension from the given file name. Returns the filename
unmodified if it does not have a supported extension.
:arg filename: The file name to strip.
:arg allowedExts: A list of strings containing the allowed file
extensions.
"""
# figure out the extension of the given file
extMatches
=
map
(
lambda
ext
:
filename
.
endswith
(
ext
),
allowedExts
)
# the file does not have a supported extension
if
not
any
(
extMatches
):
return
filename
# figure out the length of the matched extension
extIdx
=
extMatches
.
index
(
True
)
extLen
=
len
(
allowedExts
[
extIdx
])
# and trim it from the file name
return
filename
[:
-
extLen
]
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment