Skip to content

Commit cf2f548

Browse files
committed
Issue #391 support shared PGNodes better in MultiResult
1 parent 45014eb commit cf2f548

File tree

3 files changed

+35
-11
lines changed

3 files changed

+35
-11
lines changed

openeo/internal/graph_building.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -451,11 +451,13 @@ def __init__(self, leaves: List[FlatGraphableMixin]):
451451

452452
def flat_graph(self) -> Dict[str, dict]:
453453
result = {}
454+
flattener = GraphFlattener()
454455
for leave in self._leaves:
455-
leave_graph = leave.flat_graph()
456-
existing = set(leave_graph.keys()).intersection(result.keys())
457-
if existing:
458-
# TODO: automatic renaming of duplicate node ids?
459-
raise ValueError(f"Duplicate node ids while building multi-result process graph: {sorted(existing)}")
460-
result.update(leave_graph)
456+
if isinstance(leave, PGNode):
457+
result = flattener.flatten(leave)
458+
elif isinstance(leave, _FromNodeMixin):
459+
result = flattener.flatten(leave.from_node())
460+
else:
461+
raise ValueError(leave)
462+
461463
return result

openeo/rest/datacube.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,9 @@ class DataCube(_ProcessGraphAbstraction):
8585
# TODO: set this based on back-end or user preference?
8686
_DEFAULT_RASTER_FORMAT = "GTiff"
8787

88-
def __init__(self, graph: PGNode, connection: Optional[Connection], metadata: Optional[CollectionMetadata] = None):
88+
def __init__(
89+
self, graph: PGNode, connection: Optional[Connection] = None, metadata: Optional[CollectionMetadata] = None
90+
):
8991
super().__init__(pgnode=graph, connection=connection)
9092
self.metadata: Optional[CollectionMetadata] = metadata
9193

tests/internal/test_graphbuilding.py

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
ReduceNode,
1515
)
1616
from openeo.internal.process_graph_visitor import ProcessGraphVisitException
17+
from openeo.rest.datacube import DataCube
1718

1819

1920
def test_pgnode_process_id():
@@ -426,7 +427,26 @@ def test_simple(self):
426427

427428
def test_simple_duplicates(self):
428429
multi = MultiResult([PGNode("foo"), PGNode("foo")])
429-
with pytest.raises(
430-
ValueError, match=re.escape("Duplicate node ids while building multi-result process graph: ['foo1']")
431-
):
432-
multi.flat_graph()
430+
assert multi.flat_graph() == {
431+
"foo1": {"process_id": "foo", "arguments": {}, "result": True},
432+
"foo2": {"process_id": "foo", "arguments": {}, "result": True},
433+
}
434+
435+
def test_multi_save_result_same_root(self):
436+
load_collection = DataCube(PGNode("load_collection", collection_id="S2"))
437+
save_a = load_collection.save_result(format="GTiff")
438+
save_b = load_collection.save_result(format="NetCDF")
439+
multi = MultiResult([save_a, save_b])
440+
assert multi.flat_graph() == {
441+
"loadcollection1": {"process_id": "load_collection", "arguments": {"collection_id": "S2"}},
442+
"saveresult1": {
443+
"process_id": "save_result",
444+
"arguments": {"data": {"from_node": "loadcollection1"}, "format": "GTiff", "options": {}},
445+
"result": True,
446+
},
447+
"saveresult2": {
448+
"process_id": "save_result",
449+
"arguments": {"data": {"from_node": "loadcollection1"}, "format": "NetCDF", "options": {}},
450+
"result": True,
451+
},
452+
}

0 commit comments

Comments
 (0)