Skip to content

Commit 96cc236

Browse files
committed
FIX: pytest and doctest pass
1 parent 0967dd8 commit 96cc236

File tree

5 files changed

+210
-55
lines changed

5 files changed

+210
-55
lines changed

pydra/tasks/fsl/model/cluster.py

Lines changed: 119 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,35 @@
33
import typing as ty
44

55

6-
def Cluster_output(inputs):
7-
import attr
6+
def Cluster_output(field, inputs):
7+
import os, attr
88
from pydra.engine.helpers_file import split_filename
99

1010
in_file = inputs.in_file
11+
name = field.name
12+
pth, fname, ext = split_filename(in_file)
13+
14+
if name == "out_index_file":
15+
return os.path.join(pth, f"{fname}_index{ext}")
16+
elif name == "out_localmax_txt_file":
17+
return os.path.join(pth, f"{fname}_localmax.txt")
18+
elif name == "out_localmax_vol_file":
19+
return os.path.join(pth, f"{fname}_localmax{ext}")
20+
elif name == "out_max_file":
21+
return os.path.join(pth, f"{fname}_max{ext}")
22+
elif name == "out_mean_file":
23+
return os.path.join(pth, f"{fname}_mean{ext}")
24+
elif name == "out_pval_file":
25+
return os.path.join(pth, f"{fname}_pval{ext}")
26+
elif name == "out_size_file":
27+
return os.path.join(pth, f"{fname}_size{ext}")
28+
elif name == "out_threshold_file":
29+
return os.path.join(pth, f"{fname}_threshold{ext}")
1130

12-
if in_file not in [None, attr.NOTHING]:
13-
pth, fname, ext = split_filename(in_file)
14-
return f"{fname}_localmax.txt"
1531
else:
16-
raise Exception(f"this function should be run only for out_localmax_txt_file not {name}")
32+
raise Exception(
33+
f"this function should be run only for index_file, localmax_txt_file, localmax_vol_file, max_file, mean_file, pval_file, size_file, or threshold_file not {name}"
34+
)
1735

1836

1937
input_fields = [
@@ -33,74 +51,66 @@ def Cluster_output(inputs):
3351
),
3452
(
3553
"out_index_file",
36-
str,
54+
ty.Any,
3755
{
3856
"help_string": "output of cluster index (in size order)",
3957
"argstr": "--oindex={out_index_file}",
40-
"output_file_template": "{in_file}_index",
4158
},
4259
),
4360
(
4461
"out_threshold_file",
45-
str,
62+
ty.Any,
4663
{
4764
"help_string": "thresholded image",
4865
"argstr": "--othresh={out_threshold_file}",
49-
"output_file_template": "{in_file}_threshold",
5066
},
5167
),
5268
(
5369
"out_localmax_txt_file",
54-
str,
70+
ty.Any,
5571
{
5672
"help_string": "local maxima text file",
5773
"argstr": "--olmax={out_localmax_txt_file}",
58-
"output_file_template": Cluster_output,
5974
},
6075
),
6176
(
6277
"out_localmax_vol_file",
63-
str,
78+
ty.Any,
6479
{
6580
"help_string": "output of local maxima volume",
6681
"argstr": "--olmaxim={out_localmax_vol_file}",
67-
"output_file_template": "{in_file}_localmax",
6882
},
6983
),
7084
(
7185
"out_size_file",
72-
str,
86+
ty.Any,
7387
{
7488
"help_string": "filename for output of size image",
7589
"argstr": "--osize={out_size_file}",
76-
"output_file_template": "{in_file}_size",
7790
},
7891
),
7992
(
8093
"out_max_file",
81-
str,
94+
ty.Any,
8295
{
8396
"help_string": "filename for output of max image",
8497
"argstr": "--omax={out_max_file}",
85-
"output_file_template": "{in_file}_max",
8698
},
8799
),
88100
(
89101
"out_mean_file",
90-
str,
102+
ty.Any,
91103
{
92104
"help_string": "filename for output of mean image",
93105
"argstr": "--omean={out_mean_file}",
94-
"output_file_template": "{in_file}_mean",
95106
},
96107
),
97108
(
98109
"out_pval_file",
99-
str,
110+
ty.Any,
100111
{
101112
"help_string": "filename for image output of log pvals",
102113
"argstr": "--opvals={out_pval_file}",
103-
"output_file_template": "{in_file}_pval",
104114
},
105115
),
106116
(
@@ -211,9 +221,84 @@ def Cluster_output(inputs):
211221
},
212222
),
213223
]
214-
Cluster_input_spec = specs.SpecInfo(name="Input", fields=input_fields, bases=(specs.ShellSpec,))
224+
Cluster_input_spec = specs.SpecInfo(
225+
name="Input", fields=input_fields, bases=(specs.ShellSpec,)
226+
)
215227

