-
Notifications
You must be signed in to change notification settings - Fork 42
/
test_bandmath.py
239 lines (195 loc) · 8.85 KB
/
test_bandmath.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
"""
Band math related tests against both
- 0.4.0-style ImageCollectionClient
- 1.0.0-style DataCube
"""
import pytest
from openeo.rest import BandMathException
from .. import get_download_graph
from ..conftest import reset_graphbuilder
from ... import load_json_resource
def test_band_basic(connection, api_version):
cube = connection.load_collection("SENTINEL2_RADIOMETRY_10M")
expected_graph = load_json_resource('data/%s/band0.json' % api_version)
assert cube.band(0).graph == expected_graph
reset_graphbuilder()
assert cube.band("B02").graph == expected_graph
# TODO graph contains "spectral_band" hardcoded
def test_indexing(connection, api_version):
def check_cube(cube, band_index):
reset_graphbuilder()
assert cube.band(band_index).graph == expected_graph
reset_graphbuilder()
assert cube.band("B04").graph == expected_graph
reset_graphbuilder()
assert cube.band("red").graph == expected_graph
cube = connection.load_collection("SENTINEL2_RADIOMETRY_10M")
expected_graph = load_json_resource('data/%s/band_red.json' % api_version)
check_cube(cube, 2)
cube2 = cube.filter_bands(['red', 'green'])
expected_graph = load_json_resource('data/%s/band_red_filtered.json' % api_version)
check_cube(cube2, 0)
def test_evi(connection, api_version):
cube = connection.load_collection("SENTINEL2_RADIOMETRY_10M")
B02 = cube.band('B02')
B04 = cube.band('B04')
B08 = cube.band('B08')
evi_cube = (2.5 * (B08 - B04)) / ((B08 + 6.0 * B04 - 7.5 * B02) + 1.0)
actual_graph = get_download_graph(evi_cube)
expected_graph = load_json_resource('data/%s/evi_graph.json' % api_version)
assert actual_graph == expected_graph
def test_ndvi_udf(connection, api_version):
s2_radio = connection.load_collection("SENTINEL2_RADIOMETRY_10M")
ndvi_coverage = s2_radio.apply_tiles("def myfunction(tile):\n"
" print(tile)\n"
" return tile")
actual_graph = get_download_graph(ndvi_coverage)
expected_graph = load_json_resource('data/%s/udf_graph.json' % api_version)["process_graph"]
assert actual_graph == expected_graph
def test_ndvi_udf_v100(con100):
s2_radio = con100.load_collection("SENTINEL2_RADIOMETRY_10M")
ndvi_coverage = s2_radio.reduce_bands_udf("def myfunction(tile):\n"
" print(tile)\n"
" return tile")
actual_graph = get_download_graph(ndvi_coverage)
expected_graph = load_json_resource('data/1.0.0/udf_graph.json')["process_graph"]
assert actual_graph == expected_graph
@pytest.mark.parametrize(["process", "expected"], [
((lambda b: b + 3), {
"add1": {"process_id": "add", "arguments": {"x": {"from_node": "arrayelement1"}, "y": 3}, "result": True}
}),
((lambda b: 3 + b), {
"add1": {"process_id": "add", "arguments": {"x": 3, "y": {"from_node": "arrayelement1"}}, "result": True}
}),
((lambda b: 3 + b + 5), {
"add1": {"process_id": "add", "arguments": {"x": 3, "y": {"from_node": "arrayelement1"}}},
"add2": {"process_id": "add", "arguments": {"x": {"from_node": "add1"}, "y": 5}, "result": True}
}
),
((lambda b: b - 3), {
"subtract1": {"process_id": "subtract", "arguments": {"x": {"from_node": "arrayelement1"}, "y": 3},
"result": True}
}),
((lambda b: 3 - b), {
"subtract1": {"process_id": "subtract", "arguments": {"x": 3, "y": {"from_node": "arrayelement1"}},
"result": True}
}),
((lambda b: 2 * b), {
"multiply1": {"process_id": "multiply", "arguments": {"x": 2, "y": {"from_node": "arrayelement1"}},
"result": True}
}),
((lambda b: b * 6), {
"multiply1": {"process_id": "multiply", "arguments": {"x": {"from_node": "arrayelement1"}, "y": 6},
"result": True}
}),
((lambda b: b / 8), {
"divide1": {"process_id": "divide", "arguments": {"x": {"from_node": "arrayelement1"}, "y": 8}, "result": True}
}),
])
def test_band_operation(con100, process, expected):
s2 = con100.load_collection("S2")
b = s2.band('B04')
c = process(b)
callback = {"arrayelement1": {
"process_id": "array_element", "arguments": {"data": {"from_parameter": "data"}, "index": 2}
}}
callback.update(expected)
assert c.graph == {
"loadcollection1": {
"process_id": "load_collection",
"arguments": {"id": "S2", "spatial_extent": None, "temporal_extent": None}
},
"reducedimension1": {
"process_id": "reduce_dimension",
"arguments": {
"data": {"from_node": "loadcollection1"},
"reducer": {"process_graph": callback},
"dimension": "spectral_bands",
},
"result": True,
}
}
def test_merge_issue107(con100):
"""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", "load_collection", "merge_cubes"]
def test_invert_band(connection, api_version):
cube = connection.load_collection("S2")
band = cube.band('B04')
result = (~band)
assert result.graph == load_json_resource('data/%s/bm_invert_band.json' % api_version)
def test_eq_scalar(connection, api_version):
cube = connection.load_collection("S2")
band = cube.band('B04')
result = (band == 42)
assert result.graph == load_json_resource('data/%s/bm_eq_scalar.json' % api_version)
def test_gt_scalar(connection, api_version):
cube = connection.load_collection("S2")
band = cube.band('B04')
result = (band > 42)
assert result.graph == load_json_resource('data/%s/bm_gt_scalar.json' % api_version)
def test_add_sub_mul_div_scalar(connection, api_version):
cube = connection.load_collection("S2")
band = cube.band('B04')
result = (((band + 42) - 10) * 3) / 2
assert result.graph == load_json_resource('data/%s/bm_add_sub_mul_div_scalar.json' % api_version)
def test_add_bands(connection, api_version):
cube = connection.load_collection("S2")
b4 = cube.band("B04")
b3 = cube.band("B03")
result = b4 + b3
assert result.graph == load_json_resource('data/%s/bm_add_bands.json' % api_version)
def test_add_bands_different_collection(connection, api_version):
if api_version == "0.4.0":
pytest.skip("0.4.0 generates invalid result")
b4 = connection.load_collection("S2").band("B04")
b3 = connection.load_collection("SENTINEL2_RADIOMETRY_10M").band("B02")
with pytest.raises(BandMathException):
# TODO #123 implement band math with bands of different collections
b4 + b3
def test_logical_not_equal(connection, api_version):
s2 = connection.load_collection("SENTINEL2_SCF")
scf_band = s2.band("SCENECLASSIFICATION")
mask = scf_band != 4
actual = get_download_graph(mask)
assert actual == load_json_resource('data/%s/notequal.json' % api_version)
def test_logical_or(connection, api_version):
s2 = connection.load_collection("SENTINEL2_SCF")
scf_band = s2.band("SCENECLASSIFICATION")
mask = (scf_band == 2) | (scf_band == 5)
actual = get_download_graph(mask)
assert actual == load_json_resource('data/%s/logical_or.json' % api_version)
def test_logical_and(connection, api_version):
s2 = connection.load_collection("SENTINEL2_SCF")
b1 = s2.band("SCENECLASSIFICATION")
b2 = s2.band("MASK")
mask = (b1 == 2) & (b2 == 5)
actual = get_download_graph(mask)
assert actual == load_json_resource('data/%s/logical_and.json' % api_version)
def test_cube_merge_or(connection, api_version):
s2 = connection.load_collection("S2")
b1 = s2.band("B02") > 1
b2 = s2.band("B03") > 2
b1 = b1.linear_scale_range(0, 1, 0, 2)
b2 = b2.linear_scale_range(0, 1, 0, 2)
combined = b1 | b2
actual = get_download_graph(combined)
assert actual == load_json_resource('data/%s/cube_merge_or.json' % api_version)
def test_cube_merge_multiple(connection, api_version):
if api_version == "0.4.0":
pytest.skip("doesn't work in 0.4.0")
s2 = connection.load_collection("S2")
b1 = s2.band("B02")
b1 = b1.linear_scale_range(0, 1, 0, 2)
combined = b1 + b1 + b1
actual = get_download_graph(combined)
assert sorted(n["process_id"] for n in actual.values()) == [
"linear_scale_range", "load_collection",
"merge_cubes", "merge_cubes", "reduce_dimension", "save_result"]
assert actual == load_json_resource('data/%s/cube_merge_multiple.json' % api_version)