Skip to content

Commit

Permalink
Fix #431 fix UDP param conversion issue in run_udf context
Browse files Browse the repository at this point in the history
  • Loading branch information
soxofaan committed May 25, 2023
1 parent 9cb5376 commit 9ccf891
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 0 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Fixed

- Fixed UDP parameter conversion issue in `build_process_dict` when using parameter in `context` of `run_udf`
[#431](https://github.com/Open-EO/openeo-python-client/issues/431)


## [0.17.0] and [0.17.1] - 2023-05-16

Expand Down
2 changes: 2 additions & 0 deletions openeo/internal/graph_building.py
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,8 @@ def _flatten_argument(self, value):
raise ValueError(pg)
else:
value = {k: self._flatten_argument(v) for k, v in value.items()}
elif isinstance(value, Parameter):
value = {"from_parameter": value.name}
return value

def leaveArgument(self, argument_id: str, value):
Expand Down
98 changes: 98 additions & 0 deletions tests/rest/test_udp.py
Original file line number Diff line number Diff line change
Expand Up @@ -460,3 +460,101 @@ def test_build_process_dict_from_datacube(con100):
"returns": {"schema": {"type": "number"}},
}
assert actual == expected


def test_build_process_dict_udf_context_param_field(con100):
"""https://github.com/Open-EO/openeo-python-client/issues/431"""
udf_param = Parameter.number("udf_param")
cube = con100.datacube_from_process("add", x=3, y=5)
udf = openeo.UDF(code="print(123)", runtime="Python", context={"foo": udf_param})
cube = cube.apply(process=udf)

actual = build_process_dict(
process_graph=cube,
process_id="do_udf",
summary="do_udf value",
description="do_udf",
parameters=[udf_param],
returns={"schema": {"type": "number"}},
)
expected = {
"id": "do_udf",
"description": "do_udf",
"parameters": [{"name": "udf_param", "description": "udf_param", "schema": {"type": "number"}}],
"process_graph": {
"add1": {"arguments": {"x": 3, "y": 5}, "process_id": "add"},
"apply1": {
"arguments": {
"data": {"from_node": "add1"},
"process": {
"process_graph": {
"runudf1": {
"process_id": "run_udf",
"arguments": {
"data": {"from_parameter": "x"},
"udf": "print(123)",
"runtime": "Python",
"context": {"foo": {"from_parameter": "udf_param"}},
},
"result": True,
}
}
},
},
"process_id": "apply",
"result": True,
},
},
"returns": {"schema": {"type": "number"}},
"summary": "do_udf value",
}
assert actual == expected


def test_build_process_dict_udf_context_param_direct(con100):
"""https://github.com/Open-EO/openeo-python-client/issues/431"""
udf_param = Parameter.number("udf_param")
cube = con100.datacube_from_process("add", x=3, y=5)
udf = openeo.UDF(code="print(123)", runtime="Python", context=udf_param)
cube = cube.apply(process=udf)

actual = build_process_dict(
process_graph=cube,
process_id="do_udf",
summary="do_udf value",
description="do_udf",
parameters=[udf_param],
returns={"schema": {"type": "number"}},
)
expected = {
"id": "do_udf",
"description": "do_udf",
"parameters": [{"name": "udf_param", "description": "udf_param", "schema": {"type": "number"}}],
"process_graph": {
"add1": {"arguments": {"x": 3, "y": 5}, "process_id": "add"},
"apply1": {
"arguments": {
"data": {"from_node": "add1"},
"process": {
"process_graph": {
"runudf1": {
"process_id": "run_udf",
"arguments": {
"data": {"from_parameter": "x"},
"udf": "print(123)",
"runtime": "Python",
"context": {"from_parameter": "udf_param"},
},
"result": True,
}
}
},
},
"process_id": "apply",
"result": True,
},
},
"returns": {"schema": {"type": "number"}},
"summary": "do_udf value",
}
assert actual == expected

0 comments on commit 9ccf891

Please sign in to comment.