@@ -107,7 +107,7 @@ def multiply(x, y):
107
107
wg = WorkGraph ("add_multiply_workflow" )
108
108
add_task = wg .add_task (add , name = "add1" )
109
109
# link the output of the `add` task to one of the `x` input of the `multiply` task.
110
- wg .add_task (multiply , name = "multiply1" , x = add_task .outputs [ " result" ] )
110
+ wg .add_task (multiply , name = "multiply1" , x = add_task .outputs . result )
111
111
112
112
# export the workgraph to html file so that it can be visualized in a browser
113
113
wg .to_html ()
@@ -128,8 +128,8 @@ def multiply(x, y):
128
128
)
129
129
130
130
print ("State of WorkGraph: {}" .format (wg .state ))
131
- print ("Result of add : {}" .format (wg .tasks [ " add1" ] .outputs [ 0 ] .value ))
132
- print ("Result of multiply : {}" .format (wg .tasks [ " multiply1" ] .outputs [ 0 ] .value ))
131
+ print ("Result of add : {}" .format (wg .tasks . add1 .outputs . result .value ))
132
+ print ("Result of multiply : {}" .format (wg .tasks . multiply1 .outputs . result .value ))
133
133
134
134
135
135
######################################################################
@@ -141,172 +141,6 @@ def multiply(x, y):
141
141
generate_node_graph (wg .pk )
142
142
143
143
144
- ######################################################################
145
- # Remote job
146
- # ----------
147
- #
148
- # The ``PythonJob`` is a built-in task that allows users to run Python
149
- # functions on a remote computer.
150
- #
151
- # In this case, we use define the task using normal function instead of
152
- # ``calcfunction``. Thus, user does not need to install AiiDA on the
153
- # remote computer.
154
- #
155
-
156
- from aiida_workgraph import WorkGraph , task
157
-
158
- # define add task
159
- @task ()
160
- def add (x , y ):
161
- return x + y
162
-
163
-
164
- # define multiply task
165
- @task ()
166
- def multiply (x , y ):
167
- return x * y
168
-
169
-
170
- wg = WorkGraph ("second_workflow" )
171
- # You might need to adapt the label to python3 if you use this as your default python
172
- wg .add_task ("PythonJob" , function = add , name = "add" , command_info = {"label" : "python" })
173
- wg .add_task (
174
- "PythonJob" ,
175
- function = multiply ,
176
- name = "multiply" ,
177
- x = wg .tasks ["add" ].outputs [0 ],
178
- command_info = {"label" : "python" },
179
- )
180
-
181
- # export the workgraph to html file so that it can be visualized in a browser
182
- wg .to_html ()
183
- # visualize the workgraph in jupyter-notebook
184
- # wg
185
-
186
-
187
- ######################################################################
188
- # Submit the workgraph
189
- # ~~~~~~~~~~~~~~~~~~~~
190
- #
191
- # **Code**: We can set the ``computer`` to the remote computer where we
192
- # want to run the job. This will create a code ``python@computer`` if not
193
- # exists. Of course, you can also set the ``code`` directly if you have
194
- # already created the code.
195
- #
196
- # **Data**: Users can (and is recoomaneded) use normal Python data as
197
- # input. The workgraph will transfer the data to AiiDA data
198
- # (``PickledData``) using pickle.
199
- #
200
- # **Python Version**: since pickle is used to store and load data, the
201
- # Python version on the remote computer should match the one used in the
202
- # localhost. One can use conda to create a virtual environment with the
203
- # same Python version. Then activate the environment before running the
204
- # script.
205
- #
206
- # .. code:: python
207
- #
208
- # # For real applications, one can pass metadata to the scheduler to activate the conda environment
209
- # metadata = {
210
- # "options": {
211
- # 'custom_scheduler_commands' : 'module load anaconda\nconda activate py3.11\n',
212
- # }
213
- # }
214
- #
215
-
216
- from aiida_workgraph .utils import generate_node_graph
217
-
218
- # ------------------------- Submit the calculation -------------------
219
- # For real applications, one can pass metadata to the scheduler to activate the conda environment
220
- metadata = {
221
- "options" : {
222
- # 'custom_scheduler_commands' : 'module load anaconda\nconda activate py3.11\n',
223
- "custom_scheduler_commands" : "" ,
224
- }
225
- }
226
-
227
- wg .submit (
228
- inputs = {
229
- "add" : {"x" : 2 , "y" : 3 , "computer" : "localhost" , "metadata" : metadata },
230
- "multiply" : {"y" : 4 , "computer" : "localhost" , "metadata" : metadata },
231
- },
232
- wait = True ,
233
- )
234
- # ------------------------- Print the output -------------------------
235
- print (
236
- "\n Result of multiply is {} \n \n " .format (
237
- wg .tasks ["multiply" ].outputs ["result" ].value
238
- )
239
- )
240
- # ------------------------- Generate node graph -------------------
241
- generate_node_graph (wg .pk )
242
-
243
-
244
- ######################################################################
245
- # Use parent folder
246
- # ~~~~~~~~~~~~~~~~~
247
- #
248
- # The parent_folder parameter allows a task to access the output files of
249
- # a parent task. This feature is particularly useful when you want to
250
- # reuse data generated by a previous computation in subsequent
251
- # computations. In the following example, the multiply task uses the
252
- # ``result.txt`` file created by the add task.
253
- #
254
- # By default, the content of the parent folder is symlinked to the working
255
- # directory. In the function, you can access the parent folder using the
256
- # relative path. For example, ``./parent_folder/result.txt``.
257
- #
258
-
259
- from aiida_workgraph import WorkGraph , task
260
-
261
- # define add task
262
- @task ()
263
- def add (x , y ):
264
- z = x + y
265
- with open ("result.txt" , "w" ) as f :
266
- f .write (str (z ))
267
-
268
-
269
- # define multiply task
270
- @task ()
271
- def multiply (x , y ):
272
- with open ("parent_folder/result.txt" , "r" ) as f :
273
- z = int (f .read ())
274
- return x * y + z
275
-
276
-
277
- wg = WorkGraph ("third_workflow" )
278
- # You might need to adapt the label to python3 if you use this as your default python
279
- wg .add_task ("PythonJob" , function = add , name = "add" , command_info = {"label" : "python" })
280
- wg .add_task (
281
- "PythonJob" ,
282
- function = multiply ,
283
- name = "multiply" ,
284
- parent_folder = wg .tasks ["add" ].outputs ["remote_folder" ],
285
- command_info = {"label" : "python" },
286
- )
287
-
288
- wg .to_html ()
289
-
290
-
291
- ######################################################################
292
- # Submit the calculation
293
- #
294
-
295
- # ------------------------- Submit the calculation -------------------
296
- wg .submit (
297
- inputs = {
298
- "add" : {"x" : 2 , "y" : 3 , "computer" : "localhost" },
299
- "multiply" : {"x" : 3 , "y" : 4 , "computer" : "localhost" },
300
- },
301
- wait = True ,
302
- )
303
- print (
304
- "\n Result of multiply is {} \n \n " .format (
305
- wg .tasks ["multiply" ].outputs ["result" ].value
306
- )
307
- )
308
-
309
-
310
144
######################################################################
311
145
# CalcJob and WorkChain
312
146
# ---------------------
@@ -341,7 +175,7 @@ def multiply(x, y):
341
175
wg = WorkGraph ("test_add_multiply" )
342
176
add1 = wg .add_task (ArithmeticAddCalculation , name = "add1" , x = Int (2 ), y = Int (3 ), code = code )
343
177
add2 = wg .add_task (ArithmeticAddCalculation , name = "add2" , y = Int (3 ), code = code )
344
- wg .add_link (wg .tasks [ " add1" ] .outputs [ " sum" ] , wg .tasks [ " add2" ] .inputs [ "x" ] )
178
+ wg .add_link (wg .tasks . add1 .outputs . sum , wg .tasks . add2 .inputs . x )
345
179
wg .to_html ()
346
180
347
181
@@ -350,7 +184,7 @@ def multiply(x, y):
350
184
#
351
185
352
186
wg .submit (wait = True )
353
- print ("Result of task add1: {}" .format (wg .tasks [ " add2" ] .outputs [ " sum" ] .value ))
187
+ print ("Result of task add1: {}" .format (wg .tasks . add2 .outputs . sum .value ))
354
188
355
189
from aiida_workgraph .utils import generate_node_graph
356
190
@@ -413,7 +247,7 @@ def add_multiply(x, y, z):
413
247
wg = WorkGraph ()
414
248
wg .add_task (add , name = "add" , x = x , y = y )
415
249
wg .add_task (multiply , name = "multiply" , x = z )
416
- wg .add_link (wg .tasks [ " add" ] .outputs [ " result" ] , wg .tasks [ " multiply" ] .inputs [ "y" ] )
250
+ wg .add_link (wg .tasks . add .outputs . result , wg .tasks . multiply .inputs . y )
417
251
# don't forget to return the `wg`
418
252
return wg
419
253
@@ -431,7 +265,7 @@ def add_multiply(x, y, z):
431
265
add_multiply1 = wg .add_task (add_multiply , x = Int (2 ), y = Int (3 ), z = Int (4 ))
432
266
add_multiply2 = wg .add_task (add_multiply , x = Int (2 ), y = Int (3 ))
433
267
# link the output of a task to the input of another task
434
- wg .add_link (add_multiply1 .outputs [ " multiply" ] , add_multiply2 .inputs [ "z" ] )
268
+ wg .add_link (add_multiply1 .outputs . multiply , add_multiply2 .inputs . z )
435
269
wg .submit (wait = True )
436
270
print ("WorkGraph state: " , wg .state )
437
271
@@ -440,9 +274,7 @@ def add_multiply(x, y, z):
440
274
# Get the result of the tasks:
441
275
#
442
276
443
- print (
444
- "Result of task add_multiply1: {}" .format (add_multiply1 .outputs ["multiply" ].value )
445
- )
277
+ print ("Result of task add_multiply1: {}" .format (add_multiply1 .outputs .multiply .value ))
446
278
447
279
generate_node_graph (wg .pk )
448
280
0 commit comments