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
8bc789a8
Commit
8bc789a8
authored
8 years ago
by
Paul McCarthy
Browse files
Options
Downloads
Patches
Plain Diff
More path tests, and tests for fsl.data.image
parent
b118b488
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
tests/test_fsl_utils_path.py
+12
-10
12 additions, 10 deletions
tests/test_fsl_utils_path.py
tests/test_image.py
+156
-9
156 additions, 9 deletions
tests/test_image.py
with
168 additions
and
19 deletions
tests/test_fsl_utils_path.py
+
12
−
10
View file @
8bc789a8
...
...
@@ -10,6 +10,7 @@ import os.path as op
import
pytest
import
fsl.utils.path
as
fslpath
import
fsl.data.image
as
fslimage
def
test_addExt_exists_shouldPass
(
testdir
):
...
...
@@ -17,9 +18,8 @@ def test_addExt_exists_shouldPass(testdir):
are valid.
"""
# If we have a .hdr/.img pair, the .img should take precedence
replacements
=
{
'
.hdr
'
:
[
'
.img
'
,
'
.img.gz
'
]}
allowedExts
=
[
'
.hdr
'
,
'
.img
'
,
'
.nii
'
,
'
.nii.gz
'
,
'
.img.gz
'
]
replacements
=
fslimage
.
REPLACEMENTS
allowedExts
=
fslimage
.
ALLOWED_EXTENSIONS
tests
=
[
(
'
compressed
'
,
'
compressed.nii.gz
'
),
...
...
@@ -30,7 +30,7 @@ def test_addExt_exists_shouldPass(testdir):
(
'
img_hdr_pair.hdr
'
,
'
img_hdr_pair.hdr
'
),
(
'
img_hdr_pair.img
'
,
'
img_hdr_pair.img
'
),
(
'
compressed_img_hdr_pair
'
,
'
compressed_img_hdr_pair.img.gz
'
),
(
'
compressed_img_hdr_pair.hdr
'
,
'
compressed_img_hdr_pair.hdr
'
),
(
'
compressed_img_hdr_pair.hdr
.gz
'
,
'
compressed_img_hdr_pair.hdr
.gz
'
),
(
'
compressed_img_hdr_pair.img.gz
'
,
'
compressed_img_hdr_pair.img.gz
'
),
]
...
...
@@ -48,9 +48,9 @@ def test_addExt_exists_shouldFail(testdir):
"""
Tests the addExt function with inputs that should cause it to raise an
error.
"""
replacements
=
{
'
.hdr
'
:
[
'
.img
'
,
'
.img.gz
'
]}
allowedExts
=
[
'
.hdr
'
,
'
.img
'
,
'
.nii
'
,
'
.nii.gz
'
,
'
.img.gz
'
]
replacements
=
fslimage
.
REPLACEMENTS
allowedExts
=
fslimage
.
ALLOWED_EXTENSIONS
shouldFail
=
[
...
...
@@ -73,6 +73,8 @@ def test_addExt_exists_shouldFail(testdir):
(
'
ambiguous
'
,
[],
{
'
.hdr
'
:
[
'
.img
'
]}),
(
'
ambiguous
'
,
False
,
{
'
.hdr
'
:
[
'
.img.gz
'
]}),
(
'
ambiguous
'
,
[],
{
'
.hdr
'
:
[
'
.img.gz
'
]}),
(
'
ambiguous
'
,
False
,
{
'
.hdr
'
:
[
'
.img
'
,
'
.img.gz
'
,
'
.nii
'
]}),
(
'
ambiguous
'
,
[],
{
'
.hdr
'
:
[
'
.img
'
,
'
.img.gz
'
,
'
.nii
'
]}),
(
'
badpath
'
),
(
'
badpath.nii.gz
'
),
...
...
@@ -101,7 +103,7 @@ def test_addExt_exists_shouldFail(testdir):
def
test_addExt_noExist
(
testdir
):
allowedExts
=
[
'
.hdr
'
,
'
.img
'
,
'
.nii
'
,
'
.nii.gz
'
,
'
.img.gz
'
]
allowedExts
=
fslimage
.
ALLOWED_EXTENSIONS
# Prefix, output, defaultExt, allowedExts
tests
=
[
...
...
@@ -136,7 +138,7 @@ def test_addExt_noExist(testdir):
def
test_removeExt
(
testdir
):
allowedExts
=
[
'
.hdr
'
,
'
.img
'
,
'
.nii
'
,
'
.nii.gz
'
,
'
.img.gz
'
]
allowedExts
=
fslimage
.
ALLOWED_EXTENSIONS
# If len(test) == 2, allowedExts is set from above
# Otherwise, it is set from the test tuple
...
...
@@ -170,7 +172,7 @@ def test_removeExt(testdir):
def
test_getExt
(
testdir
):
allowedExts
=
[
'
.hdr
'
,
'
.img
'
,
'
.nii
'
,
'
.nii.gz
'
,
'
.img.gz
'
]
allowedExts
=
fslimage
.
ALLOWED_EXTENSIONS
# len(test) == 2 -> allowedExts set from above
# Otherwise, allowedExts set from test tuple
...
...
This diff is collapsed.
Click to expand it.
tests/test_image.py
+
156
−
9
View file @
8bc789a8
...
...
@@ -6,13 +6,16 @@
#
import
os.path
as
op
import
logging
import
pytest
import
glob
import
numpy
as
np
import
nibabel
as
nib
import
fsl.data.image
as
fslimage
import
fsl.data.constants
as
constants
import
fsl.data.image
as
fslimage
import
fsl.utils.path
as
fslpath
# Need to test:
...
...
@@ -25,13 +28,157 @@ import fsl.data.image as fslimage
def
test_load
(
testdir
):
assert
testdir
is
not
None
shouldPass
=
[
'
compressed
'
,
'
compressed.nii.gz
'
,
'
uncompressed
'
,
'
uncompressed.nii
'
,
'
img_hdr_pair
'
,
'
img_hdr_pair.img
'
,
'
img_hdr_pair.hdr
'
,
'
compressed_img_hdr_pair
'
,
'
compressed_img_hdr_pair.img.gz
'
,
'
compressed_img_hdr_pair.hdr.gz
'
,
'
ambiguous.nii
'
,
'
ambiguous.hdr
'
,
'
ambiguous.img
'
,
'
ambiguous.hdr.gz
'
,
'
ambiguous.img.gz
'
]
shouldRaise
=
[
'
notexist
'
,
'
notexist.nii.gz
'
,
'
ambiguous
'
]
fslimage
.
Image
(
op
.
join
(
testdir
,
'
nifti_formats
'
,
'
compressed
'
))
fslimage
.
Image
(
op
.
join
(
testdir
,
'
nifti_formats
'
,
'
compressed.nii.gz
'
))
fslimage
.
Image
(
op
.
join
(
testdir
,
'
nifti_formats
'
,
'
uncompressed
'
))
fslimage
.
Image
(
op
.
join
(
testdir
,
'
nifti_formats
'
,
'
uncompressed.nii
'
))
fslimage
.
Image
(
op
.
join
(
testdir
,
'
nifti_formats
'
,
'
img_hdr_pair
'
))
fslimage
.
Image
(
op
.
join
(
testdir
,
'
nifti_formats
'
,
'
img_hdr_pair.img
'
))
fslimage
.
Image
(
op
.
join
(
testdir
,
'
nifti_formats
'
,
'
img_hdr_pair.hdr
'
))
# Not raising an error means the test passes
for
fname
in
shouldPass
:
fslimage
.
Image
(
op
.
join
(
testdir
,
'
nifti_formats
'
,
fname
))
# These should raise an error
for
fname
in
shouldRaise
:
with
pytest
.
raises
(
fslpath
.
PathError
):
print
fname
fslimage
.
Image
(
op
.
join
(
testdir
,
'
nifti_formats
'
,
fname
))
def
test_Image_atts
(
testdir
):
allowedExts
=
fslimage
.
ALLOWED_EXTENSIONS
# (file, dims, pixdims)
tests
=
[
(
'
MNI152_T1_0.5mm
'
,
(
364
,
436
,
364
),
(
0.5
,
0.5
,
0.5
),
np
.
uint8
),
(
'
MNI152_T1_1mm
'
,
(
182
,
218
,
182
),
(
1.0
,
1.0
,
1.0
),
np
.
int16
),
(
'
MNI152_T1_2mm
'
,
(
91
,
109
,
91
),
(
2.0
,
2.0
,
2.0
),
np
.
int16
),
(
'
MNI152_T1_2mm_4D
'
,
(
91
,
109
,
91
,
5
),
(
2.0
,
2.0
,
2.0
,
1.0
),
np
.
int16
),
(
op
.
join
(
'
nifti2D
'
,
'
MNI152_T1_2mm_sliceXY
'
),
(
91
,
109
,
1
),
(
2.0
,
2.0
,
2.0
),
np
.
int16
),
(
op
.
join
(
'
nifti2D
'
,
'
MNI152_T1_2mm_sliceXZ
'
),
(
91
,
1
,
91
),
(
2.0
,
2.0
,
2.0
),
np
.
int16
),
(
op
.
join
(
'
nifti2D
'
,
'
MNI152_T1_2mm_sliceYZ
'
),
(
1
,
109
,
91
),
(
2.0
,
2.0
,
2.0
),
np
.
int16
)]
for
path
,
dims
,
pixdims
,
dtype
in
tests
:
path
=
op
.
abspath
(
op
.
join
(
testdir
,
path
))
i
=
fslimage
.
Image
(
path
)
assert
tuple
(
i
.
shape
)
==
tuple
(
dims
)
assert
tuple
(
i
.
pixdim
)
==
tuple
(
pixdims
)
assert
tuple
(
i
.
nibImage
.
shape
)
==
tuple
(
dims
)
assert
tuple
(
i
.
nibImage
.
header
.
get_zooms
())
==
tuple
(
pixdims
)
assert
i
.
dtype
==
dtype
assert
i
.
name
==
op
.
basename
(
path
)
assert
i
.
dataSource
==
fslpath
.
addExt
(
path
,
allowedExts
,
mustExist
=
True
)
def
test_looksLikeImage
():
# (file, expected)
tests
=
[
(
'
blah
'
,
False
),
(
'
blah.moo
'
,
False
),
(
'
blah.nii
'
,
True
),
(
'
blah.nii.gz
'
,
True
),
(
'
blah.hdr
'
,
True
),
(
'
blah.img
'
,
True
),
(
'
blah.hdr.gz
'
,
True
),
(
'
blah.img.gz
'
,
True
),
]
for
path
,
expected
in
tests
:
assert
fslimage
.
looksLikeImage
(
path
)
==
expected
def
test_addExt
(
testdir
):
default
=
fslimage
.
DEFAULT_EXTENSION
# (file, mustExist, expected)
tests
=
[
(
'
blah
'
,
False
,
'
blah{}
'
.
format
(
default
)),
(
'
blah.nii
'
,
False
,
'
blah.nii
'
),
(
'
blah.nii.gz
'
,
False
,
'
blah.nii.gz
'
),
(
'
blah.img
'
,
False
,
'
blah.img
'
),
(
'
blah.hdr
'
,
False
,
'
blah.hdr
'
),
(
'
blah.img.gz
'
,
False
,
'
blah.img.gz
'
),
(
'
blah.hdr.gz
'
,
False
,
'
blah.hdr.gz
'
),
(
'
compressed
'
,
True
,
'
compressed.nii.gz
'
),
(
'
compressed.nii.gz
'
,
True
,
'
compressed.nii.gz
'
),
(
'
uncompressed
'
,
True
,
'
uncompressed.nii
'
),
(
'
uncompressed.nii
'
,
True
,
'
uncompressed.nii
'
),
(
'
img_hdr_pair
'
,
True
,
'
img_hdr_pair.img
'
),
(
'
img_hdr_pair.hdr
'
,
True
,
'
img_hdr_pair.hdr
'
),
(
'
img_hdr_pair.img
'
,
True
,
'
img_hdr_pair.img
'
),
(
'
compressed_img_hdr_pair
'
,
True
,
'
compressed_img_hdr_pair.img.gz
'
),
(
'
compressed_img_hdr_pair.img.gz
'
,
True
,
'
compressed_img_hdr_pair.img.gz
'
),
(
'
compressed_img_hdr_pair.hdr.gz
'
,
True
,
'
compressed_img_hdr_pair.hdr.gz
'
),
(
'
ambiguous.nii
'
,
True
,
'
ambiguous.nii
'
),
(
'
ambiguous.nii.gz
'
,
True
,
'
ambiguous.nii.gz
'
),
(
'
ambiguous.img
'
,
True
,
'
ambiguous.img
'
),
(
'
ambiguous.hdr
'
,
True
,
'
ambiguous.hdr
'
),
(
'
ambiguous.img.gz
'
,
True
,
'
ambiguous.img.gz
'
),
(
'
ambiguous.hdr.gz
'
,
True
,
'
ambiguous.hdr.gz
'
)]
for
path
,
mustExist
,
expected
in
tests
:
if
mustExist
:
path
=
op
.
join
(
testdir
,
'
nifti_formats
'
,
path
)
expected
=
op
.
join
(
testdir
,
'
nifti_formats
'
,
expected
)
assert
fslimage
.
addExt
(
path
,
mustExist
)
==
expected
with
pytest
.
raises
(
fslimage
.
PathError
):
path
=
op
.
join
(
testdir
,
'
nifti_formats
'
,
'
ambiguous
'
)
fslimage
.
addExt
(
path
,
mustExist
=
True
)
def
test_Image_orientation
(
testdir
):
neuro
=
op
.
join
(
testdir
,
'
dtifit
'
,
'
neuro
'
,
'
dti_FA
'
)
radio
=
op
.
join
(
testdir
,
'
dtifit
'
,
'
radio
'
,
'
dti_FA
'
)
neuro
=
fslimage
.
Image
(
neuro
)
radio
=
fslimage
.
Image
(
radio
)
assert
neuro
.
isNeurological
()
assert
not
radio
.
isNeurological
()
# Both images should have the
# same orientation in the
# world coordinate system
assert
neuro
.
getOrientation
(
0
,
np
.
eye
(
4
))
==
constants
.
ORIENT_L2R
assert
neuro
.
getOrientation
(
1
,
np
.
eye
(
4
))
==
constants
.
ORIENT_P2A
assert
neuro
.
getOrientation
(
2
,
np
.
eye
(
4
))
==
constants
.
ORIENT_I2S
assert
radio
.
getOrientation
(
0
,
np
.
eye
(
4
))
==
constants
.
ORIENT_L2R
assert
radio
.
getOrientation
(
1
,
np
.
eye
(
4
))
==
constants
.
ORIENT_P2A
assert
radio
.
getOrientation
(
2
,
np
.
eye
(
4
))
==
constants
.
ORIENT_I2S
# The radio image should be
# l/r flipped in the voxel
# coordinate system
assert
neuro
.
getOrientation
(
0
,
neuro
.
worldToVoxMat
)
==
constants
.
ORIENT_L2R
assert
neuro
.
getOrientation
(
1
,
neuro
.
worldToVoxMat
)
==
constants
.
ORIENT_P2A
assert
neuro
.
getOrientation
(
2
,
neuro
.
worldToVoxMat
)
==
constants
.
ORIENT_I2S
assert
radio
.
getOrientation
(
0
,
radio
.
worldToVoxMat
)
==
constants
.
ORIENT_R2L
assert
radio
.
getOrientation
(
1
,
radio
.
worldToVoxMat
)
==
constants
.
ORIENT_P2A
assert
radio
.
getOrientation
(
2
,
radio
.
worldToVoxMat
)
==
constants
.
ORIENT_I2S
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