Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
F
fslpy
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Deploy
Releases
Container Registry
Model registry
Monitor
Incidents
Analyze
Value stream analytics
Contributor 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
Michiel Cottaar
fslpy
Commits
72da9475
Commit
72da9475
authored
7 years ago
by
Paul McCarthy
Browse files
Options
Downloads
Patches
Plain Diff
Resample methods explicitly raises an error when num dims is wrong. Cleaned up
related code in atlas classes
parent
c73c918a
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
fsl/data/atlases.py
+38
-21
38 additions, 21 deletions
fsl/data/atlases.py
fsl/data/image.py
+5
-1
5 additions, 1 deletion
fsl/data/image.py
with
43 additions
and
22 deletions
fsl/data/atlases.py
+
38
−
21
View file @
72da9475
...
...
@@ -675,6 +675,42 @@ class Atlas(fslimage.Image):
return
self
.
desc
.
find
(
*
args
,
**
kwargs
)
def
prepareMask
(
self
,
mask
):
"""
Makes sure that the given mask has the same resolution as this
atlas, so it can be used for querying. Used by the
:meth:`.LabelAtlas.maskLabels` and
:meth:`.ProbabilisticAtlas.maskProportions` methods.
:arg mask: A :class:`.Image`
:returns: A ``numpy`` array containing the resampled mask data.
:raises: A :exc:`MaskError` if the mask is not in the same space as
this atlas, or does not have three dimensions.
"""
# Make sure that the mask has the same
# number of voxels as the atlas image.
# Use nearest neighbour interpolation
# for resampling, as it is most likely
# that the mask is binary.
try
:
mask
,
xform
=
mask
.
resample
(
self
.
shape
[:
3
],
dtype
=
np
.
float32
,
order
=
0
)
except
ValueError
:
raise
MaskError
(
'
Mask has wrong number of dimensions
'
)
# TODO allow non-aligned mask - as long as it overlaps
# in world coordinates, it should be allowed
if
not
fslimage
.
Image
(
mask
,
xform
=
xform
).
sameSpace
(
self
):
raise
MaskError
(
'
Mask is not in the same space as atlas
'
)
return
mask
class
MaskError
(
Exception
):
"""
Exception raised by the :meth:`LabelAtlas.maskLabel` and
:meth:`ProbabilisticAtlas.maskProportions` when a mask is provided which
...
...
@@ -781,23 +817,10 @@ class LabelAtlas(Atlas):
associated with each returned value.
"""
# Make sure that the mask has the same
# number of voxels as the atlas image.
# Use nearest neighbour interpolation
# for resampling, as it is most likely
# that the mask is binary.
mask
,
xform
=
mask
.
resample
(
self
.
shape
[:
3
],
dtype
=
np
.
float32
,
order
=
0
)
if
not
fslimage
.
Image
(
mask
,
xform
=
xform
).
sameSpace
(
self
):
raise
MaskError
(
'
Mask is not in the same space as atlas
'
)
# TODO allow non-aligned mask - as long as it overlaps
# in world coordinates, it should be allowed
# Extract the values that are in
# the mask, and their corresponding
# mask weights
mask
=
self
.
prepareMask
(
mask
)
boolmask
=
mask
>
0
vals
=
self
[
boolmask
]
weights
=
mask
[
boolmask
]
...
...
@@ -924,13 +947,7 @@ class ProbabilisticAtlas(Atlas):
props
=
[]
# Make sure that the mask has the same
# number of voxels as the atlas image
mask
,
xform
=
mask
.
resample
(
self
.
shape
[:
3
],
dtype
=
np
.
float32
,
order
=
0
)
if
not
fslimage
.
Image
(
mask
,
xform
=
xform
).
sameSpace
(
self
):
raise
MaskError
(
'
Mask is not in the same space as atlas
'
)
mask
=
self
.
prepareMask
(
mask
)
boolmask
=
mask
>
0
weights
=
mask
[
boolmask
]
weightsum
=
weights
.
sum
()
...
...
This diff is collapsed.
Click to expand it.
fsl/data/image.py
+
5
−
1
View file @
72da9475
...
...
@@ -1149,7 +1149,8 @@ class Image(Nifti):
:arg sliceobj: Slice into this ``Image``. If ``None``, the whole
image is resampled, and it is assumed that it has the
same number of dimensions as ``shape``.
same number of dimensions as ``shape``. A
:exc:`ValueError` is raised if this is not the case.
:arg dtype: ``numpy`` data type of the resampled data. If ``None``,
the :meth:`dtype` of this ``Image`` is used.
...
...
@@ -1184,6 +1185,9 @@ class Image(Nifti):
oldShape
=
np
.
array
(
data
.
shape
,
dtype
=
np
.
float
)
newShape
=
np
.
array
(
newShape
,
dtype
=
np
.
float
)
if
len
(
oldShape
)
!=
len
(
newShape
):
raise
ValueError
(
'
Shapes don
\'
t match
'
)
if
not
np
.
all
(
np
.
isclose
(
oldShape
,
newShape
)):
ratio
=
oldShape
/
newShape
...
...
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