From 786bcecb571dd71fc3922aee7d6720b3de7b2510 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?D=C3=A1vid=20D=2EKov=C3=A1cs?= <123364246+daviddkovacs@users.noreply.github.com> Date: Thu, 12 Sep 2024 16:32:56 +0200 Subject: [PATCH 1/4] Documenting "inspect" and context awareness udf.rst Hi, I made a quick description on how to use the "inspect" function to print within the UDFs. moreover, I tried to give a brief explanation on how users can provide their own local side variables to be used within the UDF. --- docs/udf.rst | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/docs/udf.rst b/docs/udf.rst index 5f7983764..6698af7f8 100644 --- a/docs/udf.rst +++ b/docs/udf.rst @@ -317,7 +317,55 @@ 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: ` + +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. +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. +See the example below: + +.. code-block:: python + :linenos: + :caption: ``Passing user defined values`` + :emphasize-lines: 10 + + context = {"factor": 0.0001} + + # 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. + return cube + """,context={"from_parameter": "context"}) # the UDF is now context aware +In the example above, the user stores a preferred value of `0.0001`, which can be passed to the UDF and used by the function. +Later, this value is accessed by calling `context["key"]` within the UDF. Example: ``apply_dimension`` with a UDF ======================================== From b59bf8583a3d03776c084b2d572a5d719f6b49da Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?D=C3=A1vid=20D=2EKov=C3=A1cs?= <123364246+daviddkovacs@users.noreply.github.com> Date: Fri, 13 Sep 2024 11:46:58 +0200 Subject: [PATCH 2/4] Update docs/udf.rst Co-authored-by: Stefaan Lippens --- docs/udf.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/udf.rst b/docs/udf.rst index 6698af7f8..fc2671115 100644 --- a/docs/udf.rst +++ b/docs/udf.rst @@ -365,7 +365,7 @@ See the example below: """,context={"from_parameter": "context"}) # the UDF is now context aware In the example above, the user stores a preferred value of `0.0001`, which can be passed to the UDF and used by the function. -Later, this value is accessed by calling `context["key"]` within the UDF. +Later, this value is accessed by calling `context["factor"]` within the UDF. Example: ``apply_dimension`` with a UDF ======================================== From e638126a05a6c67ab4b03425bb1539574a56007c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?D=C3=A1vid=20D=2EKov=C3=A1cs?= <123364246+daviddkovacs@users.noreply.github.com> Date: Fri, 13 Sep 2024 12:40:02 +0200 Subject: [PATCH 3/4] Context awareness parent udfrst --- docs/udf.rst | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/docs/udf.rst b/docs/udf.rst index fc2671115..b70084c0f 100644 --- a/docs/udf.rst +++ b/docs/udf.rst @@ -351,9 +351,7 @@ See the example below: .. code-block:: python :linenos: :caption: ``Passing user defined values`` - :emphasize-lines: 10 - - context = {"factor": 0.0001} + :emphasize-lines: 8 # Create a UDF object from inline source code. udf = openeo.UDF(""" @@ -364,8 +362,13 @@ See the example below: return cube """,context={"from_parameter": "context"}) # the UDF is now context aware -In the example above, the user stores a preferred value of `0.0001`, which can be passed to the UDF and used by the function. + 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)`. Example: ``apply_dimension`` with a UDF ======================================== From 86b35bbdff33a69545486d07408d480ddebda6ee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?D=C3=A1vid=20D=2EKov=C3=A1cs?= <123364246+daviddkovacs@users.noreply.github.com> Date: Thu, 19 Sep 2024 11:43:40 +0200 Subject: [PATCH 4/4] Update docs/udf.rst Co-authored-by: Stefaan Lippens --- docs/udf.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/udf.rst b/docs/udf.rst index b70084c0f..b6b3adf58 100644 --- a/docs/udf.rst +++ b/docs/udf.rst @@ -365,7 +365,7 @@ See the example below: 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, +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)`.