Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

changing template formatting when value (or default) is False #561

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
7 changes: 7 additions & 0 deletions pydra/engine/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
File,
Directory,
attr_fields,
attr_field,
Result,
LazyField,
MultiOutputObj,
Expand Down Expand Up @@ -884,6 +885,12 @@ def argstr_formatting(argstr, inputs, value_updates=None):
if fld_value is attr.NOTHING:
# if value is NOTHING, nothing should be added to the command
val_dict[fld_name] = ""
# if value is False, but the field has a template the output field should not be created
elif (
fld_value is False
and "output_file_template" in attr_field(inputs, fld_name).metadata
):
val_dict[fld_name] = ""
else:
val_dict[fld_name] = fld_value

Expand Down
3 changes: 1 addition & 2 deletions pydra/engine/helpers_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -579,7 +579,6 @@ def template_update(inputs, output_dir, state_ind=None, map_copyfiles=None):
Should be run when all inputs used in the templates are already set.

"""

inputs_dict_st = attr.asdict(inputs, recurse=False)
if map_copyfiles is not None:
inputs_dict_st.update(map_copyfiles)
Expand Down Expand Up @@ -654,7 +653,7 @@ def template_update_single(
return inputs_dict_st[field.name]
elif spec_type == "input" and inputs_dict_st[field.name] is False:
# if input fld is set to False, the fld shouldn't be used (setting NOTHING)
return attr.NOTHING
return False
else: # inputs_dict[field.name] is True or spec_type is output
value = _template_formatting(field, inputs, inputs_dict_st)
# changing path so it is in the output_dir
Expand Down
4 changes: 4 additions & 0 deletions pydra/engine/specs.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ def attr_fields(spec, exclude_names=()):
return [field for field in spec.__attrs_attrs__ if field.name not in exclude_names]


def attr_field(spec, name):
return [field for field in spec.__attrs_attrs__ if field.name == name][0]


def attr_fields_dict(spec, exclude_names=()):
return {
field.name: field
Expand Down
8 changes: 6 additions & 2 deletions pydra/engine/task.py
Original file line number Diff line number Diff line change
Expand Up @@ -479,8 +479,12 @@ def _command_pos_args(self, field):
value = cmd_el_str
# if argstr has a more complex form, with "{input_field}"
if "{" in argstr and "}" in argstr:
cmd_el_str = argstr.replace(f"{{{field.name}}}", str(value))
cmd_el_str = argstr_formatting(cmd_el_str, self.inputs)
# output_file_template and argstr should not be created when the value is False
if not value and "output_file_template" in field.metadata:
cmd_el_str = ""
else:
cmd_el_str = argstr.replace(f"{{{field.name}}}", str(value))
cmd_el_str = argstr_formatting(cmd_el_str, self.inputs)
else: # argstr has a simple form, e.g. "-f", or "--f"
if value:
cmd_el_str = f"{argstr} {value}"
Expand Down
60 changes: 59 additions & 1 deletion pydra/engine/tests/test_shelltask_inputspec.py
Original file line number Diff line number Diff line change
Expand Up @@ -1152,6 +1152,8 @@ def test_shell_cmd_inputs_template_6():
executable="executable", input_spec=my_input_spec, inpA="inpA"
)
assert shelly.cmdline == f"executable inpA -o {str(shelly.output_dir / 'inpA_out')}"
# checking if the command is the same
assert shelly.cmdline == f"executable inpA -o {str(shelly.output_dir / 'inpA_out')}"

# a string is provided for outA, so this should be used as the outA value
shelly = ShellCommandTask(
Expand All @@ -1170,6 +1172,8 @@ def test_shell_cmd_inputs_template_6():
executable="executable", input_spec=my_input_spec, inpA="inpA", outA=False
)
assert shelly.cmdline == "executable inpA"
# checking of the command is the same
assert shelly.cmdline == "executable inpA"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@djarecka isn't line 1176 the same as the line 1174? shouldn't we add something like shelly.run() between 1174 and 1176?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

actually I realized that the run wasn't an issue when we were checking this last week, simply repeating cmdline was giving a new result, it was a bug... That's why I added this to the test...



def test_shell_cmd_inputs_template_6a():
Expand Down Expand Up @@ -1214,6 +1218,8 @@ def test_shell_cmd_inputs_template_6a():
executable="executable", input_spec=my_input_spec, inpA="inpA"
)
assert shelly.cmdline == "executable inpA"
# checking if the command is the same
assert shelly.cmdline == "executable inpA"

# a string is provided for outA, so this should be used as the outA value
shelly = ShellCommandTask(
Expand All @@ -1232,6 +1238,58 @@ def test_shell_cmd_inputs_template_6a():
executable="executable", input_spec=my_input_spec, inpA="inpA", outA=False
)
assert shelly.cmdline == "executable inpA"
# checking if the command is the same
assert shelly.cmdline == "executable inpA"


def test_shell_cmd_inputs_template_6b():
"""additional inputs with output_file_template that has type ty.Union[str, bool]
and default is set to False,
(using argstr in a format --argstr={})
"""
my_input_spec = SpecInfo(
name="Input",
fields=[
(
"inpA",
attr.ib(
type=str,
metadata={
"position": 1,
"help_string": "inpA",
"argstr": "",
"mandatory": True,
},
),
),
(
"outA",
attr.ib(
type=ty.Union[str, bool],
default=False,
metadata={
"position": 2,
"help_string": "outA",
"argstr": "--o={outA}",
"output_file_template": "{inpA}_out",
},
),
),
],
bases=(ShellSpec,),
)

# no input for outA, but default is False, so the outA shouldn't be used
shelly = ShellCommandTask(
executable="executable", input_spec=my_input_spec, inpA="inpA"
)
assert shelly.cmdline == "executable inpA"

# a string is provided for outA, so this should be used as the outA value
shelly = ShellCommandTask(
executable="executable", input_spec=my_input_spec, inpA="inpA", outA="outA"
)
assert shelly.cmdline == "executable inpA --o=outA"


def test_shell_cmd_inputs_template_7(tmpdir):
Expand Down Expand Up @@ -2080,7 +2138,7 @@ def test_shell_cmd_inputs_di(tmpdir, use_validator):
== f"DenoiseImage -i {tmpdir.join('a_file.ext')} -s 1 -p 1 -r 2 -o [{str(shelly.output_dir / 'a_file_out.ext')}]"
)

# input file name, noiseImage is set to True, so template is used in the output
# # input file name, noiseImage is set to True, so template is used in the output
shelly = ShellCommandTask(
executable="DenoiseImage",
inputImageFilename=my_input_file,
Expand Down