-
Notifications
You must be signed in to change notification settings - Fork 0
Adds a testing notebook to run hera files from a notebook #18
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
c97c330
feat(ipynb): adds example copy of notebook
Matt-Carre 467aef2
feat(ipynb): adds copier versions of notebooks
Matt-Carre 91152d2
feat(ipynb): clears up notebooks, adds jupyter to dev containers
Matt-Carre 6594a7b
feat(ipynb): updates copier and refactors notebooks
Matt-Carre 7478b81
docs(copier): removes division notebook template
Matt-Carre File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
251 changes: 251 additions & 0 deletions
251
...mplate/src/{{ project_name }}/workflow_definitions/notebooks/notebook_example.ipynb.jinja
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,251 @@ | ||
| {% raw %}{ | ||
| "cells": [ | ||
| { | ||
| "cell_type": "markdown", | ||
| "id": "1e3146f1", | ||
| "metadata": {}, | ||
| "source": [ | ||
| "# Running in VSCode:\n", | ||
| "\n", | ||
| "1. Set kernal to {% endraw %}{{repo_name}}{% raw %} 3.11.x\n", | ||
| "2. Hit F1, run Jupyter: Import Notebook to Script\n", | ||
| "3. Click notebook_example.ipynb\n", | ||
| "4. Run the cells sequentially" | ||
| ] | ||
| }, | ||
| { | ||
| "cell_type": "code", | ||
| "execution_count": null, | ||
| "id": "494174ef", | ||
| "metadata": {}, | ||
| "outputs": [], | ||
| "source": [ | ||
| "from hera.workflows import (\n", | ||
| " DAG,\n", | ||
| " Artifact,\n", | ||
| " Parameter,\n", | ||
| " Volume,\n", | ||
| " Workflow,\n", | ||
| " script, # pyright: ignore[reportUnknownVariableType]\n", | ||
| ")\n", | ||
| "from hera.workflows import models as m\n", | ||
| "from hera.workflows.archive import NoneArchiveStrategy\n", | ||
| "\n", | ||
| "\n", | ||
| "@script(\n", | ||
| " command=[\"python\"],\n", | ||
| " volume_mounts=[m.VolumeMount(name=\"tmpdir\", mount_path=\"/tmp\")],\n", | ||
| ")\n", | ||
| "def install_dependencies():\n", | ||
| " import subprocess\n", | ||
| "\n", | ||
| " print(\"creating venv\")\n", | ||
| "\n", | ||
| " subprocess.check_call([\"python\", \"-m\", \"venv\", \"/tmp/venv\"])\n", | ||
| " subprocess.check_call(\n", | ||
| " [\"/tmp/venv/bin/pip\", \"install\", \"pillow\", \"h5py\", \"numpy\", \"hera\"]\n", | ||
| " )\n", | ||
| "\n", | ||
| "\n", | ||
| "@script(\n", | ||
| " command=[\"python\"],\n", | ||
| " outputs=Parameter(\n", | ||
| " name=\"out-parameters\", value_from=m.ValueFrom(path=\"/tmp/parameters.json\")\n", | ||
| " ),\n", | ||
| " volume_mounts=[m.VolumeMount(name=\"tmpdir\", mount_path=\"/tmp\")],\n", | ||
| ")\n", | ||
| "def generate_parameters(\n", | ||
| " png: str,\n", | ||
| " jpg: str,\n", | ||
| " jpeg: str,\n", | ||
| " tif: str,\n", | ||
| " tiff: str,\n", | ||
| "):\n", | ||
| " import json\n", | ||
| "\n", | ||
| " params: list[dict[str, int | list[int] | str] | None] = [\n", | ||
| " {\"width\": 500, \"height\": 500, \"weights\": [255, 1, 100], \"extension\": \"png\"}\n", | ||
| " if png.lower() == \"true\"\n", | ||
| " else None,\n", | ||
| " {\"width\": 600, \"height\": 200, \"weights\": [100, 150, 100], \"extension\": \"jpg\"}\n", | ||
| " if jpg.lower() == \"true\"\n", | ||
| " else None,\n", | ||
| " {\"width\": 300, \"height\": 400, \"weights\": [100, 150, 100], \"extension\": \"jpeg\"}\n", | ||
| " if jpeg.lower() == \"true\"\n", | ||
| " else None,\n", | ||
| " {\"width\": 300, \"height\": 200, \"weights\": [230, 100, 1], \"extension\": \"tif\"}\n", | ||
| " if tif.lower() == \"true\"\n", | ||
| " else None,\n", | ||
| " {\"width\": 200, \"height\": 300, \"weights\": [230, 100, 1], \"extension\": \"tiff\"}\n", | ||
| " if tiff.lower() == \"true\"\n", | ||
| " else None,\n", | ||
| " ]\n", | ||
| " params_to_write: list[dict[str, int | list[int] | str]] = [\n", | ||
| " image_params for image_params in params if image_params is not None\n", | ||
| " ]\n", | ||
| " with open(\"/tmp/parameters.json\", \"w\") as f:\n", | ||
| " json.dump(params_to_write, f)\n", | ||
| "\n", | ||
| "\n", | ||
| "@script(\n", | ||
| " command=[\"/tmp/venv/bin/python\"],\n", | ||
| " volume_mounts=[m.VolumeMount(name=\"tmpdir\", mount_path=\"/tmp\")],\n", | ||
| " outputs=[\n", | ||
| " Parameter(\n", | ||
| " name=\"out-paths\",\n", | ||
| " value_from=m.ValueFrom(\n", | ||
| " path=\"/tmp/{{inputs.parameters.extension}}-path.json\"\n", | ||
| " ),\n", | ||
| " ),\n", | ||
| " Artifact(\n", | ||
| " name=\"{{inputs.parameters.extension}}-image\",\n", | ||
| " path=\"/tmp/{{inputs.parameters.extension}}-image.{{inputs.parameters.extension}}\",\n", | ||
| " archive=NoneArchiveStrategy(),\n", | ||
| " ),\n", | ||
| " ],\n", | ||
| ")\n", | ||
| "def create_image(\n", | ||
| " width: int, height: int, weights: tuple[int, int, int], extension: str\n", | ||
| "):\n", | ||
| " import json\n", | ||
| "\n", | ||
| " from PIL import Image\n", | ||
| "\n", | ||
| " def create_pattern(\n", | ||
| " width: int,\n", | ||
| " height: int,\n", | ||
| " weights: tuple[int, int, int],\n", | ||
| " ) -> Image.Image:\n", | ||
| " print(f\"width: {width}\")\n", | ||
| " print(f\"height: {height}\")\n", | ||
| " print(f\"RBG weights: {weights}\")\n", | ||
| " image = Image.new(\"RGB\", (width, height))\n", | ||
| " pixels = image.load()\n", | ||
| " for i in range(width):\n", | ||
| " for j in range(height):\n", | ||
| " pixels[i, j] = ( # pyright: ignore[reportOptionalSubscript]\n", | ||
| " (i + j * 50) % weights[0],\n", | ||
| " weights[1],\n", | ||
| " (i * 300 + j) % weights[2],\n", | ||
| " )\n", | ||
| " return image\n", | ||
| "\n", | ||
| " image = create_pattern(width, height, weights)\n", | ||
| " path = f\"/tmp/{extension}-image.{extension}\"\n", | ||
| " image.save(path)\n", | ||
| " with open(f\"/tmp/{extension}-path.json\", \"w\") as f:\n", | ||
| " json.dump(path, f)\n", | ||
| "\n", | ||
| "\n", | ||
| "@script(\n", | ||
| " command=[\"/tmp/venv/bin/python\"],\n", | ||
| " volume_mounts=[m.VolumeMount(name=\"tmpdir\", mount_path=\"/tmp\")],\n", | ||
| " outputs=Artifact(\n", | ||
| " name=\"hdf5output\",\n", | ||
| " path=\"/tmp/images.hdf5\",\n", | ||
| " archive=NoneArchiveStrategy(),\n", | ||
| " ),\n", | ||
| ")\n", | ||
| "def to_hdf5(paths: str):\n", | ||
| "\n", | ||
| " import h5py # pyright: ignore[reportMissingTypeStubs]\n", | ||
| " import numpy as np\n", | ||
| " from PIL import Image\n", | ||
| "\n", | ||
| " print(\"creating hdf5 file\")\n", | ||
| " with h5py.File(\"/tmp/images.hdf5\", \"w\") as f:\n", | ||
| " for i, path in enumerate(paths):\n", | ||
| " path = path.strip('\"')\n", | ||
| " print(f\"Got {path}\")\n", | ||
| " with Image.open(path) as image:\n", | ||
| " arr = np.array(image)\n", | ||
| " f.create_dataset( # pyright: ignore[reportUnknownMemberType]\n", | ||
| " f\"image_{i}\", data=arr, dtype=arr.dtype\n", | ||
| " )\n", | ||
| " print(\"done\")\n", | ||
| "\n", | ||
| "\n", | ||
| "with Workflow(\n", | ||
| " generate_name=\"hera-example-\", # when running on graphql this should be name\n", | ||
| " entrypoint=\"workflowentry\",\n", | ||
| " api_version=\"argoproj.io/v1alpha1\",\n", | ||
| " kind=\"Workflow\", # ClusterWorkflowTemplate\", when on graphql\n", | ||
| " labels={\"workflows.diamond.ac.uk/science-group-examples\": \"true\"},\n", | ||
| " annotations={\n", | ||
| " \"workflows.argoproj.io/title\": \"example remade via hera\",\n", | ||
| " \"workflows.argoproj.io/description\": \"\"\"Replicates the functionality of\n", | ||
| "example.yaml\"\"\",\n", | ||
| " \"workflows.diamond.ac.uk/repository\": \"https://github.com/{% endraw %}{{github_org}}{% raw %}/{% endraw %}{{repo_name}}{% raw %}\",\n", | ||
| " },\n", | ||
| " volumes=Volume(name=\"tmpdir\", mount_path=\"/tmp/\", size=\"1Gi\"),\n", | ||
| ") as w:\n", | ||
| " with DAG(name=\"workflowentry\"):\n", | ||
| " install = install_dependencies(name=\"install\")\n", | ||
| " params = generate_parameters(\n", | ||
| " name=\"params\",\n", | ||
| " arguments={\n", | ||
| " \"png\": \"True\",\n", | ||
| " \"jpg\": \"True\",\n", | ||
| " \"jpeg\": \"True\",\n", | ||
| " \"tif\": \"True\",\n", | ||
| " \"tiff\": \"True\",\n", | ||
| " },\n", | ||
| " )\n", | ||
| " makeimages = create_image(with_param=params.get_parameter(\"out-parameters\"))\n", | ||
| " makehdf5 = to_hdf5(\n", | ||
| " arguments={\n", | ||
| " \"paths\": makeimages.get_parameter(\"out-paths\"),\n", | ||
| " }\n", | ||
| " )\n", | ||
| " [install, params] >> makeimages >> makehdf5 # pyright: ignore\n", | ||
| "\n", | ||
| "\n" | ||
| ] | ||
| }, | ||
| { | ||
| "cell_type": "code", | ||
| "execution_count": null, | ||
| "id": "04708f46", | ||
| "metadata": {}, | ||
| "outputs": [], | ||
| "source": [ | ||
| "\n", | ||
| "with open(\"example.txt\", \"w\") as div:\n", | ||
| " div.write(w.to_yaml()) # pyright: ignore[reportUnknownMemberType]" | ||
| ] | ||
| }, | ||
| { | ||
| "cell_type": "code", | ||
| "execution_count": null, | ||
| "id": "7e9f88cb", | ||
| "metadata": {}, | ||
| "source": [ | ||
| "from {% endraw %}{{project_name}}{% raw %}.submit_workflow import submit_workflow\n", | ||
| "\n", | ||
| "submit_workflow(w)" | ||
| ] | ||
| } | ||
| ], | ||
| "metadata": { | ||
| "kernelspec": { | ||
| "display_name": "{% endraw %}{{repo_name}}{% raw %} (3.11.x)", | ||
| "language": "python", | ||
| "name": "python3" | ||
| }, | ||
| "language_info": { | ||
| "codemirror_mode": { | ||
| "name": "ipython", | ||
| "version": 3 | ||
| }, | ||
| "file_extension": ".py", | ||
| "mimetype": "text/x-python", | ||
| "name": "python", | ||
| "nbconvert_exporter": "python", | ||
| "pygments_lexer": "ipython3", | ||
| "version": "3.11.15" | ||
| } | ||
| }, | ||
| "nbformat": 4, | ||
| "nbformat_minor": 5 | ||
| } | ||
| {% endraw %} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.