Skip to content

Commit 9cad3cd

Browse files
committed
@effigies R interface suggestions
1 parent c4cb539 commit 9cad3cd

File tree

2 files changed

+17
-65
lines changed

2 files changed

+17
-65
lines changed

nipype/interfaces/r.py

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,6 @@ class RCommand(CommandLine):
5151
"""
5252

5353
_cmd = get_r_command()
54-
_default_r_cmd = None
55-
_default_rfile = None
5654
input_spec = RInputSpec
5755

5856
def __init__(self, r_cmd=None, **inputs):
@@ -62,37 +60,26 @@ def __init__(self, r_cmd=None, **inputs):
6260
super(RCommand, self).__init__(**inputs)
6361
if r_cmd and isdefined(r_cmd):
6462
self._cmd = r_cmd
65-
elif self._default_r_cmd:
66-
self._cmd = self._default_r_cmd
67-
68-
if self._default_rfile and not isdefined(self.inputs.rfile):
69-
self.inputs.rfile = self._default_rfile
7063

7164
# For r commands force all output to be returned since r
7265
# does not have a clean way of notifying an error
7366
self.terminal_output = "allatonce"
7467

75-
@classmethod
76-
def set_default_r_cmd(cls, r_cmd):
68+
def set_default_r_cmd(self, r_cmd):
7769
"""Set the default R command line for R classes.
7870
7971
This method is used to set values for all R
80-
subclasses. However, setting this will not update the output
81-
type for any existing instances. For these, assign the
82-
<instance>.inputs.r_cmd.
72+
subclasses.
8373
"""
84-
cls._default_r_cmd = r_cmd
74+
self._cmd = r_cmd
8575

86-
@classmethod
87-
def set_default_rfile(cls, rfile):
76+
def set_default_rfile(self, rfile):
8877
"""Set the default R script file format for R classes.
8978
9079
This method is used to set values for all R
91-
subclasses. However, setting this will not update the output
92-
type for any existing instances. For these, assign the
93-
<instance>.inputs.rfile.
80+
subclasses.
9481
"""
95-
cls._default_rfile = rfile
82+
self._rfile = rfile
9683

9784
def _run_interface(self, runtime):
9885
self.terminal_output = "allatonce"

nipype/interfaces/tests/test_r.py

Lines changed: 11 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -8,48 +8,28 @@
88

99
no_r = r.no_r
1010

11-
12-
def clean_workspace_and_get_default_script_file():
13-
# Make sure things are clean.
14-
default_script_file = r.RInputSpec().script_file
15-
if os.path.exists(default_script_file):
16-
os.remove(
17-
default_script_file
18-
) # raise Exception('Default script file needed for tests; please remove %s!' % default_script_file)
19-
return default_script_file
20-
21-
2211
@pytest.mark.skipif(no_r, reason="R is not available")
2312
def test_cmdline(tmp_path):
24-
ri = r.RCommand(script="1 + 1", script_file=str(tmp_path / "testscript"), rfile=False)
13+
default_script_file = str(tmp_path / "testscript")
14+
ri = r.RCommand(script="1 + 1", script_file=default_script_file, rfile=False)
15+
r_cmd = r.get_r_command()
2516

2617
assert ri.cmdline == r_cmd + (
2718
' -e "1 + 1"'
2819
)
2920

3021
assert ri.inputs.script == "1 + 1"
31-
assert ri.inputs.script_file == str(tmp_path / "testscript")
22+
assert ri.inputs.script_file == default_script_file
3223
assert not os.path.exists(ri.inputs.script_file), "scriptfile should not exist"
3324
assert not os.path.exists(
3425
default_script_file
3526
), "default scriptfile should not exist."
3627

3728

38-
@pytest.mark.skipif(no_r, reason="R is not available")
39-
def test_r_init():
40-
default_script_file = clean_workspace_and_get_default_script_file()
41-
42-
assert r.RCommand._cmd == r.get_r_command()
43-
assert r.RCommand.input_spec == r.RInputSpec
44-
45-
assert r.RCommand().cmd == r_cmd
46-
rc = r.RCommand(r_cmd="foo_m")
47-
assert rc.cmd == "foo_m"
48-
49-
5029
@pytest.mark.skipif(no_r, reason="R is not available")
5130
def test_run_interface(tmpdir):
52-
default_script_file = clean_workspace_and_get_default_script_file()
31+
os.chdir(tmpdir)
32+
default_script_file = r.RInputSpec().script_file
5333

5434
rc = r.RCommand(r_cmd="foo_m")
5535
assert not os.path.exists(default_script_file), "scriptfile should not exist 1."
@@ -67,31 +47,16 @@ def test_run_interface(tmpdir):
6747
if os.path.exists(default_script_file): # cleanup
6848
os.remove(default_script_file)
6949

70-
cwd = tmpdir.chdir()
71-
72-
# bypasses ubuntu dash issue
73-
rc = r.RCommand(script="foo;", rfile=True)
74-
assert not os.path.exists(default_script_file), "scriptfile should not exist 4."
75-
with pytest.raises(RuntimeError):
76-
rc.run()
77-
assert os.path.exists(default_script_file), "scriptfile should exist 4."
78-
if os.path.exists(default_script_file): # cleanup
79-
os.remove(default_script_file)
80-
81-
# bypasses ubuntu dash issue
82-
res = r.RCommand(script="a=1;", rfile=True).run()
83-
assert res.runtime.returncode == 0
84-
assert os.path.exists(default_script_file), "scriptfile should exist 5."
85-
cwd.chdir()
8650

8751

8852
@pytest.mark.skipif(no_r, reason="R is not available")
89-
def test_set_rcmd():
90-
default_script_file = clean_workspace_and_get_default_script_file()
53+
def test_set_rcmd(tmpdir):
54+
os.chdir(tmpdir)
55+
default_script_file = r.RInputSpec().script_file
9156

9257
ri = r.RCommand()
93-
_default_r_cmd = ri._default_r_cmd
58+
_default_r_cmd = ri._cmd
9459
ri.set_default_r_cmd("foo")
9560
assert not os.path.exists(default_script_file), "scriptfile should not exist."
96-
assert ri._default_r_cmd == "foo"
61+
assert ri._cmd == "foo"
9762
ri.set_default_r_cmd(_default_r_cmd)

0 commit comments

Comments
 (0)