@@ -227,20 +227,37 @@ to be invoked on a subset or slice of the datacube.
227
227
For example:
228
228
229
229
* process ``apply `` requires a transformation that will be applied
230
- to each pixel in the cube (separately)
230
+ to each pixel in the cube (separately), e.g. in pseudocode
231
+
232
+ .. code-block :: text
233
+
234
+ cube.apply(
235
+ given a pixel value
236
+ => scale it with factor 0.01
237
+ )
238
+
231
239
* process ``reduce_dimension `` requires an aggregation function to convert
232
- an array of pixel values (along a given dimension) to a single value
240
+ an array of pixel values (along a given dimension) to a single value,
241
+ e.g. in pseudocode
242
+
243
+ .. code-block :: text
244
+
245
+ cube.reduce_dimension(
246
+ given a pixel timeseries (array) for a (x,y)-location
247
+ => temporal mean of that array
248
+ )
249
+
233
250
* process ``aggregate_spatial `` requires a function to aggregate the values
234
251
in one or more geometries
235
252
236
253
These transformation functions are usually called "**callbacks **"
237
254
because instead of being called explicitly by the user,
238
- they are called by their "parent" process
255
+ they are called and managed by their "parent" process
239
256
(the ``apply ``, ``reduce_dimension `` and ``aggregate_spatial `` in the examples)
240
257
241
258
242
259
The openEO Python Client Library currently provides a couple of DataCube methods
243
- that expect a callback, most commonly:
260
+ that expect such a callback, most commonly:
244
261
245
262
- :py:meth: `openeo.rest.datacube.DataCube.aggregate_spatial `
246
263
- :py:meth: `openeo.rest.datacube.DataCube.aggregate_temporal `
@@ -249,8 +266,14 @@ that expect a callback, most commonly:
249
266
- :py:meth: `openeo.rest.datacube.DataCube.apply_neighborhood `
250
267
- :py:meth: `openeo.rest.datacube.DataCube.reduce_dimension `
251
268
252
- These functions support several ways to specify the desired callback.
269
+ The openEO Python Client Library supports several ways
270
+ to specify the desired callback for these functions:
271
+
253
272
273
+ .. contents ::
274
+ :depth: 1
275
+ :local:
276
+ :backlinks: top
254
277
255
278
Callback as string
256
279
------------------
@@ -269,7 +292,7 @@ for example:
269
292
This approach is only possible if the desired transformation is available
270
293
as a single process. If not, use one of the methods below.
271
294
272
- Also important is that the "signature" of the provided callback process
295
+ It's also important to note that the "signature" of the provided callback process
273
296
should correspond properly with what the parent process expects.
274
297
For example: ``apply `` requires a callback process that receives a
275
298
number and returns one (like ``absolute `` or ``sqrt ``),
@@ -283,44 +306,41 @@ Callback as a callable
283
306
-----------------------
284
307
285
308
You can also specify the callback as a "callable":
286
- a Python object that can be called (e.g. a function without parenthesis).
309
+ which is a fancy word for a Python object that can be called,
310
+ but just think of it like a function you can call.
287
311
288
- The openEO Python Client Library defines the
289
- official processes in the :py:mod: `openeo.processes ` module,
290
- which can be used directly:
312
+ You can use a regular Python function, like this:
291
313
292
314
.. code-block :: python
293
315
294
- from openeo.processes import absolute, max
316
+ def transform (x ):
317
+ return x * 2 + 3
295
318
296
- cube.apply(absolute)
297
- cube.reduce_dimension(max , dimension = " t" )
319
+ cube.apply(transform)
298
320
299
- You can also use ``lambda `` functions:
321
+ or, more compactly, a "lambda"
322
+ (a construct in Python to create anonymous inline functions):
300
323
301
324
.. code-block :: python
302
325
303
326
cube.apply(lambda x : x * 2 + 3 )
304
327
305
328
306
- or normal Python functions:
329
+ The openEO Python Client Library implements most of the official openEO processes as
330
+ :ref: `functions in the "openeo.processes" module <openeo_processes_functions >`,
331
+ which can be used directly as callback:
307
332
308
333
.. code-block :: python
309
334
310
- from openeo.processes import array_element
311
-
312
- def my_bandmath (data ):
313
- band1 = array_element(data, index = 0 )
314
- band2 = array_element(data, index = 1 )
315
- return band1 + 1.2 * band2
316
-
335
+ from openeo.processes import absolute, max
317
336
318
- cube.reduce_dimension(my_bandmath, dimension = " bands" )
337
+ cube.apply(absolute)
338
+ cube.reduce_dimension(max , dimension = " t" )
319
339
320
340
321
- The argument that is passed to these functions is
322
- an instance of :py:class: `openeo.processes.ProcessBuilder ` .
323
- This is a helper object with predefined methods for all standard processes,
341
+ The argument that will be passed to all these callback functions is
342
+ a :py:class: `ProcessBuilder < openeo.processes.ProcessBuilder> ` instance .
343
+ This is a helper object with predefined methods for all standard openEO processes,
324
344
allowing to use an object oriented coding style to define the callback.
325
345
For example:
326
346
@@ -334,12 +354,14 @@ For example:
334
354
cube.reduce_dimension(avg, dimension = " t" )
335
355
336
356
337
- These methods also return `` ProcessBuilder ` ` objects,
357
+ These methods also return :py:class: ` ProcessBuilder <openeo.processes.ProcessBuilder> ` objects,
338
358
which also allows writing callbacks in chained fashion:
339
359
340
360
.. code-block :: python
341
361
342
- cube.apply(lambda x : x.absolute().cos().add(y = 1.23 ))
362
+ cube.apply(
363
+ lambda x : x.absolute().cos().add(y = 1.23 )
364
+ )
343
365
344
366
345
367
All this gives a lot of flexibility to define callbacks compactly
@@ -377,7 +399,7 @@ looks intuitive and straightforward, but it should be noted
377
399
that not everything is allowed in these functions.
378
400
You should just limit yourself to calling
379
401
:py:mod: `openeo.processes ` functions,
380
- :py:class: `openeo.processes.ProcessBuilder ` methods
402
+ :py:class: `ProcessBuilder < openeo.processes.ProcessBuilder> ` methods
381
403
and basic math operators.
382
404
Don't call functions from other libraries like numpy or scipy.
383
405
Don't use Python control flow statements like ``if/else `` constructs
@@ -414,9 +436,14 @@ Callback as ``PGNode``
414
436
-----------------------
415
437
416
438
You can also pass a :py:class: `~openeo.internal.graph_building.PGNode ` object as callback.
417
- This method is used internally and could be useful for more
418
- advanced use cases, but it requires more in-depth knowledge of
419
- the openEO API and openEO Python Client Library to construct correctly.
439
+
440
+ .. attention ::
441
+ This approach should generally not be used in normal use cases.
442
+ The other options discussed above should be preferred.
443
+ It's mainly intended for internal use and an occasional, advanced use case.
444
+ It requires in-depth knowledge of the openEO API
445
+ and openEO Python Client Library to construct correctly.
446
+
420
447
Some examples:
421
448
422
449
.. code-block :: python
0 commit comments