From fbe2fce1f963ee22b534e9ff7a1ddbf2b04919cd Mon Sep 17 00:00:00 2001 From: Emma Ai Date: Mon, 25 Nov 2024 00:12:15 +0000 Subject: [PATCH] deconvolute landcover l34 tests --- odc/stats/plugins/l34_utils/lc_level3.py | 1 + tests/conftest.py | 10 +++++++ tests/test_lc_l34.py | 2 +- tests/test_lc_l4_ctv.py | 36 ++++++++++++------------ tests/test_lc_l4_natural_surface.py | 8 +++--- tests/test_lc_l4_nav.py | 27 +++++++++--------- tests/test_lc_l4_ntv.py | 35 +++++++++++------------ tests/test_lc_l4_water.py | 19 ++++--------- 8 files changed, 69 insertions(+), 69 deletions(-) diff --git a/odc/stats/plugins/l34_utils/lc_level3.py b/odc/stats/plugins/l34_utils/lc_level3.py index e4e4f552..d6ae6cd3 100644 --- a/odc/stats/plugins/l34_utils/lc_level3.py +++ b/odc/stats/plugins/l34_utils/lc_level3.py @@ -21,6 +21,7 @@ def lc_level3(xx: xr.Dataset, urban_mask): ) # Mask urban results with bare sfc (210) + # and urban mask res = expr_eval( "where((a==_u)&(c>0), b, a)", diff --git a/tests/conftest.py b/tests/conftest.py index a8a6751d..3675c165 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -215,3 +215,13 @@ def urban_shape(): ) as dst: dst.write(data) return filename + + +@pytest.fixture() +def veg_threshold(): + return [1, 4, 15, 40, 65, 100] + + +@pytest.fixture() +def watper_threshold(): + return [1, 4, 7, 10] diff --git a/tests/test_lc_l34.py b/tests/test_lc_l34.py index 552c60f3..54706d05 100644 --- a/tests/test_lc_l34.py +++ b/tests/test_lc_l34.py @@ -172,7 +172,7 @@ def image_groups(): return xx -def test_l4_classes(image_groups): +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]] diff --git a/tests/test_lc_l4_ctv.py b/tests/test_lc_l4_ctv.py index 16546d0c..92cb6073 100644 --- a/tests/test_lc_l4_ctv.py +++ b/tests/test_lc_l4_ctv.py @@ -2,7 +2,6 @@ import xarray as xr import dask.array as da -from odc.stats.plugins.lc_level34 import StatsLccsLevel4 from odc.stats.plugins.l34_utils import ( l4_cultivated, lc_level3, @@ -31,7 +30,7 @@ def image_groups(l34, urban, cultivated, woody, pv_pc_50): dims=("spec", "y", "x"), attrs={"nodata": 255}, ), - "urban_classes": xr.DataArray( + "artificial_surface": xr.DataArray( da.from_array(urban, chunks=(1, -1, -1)), dims=("spec", "y", "x"), attrs={"nodata": 255}, @@ -57,7 +56,7 @@ def image_groups(l34, urban, cultivated, woody, pv_pc_50): return xx -def test_ctv_classes_woody(): +def test_ctv_classes_woody(veg_threshold): expected_cultivated_classes = [ [13, 10, 9], @@ -126,17 +125,17 @@ def test_ctv_classes_woody(): dtype="uint8", ) xx = image_groups(l34, urban, cultivated, woody, pv_pc_50) + mock_urban_mask = da.ones(xx.artificial_surface.shape) - stats_l4 = StatsLccsLevel4() - level3 = lc_level3.lc_level3(xx) + level3 = lc_level3.lc_level3(xx, mock_urban_mask) - veg_cover = l4_veg_cover.canopyco_veg_con(xx, stats_l4.veg_threshold) + 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(): +def test_ctv_classes_herbaceous(veg_threshold): expected_cultivated_classes = [ [18, 15, 14], @@ -205,16 +204,16 @@ def test_ctv_classes_herbaceous(): dtype="uint8", ) xx = image_groups(l34, urban, cultivated, woody, pv_pc_50) + mock_urban_mask = da.ones(xx.artificial_surface.shape) - stats_l4 = StatsLccsLevel4() - level3 = lc_level3.lc_level3(xx) - veg_cover = l4_veg_cover.canopyco_veg_con(xx, stats_l4.veg_threshold) + 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(): +def test_ctv_classes_woody_herbaceous(veg_threshold): expected_cultivated_classes = [ [13, 10, 9], @@ -283,17 +282,17 @@ def test_ctv_classes_woody_herbaceous(): dtype="int", ) xx = image_groups(l34, urban, cultivated, woody, pv_pc_50) + mock_urban_mask = da.ones(xx.artificial_surface.shape) - stats_l4 = StatsLccsLevel4() - level3 = lc_level3.lc_level3(xx) + level3 = lc_level3.lc_level3(xx, mock_urban_mask) - veg_cover = l4_veg_cover.canopyco_veg_con(xx, stats_l4.veg_threshold) + 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(): +def test_ctv_classes_no_vegcover(veg_threshold): expected_cultivated_classes = [ [2, 2, 2], @@ -363,10 +362,11 @@ def test_ctv_classes_no_vegcover(): ) xx = image_groups(l34, urban, cultivated, woody, pv_pc_50) - stats_l4 = StatsLccsLevel4() - level3 = lc_level3.lc_level3(xx) + mock_urban_mask = da.ones(xx.artificial_surface.shape) - veg_cover = l4_veg_cover.canopyco_veg_con(xx, stats_l4.veg_threshold) + 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 index b53bb0a4..b77e6044 100644 --- a/tests/test_lc_l4_natural_surface.py +++ b/tests/test_lc_l4_natural_surface.py @@ -40,7 +40,7 @@ def image_groups( dims=("spec", "y", "x"), attrs={"nodata": 255}, ), - "urban_classes": xr.DataArray( + "artificial_surface": xr.DataArray( da.from_array(urban, chunks=(1, -1, -1)), dims=("spec", "y", "x"), attrs={"nodata": 255}, @@ -81,7 +81,7 @@ def image_groups( return xx -def test_ns(): +def test_ns(veg_threshold): expected_l4_srf_classes = [ [95, 97, 93], [97, 96, 96], @@ -188,9 +188,9 @@ def test_ns(): l34, urban, woody, bs_pc_50, pv_pc_50, cultivated, water_frequency, water_season ) - level3 = lc_level3.lc_level3(xx) + mock_urban_mask = da.ones(xx.artificial_surface.shape) + level3 = lc_level3.lc_level3(xx, mock_urban_mask) - veg_threshold = [1, 4, 15, 40, 65, 100] veg_cover = l4_veg_cover.canopyco_veg_con(xx, veg_threshold) # Apply cultivated to match the code in Level4 processing diff --git a/tests/test_lc_l4_nav.py b/tests/test_lc_l4_nav.py index f91ac6fb..e3e1849e 100644 --- a/tests/test_lc_l4_nav.py +++ b/tests/test_lc_l4_nav.py @@ -6,7 +6,6 @@ import xarray as xr import dask.array as da -from odc.stats.plugins.lc_level34 import StatsLccsLevel4 from odc.stats.plugins.l34_utils import ( l4_cultivated, lc_level3, @@ -39,7 +38,7 @@ def image_groups( dims=("spec", "y", "x"), attrs={"nodata": 255}, ), - "urban_classes": xr.DataArray( + "artificial_surface": xr.DataArray( da.from_array(urban, chunks=(1, -1, -1)), dims=("spec", "y", "x"), attrs={"nodata": 255}, @@ -75,7 +74,7 @@ def image_groups( return xx -def test_ntv_classes_woody_herbaceous(): +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( @@ -165,10 +164,10 @@ def test_ntv_classes_woody_herbaceous(): xx = image_groups( l34, urban, cultivated, woody, pv_pc_50, water_frequency, water_season ) + mock_urban_mask = da.ones(xx.artificial_surface.shape) - stats_l4 = StatsLccsLevel4() - level3 = lc_level3.lc_level3(xx) - veg_cover = l4_veg_cover.canopyco_veg_con(xx, stats_l4.veg_threshold) + 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) @@ -181,7 +180,7 @@ def test_ntv_classes_woody_herbaceous(): assert (l4_ctv_ntv_nav.compute() == expected_l4_ntv_classes).all() -def test_ntv_herbaceous_seasonal_water_veg_cover(): +def test_ntv_herbaceous_seasonal_water_veg_cover(veg_threshold): expected_l4_ntv_classes = [ [91, 83, 79], [80, 82, 83], @@ -275,10 +274,10 @@ def test_ntv_herbaceous_seasonal_water_veg_cover(): xx = image_groups( l34, urban, cultivated, woody, pv_pc_50, water_frequency, water_season ) + mock_urban_mask = da.ones(xx.artificial_surface.shape) - stats_l4 = StatsLccsLevel4() - level3 = lc_level3.lc_level3(xx) - veg_cover = l4_veg_cover.canopyco_veg_con(xx, stats_l4.veg_threshold) + 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) @@ -291,7 +290,7 @@ def test_ntv_herbaceous_seasonal_water_veg_cover(): assert (l4_ctv_ntv_nav.compute() == expected_l4_ntv_classes).all() -def test_ntv_woody_seasonal_water_veg_cover(): +def test_ntv_woody_seasonal_water_veg_cover(veg_threshold): expected_l4_ntv_classes = [ [76, 68, 64], [65, 67, 68], @@ -385,10 +384,10 @@ def test_ntv_woody_seasonal_water_veg_cover(): xx = image_groups( l34, urban, cultivated, woody, pv_pc_50, water_frequency, water_season ) + mock_urban_mask = da.ones(xx.artificial_surface.shape) - stats_l4 = StatsLccsLevel4() - level3 = lc_level3.lc_level3(xx) - veg_cover = l4_veg_cover.canopyco_veg_con(xx, stats_l4.veg_threshold) + 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) diff --git a/tests/test_lc_l4_ntv.py b/tests/test_lc_l4_ntv.py index 0d89005e..fb3bbe4f 100644 --- a/tests/test_lc_l4_ntv.py +++ b/tests/test_lc_l4_ntv.py @@ -6,7 +6,6 @@ import xarray as xr import dask.array as da -from odc.stats.plugins.lc_level34 import StatsLccsLevel4 from odc.stats.plugins.l34_utils import ( lc_level3, l4_veg_cover, @@ -35,7 +34,7 @@ def image_groups(l34, urban, cultivated, woody, pv_pc_50): dims=("spec", "y", "x"), attrs={"nodata": 255}, ), - "urban_classes": xr.DataArray( + "artificial_surface": xr.DataArray( da.from_array(urban, chunks=(1, -1, -1)), dims=("spec", "y", "x"), attrs={"nodata": 255}, @@ -61,7 +60,7 @@ def image_groups(l34, urban, cultivated, woody, pv_pc_50): return xx -def test_ntv_classes_herbaceous(): +def test_ntv_classes_herbaceous(veg_threshold): expected_natural_terrestrial_veg_classes = [ [36, 33, 32], @@ -130,16 +129,16 @@ def test_ntv_classes_herbaceous(): dtype="uint8", ) xx = image_groups(l34, urban, cultivated, woody, pv_pc_50) + mock_urban_mask = da.ones(xx.artificial_surface.shape) - stats_l4 = StatsLccsLevel4() - level3 = lc_level3.lc_level3(xx) + level3 = lc_level3.lc_level3(xx, mock_urban_mask) - veg_cover = l4_veg_cover.canopyco_veg_con(xx, stats_l4.veg_threshold) + 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(): +def test_ntv_classes_woody(veg_threshold): expected_natural_terrestrial_veg_classes = [ [31, 28, 27], @@ -208,16 +207,16 @@ def test_ntv_classes_woody(): dtype="uint8", ) xx = image_groups(l34, urban, cultivated, woody, pv_pc_50) + mock_urban_mask = da.ones(xx.artificial_surface.shape) - stats_l4 = StatsLccsLevel4() - level3 = lc_level3.lc_level3(xx) - veg_cover = l4_veg_cover.canopyco_veg_con(xx, stats_l4.veg_threshold) + 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(): +def test_ntv_classes_no_veg(veg_threshold): expected_natural_terrestrial_veg_classes = [ [20, 20, 20], @@ -286,16 +285,16 @@ def test_ntv_classes_no_veg(): dtype="uint8", ) xx = image_groups(l34, urban, cultivated, woody, pv_pc_50) + mock_urban_mask = da.ones(xx.artificial_surface.shape) - stats_l4 = StatsLccsLevel4() - level3 = lc_level3.lc_level3(xx) + level3 = lc_level3.lc_level3(xx, mock_urban_mask) - veg_cover = l4_veg_cover.canopyco_veg_con(xx, stats_l4.veg_threshold) + 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(): +def test_ntv_classes_no_lifeform(veg_threshold): expected_natural_terrestrial_veg_classes = [ [26, 23, 22], @@ -364,10 +363,10 @@ def test_ntv_classes_no_lifeform(): dtype="uint8", ) xx = image_groups(l34, urban, cultivated, woody, pv_pc_50) + mock_urban_mask = da.ones(xx.artificial_surface.shape) - stats_l4 = StatsLccsLevel4() - level3 = lc_level3.lc_level3(xx) + level3 = lc_level3.lc_level3(xx, mock_urban_mask) - veg_cover = l4_veg_cover.canopyco_veg_con(xx, stats_l4.veg_threshold) + 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 index 851e3d75..e0d021ee 100644 --- a/tests/test_lc_l4_water.py +++ b/tests/test_lc_l4_water.py @@ -6,7 +6,6 @@ import xarray as xr import dask.array as da -from odc.stats.plugins.lc_level34 import StatsLccsLevel4 from odc.stats.plugins.l34_utils import ( l4_water_persistence, l4_water, @@ -35,7 +34,7 @@ def image_groups(l34, urban, cultivated, woody, bs_pc_50, pv_pc_50, water_freque dims=("spec", "y", "x"), attrs={"nodata": 255}, ), - "urban_classes": xr.DataArray( + "artificial_surface": xr.DataArray( da.from_array(urban, chunks=(1, -1, -1)), dims=("spec", "y", "x"), attrs={"nodata": 255}, @@ -71,7 +70,7 @@ def image_groups(l34, urban, cultivated, woody, bs_pc_50, pv_pc_50, water_freque return xx -def test_water_classes(): +def test_water_classes(watper_threshold): expected_water_classes = [ [[104, 104, 104], [103, 103, 103], [102, 102, 101], [99, 101, 101]], ] @@ -161,19 +160,15 @@ def test_water_classes(): l34, urban, cultivated, woody, bs_pc_50, pv_pc_50, water_frequency ) - stats_l4 = StatsLccsLevel4() - # Water persistence - water_persistence = l4_water_persistence.water_persistence( - xx, stats_l4.watper_threshold - ) + 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(): +def test_water_intertidal(watper_threshold): expected_water_classes = [ [100, 100, 100], @@ -267,12 +262,8 @@ def test_water_intertidal(): l34, urban, cultivated, woody, bs_pc_50, pv_pc_50, water_frequency ) - stats_l4 = StatsLccsLevel4() - # Water persistence - water_persistence = l4_water_persistence.water_persistence( - xx, stats_l4.watper_threshold - ) + water_persistence = l4_water_persistence.water_persistence(xx, watper_threshold) l4_water_classes = l4_water.water_classification(xx, water_persistence)