Skip to content

Commit c8cb96c

Browse files
committed
debugged and formatted tutorial notebooks
1 parent bb545c1 commit c8cb96c

File tree

6 files changed

+50
-33
lines changed

6 files changed

+50
-33
lines changed

new-docs/source/tutorial/3-troubleshooting.ipynb

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
"source": [
3737
"# This is needed to run parallel workflows in Jupyter notebooks\n",
3838
"import nest_asyncio\n",
39+
"\n",
3940
"nest_asyncio.apply()"
4041
]
4142
},
@@ -126,14 +127,14 @@
126127
"outputs": [],
127128
"source": [
128129
"# This workflow will fail because we are trying to divide by 0\n",
129-
"wf = UnsafeDivisionWorkflow(a=10, b=5).split(denominator=[3, 2 ,0])\n",
130+
"wf = UnsafeDivisionWorkflow(a=10, b=5).split(denominator=[3, 2, 0])\n",
130131
"\n",
131132
"if __name__ == \"__main__\":\n",
132133
" try:\n",
133134
" with Submitter(worker=\"cf\") as sub:\n",
134135
" result = sub(wf)\n",
135136
" except Exception as e:\n",
136-
" print(e)\n"
137+
" print(e)"
137138
]
138139
},
139140
{
@@ -156,7 +157,9 @@
156157
"from pprint import pprint\n",
157158
"from pydra.tasks.testing import Divide\n",
158159
"\n",
159-
"with open(default_run_cache_dir / Divide(x=15, y=0)._checksum / \"_error.pklz\", \"rb\") as f:\n",
160+
"with open(\n",
161+
" default_run_cache_dir / Divide(x=15, y=0)._checksum / \"_error.pklz\", \"rb\"\n",
162+
") as f:\n",
160163
" error = cp.load(f)\n",
161164
"\n",
162165
"pprint(error)"
@@ -196,12 +199,12 @@
196199
"from pydra.engine.submitter import Submitter\n",
197200
"from pydra.tasks.testing import SafeDivisionWorkflow\n",
198201
"\n",
199-
"wf = SafeDivisionWorkflow(a=10, b=5).split(denominator=[3, 2 ,0])\n",
202+
"wf = SafeDivisionWorkflow(a=10, b=5).split(denominator=[3, 2, 0])\n",
200203
"\n",
201204
"if __name__ == \"__main__\":\n",
202205
" with Submitter(worker=\"cf\") as sub:\n",
203206
" result = sub(wf)\n",
204-
" \n",
207+
"\n",
205208
"print(f\"Workflow completed successfully, results saved in: {result.output_dir}\")"
206209
]
207210
},
@@ -229,9 +232,10 @@
229232
" result = cp.load(f)\n",
230233
" if result.outputs is not None:\n",
231234
" for field_name in result.outputs:\n",
232-
" if result.outputs[field_name] == float('nan'):\n",
233-
" print(f\"Task {task_cache_dir.name!r} produced a NaN value for {field_name!r}\")\n",
234-
" "
235+
" if result.outputs[field_name] == float(\"nan\"):\n",
236+
" print(\n",
237+
" f\"Task {task_cache_dir.name!r} produced a NaN value for {field_name!r}\"\n",
238+
" )"
235239
]
236240
}
237241
],

