Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
rewrote test to check command line string
  • Loading branch information
Terf committed Jul 8, 2021
commit 1b3287a6aa1a946609cfd04462d5eec4369fdb6e
36 changes: 24 additions & 12 deletions nipype/interfaces/tests/test_whitestripe.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,43 @@

import pytest
import requests
from pathlib import Path
from string import Template
from nipype.interfaces import whitestripe
from nipype.interfaces.r import get_r_command


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

filename = "T1W.nii.gz"
req = requests.get(
"https://johnmuschelli.com/open_ms_data/cross_sectional/coregistered_resampled/patient01/T1W.nii.gz"
)
with open(filename, "wb") as fd:
for chunk in req.iter_content(chunk_size=128):
fd.write(chunk)
Path("T1W.nii.gz").touch()

normalizer = whitestripe.WhiteStripe()
normalizer.inputs.img_type = "T1"
normalizer.inputs.in_file = "T1W.nii.gz"
normalizer.inputs.indices = normalizer.gen_indices()
normalizer.inputs.out_file = "T1W_ws.nii.gz"
normalizer.run()
tmpfile, script = normalizer._cmdline(normalizer)

assert os.path.isfile(normalizer.inputs.out_file)
os.remove(normalizer.inputs.out_file)
expected_script = Template(
# the level of indentation needs to match what's in the whitestripe interface
"""
library(neurobase)
library(WhiteStripe)
in_file = readnii('$in_file')
ind = whitestripe(in_file, "$img_type")$$whitestripe.ind
norm = whitestripe_norm(in_file, ind)
out_file = '$out_file'
writenii(norm, out_file)
"""
).substitute(
{
"in_file": normalizer.inputs.in_file,
"out_file": normalizer.inputs.out_file,
"img_type": normalizer.inputs.img_type,
}
)
assert tmpfile is False
assert script == expected_script
os.remove(normalizer.inputs.in_file)

cwd.chdir()
43 changes: 24 additions & 19 deletions nipype/interfaces/whitestripe.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,29 @@ class WhiteStripe(BaseInterface):
output_spec = WhiteStripeOutputSpec

def _run_interface(self, runtime):
tmpfile, script = self._cmdline(runtime)

# rfile = True will create a .R file with your script and executed.
# Alternatively
# rfile can be set to False which will cause the R code to be
# passed
# as a commandline argument to the R executable
# (without creating any files).
# This, however, is less reliable and harder to debug
# (code will be reduced to
# a single line and stripped of any comments).
rcmd = RCommand(script=script, rfile=False)
result = rcmd.run()
if tmpfile:
os.remove(tmpfile)
return result.runtime

def _list_outputs(self):
outputs = self._outputs().get()
outputs["out_file"] = os.path.abspath(self.inputs.out_file)
return outputs

def _cmdline(self, runtime):
d = dict(
in_file=self.inputs.in_file,
out_file=self.inputs.out_file,
Expand Down Expand Up @@ -65,25 +88,7 @@ def _run_interface(self, runtime):
"""
).substitute(d)

# rfile = True will create a .R file with your script and executed.
# Alternatively
# rfile can be set to False which will cause the R code to be
# passed
# as a commandline argument to the R executable
# (without creating any files).
# This, however, is less reliable and harder to debug
# (code will be reduced to
# a single line and stripped of any comments).
rcmd = RCommand(script=script, rfile=False)
result = rcmd.run()
if tmpfile:
os.remove(tmpfile)
return result.runtime

def _list_outputs(self):
outputs = self._outputs().get()
outputs["out_file"] = os.path.abspath(self.inputs.out_file)
return outputs
return tmpfile, script

def gen_indices(self):
path = tempfile.mkstemp()[1]
Expand Down