Skip to content

Commit bb04cdc

Browse files
committed
Finetune docs on loading a DataCube from a JSON resource (e.g. UDPs)
1 parent e22fca7 commit bb04cdc

File tree

3 files changed

+64
-28
lines changed

3 files changed

+64
-28
lines changed

docs/cookbook/udp_sharing.rst

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -105,21 +105,29 @@ Loading a published user-defined process as DataCube
105105

106106
From the public URL of the user-defined process,
107107
it is also possible for another user to construct, fully client-side,
108-
a new :class:`~openeo.rest.datacube.DataCube`
109-
with :meth:`Connection.datacube_from_json <openeo.rest.connection.Connection.datacube_from_json>`.
108+
a new :py:class:`~openeo.rest.datacube.DataCube`
109+
with :py:meth:`Connection.datacube_from_json() <openeo.rest.connection.Connection.datacube_from_json>`.
110110

111111
It is important to note that this approach is different from calling
112112
a user-defined process as described in :ref:`evaluate_udp` and :ref:`udp_sharing_call_url_namespace`.
113-
:meth:`Connection.datacube_from_json <openeo.rest.connection.Connection.datacube_from_json>`
113+
:py:meth:`Connection.datacube_from_json() <openeo.rest.connection.Connection.datacube_from_json>`
114114
breaks open the encapsulation of the user-defined process and "unrolls" the process graph inside
115-
into a new :class:`~openeo.rest.datacube.DataCube`.
115+
into a new :py:class:`~openeo.rest.datacube.DataCube`.
116116
This also implies that parameters defined in the user-defined process have to be provided when calling
117-
:meth:`Connection.datacube_from_json <openeo.rest.connection.Connection.datacube_from_json>` ::
117+
:py:meth:`Connection.datacube_from_json() <openeo.rest.connection.Connection.datacube_from_json>`:
118118

119119

120+
.. code-block:: python
121+
:emphasize-lines: 4
122+
120123
udp_url = "https://openeo.vito.be/openeo/1.0/processes/u:johndoe/fahrenheit_to_celsius"
121-
cube = connection.datacube_from_json(udp_url, parameters={"f": 86})
124+
cube = connection.datacube_from_json(
125+
udp_url,
126+
parameters={"f": 86},
127+
)
122128
print(cube.execute())
123129
# Prints: 30.0
124130
131+
Note that :py:meth:`Connection.datacube_from_json() <openeo.rest.connection.Connection.datacube_from_json>`
132+
not only supports loading UDPs from an URL but also from a raw JSON string or a local file path.
125133
For more information, also see :ref:`datacube_from_json`.

docs/datacube_construction.rst

Lines changed: 42 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,16 @@ The ``load_collection`` process
99

1010
The most straightforward way to start building your openEO data cube is through the ``load_collection`` process.
1111
As mentioned earlier, this is provided by the
12-
:meth:`~openeo.rest.connection.Connection.load_collection` method
13-
on a :class:`~openeo.rest.connection.Connection` object,
14-
which produces a :class:`~openeo.rest.datacube.DataCube` instance.
12+
:py:meth:`~openeo.rest.connection.Connection.load_collection` method
13+
on a :py:class:`~openeo.rest.connection.Connection` object,
14+
which produces a :py:class:`~openeo.rest.datacube.DataCube` instance.
1515
For example::
1616

1717
cube = connection.load_collection("SENTINEL2_TOC")
1818

1919
While this should cover the majority of use cases,
2020
there some cases
21-
where one wants to build a :class:`~openeo.rest.datacube.DataCube` object
21+
where one wants to build a :py:class:`~openeo.rest.datacube.DataCube` object
2222
from something else or something more than just a simple ``load_collection`` process.
2323

2424

@@ -38,7 +38,7 @@ but let's assume there is a parameter "dilation" to fine-tune the cloud mask.
3838
Also note that the collection id "SENTINEL2_TOC" is hardcoded in the user-defined process.
3939

