Skip to content

Commit 1927290

Browse files
committed
Simplify doing run_udf on VectorCube
Just use standard method instead of cumbersome PGNode hacking. Related to #278
1 parent 5688088 commit 1927290

File tree

4 files changed

+16
-33
lines changed

4 files changed

+16
-33
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1313
- Added `Connection.describe_process()` to retrieve and show a single process
1414
- Added `DataCube.flatten_dimensions()` and `DataCube.unflatten_dimension`
1515
([Open-EO/openeo-processes#308](https://github.com/Open-EO/openeo-processes/issues/308), [Open-EO/openeo-processes#316](https://github.com/Open-EO/openeo-processes/pull/316))
16+
- Added `VectorCube.run_udf` (to avoid non-standard `process_with_node(UDF(...))` usage)
1617

1718
### Changed
1819

openeo/internal/graph_building.py

+2-4
Original file line numberDiff line numberDiff line change
@@ -145,11 +145,9 @@ class UDF(PGNode):
145145
A 'run_udf' process graph node. This is offered as a convenient way to construct run_udf processes.
146146
"""
147147

148-
def __init__(self, code: str, runtime: str, data=None, version: str = None, context: Dict = None):
149-
# TODO: avoid local import: make `THIS` more general than DataCube-specific?
150-
from openeo.rest.datacube import THIS
148+
def __init__(self, code: str, runtime: str, data, version: str = None, context: Dict = None):
151149
arguments = {
152-
"data": data or THIS,
150+
"data": data,
153151
"udf": code,
154152
"runtime": runtime
155153
}

openeo/rest/vectorcube.py

+12-22
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
1-
import json
21
import pathlib
3-
from typing import Union, Optional
42
import typing
3+
from typing import Union, Optional
54

6-
from openeo.internal.graph_building import PGNode, _FromNodeMixin
5+
from openeo.internal.graph_building import PGNode
76
from openeo.metadata import CollectionMetadata
8-
from openeo.rest._datacube import _ProcessGraphAbstraction, THIS
7+
from openeo.rest._datacube import _ProcessGraphAbstraction
98
from openeo.rest.job import RESTJob
10-
from openeo.util import legacy_alias
9+
from openeo.util import legacy_alias, dict_no_none
1110

1211
if hasattr(typing, 'TYPE_CHECKING') and typing.TYPE_CHECKING:
1312
# Imports for type checking only (circular import issue at runtime). `hasattr` is Python 3.5 workaround #210
@@ -45,23 +44,14 @@ def process(
4544
pg = self._build_pgnode(process_id=process_id, arguments=arguments, namespace=namespace, **kwargs)
4645
return VectorCube(graph=pg, connection=self._connection, metadata=metadata or self.metadata)
4746

48-
def process_with_node(self, pg: PGNode, metadata: CollectionMetadata = None) -> 'VectorCube':
49-
"""
50-
Generic helper to create a new DataCube by applying a process (given as process graph node)
51-
52-
:param pg: process graph node (containing process id and arguments)
53-
:param metadata: (optional) metadata to override original cube metadata (e.g. when reducing dimensions)
54-
:return: new DataCube instance
55-
"""
56-
arguments = pg.arguments
57-
for k, v in arguments.items():
58-
# TODO: it's against intended flow to resolve THIS and _FromNodeMixin at this point (should be done before building PGNode)
59-
if v is THIS:
60-
v = self
61-
if isinstance(v, _FromNodeMixin):
62-
arguments[k] = {"from_node": v.from_node()}
63-
# TODO: deep copy `self.metadata` instead of using same instance?
64-
return VectorCube(graph=pg, connection=self._connection, metadata=metadata or self.metadata)
47+
def run_udf(
48+
self, udf: str, runtime: str, version: Optional[str] = None, context: Optional[dict] = None
49+
) -> "VectorCube":
50+
return self.process(
51+
process_id="run_udf",
52+
data=self, udf=udf, runtime=runtime,
53+
arguments=dict_no_none({"version": version, "context": context}),
54+
)
6555

6656
def save_result(self, format: str = "GeoJson", options: dict = None):
6757
return self.process(

tests/rest/datacube/test_vectorcube.py

+1-7
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,7 @@
1-
import pytest
2-
from openeo.internal.graph_building import PGNode
3-
from openeo.rest.connection import Connection
4-
import openeo
5-
6-
71
def test_raster_to_vector(con100):
82
img = con100.load_collection("S2")
93
vector_cube = img.raster_to_vector()
10-
vector_cube_tranformed = vector_cube.process_with_node(openeo.UDF("python source code", "Python"))
4+
vector_cube_tranformed = vector_cube.run_udf(udf="python source code", runtime="Python")
115

126
assert vector_cube_tranformed.flat_graph() == {
137
'loadcollection1': {

0 commit comments

Comments
 (0)