Skip to content

Commit 1b3287a

Browse files
committed
rewrote test to check command line string
1 parent 31c8702 commit 1b3287a

File tree

2 files changed

+48
-31
lines changed

2 files changed

+48
-31
lines changed

nipype/interfaces/tests/test_whitestripe.py

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,31 +5,43 @@
55

66
import pytest
77
import requests
8+
from pathlib import Path
9+
from string import Template
810
from nipype.interfaces import whitestripe
911
from nipype.interfaces.r import get_r_command
1012

1113

12-
@pytest.mark.skipif(get_r_command() is None, reason="R is not available")
1314
def test_whitestripe(tmpdir):
1415
cwd = tmpdir.chdir()
1516

16-
filename = "T1W.nii.gz"
17-
req = requests.get(
18-
"https://johnmuschelli.com/open_ms_data/cross_sectional/coregistered_resampled/patient01/T1W.nii.gz"
19-
)
20-
with open(filename, "wb") as fd:
21-
for chunk in req.iter_content(chunk_size=128):
22-
fd.write(chunk)
17+
Path("T1W.nii.gz").touch()
2318

2419
normalizer = whitestripe.WhiteStripe()
2520
normalizer.inputs.img_type = "T1"
2621
normalizer.inputs.in_file = "T1W.nii.gz"
27-
normalizer.inputs.indices = normalizer.gen_indices()
2822
normalizer.inputs.out_file = "T1W_ws.nii.gz"
29-
normalizer.run()
23+
tmpfile, script = normalizer._cmdline(normalizer)
3024

31-
assert os.path.isfile(normalizer.inputs.out_file)
32-
os.remove(normalizer.inputs.out_file)
25+
expected_script = Template(
26+
# the level of indentation needs to match what's in the whitestripe interface
27+
"""
28+
library(neurobase)
29+
library(WhiteStripe)
30+
in_file = readnii('$in_file')
31+
ind = whitestripe(in_file, "$img_type")$$whitestripe.ind
32+
norm = whitestripe_norm(in_file, ind)
33+
out_file = '$out_file'
34+
writenii(norm, out_file)
35+
"""
36+
).substitute(
37+
{
38+
"in_file": normalizer.inputs.in_file,
39+
"out_file": normalizer.inputs.out_file,
40+
"img_type": normalizer.inputs.img_type,
41+
}
42+
)
43+
assert tmpfile is False
44+
assert script == expected_script
3345
os.remove(normalizer.inputs.in_file)
3446

3547
cwd.chdir()

nipype/interfaces/whitestripe.py

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,29 @@ class WhiteStripe(BaseInterface):
2929
output_spec = WhiteStripeOutputSpec
3030

3131
def _run_interface(self, runtime):
32+
tmpfile, script = self._cmdline(runtime)
33+
34+
# rfile = True will create a .R file with your script and executed.
35+
# Alternatively
36+
# rfile can be set to False which will cause the R code to be
37+
# passed
38+
# as a commandline argument to the R executable
39+
# (without creating any files).
40+
# This, however, is less reliable and harder to debug
41+
# (code will be reduced to
42+
# a single line and stripped of any comments).
43+
rcmd = RCommand(script=script, rfile=False)
44+
result = rcmd.run()
45+
if tmpfile:
46+
os.remove(tmpfile)
47+
return result.runtime
48+
49+
def _list_outputs(self):
50+
outputs = self._outputs().get()
51+
outputs["out_file"] = os.path.abspath(self.inputs.out_file)
52+
return outputs
53+
54+
def _cmdline(self, runtime):
3255
d = dict(
3356
in_file=self.inputs.in_file,
3457
out_file=self.inputs.out_file,
@@ -65,25 +88,7 @@ def _run_interface(self, runtime):
6588
"""
6689
).substitute(d)
6790

68-
# rfile = True will create a .R file with your script and executed.
69-
# Alternatively
70-
# rfile can be set to False which will cause the R code to be
71-
# passed
72-
# as a commandline argument to the R executable
73-
# (without creating any files).
74-
# This, however, is less reliable and harder to debug
75-
# (code will be reduced to
76-
# a single line and stripped of any comments).
77-
rcmd = RCommand(script=script, rfile=False)
78-
result = rcmd.run()
79-
if tmpfile:
80-
os.remove(tmpfile)
81-
return result.runtime
82-
83-
def _list_outputs(self):
84-
outputs = self._outputs().get()
85-
outputs["out_file"] = os.path.abspath(self.inputs.out_file)
86-
return outputs
91+
return tmpfile, script
8792

8893
def gen_indices(self):
8994
path = tempfile.mkstemp()[1]

0 commit comments

Comments
 (0)