Skip to content

Commit

Permalink
EP-3251 Open-EO#117 get rid of redundant "node_id" property in DataCu…
Browse files Browse the repository at this point in the history
…be (v1.0.0)
  • Loading branch information
soxofaan committed Mar 11, 2020
1 parent 7298baa commit 6977064
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 23 deletions.
6 changes: 1 addition & 5 deletions openeo/internal/graphbuilder.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,10 @@ def from_process_graph(cls,graph:Dict):

def add_process(self, process_id: str, arguments: dict = None, **kwargs):
"""
Add a process and return the id. Do not add a new process if it already exists in the graph.
# TODO: this used to return process key of flattened version, but that is not necessary anymore
Add a process to the graph
:param process_id: process id
:param arguments: dictionary of process arguments (can also be provided through kwargs)
:return:
"""
if arguments and kwargs:
raise ValueError("At most one of `arguments` and `kwargs` should be specified")
Expand All @@ -71,7 +68,6 @@ def add_process(self, process_id: str, arguments: dict = None, **kwargs):
'arguments': arguments
}
self.result_node = new_process
return process_id

@classmethod
def combine(cls, operator: str, first: Union['GraphBuilder', dict], second: Union['GraphBuilder', dict], arg_name='data'):
Expand Down
30 changes: 12 additions & 18 deletions openeo/rest/datacube.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,14 @@ class DataCube(ImageCollection):
In earlier versions this was called ImageCollection
"""

def __init__(self, node_id: str, builder: GraphBuilder, connection: 'Connection', metadata: CollectionMetadata = None):
def __init__(self, builder: GraphBuilder, connection: 'Connection', metadata: CollectionMetadata = None):
super().__init__(metadata=metadata)
# TODO: what is node_id used for? Does it still needs to be in constructor?
self.node_id = node_id
self.builder = builder
self._connection = connection
self.metadata = metadata

def __str__(self):
return "DataCube: %s" % self.node_id
return "DataCube: %s" % self.builder.result_node["process_id"]

@property
def graph(self):
Expand Down Expand Up @@ -76,11 +74,11 @@ def load_collection(
}
if bands:
arguments['bands'] = bands
node_id = builder.add_process(process_id, arguments=arguments)
builder.add_process(process_id, arguments=arguments)
metadata = connection.collection_metadata(collection_id) if fetch_metadata else None
if bands:
metadata.filter_bands(bands)
return cls(node_id, builder, connection, metadata=metadata)
return cls(builder=builder, connection=connection, metadata=metadata)

@classmethod
@deprecated("use load_collection instead")
Expand Down Expand Up @@ -108,9 +106,8 @@ def load_disk_collection(cls, connection: 'Connection', file_format: str, glob_p
'options': options
}

node_id = builder.add_process(process_id, arguments=arguments)

return cls(node_id, builder, connection, metadata={})
builder.add_process(process_id, arguments=arguments)
return cls(builder=builder, connection=connection, metadata={})

def _filter_temporal(self, start: str, end: str) -> 'ImageCollection':
return self.graph_add_process(
Expand Down Expand Up @@ -323,7 +320,7 @@ def _create_reduced_collection(self, callback_graph_builder, extend_previous_cal

# now current_node should be a reduce node, let's modify it
# TODO: set metadata of reduced cube?
return DataCube(self.node_id, process_graph_copy, self._connection)
return DataCube(builder=process_graph_copy, connection=self._connection)

def __truediv__(self, other):
return self.divide(other)
Expand Down Expand Up @@ -382,9 +379,7 @@ def _reduce_bands_binary(self, operator, other: 'DataCube', arg_name='data'):

# set result node id's first, to keep track
my_builder = self.builder
#my_builder.processes[self.node_id]['result'] = True
other_builder = other.builder
#other_builder.processes[other.node_id]['result'] = True

cubes_merged = GraphBuilder.combine(operator="merge_cubes",
first=my_builder,
Expand All @@ -408,7 +403,7 @@ def _reduce_bands_binary(self, operator, other: 'DataCube', arg_name='data'):
the_node["arguments"]["overlap_resolver"] = {
'callback': merged.result_node
}
return DataCube(None, cubes_merged, self._connection, metadata=self.metadata)
return DataCube(builder=cubes_merged, connection=self._connection, metadata=self.metadata)
else:
args = {
'data': {'from_node': self.builder.result_node},
Expand All @@ -421,13 +416,12 @@ def _reduce_bands_binary(self, operator, other: 'DataCube', arg_name='data'):

reducing_graph = self
if reducing_graph.builder.result_node["process_id"] != "reduce":
node_id = other.node_id
reducing_graph = other
new_builder = reducing_graph.builder.shallow_copy()
new_builder.result_node['arguments']['reducer']['callback'] = merged.result_node
# now current_node should be a reduce node, let's modify it
# TODO: set metadata of reduced cube?
return DataCube(None, new_builder, reducing_graph._connection)
return DataCube(builder=new_builder, connection=reducing_graph._connection)

def _reduce_bands_binary_xy(self, operator, other: Union[ImageCollection, Union[int, float]]):
"""
Expand Down Expand Up @@ -828,7 +822,7 @@ def merge(self, other: 'DataCube') -> 'DataCube':
}
)
# TODO: metadata?
return DataCube(node_id="merge_cubes", builder=builder, connection=self._connection, metadata=None)
return DataCube(builder=builder, connection=self._connection, metadata=None)

def apply_kernel(self, kernel, factor=1.0) -> 'ImageCollection':
"""
Expand Down Expand Up @@ -1011,10 +1005,10 @@ def graph_add_process(self, process_id, args) -> 'DataCube':
"""
# don't modify in place, return new builder
newbuilder = self.builder.shallow_copy()
id = newbuilder.add_process(process_id, arguments=args)
newbuilder.add_process(process_id, arguments=args)

# TODO: properly update metadata as well?
newCollection = DataCube(id, newbuilder, self._connection, metadata=copy.copy(self.metadata))
newCollection = DataCube(builder=newbuilder, connection=self._connection, metadata=copy.copy(self.metadata))
return newCollection

def to_graphviz(self):
Expand Down

0 comments on commit 6977064

Please sign in to comment.