diff --git a/odc/stats/plugins/l34_utils/l4_natural_aquatic.py b/odc/stats/plugins/l34_utils/l4_natural_aquatic.py index 0036ae43..34afaa4e 100644 --- a/odc/stats/plugins/l34_utils/l4_natural_aquatic.py +++ b/odc/stats/plugins/l34_utils/l4_natural_aquatic.py @@ -5,400 +5,281 @@ from odc.stats._algebra import expr_eval -def natural_auquatic_veg(l4, lifeform, veg_cover, water_seasonality): +def natural_auquatic_veg(l4, veg_cover, water_seasonality): - l4 = expr_eval( - "where((a==124)&(b==10)&(c==1)&(d==1), 64, a)", + # mark woody/herbaceous + # mangroves -> woody + # everything else -> herbaceous + res = expr_eval( + "where((a==124), 56, a)", { "a": l4, - "b": veg_cover, - "c": lifeform, - "d": water_seasonality, - }, - name="mark_cultivated", - dtype="uint8", - ) - l4 = expr_eval( - "where((a==124)&(b==10)&(c==1)&(d==2), 65, a)", - { - "a": l4, - "b": veg_cover, - "c": lifeform, - "d": water_seasonality, }, - name="mark_cultivated", + name="mark_woody", dtype="uint8", ) - l4 = expr_eval( - "where((a==124)&(b==12)&(c==1)&(d==1), 67, a)", + res = expr_eval( + "where((a==125), 57, a)", { - "a": l4, - "b": veg_cover, - "c": lifeform, - "d": water_seasonality, + "a": res, }, - name="mark_cultivated", + name="mark_herbaceous", dtype="uint8", ) - l4 = expr_eval( - "where((a==124)&(b==12)&(c==1)&(d==2), 68, a)", - { - "a": l4, - "b": veg_cover, - "c": lifeform, - "d": water_seasonality, - }, - name="mark_cultivated", - dtype="uint8", - ) + # res = expr_eval( + # "where((a!=124)|(a!=125), 255, a)", + # { + # "a": res, + # }, + # name="mark_nodata", + # dtype="uint8", + # ) - l4 = expr_eval( - "where((a==124)&(b==13)&(c==1)&(d==1), 70, a)", + # mark water season + # use some value not used in final class + res = expr_eval( + "where((a==56)&(b==1), 254, a)", { - "a": l4, - "b": veg_cover, - "c": lifeform, - "d": water_seasonality, + "a": res, + "b": water_seasonality, }, - name="mark_cultivated", + name="mark_water_season", dtype="uint8", ) - l4 = expr_eval( - "where((a==124)&(b==13)&(c==1)&(d==2), 71, a)", + res = expr_eval( + "where((a==56)&(b==2), 253, a)", { - "a": l4, - "b": veg_cover, - "c": lifeform, - "d": water_seasonality, + "a": res, + "b": water_seasonality, }, - name="mark_cultivated", + name="mark_water_season", dtype="uint8", ) - l4 = expr_eval( - "where((a==124)&(b==15)&(c==1)&(d==1), 73, a)", + res = expr_eval( + "where((a==57)&(b==1), 252, a)", { - "a": l4, - "b": veg_cover, - "c": lifeform, - "d": water_seasonality, + "a": res, + "b": water_seasonality, }, - name="mark_cultivated", + name="mark_water_season", dtype="uint8", ) - l4 = expr_eval( - "where((a==124)&(b==15)&(c==1)&(d==2), 74, a)", + res = expr_eval( + "where((a==57)&(b==2), 251, a)", { - "a": l4, - "b": veg_cover, - "c": lifeform, - "d": water_seasonality, + "a": res, + "b": water_seasonality, }, - name="mark_cultivated", + name="mark_water_season", dtype="uint8", ) - l4 = expr_eval( - "where((a==124)&(b==16)&(c==1)&(d==1), 76, a)", + # mark final + + res = expr_eval( + "where((a==254)&(b==10), 64, a)", { - "a": l4, + "a": res, "b": veg_cover, - "c": lifeform, - "d": water_seasonality, }, - name="mark_cultivated", + name="mark_final", dtype="uint8", ) - l4 = expr_eval( - "where((a==124)&(b==16)&(c==1)&(d==2), 77, a)", + res = expr_eval( + "where((a==253)&(b==10), 65, a)", { - "a": l4, + "a": res, "b": veg_cover, - "c": lifeform, - "d": water_seasonality, }, - name="mark_cultivated", + name="mark_final", dtype="uint8", ) - l4 = expr_eval( - "where((a==124)&(b==10)&(c==2)&(d==1), 79, a)", + res = expr_eval( + "where((a==252)&(b==10), 80, a)", { - "a": l4, + "a": res, "b": veg_cover, - "c": lifeform, - "d": water_seasonality, }, - name="mark_cultivated", + name="mark_final", dtype="uint8", ) - l4 = expr_eval( - "where((a==124)&(b==10)&(c==2)&(d==2), 80, a)", + res = expr_eval( + "where((a==251)&(b==10), 81, a)", { - "a": l4, + "a": res, "b": veg_cover, - "c": lifeform, - "d": water_seasonality, }, - name="mark_cultivated", + name="mark_final", dtype="uint8", ) - - l4 = expr_eval( - "where((a==124)&(b==12)&(c==2)&(d==1), 82, a)", + ######################################### + res = expr_eval( + "where((a==254)&(b==12), 67, a)", { - "a": l4, + "a": res, "b": veg_cover, - "c": lifeform, - "d": water_seasonality, }, - name="mark_cultivated", + name="mark_final", dtype="uint8", ) - l4 = expr_eval( - "where((a==124)&(b==12)&(c==2)&(d==2), 83, a)", + res = expr_eval( + "where((a==253)&(b==12), 68, a)", { - "a": l4, + "a": res, "b": veg_cover, - "c": lifeform, - "d": water_seasonality, }, - name="mark_cultivated", + name="mark_final", dtype="uint8", ) - l4 = expr_eval( - "where((a==124)&(b==13)&(c==2)&(d==1), 85, a)", + res = expr_eval( + "where((a==252)&(b==12), 82, a)", { - "a": l4, + "a": res, "b": veg_cover, - "c": lifeform, - "d": water_seasonality, }, - name="mark_cultivated", + name="mark_final", dtype="uint8", ) - l4 = expr_eval( - "where((a==124)&(b==13)&(c==2)&(d==2), 86, a)", + res = expr_eval( + "where((a==251)&(b==12), 83, a)", { - "a": l4, + "a": res, "b": veg_cover, - "c": lifeform, - "d": water_seasonality, }, - name="mark_cultivated", + name="mark_final", dtype="uint8", ) - - l4 = expr_eval( - "where((a==124)&(b==15)&(c==2)&(d==1), 88, a)", + ########################################## + res = expr_eval( + "where((a==254)&(b==13), 70, a)", { - "a": l4, + "a": res, "b": veg_cover, - "c": lifeform, - "d": water_seasonality, }, - name="mark_cultivated", + name="mark_final", dtype="uint8", ) - l4 = expr_eval( - "where((a==124)&(b==15)&(c==2)&(d==2), 89, a)", + res = expr_eval( + "where((a==253)&(b==13), 71, a)", { - "a": l4, + "a": res, "b": veg_cover, - "c": lifeform, - "d": water_seasonality, }, - name="mark_cultivated", + name="mark_final", dtype="uint8", ) - l4 = expr_eval( - "where((a==124)&(b==16)&(c==2)&(d==1), 91, a)", + res = expr_eval( + "where((a==252)&(b==13), 85, a)", { - "a": l4, + "a": res, "b": veg_cover, - "c": lifeform, - "d": water_seasonality, }, - name="mark_cultivated", + name="mark_final", dtype="uint8", ) - l4 = expr_eval( - "where((a==124)&(b==16)&(c==2)&(d==2), 92, a)", + res = expr_eval( + "where((a==251)&(b==13), 86, a)", { - "a": l4, + "a": res, "b": veg_cover, - "c": lifeform, - "d": water_seasonality, }, - name="mark_cultivated", + name="mark_final", dtype="uint8", ) + ######################################### - l4 = expr_eval( - "where((a==124)&(b==15)&(c==1), 72, a)", + res = expr_eval( + "where((a==254)&(b==15), 73, a)", { - "a": l4, + "a": res, "b": veg_cover, - "c": lifeform, }, - name="mark_cultivated", + name="mark_final", dtype="uint8", ) - l4 = expr_eval( - "where((a==124)&(b==16)&(c==1), 75, a)", + res = expr_eval( + "where((a==253)&(b==15), 74, a)", { - "a": l4, + "a": res, "b": veg_cover, - "c": lifeform, }, - name="mark_cultivated", + name="mark_final", dtype="uint8", ) - l4 = expr_eval( - "where((a==124)&(b==10)&(c==2), 78, a)", + + res = expr_eval( + "where((a==252)&(b==15), 88, a)", { - "a": l4, + "a": res, "b": veg_cover, - "c": lifeform, }, - name="mark_cultivated", + name="mark_final", dtype="uint8", ) - l4 = expr_eval( - "where((a==124)&(b==12)&(c==2), 81, a)", + + res = expr_eval( + "where((a==251)&(b==15), 89, a)", { - "a": l4, + "a": res, "b": veg_cover, - "c": lifeform, }, - name="mark_cultivated", + name="mark_final", dtype="uint8", ) - l4 = expr_eval( - "where((a==124)&(b==13)&(c==2), 84, a)", + ########################################## + res = expr_eval( + "where((a==254)&(b==16), 76, a)", { - "a": l4, + "a": res, "b": veg_cover, - "c": lifeform, }, - name="mark_cultivated", + name="mark_final", dtype="uint8", ) - l4 = expr_eval( - "where((a==124)&(b==15)&(c==2), 87, a)", + res = expr_eval( + "where((a==253)&(b==16), 77, a)", { - "a": l4, + "a": res, "b": veg_cover, - "c": lifeform, }, - name="mark_cultivated", + name="mark_final", dtype="uint8", ) - l4 = expr_eval( - "where((a==124)&(b==16)&(c==2), 90, a)", + res = expr_eval( + "where((a==252)&(b==16), 91, a)", { - "a": l4, + "a": res, "b": veg_cover, - "c": lifeform, }, - name="mark_cultivated", + name="mark_final", dtype="uint8", ) - l4 = expr_eval( - "where((a==124)&(b==10)&(c==1), 63, a)", - { - "a": l4, - "b": veg_cover, - "c": lifeform, - }, - name="mark_cultivated", - dtype="uint8", - ) - l4 = expr_eval( - "where((a==124)&(b==12)&(c==1), 66, a)", - { - "a": l4, - "b": veg_cover, - "c": lifeform, - }, - name="mark_cultivated", - dtype="uint8", - ) - l4 = expr_eval( - "where((a==124)&(b==13)&(c==1), 69, a)", + res = expr_eval( + "where((a==251)&(b==16), 92, a)", { - "a": l4, + "a": res, "b": veg_cover, - "c": lifeform, }, - name="mark_cultivated", + name="mark_final", dtype="uint8", ) - l4 = expr_eval( - "where((a==124)&(b==1), 56, a)", - {"a": l4, "b": lifeform}, - name="mark_cultivated", - dtype="uint8", - ) - - l4 = expr_eval( - "where((a==124)&(c==2), 57, a)", - {"a": l4, "c": lifeform}, - name="mark_cultivated", - dtype="uint8", - ) - - l4 = expr_eval( - "where((a==124)&(b==10), 58, a)", - {"a": l4, "b": veg_cover}, - name="mark_cultivated", - dtype="uint8", - ) - l4 = expr_eval( - "where((a==124)&(b==12), 59, a)", - {"a": l4, "b": veg_cover}, - name="mark_cultivated", - dtype="uint8", - ) - - l4 = expr_eval( - "where((a==124)&(b==13), 60, a)", - {"a": l4, "b": veg_cover}, - name="mark_cultivated", - dtype="uint8", - ) - - l4 = expr_eval( - "where((a==124)&(b==15), 61, a)", - {"a": l4, "b": veg_cover}, - name="mark_cultivated", - dtype="uint8", - ) - l4 = expr_eval( - "where((a==124)&(b==16), 62, a)", - {"a": l4, "b": veg_cover}, - name="mark_cultivated", - dtype="uint8", - ) - l4 = expr_eval( - "where((a==124), 55, a)", {"a": l4}, name="mark_mangroves", dtype="uint8" - ) - - return l4 + return res diff --git a/odc/stats/plugins/l34_utils/l4_veg_cover.py b/odc/stats/plugins/l34_utils/l4_veg_cover.py index b2837491..7f194444 100644 --- a/odc/stats/plugins/l34_utils/l4_veg_cover.py +++ b/odc/stats/plugins/l34_utils/l4_veg_cover.py @@ -9,10 +9,10 @@ def canopyco_veg_con(xx: xr.Dataset, veg_threshold): # Mask NODATA pv_pc_50 = expr_eval( - "where(a!=nodata, a, nodata)", + "where(a==a, a, nodata)", {"a": xx.pv_pc_50.data}, name="mark_nodata", - dtype="uint8", + dtype="float32", **{"nodata": NODATA}, ) diff --git a/odc/stats/plugins/l34_utils/lc_level3.py b/odc/stats/plugins/l34_utils/lc_level3.py index 796d2fde..146a64fc 100644 --- a/odc/stats/plugins/l34_utils/lc_level3.py +++ b/odc/stats/plugins/l34_utils/lc_level3.py @@ -14,10 +14,10 @@ def lc_level3(xx: xr.Dataset): res = expr_eval( "where((a!=a)|(a>=nodata), b, a)", - {"a": xx.cultivated_class.data, "b": xx.classes_l3_l4.data}, + {"a": xx.cultivated.data, "b": xx.classes_l3_l4.data}, name="mask_cultivated", dtype="float32", - **{"nodata": xx.cultivated_class.attrs.get("nodata")}, + **{"nodata": xx.cultivated.attrs.get("nodata")}, ) # Mask urban results with bare sfc (210) diff --git a/odc/stats/plugins/l34_utils/lc_lifeform.py b/odc/stats/plugins/l34_utils/lc_lifeform.py index 152ad968..53378be1 100644 --- a/odc/stats/plugins/l34_utils/lc_lifeform.py +++ b/odc/stats/plugins/l34_utils/lc_lifeform.py @@ -1,14 +1,25 @@ from odc.stats._algebra import expr_eval import xarray as xr +NODATA = 255 + def lifeform(xx: xr.Dataset): # 113 ----> 1 woody # 114 ----> 2 herbaceous + + lifeform_mask = expr_eval( + "where((a!=a)|(a>=nodata), nodata, a)", + {"a": xx.woody.data}, + name="mark_lifeform", + dtype="float32", + **{"nodata": NODATA}, + ) + lifeform_mask = expr_eval( "where(a==113, 1, a)", - {"a": xx.woody_cover.data}, + {"a": lifeform_mask}, name="mark_lifeform", dtype="uint8", ) diff --git a/odc/stats/plugins/l34_utils/lc_water_seasonality.py b/odc/stats/plugins/l34_utils/lc_water_seasonality.py index b727831d..d5fca52d 100644 --- a/odc/stats/plugins/l34_utils/lc_water_seasonality.py +++ b/odc/stats/plugins/l34_utils/lc_water_seasonality.py @@ -12,11 +12,11 @@ def water_seasonality(xx: xr.Dataset, water_seasonality_threshold): # Apply nodata water_frequency = expr_eval( - "where((a==watersea_nodata), nodata, a)", + "where((a!=a)|(a==watersea_nodata), nodata, a)", {"a": xx.water_frequency.data}, name="mark_water_season", - dtype="uint8", - **{"watersea_nodata": WATER_FREQ_NODATA, "nodata": NODATA}, + dtype="float32", + **{"nodata": NODATA, "watersea_nodata": WATER_FREQ_NODATA}, ) water_season_mask = expr_eval( diff --git a/odc/stats/plugins/lc_level34.py b/odc/stats/plugins/lc_level34.py index 72fa6145..5fe958a3 100644 --- a/odc/stats/plugins/lc_level34.py +++ b/odc/stats/plugins/lc_level34.py @@ -94,9 +94,7 @@ def reduce(self, xx: xr.Dataset) -> xr.Dataset: xx, self.bare_threshold, veg_cover ) - l4 = l4_natural_aquatic.natural_auquatic_veg( - l4, lifeform, veg_cover, water_seasonality - ) + l4 = l4_natural_aquatic.natural_auquatic_veg(l4, veg_cover, water_seasonality) level4 = l4_surface.lc_l4_surface(l4, level3, bare_gradation) diff --git a/odc/stats/plugins/lc_veg_class_a1.py b/odc/stats/plugins/lc_veg_class_a1.py index 15186703..00cbdc3f 100644 --- a/odc/stats/plugins/lc_veg_class_a1.py +++ b/odc/stats/plugins/lc_veg_class_a1.py @@ -149,7 +149,7 @@ def l3_class(self, xx: xr.Dataset): {"a": data, "b": l3_mask}, name="intertidal_veg", dtype="uint8", - **{"m": self.output_classes["aquatic_veg"]}, + **{"m": self.output_classes["aquatic_veg_herb"]}, ) elif b == "canopy_cover_class": # aquatic_veg: (mangroves > 0) & (mangroves != nodata) @@ -161,7 +161,7 @@ def l3_class(self, xx: xr.Dataset): dtype="uint8", **{ "nodata": xx[b].attrs["nodata"], - "m": self.output_classes["aquatic_veg"], + "m": self.output_classes["aquatic_veg_wood"], }, ) diff --git a/tests/test_landcover_plugin_a1.py b/tests/test_landcover_plugin_a1.py index 233da316..04b14963 100644 --- a/tests/test_landcover_plugin_a1.py +++ b/tests/test_landcover_plugin_a1.py @@ -128,7 +128,8 @@ def dataset(): def test_l3_classes(dataset): stats_l3 = StatsVegClassL1( output_classes={ - "aquatic_veg": 124, + "aquatic_veg_wood": 124, + "aquatic_veg_herb": 125, "terrestrial_veg": 110, "water": 221, "intertidal": 223, @@ -141,7 +142,7 @@ def test_l3_classes(dataset): expected_res = np.array( [ [ - [223, 221, 210, 124], + [223, 221, 210, 125], [223, 223, 223, 210], [223, 221, 223, 223], [221, 223, 223, 223], @@ -157,7 +158,8 @@ def test_l3_classes(dataset): def test_l4_water_seasonality(dataset): stats_l3 = StatsVegClassL1( output_classes={ - "aquatic_veg": 124, + "aquatic_veg_wood": 124, + "aquatic_veg_herb": 125, "terrestrial_veg": 110, "water": 221, "intertidal": 223, @@ -203,7 +205,8 @@ def test_l4_water_seasonality(dataset): def test_reduce(dataset): stats_l3 = StatsVegClassL1( output_classes={ - "aquatic_veg": 124, + "aquatic_veg_wood": 124, + "aquatic_veg_herb": 125, "terrestrial_veg": 110, "water": 221, "intertidal": 223, diff --git a/tests/test_lc_l34.py b/tests/test_lc_l34.py index 96f1f278..4fba2fb5 100644 --- a/tests/test_lc_l34.py +++ b/tests/test_lc_l34.py @@ -116,12 +116,12 @@ def image_groups(): dims=("spec", "y", "x"), attrs={"nodata": 255}, ), - "cultivated_class": xr.DataArray( + "cultivated": xr.DataArray( da.from_array(cultivated, chunks=(1, -1, -1)), dims=("spec", "y", "x"), attrs={"nodata": 255}, ), - "woody_cover": xr.DataArray( + "woody": xr.DataArray( da.from_array(woody, chunks=(1, -1, -1)), dims=("spec", "y", "x"), attrs={"nodata": 255}, diff --git a/tests/test_lc_l4_ctv.py b/tests/test_lc_l4_ctv.py index b005b71a..6e160220 100644 --- a/tests/test_lc_l4_ctv.py +++ b/tests/test_lc_l4_ctv.py @@ -37,12 +37,12 @@ def image_groups(l34, urban, cultivated, woody, pv_pc_50): dims=("spec", "y", "x"), attrs={"nodata": 255}, ), - "cultivated_class": xr.DataArray( + "cultivated": xr.DataArray( da.from_array(cultivated, chunks=(1, -1, -1)), dims=("spec", "y", "x"), attrs={"nodata": 255}, ), - "woody_cover": xr.DataArray( + "woody": xr.DataArray( da.from_array(woody, chunks=(1, -1, -1)), dims=("spec", "y", "x"), attrs={"nodata": 255}, @@ -300,6 +300,7 @@ def test_ctv_classes_woody_herbaceous(): l4_ctv = l4_cultivated.lc_l4_cultivated( xx.classes_l3_l4, level3, lifeform, 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 e8b3d528..97322542 100644 --- a/tests/test_lc_l4_natural_surface.py +++ b/tests/test_lc_l4_natural_surface.py @@ -46,12 +46,12 @@ def image_groups(l34, urban, woody, bs_pc_50, pv_pc_50, cultivated, water_freque dims=("spec", "y", "x"), attrs={"nodata": 255}, ), - "cultivated_class": xr.DataArray( + "cultivated": xr.DataArray( da.from_array(cultivated, chunks=(1, -1, -1)), dims=("spec", "y", "x"), attrs={"nodata": 255}, ), - "woody_cover": xr.DataArray( + "woody": xr.DataArray( da.from_array(woody, chunks=(1, -1, -1)), dims=("spec", "y", "x"), attrs={"nodata": 255}, @@ -188,7 +188,7 @@ def test_ns(): xx, stats_l4.water_seasonality_threshold ) l4_ctv_ntv_nav = l4_natural_aquatic.natural_auquatic_veg( - l4_ctv_ntv, lifeform, veg_cover, water_seasonality + l4_ctv_ntv, veg_cover, water_seasonality ) # Bare gradation diff --git a/tests/test_lc_l4_nav.py b/tests/test_lc_l4_nav.py index 2d883ae2..26af5ccb 100644 --- a/tests/test_lc_l4_nav.py +++ b/tests/test_lc_l4_nav.py @@ -44,12 +44,12 @@ def image_groups(l34, urban, cultivated, woody, pv_pc_50, water_frequency): dims=("spec", "y", "x"), attrs={"nodata": 255}, ), - "cultivated_class": xr.DataArray( + "cultivated": xr.DataArray( da.from_array(cultivated, chunks=(1, -1, -1)), dims=("spec", "y", "x"), attrs={"nodata": 255}, ), - "woody_cover": xr.DataArray( + "woody": xr.DataArray( da.from_array(woody, chunks=(1, -1, -1)), dims=("spec", "y", "x"), attrs={"nodata": 255}, @@ -71,15 +71,15 @@ def image_groups(l34, urban, cultivated, woody, pv_pc_50, water_frequency): def test_ntv_classes_woody_herbaceous(): - expected_l4_ntv_classes = [[56, 56, 56], [56, 56, 55], [57, 57, 57], [57, 57, 55]] + 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], - [124, 124, 124], - [124, 124, 124], + [125, 125, 125], ] ], dtype="uint8", @@ -134,275 +134,6 @@ def test_ntv_classes_woody_herbaceous(): ) water_frequency = np.array( - [ - [ - [1, 3, 2], - [4, 5, 6], - [9, 2, 11], - [10, 11, 12], - ] - ], - dtype="uint8", - ) - - xx = image_groups(l34, urban, cultivated, woody, pv_pc_50, water_frequency) - - stats_l4 = StatsLccsLevel4() - level3 = lc_level3.lc_level3(xx) - lifeform = lc_lifeform.lifeform(xx) - veg_cover = l4_veg_cover.canopyco_veg_con(xx, stats_l4.veg_threshold) - - # Apply cultivated to match the code in Level4 processing - l4_ctv = l4_cultivated.lc_l4_cultivated( - xx.classes_l3_l4, level3, lifeform, veg_cover - ) - l4_ctv_ntv = l4_natural_veg.lc_l4_natural_veg(l4_ctv, level3, lifeform, veg_cover) - - water_seasonality = lc_water_seasonality.water_seasonality( - xx, stats_l4.water_seasonality_threshold - ) - l4_ctv_ntv_nav = l4_natural_aquatic.natural_auquatic_veg( - l4_ctv_ntv, lifeform, veg_cover, water_seasonality - ) - - assert (l4_ctv_ntv_nav.compute() == expected_l4_ntv_classes).all() - - -def test_ntv_veg_cover(): - expected_l4_ntv_classes = [ - [62, 59, 58], - [58, 59, 59], - [62, 60, 60], - [61, 62, 59], - ] - - 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", - ) - # 112 --> natural veg - cultivated = np.array( - [ - [ - [255, 255, 255], - [255, 255, 255], - [255, 255, 255], - [255, 255, 255], - ] - ], - 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", - ) - water_frequency = 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, water_frequency) - - stats_l4 = StatsLccsLevel4() - level3 = lc_level3.lc_level3(xx) - lifeform = lc_lifeform.lifeform(xx) - veg_cover = l4_veg_cover.canopyco_veg_con(xx, stats_l4.veg_threshold) - - # Apply cultivated to match the code in Level4 processing - l4_ctv = l4_cultivated.lc_l4_cultivated( - xx.classes_l3_l4, level3, lifeform, veg_cover - ) - l4_ctv_ntv = l4_natural_veg.lc_l4_natural_veg(l4_ctv, level3, lifeform, veg_cover) - - water_seasonality = lc_water_seasonality.water_seasonality( - xx, stats_l4.water_seasonality_threshold - ) - l4_ctv_ntv_nav = l4_natural_aquatic.natural_auquatic_veg( - l4_ctv_ntv, lifeform, veg_cover, water_seasonality - ) - assert (l4_ctv_ntv_nav.compute() == expected_l4_ntv_classes).all() - - -def test_ntv_woody_veg_cover(): - expected_l4_ntv_classes = [ - [75, 66, 63], - [63, 66, 66], - [75, 69, 69], - [72, 75, 66], - ] - - 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", - ) - # 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, 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", - ) - water_frequency = 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, water_frequency) - - stats_l4 = StatsLccsLevel4() - level3 = lc_level3.lc_level3(xx) - lifeform = lc_lifeform.lifeform(xx) - veg_cover = l4_veg_cover.canopyco_veg_con(xx, stats_l4.veg_threshold) - - # Apply cultivated to match the code in Level4 processing - l4_ctv = l4_cultivated.lc_l4_cultivated( - xx.classes_l3_l4, level3, lifeform, veg_cover - ) - l4_ctv_ntv = l4_natural_veg.lc_l4_natural_veg(l4_ctv, level3, lifeform, veg_cover) - - water_seasonality = lc_water_seasonality.water_seasonality( - xx, stats_l4.water_seasonality_threshold - ) - l4_ctv_ntv_nav = l4_natural_aquatic.natural_auquatic_veg( - l4_ctv_ntv, lifeform, veg_cover, water_seasonality - ) - assert (l4_ctv_ntv_nav.compute() == expected_l4_ntv_classes).all() - - -def test_ntv_woody_seasonal_water_veg_cover(): - expected_l4_ntv_classes = [ - [77, 68, 65], - [65, 68, 68], - [77, 71, 71], - [74, 77, 68], - ] - - 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", - ) - # 112 --> natural veg - cultivated = np.array( [ [ [255, 255, 255], @@ -414,41 +145,6 @@ def test_ntv_woody_seasonal_water_veg_cover(): 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", - ) - water_frequency = np.array( - [ - [ - [1, 2, 3], - [1, 2, 3], - [1, 2, 3], - [1, 2, 3], - ] - ], - dtype="uint8", - ) - xx = image_groups(l34, urban, cultivated, woody, pv_pc_50, water_frequency) stats_l4 = StatsLccsLevel4() @@ -466,27 +162,27 @@ def test_ntv_woody_seasonal_water_veg_cover(): xx, stats_l4.water_seasonality_threshold ) l4_ctv_ntv_nav = l4_natural_aquatic.natural_auquatic_veg( - l4_ctv_ntv, lifeform, veg_cover, water_seasonality + l4_ctv_ntv, veg_cover, water_seasonality ) assert (l4_ctv_ntv_nav.compute() == expected_l4_ntv_classes).all() -def test_ntv_woody_permanent_water_veg_cover(): +def test_ntv_herbaceous_seasonal_water_veg_cover(): expected_l4_ntv_classes = [ - [76, 67, 64], - [64, 67, 67], - [76, 70, 70], - [73, 76, 67], + [92, 83, 81], + [80, 82, 82], + [92, 86, 86], + [88, 91, 82], ] l34 = np.array( [ [ - [124, 124, 124], - [124, 124, 124], - [124, 124, 124], - [124, 124, 124], + [125, 125, 125], + [125, 125, 125], + [125, 125, 125], + [125, 125, 125], ] ], dtype="uint8", @@ -503,103 +199,7 @@ def test_ntv_woody_permanent_water_veg_cover(): ], 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, 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", - ) - water_frequency = np.array( - [ - [ - [4, 5, 6], - [7, 8, 9], - [10, 11, 12], - [11, 10, 8], - ] - ], - dtype="uint8", - ) - - xx = image_groups(l34, urban, cultivated, woody, pv_pc_50, water_frequency) - - stats_l4 = StatsLccsLevel4() - level3 = lc_level3.lc_level3(xx) - lifeform = lc_lifeform.lifeform(xx) - veg_cover = l4_veg_cover.canopyco_veg_con(xx, stats_l4.veg_threshold) - - # Apply cultivated to match the code in Level4 processing - l4_ctv = l4_cultivated.lc_l4_cultivated( - xx.classes_l3_l4, level3, lifeform, veg_cover - ) - l4_ctv_ntv = l4_natural_veg.lc_l4_natural_veg(l4_ctv, level3, lifeform, veg_cover) - - water_seasonality = lc_water_seasonality.water_seasonality( - xx, stats_l4.water_seasonality_threshold - ) - l4_ctv_ntv_nav = l4_natural_aquatic.natural_auquatic_veg( - l4_ctv_ntv, lifeform, veg_cover, water_seasonality - ) - assert (l4_ctv_ntv_nav.compute() == expected_l4_ntv_classes).all() - - -def test_ntv_herbaceous_veg_cover(): - expected_l4_ntv_classes = [[90, 81, 78], [78, 81, 81], [90, 84, 84], [87, 90, 81]] - - 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", - ) - # 112 --> natural veg cultivated = np.array( [ [ @@ -638,10 +238,10 @@ def test_ntv_herbaceous_veg_cover(): water_frequency = np.array( [ [ - [255, 255, 255], - [255, 255, 255], - [255, 255, 255], - [255, 255, 255], + [1, 2, 3], + [6, 8, 10], + [1, 2, 3], + [11, 12, 12], ] ], dtype="uint8", @@ -664,17 +264,18 @@ def test_ntv_herbaceous_veg_cover(): xx, stats_l4.water_seasonality_threshold ) l4_ctv_ntv_nav = l4_natural_aquatic.natural_auquatic_veg( - l4_ctv_ntv, lifeform, veg_cover, water_seasonality + l4_ctv_ntv, veg_cover, water_seasonality ) + assert (l4_ctv_ntv_nav.compute() == expected_l4_ntv_classes).all() -def test_ntv_herbaceous_seasonal_water_veg_cover(): +def test_ntv_woody_seasonal_water_veg_cover(): expected_l4_ntv_classes = [ - [92, 83, 80], - [80, 83, 83], - [92, 86, 86], - [89, 92, 83], + [77, 68, 65], + [65, 68, 68], + [77, 71, 71], + [74, 77, 68], ] l34 = np.array( @@ -765,19 +366,14 @@ def test_ntv_herbaceous_seasonal_water_veg_cover(): xx, stats_l4.water_seasonality_threshold ) l4_ctv_ntv_nav = l4_natural_aquatic.natural_auquatic_veg( - l4_ctv_ntv, lifeform, veg_cover, water_seasonality + l4_ctv_ntv, veg_cover, water_seasonality ) assert (l4_ctv_ntv_nav.compute() == expected_l4_ntv_classes).all() def test_ntv_herbaceous_permanent_water_veg_cover(): - expected_l4_ntv_classes = [ - [91, 82, 79], - [79, 82, 82], - [91, 85, 85], - [88, 91, 82], - ] + expected_l4_ntv_classes = [[76, 67, 64], [64, 67, 67], [76, 70, 70], [73, 76, 67]] l34 = np.array( [ @@ -867,6 +463,6 @@ def test_ntv_herbaceous_permanent_water_veg_cover(): xx, stats_l4.water_seasonality_threshold ) l4_ctv_ntv_nav = l4_natural_aquatic.natural_auquatic_veg( - l4_ctv_ntv, lifeform, veg_cover, water_seasonality + l4_ctv_ntv, veg_cover, water_seasonality ) 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 index 2154e73b..e4761908 100644 --- a/tests/test_lc_l4_ntv.py +++ b/tests/test_lc_l4_ntv.py @@ -41,12 +41,12 @@ def image_groups(l34, urban, cultivated, woody, pv_pc_50): dims=("spec", "y", "x"), attrs={"nodata": 255}, ), - "cultivated_class": xr.DataArray( + "cultivated": xr.DataArray( da.from_array(cultivated, chunks=(1, -1, -1)), dims=("spec", "y", "x"), attrs={"nodata": 255}, ), - "woody_cover": xr.DataArray( + "woody": xr.DataArray( da.from_array(woody, chunks=(1, -1, -1)), dims=("spec", "y", "x"), attrs={"nodata": 255}, diff --git a/tests/test_lc_level3.py b/tests/test_lc_level3.py index c46f8935..1906c758 100644 --- a/tests/test_lc_level3.py +++ b/tests/test_lc_level3.py @@ -74,7 +74,7 @@ def image_groups(): dims=("spec", "y", "x"), attrs={"nodata": 255}, ), - "cultivated_class": xr.DataArray( + "cultivated": xr.DataArray( da.from_array(cultivated, chunks=(1, -1, -1)), dims=("spec", "y", "x"), attrs={"nodata": 255}, diff --git a/tests/test_urban_model.py b/tests/test_urban_model.py index c81f28cb..dfd2b23d 100644 --- a/tests/test_urban_model.py +++ b/tests/test_urban_model.py @@ -25,7 +25,7 @@ def dask_client(): @pytest.fixture(scope="module") def tflite_model_path(): s3_bucket = "dea-public-data-dev" - s3_key = "lccs_models/urban_models/tflite/urban_model_tf_2_16_2.tflite" + s3_key = "lccs_models/urban_models/0-0-5/tflite/urban_model_tf_2_16_2.tflite" local_path = "/tmp/model.tflite" # Download the model from S3