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
7485f176
Commit
7485f176
authored
Mar 27, 2020
by
Paul McCarthy
🚵
Browse files
TEST: test fslstats wrapper
parent
195cb21f
Changes
1
Hide whitespace changes
Inline
Side-by-side
tests/test_wrappers/test_fslstats.py
0 → 100644
View file @
7485f176
#!/usr/bin/env python
import
os
import
os.path
as
op
import
sys
import
contextlib
import
numpy
as
np
import
fsl.utils.run
as
run
import
fsl.utils.tempdir
as
tempdir
import
fsl.wrappers
as
fw
from
..
import
mockFSLDIR
as
mockFSLDIR_base
,
make_random_image
mock_fslstats
=
"""
#!{}
shape = {{outshape}}
import sys
import numpy as np
data = np.random.randint(1, 10, shape)
if len(shape) == 1:
data = data.reshape(1, -1)
np.savetxt(sys.stdout, data, fmt='%i')
"""
.
format
(
sys
.
executable
).
strip
()
@
contextlib
.
contextmanager
def
mockFSLDIR
(
shape
):
with
mockFSLDIR_base
()
as
fd
:
fname
=
op
.
join
(
fd
,
'bin'
,
'fslstats'
)
script
=
mock_fslstats
.
format
(
outshape
=
shape
)
with
open
(
fname
,
'wt'
)
as
f
:
f
.
write
(
script
)
os
.
chmod
(
fname
,
0o755
)
yield
fd
def
test_fslstats_cmdline
():
with
tempdir
.
tempdir
(),
run
.
dryrun
(),
mockFSLDIR
(
1
)
as
fsldir
:
make_random_image
(
'image'
)
cmd
=
op
.
join
(
fsldir
,
'bin'
,
'fslstats'
)
result
=
fw
.
fslstats
(
'image'
).
m
.
r
.
mask
(
'mask'
).
k
(
'mask'
).
r
.
run
(
True
)
expected
=
cmd
+
' image -m -r -k mask -k mask -r'
assert
result
[
0
]
==
expected
result
=
fw
.
fslstats
(
'image'
,
t
=
True
,
K
=
'mask'
).
m
.
R
.
u
(
123
).
s
.
volume
.
run
(
True
)
expected
=
cmd
+
' -t -K mask image -m -R -u 123 -s -v'
assert
result
[
0
]
==
expected
result
=
fw
.
fslstats
(
'image'
,
K
=
'mask'
).
n
.
V
.
p
(
1
).
run
(
True
)
expected
=
cmd
+
' -K mask image -n -V -p 1'
assert
result
[
0
]
==
expected
result
=
fw
.
fslstats
(
'image'
,
t
=
True
).
H
(
10
,
1
,
99
).
d
(
'diff'
).
run
(
True
)
expected
=
cmd
+
' -t image -H 10 1 99 -d diff'
assert
result
[
0
]
==
expected
def
test_fslstats_result
():
with
tempdir
.
tempdir
():
with
mockFSLDIR
(
'(1,)'
)
as
fsldir
:
result
=
fw
.
fslstats
(
'image'
).
run
()
assert
np
.
isscalar
(
result
)
with
mockFSLDIR
(
'(2,)'
)
as
fsldir
:
result
=
fw
.
fslstats
(
'image'
).
run
()
assert
result
.
shape
==
(
2
,)
# 3 mask lbls, 2 values
with
mockFSLDIR
(
'(3, 2)'
)
as
fsldir
:
result
=
fw
.
fslstats
(
'image'
,
K
=
'mask'
).
run
()
assert
result
.
shape
==
(
3
,
2
)
# 5 vols, 2 values
with
mockFSLDIR
(
'(5, 2)'
)
as
fsldir
:
result
=
fw
.
fslstats
(
'image'
,
t
=
True
).
run
()
assert
result
.
shape
==
(
5
,
2
)
# 5 vols, 3 mask lbls, 2 values
with
mockFSLDIR
(
'(15, 2)'
)
as
fsldir
:
make_random_image
(
'image'
,
(
10
,
10
,
10
,
5
))
result
=
fw
.
fslstats
(
'image'
,
K
=
'mask'
,
t
=
True
).
run
()
assert
result
.
shape
==
(
5
,
3
,
2
)
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