Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
FSL
fslpy
Commits
5f67d58c
Commit
5f67d58c
authored
May 03, 2019
by
Paul McCarthy
🚵
Browse files
TEST: Expand resample tests
parent
4e0acb87
Changes
1
Show whitespace changes
Inline
Side-by-side
tests/test_image_resample.py
View file @
5f67d58c
...
...
@@ -2,7 +2,6 @@
import
itertools
as
it
import
os.path
as
op
import
numpy
as
np
import
pytest
...
...
@@ -10,24 +9,17 @@ import pytest
import
fsl.data.image
as
fslimage
import
fsl.utils.transform
as
transform
import
fsl.utils.image.resample
as
resample
from
fsl.utils.tempdir
import
tempdir
from
.
import
make_random_image
def
test_resample
(
seed
):
with
tempdir
()
as
td
:
fname
=
op
.
join
(
td
,
'test.nii'
)
# Random base image shapes
for
i
in
range
(
25
):
shape
=
np
.
random
.
randint
(
5
,
50
,
3
)
make_random_image
(
fname
,
shape
)
img
=
fslimage
.
Image
(
fname
,
mmap
=
False
)
img
=
fslimage
.
Image
(
make_random_image
(
dims
=
shape
))
# bad shape
with
pytest
.
raises
(
ValueError
):
...
...
@@ -46,10 +38,6 @@ def test_resample(seed):
rshape
=
np
.
random
.
randint
(
5
,
50
,
3
)
resampled
,
xf
=
resample
.
resample
(
img
,
rshape
,
order
=
0
)
img
.
save
(
'base.nii.gz'
)
fslimage
.
Image
(
resampled
,
xform
=
xf
,
mmap
=
False
).
save
(
'res.nii.gz'
)
assert
tuple
(
resampled
.
shape
)
==
tuple
(
rshape
)
# We used nearest neighbour interp, so the
...
...
@@ -91,20 +79,11 @@ def test_resample(seed):
assert
np
.
all
(
np
.
isclose
(
resvals
,
origvals
))
del
img
img
=
None
def
test_resample_4d
(
seed
):
fname
=
'test.nii.gz'
with
tempdir
():
make_random_image
(
fname
,
(
10
,
10
,
10
,
10
))
# resample one volume
img
=
fslimage
.
Image
(
fname
)
img
=
fslimage
.
Image
(
make_random_image
(
dims
=
(
10
,
10
,
10
,
10
))
)
slc
=
(
slice
(
None
),
slice
(
None
),
slice
(
None
),
3
)
resampled
=
resample
.
resample
(
img
,
img
.
shape
[:
3
],
slc
)[
0
]
assert
np
.
all
(
resampled
==
img
[...,
3
])
...
...
@@ -131,18 +110,10 @@ def test_resample_4d(seed):
resampled
=
resample
.
resample
(
img
,
(
5
,
5
,
5
,
15
),
None
)[
0
]
assert
tuple
(
resampled
.
shape
)
==
(
5
,
5
,
5
,
15
)
del
img
del
resampled
img
=
None
resampled
=
None
def
test_resample_origin
(
seed
):
with
tempdir
()
as
td
:
fname
=
op
.
join
(
td
,
'test.nii'
)
make_random_image
(
fname
,
(
10
,
10
,
10
))
img
=
fslimage
.
Image
(
fname
)
img
=
fslimage
.
Image
(
make_random_image
(
dims
=
(
10
,
10
,
10
)))
# with origin='corner', image
# bounding boxes should match
...
...
@@ -183,8 +154,72 @@ def test_resample_origin(seed):
def
test_resampleToPixdims
():
pass
img
=
fslimage
.
Image
(
make_random_image
(
dims
=
(
10
,
10
,
10
)))
imglo
,
imghi
=
transform
.
axisBounds
(
img
.
shape
,
img
.
voxToWorldMat
)
oldpix
=
np
.
array
(
img
.
pixdim
,
dtype
=
np
.
float
)
oldshape
=
np
.
array
(
img
.
shape
,
dtype
=
np
.
float
)
for
i
,
origin
in
it
.
product
(
range
(
25
),
(
'centre'
,
'corner'
)):
# random pixdims in the range 0.1 - 5.0
newpix
=
0.1
+
4.9
*
np
.
random
.
random
(
3
)
expshape
=
np
.
round
(
oldshape
*
(
oldpix
/
newpix
))
res
=
resample
.
resampleToPixdims
(
img
,
newpix
,
origin
=
origin
)
res
=
fslimage
.
Image
(
res
[
0
],
xform
=
res
[
1
])
reslo
,
reshi
=
transform
.
axisBounds
(
res
.
shape
,
res
.
voxToWorldMat
)
resfov
=
reshi
-
reslo
expfov
=
newpix
*
res
.
shape
assert
np
.
all
(
np
.
isclose
(
res
.
shape
,
expshape
))
assert
np
.
all
(
np
.
isclose
(
res
.
pixdim
,
newpix
))
assert
np
.
all
(
np
.
isclose
(
resfov
,
expfov
,
rtol
=
1e-2
,
atol
=
1e-2
))
if
origin
==
'corner'
:
assert
np
.
all
(
np
.
isclose
(
imglo
,
reslo
))
assert
np
.
all
(
np
.
isclose
(
reshi
,
reslo
+
expfov
,
rtol
=
1e-2
,
atol
=
1e-2
))
def
test_resampleToReference
():
pass
def
random_v2w
():
return
transform
.
compose
(
0.25
+
4.75
*
np
.
random
.
random
(
3
),
-
50
+
100
*
np
.
random
.
random
(
3
),
-
np
.
pi
+
2
*
np
.
pi
*
np
.
random
.
random
(
3
))
# Basic test - output has same
# dimensions/space as reference
for
i
in
range
(
25
):
ishape
=
np
.
random
.
randint
(
5
,
50
,
3
)
rshape
=
np
.
random
.
randint
(
5
,
50
,
3
)
iv2w
=
random_v2w
()
rv2w
=
random_v2w
()
img
=
fslimage
.
Image
(
make_random_image
(
dims
=
ishape
,
xform
=
iv2w
))
ref
=
fslimage
.
Image
(
make_random_image
(
dims
=
rshape
,
xform
=
rv2w
))
res
=
resample
.
resampleToReference
(
img
,
ref
)
res
=
fslimage
.
Image
(
res
[
0
],
header
=
ref
.
header
)
assert
res
.
sameSpace
(
ref
)
# More specific test - output
# data gets transformed correctly
# into reference space
img
=
np
.
zeros
((
5
,
5
,
5
),
dtype
=
np
.
float
)
img
[
1
,
1
,
1
]
=
1
img
=
fslimage
.
Image
(
img
)
refv2w
=
transform
.
scaleOffsetXform
([
1
,
1
,
1
],
[
-
1
,
-
1
,
-
1
])
ref
=
np
.
zeros
((
5
,
5
,
5
),
dtype
=
np
.
float
)
ref
=
fslimage
.
Image
(
ref
,
xform
=
refv2w
)
res
=
resample
.
resampleToReference
(
img
,
ref
,
order
=
0
)
exp
=
np
.
zeros
((
5
,
5
,
5
),
dtype
=
np
.
float
)
exp
[
2
,
2
,
2
]
=
1
assert
np
.
all
(
np
.
isclose
(
res
[
0
],
exp
))
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment