Skip to content

Commit

Permalink
fixup! make linear_scale_range return a valid process graph
Browse files Browse the repository at this point in the history
  • Loading branch information
soxofaan committed Oct 7, 2021
1 parent 1e94ead commit b3fae62
Show file tree
Hide file tree
Showing 7 changed files with 111 additions and 61 deletions.
2 changes: 1 addition & 1 deletion openeo/rest/datacube.py
Original file line number Diff line number Diff line change
Expand Up @@ -1210,7 +1210,7 @@ def linear_scale_range(self, input_min, input_max, output_min, output_max) -> 'D
:return: a DataCube instance
"""

return self.apply(lambda x:x.x.linear_scale_range( input_min, input_max, output_min, output_max))
return self.apply(lambda x: x.linear_scale_range(input_min, input_max, output_min, output_max))

def mask(self, mask: 'DataCube' = None, replacement=None) -> 'DataCube':
"""
Expand Down
33 changes: 23 additions & 10 deletions tests/data/1.0.0/merge_cubes_multiple.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,26 +30,39 @@
}
}
},
"linearscalerange1": {
"process_id": "linear_scale_range",
"apply1": {
"process_id": "apply",
"arguments": {
"x": {
"data": {
"from_node": "reducedimension1"
},
"inputMin": 0,
"inputMax": 1,
"outputMin": 0,
"outputMax": 2
"process": {
"process_graph": {
"linearscalerange1": {
"process_id": "linear_scale_range",
"arguments": {
"inputMax": 1,
"inputMin": 0,
"outputMax": 2,
"outputMin": 0,
"x": {
"from_parameter": "x"
}
},
"result": true
}
}
}
}
},
"mergecubes1": {
"process_id": "merge_cubes",
"arguments": {
"cube1": {
"from_node": "linearscalerange1"
"from_node": "apply1"
},
"cube2": {
"from_node": "linearscalerange1"
"from_node": "apply1"
},
"overlap_resolver": {
"process_graph": {
Expand All @@ -76,7 +89,7 @@
"from_node": "mergecubes1"
},
"cube2": {
"from_node": "linearscalerange1"
"from_node": "apply1"
},
"overlap_resolver": {
"process_graph": {
Expand Down
68 changes: 47 additions & 21 deletions tests/data/1.0.0/merge_cubes_or.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,18 +39,6 @@
}
}
},
"linearscalerange1": {
"process_id": "linear_scale_range",
"arguments": {
"x": {
"from_node": "reducedimension1"
},
"inputMin": 0,
"inputMax": 1,
"outputMin": 0,
"outputMax": 2
}
},
"reducedimension2": {
"process_id": "reduce_dimension",
"arguments": {
Expand Down Expand Up @@ -83,26 +71,64 @@
}
}
},
"linearscalerange2": {
"process_id": "linear_scale_range",
"apply1": {
"process_id": "apply",
"arguments": {
"data": {
"from_node": "reducedimension1"
},
"process": {
"process_graph": {
"linearscalerange1": {
"process_id": "linear_scale_range",
"arguments": {
"inputMax": 1,
"inputMin": 0,
"outputMax": 2,
"outputMin": 0,
"x": {
"from_parameter": "x"
}
},
"result": true
}
}
}
}
},
"apply2": {
"process_id": "apply",
"arguments": {
"x": {
"data": {
"from_node": "reducedimension2"
},
"inputMin": 0,
"inputMax": 1,
"outputMin": 0,
"outputMax": 2
"process": {
"process_graph": {
"linearscalerange2": {
"arguments": {
"inputMax": 1,
"inputMin": 0,
"outputMax": 2,
"outputMin": 0,
"x": {
"from_parameter": "x"
}
},
"process_id": "linear_scale_range",
"result": true
}
}
}
}
},
"mergecubes1": {
"process_id": "merge_cubes",
"arguments": {
"cube1": {
"from_node": "linearscalerange1"
"from_node": "apply1"
},
"cube2": {
"from_node": "linearscalerange2"
"from_node": "apply2"
},
"overlap_resolver": {
"process_graph": {
Expand Down
27 changes: 20 additions & 7 deletions tests/data/1.0.0/pipe.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,29 @@
}
}
},
"linearscalerange1": {
"process_id": "linear_scale_range",
"apply1": {
"process_id": "apply",
"arguments": {
"x": {
"data": {
"from_node": "ndvi1"
},
"inputMin": 0,
"inputMax": 1,
"outputMin": 0,
"outputMax": 100
"process": {
"process_graph": {
"linearscalerange1": {
"arguments": {
"inputMax": 1,
"inputMin": 0,
"outputMax": 100,
"outputMin": 0,
"x": {
"from_parameter": "x"
}
},
"process_id": "linear_scale_range",
"result": true
}
}
}
},
"result": true
}
Expand Down
2 changes: 1 addition & 1 deletion tests/rest/datacube/test_bandmath.py
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ def test_merge_cubes_multiple(connection, api_version):
combined = b1 + b1 + b1
actual = get_download_graph(combined)
assert sorted(n["process_id"] for n in actual.values()) == [
"linear_scale_range", "load_collection",
"apply", "load_collection",
"merge_cubes", "merge_cubes", "reduce_dimension", "save_result"]
assert actual == load_json_resource('data/%s/merge_cubes_multiple.json' % api_version)

Expand Down
21 changes: 0 additions & 21 deletions tests/rest/datacube/test_datacube.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,27 +198,6 @@ def ndvi_percent(cube):
assert im.graph == load_json_resource('data/{v}/pipe.json'.format(v=api_version))


def test_pipe_with_args(s2cube):
def ndvi_scaled(cube, in_max=2, out_max=3):
return cube.ndvi().linear_scale_range(0, in_max, 0, out_max)

reset_graphbuilder()
im = s2cube.pipe(ndvi_scaled)
assert im.graph["linearscalerange1"]["arguments"] == {
'inputMax': 2, 'inputMin': 0, 'outputMax': 3, 'outputMin': 0, 'x': {'from_node': 'ndvi1'}
}
reset_graphbuilder()
im = s2cube.pipe(ndvi_scaled, 4, 5)
assert im.graph["linearscalerange1"]["arguments"] == {
'inputMax': 4, 'inputMin': 0, 'outputMax': 5, 'outputMin': 0, 'x': {'from_node': 'ndvi1'}
}
reset_graphbuilder()
im = s2cube.pipe(ndvi_scaled, out_max=7)
assert im.graph["linearscalerange1"]["arguments"] == {
'inputMax': 2, 'inputMin': 0, 'outputMax': 7, 'outputMin': 0, 'x': {'from_node': 'ndvi1'}
}


def test_filter_bbox_minimal(s2cube):
im = s2cube.filter_bbox(west=3.0, east=3.1, north=51.1, south=51.0)
graph = _get_leaf_node(im)
Expand Down
19 changes: 19 additions & 0 deletions tests/rest/datacube/test_datacube100.py
Original file line number Diff line number Diff line change
Expand Up @@ -1290,3 +1290,22 @@ def validation(request, context):
errors = cube.validate()
assert errors == [{"code": "Invalid", "message": "Invalid process graph"}]
assert m.call_count == 1


def test_pipe_with_args(con100):
def ndvi_scaled(cube, in_max=2, out_max=3):
return cube.ndvi().linear_scale_range(0, in_max, 0, out_max)

s2cube = con100.load_collection("S2")
im = s2cube.pipe(ndvi_scaled)
assert im.graph["apply1"]["arguments"]["process"]["process_graph"]["linearscalerange1"]["arguments"] == {
'inputMax': 2, 'inputMin': 0, 'outputMax': 3, 'outputMin': 0, 'x': {'from_parameter': 'x'}
}
im = s2cube.pipe(ndvi_scaled, 4, 5)
assert im.graph["apply1"]["arguments"]["process"]["process_graph"]["linearscalerange1"]["arguments"] == {
'inputMax': 4, 'inputMin': 0, 'outputMax': 5, 'outputMin': 0, 'x': {'from_parameter': 'x'}
}
im = s2cube.pipe(ndvi_scaled, out_max=7)
assert im.graph["apply1"]["arguments"]["process"]["process_graph"]["linearscalerange1"]["arguments"] == {
'inputMax': 2, 'inputMin': 0, 'outputMax': 7, 'outputMin': 0, 'x': {'from_parameter': 'x'}
}

0 comments on commit b3fae62

Please sign in to comment.