216-
output_fields = []
228+
output_fields = [
229+
(
230+
"out_index_file",
231+
specs.File,
232+
{
233+
"help_string": "output of cluster index (in size order)",
234+
"requires": ["in_file", "out_index_file"],
235+
"callable": Cluster_output,
236+
},
237+
),
238+
(
239+
"out_threshold_file",
240+
specs.File,
241+
{
242+
"help_string": "thresholded image",
243+
"requires": ["in_file", "out_threshold_file"],
244+
"callable": Cluster_output,
245+
},
246+
),
247+
(
248+
"out_localmax_txt_file",
249+
specs.File,
250+
{
251+
"help_string": "local maxima text file",
252+
"requires": ["in_file", "out_localmax_txt_file"],
253+
"callable": Cluster_output,
254+
},
255+
),
256+
(
257+
"out_localmax_vol_file",
258+
specs.File,
259+
{
260+
"help_string": "output of local maxima volume",
261+
"requires": ["in_file", "out_localmax_vol_file"],
262+
"callable": Cluster_output,
263+
},
264+
),
265+
(
266+
"out_size_file",
267+
specs.File,
268+
{
269+
"help_string": "filename for output of size image",
270+
"requires": ["in_file", "out_size_file"],
271+
"callable": Cluster_output,
272+
},
273+
),
274+
(
275+
"out_max_file",
276+
specs.File,
277+
{
278+
"help_string": "filename for output of max image",
279+
"requires": ["in_file", "out_max_file"],
280+
"callable": Cluster_output,
281+
},
282+
),
283+
(
284+
"out_mean_file",
285+
specs.File,
286+
{
287+
"help_string": "filename for output of mean image",
288+
"requires": ["in_file", "out_mean_file"],
289+
"callable": Cluster_output,
290+
},
291+
),
292+
(
293+
"out_pval_file",
294+
specs.File,
295+
{
296+
"help_string": "filename for image output of log pvals",
297+
"requires": ["in_file", "out_pval_file"],
298+
"callable": Cluster_output,
299+
},
300+
),
301+
]
217302
Cluster_output_spec = specs.SpecInfo(
218303
name="Output", fields=output_fields, bases=(specs.ShellOutSpec,)
219304
)
@@ -227,8 +312,16 @@ class Cluster(ShellCommandTask):
227312
>>> task.inputs.in_file = "zstat1.nii.gz"
228313
>>> task.inputs.threshold = 2.3
229314
>>> task.inputs.use_mm = True
230-
>>> task.cmdline # doctest: +SKIP
231-
'cluster --in=zstat1.nii.gz --thresh=2.3000000000 --mm'
315+
>>> task.inputs.out_index_file = "zstat1_index.nii.gz"
316+
>>> task.inputs.out_threshold_file = "zstat1_threshold.nii.gz"
317+
>>> task.inputs.out_localmax_txt_file = "zstat1_localmax.txt"
318+
>>> task.inputs.out_localmax_vol_file = "zstat1_localmax.nii.gz"
319+
>>> task.inputs.out_size_file = "zstat1_size.nii.gz"
320+
>>> task.inputs.out_max_file = "zstat1_max.nii.gz"
321+
>>> task.inputs.out_mean_file = "zstat1_mean.nii.gz"
322+
>>> task.inputs.out_pval_file = "zstat1_pval.nii.gz"
323+
>>> task.cmdline
324+
'cluster --in=zstat1.nii.gz --thresh=2.3000000000 --oindex=zstat1_index.nii.gz --othresh=zstat1_threshold.nii.gz --olmax=zstat1_localmax.txt --olmaxim=zstat1_localmax.nii.gz --osize=zstat1_size.nii.gz --omax=zstat1_max.nii.gz --omean=zstat1_mean.nii.gz --opvals=zstat1_pval.nii.gz --mm'
232325
"""
233326

234327
input_spec = Cluster_input_spec

pydra/tasks/fsl/model/tests/test_run_cluster.py

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,26 @@
33
from ..cluster import Cluster
44

55

6-
@pytest.mark.xfail("FSLDIR" not in os.environ, reason="no FSL found", raises=FileNotFoundError)
6+
@pytest.mark.xfail(
7+
"FSLDIR" not in os.environ, reason="no FSL found", raises=FileNotFoundError
8+
)
79
@pytest.mark.parametrize(
810
"inputs, outputs",
911
[
1012
(
11-
{"in_file": "zstat1.nii.gz", "threshold": 2.3, "use_mm": True},
13+
{
14+
"in_file": "zstat1.nii.gz",
15+
"threshold": 2.3,
16+
"use_mm": True,
17+
"out_index_file": "zstat1_index.nii.gz",
18+
"out_threshold_file": "zstat1_threshold.nii.gz",
19+
"out_localmax_txt_file": "zstat1_localmax.txt",
20+
"out_localmax_vol_file": "zstat1_localmax.nii.gz",
21+
"out_size_file": "zstat1_size.nii.gz",
22+
"out_max_file": "zstat1_max.nii.gz",
23+
"out_mean_file": "zstat1_mean.nii.gz",
24+
"out_pval_file": "zstat1_pval.nii.gz",
25+
},
1226
[
1327
"out_index_file",
1428
"out_localmax_txt_file",
@@ -49,7 +63,9 @@ def test_Cluster(test_data, inputs, outputs):
4963
except:
5064
pass
5165
task = Cluster(**inputs)
52-
assert set(task.generated_output_names) == set(["return_code", "stdout", "stderr"] + outputs)
66+
assert set(task.generated_output_names) == set(
67+
["return_code", "stdout", "stderr"] + outputs
68+
)
5369
res = task()
5470
print("RESULT: ", res)
5571
for out_nm in outputs:

pydra/tasks/fsl/model/tests/test_spec_cluster.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,19 @@
77
"inputs, outputs",
88
[
99
(
10-
{"in_file": "zstat1.nii.gz", "threshold": 2.3, "use_mm": True},
10+
{
11+
"in_file": "zstat1.nii.gz",
12+
"threshold": 2.3,
13+
"use_mm": True,
14+
"out_index_file": "zstat1_index.nii.gz",
15+
"out_threshold_file": "zstat1_threshold.nii.gz",
16+
"out_localmax_txt_file": "zstat1_localmax.txt",
17+
"out_localmax_vol_file": "zstat1_localmax.nii.gz",
18+
"out_size_file": "zstat1_size.nii.gz",
19+
"out_max_file": "zstat1_max.nii.gz",
20+
"out_mean_file": "zstat1_mean.nii.gz",
21+
"out_pval_file": "zstat1_pval.nii.gz",
22+
},
1123
[
1224
"out_index_file",
1325
"out_localmax_txt_file",
@@ -48,4 +60,6 @@ def test_Cluster(test_data, inputs, outputs):
4860
except:
4961
pass
5062
task = Cluster(**inputs)
51-
assert set(task.generated_output_names) == set(["return_code", "stdout", "stderr"] + outputs)
63+
assert set(task.generated_output_names) == set(
64+
["return_code", "stdout", "stderr"] + outputs
65+
)

specs/callables.py

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,30 @@
1-
def Cluster_output(inputs):
2-
import attr
1+
def Cluster_output(field, inputs):
2+
import os, attr
33
from pydra.engine.helpers_file import split_filename
44

55
in_file = inputs.in_file
6+
name = field.name
7+
pth, fname, ext = split_filename(in_file)
8+
9+
if name == "index_file":
10+
return os.path.join(pth, f"{fname}_index{ext}")
11+
elif name == "localmax_txt_file":
12+
return os.path.join(pth, f"{fname}_localmax.txt")
13+
elif name == "localmax_vol_file":
14+
return os.path.join(pth, f"{fname}_localmax{ext}")
15+
elif name == "max_file":
16+
return os.path.join(pth, f"{fname}_max{ext}")
17+
elif name == "mean_file":
18+
return os.path.join(pth, f"{fname}_mean{ext}")
19+
elif name == "pval_file":
20+
return os.path.join(pth, f"{fname}_pval{ext}")
21+
elif name == "size_file":
22+
return os.path.join(pth, f"{fname}_size{ext}")
23+
elif name == "threshold_file":
24+
return os.path.join(pth, f"{fname}_threshold{ext}")
625

7-
if in_file not in [None, attr.NOTHING]:
8-
pth, fname, ext = split_filename(in_file)
9-
return f"{fname}_localmax.txt"
1026
else:
11-
raise Exception(f"this function should be run only for out_localmax_txt_file not {name}")
27+
raise Exception(f"this function should be run only for index_file, localmax_txt_file, localmax_vol_file, max_file, mean_file, pval_file, size_file, or threshold_file not {name}")
1228

1329

1430
def Complex_output(inputs):

0 commit comments

Comments
 (0)