|
4 | 4 | "cell_type": "markdown",
|
5 | 5 | "metadata": {},
|
6 | 6 | "source": [
|
7 |
| - "# Canonical task form\n", |
| 7 | + "# Canonical form and serialisation\n", |
| 8 | + "\n", |
| 9 | + "## Canonical task form\n", |
8 | 10 | "\n",
|
9 | 11 | "Under the hood, all Python, shell and workflow tasks generated by the\n",
|
10 | 12 | "`pydra.compose.*.define` decorators/functions are translated to\n",
|
|
149 | 151 | "from pathlib import Path\n",
|
150 | 152 | "from fileformats import generic\n",
|
151 | 153 | "from pydra.compose import shell\n",
|
152 |
| - "from pydra.utils.typing import MultiInputObj\n", |
| 154 | + "\n", |
| 155 | + "\n", |
| 156 | + "# the \"copied\" output is magically passed to this function because the name matches\n", |
| 157 | + "def get_file_size(copied: Path) -> int:\n", |
| 158 | + " \"\"\"Calculate the file size\"\"\"\n", |
| 159 | + " result = os.stat(copied)\n", |
| 160 | + " return result.st_size\n", |
153 | 161 | "\n",
|
154 | 162 | "\n",
|
155 | 163 | "@shell.define\n",
|
156 |
| - "class CpWithSize(shell.Task[\"CpWithSize.Outputs\"]):\n", |
| 164 | + "class CpFileWithSize(shell.Task[\"CpFileWithSize.Outputs\"]):\n", |
157 | 165 | "\n",
|
158 | 166 | " executable = \"cp\"\n",
|
159 | 167 | "\n",
|
160 |
| - " in_fs_objects: MultiInputObj[generic.FsObject]\n", |
161 |
| - " recursive: bool = shell.arg(argstr=\"-R\")\n", |
162 |
| - " text_arg: str = shell.arg(argstr=\"--text-arg\")\n", |
163 |
| - " int_arg: int | None = shell.arg(argstr=\"--int-arg\")\n", |
164 |
| - " tuple_arg: tuple[int, str] | None = shell.arg(argstr=\"--tuple-arg\")\n", |
| 168 | + " in_file: generic.File # = shell.arg() is assumed\n", |
| 169 | + " archive_mode: bool = shell.arg(argstr=\"-a\", default=False)\n", |
165 | 170 | "\n",
|
166 | 171 | " class Outputs(shell.Outputs):\n",
|
167 | 172 | "\n",
|
168 |
| - " @staticmethod\n", |
169 |
| - " def get_file_size(out_file: Path) -> int:\n", |
170 |
| - " \"\"\"Calculate the file size\"\"\"\n", |
171 |
| - " result = os.stat(out_file)\n", |
172 |
| - " return result.st_size\n", |
| 173 | + " copied: generic.File = shell.outarg(\n", |
| 174 | + " position=-1, path_template=\"{in_file}_copied\"\n", |
| 175 | + " )\n", |
| 176 | + " file_size: int = shell.out(callable=get_file_size)\n", |
173 | 177 | "\n",
|
174 |
| - " copied: generic.FsObject = shell.outarg(path_template=\"copied\")\n", |
175 |
| - " out_file_size: int = shell.out(callable=get_file_size)\n", |
176 | 178 | "\n",
|
177 |
| - "\n", |
178 |
| - "print_help(CpWithSize)" |
| 179 | + "print_help(CpFileWithSize)" |
179 | 180 | ]
|
180 | 181 | },
|
181 | 182 | {
|
|
197 | 198 | "import typing as ty\n",
|
198 | 199 | "import re\n",
|
199 | 200 | "from pydra.compose import python, workflow\n",
|
200 |
| - "from pydra.compose.base import is_set\n", |
201 | 201 | "from pydra.utils import print_help, show_workflow\n",
|
202 | 202 | "\n",
|
203 | 203 | "\n",
|
|
237 | 237 | "print_help(CanonicalWorkflowTask)\n",
|
238 | 238 | "show_workflow(CanonicalWorkflowTask)"
|
239 | 239 | ]
|
| 240 | + }, |
| 241 | + { |
| 242 | + "cell_type": "markdown", |
| 243 | + "metadata": {}, |
| 244 | + "source": [ |
| 245 | + "## Serialization\n", |
| 246 | + "\n", |
| 247 | + "As well as the dataclass-like canonical form, it is also possible to represent all tasks\n", |
| 248 | + "in a nested dictionary form, which could be written to a static file (e.g. in JSON or\n", |
| 249 | + "YAML format). The dictionary form of a class can be generated by the `pydra.utils.unstructure`\n", |
| 250 | + "function. For example, the following shell command" |
| 251 | + ] |
| 252 | + }, |
| 253 | + { |
| 254 | + "cell_type": "code", |
| 255 | + "execution_count": null, |
| 256 | + "metadata": {}, |
| 257 | + "outputs": [], |
| 258 | + "source": [ |
| 259 | + "MyCmd = shell.define(\n", |
| 260 | + " \"my-cmd <in_file> <out|out_file> --an-arg <an_arg?> \"\n", |
| 261 | + " \"--a-flag<a_flag> --arg-with-default <arg_with_default:int=3>\"\n", |
| 262 | + ")\n", |
| 263 | + "\n", |
| 264 | + "print_help(MyCmd)" |
| 265 | + ] |
| 266 | + }, |
| 267 | + { |
| 268 | + "cell_type": "markdown", |
| 269 | + "metadata": {}, |
| 270 | + "source": [ |
| 271 | + "Can be converted into a serialised dictionary form" |
| 272 | + ] |
| 273 | + }, |
| 274 | + { |
| 275 | + "cell_type": "code", |
| 276 | + "execution_count": null, |
| 277 | + "metadata": {}, |
| 278 | + "outputs": [], |
| 279 | + "source": [ |
| 280 | + "from pprint import pprint\n", |
| 281 | + "from pydra.utils import unstructure\n", |
| 282 | + "\n", |
| 283 | + "my_cmd_dict = unstructure(MyCmd)\n", |
| 284 | + "\n", |
| 285 | + "pprint(my_cmd_dict)" |
| 286 | + ] |
| 287 | + }, |
| 288 | + { |
| 289 | + "cell_type": "markdown", |
| 290 | + "metadata": {}, |
| 291 | + "source": [ |
| 292 | + "Noting that there is still a little more work has to be done to serialise some Python\n", |
| 293 | + "objects, e.g. classes used in field types and functions that are run in Python and\n", |
| 294 | + "construct workflows in workflow tasks, before the serialized form can be written to JSON/YAML." |
| 295 | + ] |
| 296 | + }, |
| 297 | + { |
| 298 | + "cell_type": "code", |
| 299 | + "execution_count": null, |
| 300 | + "metadata": {}, |
| 301 | + "outputs": [], |
| 302 | + "source": [ |
| 303 | + "cp_with_size_dict = unstructure(CpFileWithSize)\n", |
| 304 | + "\n", |
| 305 | + "pprint(cp_with_size_dict)" |
| 306 | + ] |
| 307 | + }, |
| 308 | + { |
| 309 | + "cell_type": "markdown", |
| 310 | + "metadata": {}, |
| 311 | + "source": [ |
| 312 | + "To unserialize the general dictionary form back into a Task class, you can use the\n", |
| 313 | + "`pydra.utils.structure` method" |
| 314 | + ] |
| 315 | + }, |
| 316 | + { |
| 317 | + "cell_type": "code", |
| 318 | + "execution_count": null, |
| 319 | + "metadata": {}, |
| 320 | + "outputs": [], |
| 321 | + "source": [ |
| 322 | + "from pydra.utils import structure\n", |
| 323 | + "\n", |
| 324 | + "ReloadedCpFileWithSize = structure(cp_with_size_dict)" |
| 325 | + ] |
| 326 | + }, |
| 327 | + { |
| 328 | + "cell_type": "markdown", |
| 329 | + "metadata": {}, |
| 330 | + "source": [ |
| 331 | + "which should run just as before" |
| 332 | + ] |
| 333 | + }, |
| 334 | + { |
| 335 | + "cell_type": "code", |
| 336 | + "execution_count": null, |
| 337 | + "metadata": {}, |
| 338 | + "outputs": [], |
| 339 | + "source": [ |
| 340 | + "from pathlib import Path\n", |
| 341 | + "import tempfile\n", |
| 342 | + "from pydra.utils import asdict\n", |
| 343 | + "\n", |
| 344 | + "tmp_dir = Path(tempfile.mkdtemp())\n", |
| 345 | + "\n", |
| 346 | + "a_file = tmp_dir / \"hello-world.txt\"\n", |
| 347 | + "a_file.write_text(\"Hello world\")\n", |
| 348 | + "\n", |
| 349 | + "cp_file_with_size = ReloadedCpFileWithSize(in_file=a_file)\n", |
| 350 | + "outputs = cp_file_with_size(cache_root=tmp_dir / \"cache\")\n", |
| 351 | + "\n", |
| 352 | + "pprint(asdict(outputs))" |
| 353 | + ] |
240 | 354 | }
|
241 | 355 | ],
|
242 | 356 | "metadata": {
|
|
0 commit comments