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
a6f32494
Commit
a6f32494
authored
7 years ago
by
Paul McCarthy
Browse files
Options
Downloads
Patches
Plain Diff
Avoid random 'Bus error' when using mmap in docker container
parent
561e33e1
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
+47
-45
47 additions, 45 deletions
tests/test_image.py
with
47 additions
and
45 deletions
tests/test_image.py
+
47
−
45
View file @
a6f32494
...
...
@@ -56,7 +56,7 @@ def make_image(filename=None,
xform
=
np
.
eye
(
4
)
for
i
,
p
in
enumerate
(
pixdims
):
xform
[
i
,
i
]
=
p
data
=
np
.
array
(
np
.
random
.
random
(
dims
)
*
100
,
dtype
=
dtype
)
if
imgtype
==
0
:
img
=
nib
.
AnalyzeImage
(
data
,
xform
,
hdr
)
...
...
@@ -68,7 +68,7 @@ def make_image(filename=None,
if
op
.
splitext
(
filename
)[
1
]
==
''
:
if
imgtype
==
0
:
filename
=
'
{}.img
'
.
format
(
filename
)
else
:
filename
=
'
{}.nii
'
.
format
(
filename
)
nib
.
save
(
img
,
filename
)
return
img
...
...
@@ -95,7 +95,7 @@ def test_load():
'
ambiguous.img
'
,
'
ambiguous.img.gz
'
,
'
notnifti.nii.gz
'
]
shouldPass
=
[
'
compressed
'
,
'
compressed.nii.gz
'
,
...
...
@@ -121,12 +121,12 @@ def test_load():
testdir
=
tempfile
.
mkdtemp
()
for
f
in
toCreate
:
if
f
.
startswith
(
'
notnifti
'
):
make_dummy_file
(
op
.
join
(
testdir
,
f
))
else
:
make_random_image
(
op
.
join
(
testdir
,
f
))
# Not raising an error means the test passes
try
:
for
fname
in
shouldPass
:
...
...
@@ -164,7 +164,7 @@ def test_create():
assert
img
.
niftiVersion
==
1
for
imgType
in
[
0
,
1
,
2
]:
nimg
=
make_image
(
imgtype
=
imgType
,
pixdims
=
(
5
,
6
,
7
))
nhdr
=
nimg
.
header
...
...
@@ -189,13 +189,13 @@ def test_create():
img
=
fslimage
.
Image
(
nimg
)
assert
img
.
niftiVersion
==
imgtype
assert
np
.
all
(
np
.
isclose
(
img
.
pixdim
,
(
2
,
3
,
4
)))
finally
:
shutil
.
rmtree
(
testdir
)
def
test_bad_create
():
class
BadThing
(
object
):
pass
...
...
@@ -204,7 +204,7 @@ def test_bad_create():
fslimage
.
Image
(
np
.
random
.
random
((
10
,
10
,
10
)),
header
=
BadThing
())
# Bad data
with
pytest
.
raises
(
Exception
):
fslimage
.
Image
(
BadThing
())
...
...
@@ -221,18 +221,18 @@ def test_bad_create():
with
pytest
.
raises
(
Exception
):
fslimage
.
Image
(
np
.
random
.
random
(
10
,
10
,
10
),
xform
=
np
.
eye
(
3
))
with
pytest
.
raises
(
Exception
):
fslimage
.
Image
(
np
.
random
.
random
(
10
,
10
,
10
),
xform
=
np
.
eye
(
5
))
def
test_Image_atts_analyze
():
_test_Image_atts
(
0
)
def
test_Image_atts_analyze
():
_test_Image_atts
(
0
)
def
test_Image_atts_nifti1
():
_test_Image_atts
(
1
)
def
test_Image_atts_nifti2
():
_test_Image_atts
(
2
)
def
_test_Image_atts
(
imgtype
):
"""
Test that basic Nifti/Image attributes are correct.
"""
testdir
=
tempfile
.
mkdtemp
()
allowedExts
=
fslimage
.
ALLOWED_EXTENSIONS
fileGroups
=
fslimage
.
FILE_GROUPS
...
...
@@ -259,17 +259,17 @@ def _test_Image_atts(imgtype):
tests
=
it
.
product
(
dims
,
pixdims
,
dtypes
)
tests
=
list
(
tests
)
paths
=
[
'
test{:03d}
'
.
format
(
i
)
for
i
in
range
(
len
(
tests
))]
for
path
,
atts
in
zip
(
paths
,
tests
):
dims
,
pixdims
,
dtype
=
atts
ndims
=
len
(
dims
)
pixdims
=
pixdims
[:
ndims
]
pixdims
=
pixdims
[:
ndims
]
path
=
op
.
abspath
(
op
.
join
(
testdir
,
path
))
make_image
(
path
,
imgtype
,
dims
,
pixdims
,
dtype
)
try
:
for
path
,
atts
in
zip
(
paths
,
tests
):
...
...
@@ -295,12 +295,12 @@ def _test_Image_atts(imgtype):
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_nifti2
():
_test_Image_atts2
(
2
)
def
_test_Image_atts2
(
imgtype
):
# See fsl.utils.constants for the meanings of these codes
xyzUnits
=
[
0
,
1
,
2
,
3
]
timeUnits
=
[
8
,
16
,
24
,
32
,
40
,
48
]
...
...
@@ -392,7 +392,7 @@ def test_addExt():
for
path
in
toCreate
:
path
=
op
.
abspath
(
op
.
join
(
testdir
,
path
))
make_random_image
(
path
)
make_random_image
(
path
)
try
:
for
path
,
mustExist
,
expected
in
tests
:
...
...
@@ -456,14 +456,14 @@ def test_defaultExt():
os
.
environ
[
'
FSLOUTPUTTYPE
'
]
=
o
assert
fslimage
.
defaultExt
()
==
e
def
test_Image_orientation_analyze_neuro
():
_test_Image_orientation
(
0
,
'
neuro
'
)
def
test_Image_orientation_analyze_radio
():
_test_Image_orientation
(
0
,
'
radio
'
)
def
test_Image_orientation_nifti1_neuro
():
_test_Image_orientation
(
1
,
'
neuro
'
)
def
test_Image_orientation_nifti1_radio
():
_test_Image_orientation
(
1
,
'
radio
'
)
def
test_Image_orientation_nifti2_neuro
():
_test_Image_orientation
(
2
,
'
neuro
'
)
def
test_Image_orientation_nifti2_radio
():
_test_Image_orientation
(
2
,
'
radio
'
)
def
test_Image_orientation_nifti2_radio
():
_test_Image_orientation
(
2
,
'
radio
'
)
def
_test_Image_orientation
(
imgtype
,
voxorient
):
"""
Test the Nifti.isNeurological and Nifti.getOrientation methods.
"""
...
...
@@ -493,7 +493,7 @@ def _test_Image_orientation(imgtype, voxorient):
expectvox0Orientation
=
constants
.
ORIENT_R2L
expectvox1Orientation
=
constants
.
ORIENT_P2A
expectvox2Orientation
=
constants
.
ORIENT_I2S
elif
voxorient
==
'
neuro
'
:
expectNeuroTest
=
True
expectvox0Orientation
=
constants
.
ORIENT_L2R
...
...
@@ -534,7 +534,7 @@ def test_Image_sqforms_nifti1_nosqform(): _test_Image_sqforms(1, 1, 0)
def
test_Image_sqforms_nifti2_normal
():
_test_Image_sqforms
(
2
,
1
,
1
)
def
test_Image_sqforms_nifti2_nosform
():
_test_Image_sqforms
(
2
,
0
,
1
)
def
test_Image_sqforms_nifti2_noqform
():
_test_Image_sqforms
(
2
,
1
,
0
)
def
test_Image_sqforms_nifti2_nosqform
():
_test_Image_sqforms
(
2
,
0
,
0
)
def
test_Image_sqforms_nifti2_nosqform
():
_test_Image_sqforms
(
2
,
0
,
0
)
def
_test_Image_sqforms
(
imgtype
,
sformcode
,
qformcode
):
"""
Test the Nifti.getXFormCode method, and the voxToWorldMat/worldToVoxMat
attributes for NIFTI images with the given sform/qform code combination.
...
...
@@ -587,7 +587,7 @@ def _test_Image_sqforms(imgtype, sformcode, qformcode):
with
pytest
.
raises
(
ValueError
):
image
.
getXFormCode
(
'
badcode
'
)
try
:
assert
np
.
all
(
np
.
isclose
(
image
.
voxToWorldMat
,
expAffine
))
assert
np
.
all
(
np
.
isclose
(
image
.
worldToVoxMat
,
invExpAffine
))
...
...
@@ -601,10 +601,10 @@ def _test_Image_sqforms(imgtype, sformcode, qformcode):
shutil
.
rmtree
(
testdir
)
def
test_Image_changeXform_analyze
():
_test_Image_changeXform
(
0
)
def
test_Image_changeXform_analyze
():
_test_Image_changeXform
(
0
)
def
test_Image_changeXform_nifti1
():
_test_Image_changeXform
(
1
)
def
test_Image_changeXform_nifti1_nosqform
():
_test_Image_changeXform
(
1
,
0
,
0
)
def
test_Image_changeXform_nifti2
():
_test_Image_changeXform
(
2
)
def
test_Image_changeXform_nifti2
():
_test_Image_changeXform
(
2
)
def
_test_Image_changeXform
(
imgtype
,
sformcode
=
None
,
qformcode
=
None
):
"""
Test changing the Nifti.voxToWorldMat attribute.
"""
...
...
@@ -641,7 +641,7 @@ def _test_Image_changeXform(imgtype, sformcode=None, qformcode=None):
if
imgtype
>
0
:
expSformCode
=
image
.
get_sform
(
coded
=
True
)[
1
]
expQformCode
=
image
.
get_qform
(
coded
=
True
)[
1
]
if
sformcode
==
0
:
expSformCode
=
constants
.
NIFTI_XFORM_ALIGNED_ANAT
else
:
...
...
@@ -654,7 +654,7 @@ def _test_Image_changeXform(imgtype, sformcode=None, qformcode=None):
assert
img
.
saveState
if
imgtype
==
0
:
# ANALYZE affine is not editable
# ANALYZE affine is not editable
with
pytest
.
raises
(
Exception
):
img
.
voxToWorldMat
=
newXform
return
...
...
@@ -679,7 +679,7 @@ def _test_Image_changeXform(imgtype, sformcode=None, qformcode=None):
def
test_Image_changeData_analyze
(
seed
):
_test_Image_changeData
(
0
)
def
test_Image_changeData_nifti1
(
seed
):
_test_Image_changeData
(
1
)
def
test_Image_changeData_nifti2
(
seed
):
_test_Image_changeData
(
2
)
def
test_Image_changeData_nifti2
(
seed
):
_test_Image_changeData
(
2
)
def
_test_Image_changeData
(
imgtype
):
"""
Test that changing image data triggers notification, and also causes
the dataRange attribute to be updated.
...
...
@@ -687,13 +687,13 @@ def _test_Image_changeData(imgtype):
testdir
=
tempfile
.
mkdtemp
()
imagefile
=
op
.
join
(
testdir
,
'
image
'
)
make_image
(
imagefile
,
imgtype
)
img
=
fslimage
.
Image
(
imagefile
)
notified
=
{}
def
randvox
():
return
(
np
.
random
.
randint
(
0
,
img
.
shape
[
0
]),
np
.
random
.
randint
(
0
,
img
.
shape
[
1
]),
...
...
@@ -773,7 +773,7 @@ def _test_Image_2D(imgtype):
# which happens when you create
# an XY slice with fslroi. This
# should still be read in as a
# 3D image.
# 3D image.
testdims
=
[(
10
,
20
),
(
10
,
20
,
1
),
(
10
,
1
,
20
),
...
...
@@ -787,9 +787,9 @@ def _test_Image_2D(imgtype):
for
shape
in
testdims
:
pixdim
=
[
2
]
*
len
(
shape
)
imagefile
=
op
.
join
(
testdir
,
'
image
'
)
make_image
(
imagefile
,
imgtype
,
shape
,
pixdim
)
image
=
fslimage
.
Image
(
imagefile
)
...
...
@@ -831,9 +831,9 @@ def test_Image_voxelsToScaledVoxels():
if
itype
>
0
and
pixdims
[
0
]
>
0
:
xf
[
0
,
0
]
=
-
pixdims
[
0
]
xf
[
0
,
3
]
=
pixdims
[
0
]
*
(
dims
[
0
]
-
1
)
return
xf
for
imgType
,
dim
,
pixdim
in
it
.
product
(
imgTypes
,
dims
,
pixdims
):
nimg
=
make_image
(
imgtype
=
imgType
,
dims
=
dim
,
pixdims
=
pixdim
)
img
=
fslimage
.
Image
(
nimg
)
...
...
@@ -842,7 +842,7 @@ def test_Image_voxelsToScaledVoxels():
result
=
img
.
voxelsToScaledVoxels
()
assert
np
.
all
(
np
.
isclose
(
result
,
expected
))
def
test_Image_sameSpace
():
...
...
@@ -853,7 +853,7 @@ def test_Image_sameSpace():
pixdims
=
[(
2
,
2
,
2
,
1
),
(
2
,
3
,
4
,
1
)]
for
(
imgType
,
for
(
imgType
,
dim1
,
dim2
,
pixdim1
,
...
...
@@ -885,8 +885,8 @@ def _test_Image_save(imgtype):
if
rvox
not
in
rvoxes
:
rvoxes
.
append
(
rvox
)
return
rvoxes
testdir
=
tempfile
.
mkdtemp
()
if
imgtype
==
0
:
filename
=
op
.
join
(
testdir
,
'
blob.img
'
)
...
...
@@ -904,11 +904,13 @@ def _test_Image_save(imgtype):
try
:
make_image
(
filename
,
imgtype
)
img
=
fslimage
.
Image
(
filename
)
# Using mmap can cause a "Bus error"
# under docker. No idea why.
img
=
fslimage
.
Image
(
filename
,
mmap
=
False
)
randvoxes
=
randvoxes
(
5
)
randvals
=
[
np
.
random
.
random
()
for
i
in
range
(
5
)]
for
(
x
,
y
,
z
),
v
in
zip
(
randvoxes
,
randvals
):
img
[
x
,
y
,
z
]
=
v
...
...
@@ -937,7 +939,7 @@ def _test_Image_save(imgtype):
# Load the image back in
img2
=
fslimage
.
Image
(
img
.
dataSource
)
assert
img
.
saveState
assert
img
.
dataSource
==
expDataSource
...
...
@@ -945,8 +947,8 @@ def _test_Image_save(imgtype):
assert
np
.
all
(
np
.
isclose
(
img
.
voxToWorldMat
,
xform
))
for
(
x
,
y
,
z
),
v
in
zip
(
randvoxes
,
randvals
):
assert
np
.
isclose
(
img
[
x
,
y
,
z
],
v
)
assert
np
.
isclose
(
img
[
x
,
y
,
z
],
v
)
finally
:
shutil
.
rmtree
(
testdir
)
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