Skip to content

Commit

Permalink
deconvolute landcover l34 tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Emma Ai committed Nov 25, 2024
1 parent 44a33f0 commit fbe2fce
Show file tree
Hide file tree
Showing 8 changed files with 69 additions and 69 deletions.
1 change: 1 addition & 0 deletions odc/stats/plugins/l34_utils/lc_level3.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)",
Expand Down
10 changes: 10 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
2 changes: 1 addition & 1 deletion tests/test_lc_l34.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]]
Expand Down
36 changes: 18 additions & 18 deletions tests/test_lc_l4_ctv.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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},
Expand All @@ -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],
Expand Down Expand Up @@ -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],
Expand Down Expand Up @@ -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],
Expand Down Expand Up @@ -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],
Expand Down Expand Up @@ -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()
8 changes: 4 additions & 4 deletions tests/test_lc_l4_natural_surface.py
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down Expand Up @@ -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],
Expand Down Expand Up @@ -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
Expand Down
27 changes: 13 additions & 14 deletions tests/test_lc_l4_nav.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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},
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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)
Expand All @@ -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],
Expand Down Expand Up @@ -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)
Expand All @@ -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],
Expand Down Expand Up @@ -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)
Expand Down
35 changes: 17 additions & 18 deletions tests/test_lc_l4_ntv.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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},
Expand All @@ -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],
Expand Down Expand Up @@ -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],
Expand Down Expand Up @@ -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],
Expand Down Expand Up @@ -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],
Expand Down Expand Up @@ -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()
Loading

0 comments on commit fbe2fce

Please sign in to comment.