Skip to content

Commit

Permalink
Elimate DataCube._pg usage
Browse files Browse the repository at this point in the history
related to #275
  • Loading branch information
soxofaan committed Mar 15, 2022
1 parent 7cc6ba0 commit d167ab1
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 35 deletions.
4 changes: 2 additions & 2 deletions openeo/internal/graph_building.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,11 +167,11 @@ class ReduceNode(PGNode):
A process graph node for "reduce" processes (has a reducer sub-process-graph)
"""

def __init__(self, data: PGNode, reducer: Union[PGNode, str, dict], dimension: str, process_id="reduce_dimension",
def __init__(self, data: _FromNodeMixin, reducer: Union[PGNode, str, dict], dimension: str, process_id="reduce_dimension",
band_math_mode: bool = False):
assert process_id in ("reduce_dimension", "reduce_dimension_binary")
arguments = {
"data": {"from_node": data},
"data": data,
"reducer": self.to_process_graph_argument(reducer),
"dimension": dimension,
# TODO #125 context
Expand Down
29 changes: 9 additions & 20 deletions openeo/rest/datacube.py
Original file line number Diff line number Diff line change
Expand Up @@ -461,12 +461,8 @@ def resample_spatial(
'align': align
})

def resample_cube_spatial(self, target: 'DataCube' , method: str = 'near'):
return self.process('resample_cube_spatial', {
'data': THIS,
'target': {'from_node': target._pg},
'method': method
})
def resample_cube_spatial(self, target: "DataCube", method: str = "near"):
return self.process("resample_cube_spatial", {"data": self, "target": target, "method": method})

def _operator_binary(self, operator: str, other: Union['DataCube', int, float], reverse=False) -> 'DataCube':
"""Generic handling of (mathematical) binary operator"""
Expand Down Expand Up @@ -874,7 +870,7 @@ def reduce_dimension(

return self.process_with_node(ReduceNode(
process_id=process_id,
data=self._pg,
data=self,
reducer=reducer,
dimension=self.metadata.assert_valid_dimension(dimension),
# TODO #123 is it (still) necessary to make "band" math a special case?
Expand Down Expand Up @@ -929,7 +925,7 @@ def reduce_bands_udf(self, code: str, runtime="Python", version="latest") -> 'Da
# TODO EP-3555: unify better with UDF(PGNode) class and avoid doing same UDF code-runtime-version argument stuff in each method
return self._reduce_bands(reducer=self._create_run_udf(code, runtime, version))

def add_dimension(self, name: str, label: str, type: str = None):
def add_dimension(self, name: str, label: str, type: Optional[str] = None):
"""
Adds a new named dimension to the data cube.
Afterwards, the dimension can be referenced with the specified name. If a dimension with the specified name exists,
Expand All @@ -944,7 +940,7 @@ def add_dimension(self, name: str, label: str, type: str = None):
"""
return self.process(
process_id="add_dimension",
arguments={"data": self._pg, "name": name, "label": label, "type": type},
arguments=dict_no_none({"data": self, "name": name, "label": label, "type": type}),
metadata=self.metadata.add_dimension(name=name, label=label, type=type)
)

Expand All @@ -961,7 +957,7 @@ def drop_dimension(self, name: str):
"""
return self.process(
process_id="drop_dimension",
arguments={"data": self._pg, "name": name},
arguments={"data": self, "name": name},
metadata=self.metadata.drop_dimension(name=name)
)

Expand Down Expand Up @@ -1250,11 +1246,7 @@ def mask(self, mask: 'DataCube' = None, replacement=None) -> 'DataCube':
"""
return self.process(
process_id="mask",
arguments=dict_no_none(
data=THIS,
mask={'from_node': mask._pg},
replacement=replacement
)
arguments=dict_no_none(data=self, mask=mask, replacement=replacement),
)

def mask_polygon(
Expand Down Expand Up @@ -1388,11 +1380,8 @@ def raster_to_vector(self) -> VectorCube:
:return: A vectorcube
"""
return VectorCube(PGNode(
process_id='raster_to_vector',
arguments={
'data': self._pg
}),connection=self._connection, metadata=self.metadata)
pg_node = PGNode(process_id="raster_to_vector", arguments={"data": self})
return VectorCube(pg_node, connection=self._connection, metadata=self.metadata)

####VIEW methods #######

Expand Down
61 changes: 48 additions & 13 deletions tests/rest/datacube/test_datacube100.py
Original file line number Diff line number Diff line change
Expand Up @@ -488,22 +488,57 @@ def test_rename_dimension(con100):
s2 = con100.load_collection("S2")
x = s2.rename_dimension(source="bands", target="ThisIsNotTheBandsDimension")
assert x.flat_graph() == {
'loadcollection1': {
'arguments': {
'id': 'S2',
'spatial_extent': None,
'temporal_extent': None
"loadcollection1": {
"process_id": "load_collection",
"arguments": {"id": "S2", "spatial_extent": None, "temporal_extent": None},
},
"renamedimension1": {
"process_id": "rename_dimension",
"arguments": {
"data": {"from_node": "loadcollection1"},
"source": "bands",
"target": "ThisIsNotTheBandsDimension"
},
'process_id': 'load_collection'
"result": True
}
}


def test_add_dimension(con100):
s2 = con100.load_collection("S2")
x = s2.add_dimension(name="james_band", label="alpha")
assert x.flat_graph() == {
"loadcollection1": {
"process_id": "load_collection",
"arguments": {"id": "S2", "spatial_extent": None, "temporal_extent": None},
},
'renamedimension1': {
'arguments': {
'data': {'from_node': 'loadcollection1'},
'source': 'bands',
'target': 'ThisIsNotTheBandsDimension'
"adddimension1": {
"process_id": "add_dimension",
"arguments": {
"data": {"from_node": "loadcollection1"},
"name": "james_band",
"label": "alpha"
},
'process_id': 'rename_dimension',
'result': True
"result": True
}
}


def test_drop_dimension(con100):
s2 = con100.load_collection("S2")
x = s2.drop_dimension(name="bands")
assert x.flat_graph() == {
"loadcollection1": {
"process_id": "load_collection",
"arguments": {"id": "S2", "spatial_extent": None, "temporal_extent": None},
},
"dropdimension1": {
"process_id": "drop_dimension",
"arguments": {
"data": {"from_node": "loadcollection1"},
"name": "bands",
},
"result": True
}
}

Expand Down

0 comments on commit d167ab1

Please sign in to comment.