4040
We can now construct a data cube from this user-defined process
41-
with :meth:`~openeo.rest.connection.Connection.datacube_from_process`
41+
with :py:meth:`~openeo.rest.connection.Connection.datacube_from_process`
4242
as follows::
4343

4444
cube = connection.datacube_from_process("masked_s2", dilation=10)
@@ -47,7 +47,7 @@ as follows::
4747
cube = cube.filter_temporal("2020-09-01", "2020-09-10")
4848

4949

50-
Note that :meth:`~openeo.rest.connection.Connection.datacube_from_process` can be
50+
Note that :py:meth:`~openeo.rest.connection.Connection.datacube_from_process` can be
5151
used with all kind of processes, not only user-defined processes.
5252
For example, while this is not exactly a real EO data use case,
5353
it will produce a valid openEO process graph that can be executed::
@@ -60,8 +60,8 @@ it will produce a valid openEO process graph that can be executed::
6060

6161
.. _datacube_from_json:
6262

63-
Construct DataCube from JSON
64-
==============================
63+
Construct a DataCube from JSON
64+
===============================
6565

6666
openEO process graphs are typically stored and published in JSON format.
6767
Most notably, user-defined processes are transferred between openEO client
@@ -81,8 +81,8 @@ and back-end in a JSON structure roughly like in this example::
8181
...
8282

8383

84-
It is possible to construct a :class:`~openeo.rest.datacube.DataCube` object that corresponds with this
85-
process graph with the :meth:`Connection.datacube_from_json <openeo.rest.connection.Connection.datacube_from_json>` method.
84+
It is possible to construct a :py:class:`~openeo.rest.datacube.DataCube` object that corresponds with this
85+
process graph with the :py:meth:`Connection.datacube_from_json <openeo.rest.connection.Connection.datacube_from_json>` method.
8686
It can be given one of:
8787

8888
- a raw JSON string,
@@ -99,16 +99,20 @@ The JSON structure should be one of:
9999
Some examples
100100
---------------
101101

102-
Load a :class:`~openeo.rest.datacube.DataCube` from a raw JSON string, containing a
103-
simple "flat graph" representation::
102+
Load a :py:class:`~openeo.rest.datacube.DataCube` from a raw JSON string, containing a
103+
simple "flat graph" representation:
104+
105+
.. code-block:: python
104106
105107
raw_json = '''{
106108
"lc": {"process_id": "load_collection", "arguments": {"id": "SENTINEL2_TOC"}},
107109
"ak": {"process_id": "apply_kernel", "arguments": {"data": {"from_node": "lc"}, "kernel": [[1,2,1],[2,5,2],[1,2,1]]}, "result": true}
108110
}'''
109111
cube = connection.datacube_from_json(raw_json)
110112
111-
Load from a raw JSON string, containing a mapping with "process_graph" and "parameters"::
113+
Load from a raw JSON string, containing a mapping with "process_graph" and "parameters":
114+
115+
.. code-block:: python
112116
113117
raw_json = '''{
114118
"parameters": [
@@ -121,42 +125,58 @@ Load from a raw JSON string, containing a mapping with "process_graph" and "para
121125
}'''
122126
cube = connection.datacube_from_json(raw_json)
123127
124-
Load directly from a file or URL containing these kind of JSON representations::
128+
Load directly from a local file or URL containing these kind of JSON representations:
125129

130+
.. code-block:: python
131+
132+
# Local file
126133
cube = connection.datacube_from_json("path/to/my_udp.json")
127134
128-
cube = connection.datacube_from_json("https://openeo.example/process_graphs/my_udp")
135+
# URL
136+
cube = connection.datacube_from_json("https://example.com/my_udp.json")
129137
130138
131139
Parameterization
132140
-----------------
133141

