Skip to content

Commit 9fc8b21

Browse files
committed
First "working" example with execution and display. Only mimicking fairworkflows class for now.
1 parent 5f8c11d commit 9fc8b21

1 file changed

Lines changed: 201 additions & 0 deletions

File tree

examples/noodles_fw.ipynb

Lines changed: 201 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,201 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": 85,
6+
"id": "accurate-endorsement",
7+
"metadata": {},
8+
"outputs": [],
9+
"source": [
10+
"from fairworkflows import mark_as_fairstep, FairWorkflow, FairStep\n",
11+
"import functools\n",
12+
"from noodles import schedule, run_single, run_parallel\n",
13+
"from noodles.tutorial import display_workflows\n",
14+
"\n",
15+
"\n",
16+
"def fairstep(func):\n",
17+
" func.fs = mark_as_fairstep(func)\n",
18+
" return schedule(func)\n",
19+
"\n",
20+
"def fairworkflow(name=None):\n",
21+
" def fairworkflow_inner(func):\n",
22+
" def wrapper(*args, **kwargs):\n",
23+
" print(*args, **kwargs)\n",
24+
" promise = func(*args, **kwargs)\n",
25+
" print('aaa', promise)\n",
26+
" class FairWorkflowP:\n",
27+
" def __init__(self):\n",
28+
" self.name = name\n",
29+
" self.promise = promise\n",
30+
" def display(self):\n",
31+
" display_workflows(prefix='control', workflow=self.promise)\n",
32+
" def execute(self, num_threads=1):\n",
33+
" if num_threads==1:\n",
34+
" return run_single(self.promise)\n",
35+
" elif num_threads>1:\n",
36+
" return run_parallel(self.promise, num_threads)\n",
37+
" return FairWorkflowP()\n",
38+
" return wrapper\n",
39+
" return fairworkflow_inner"
40+
]
41+
},
42+
{
43+
"cell_type": "code",
44+
"execution_count": 86,
45+
"id": "reflected-republic",
46+
"metadata": {},
47+
"outputs": [],
48+
"source": [
49+
"@fairstep\n",
50+
"def add(a, b):\n",
51+
" \"\"\"Adding up numbers!\"\"\"\n",
52+
" return a + b"
53+
]
54+
},
55+
{
56+
"cell_type": "code",
57+
"execution_count": 87,
58+
"id": "cubic-basics",
59+
"metadata": {},
60+
"outputs": [],
61+
"source": [
62+
"@fairstep\n",
63+
"def sub(a, b):\n",
64+
" \"\"\"Subtracting numbers.\"\"\"\n",
65+
" return a - b"
66+
]
67+
},
68+
{
69+
"cell_type": "code",
70+
"execution_count": 88,
71+
"id": "virtual-triple",
72+
"metadata": {},
73+
"outputs": [],
74+
"source": [
75+
"@fairstep\n",
76+
"def mul(a, b):\n",
77+
" \"\"\"Multiplying numbers.\"\"\"\n",
78+
" return a * b"
79+
]
80+
},
81+
{
82+
"cell_type": "code",
83+
"execution_count": 89,
84+
"id": "rocky-times",
85+
"metadata": {},
86+
"outputs": [],
87+
"source": [
88+
"@fairworkflow(name='My Workflow')\n",
89+
"def my_workflow(in1, in2, in3):\n",
90+
" t1 = add(in1, in2)\n",
91+
" t2 = sub(in1, in2)\n",
92+
" t3 = mul(sub(t1, in3), t2)\n",
93+
" return t3"
94+
]
95+
},
96+
{
97+
"cell_type": "code",
98+
"execution_count": null,
99+
"id": "bridal-kazakhstan",
100+
"metadata": {},
101+
"outputs": [],
102+
"source": []
103+
},
104+
{
105+
"cell_type": "code",
106+
"execution_count": 90,
107+
"id": "average-turkey",
108+
"metadata": {},
109+
"outputs": [
110+
{
111+
"name": "stdout",
112+
"output_type": "stream",
113+
"text": [
114+
"1 4 3\n",
115+
"aaa <noodles.interface.decorator.PromisedObject object at 0x7f801588f700>\n"
116+
]
117+
}
118+
],
119+
"source": [
120+
"fw = my_workflow(1, 4, 3)"
121+
]
122+
},
123+
{
124+
"cell_type": "code",
125+
"execution_count": 91,
126+
"id": "afraid-reasoning",
127+
"metadata": {},
128+
"outputs": [
129+
{
130+
"data": {
131+
"text/markdown": [
132+
"| workflow |\n",
133+
"| --- |\n",
134+
"| ![workflow workflow](control-workflow.svg) |"
135+
],
136+
"text/plain": [
137+
"<IPython.core.display.Markdown object>"
138+
]
139+
},
140+
"metadata": {},
141+
"output_type": "display_data"
142+
}
143+
],
144+
"source": [
145+
"fw.display()"
146+
]
147+
},
148+
{
149+
"cell_type": "code",
150+
"execution_count": 92,
151+
"id": "aware-wales",
152+
"metadata": {},
153+
"outputs": [
154+
{
155+
"data": {
156+
"text/plain": [
157+
"-6"
158+
]
159+
},
160+
"execution_count": 92,
161+
"metadata": {},
162+
"output_type": "execute_result"
163+
}
164+
],
165+
"source": [
166+
"answer = fw.execute(num_threads=2)\n",
167+
"\n",
168+
"answer"
169+
]
170+
},
171+
{
172+
"cell_type": "code",
173+
"execution_count": null,
174+
"id": "exotic-caribbean",
175+
"metadata": {},
176+
"outputs": [],
177+
"source": []
178+
}
179+
],
180+
"metadata": {
181+
"kernelspec": {
182+
"display_name": "Python 3",
183+
"language": "python",
184+
"name": "python3"
185+
},
186+
"language_info": {
187+
"codemirror_mode": {
188+
"name": "ipython",
189+
"version": 3
190+
},
191+
"file_extension": ".py",
192+
"mimetype": "text/x-python",
193+
"name": "python",
194+
"nbconvert_exporter": "python",
195+
"pygments_lexer": "ipython3",
196+
"version": "3.8.6"
197+
}
198+
},
199+
"nbformat": 4,
200+
"nbformat_minor": 5
201+
}

0 commit comments

Comments
 (0)