From 7753d1366aacdc8df44c0efd39ca93cdef16ed7b Mon Sep 17 00:00:00 2001 From: Emma Ai Date: Thu, 28 Nov 2024 02:26:10 +0000 Subject: [PATCH] add tests for level34 --- odc/stats/plugins/lc_level34.py | 2 +- tests/test_lc_l34.py | 188 -------------------------------- tests/test_lc_level3.py | 107 ------------------ 3 files changed, 1 insertion(+), 296 deletions(-) delete mode 100644 tests/test_lc_l34.py delete mode 100644 tests/test_lc_level3.py diff --git a/odc/stats/plugins/lc_level34.py b/odc/stats/plugins/lc_level34.py index 238850d..ef48287 100644 --- a/odc/stats/plugins/lc_level34.py +++ b/odc/stats/plugins/lc_level34.py @@ -87,7 +87,7 @@ def classification(self, xx, class_def, con_cols, class_col): res = da.full(xx.level_3_4.shape, 0, dtype="uint8") for expression in expressions: - _log.info(expression) + _log.debug(expression) local_dict.update({"res": res}) res = expr_eval( expression, diff --git a/tests/test_lc_l34.py b/tests/test_lc_l34.py deleted file mode 100644 index 54706d0..0000000 --- a/tests/test_lc_l34.py +++ /dev/null @@ -1,188 +0,0 @@ -from odc.stats.plugins.lc_level34 import StatsLccsLevel4 -import numpy as np -import pandas as pd -import xarray as xr -import dask.array as da -from datacube.utils.geometry import GeoBox -from affine import Affine - - -import pytest - - -NODATA = 255 - - -@pytest.fixture(scope="module") -def image_groups(): - l34 = np.array( - [ - [ - [210, 210, 210], - [210, 210, 210], - [223, 210, 210], - [221, 221, 221], - ] - ], - dtype="uint8", - ) - - urban = np.array( - [ - [ - [216, 216, 215], - [216, 216, 216], - [215, 215, 215], - [215, 215, 215], - ] - ], - dtype="uint8", - ) - - woody = np.array( - [ - [ - [113, 113, 113], - [113, 113, 255], - [114, 114, 114], - [114, 114, 255], - ] - ], - dtype="uint8", - ) - - pv_pc_50 = np.array( - [ - [ - [1, 64, 65], - [66, 40, 41], - [3, 61, 78], - [4, 23, 42], - ] - ], - dtype="uint8", - ) - - bs_pc_50 = np.array( - [ - [ - [1, 64, NODATA], - [66, 40, 41], - [1, 40, 66], - [NODATA, 1, 42], - ] - ], - dtype="uint8", - ) - - cultivated = np.array( - [ - [ - [255, 255, 255], - [255, 255, 255], - [255, 255, 255], - [255, 255, 255], - ] - ], - dtype="uint8", - ) - - water_frequency = np.array( - [ - [ - [1, 3, 2], - [4, 5, 6], - [2, 2, 11], - [10, 11, 12], - ] - ], - dtype="uint8", - ) - - water_season = np.array( - [ - [ - [1, 2, 1], - [2, 1, 2], - [1, 1, 2], - [2, 2, 1], - ] - ], - dtype="uint8", - ) - - tuples = [ - (np.datetime64("2000-01-01T00"), np.datetime64("2000-01-01")), - ] - index = pd.MultiIndex.from_tuples(tuples, names=["time", "solar_day"]) - - affine = Affine.translation(10, 0) * Affine.scale( - (20 - 10) / l34.shape[2], (5 - 0) / l34.shape[1] - ) - geobox = GeoBox( - crs="epsg:3577", affine=affine, width=l34.shape[2], height=l34.shape[1] - ) - coords = geobox.xr_coords() - - data_vars = { - "level_3_4": xr.DataArray( - da.from_array(l34, chunks=(1, -1, -1)), - dims=("spec", "y", "x"), - attrs={"nodata": 255}, - ), - "artificial_surface": xr.DataArray( - da.from_array(urban, chunks=(1, -1, -1)), - dims=("spec", "y", "x"), - attrs={"nodata": 255}, - ), - "cultivated": xr.DataArray( - da.from_array(cultivated, chunks=(1, -1, -1)), - dims=("spec", "y", "x"), - attrs={"nodata": 255}, - ), - "woody": xr.DataArray( - da.from_array(woody, chunks=(1, -1, -1)), - dims=("spec", "y", "x"), - attrs={"nodata": 255}, - ), - "pv_pc_50": xr.DataArray( - da.from_array(pv_pc_50, chunks=(1, -1, -1)), - dims=("spec", "y", "x"), - attrs={"nodata": 255}, - ), - "bs_pc_50": xr.DataArray( - da.from_array(bs_pc_50, chunks=(1, -1, -1)), - dims=("spec", "y", "x"), - attrs={"nodata": 255}, - ), - "water_frequency": xr.DataArray( - da.from_array(water_frequency, chunks=(1, -1, -1)), - dims=("spec", "y", "x"), - attrs={"nodata": 255}, - ), - "water_season": xr.DataArray( - da.from_array(water_season, chunks=(1, -1, -1)), - dims=("spec", "y", "x"), - attrs={"nodata": 255}, - ), - } - - xx = xr.Dataset(data_vars=data_vars, coords=coords) - xx = xx.assign_coords(xr.Coordinates.from_pandas_multiindex(index, "spec")) - return xx - - -def test_l4_classes(image_groups, urban_shape): - expected_l3 = [[216, 216, 215], [216, 216, 216], [220, 215, 215], [220, 220, 220]] - - expected_l4 = [[95, 97, 93], [97, 96, 96], [100, 93, 93], [101, 101, 101]] - stats_l4 = StatsLccsLevel4( - measurements=["level3", "level4"], - urban_mask=urban_shape, - filter_expression="mock > 9", - mask_threshold=0.3, - ) - ds = stats_l4.reduce(image_groups) - - assert (ds.level3.compute() == expected_l3).all() - assert (ds.level4.compute() == expected_l4).all() diff --git a/tests/test_lc_level3.py b/tests/test_lc_level3.py deleted file mode 100644 index 90ecbd7..0000000 --- a/tests/test_lc_level3.py +++ /dev/null @@ -1,107 +0,0 @@ -import numpy as np -import pandas as pd -import xarray as xr -import dask.array as da - -from odc.stats.plugins.l34_utils import lc_level3 -from odc.stats.plugins._utils import rasterize_vector_mask -from datacube.utils.geometry import GeoBox -from affine import Affine - -import pytest - -NODATA = 255 - -expected_l3_classes = [ - [111, 112, 215], - [124, 112, 215], - [220, 215, 216], - [220, 255, 220], -] - - -@pytest.fixture(scope="module") -def image_groups(): - l34 = np.array( - [ - [ - [110, 110, 210], - [124, 110, 210], - [221, 210, 210], - [223, np.nan, 223], - ] - ], - dtype="float32", - ) - - urban = np.array( - [ - [ - [215, 215, 215], - [216, 216, 215], - [116, 215, 216], - [216, 216, 216], - ] - ], - dtype="uint8", - ) - - cultivated = np.array( - [ - [ - [111, 112, 255], - [255, 112, 255], - [255, 255, 255], - [255, np.nan, np.nan], - ] - ], - dtype="float32", - ) - - tuples = [ - (np.datetime64("2000-01-01T00"), np.datetime64("2000-01-01")), - ] - index = pd.MultiIndex.from_tuples(tuples, names=["time", "solar_day"]) - - affine = Affine.translation(10, 0) * Affine.scale( - (20 - 10) / l34.shape[2], (5 - 0) / l34.shape[1] - ) - geobox = GeoBox( - crs="epsg:3577", affine=affine, width=l34.shape[2], height=l34.shape[1] - ) - coords = geobox.xr_coords() - - data_vars = { - "level_3_4": xr.DataArray( - da.from_array(l34, chunks=(1, -1, -1)), - dims=("spec", "y", "x"), - attrs={"nodata": 255}, - ), - "artificial_surface": xr.DataArray( - da.from_array(urban, chunks=(1, -1, -1)), - dims=("spec", "y", "x"), - attrs={"nodata": 255}, - ), - "cultivated": xr.DataArray( - da.from_array(cultivated, chunks=(1, -1, -1)), - dims=("spec", "y", "x"), - attrs={"nodata": 255}, - ), - } - xx = xr.Dataset(data_vars=data_vars, coords=coords) - xx = xx.assign_coords(xr.Coordinates.from_pandas_multiindex(index, "spec")) - return xx - - -def test_l3_classes(image_groups, urban_shape): - filter_expression = "mock > 9" - urban_mask = rasterize_vector_mask( - urban_shape, - image_groups.geobox.transform, - image_groups.artificial_surface.shape, - filter_expression=filter_expression, - threshold=0.3, - ) - - level3_classes = lc_level3.lc_level3(image_groups, urban_mask) - assert (level3_classes == expected_l3_classes).all()