|
2 | 2 | "cells": [ |
3 | 3 | { |
4 | 4 | "cell_type": "code", |
5 | | - "execution_count": 22, |
| 5 | + "execution_count": 1, |
6 | 6 | "metadata": {}, |
7 | 7 | "outputs": [], |
8 | 8 | "source": [ |
9 | | - "from fairworkflows import FairStep, FairWorkflow, add_step\n", |
10 | | - "import rdflib" |
| 9 | + "from fairworkflows import is_fairworkflow, is_fairstep" |
11 | 10 | ] |
12 | 11 | }, |
13 | 12 | { |
14 | 13 | "cell_type": "code", |
15 | | - "execution_count": 23, |
| 14 | + "execution_count": 2, |
16 | 15 | "metadata": {}, |
17 | 16 | "outputs": [], |
18 | 17 | "source": [ |
19 | | - "# Create a workflow\n", |
20 | | - "workflow = FairWorkflow(description='This is a test workflow for image processing.')" |
| 18 | + "import io" |
21 | 19 | ] |
22 | 20 | }, |
23 | 21 | { |
24 | 22 | "cell_type": "code", |
25 | | - "execution_count": 24, |
| 23 | + "execution_count": 3, |
26 | 24 | "metadata": {}, |
27 | 25 | "outputs": [], |
28 | 26 | "source": [ |
29 | | - "from PIL import Image\n", |
30 | | - "@add_step(workflow)\n", |
31 | | - "def resize_image(image:Image) -> Image: \n", |
| 27 | + "from PIL import Image" |
| 28 | + ] |
| 29 | + }, |
| 30 | + { |
| 31 | + "cell_type": "code", |
| 32 | + "execution_count": 47, |
| 33 | + "metadata": { |
| 34 | + "nanopubURI": "http://purl.org/np/RAjc1d2L36iZcgEJ7Lcp06mWA6gA1QOJh5oY5bPRDfbkk" |
| 35 | + }, |
| 36 | + "outputs": [], |
| 37 | + "source": [ |
| 38 | + "@is_fairstep(label='Resize image')\n", |
| 39 | + "def resize_image(imgStr:str) -> str: \n", |
32 | 40 | " \"\"\"Resize the image 300x300 \"\"\"\n", |
| 41 | + " image = Image.open(io.BytesIO(imgStr))\n", |
33 | 42 | " new_image = image.resize((300, 300))\n", |
34 | | - " return new_image" |
| 43 | + " imgOut = io.BytesIO()\n", |
| 44 | + " new_image.save(imgOut, format=\"png\")\n", |
| 45 | + " return imgOut.getvalue()" |
35 | 46 | ] |
36 | 47 | }, |
37 | 48 | { |
38 | 49 | "cell_type": "code", |
39 | | - "execution_count": 25, |
40 | | - "metadata": {}, |
| 50 | + "execution_count": 5, |
| 51 | + "metadata": { |
| 52 | + "nanopubURI": "http://purl.org/np/RAlBYV-4V0SjcFXeylB7_MSPlrVqt9ftMNdFUoZZIJlNs" |
| 53 | + }, |
41 | 54 | "outputs": [], |
42 | 55 | "source": [ |
43 | | - "@add_step(workflow)\n", |
44 | | - "def rotate_image(image:Image) -> Image: \n", |
| 56 | + "@is_fairstep(label='Rotate image')\n", |
| 57 | + "def rotate_image(imgStr:str) -> str: \n", |
45 | 58 | " \"\"\"Rotate image\"\"\"\n", |
| 59 | + " image = Image.open(io.BytesIO(imgStr))\n", |
46 | 60 | " new_image = image.transpose(Image.ROTATE_90)\n", |
47 | | - " return new_image" |
| 61 | + " imgOut = io.BytesIO()\n", |
| 62 | + " new_image.save(imgOut, format=\"png\")\n", |
| 63 | + " return imgOut.getvalue()" |
48 | 64 | ] |
49 | 65 | }, |
50 | 66 | { |
51 | 67 | "cell_type": "code", |
52 | | - "execution_count": 26, |
| 68 | + "execution_count": 61, |
53 | 69 | "metadata": {}, |
54 | 70 | "outputs": [], |
55 | 71 | "source": [ |
56 | | - "image = Image.open('images/demo_image.jpg')" |
| 72 | + "@is_fairworkflow(label='My Workflow for Resize and Rotate')\n", |
| 73 | + "def my_workflow(im1):\n", |
| 74 | + " \"\"\"\n", |
| 75 | + " A simple addition, subtraction, multiplication workflow\n", |
| 76 | + " \"\"\"\n", |
| 77 | + " im2 = resize_image(im1)\n", |
| 78 | + " im3 = rotate_image(im2)\n", |
| 79 | + " return im3" |
57 | 80 | ] |
58 | 81 | }, |
59 | 82 | { |
60 | 83 | "cell_type": "code", |
61 | | - "execution_count": null, |
| 84 | + "execution_count": 53, |
62 | 85 | "metadata": {}, |
63 | 86 | "outputs": [], |
64 | | - "source": [] |
| 87 | + "source": [ |
| 88 | + "image = Image.open('img1.png')" |
| 89 | + ] |
| 90 | + }, |
| 91 | + { |
| 92 | + "cell_type": "code", |
| 93 | + "execution_count": 54, |
| 94 | + "metadata": {}, |
| 95 | + "outputs": [], |
| 96 | + "source": [ |
| 97 | + "image.show()" |
| 98 | + ] |
| 99 | + }, |
| 100 | + { |
| 101 | + "cell_type": "code", |
| 102 | + "execution_count": 55, |
| 103 | + "metadata": {}, |
| 104 | + "outputs": [], |
| 105 | + "source": [ |
| 106 | + "imgIn = io.BytesIO()\n", |
| 107 | + "image.save(imgIn, format=\"png\")" |
| 108 | + ] |
65 | 109 | }, |
66 | 110 | { |
67 | 111 | "cell_type": "code", |
68 | | - "execution_count": 27, |
| 112 | + "execution_count": 56, |
69 | 113 | "metadata": {}, |
70 | 114 | "outputs": [], |
71 | 115 | "source": [ |
72 | | - "step_resize = FairStep.from_function(resize_image)\n", |
73 | | - "workflow.add(step_resize)" |
| 116 | + "imgStr = imgIn.getvalue()" |
74 | 117 | ] |
75 | 118 | }, |
76 | 119 | { |
77 | 120 | "cell_type": "code", |
78 | | - "execution_count": 28, |
| 121 | + "execution_count": 62, |
| 122 | + "metadata": {}, |
| 123 | + "outputs": [ |
| 124 | + { |
| 125 | + "data": { |
| 126 | + "text/plain": [ |
| 127 | + "fairworkflows.fairworkflow.FairWorkflow" |
| 128 | + ] |
| 129 | + }, |
| 130 | + "execution_count": 62, |
| 131 | + "metadata": {}, |
| 132 | + "output_type": "execute_result" |
| 133 | + } |
| 134 | + ], |
| 135 | + "source": [ |
| 136 | + "fw = my_workflow(imgStr)\n", |
| 137 | + "type(fw)" |
| 138 | + ] |
| 139 | + }, |
| 140 | + { |
| 141 | + "cell_type": "code", |
| 142 | + "execution_count": 63, |
79 | 143 | "metadata": {}, |
80 | 144 | "outputs": [], |
81 | 145 | "source": [ |
82 | | - "step_rotate = FairStep.from_function(rotate_image)\n", |
83 | | - "workflow.add(step_rotate)" |
| 146 | + "result, prov = fw.execute(num_threads=2)\n", |
| 147 | + "#result" |
| 148 | + ] |
| 149 | + }, |
| 150 | + { |
| 151 | + "cell_type": "code", |
| 152 | + "execution_count": 64, |
| 153 | + "metadata": {}, |
| 154 | + "outputs": [], |
| 155 | + "source": [ |
| 156 | + "imgOut = Image.open(io.BytesIO(result))" |
| 157 | + ] |
| 158 | + }, |
| 159 | + { |
| 160 | + "cell_type": "code", |
| 161 | + "execution_count": 65, |
| 162 | + "metadata": {}, |
| 163 | + "outputs": [], |
| 164 | + "source": [ |
| 165 | + "imgOut.show()" |
| 166 | + ] |
| 167 | + }, |
| 168 | + { |
| 169 | + "cell_type": "code", |
| 170 | + "execution_count": 66, |
| 171 | + "metadata": {}, |
| 172 | + "outputs": [ |
| 173 | + { |
| 174 | + "data": { |
| 175 | + "text/markdown": [ |
| 176 | + "| workflow |\n", |
| 177 | + "| --- |\n", |
| 178 | + "|  |" |
| 179 | + ], |
| 180 | + "text/plain": [ |
| 181 | + "<IPython.core.display.Markdown object>" |
| 182 | + ] |
| 183 | + }, |
| 184 | + "metadata": {}, |
| 185 | + "output_type": "display_data" |
| 186 | + } |
| 187 | + ], |
| 188 | + "source": [ |
| 189 | + "fw.display()" |
84 | 190 | ] |
85 | 191 | }, |
86 | 192 | { |
87 | 193 | "cell_type": "code", |
88 | | - "execution_count": 29, |
| 194 | + "execution_count": 67, |
89 | 195 | "metadata": {}, |
90 | 196 | "outputs": [ |
91 | 197 | { |
92 | 198 | "name": "stdout", |
93 | 199 | "output_type": "stream", |
94 | 200 | "text": [ |
95 | | - "Workflow URI = None\n", |
96 | | - "@prefix dul: <http://www.ontologydesignpatterns.org/ont/dul/DUL.owl#> .\n", |
97 | | - "@prefix ns1: <http://purl.org/dc/terms/> .\n", |
98 | | - "@prefix pplan: <http://purl.org/net/p-plan#> .\n", |
99 | | - "@prefix pwo: <http://purl.org/spar/pwo/> .\n", |
100 | | - "\n", |
101 | | - "_:N15cc257e136f4eb1989df02ecf776463 {\n", |
102 | | - " <http://purl.org/nanopub/temp/mynanopub#functionwrapped_step1607592631.296226> pplan:isStepOfPlan _:plan ;\n", |
103 | | - " dul:precedes <http://purl.org/nanopub/temp/mynanopub#functionwrapped_step1607592631.5940008> .\n", |
104 | | - "\n", |
105 | | - " <http://purl.org/nanopub/temp/mynanopub#functionwrapped_step1607592631.5940008> pplan:isStepOfPlan _:plan .\n", |
106 | | - "\n", |
107 | | - " _:plan a pplan:Plan ;\n", |
108 | | - " ns1:description \"This is a test workflow for image processing.\" ;\n", |
109 | | - " pwo:hasFirstStep <http://purl.org/nanopub/temp/mynanopub#functionwrapped_step1607592631.296226> .\n", |
110 | | - "}\n", |
111 | | - "\n", |
112 | | - "\n" |
| 201 | + "Published to http://purl.org/np/RAsawUg0loIhWWHzeq1T_hWBnSsDTNp-pvduWX8mksbZw\n", |
| 202 | + "Published concept to http://purl.org/np/RAsawUg0loIhWWHzeq1T_hWBnSsDTNp-pvduWX8mksbZw#step\n", |
| 203 | + "Published to http://purl.org/np/RAoJhysI1KA311aKEpDtkX6UjRxZ-aOJKkKErf9G5MS9g\n", |
| 204 | + "Published concept to http://purl.org/np/RAoJhysI1KA311aKEpDtkX6UjRxZ-aOJKkKErf9G5MS9g#step\n", |
| 205 | + "Published to http://purl.org/np/RAhJUgk9_tIle6zZpLpUFI0Znqvug4reNTOTvCHXHMCmI\n", |
| 206 | + "Published concept to http://purl.org/np/RAhJUgk9_tIle6zZpLpUFI0Znqvug4reNTOTvCHXHMCmI#plan\n" |
113 | 207 | ] |
| 208 | + }, |
| 209 | + { |
| 210 | + "data": { |
| 211 | + "text/plain": [ |
| 212 | + "{'nanopub_uri': 'http://purl.org/np/RAhJUgk9_tIle6zZpLpUFI0Znqvug4reNTOTvCHXHMCmI',\n", |
| 213 | + " 'concept_uri': 'http://purl.org/np/RAhJUgk9_tIle6zZpLpUFI0Znqvug4reNTOTvCHXHMCmI#plan'}" |
| 214 | + ] |
| 215 | + }, |
| 216 | + "execution_count": 67, |
| 217 | + "metadata": {}, |
| 218 | + "output_type": "execute_result" |
114 | 219 | } |
115 | 220 | ], |
| 221 | + "source": [ |
| 222 | + "fw.publish_as_nanopub(use_test_server=True)" |
| 223 | + ] |
| 224 | + }, |
| 225 | + { |
| 226 | + "cell_type": "code", |
| 227 | + "execution_count": null, |
| 228 | + "metadata": {}, |
| 229 | + "outputs": [], |
116 | 230 | "source": [ |
117 | 231 | "print(workflow)" |
118 | 232 | ] |
|
140 | 254 | "outputs": [], |
141 | 255 | "source": [ |
142 | 256 | "from fairworkflows import FairStep\n", |
143 | | - "step = FairStep.from_nanopub(uri='http://purl.org/np/RAz-A7EGUT9VCrSjK92HHc9DjwBssuc5eMdF09u1Psx5Q')\n", |
| 257 | + "step = FairStep.from_nanopub(uri='http://purl.org/np/RAjc1d2L36iZcgEJ7Lcp06mWA6gA1QOJh5oY5bPRDfbkk')\n", |
| 258 | + "print(step)" |
| 259 | + ] |
| 260 | + }, |
| 261 | + { |
| 262 | + "cell_type": "code", |
| 263 | + "execution_count": 1, |
| 264 | + "metadata": { |
| 265 | + "collapsed": false, |
| 266 | + "jupyter": { |
| 267 | + "outputs_hidden": false |
| 268 | + }, |
| 269 | + "nanopubURI": "http://purl.org/np/RAGOsaM6E1TmiJ6o89M590Ia6cV2xLGKoQc4WQbFQdlio", |
| 270 | + "tags": [ |
| 271 | + "Injected by FAIR Workflows Widget" |
| 272 | + ] |
| 273 | + }, |
| 274 | + "outputs": [ |
| 275 | + { |
| 276 | + "name": "stdout", |
| 277 | + "output_type": "stream", |
| 278 | + "text": [ |
| 279 | + "Step URI = http://purl.org/np/RAGOsaM6E1TmiJ6o89M590Ia6cV2xLGKoQc4WQbFQdlio#step\n", |
| 280 | + "@prefix bpmn: <http://dkm.fbk.eu/index.php/BPMN2_Ontology#> .\n", |
| 281 | + "@prefix dcterms: <http://purl.org/dc/terms/> .\n", |
| 282 | + "@prefix pplan: <http://purl.org/net/p-plan#> .\n", |
| 283 | + "@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .\n", |
| 284 | + "\n", |
| 285 | + "_:N670443d1e2c64968bb509c8e15682905 {\n", |
| 286 | + " [] a bpmn:ScriptTask,\n", |
| 287 | + " pplan:Step ;\n", |
| 288 | + " rdfs:label \"rotate_image\" ;\n", |
| 289 | + " dcterms:description \"\"\"@add_step(workflow)\n", |
| 290 | + "#@FairStep\n", |
| 291 | + "def rotate_image(img:Image) -> Image:\n", |
| 292 | + " \\\"\\\"\\\"Transforming an image\\\"\\\"\\\"\n", |
| 293 | + " out = im.transpose(Image.ROTATE_90)\n", |
| 294 | + " return out\n", |
| 295 | + "\"\"\" .\n", |
| 296 | + "}\n", |
| 297 | + "\n", |
| 298 | + "\n" |
| 299 | + ] |
| 300 | + } |
| 301 | + ], |
| 302 | + "source": [ |
| 303 | + "from fairworkflows import FairStep\n", |
| 304 | + "step = FairStep.from_nanopub(uri='http://purl.org/np/RAGOsaM6E1TmiJ6o89M590Ia6cV2xLGKoQc4WQbFQdlio')\n", |
144 | 305 | "print(step)" |
145 | 306 | ] |
| 307 | + }, |
| 308 | + { |
| 309 | + "cell_type": "code", |
| 310 | + "execution_count": null, |
| 311 | + "metadata": {}, |
| 312 | + "outputs": [], |
| 313 | + "source": [] |
146 | 314 | } |
147 | 315 | ], |
148 | 316 | "metadata": { |
|
0 commit comments