134142
When the process graph uses parameters, you must specify the desired parameter values
135-
at the time of calling :meth:`Connection.datacube_from_json <openeo.rest.connection.Connection.datacube_from_json>`.
143+
at the time of calling :py:meth:`Connection.datacube_from_json <openeo.rest.connection.Connection.datacube_from_json>`.
144+
145+
For example, take this simple toy example of a process graph that takes the sum of 5 and a parameter "increment":
136146

137-
For example, take this simple toy example of a process graph that takes the sum of 5 and a parameter "increment"::
147+
.. code-block:: python
138148
139149
raw_json = '''{"add": {
140150
"process_id": "add",
141151
"arguments": {"x": 5, "y": {"from_parameter": "increment"}},
142152
"result": true
143153
}}'''
144154
145-
Trying to build a :class:`~openeo.rest.datacube.DataCube` from it without specifying parameter values will fail
146-
like this::
155+
Trying to build a :py:class:`~openeo.rest.datacube.DataCube` from it without specifying parameter values will fail
156+
like this:
157+
158+
.. code-block:: pycon
147159
148160
>>> cube = connection.datacube_from_json(raw_json)
149161
ProcessGraphVisitException: No substitution value for parameter 'increment'.
150162
151-
Instead, specify the parameter value::
163+
Instead, specify the parameter value:
164+
165+
.. code-block:: pycon
166+
:emphasize-lines: 3
152167
153-
>>> cube = connection.datacube_from_json(raw_json, parameters={"increment": 4})
168+
>>> cube = connection.datacube_from_json(
169+
... raw_json,
170+
... parameters={"increment": 4},
171+
... )
154172
>>> cube.execute()
155173
9
156174
157175
158176
Parameters can also be defined with default values, which will be used when they are not specified
159-
in the :meth:`Connection.datacube_from_json <openeo.rest.connection.Connection.datacube_from_json>` call::
177+
in the :py:meth:`Connection.datacube_from_json <openeo.rest.connection.Connection.datacube_from_json>` call:
178+
179+
.. code-block:: python
160180
161181
raw_json = '''{
162182
"parameters": [

openeo/rest/connection.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1139,9 +1139,13 @@ def datacube_from_flat_graph(self, flat_graph: dict, parameters: Optional[dict]
11391139
"""
11401140
Construct a :py:class:`DataCube` from a flat dictionary representation of a process graph.
11411141
1142+
.. seealso:: :ref:`datacube_from_json`, :py:meth:`~openeo.rest.connection.Connection.datacube_from_json`
1143+
11421144
:param flat_graph: flat dictionary representation of a process graph
11431145
or a process dictionary with such a flat process graph under a "process_graph" field
11441146
(and optionally parameter metadata under a "parameters" field).
1147+
:param parameters: Optional dictionary mapping parameter names to parameter values
1148+
to use for parameters occurring in the process graph (e.g. as used in user-defined processes)
11451149
:return: A :py:class:`DataCube` corresponding with the operations encoded in the process graph
11461150
"""
11471151
parameters = parameters or {}
@@ -1162,7 +1166,11 @@ def datacube_from_json(self, src: Union[str, Path], parameters: Optional[dict] =
11621166
"""
11631167
Construct a :py:class:`DataCube` from JSON resource containing (flat) process graph representation.
11641168
1169+
.. seealso:: :ref:`datacube_from_json`, :py:meth:`~openeo.rest.connection.Connection.datacube_from_flat_graph`
1170+
11651171
:param src: raw JSON string, URL to JSON resource or path to local JSON file
1172+
:param parameters: Optional dictionary mapping parameter names to parameter values
1173+
to use for parameters occurring in the process graph (e.g. as used in user-defined processes)
11661174
:return: A :py:class:`DataCube` corresponding with the operations encoded in the process graph
11671175
"""
11681176
return self.datacube_from_flat_graph(load_json_resource(src), parameters=parameters)

0 commit comments

Comments
 (0)