new-docs/source/tutorial/5-shell.ipynb

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -260,12 +260,14 @@
260260
" \"--int-arg <int_arg:int?> \"\n",
261261
" \"--tuple-arg <tuple_arg:int,str> \"\n",
262262
" ),\n",
263-
" inputs={\"recursive\": shell.arg(\n",
264-
" help=(\n",
265-
" \"If source_file designates a directory, cp copies the directory and \"\n",
266-
" \"the entire subtree connected at that point.\"\n",
263+
" inputs={\n",
264+
" \"recursive\": shell.arg(\n",
265+
" help=(\n",
266+
" \"If source_file designates a directory, cp copies the directory and \"\n",
267+
" \"the entire subtree connected at that point.\"\n",
268+
" )\n",
267269
" )\n",
268-
" )},\n",
270+
" },\n",
269271
" outputs={\n",
270272
" \"out_dir\": shell.outarg(position=-2),\n",
271273
" \"out_file\": shell.outarg(position=-1),\n",
@@ -297,7 +299,8 @@
297299
"from pathlib import Path\n",
298300
"from fileformats.generic import File\n",
299301
"\n",
300-
"# Arguments to the callable function can be one of \n",
302+
"\n",
303+
"# Arguments to the callable function can be one of\n",
301304
"def get_file_size(out_file: Path) -> int:\n",
302305
" \"\"\"Calculate the file size\"\"\"\n",
303306
" result = os.stat(out_file)\n",
@@ -356,6 +359,7 @@
356359
"from pydra.utils.typing import MultiInputObj\n",
357360
"from fileformats.generic import FsObject, Directory\n",
358361
"\n",
362+
"\n",
359363
"@shell.define\n",
360364
"class Cp(ShellDef[\"Cp.Outputs\"]):\n",
361365
"\n",
@@ -369,7 +373,7 @@
369373
"\n",
370374
" @shell.outputs\n",
371375
" class Outputs(ShellOutputs):\n",
372-
" out_dir: Directory = shell.outarg(path_template=\"{out_dir}\")\n"
376+
" out_dir: Directory = shell.outarg(path_template=\"{out_dir}\")"
373377
]
374378
},
375379
{
@@ -396,20 +400,18 @@
396400
" \"in_file\": shell.arg(type=File, help=\"output file\", argstr=\"\", position=-2)\n",
397401
" },\n",
398402
" outputs={\n",
399-
" \"out_file\": shell.outarg(\n",
400-
" type=File, help=\"output file\", argstr=\"\", position=-1\n",
401-
" ),\n",
403+
" \"out_file\": shell.outarg(type=File, help=\"output file\", argstr=\"\", position=-1),\n",
402404
" \"out_file_size\": {\n",
403405
" \"type\": int,\n",
404406
" \"help\": \"size of the output directory\",\n",
405407
" \"callable\": get_file_size,\n",
406-
" }\n",
408+
" },\n",
407409
" },\n",
408410
")\n",
409411
"\n",
410412
"\n",
411413
"print(f\"ACommand input fields: {list_fields(ACommand)}\")\n",
412-
"print(f\"ACommand input fields: {list_fields(ACommand.Outputs)}\")\n"
414+
"print(f\"ACommand input fields: {list_fields(ACommand.Outputs)}\")"
413415
]
414416
},
415417
{

new-docs/source/tutorial/6-workflow.ipynb

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
"source": [
3131
"from pydra.design import workflow, python\n",
3232
"\n",
33+
"\n",
3334
"# Example python task definitions\n",
3435
"@python.define\n",
3536
"def Add(a, b):\n",
@@ -83,6 +84,7 @@
8384
"from pydra.design import shell\n",
8485
"from fileformats import image, video\n",
8586
"\n",
87+
"\n",
8688
"@workflow.define\n",
8789
"def ShellWorkflow(\n",
8890
" input_video: video.Mp4,\n",
@@ -129,6 +131,7 @@
129131
"def Sum(x: list[float]) -> float:\n",
130132
" return sum(x)\n",
131133
"\n",
134+
"\n",
132135
"@workflow.define\n",
133136
"def SplitWorkflow(a: list[int], b: list[float]) -> list[float]:\n",
134137
" # Multiply over all combinations of the elements of a and b, then combine the results\n",
@@ -232,6 +235,7 @@
232235
"def Subtract(x: float, y: float) -> float:\n",
233236
" return x - y\n",
234237
"\n",
238+
"\n",
235239
"@workflow.define\n",
236240
"def RecursiveNestedWorkflow(a: float, depth: int) -> float:\n",
237241
" add = workflow.add(Add(x=a, y=1))\n",
@@ -292,6 +296,7 @@
292296
" \"--width <width:int> --height <height:int>\",\n",
293297
")\n",
294298
"\n",
299+
"\n",
295300
"@workflow.define\n",
296301
"def TypeErrorWorkflow(\n",
297302
" input_video: video.Mp4,\n",
@@ -313,7 +318,9 @@
313318
"\n",
314319
" try:\n",
315320
" handbrake = workflow.add(\n",
316-
" QuicktimeHandbrake(in_video=add_watermark.out_video, width=1280, height=720),\n",
321+
" QuicktimeHandbrake(\n",
322+
" in_video=add_watermark.out_video, width=1280, height=720\n",
323+
" ),\n",
317324
" ) # This will raise a TypeError because the input video is an Mp4\n",
318325
" except TypeError:\n",
319326
" handbrake = workflow.add(\n",

new-docs/source/tutorial/7-canonical-form.ipynb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@
7979
" def function(a, b):\n",
8080
" return a + b, a / b\n",
8181
"\n",
82+
"\n",
8283
"pprint(fields_dict(CanonicalPythonDef))\n",
8384
"pprint(fields_dict(CanonicalPythonDef.Outputs))"
8485
]
@@ -127,6 +128,7 @@
127128
" def function(a, b):\n",
128129
" return a + b, a / b\n",
129130
"\n",
131+
"\n",
130132
"pprint(fields_dict(CanonicalPythonDef))\n",
131133
"pprint(fields_dict(CanonicalPythonDef.Outputs))"
132134
]
@@ -174,7 +176,7 @@
174176
" result = os.stat(out_file)\n",
175177
" return result.st_size\n",
176178
"\n",
177-
" out_file: generic.File\n",
179+
" copied: generic.FsObject = shell.outarg(path_template=\"copied\")\n",
178180
" out_file_size: int = shell.out(callable=get_file_size)\n",
179181
"\n",
180182
"\n",
@@ -201,6 +203,7 @@
201203
"from pydra.design import python, workflow\n",
202204
"from pydra.engine.specs import WorkflowDef, WorkflowOutputs\n",
203205
"\n",
206+
"\n",
204207
"# Example python task definitions\n",
205208
"@python.define\n",
206209
"def Add(a, b):\n",

