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

[rfc]changing Task initialization syntax closes #295 #305

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
5 changes: 2 additions & 3 deletions pydra/engine/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,6 @@ def __init__(

if TaskBase._etelemetry_version_data is None:
TaskBase._etelemetry_version_data = check_latest_version()

# raise error if name is same as of attributes
if name in dir(self):
raise ValueError("Cannot use names of attributes or methods as task name")
Expand Down Expand Up @@ -166,7 +165,6 @@ def __init__(
self.inputs = attr.evolve(self.inputs, **inputs)
self.inputs.check_metadata()
self.state_inputs = inputs

self.audit = Audit(
audit_flags=audit_flags,
messengers=messengers,
Expand Down Expand Up @@ -664,6 +662,7 @@ class Workflow(TaskBase):
def __init__(
self,
name,
inputs: ty.Optional[ty.Union[ty.Text, File, ty.Dict]] = None,
audit_flags: AuditFlag = AuditFlag.NONE,
cache_dir=None,
cache_locations=None,
Expand Down Expand Up @@ -730,7 +729,7 @@ def __init__(

super(Workflow, self).__init__(
name=name,
inputs=kwargs,
inputs=inputs,
cache_dir=cache_dir,
cache_locations=cache_locations,
audit_flags=audit_flags,
Expand Down
2 changes: 1 addition & 1 deletion pydra/engine/specs.py
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,7 @@ def _file_check(self, field):
pass
# if this is a local path, checking if the path exists
elif file.exists():
if self.bindings is None:
if self.bindings in [None, attr.NOTHING]:
self.bindings = []
self.bindings.append((file.parent, f"/pydra_inp_{field.name}", "ro"))
else:
Expand Down
27 changes: 23 additions & 4 deletions pydra/engine/task.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ class FunctionTask(TaskBase):
def __init__(
self,
func: ty.Callable,
inputs: ty.Optional[ty.Union[ty.Text, File, ty.Dict]] = None,
audit_flags: AuditFlag = AuditFlag.NONE,
cache_dir=None,
cache_locations=None,
Expand Down Expand Up @@ -138,7 +139,7 @@ def __init__(
name = func.__name__
super(FunctionTask, self).__init__(
name,
inputs=kwargs,
inputs=inputs,
audit_flags=audit_flags,
messengers=messengers,
messenger_args=messenger_args,
Expand Down Expand Up @@ -244,6 +245,9 @@ def __new__(cls, container_info=None, *args, **kwargs):

def __init__(
self,
inputs: ty.Optional[ty.Union[ty.Text, File, ty.Dict]] = None,
executable: ty.Optional[ty.Union[ty.List, str]] = None,
args: ty.Optional[ty.Union[ty.List, str, File]] = None,
audit_flags: AuditFlag = AuditFlag.NONE,
cache_dir=None,
input_spec: ty.Optional[SpecInfo] = None,
Expand Down Expand Up @@ -283,12 +287,21 @@ def __init__(
self.input_spec = input_spec
if output_spec is None:
output_spec = SpecInfo(name="Output", fields=[], bases=(ShellOutSpec,))
if inputs is None:
inputs = {}
# adding executable and args to inputs
inputs["executable"] = executable
inputs["args"] = args
if isinstance(self, ContainerTask):
# adding image and bindings when ContainerTask
inputs["image"] = kwargs["image"]
inputs["bindings"] = kwargs["bindings"]

self.output_spec = output_spec

super(ShellCommandTask, self).__init__(
name=name,
inputs=kwargs,
inputs=inputs,
audit_flags=audit_flags,
messengers=messengers,
messenger_args=messenger_args,
Expand Down Expand Up @@ -350,6 +363,8 @@ def _command_args_single(self, state_ind, ind=None):
if f.type is bool:
if value is not True:
break
elif value in [None, attr.NOTHING]:
continue
else:
cmd_add += ensure_list(value, tuple2list=True)
if cmd_add is not None:
Expand Down Expand Up @@ -419,6 +434,8 @@ class ContainerTask(ShellCommandTask):
def __init__(
self,
name,
image: ty.Optional[str] = None,
bindings: ty.Optional[ty.List] = None,
audit_flags: AuditFlag = AuditFlag.NONE,
cache_dir=None,
input_spec: ty.Optional[SpecInfo] = None,
Expand Down Expand Up @@ -468,6 +485,8 @@ def __init__(
cache_dir=cache_dir,
strip=strip,
rerun=rerun,
image=image,
bindings=bindings,
**kwargs,
)

Expand All @@ -479,14 +498,14 @@ def container_check(self, container_type):
raise AttributeError(
f"Container type should be {container_type}, but {self.inputs.container} given"
)
if self.inputs.image is attr.NOTHING:
if self.inputs.image in [None, attr.NOTHING]:
raise AttributeError("Container image is not specified")

def bind_paths(self, ind=None):
"""Return bound mount points: ``dict(lpath: (cpath, mode))``."""
bind_paths = {}
output_dir_cpath = None
if self.inputs.bindings is None:
if self.inputs.bindings in [None, attr.NOTHING]:
self.inputs.bindings = []
if ind is None:
output_dir = self.output_dir
Expand Down
16 changes: 7 additions & 9 deletions pydra/engine/tests/test_boutiques.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,7 @@ def test_boutiques_spec_1():
btask = BoshTask(
name="NA",
zenodo_id="1482743",
infile=Infile,
maskfile="test_brain.nii.gz",
inputs={"infile": Infile, "maskfile": "test_brain.nii.gz"},
input_spec_names=["infile", "maskfile"],
output_spec_names=["outfile", "out_outskin_off"],
)
Expand All @@ -76,8 +75,7 @@ def test_boutiques_spec_2():
btask = BoshTask(
name="NA",
zenodo_id="1482743",
infile=Infile,
maskfile="test_brain.nii.gz",
inputs={"infile": Infile, "maskfile": "test_brain.nii.gz"},
input_spec_names=["infile"],
output_spec_names=[],
)
Expand Down Expand Up @@ -107,8 +105,7 @@ def test_boutiques_wf_1(maskfile, plugin):
BoshTask(
name="bet",
zenodo_id="1482743",
infile=wf.lzin.infile,
maskfile=wf.lzin.maskfile,
inputs={"infile": wf.lzin.infile, "maskfile": wf.lzin.maskfile},
)
)

Expand Down Expand Up @@ -138,13 +135,14 @@ def test_boutiques_wf_2(maskfile, plugin):
BoshTask(
name="bet",
zenodo_id="1482743",
infile=wf.lzin.infile,
maskfile=wf.lzin.maskfile,
inputs={"infile": wf.lzin.infile, "maskfile": wf.lzin.maskfile},
)
)
wf.add(
BoshTask(
name="stat", zenodo_id="3240521", input_file=wf.bet.lzout.outfile, v=True
name="stat",
zenodo_id="3240521",
inputs={"input_file": wf.bet.lzout.outfile, "v": True},
)
)
wf.add(ShellCommandTask(name="cat", executable="cat", args=wf.stat.lzout.output))
Expand Down
24 changes: 12 additions & 12 deletions pydra/engine/tests/test_dockertask.py
Original file line number Diff line number Diff line change
Expand Up @@ -689,7 +689,7 @@ def test_docker_inputspec_1(plugin, tmpdir):
name="docky",
image="busybox",
executable=cmd,
file=filename,
inputs={"file": filename},
input_spec=my_input_spec,
strip=True,
)
Expand Down Expand Up @@ -776,7 +776,7 @@ def test_docker_inputspec_2(plugin, tmpdir):
name="docky",
image="busybox",
executable=cmd,
file1=filename_1,
inputs={"file1": filename_1},
input_spec=my_input_spec,
strip=True,
)
Expand Down Expand Up @@ -826,7 +826,7 @@ def test_docker_inputspec_2a_except(plugin, tmpdir):
name="docky",
image="busybox",
executable=cmd,
file2=filename_2,
inputs={"file2": filename_2},
input_spec=my_input_spec,
strip=True,
)
Expand Down Expand Up @@ -878,7 +878,7 @@ def test_docker_inputspec_2a(plugin, tmpdir):
name="docky",
image="busybox",
executable=cmd,
file2=filename_2,
inputs={"file2": filename_2},
input_spec=my_input_spec,
strip=True,
)
Expand Down Expand Up @@ -919,7 +919,7 @@ def test_docker_inputspec_3(plugin, tmpdir):
name="docky",
image="busybox",
executable=cmd,
file=filename,
inputs={"file": filename},
input_spec=my_input_spec,
strip=True,
)
Expand Down Expand Up @@ -963,7 +963,7 @@ def test_docker_inputspec_3a(plugin, tmpdir):
name="docky",
image="busybox",
executable=cmd,
file=filename,
inputs={"file": filename},
input_spec=my_input_spec,
strip=True,
)
Expand Down Expand Up @@ -1020,7 +1020,7 @@ def test_docker_cmd_inputspec_copyfile_1(plugin, tmpdir):
image="busybox",
executable=cmd,
input_spec=my_input_spec,
orig_file=str(file),
inputs={"orig_file": str(file)},
)

res = docky()
Expand Down Expand Up @@ -1073,7 +1073,7 @@ def test_docker_inputspec_state_1(plugin, tmpdir):
name="docky",
image="busybox",
executable=cmd,
file=filename,
inputs={"file": filename},
input_spec=my_input_spec,
strip=True,
).split("file")
Expand Down Expand Up @@ -1122,7 +1122,7 @@ def test_docker_inputspec_state_1b(plugin, tmpdir):
name="docky",
image="busybox",
executable=cmd,
file=filename,
inputs={"file": filename},
input_spec=my_input_spec,
strip=True,
).split("file")
Expand Down Expand Up @@ -1168,7 +1168,7 @@ def test_docker_wf_inputspec_1(plugin, tmpdir):
name="docky",
image="busybox",
executable=wf.lzin.cmd,
file=wf.lzin.file,
inputs={"file": wf.lzin.file},
input_spec=my_input_spec,
strip=True,
)
Expand Down Expand Up @@ -1223,7 +1223,7 @@ def test_docker_wf_state_inputspec_1(plugin, tmpdir):
name="docky",
image="busybox",
executable=wf.lzin.cmd,
file=wf.lzin.file,
inputs={"file": wf.lzin.file},
input_spec=my_input_spec,
strip=True,
)
Expand Down Expand Up @@ -1280,7 +1280,7 @@ def test_docker_wf_ndst_inputspec_1(plugin, tmpdir):
name="docky",
image="busybox",
executable=wf.lzin.cmd,
file=wf.lzin.file,
inputs={"file": wf.lzin.file},
input_spec=my_input_spec,
strip=True,
).split("file")
Expand Down
10 changes: 5 additions & 5 deletions pydra/engine/tests/test_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def test_save(tmpdir):
outdir = Path(tmpdir)
with pytest.raises(ValueError):
save(tmpdir)
foo = multiply(name="mult", x=1, y=2)
foo = multiply(name="mult", inputs={"x": 1, "y": 2})
# save task
save(outdir, task=foo)
del foo
Expand Down Expand Up @@ -203,7 +203,7 @@ def test_load_and_run(tmpdir):
""" testing load_and_run for pickled task"""
task_pkl = Path(tmpdir.join("task_main.pkl"))

