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
d761cd91
Commit
d761cd91
authored
7 years ago
by
Paul McCarthy
Browse files
Options
Downloads
Patches
Plain Diff
Bugfix in _test_Image_changeData. All uses of mkdtemp replaced with fsl.utils.tempdir
parent
ebd4a8b5
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
tests/test_image.py
+175
-207
175 additions, 207 deletions
tests/test_image.py
with
175 additions
and
207 deletions
tests/test_image.py
+
175
−
207
View file @
d761cd91
...
@@ -10,8 +10,6 @@
...
@@ -10,8 +10,6 @@
import
os
import
os
import
os.path
as
op
import
os.path
as
op
import
itertools
as
it
import
itertools
as
it
import
tempfile
import
shutil
import
pytest
import
pytest
...
@@ -27,10 +25,10 @@ import fsl.data.imagewrapper as imagewrapper
...
@@ -27,10 +25,10 @@ import fsl.data.imagewrapper as imagewrapper
import
fsl.utils.path
as
fslpath
import
fsl.utils.path
as
fslpath
import
fsl.utils.transform
as
transform
import
fsl.utils.transform
as
transform
from
fsl.utils.tempdir
import
tempdir
from
.
import
make_random_image
from
.
import
make_random_image
from
.
import
make_dummy_file
from
.
import
make_dummy_file
from
.
import
testdir
from
.
import
tempdir
def
make_image
(
filename
=
None
,
def
make_image
(
filename
=
None
,
...
@@ -121,17 +119,17 @@ def test_load():
...
@@ -121,17 +119,17 @@ def test_load():
(
'
notnifti
'
,
ImageFileError
),
(
'
notnifti
'
,
ImageFileError
),
(
'
notnifti.nii.gz
'
,
ImageFileError
)]
(
'
notnifti.nii.gz
'
,
ImageFileError
)]
testdir
=
tempfile
.
mkdtemp
()
for
f
in
toCreate
:
with
tempdir
()
as
testdir
:
if
f
.
startswith
(
'
notnifti
'
):
for
f
in
toCreate
:
make_dummy_file
(
op
.
join
(
testdir
,
f
))
else
:
if
f
.
startswith
(
'
notnifti
'
):
make_random_image
(
op
.
join
(
testdir
,
f
))
make_dummy_file
(
op
.
join
(
testdir
,
f
))
else
:
make_random_image
(
op
.
join
(
testdir
,
f
))
# Not raising an error means the test passes
# Not raising an error means the test passes
try
:
for
fname
in
shouldPass
:
for
fname
in
shouldPass
:
fslimage
.
Image
(
op
.
join
(
testdir
,
fname
))
fslimage
.
Image
(
op
.
join
(
testdir
,
fname
))
...
@@ -139,8 +137,6 @@ def test_load():
...
@@ -139,8 +137,6 @@ def test_load():
for
fname
,
exc
in
shouldRaise
:
for
fname
,
exc
in
shouldRaise
:
with
pytest
.
raises
(
exc
):
with
pytest
.
raises
(
exc
):
fslimage
.
Image
(
op
.
join
(
testdir
,
fname
))
fslimage
.
Image
(
op
.
join
(
testdir
,
fname
))
finally
:
shutil
.
rmtree
(
testdir
)
def
test_create
():
def
test_create
():
...
@@ -180,8 +176,7 @@ def test_create():
...
@@ -180,8 +176,7 @@ def test_create():
assert
np
.
all
(
np
.
isclose
(
img
.
pixdim
,
(
2
,
3
,
4
)))
assert
np
.
all
(
np
.
isclose
(
img
.
pixdim
,
(
2
,
3
,
4
)))
for
imgtype
in
[
0
,
1
,
2
]:
for
imgtype
in
[
0
,
1
,
2
]:
testdir
=
tempfile
.
mkdtemp
()
with
tempdir
()
as
testdir
:
try
:
fname
=
op
.
join
(
testdir
,
'
myimage
'
)
fname
=
op
.
join
(
testdir
,
'
myimage
'
)
nimg
=
make_image
(
fname
,
imgtype
,
pixdims
=
(
2
,
3
,
4
))
nimg
=
make_image
(
fname
,
imgtype
,
pixdims
=
(
2
,
3
,
4
))
...
@@ -193,9 +188,6 @@ def test_create():
...
@@ -193,9 +188,6 @@ def test_create():
assert
img
.
niftiVersion
==
imgtype
assert
img
.
niftiVersion
==
imgtype
assert
np
.
all
(
np
.
isclose
(
img
.
pixdim
,
(
2
,
3
,
4
)))
assert
np
.
all
(
np
.
isclose
(
img
.
pixdim
,
(
2
,
3
,
4
)))
finally
:
shutil
.
rmtree
(
testdir
)
def
test_bad_create
():
def
test_bad_create
():
...
@@ -236,7 +228,6 @@ def test_Image_atts_nifti2(): _test_Image_atts(2)
...
@@ -236,7 +228,6 @@ def test_Image_atts_nifti2(): _test_Image_atts(2)
def
_test_Image_atts
(
imgtype
):
def
_test_Image_atts
(
imgtype
):
"""
Test that basic Nifti/Image attributes are correct.
"""
"""
Test that basic Nifti/Image attributes are correct.
"""
testdir
=
tempfile
.
mkdtemp
()
allowedExts
=
fslimage
.
ALLOWED_EXTENSIONS
allowedExts
=
fslimage
.
ALLOWED_EXTENSIONS
fileGroups
=
fslimage
.
FILE_GROUPS
fileGroups
=
fslimage
.
FILE_GROUPS
...
@@ -268,17 +259,17 @@ def _test_Image_atts(imgtype):
...
@@ -268,17 +259,17 @@ def _test_Image_atts(imgtype):
tests
=
list
(
tests
)
tests
=
list
(
tests
)
paths
=
[
'
test{:03d}
'
.
format
(
i
)
for
i
in
range
(
len
(
tests
))]
paths
=
[
'
test{:03d}
'
.
format
(
i
)
for
i
in
range
(
len
(
tests
))]
for
path
,
atts
in
zip
(
paths
,
test
s
)
:
with
tempdir
()
as
test
dir
:
dims
,
pixdims
,
dtype
=
atts
for
path
,
atts
in
zip
(
paths
,
tests
):
ndims
=
len
(
dims
)
dims
,
pixdims
,
dtype
=
atts
pixdims
=
pixdims
[:
ndims
]
path
=
op
.
abspath
(
op
.
join
(
testdir
,
path
)
)
ndims
=
len
(
dims
)
make_image
(
path
,
imgtype
,
dims
,
pixdims
,
dtype
)
pix
dims
=
pixdims
[:
ndims
]
try
:
path
=
op
.
abspath
(
op
.
join
(
testdir
,
path
))
make_image
(
path
,
imgtype
,
dims
,
pixdims
,
dtype
)
for
path
,
atts
in
zip
(
paths
,
tests
):
for
path
,
atts
in
zip
(
paths
,
tests
):
...
@@ -305,8 +296,7 @@ def _test_Image_atts(imgtype):
...
@@ -305,8 +296,7 @@ def _test_Image_atts(imgtype):
allowedExts
=
allowedExts
,
allowedExts
=
allowedExts
,
mustExist
=
True
,
mustExist
=
True
,
fileGroups
=
fileGroups
)
fileGroups
=
fileGroups
)
finally
:
shutil
.
rmtree
(
testdir
)
def
test_Image_atts2_analyze
():
_test_Image_atts2
(
0
)
def
test_Image_atts2_analyze
():
_test_Image_atts2
(
0
)
def
test_Image_atts2_nifti1
():
_test_Image_atts2
(
1
)
def
test_Image_atts2_nifti1
():
_test_Image_atts2
(
1
)
...
@@ -390,7 +380,6 @@ def test_addExt():
...
@@ -390,7 +380,6 @@ def test_addExt():
"""
Test the addExt function.
"""
"""
Test the addExt function.
"""
default
=
fslimage
.
defaultExt
()
default
=
fslimage
.
defaultExt
()
testdir
=
tempfile
.
mkdtemp
()
toCreate
=
[
toCreate
=
[
'
compressed.nii.gz
'
,
'
compressed.nii.gz
'
,
...
@@ -429,11 +418,13 @@ def test_addExt():
...
@@ -429,11 +418,13 @@ def test_addExt():
(
'
ambiguous.img.gz
'
,
True
,
'
ambiguous.img.gz
'
),
(
'
ambiguous.img.gz
'
,
True
,
'
ambiguous.img.gz
'
),
(
'
ambiguous.hdr.gz
'
,
True
,
'
ambiguous.hdr.gz
'
)]
(
'
ambiguous.hdr.gz
'
,
True
,
'
ambiguous.hdr.gz
'
)]
for
path
in
toCreate
:
path
=
op
.
abspath
(
op
.
join
(
testdir
,
path
))
make_random_image
(
path
)
try
:
with
tempdir
()
as
testdir
:
for
path
in
toCreate
:
path
=
op
.
abspath
(
op
.
join
(
testdir
,
path
))
make_random_image
(
path
)
for
path
,
mustExist
,
expected
in
tests
:
for
path
,
mustExist
,
expected
in
tests
:
path
=
op
.
abspath
(
op
.
join
(
testdir
,
path
))
path
=
op
.
abspath
(
op
.
join
(
testdir
,
path
))
...
@@ -445,8 +436,6 @@ def test_addExt():
...
@@ -445,8 +436,6 @@ def test_addExt():
with
pytest
.
raises
(
fslimage
.
PathError
):
with
pytest
.
raises
(
fslimage
.
PathError
):
path
=
op
.
join
(
testdir
,
'
ambiguous
'
)
path
=
op
.
join
(
testdir
,
'
ambiguous
'
)
fslimage
.
addExt
(
path
,
mustExist
=
True
)
fslimage
.
addExt
(
path
,
mustExist
=
True
)
finally
:
shutil
.
rmtree
(
testdir
)
def
test_removeExt
():
def
test_removeExt
():
...
@@ -506,45 +495,43 @@ def test_Image_orientation_nifti2_radio(): _test_Image_orientation(2, 'radio')
...
@@ -506,45 +495,43 @@ def test_Image_orientation_nifti2_radio(): _test_Image_orientation(2, 'radio')
def
_test_Image_orientation
(
imgtype
,
voxorient
):
def
_test_Image_orientation
(
imgtype
,
voxorient
):
"""
Test the Nifti.isNeurological and Nifti.getOrientation methods.
"""
"""
Test the Nifti.isNeurological and Nifti.getOrientation methods.
"""
testdir
=
tempfile
.
mkdtemp
()
with
tempdir
()
as
testdir
:
imagefile
=
op
.
join
(
testdir
,
'
image
'
)
imagefile
=
op
.
join
(
testdir
,
'
image
'
)
# an image with RAS voxel storage order
# an image with RAS voxel storage order
# (affine has a positive determinant)
# (affine has a positive determinant)
# is said to be "neurological", whereas
# is said to be "neurological", whereas
# an image with LAS voxel storage order
# an image with LAS voxel storage order
# (negative determinant - x axis must
# (negative determinant - x axis must
# be flipped to bring it into RAS nifti
# be flipped to bring it into RAS nifti
# world coordinates)) is said to be
# world coordinates)) is said to be
# "radiological". The make_image function
# "radiological". The make_image function
# forms the affine from these pixdims.
# forms the affine from these pixdims.
if
voxorient
==
'
neuro
'
:
pixdims
=
(
1
,
1
,
1
)
if
voxorient
==
'
neuro
'
:
pixdims
=
(
1
,
1
,
1
)
elif
voxorient
==
'
radio
'
:
pixdims
=
(
-
1
,
1
,
1
)
elif
voxorient
==
'
radio
'
:
pixdims
=
(
-
1
,
1
,
1
)
make_image
(
imagefile
,
imgtype
,
(
10
,
10
,
10
),
pixdims
,
np
.
float32
)
make_image
(
imagefile
,
imgtype
,
(
10
,
10
,
10
),
pixdims
,
np
.
float32
)
image
=
fslimage
.
Image
(
imagefile
)
image
=
fslimage
.
Image
(
imagefile
)
# analyze images are always assumed to be
# analyze images are always assumed to be
# stored in radiological (LAS) orientation
# stored in radiological (LAS) orientation
if
imgtype
==
0
:
if
imgtype
==
0
:
expectNeuroTest
=
False
expectNeuroTest
=
False
expectvox0Orientation
=
constants
.
ORIENT_R2L
expectvox0Orientation
=
constants
.
ORIENT_R2L
expectvox1Orientation
=
constants
.
ORIENT_P2A
expectvox1Orientation
=
constants
.
ORIENT_P2A
expectvox2Orientation
=
constants
.
ORIENT_I2S
expectvox2Orientation
=
constants
.
ORIENT_I2S
elif
voxorient
==
'
neuro
'
:
elif
voxorient
==
'
neuro
'
:
expectNeuroTest
=
True
expectNeuroTest
=
True
expectvox0Orientation
=
constants
.
ORIENT_L2R
expectvox0Orientation
=
constants
.
ORIENT_L2R
expectvox1Orientation
=
constants
.
ORIENT_P2A
expectvox1Orientation
=
constants
.
ORIENT_P2A
expectvox2Orientation
=
constants
.
ORIENT_I2S
expectvox2Orientation
=
constants
.
ORIENT_I2S
else
:
else
:
expectNeuroTest
=
False
expectNeuroTest
=
False
expectvox0Orientation
=
constants
.
ORIENT_R2L
expectvox0Orientation
=
constants
.
ORIENT_R2L
expectvox1Orientation
=
constants
.
ORIENT_P2A
expectvox1Orientation
=
constants
.
ORIENT_P2A
expectvox2Orientation
=
constants
.
ORIENT_I2S
expectvox2Orientation
=
constants
.
ORIENT_I2S
try
:
assert
image
.
isNeurological
()
==
expectNeuroTest
assert
image
.
isNeurological
()
==
expectNeuroTest
...
@@ -562,9 +549,6 @@ def _test_Image_orientation(imgtype, voxorient):
...
@@ -562,9 +549,6 @@ def _test_Image_orientation(imgtype, voxorient):
assert
image
.
getOrientation
(
1
,
affine
)
==
expectvox1Orientation
assert
image
.
getOrientation
(
1
,
affine
)
==
expectvox1Orientation
assert
image
.
getOrientation
(
2
,
affine
)
==
expectvox2Orientation
assert
image
.
getOrientation
(
2
,
affine
)
==
expectvox2Orientation
finally
:
shutil
.
rmtree
(
testdir
)
def
test_Image_sqforms_nifti1_normal
():
_test_Image_sqforms
(
1
,
1
,
1
)
def
test_Image_sqforms_nifti1_normal
():
_test_Image_sqforms
(
1
,
1
,
1
)
def
test_Image_sqforms_nifti1_nosform
():
_test_Image_sqforms
(
1
,
0
,
1
)
def
test_Image_sqforms_nifti1_nosform
():
_test_Image_sqforms
(
1
,
0
,
1
)
...
@@ -579,55 +563,54 @@ def _test_Image_sqforms(imgtype, sformcode, qformcode):
...
@@ -579,55 +563,54 @@ def _test_Image_sqforms(imgtype, sformcode, qformcode):
attributes for NIFTI images with the given sform/qform code combination.
attributes for NIFTI images with the given sform/qform code combination.
"""
"""
testdir
=
tempfile
.
mkdtemp
()
with
tempdir
()
as
testdir
:
imagefile
=
op
.
abspath
(
op
.
join
(
testdir
,
'
image.nii.gz
'
))
imagefile
=
op
.
abspath
(
op
.
join
(
testdir
,
'
image.nii.gz
'
))
# For an image with no s/q form, we expect the
# For an image with no s/q form, we expect the
# fallback affine - a simple scaling matrix.
# fallback affine - a simple scaling matrix.
# We add some offsets to the actual affine so
# We add some offsets to the actual affine so
# we can distinguish it from the fallback affine.
# we can distinguish it from the fallback affine.
scaleMat
=
np
.
diag
([
2
,
2
,
2
,
1
])
scaleMat
=
np
.
diag
([
2
,
2
,
2
,
1
])
invScaleMat
=
np
.
diag
([
0.5
,
0.5
,
0.5
,
1
])
invScaleMat
=
np
.
diag
([
0.5
,
0.5
,
0.5
,
1
])
affine
=
np
.
array
(
scaleMat
)
affine
=
np
.
array
(
scaleMat
)
affine
[:
3
,
3
]
=
[
25
,
20
,
20
]
affine
[:
3
,
3
]
=
[
25
,
20
,
20
]
invAffine
=
npla
.
inv
(
affine
)
invAffine
=
npla
.
inv
(
affine
)
image
=
make_image
(
imagefile
,
imgtype
,
(
10
,
10
,
10
),
(
2
,
2
,
2
),
np
.
float32
)
image
=
make_image
(
imagefile
,
imgtype
,
(
10
,
10
,
10
),
(
2
,
2
,
2
),
np
.
float32
)
image
.
set_sform
(
affine
,
sformcode
)
image
.
set_sform
(
affine
,
sformcode
)
image
.
set_qform
(
affine
,
qformcode
)
image
.
set_qform
(
affine
,
qformcode
)
image
.
update_header
()
image
.
update_header
()
nib
.
save
(
image
,
imagefile
)
nib
.
save
(
image
,
imagefile
)
# No s or qform - we expect the fallback affine
# No s or qform - we expect the fallback affine
if
sformcode
==
0
and
qformcode
==
0
:
if
sformcode
==
0
and
qformcode
==
0
:
expAffine
=
scaleMat
expAffine
=
scaleMat
invExpAffine
=
invScaleMat
invExpAffine
=
invScaleMat
expCode
=
constants
.
NIFTI_XFORM_UNKNOWN
expCode
=
constants
.
NIFTI_XFORM_UNKNOWN
expOrient
=
constants
.
ORIENT_UNKNOWN
expOrient
=
constants
.
ORIENT_UNKNOWN
# No sform, but valid qform - expect the affine
# No sform, but valid qform - expect the affine
elif
sformcode
==
0
and
qformcode
>
0
:
elif
sformcode
==
0
and
qformcode
>
0
:
expAffine
=
affine
expAffine
=
affine
invExpAffine
=
invAffine
invExpAffine
=
invAffine
expCode
=
qformcode
expCode
=
qformcode
expOrient
=
constants
.
ORIENT_L2R
expOrient
=
constants
.
ORIENT_L2R
# Valid sform (qform irrelevant) - expect the affine
# Valid sform (qform irrelevant) - expect the affine
elif
sformcode
>
0
:
elif
sformcode
>
0
:
expAffine
=
affine
expAffine
=
affine
invExpAffine
=
invAffine
invExpAffine
=
invAffine
expCode
=
sformcode
expCode
=
sformcode
expOrient
=
constants
.
ORIENT_L2R
expOrient
=
constants
.
ORIENT_L2R
image
=
fslimage
.
Image
(
imagefile
)
image
=
fslimage
.
Image
(
imagefile
)
with
pytest
.
raises
(
ValueError
):
with
pytest
.
raises
(
ValueError
):
image
.
getXFormCode
(
'
badcode
'
)
image
.
getXFormCode
(
'
badcode
'
)
try
:
assert
np
.
all
(
np
.
isclose
(
image
.
voxToWorldMat
,
expAffine
))
assert
np
.
all
(
np
.
isclose
(
image
.
voxToWorldMat
,
expAffine
))
assert
np
.
all
(
np
.
isclose
(
image
.
worldToVoxMat
,
invExpAffine
))
assert
np
.
all
(
np
.
isclose
(
image
.
worldToVoxMat
,
invExpAffine
))
...
@@ -636,8 +619,6 @@ def _test_Image_sqforms(imgtype, sformcode, qformcode):
...
@@ -636,8 +619,6 @@ def _test_Image_sqforms(imgtype, sformcode, qformcode):
assert
image
.
getXFormCode
(
'
qform
'
)
==
qformcode
assert
image
.
getXFormCode
(
'
qform
'
)
==
qformcode
assert
image
.
getOrientation
(
0
,
image
.
voxToWorldMat
)
==
expOrient
assert
image
.
getOrientation
(
0
,
image
.
voxToWorldMat
)
==
expOrient
finally
:
shutil
.
rmtree
(
testdir
)
def
test_Image_changeXform_analyze
():
_test_Image_changeXform
(
0
)
def
test_Image_changeXform_analyze
():
_test_Image_changeXform
(
0
)
...
@@ -647,47 +628,45 @@ def test_Image_changeXform_nifti2(): _test_Image_changeXform(2)
...
@@ -647,47 +628,45 @@ def test_Image_changeXform_nifti2(): _test_Image_changeXform(2)
def
_test_Image_changeXform
(
imgtype
,
sformcode
=
None
,
qformcode
=
None
):
def
_test_Image_changeXform
(
imgtype
,
sformcode
=
None
,
qformcode
=
None
):
"""
Test changing the Nifti.voxToWorldMat attribute.
"""
"""
Test changing the Nifti.voxToWorldMat attribute.
"""
testdir
=
tempfile
.
mkdtemp
()
with
tempdir
()
as
testdir
:
imagefile
=
op
.
join
(
testdir
,
'
image
'
)
imagefile
=
op
.
join
(
testdir
,
'
image
'
)
image
=
make_image
(
imagefile
,
imgtype
)
image
=
make_image
(
imagefile
,
imgtype
)
if
imgtype
>
0
:
if
imgtype
>
0
:
if
sformcode
is
not
None
:
image
.
set_sform
(
image
.
affine
,
sformcode
)
if
qformcode
is
not
None
:
image
.
set_qform
(
image
.
affine
,
qformcode
)
image
.
update_header
()
nib
.
save
(
image
,
imagefile
)
notified
=
{}
if
sformcode
is
not
None
:
image
.
set_sform
(
image
.
affine
,
sformcode
)
if
qformcode
is
not
None
:
image
.
set_qform
(
image
.
affine
,
qformcode
)
image
.
update_header
()
nib
.
save
(
image
,
imagefile
)
def
onXform
(
*
a
):
notified
=
{}
notified
[
'
xform
'
]
=
True
def
on
Save
(
*
a
):
def
on
Xform
(
*
a
):
notified
[
'
save
'
]
=
True
notified
[
'
xform
'
]
=
True
img
=
fslimage
.
Image
(
imagefile
)
def
onSave
(
*
a
):
notified
[
'
save
'
]
=
True
img
.
register
(
'
name1
'
,
onXform
,
'
transform
'
)
img
=
fslimage
.
Image
(
imagefile
)
img
.
register
(
'
name2
'
,
onSave
,
'
saveState
'
)
newXform
=
np
.
array
([[
5
,
0
,
0
,
10
],
img
.
register
(
'
name1
'
,
onXform
,
'
transform
'
)
[
0
,
2
,
0
,
23
],
img
.
register
(
'
name2
'
,
onSave
,
'
saveState
'
)
[
0
,
0
,
14
,
5
],
[
0
,
0
,
0
,
1
]])
if
imgtype
>
0
:
newXform
=
np
.
array
([[
5
,
0
,
0
,
10
],
expSformCode
=
image
.
get_sform
(
coded
=
True
)[
1
]
[
0
,
2
,
0
,
23
],
expQformCode
=
image
.
get_qform
(
coded
=
True
)[
1
]
[
0
,
0
,
14
,
5
],
[
0
,
0
,
0
,
1
]])
if
sformcode
==
0
:
if
imgtype
>
0
:
expSformCode
=
constants
.
NIFTI_XFORM_ALIGNED_ANAT
expSformCode
=
image
.
get_sform
(
coded
=
True
)[
1
]
else
:
expQformCode
=
image
.
get_qform
(
coded
=
True
)[
1
]
expSformCode
=
constants
.
NIFTI_XFORM_ANALYZE
expQformCode
=
constants
.
NIFTI_XFORM_ANALYZE
try
:
if
sformcode
==
0
:
expSformCode
=
constants
.
NIFTI_XFORM_ALIGNED_ANAT
else
:
expSformCode
=
constants
.
NIFTI_XFORM_ANALYZE
expQformCode
=
constants
.
NIFTI_XFORM_ANALYZE
# Image state should initially be saved
# Image state should initially be saved
assert
img
.
saveState
assert
img
.
saveState
...
@@ -712,8 +691,6 @@ def _test_Image_changeXform(imgtype, sformcode=None, qformcode=None):
...
@@ -712,8 +691,6 @@ def _test_Image_changeXform(imgtype, sformcode=None, qformcode=None):
assert
np
.
all
(
np
.
isclose
(
img
.
worldToVoxMat
,
invx
))
assert
np
.
all
(
np
.
isclose
(
img
.
worldToVoxMat
,
invx
))
assert
img
.
getXFormCode
(
'
sform
'
)
==
expSformCode
assert
img
.
getXFormCode
(
'
sform
'
)
==
expSformCode
assert
img
.
getXFormCode
(
'
qform
'
)
==
expQformCode
assert
img
.
getXFormCode
(
'
qform
'
)
==
expQformCode
finally
:
shutil
.
rmtree
(
testdir
)
def
test_Image_changeData_analyze
(
seed
):
_test_Image_changeData
(
0
)
def
test_Image_changeData_analyze
(
seed
):
_test_Image_changeData
(
0
)
...
@@ -724,48 +701,52 @@ def _test_Image_changeData(imgtype):
...
@@ -724,48 +701,52 @@ def _test_Image_changeData(imgtype):
the dataRange attribute to be updated.
the dataRange attribute to be updated.
"""
"""
testdir
=
tempfile
.
mkdtemp
()
with
tempdir
()
as
testdir
:
imagefile
=
op
.
join
(
testdir
,
'
image
'
)
imagefile
=
op
.
join
(
testdir
,
'
image
'
)
make_image
(
imagefile
,
imgtype
)
img
=
fslimage
.
I
mage
(
imagefile
)
make_i
mage
(
imagefile
,
imgtype
)
notified
=
{}
img
=
fslimage
.
Image
(
imagefile
)
def
randvox
():
notified
=
{}
return
(
np
.
random
.
randint
(
0
,
img
.
shape
[
0
]),
np
.
random
.
randint
(
0
,
img
.
shape
[
1
]),
np
.
random
.
randint
(
0
,
img
.
shape
[
2
]))
def
onData
(
*
a
):
def
randvox
():
notified
[
'
data
'
]
=
True
return
(
np
.
random
.
randint
(
0
,
img
.
shape
[
0
]),
np
.
random
.
randint
(
0
,
img
.
shape
[
1
]),
np
.
random
.
randint
(
0
,
img
.
shape
[
2
]))
def
on
SaveState
(
*
a
):
def
on
Data
(
*
a
):
notified
[
'
save
'
]
=
True
notified
[
'
data
'
]
=
True
def
on
DataRang
e
(
*
a
):
def
on
SaveStat
e
(
*
a
):
notified
[
'
dataRang
e
'
]
=
True
notified
[
'
sav
e
'
]
=
True
img
.
register
(
'
name1
'
,
onData
,
'
data
'
)
def
onDataRange
(
*
a
):
img
.
register
(
'
name2
'
,
onSaveState
,
'
saveState
'
)
notified
[
'
dataRange
'
]
=
True
img
.
register
(
'
name3
'
,
onDataRange
,
'
dataRange
'
)
# Calculate the actual data range
img
.
register
(
'
name1
'
,
onData
,
'
data
'
)
data
=
img
.
nibImage
.
get_data
()
img
.
register
(
'
name2
'
,
onSaveState
,
'
saveState
'
)
dmin
=
data
.
min
()
img
.
register
(
'
name3
'
,
onDataRange
,
'
dataRange
'
)
dmax
=
data
.
max
()
drange
=
dmax
-
dmin
try
:
# Calculate the actual data range
data
=
img
.
nibImage
.
get_data
()
dmin
=
data
.
min
()
dmax
=
data
.
max
()
drange
=
dmax
-
dmin
assert
img
.
saveState
assert
img
.
saveState
assert
np
.
all
(
np
.
isclose
(
img
.
dataRange
,
(
dmin
,
dmax
)))
assert
np
.
all
(
np
.
isclose
(
img
.
dataRange
,
(
dmin
,
dmax
)))
# random value within the existing data range
# random value within the existing data range,
randval
=
dmin
+
np
.
random
.
random
()
*
drange
# making sure not to overwite the min or max
rx
,
ry
,
rz
=
randvox
()
randval
=
dmin
+
np
.
random
.
random
()
*
drange
img
[
rx
,
ry
,
rz
]
=
randval
while
True
:
rx
,
ry
,
rz
=
randvox
()
if
not
(
np
.
isclose
(
img
[
rx
,
ry
,
rz
],
dmin
)
or
np
.
isclose
(
img
[
rx
,
ry
,
rz
],
dmax
)):
img
[
rx
,
ry
,
rz
]
=
randval
break
assert
np
.
isclose
(
img
[
rx
,
ry
,
rz
],
randval
)
assert
np
.
isclose
(
img
[
rx
,
ry
,
rz
],
randval
)
assert
notified
.
get
(
'
data
'
,
False
)
assert
notified
.
get
(
'
data
'
,
False
)
...
@@ -808,17 +789,12 @@ def _test_Image_changeData(imgtype):
...
@@ -808,17 +789,12 @@ def _test_Image_changeData(imgtype):
assert
np
.
isclose
(
img
[
maxx
,
maxy
,
maxz
],
newdmax
)
assert
np
.
isclose
(
img
[
maxx
,
maxy
,
maxz
],
newdmax
)
assert
np
.
all
(
np
.
isclose
(
img
.
dataRange
,
(
newdmin
,
newdmax
)))
assert
np
.
all
(
np
.
isclose
(
img
.
dataRange
,
(
newdmin
,
newdmax
)))
finally
:
shutil
.
rmtree
(
testdir
)
def
test_Image_2D_analyze
():
_test_Image_2D
(
0
)
def
test_Image_2D_analyze
():
_test_Image_2D
(
0
)
def
test_Image_2D_nifti1
():
_test_Image_2D
(
1
)
def
test_Image_2D_nifti1
():
_test_Image_2D
(
1
)
def
test_Image_2D_nifti2
():
_test_Image_2D
(
2
)
def
test_Image_2D_nifti2
():
_test_Image_2D
(
2
)
def
_test_Image_2D
(
imgtype
):
def
_test_Image_2D
(
imgtype
):
testdir
=
tempfile
.
mkdtemp
()
# The first shape tests when the
# The first shape tests when the
# nifti dim0 field is set to 2,
# nifti dim0 field is set to 2,
# which happens when you create
# which happens when you create
...
@@ -833,7 +809,7 @@ def _test_Image_2D(imgtype):
...
@@ -833,7 +809,7 @@ def _test_Image_2D(imgtype):
(
10
,
1
,
20
,
5
),
(
10
,
1
,
20
,
5
),
(
1
,
10
,
20
,
5
)]
(
1
,
10
,
20
,
5
)]
try
:
with
tempdir
()
as
testdir
:
for
shape
in
testdims
:
for
shape
in
testdims
:
...
@@ -858,9 +834,6 @@ def _test_Image_2D(imgtype):
...
@@ -858,9 +834,6 @@ def _test_Image_2D(imgtype):
assert
tuple
(
map
(
float
,
shape
))
==
tuple
(
map
(
float
,
image
[:].
shape
))
assert
tuple
(
map
(
float
,
shape
))
==
tuple
(
map
(
float
,
image
[:].
shape
))
assert
tuple
(
map
(
float
,
pixdim
))
==
tuple
(
map
(
float
,
image
.
pixdim
))
assert
tuple
(
map
(
float
,
pixdim
))
==
tuple
(
map
(
float
,
image
.
pixdim
))
finally
:
shutil
.
rmtree
(
testdir
)
def
test_Image_5D_analyze
():
_test_Image_5D
(
0
)
def
test_Image_5D_analyze
():
_test_Image_5D
(
0
)
def
test_Image_5D_nifti1
():
_test_Image_5D
(
1
)
def
test_Image_5D_nifti1
():
_test_Image_5D
(
1
)
...
@@ -878,7 +851,7 @@ def _test_Image_5D(imgtype):
...
@@ -878,7 +851,7 @@ def _test_Image_5D(imgtype):
for
dims
in
testdims
:
for
dims
in
testdims
:
with
te
st
dir
()
as
td
:
with
te
mp
dir
()
as
td
:
path
=
op
.
join
(
td
,
'
test.nii
'
)
path
=
op
.
join
(
td
,
'
test.nii
'
)
...
@@ -970,21 +943,20 @@ def _test_Image_save(imgtype):
...
@@ -970,21 +943,20 @@ def _test_Image_save(imgtype):
return
rvoxes
return
rvoxes
testdir
=
tempfile
.
mkdtemp
()
with
tempdir
()
as
testdir
:
if
imgtype
==
0
:
if
imgtype
==
0
:
filename
=
op
.
join
(
testdir
,
'
blob.img
'
)
filename
=
op
.
join
(
testdir
,
'
blob.img
'
)
filename2
=
op
.
join
(
testdir
,
'
blob_copy.img
'
)
filename2
=
op
.
join
(
testdir
,
'
blob_copy.img
'
)
else
:
else
:
filename
=
op
.
join
(
testdir
,
'
blob.nii
'
)
filename
=
op
.
join
(
testdir
,
'
blob.nii
'
)
filename2
=
op
.
join
(
testdir
,
'
blob_copy.nii
'
)
filename2
=
op
.
join
(
testdir
,
'
blob_copy.nii
'
)
xform
=
np
.
eye
(
4
)
xform
=
np
.
eye
(
4
)
xform
[:
3
,
3
]
=
[
-
10
,
20
,
30
]
xform
[:
3
,
3
]
=
[
-
10
,
20
,
30
]
xform
[
0
,
0
]
=
33
xform
[
0
,
0
]
=
33
xform
[
1
,
1
]
=
55
xform
[
1
,
1
]
=
55
xform
[
2
,
2
]
=
38
xform
[
2
,
2
]
=
38
try
:
make_image
(
filename
,
imgtype
)
make_image
(
filename
,
imgtype
)
# Using mmap can cause a "Bus error"
# Using mmap can cause a "Bus error"
...
@@ -1033,13 +1005,9 @@ def _test_Image_save(imgtype):
...
@@ -1033,13 +1005,9 @@ def _test_Image_save(imgtype):
assert
np
.
isclose
(
img
[
x
,
y
,
z
],
v
)
assert
np
.
isclose
(
img
[
x
,
y
,
z
],
v
)
finally
:
shutil
.
rmtree
(
testdir
)
def
test_image_resample
(
seed
):
def
test_image_resample
(
seed
):
with
te
st
dir
()
as
td
:
with
te
mp
dir
()
as
td
:
fname
=
op
.
join
(
td
,
'
test.nii
'
)
fname
=
op
.
join
(
td
,
'
test.nii
'
)
...
...
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