pydra/engine/helpers.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -662,8 +662,12 @@ def fields_in_formatter(formatter: str | ty.Callable[..., str]) -> set[str]:
662662
"""Extract all field names from a formatter string or function."""
663663
if isinstance(formatter, str):
664664
return parse_format_string(formatter)
665-
else:
665+
elif isinstance(formatter, ty.Sequence):
666+
return set().union(*[fields_in_formatter(f) for f in formatter])
667+
elif isinstance(formatter, ty.Callable):
666668
return set(inspect.signature(formatter).parameters.keys())
669+
else:
670+
raise ValueError(f"Unsupported formatter type: {type(formatter)} ({formatter})")
667671

668672

669673
def ensure_list(obj, tuple2list=False):

pydra/engine/specs.py

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -298,16 +298,13 @@ def __call__(
298298
time_of_crash = result.errors["time of crash"]
299299
error_message = "\n".join(result.errors["error message"])
300300
else:
301-
time_of_crash = "UNKNOWN"
301+
time_of_crash = "UNKNOWN-TIME"
302302
error_message = "NOT RETRIEVED"
303303
raise RuntimeError(
304304
f"Task {self} failed @ {time_of_crash} with the "
305-
"following errors:\n"
306-
+ error_message
307-
+ (
308-
"To inspect, please load the pickled task object from here: "
309-
f"{result.output_dir}/_task.pklz"
310-
)
305+
f"following errors:\n{error_message}\n"
306+
"To inspect, please load the pickled task object from here: "
307+
f"{result.output_dir}/_task.pklz"
311308
)
312309
return result.outputs
313310

@@ -835,9 +832,9 @@ def _from_task(cls, task: "Task[WorkflowDef]") -> Self:
835832
time_of_crash = "UNKNOWN-TIME"
836833
error_message = "NOT RETRIEVED"
837834
errors.append(
838-
f"Task {node.name!r} failed @ {time_of_crash} "
839-
f"running {node._definition} with the following errors:\n{error_message}"
840-
"To inspect, please load the pickled task object from here: "
835+
f"Task {node.name!r} failed @ {time_of_crash} running "
836+
f"{node._definition} with the following errors:\n{error_message}"
837+
"\nTo inspect, please load the pickled task object from here: "
841838
f"{result.output_dir}/_task.pklz"
842839
)
843840
raise RuntimeError(

0 commit comments

Comments
 (0)