Skip to content

Commit 00c668d

Browse files
committed
fixing output spec for bosh; changing output_file_template so it works for templates with or w/o extensions
1 parent 7576fdc commit 00c668d

File tree

3 files changed

+40
-9
lines changed

3 files changed

+40
-9
lines changed

pydra/engine/boutiques.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import subprocess as sp
55
import os
66
from pathlib import Path
7+
from functools import reduce
78

89
from ..utils.messenger import AuditFlag
910
from ..engine import ShellCommandTask
@@ -110,6 +111,7 @@ def _download_spec(self, zenodo):
110111
def _prepare_input_spec(self):
111112
""" creating input spec from the zenodo file"""
112113
binputs = self.bosh_spec["inputs"]
114+
self._input_spec_keys = {}
113115
fields = []
114116
for input in binputs:
115117
name = input["id"]
@@ -133,6 +135,7 @@ def _prepare_input_spec(self):
133135
"argstr": input.get("command-line-flag", None),
134136
}
135137
fields.append((name, tp, mdata))
138+
self._input_spec_keys[input["value-key"]] = "{" + f"{name}" + "}"
136139

137140
spec = SpecInfo(name="Inputs", fields=fields, bases=(ShellSpec,))
138141
return spec
@@ -143,14 +146,17 @@ def _prepare_output_spec(self):
143146
fields = []
144147
for output in boutputs:
145148
name = output["id"]
149+
path_template = reduce(
150+
lambda s, r: s.replace(*r),
151+
self._input_spec_keys.items(),
152+
output["path-template"],
153+
)
146154
mdata = {
147155
"help_string": output["description"],
148156
"mandatory": not output["optional"],
157+
"output_file_template": path_template,
149158
}
150-
# TODO NOW: temp. default value
151-
fields.append(
152-
(name, attr.ib(type=File, default="test_brain.nii.gz", metadata=mdata))
153-
)
159+
fields.append((name, attr.ib(type=File, metadata=mdata)))
154160

155161
spec = SpecInfo(name="Outputs", fields=fields, bases=(ShellOutSpec,))
156162
return spec

pydra/engine/specs.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -359,9 +359,23 @@ def _field_metadata(self, fld, inputs, output_dir):
359359
if "value" in fld.metadata:
360360
return output_dir / fld.metadata["value"]
361361
elif "output_file_template" in fld.metadata:
362-
return output_dir / fld.metadata["output_file_template"].format(
363-
**inputs.__dict__
362+
sfx_tmpl = (output_dir / fld.metadata["output_file_template"]).suffixes
363+
if sfx_tmpl:
364+
# removing suffix from input field if template has it's own suffix
365+
inputs_templ = {
366+
k: v.split(".")[0]
367+
for k, v in inputs.__dict__.items()
368+
if isinstance(v, str)
369+
}
370+
else:
371+
inputs_templ = {
372+
k: v for k, v in inputs.__dict__.items() if isinstance(v, str)
373+
}
374+
out_path = output_dir / fld.metadata["output_file_template"].format(
375+
**inputs_templ
364376
)
377+
return out_path
378+
365379
elif "callable" in fld.metadata:
366380
return fld.metadata["callable"](fld.name, output_dir)
367381
else:

pydra/engine/tests/test_boutiques.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,20 @@
1313

1414

1515
@need_bosh_docker
16-
def test_boutiques():
16+
@pytest.mark.parametrize(
17+
"maskfile", ["test_brain.nii.gz", "test_brain", "test_brain.nii"]
18+
)
19+
def test_boutiques_1(maskfile):
1720
btask = BoshTask(name="NA", zenodo="zenodo.1482743")
1821
btask.inputs.infile = Path(__file__).resolve().parent / "data_tests" / "test.nii.gz"
19-
btask.inputs.maskfile = "test_brain.nii.gz"
22+
btask.inputs.maskfile = maskfile
2023
res = btask()
21-
# breakpoint()
24+
25+
assert res.output.return_code == 0
26+
27+
# checking if the outfile exists and if it has proper name
28+
assert res.output.outfile.name == "test_brain.nii.gz"
29+
assert res.output.outfile.exists()
30+
# other files should also have proper names, but they do not exist
31+
assert res.output.out_outskin_off.name == "test_brain_outskin_mesh.off"
32+
assert not res.output.out_outskin_off.exists()

0 commit comments

Comments
 (0)