Skip to content

Commit 2710fc9

Browse files
committed
Adapted image workflow example to new fair decorator
1 parent b42e360 commit 2710fc9

1 file changed

Lines changed: 214 additions & 46 deletions

File tree

examples/test_image_processing_wf_remzi.ipynb

Lines changed: 214 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -2,117 +2,231 @@
22
"cells": [
33
{
44
"cell_type": "code",
5-
"execution_count": 22,
5+
"execution_count": 1,
66
"metadata": {},
77
"outputs": [],
88
"source": [
9-
"from fairworkflows import FairStep, FairWorkflow, add_step\n",
10-
"import rdflib"
9+
"from fairworkflows import is_fairworkflow, is_fairstep"
1110
]
1211
},
1312
{
1413
"cell_type": "code",
15-
"execution_count": 23,
14+
"execution_count": 2,
1615
"metadata": {},
1716
"outputs": [],
1817
"source": [
19-
"# Create a workflow\n",
20-
"workflow = FairWorkflow(description='This is a test workflow for image processing.')"
18+
"import io"
2119
]
2220
},
2321
{
2422
"cell_type": "code",
25-
"execution_count": 24,
23+
"execution_count": 3,
2624
"metadata": {},
2725
"outputs": [],
2826
"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",
3240
" \"\"\"Resize the image 300x300 \"\"\"\n",
41+
" image = Image.open(io.BytesIO(imgStr))\n",
3342
" 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()"
3546
]
3647
},
3748
{
3849
"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+
},
4154
"outputs": [],
4255
"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",
4558
" \"\"\"Rotate image\"\"\"\n",
59+
" image = Image.open(io.BytesIO(imgStr))\n",
4660
" 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()"
4864
]
4965
},
5066
{
5167
"cell_type": "code",
52-
"execution_count": 26,
68+
"execution_count": 61,
5369
"metadata": {},
5470
"outputs": [],
5571
"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"
5780
]
5881
},
5982
{
6083
"cell_type": "code",
61-
"execution_count": null,
84+
"execution_count": 53,
6285
"metadata": {},
6386
"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+
]
65109
},
66110
{
67111
"cell_type": "code",
68-
"execution_count": 27,
112+
"execution_count": 56,
69113
"metadata": {},
70114
"outputs": [],
71115
"source": [
72-
"step_resize = FairStep.from_function(resize_image)\n",
73-
"workflow.add(step_resize)"
116+
"imgStr = imgIn.getvalue()"
74117
]
75118
},
76119
{
77120
"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,
79143
"metadata": {},
80144
"outputs": [],
81145
"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+
"| ![workflow workflow](control-workflow.svg) |"
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()"
84190
]
85191
},
86192
{
87193
"cell_type": "code",
88-
"execution_count": 29,
194+
"execution_count": 67,
89195
"metadata": {},
90196
"outputs": [
91197
{
92198
"name": "stdout",
93199
"output_type": "stream",
94200
"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"
113207
]
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"
114219
}
115220
],
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": [],
116230
"source": [
117231
"print(workflow)"
118232
]
@@ -140,9 +254,63 @@
140254
"outputs": [],
141255
"source": [
142256
"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",
144305
"print(step)"
145306
]
307+
},
308+
{
309+
"cell_type": "code",
310+
"execution_count": null,
311+
"metadata": {},
312+
"outputs": [],
313+
"source": []
146314
}
147315
],
148316
"metadata": {

0 commit comments

Comments
 (0)