task = multiply(name="mult", x=[1, 2], y=10).split("x")
task = multiply(name="mult", inputs={"x": [1, 2], "y": 10}).split("x")
task.state.prepare_states(inputs=task.inputs)
task.state.prepare_inputs()
with task_pkl.open("wb") as fp:
Expand All @@ -221,7 +221,7 @@ def test_load_and_run(tmpdir):
def test_load_and_run_exception_load(tmpdir):
""" testing raising exception and saving info in crashfile when when load_and_run"""
task_pkl = Path(tmpdir.join("task_main.pkl"))
task = raise_xeq1(name="raise", x=[1, 2]).split("x")
task = raise_xeq1(name="raise", inputs={"x": [1, 2]}).split("x")
with pytest.raises(FileNotFoundError) as excinfo:
task_0 = load_and_run(task_pkl=task_pkl, ind=0)

Expand All @@ -230,7 +230,7 @@ def test_load_and_run_exception_run(tmpdir):
""" testing raising exception and saving info in crashfile when when load_and_run"""
task_pkl = Path(tmpdir.join("task_main.pkl"))

task = raise_xeq1(name="raise", x=[1, 2]).split("x")
task = raise_xeq1(name="raise", inputs={"x": [1, 2]}).split("x")
task.state.prepare_states(inputs=task.inputs)
task.state.prepare_inputs()

Expand Down Expand Up @@ -262,7 +262,7 @@ def test_load_and_run_wf(tmpdir):
wf_pkl = Path(tmpdir.join("wf_main.pkl"))

wf = Workflow(name="wf", input_spec=["x", "y"])
wf.add(multiply(name="mult", x=wf.lzin.x, y=wf.lzin.y))
wf.add(multiply(name="mult", inputs={"x": wf.lzin.x, "y": wf.lzin.y}))
wf.split(("x"))
wf.inputs.x = [1, 2]
wf.inputs.y = 10
Expand Down
Loading