From 9b75a301ad39f8cd6c1ebaea380bdb40a360e946 Mon Sep 17 00:00:00 2001 From: Emma Ai Date: Wed, 27 Nov 2024 12:19:12 +0000 Subject: [PATCH] remove unneeded test --- tests/test_lc_l4_ctv.py | 372 -------------------------- tests/test_lc_l4_natural_surface.py | 212 --------------- tests/test_lc_l4_nav.py | 400 ---------------------------- tests/test_lc_l4_ntv.py | 372 -------------------------- tests/test_lc_l4_water.py | 270 ------------------- 5 files changed, 1626 deletions(-) delete mode 100644 tests/test_lc_l4_ctv.py delete mode 100644 tests/test_lc_l4_natural_surface.py delete mode 100644 tests/test_lc_l4_nav.py delete mode 100644 tests/test_lc_l4_ntv.py delete mode 100644 tests/test_lc_l4_water.py diff --git a/tests/test_lc_l4_ctv.py b/tests/test_lc_l4_ctv.py deleted file mode 100644 index 92cb607..0000000 --- a/tests/test_lc_l4_ctv.py +++ /dev/null @@ -1,372 +0,0 @@ -import numpy as np -import xarray as xr -import dask.array as da - -from odc.stats.plugins.l34_utils import ( - l4_cultivated, - lc_level3, - l4_veg_cover, -) - -import pandas as pd - -NODATA = 255 - - -def image_groups(l34, urban, cultivated, woody, pv_pc_50): - - tuples = [ - (np.datetime64("2000-01-01T00"), np.datetime64("2000-01-01")), - ] - index = pd.MultiIndex.from_tuples(tuples, names=["time", "solar_day"]) - coords = { - "x": np.linspace(10, 20, l34.shape[2]), - "y": np.linspace(0, 5, l34.shape[1]), - } - - 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}, - ), - } - xx = xr.Dataset(data_vars=data_vars, coords=coords) - xx = xx.assign_coords(xr.Coordinates.from_pandas_multiindex(index, "spec")) - return xx - - -def test_ctv_classes_woody(veg_threshold): - - expected_cultivated_classes = [ - [13, 10, 9], - [110, 10, 10], - [13, 11, 11], - [12, 13, 10], - ] - - l34 = np.array( - [ - [ - [110, 110, 110], - [110, 110, 110], - [110, 110, 110], - [110, 110, 110], - ] - ], - dtype="uint8", - ) - - urban = np.array( - [ - [ - [216, 216, 216], - [216, 216, 216], - [216, 216, 216], - [216, 216, 216], - ] - ], - dtype="uint8", - ) - # 111 --> cultivated - cultivated = np.array( - [ - [ - [111, 111, 111], - [255, 111, 111], - [111, 111, 111], - [111, 111, 111], - ] - ], - dtype="uint8", - ) - - woody = np.array( - [ - [ - [113, 113, 113], - [113, 113, 113], - [113, 113, 113], - [113, 113, 113], - ] - ], - dtype="uint8", - ) - - pv_pc_50 = np.array( - [ - [ - [1, 64, 65], - [66, 40, 41], - [3, 16, 15], - [4, 1, 42], - ] - ], - dtype="uint8", - ) - xx = image_groups(l34, urban, cultivated, woody, pv_pc_50) - mock_urban_mask = da.ones(xx.artificial_surface.shape) - - level3 = lc_level3.lc_level3(xx, mock_urban_mask) - - veg_cover = l4_veg_cover.canopyco_veg_con(xx, veg_threshold) - l4_ctv = l4_cultivated.lc_l4_cultivated(xx.level_3_4, level3, xx.woody, veg_cover) - - assert (l4_ctv.compute() == expected_cultivated_classes).all() - - -def test_ctv_classes_herbaceous(veg_threshold): - - expected_cultivated_classes = [ - [18, 15, 14], - [110, 15, 15], - [18, 16, 16], - [17, 18, 15], - ] - - l34 = np.array( - [ - [ - [110, 110, 110], - [110, 110, 110], - [110, 110, 110], - [110, 110, 110], - ] - ], - dtype="uint8", - ) - - urban = np.array( - [ - [ - [216, 216, 216], - [216, 216, 216], - [216, 216, 216], - [216, 216, 216], - ] - ], - dtype="uint8", - ) - - cultivated = np.array( - [ - [ - [111, 111, 111], - [255, 111, 111], - [111, 111, 111], - [111, 111, 111], - ] - ], - dtype="uint8", - ) - - woody = np.array( - [ - [ - [114, 114, 114], - [114, 114, 114], - [114, 114, 114], - [114, 114, 114], - ] - ], - dtype="uint8", - ) - - pv_pc_50 = np.array( - [ - [ - [1, 64, 65], - [66, 40, 41], - [3, 16, 15], - [4, 1, 42], - ] - ], - dtype="uint8", - ) - xx = image_groups(l34, urban, cultivated, woody, pv_pc_50) - mock_urban_mask = da.ones(xx.artificial_surface.shape) - - level3 = lc_level3.lc_level3(xx, mock_urban_mask) - veg_cover = l4_veg_cover.canopyco_veg_con(xx, veg_threshold) - - l4_ctv = l4_cultivated.lc_l4_cultivated(xx.level_3_4, level3, xx.woody, veg_cover) - assert (l4_ctv.compute() == expected_cultivated_classes).all() - - -def test_ctv_classes_woody_herbaceous(veg_threshold): - - expected_cultivated_classes = [ - [13, 10, 9], - [110, 15, 15], - [13, 11, 11], - [17, 18, 15], - ] - - l34 = np.array( - [ - [ - [110, 110, 110], - [110, 110, 110], - [110, 110, 110], - [110, 110, 110], - ] - ], - dtype="int", - ) - - urban = np.array( - [ - [ - [216, 216, 216], - [216, 216, 216], - [216, 216, 216], - [216, 216, 216], - ] - ], - dtype="int", - ) - - cultivated = np.array( - [ - [ - [111, 111, 111], - [255, 111, 111], - [111, 111, 111], - [111, 111, 111], - ] - ], - dtype="int", - ) - - woody = np.array( - [ - [ - [113, 113, 113], - [114, 114, 114], - [113, 113, 113], - [114, 114, 114], - ] - ], - dtype="int", - ) - - pv_pc_50 = np.array( - [ - [ - [1, 64, 65], - [66, 40, 41], - [3, 16, 15], - [4, 1, 42], - ] - ], - dtype="int", - ) - xx = image_groups(l34, urban, cultivated, woody, pv_pc_50) - mock_urban_mask = da.ones(xx.artificial_surface.shape) - - level3 = lc_level3.lc_level3(xx, mock_urban_mask) - - veg_cover = l4_veg_cover.canopyco_veg_con(xx, veg_threshold) - l4_ctv = l4_cultivated.lc_l4_cultivated(xx.level_3_4, level3, xx.woody, veg_cover) - - assert (l4_ctv.compute() == expected_cultivated_classes).all() - - -def test_ctv_classes_no_vegcover(veg_threshold): - - expected_cultivated_classes = [ - [2, 2, 2], - [110, 3, 3], - [2, 2, 2], - [3, 3, 3], - ] - - l34 = np.array( - [ - [ - [110, 110, 110], - [110, 110, 110], - [110, 110, 110], - [110, 110, 110], - ] - ], - dtype="int", - ) - - urban = np.array( - [ - [ - [216, 216, 216], - [216, 216, 216], - [216, 216, 216], - [216, 216, 216], - ] - ], - dtype="int", - ) - - cultivated = np.array( - [ - [ - [111, 111, 111], - [255, 111, 111], - [111, 111, 111], - [111, 111, 111], - ] - ], - dtype="int", - ) - - woody = np.array( - [ - [ - [113, 113, 113], - [114, 114, 114], - [113, 113, 113], - [114, 114, 114], - ] - ], - dtype="int", - ) - - pv_pc_50 = np.array( - [ - [ - [255, 255, 255], - [255, 255, 255], - [255, 255, 255], - [255, 255, 255], - ] - ], - dtype="int", - ) - xx = image_groups(l34, urban, cultivated, woody, pv_pc_50) - - mock_urban_mask = da.ones(xx.artificial_surface.shape) - - level3 = lc_level3.lc_level3(xx, mock_urban_mask) - - veg_cover = l4_veg_cover.canopyco_veg_con(xx, veg_threshold) - l4_ctv = l4_cultivated.lc_l4_cultivated(xx.level_3_4, level3, xx.woody, veg_cover) - - assert (l4_ctv.compute() == expected_cultivated_classes).all() diff --git a/tests/test_lc_l4_natural_surface.py b/tests/test_lc_l4_natural_surface.py deleted file mode 100644 index b77e604..0000000 --- a/tests/test_lc_l4_natural_surface.py +++ /dev/null @@ -1,212 +0,0 @@ -""" - Unit tests for LandCover Natural Aquatic Vegetation classes -""" - -import numpy as np -import xarray as xr -import dask.array as da - -from odc.stats.plugins.l34_utils import ( - l4_cultivated, - lc_level3, - l4_veg_cover, - l4_natural_veg, - l4_natural_aquatic, - l4_surface, - l4_bare_gradation, -) - -import pandas as pd - -NODATA = 255 - - -def image_groups( - l34, urban, woody, bs_pc_50, pv_pc_50, cultivated, water_frequency, water_season -): - - tuples = [ - (np.datetime64("2000-01-01T00"), np.datetime64("2000-01-01")), - ] - index = pd.MultiIndex.from_tuples(tuples, names=["time", "solar_day"]) - coords = { - "x": np.linspace(10, 20, l34.shape[2]), - "y": np.linspace(0, 5, l34.shape[1]), - } - - 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_ns(veg_threshold): - expected_l4_srf_classes = [ - [95, 97, 93], - [97, 96, 96], - [95, 95, 95], - [94, 95, 96], - ] - - l34 = np.array( - [ - [ - [210, 210, 210], - [210, 210, 210], - [210, 210, 210], - [210, 210, 210], - ] - ], - dtype="uint8", - ) - - urban = np.array( - [ - [ - [216, 216, 215], - [216, 216, 216], - [216, 216, 216], - [216, 216, 216], - ] - ], - 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, 16, 15], - [4, 1, 42], - ] - ], - dtype="uint8", - ) - - bs_pc_50 = np.array( - [ - [ - [1, 64, NODATA], - [66, 40, 41], - [3, 16, 15], - [NODATA, 1, 42], - ] - ], - dtype="uint8", - ) - # 112 --> natural veg - 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], - [9, 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", - ) - - xx = image_groups( - l34, urban, woody, bs_pc_50, pv_pc_50, cultivated, water_frequency, water_season - ) - - mock_urban_mask = da.ones(xx.artificial_surface.shape) - level3 = lc_level3.lc_level3(xx, mock_urban_mask) - - veg_cover = l4_veg_cover.canopyco_veg_con(xx, veg_threshold) - - # Apply cultivated to match the code in Level4 processing - l4_ctv = l4_cultivated.lc_l4_cultivated(xx.level_3_4, level3, xx.woody, veg_cover) - l4_ctv_ntv = l4_natural_veg.lc_l4_natural_veg(l4_ctv, level3, xx.woody, veg_cover) - - l4_ctv_ntv_nav = l4_natural_aquatic.natural_auquatic_veg( - l4_ctv_ntv, veg_cover, xx.water_season - ) - - # Bare gradation - bare_threshold = [20, 60] - bare_gradation = l4_bare_gradation.bare_gradation(xx, bare_threshold, veg_cover) - - l4_ctv_ntv_nav_surface = l4_surface.lc_l4_surface( - l4_ctv_ntv_nav, level3, bare_gradation - ) - - assert (l4_ctv_ntv_nav_surface.compute() == expected_l4_srf_classes).all() diff --git a/tests/test_lc_l4_nav.py b/tests/test_lc_l4_nav.py deleted file mode 100644 index e3e1849..0000000 --- a/tests/test_lc_l4_nav.py +++ /dev/null @@ -1,400 +0,0 @@ -""" - Unit tests for LandCover Natural Aquatic Vegetation classes -""" - -import numpy as np -import xarray as xr -import dask.array as da - -from odc.stats.plugins.l34_utils import ( - l4_cultivated, - lc_level3, - l4_veg_cover, - l4_natural_veg, - l4_natural_aquatic, -) - -import pandas as pd - -NODATA = 255 - - -def image_groups( - l34, urban, cultivated, woody, pv_pc_50, water_frequency, water_season -): - - tuples = [ - (np.datetime64("2000-01-01T00"), np.datetime64("2000-01-01")), - ] - index = pd.MultiIndex.from_tuples(tuples, names=["time", "solar_day"]) - coords = { - "x": np.linspace(10, 20, l34.shape[2]), - "y": np.linspace(0, 5, l34.shape[1]), - } - - 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}, - ), - "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_ntv_classes_woody_herbaceous(veg_threshold): - expected_l4_ntv_classes = [[56, 56, 56], [57, 57, 57], [56, 56, 56], [57, 57, 57]] - - l34 = np.array( - [ - [ - [124, 124, 124], - [125, 125, 125], - [124, 124, 124], - [125, 125, 125], - ] - ], - dtype="uint8", - ) - - urban = np.array( - [ - [ - [216, 216, 216], - [216, 216, 216], - [216, 216, 216], - [216, 216, 216], - ] - ], - dtype="uint8", - ) - # 112 --> natural veg - cultivated = np.array( - [ - [ - [255, 255, 255], - [255, 255, 255], - [255, 255, 255], - [255, 255, 255], - ] - ], - dtype="uint8", - ) - - woody = np.array( - [ - [ - [113, 113, 113], - [113, 113, 255], - [114, 114, 114], - [114, 114, 255], - ] - ], - dtype="uint8", - ) - - pv_pc_50 = np.array( - [ - [ - [255, 255, 255], - [255, 255, 255], - [255, 255, 255], - [255, 255, 255], - ] - ], - dtype="uint8", - ) - - water_frequency = np.array( - [ - [ - [255, 255, 255], - [255, 255, 255], - [255, 255, 255], - [255, 255, 255], - ] - ], - dtype="uint8", - ) - - water_season = np.array( - [ - [ - [0, 0, 0], - [0, 0, 0], - [0, 0, 0], - [0, 0, 0], - ] - ], - dtype="uint8", - ) - - xx = image_groups( - l34, urban, cultivated, woody, pv_pc_50, water_frequency, water_season - ) - mock_urban_mask = da.ones(xx.artificial_surface.shape) - - level3 = lc_level3.lc_level3(xx, mock_urban_mask) - veg_cover = l4_veg_cover.canopyco_veg_con(xx, veg_threshold) - - # Apply cultivated to match the code in Level4 processing - l4_ctv = l4_cultivated.lc_l4_cultivated(xx.level_3_4, level3, xx.woody, veg_cover) - l4_ctv_ntv = l4_natural_veg.lc_l4_natural_veg(l4_ctv, level3, xx.woody, veg_cover) - - l4_ctv_ntv_nav = l4_natural_aquatic.natural_auquatic_veg( - l4_ctv_ntv, veg_cover, xx.water_season - ) - - assert (l4_ctv_ntv_nav.compute() == expected_l4_ntv_classes).all() - - -def test_ntv_herbaceous_seasonal_water_veg_cover(veg_threshold): - expected_l4_ntv_classes = [ - [91, 83, 79], - [80, 82, 83], - [91, 85, 86], - [89, 92, 82], - ] - - l34 = np.array( - [ - [ - [125, 125, 125], - [125, 125, 125], - [125, 125, 125], - [125, 125, 125], - ] - ], - dtype="uint8", - ) - - urban = np.array( - [ - [ - [216, 216, 216], - [216, 216, 216], - [216, 216, 216], - [216, 216, 216], - ] - ], - dtype="uint8", - ) - - cultivated = np.array( - [ - [ - [255, 255, 255], - [255, 255, 255], - [255, 255, 255], - [255, 255, 255], - ] - ], - dtype="uint8", - ) - - woody = np.array( - [ - [ - [114, 114, 114], - [114, 114, 114], - [114, 114, 114], - [114, 114, 114], - ] - ], - dtype="uint8", - ) - - pv_pc_50 = np.array( - [ - [ - [1, 64, 65], - [66, 40, 41], - [3, 16, 15], - [4, 1, 42], - ] - ], - dtype="uint8", - ) - water_frequency = np.array( - [ - [ - [2, 2, 2], - [2, 2, 2], - [2, 2, 2], - [2, 2, 2], - ] - ], - dtype="uint8", - ) - - water_season = np.array( - [ - [ - [1, 2, 1], - [2, 1, 2], - [1, 1, 2], - [2, 2, 1], - ] - ], - dtype="uint8", - ) - - xx = image_groups( - l34, urban, cultivated, woody, pv_pc_50, water_frequency, water_season - ) - mock_urban_mask = da.ones(xx.artificial_surface.shape) - - level3 = lc_level3.lc_level3(xx, mock_urban_mask) - veg_cover = l4_veg_cover.canopyco_veg_con(xx, veg_threshold) - - # Apply cultivated to match the code in Level4 processing - l4_ctv = l4_cultivated.lc_l4_cultivated(xx.level_3_4, level3, xx.woody, veg_cover) - l4_ctv_ntv = l4_natural_veg.lc_l4_natural_veg(l4_ctv, level3, xx.woody, veg_cover) - - l4_ctv_ntv_nav = l4_natural_aquatic.natural_auquatic_veg( - l4_ctv_ntv, veg_cover, xx.water_season - ) - - assert (l4_ctv_ntv_nav.compute() == expected_l4_ntv_classes).all() - - -def test_ntv_woody_seasonal_water_veg_cover(veg_threshold): - expected_l4_ntv_classes = [ - [76, 68, 64], - [65, 67, 68], - [76, 70, 71], - [74, 77, 67], - ] - - l34 = np.array( - [ - [ - [124, 124, 124], - [124, 124, 124], - [124, 124, 124], - [124, 124, 124], - ] - ], - dtype="uint8", - ) - - urban = np.array( - [ - [ - [216, 216, 216], - [216, 216, 216], - [216, 216, 216], - [216, 216, 216], - ] - ], - dtype="uint8", - ) - - cultivated = np.array( - [ - [ - [255, 255, 255], - [255, 255, 255], - [255, 255, 255], - [255, 255, 255], - ] - ], - dtype="uint8", - ) - - woody = np.array( - [ - [ - [114, 114, 114], - [114, 114, 114], - [114, 114, 114], - [114, 114, 114], - ] - ], - dtype="uint8", - ) - - pv_pc_50 = np.array( - [ - [ - [1, 64, 65], - [66, 40, 41], - [3, 16, 15], - [4, 1, 42], - ] - ], - dtype="uint8", - ) - - water_frequency = np.array( - [ - [ - [1, 2, 3], - [1, 2, 3], - [1, 2, 3], - [1, 2, 3], - ] - ], - dtype="uint8", - ) - - water_season = np.array( - [ - [ - [1, 2, 1], - [2, 1, 2], - [1, 1, 2], - [2, 2, 1], - ] - ], - dtype="uint8", - ) - xx = image_groups( - l34, urban, cultivated, woody, pv_pc_50, water_frequency, water_season - ) - mock_urban_mask = da.ones(xx.artificial_surface.shape) - - level3 = lc_level3.lc_level3(xx, mock_urban_mask) - veg_cover = l4_veg_cover.canopyco_veg_con(xx, veg_threshold) - - # Apply cultivated to match the code in Level4 processing - l4_ctv = l4_cultivated.lc_l4_cultivated(xx.level_3_4, level3, xx.woody, veg_cover) - l4_ctv_ntv = l4_natural_veg.lc_l4_natural_veg(l4_ctv, level3, xx.woody, veg_cover) - - l4_ctv_ntv_nav = l4_natural_aquatic.natural_auquatic_veg( - l4_ctv_ntv, veg_cover, xx.water_season - ) - - assert (l4_ctv_ntv_nav.compute() == expected_l4_ntv_classes).all() diff --git a/tests/test_lc_l4_ntv.py b/tests/test_lc_l4_ntv.py deleted file mode 100644 index fb3bbe4..0000000 --- a/tests/test_lc_l4_ntv.py +++ /dev/null @@ -1,372 +0,0 @@ -""" - Unit tests for LandCover Natural Terrestrial Vegetated classes -""" - -import numpy as np -import xarray as xr -import dask.array as da - -from odc.stats.plugins.l34_utils import ( - lc_level3, - l4_veg_cover, - l4_natural_veg, -) - -import pandas as pd - -NODATA = 255 - - -def image_groups(l34, urban, cultivated, woody, pv_pc_50): - - tuples = [ - (np.datetime64("2000-01-01T00"), np.datetime64("2000-01-01")), - ] - index = pd.MultiIndex.from_tuples(tuples, names=["time", "solar_day"]) - coords = { - "x": np.linspace(10, 20, l34.shape[2]), - "y": np.linspace(0, 5, l34.shape[1]), - } - - 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}, - ), - } - xx = xr.Dataset(data_vars=data_vars, coords=coords) - xx = xx.assign_coords(xr.Coordinates.from_pandas_multiindex(index, "spec")) - return xx - - -def test_ntv_classes_herbaceous(veg_threshold): - - expected_natural_terrestrial_veg_classes = [ - [36, 33, 32], - [110, 33, 33], - [36, 34, 34], - [35, 36, 33], - ] - - l34 = np.array( - [ - [ - [110, 110, 110], - [110, 110, 110], - [110, 110, 110], - [110, 110, 110], - ] - ], - dtype="uint8", - ) - - urban = np.array( - [ - [ - [216, 216, 216], - [216, 216, 216], - [216, 216, 216], - [216, 216, 216], - ] - ], - dtype="uint8", - ) - # 112 --> natural veg - cultivated = np.array( - [ - [ - [112, 112, 112], - [255, 112, 112], - [112, 112, 112], - [112, 112, 112], - ] - ], - dtype="uint8", - ) - - woody = np.array( - [ - [ - [114, 114, 114], - [114, 114, 114], - [114, 114, 114], - [114, 114, 114], - ] - ], - dtype="uint8", - ) - - pv_pc_50 = np.array( - [ - [ - [1, 64, 65], - [66, 40, 41], - [3, 16, 15], - [4, 1, 42], - ] - ], - dtype="uint8", - ) - xx = image_groups(l34, urban, cultivated, woody, pv_pc_50) - mock_urban_mask = da.ones(xx.artificial_surface.shape) - - level3 = lc_level3.lc_level3(xx, mock_urban_mask) - - veg_cover = l4_veg_cover.canopyco_veg_con(xx, veg_threshold) - l4_ntv = l4_natural_veg.lc_l4_natural_veg(xx.level_3_4, level3, xx.woody, veg_cover) - assert (l4_ntv.compute() == expected_natural_terrestrial_veg_classes).all() - - -def test_ntv_classes_woody(veg_threshold): - - expected_natural_terrestrial_veg_classes = [ - [31, 28, 27], - [110, 28, 28], - [31, 29, 29], - [30, 26, 28], - ] - - l34 = np.array( - [ - [ - [110, 110, 110], - [110, 110, 110], - [110, 110, 110], - [110, 110, 110], - ] - ], - dtype="uint8", - ) - - urban = np.array( - [ - [ - [216, 216, 216], - [216, 216, 216], - [216, 216, 216], - [216, 216, 216], - ] - ], - dtype="uint8", - ) - # 112 --> natural veg - cultivated = np.array( - [ - [ - [112, 112, 112], - [255, 112, 112], - [112, 112, 112], - [112, 112, 112], - ] - ], - dtype="uint8", - ) - - woody = np.array( - [ - [ - [113, 113, 113], - [113, 113, 113], - [113, 113, 113], - [113, 255, 113], - ] - ], - dtype="uint8", - ) - - pv_pc_50 = np.array( - [ - [ - [1, 64, 65], - [66, 40, 41], - [3, 16, 15], - [4, 1, 42], - ] - ], - dtype="uint8", - ) - xx = image_groups(l34, urban, cultivated, woody, pv_pc_50) - mock_urban_mask = da.ones(xx.artificial_surface.shape) - - level3 = lc_level3.lc_level3(xx, mock_urban_mask) - veg_cover = l4_veg_cover.canopyco_veg_con(xx, veg_threshold) - - l4_ntv = l4_natural_veg.lc_l4_natural_veg(xx.level_3_4, level3, xx.woody, veg_cover) - assert (l4_ntv.compute() == expected_natural_terrestrial_veg_classes).all() - - -def test_ntv_classes_no_veg(veg_threshold): - - expected_natural_terrestrial_veg_classes = [ - [20, 20, 20], - [110, 21, 21], - [20, 20, 20], - [21, 21, 21], - ] - - l34 = np.array( - [ - [ - [110, 110, 110], - [110, 110, 110], - [110, 110, 110], - [110, 110, 110], - ] - ], - dtype="uint8", - ) - - urban = np.array( - [ - [ - [216, 216, 216], - [216, 216, 216], - [216, 216, 216], - [216, 216, 216], - ] - ], - dtype="uint8", - ) - # 112 --> natural veg - cultivated = np.array( - [ - [ - [112, 112, 112], - [255, 112, 112], - [112, 112, 112], - [112, 112, 112], - ] - ], - dtype="uint8", - ) - - woody = np.array( - [ - [ - [113, 113, 113], - [114, 114, 114], - [113, 113, 113], - [114, 114, 114], - ] - ], - dtype="uint8", - ) - - pv_pc_50 = np.array( - [ - [ - [255, 255, 255], - [255, 255, 255], - [255, 255, 255], - [255, 255, 255], - ] - ], - dtype="uint8", - ) - xx = image_groups(l34, urban, cultivated, woody, pv_pc_50) - mock_urban_mask = da.ones(xx.artificial_surface.shape) - - level3 = lc_level3.lc_level3(xx, mock_urban_mask) - - veg_cover = l4_veg_cover.canopyco_veg_con(xx, veg_threshold) - l4_ntv = l4_natural_veg.lc_l4_natural_veg(xx.level_3_4, level3, xx.woody, veg_cover) - assert (l4_ntv.compute() == expected_natural_terrestrial_veg_classes).all() - - -def test_ntv_classes_no_lifeform(veg_threshold): - - expected_natural_terrestrial_veg_classes = [ - [26, 23, 22], - [22, 23, 23], - [26, 24, 24], - [25, 26, 23], - ] - - l34 = np.array( - [ - [ - [110, 110, 110], - [110, 110, 110], - [110, 110, 110], - [110, 110, 110], - ] - ], - dtype="uint8", - ) - - urban = np.array( - [ - [ - [216, 216, 216], - [216, 216, 216], - [216, 216, 216], - [216, 216, 216], - ] - ], - dtype="uint8", - ) - # 112 --> natural veg - cultivated = np.array( - [ - [ - [112, 112, 112], - [112, 112, 112], - [112, 112, 112], - [112, 112, 112], - ] - ], - dtype="uint8", - ) - - woody = np.array( - [ - [ - [255, 255, 255], - [255, 255, 255], - [255, 255, 255], - [255, 255, 255], - ] - ], - dtype="uint8", - ) - - pv_pc_50 = np.array( - [ - [ - [1, 64, 65], - [66, 40, 41], - [3, 16, 15], - [4, 1, 42], - ] - ], - dtype="uint8", - ) - xx = image_groups(l34, urban, cultivated, woody, pv_pc_50) - mock_urban_mask = da.ones(xx.artificial_surface.shape) - - level3 = lc_level3.lc_level3(xx, mock_urban_mask) - - veg_cover = l4_veg_cover.canopyco_veg_con(xx, veg_threshold) - l4_ntv = l4_natural_veg.lc_l4_natural_veg(xx.level_3_4, level3, xx.woody, veg_cover) - assert (l4_ntv.compute() == expected_natural_terrestrial_veg_classes).all() diff --git a/tests/test_lc_l4_water.py b/tests/test_lc_l4_water.py deleted file mode 100644 index e0d021e..0000000 --- a/tests/test_lc_l4_water.py +++ /dev/null @@ -1,270 +0,0 @@ -""" - Unit tests for LandCover water classes -""" - -import numpy as np -import xarray as xr -import dask.array as da - -from odc.stats.plugins.l34_utils import ( - l4_water_persistence, - l4_water, -) - -import pandas as pd - -NODATA = 255 - - -# @pytest.fixture(scope="module") -def image_groups(l34, urban, cultivated, woody, bs_pc_50, pv_pc_50, water_frequency): - - tuples = [ - (np.datetime64("2000-01-01T00"), np.datetime64("2000-01-01")), - ] - index = pd.MultiIndex.from_tuples(tuples, names=["time", "solar_day"]) - coords = { - "x": np.linspace(10, 20, l34.shape[2]), - "y": np.linspace(0, 5, l34.shape[1]), - } - - 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_class": xr.DataArray( - da.from_array(cultivated, chunks=(1, -1, -1)), - dims=("spec", "y", "x"), - attrs={"nodata": 255}, - ), - "woody_cover": 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}, - ), - } - xx = xr.Dataset(data_vars=data_vars, coords=coords) - xx = xx.assign_coords(xr.Coordinates.from_pandas_multiindex(index, "spec")) - return xx - - -def test_water_classes(watper_threshold): - expected_water_classes = [ - [[104, 104, 104], [103, 103, 103], [102, 102, 101], [99, 101, 101]], - ] - - l34 = np.array( - [ - [ - [221, 221, 221], - [221, 221, 221], - [221, 221, 221], - [221, 221, 221], - ] - ], - dtype="uint8", - ) - - urban = np.array( - [ - [ - [216, 216, 216], - [216, 216, 216], - [216, 216, 216], - [216, 216, 216], - ] - ], - dtype="uint8", - ) - # 112 --> natural veg - cultivated = np.array( - [ - [ - [112, 112, 112], - [255, 112, 112], - [112, 112, 112], - [112, 112, 112], - ] - ], - dtype="uint8", - ) - - woody = np.array( - [ - [ - [114, 114, 114], - [114, 114, 114], - [114, 114, 114], - [114, 114, 114], - ] - ], - dtype="uint8", - ) - - pv_pc_50 = np.array( - [ - [ - [1, 64, 65], - [66, 40, 41], - [3, 16, 15], - [4, 1, 42], - ] - ], - dtype="uint8", - ) - bs_pc_50 = np.array( - [ - [ - [1, 64, NODATA], - [66, 40, 41], - [3, 16, 15], - [NODATA, 1, 42], - ] - ], - dtype="uint8", - ) - water_frequency = np.array( - [ - [ - [1, 3, 2], - [4, 5, 6], - [9, 7, 11], - [NODATA, 11, 12], - ] - ], - dtype="float", - ) - xx = image_groups( - l34, urban, cultivated, woody, bs_pc_50, pv_pc_50, water_frequency - ) - - # Water persistence - water_persistence = l4_water_persistence.water_persistence(xx, watper_threshold) - - l4_water_classes = l4_water.water_classification(xx, water_persistence) - - assert (l4_water_classes.compute() == expected_water_classes).all() - - -def test_water_intertidal(watper_threshold): - - expected_water_classes = [ - [100, 100, 100], - [100, 100, 100], - [102, 102, 101], - [101, 99, 100], - ] - - l34 = np.array( - [ - [ - [223, 223, 223], - [223, 223, 223], - [221, 221, 221], - [221, 221, 223], - ] - ], - dtype="uint8", - ) - - urban = np.array( - [ - [ - [216, 216, 216], - [216, 216, 216], - [216, 216, 216], - [216, 216, 216], - ] - ], - dtype="uint8", - ) - # 112 --> natural veg - cultivated = np.array( - [ - [ - [112, 112, 112], - [255, 112, 112], - [112, 112, 112], - [112, 112, 112], - ] - ], - dtype="uint8", - ) - - woody = np.array( - [ - [ - [114, 114, 114], - [114, 114, 114], - [114, 114, 114], - [114, 114, 114], - ] - ], - dtype="uint8", - ) - - pv_pc_50 = np.array( - [ - [ - [1, 64, 65], - [66, 40, 41], - [3, 16, 15], - [4, 1, 42], - ] - ], - dtype="uint8", - ) - bs_pc_50 = np.array( - [ - [ - [1, 64, NODATA], - [66, 40, 41], - [3, 16, 15], - [NODATA, 1, 42], - ] - ], - dtype="uint8", - ) - water_frequency = np.array( - [ - [ - [1, 3, 2], - [4, 5, 6], - [9, 7, 11], - [10, 255, 255], - ] - ], - dtype="uint8", - ) - xx = image_groups( - l34, urban, cultivated, woody, bs_pc_50, pv_pc_50, water_frequency - ) - - # Water persistence - water_persistence = l4_water_persistence.water_persistence(xx, watper_threshold) - - l4_water_classes = l4_water.water_classification(xx, water_persistence) - - assert (l4_water_classes.compute() == expected_water_classes).all()