Skip to content

Commit

Permalink
EP-3251 #116 #108 #124 added todo notes
Browse files Browse the repository at this point in the history
  • Loading branch information
soxofaan committed Mar 10, 2020
1 parent 0e32d84 commit 7db9ab9
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 8 deletions.
25 changes: 17 additions & 8 deletions openeo/rest/datacube.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import copy
import datetime
import logging
import pathlib
import typing
from typing import List, Dict, Union, Tuple
Expand All @@ -20,6 +21,11 @@
from openeo.rest.connection import Connection


log = logging.getLogger(__name__)


# TODO #108 this file is still full of "ImageCollection" references (type hints, docs, isinstance checks, ...)

class DataCube(ImageCollection):
"""
Class representing a Data Cube.
Expand Down Expand Up @@ -156,11 +162,12 @@ def band(self, band: Union[str, int]) -> 'ImageCollection':
:return An ImageCollection instance
"""

process_id = 'reduce'
process_id = 'reduce' # TODO #124 reduce_dimension/reduce_dimension_binary
band_index = self.metadata.get_band_index(band)

args = {
'data': {'from_node': self.builder.result_node},
# TODO #116 hardcoded dimension name
'dimension': 'spectral_bands',
'reducer': {
'callback': {
Expand Down Expand Up @@ -308,6 +315,7 @@ def __lt__(self, other: Union[ImageCollection, Union[int, float]]):
def _create_reduced_collection(self, callback_graph_builder, extend_previous_callback_graph):
if not extend_previous_callback_graph:
# there was no previous reduce step
log.warning("Doing band math without proper `DataCube.band()` usage. There is probably something wrong. See issue #123")
args = {
'data': {'from_node': self.builder.result_node},
# TODO: avoid hardcoded dimension name 'spectral_bands' #116
Expand All @@ -316,7 +324,7 @@ def _create_reduced_collection(self, callback_graph_builder, extend_previous_cal
'callback': callback_graph_builder.result_node
}
}
return self.graph_add_process("reduce", args)
return self.graph_add_process("reduce", args) # TODO #124 reduce_dimension/reduce_dimension_binary
else:
process_graph_copy = self.builder.shallow_copy()
process_graph_copy.result_node['arguments']['reducer']['callback'] = callback_graph_builder.result_node
Expand Down Expand Up @@ -417,11 +425,11 @@ def _reduce_bands_binary(self, operator, other: 'DataCube', arg_name='data'):
'callback': merged.processes
}
}
return self.graph_add_process("reduce", args)
return self.graph_add_process("reduce", args) # TODO #124 reduce_dimension/reduce_dimension_binary
else:

reducing_graph = self
if reducing_graph.builder.result_node["process_id"] != "reduce":
if reducing_graph.builder.result_node["process_id"] != "reduce": # TODO #124 reduce_dimension/reduce_dimension_binary
reducing_graph = other
new_builder = reducing_graph.builder.shallow_copy()
new_builder.result_node['arguments']['reducer']['callback'] = merged.result_node
Expand Down Expand Up @@ -472,8 +480,9 @@ def _reduce_bands_binary_const(self, operator, other: Union[int, float], reverse
return self._create_reduced_collection(new_callback_builder, extend_previous_callback_graph)

def _get_band_graph_builder(self):
"""Get process graph builder of "spectral" reduce callback if available"""
current_node = self.builder.result_node
if current_node["process_id"] == "reduce":
if current_node["process_id"] == "reduce": # TODO #124 reduce_dimension/reduce_dimension_binary
# TODO: avoid hardcoded "spectral_bands" dimension #76 #93 #116
if current_node["arguments"]["dimension"] == "spectral_bands":
callback_graph = current_node["arguments"]["reducer"]["callback"]
Expand Down Expand Up @@ -564,7 +573,7 @@ def apply_tiles(self, code: str, runtime="Python", version="latest") -> 'ImageCo
:param code: String representing Python code to be executed in the backend.
"""
process_id = 'reduce'
process_id = 'reduce' # TODO #124 reduce_dimension/reduce_dimension_binary
args = {
'data': {
'from_node': self.builder.result_node
Expand Down Expand Up @@ -603,7 +612,7 @@ def reduce_tiles_over_time(self, code: str, runtime="Python", version="latest"):
:param version: The UDF runtime version
:return:
"""
process_id = 'reduce'
process_id = 'reduce' # TODO #124 reduce_dimension/reduce_dimension_binary
args = {
'data': {
'from_node': self.builder.result_node
Expand Down Expand Up @@ -640,7 +649,7 @@ def apply(self, process: str, data_argument='data', arguments={}) -> 'ImageColle
return self.graph_add_process(process_id, args)

def _reduce_time(self, reduce_function="max"):
process_id = 'reduce'
process_id = 'reduce' # TODO #124 reduce_dimension/reduce_dimension_binary

args = {
'data': {'from_node': self.builder.result_node},
Expand Down
14 changes: 14 additions & 0 deletions tests/rest/datacube/test_bandmath.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ def test_band_basic(con100):
expected_graph = load_json_resource('data/1.0.0/band0.json')
assert cube.band(0).graph == expected_graph
assert cube.band("B02").graph == expected_graph
# TODO graph contains "spectral_band" hardcoded


def test_indexing(con100):
Expand Down Expand Up @@ -150,3 +151,16 @@ def test_band_operation(con100, process, callback_tail):
}
}


@pytest.mark.skip("TODO issue #107")
def test_merge_issue107(con100, requests_mock):
"""https://github.com/Open-EO/openeo-python-client/issues/107"""
s2 = con100.load_collection("S2")
a = s2.filter_bands(['B02'])
b = s2.filter_bands(['B04'])
c = a.merge(b)

flat = c.graph
# There should be only one `load_collection` node (but two `filter_band` ones)
processes = sorted(n["process_id"] for n in flat.values())
assert processes == ["filter_bands", "filter_bands", "merge_cubes", "load_collection"]

0 comments on commit 7db9ab9

Please sign in to comment.