-
Notifications
You must be signed in to change notification settings - Fork 38
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Documenting "inspect" and context awareness udf.rst #617
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -317,7 +317,58 @@ To invoke a UDF like this, the apply_neighborhood method is most suitable: | |||||
{'dimension': 'y', 'value': 128, 'unit': 'px'} | ||||||
], overlap=[]) | ||||||
|
||||||
Inspecting variables within UDF | ||||||
======================================== | ||||||
|
||||||
To print and inspect variables that are within the UDF, users can use `inspect(data=[], message="")` function. | ||||||
This will print the data that is supplied within, and show it with the message within the logs. | ||||||
|
||||||
.. code-block:: python | ||||||
:linenos: | ||||||
:caption: ``Inspecting UDFs`` | ||||||
:emphasize-lines: 7 | ||||||
|
||||||
# Create a UDF object from inline source code. | ||||||
udf = openeo.UDF(""" | ||||||
import xarray | ||||||
|
||||||
def apply_datacube(cube: xarray.DataArray, context: dict) -> xarray.DataArray: | ||||||
cube.values = 0.0001 * cube.values | ||||||
inspect(data=[type(cube.values)], message="The dtype of cube.values") | ||||||
return cube | ||||||
""") | ||||||
|
||||||
In the above example, the `inspect` function is used to retrieve the datatype of `cube.values`. Once the job logs are opened in the Web Editor, the result will appear | ||||||
under the supplied message. This case it will be shown that `Data: <class 'numpy.ndarray'>` | ||||||
|
||||||
Passing user defined variables to UDF | ||||||
======================================== | ||||||
|
||||||
In order to pass variables and values that are used throughout the user side of script, these need to be put in the `context` dictionary. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I'm not sure what you mean here, e.g. with "user side". And what "script" are you referring to? The script that build the process graph, or the UDF script? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. from the user side, what I mean is the script where the user runs the UDF. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm still a bit confused what you mean:
the user does not run the UDF user side, it's the backend that executes the UDF backend-side There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry, the script where the user defines the UDF script. |
||||||
Once, these variables are defined within `context` dictionary, the UDF needs to be made context aware, by adding `context={"from_parameter": "context"}` at the end of your UDF. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
This is a bit confusing, because in your example you define them in a |
||||||
See the example below: | ||||||
|
||||||
.. code-block:: python | ||||||
:linenos: | ||||||
:caption: ``Passing user defined values`` | ||||||
:emphasize-lines: 8 | ||||||
|
||||||
# Create a UDF object from inline source code. | ||||||
udf = openeo.UDF(""" | ||||||
import xarray | ||||||
|
||||||
def apply_datacube(cube: xarray.DataArray, context: dict) -> xarray.DataArray: | ||||||
cube.values = context["factor"] * cube.values # Accessing the value stored in the context dictionary by the "factor" key. | ||||||
daviddkovacs marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
return cube | ||||||
""",context={"from_parameter": "context"}) # the UDF is now context aware | ||||||
|
||||||
user_variable = {"factor": 0.0001} | ||||||
cube = cube.apply(udf, context = user_variable) | ||||||
|
||||||
In the example above, the user stores a preferred value of ``0.0001`` in the ``user_variable`` dictionary, | ||||||
which can be passed to the UDF and used by the function. | ||||||
Later, this value is accessed by calling `context["factor"]` within the UDF. | ||||||
The parent UDF is called with the user's custom dictionary with `.apply(udf, context = user_variable)`. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. FYI: The UDF is not the parent here, the hierarchical flow is |
||||||
|
||||||
Example: ``apply_dimension`` with a UDF | ||||||
======================================== | ||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This section is still redundant given the existing "Logging from a UDF" section at
openeo-python-client/docs/udf.rst
Lines 616 to 645 in 21922f1
I'd propose to finetune the existing docs if that is necessary