Skip to content

Commit

Permalink
Ensured nodata is replaced with 255 in all level4 plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
tebadi committed Nov 19, 2024
1 parent 8227e5b commit fd6f799
Show file tree
Hide file tree
Showing 15 changed files with 48 additions and 46 deletions.
12 changes: 10 additions & 2 deletions odc/stats/plugins/l34_utils/l4_bare_gradation.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,23 @@

def bare_gradation(xx: xr.Dataset, bare_threshold, veg_cover):

bs_pc_50 = expr_eval(
"where((a!=a), nodata, a)",
{"a": xx.bs_pc_50.data},
name="mark_veg",
dtype="float32",
**{"nodata": NODATA},
)

# Map any data > 100 ---> 100
bs_pc_50 = expr_eval(
"where((a>100)&(a!=nodata), 100, a)",
{"a": xx.bs_pc_50.data},
{"a": bs_pc_50},
name="mark_veg",
dtype="uint8",
**{"nodata": NODATA},
)

# 60% <= data --> 15
bs_mask = expr_eval(
"where((a>=m)&(a!=nodata), 15, a)",
Expand Down
4 changes: 2 additions & 2 deletions odc/stats/plugins/l34_utils/l4_veg_cover.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def canopyco_veg_con(xx: xr.Dataset, veg_threshold):
dtype="float32",
**{"nodata": NODATA},
)

# Map any data > 100 ---> 100
pv_pc_50 = expr_eval(
"where((a>100) & (a!=nodata), 100, a)",
Expand All @@ -27,7 +27,7 @@ def canopyco_veg_con(xx: xr.Dataset, veg_threshold):
**{"nodata": NODATA},
)

# ## data < 1 ---> 0
# data < 1 ---> 0
veg_mask = expr_eval(
"where(a<m, 0, a)",
{
Expand Down
2 changes: 1 addition & 1 deletion odc/stats/plugins/l34_utils/l4_water.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ def water_classification(xx, intertidal_mask, water_persistence):

l4 = expr_eval(
"where(((a==223)|(a==221))&(b==1), 101, a)",
{"a": xx.classes_l3_l4.data, "b": water_persistence},
{"a": xx.level_3_4.data, "b": water_persistence},
name="mark_water",
dtype="uint8",
)
Expand Down
14 changes: 5 additions & 9 deletions odc/stats/plugins/l34_utils/l4_water_persistence.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,14 @@


def water_persistence(xx: xr.Dataset, watper_threshold):

# Apply nodata
# Address nan
water_frequency = expr_eval(
"where((a==water_freq_nodata), nodata, a)",
"where((a!=a), nodata, a)",
{"a": xx.water_frequency.data},
name="mark_water",
dtype="uint8",
**{
"m": watper_threshold[3],
"nodata": NODATA,
"water_freq_nodata": WATER_FREQ_NODATA,
},
dtype="float32",
**{"nodata": NODATA},
)

# 10 <= water_frequency < 1 --> 1
Expand Down
2 changes: 1 addition & 1 deletion odc/stats/plugins/l34_utils/lc_intertidal_mask.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def intertidal_mask(xx: xr.Dataset):

res = expr_eval(
"where(a==_u, 1, 0)",
{"a": xx.classes_l3_l4.data},
{"a": xx.level_3_4.data},
name="mask_intertidal",
dtype="uint8",
**{"_u": 223},
Expand Down
4 changes: 2 additions & 2 deletions odc/stats/plugins/l34_utils/lc_level3.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def lc_level3(xx: xr.Dataset):

res = expr_eval(
"where((a!=a)|(a>=nodata), b, a)",
{"a": xx.cultivated.data, "b": xx.classes_l3_l4.data},
{"a": xx.cultivated.data, "b": xx.level_3_4.data},
name="mask_cultivated",
dtype="float32",
**{"nodata": xx.cultivated.attrs.get("nodata")},
Expand Down Expand Up @@ -46,7 +46,7 @@ def lc_level3(xx: xr.Dataset):
# Add intertidal as water
res = expr_eval(
"where((a==223)|(a==221), 220, b)",
{"a": xx.classes_l3_l4.data, "b": res},
{"a": xx.level_3_4.data, "b": res},
name="mark_urban",
dtype="uint8",
)
Expand Down
7 changes: 3 additions & 4 deletions odc/stats/plugins/l34_utils/lc_water_seasonality.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,20 @@

from odc.stats._algebra import expr_eval

WATER_FREQ_NODATA = -999
NODATA = 255


def water_seasonality(xx: xr.Dataset, water_seasonality_threshold):
# >= 3 months ----> 1 Semi-permanent or permanent
# < 3 months ----> 2 Temporary or seasonal

# Apply nodata
water_frequency = expr_eval(
"where((a!=a)|(a==watersea_nodata), nodata, a)",
"where((a!=a), nodata, a)",
{"a": xx.water_frequency.data},
name="mark_water_season",
dtype="float32",
**{"nodata": NODATA, "watersea_nodata": WATER_FREQ_NODATA},
**{"nodata": NODATA},
)

water_season_mask = expr_eval(
Expand Down
9 changes: 4 additions & 5 deletions odc/stats/plugins/lc_level34.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,16 +64,16 @@ def reduce(self, xx: xr.Dataset) -> xr.Dataset:
water_persistence = l4_water_persistence.water_persistence(
xx, self.watper_threshold
)

# print("### Water Persistence", np.unique(water_persistence.compute()))
water_seasonality = lc_water_seasonality.water_seasonality(
xx, self.water_seasonality_threshold
)

# print("### Water Seasonality", np.unique(water_persistence.compute()))
intertidal_mask = lc_intertidal_mask.intertidal_mask(xx)

# #TODO WATER (99-104)
l4 = l4_water.water_classification(xx, intertidal_mask, water_persistence)

# print("### Water classification", np.unique(water_persistence.compute()))
# Generate Level3 classes
level3 = lc_level3.lc_level3(xx)

Expand All @@ -93,7 +93,6 @@ def reduce(self, xx: xr.Dataset) -> xr.Dataset:
bare_gradation = l4_bare_gradation.bare_gradation(
xx, self.bare_threshold, veg_cover
)

l4 = l4_natural_aquatic.natural_auquatic_veg(l4, veg_cover, water_seasonality)

level4 = l4_surface.lc_l4_surface(l4, level3, bare_gradation)
Expand All @@ -103,7 +102,7 @@ def reduce(self, xx: xr.Dataset) -> xr.Dataset:

attrs = xx.attrs.copy()
attrs["nodata"] = NODATA
dims = xx.classes_l3_l4.dims[1:]
dims = xx.level_3_4.dims[1:]
data_vars = {
k: xr.DataArray(v, dims=dims, attrs=attrs)
for k, v in zip(self.measurements, [level3.squeeze(), level4.squeeze()])
Expand Down
2 changes: 1 addition & 1 deletion tests/test_lc_l34.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ def image_groups():
}

data_vars = {
"classes_l3_l4": xr.DataArray(
"level_3_4": xr.DataArray(
da.from_array(l34, chunks=(1, -1, -1)),
dims=("spec", "y", "x"),
attrs={"nodata": 255},
Expand Down
10 changes: 5 additions & 5 deletions tests/test_lc_l4_ctv.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def image_groups(l34, urban, cultivated, woody, pv_pc_50):
}

data_vars = {
"classes_l3_l4": xr.DataArray(
"level_3_4": xr.DataArray(
da.from_array(l34, chunks=(1, -1, -1)),
dims=("spec", "y", "x"),
attrs={"nodata": 255},
Expand Down Expand Up @@ -135,7 +135,7 @@ def test_ctv_classes_woody():
veg_cover = l4_veg_cover.canopyco_veg_con(xx, stats_l4.veg_threshold)

l4_ctv = l4_cultivated.lc_l4_cultivated(
xx.classes_l3_l4, level3, lifeform, veg_cover
xx.level_3_4, level3, lifeform, veg_cover
)

assert (l4_ctv.compute() == expected_cultivated_classes).all()
Expand Down Expand Up @@ -217,7 +217,7 @@ def test_ctv_classes_herbaceous():
veg_cover = l4_veg_cover.canopyco_veg_con(xx, stats_l4.veg_threshold)

l4_ctv = l4_cultivated.lc_l4_cultivated(
xx.classes_l3_l4, level3, lifeform, veg_cover
xx.level_3_4, level3, lifeform, veg_cover
)
assert (l4_ctv.compute() == expected_cultivated_classes).all()

Expand Down Expand Up @@ -298,7 +298,7 @@ def test_ctv_classes_woody_herbaceous():
veg_cover = l4_veg_cover.canopyco_veg_con(xx, stats_l4.veg_threshold)

l4_ctv = l4_cultivated.lc_l4_cultivated(
xx.classes_l3_l4, level3, lifeform, veg_cover
xx.level_3_4, level3, lifeform, veg_cover
)

assert (l4_ctv.compute() == expected_cultivated_classes).all()
Expand Down Expand Up @@ -380,6 +380,6 @@ def test_ctv_classes_no_vegcover():
veg_cover = l4_veg_cover.canopyco_veg_con(xx, stats_l4.veg_threshold)

l4_ctv = l4_cultivated.lc_l4_cultivated(
xx.classes_l3_l4, level3, lifeform, veg_cover
xx.level_3_4, level3, lifeform, veg_cover
)
assert (l4_ctv.compute() == expected_cultivated_classes).all()
4 changes: 2 additions & 2 deletions tests/test_lc_l4_natural_surface.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def image_groups(l34, urban, woody, bs_pc_50, pv_pc_50, cultivated, water_freque
}

data_vars = {
"classes_l3_l4": xr.DataArray(
"level_3_4": xr.DataArray(
da.from_array(l34, chunks=(1, -1, -1)),
dims=("spec", "y", "x"),
attrs={"nodata": 255},
Expand Down Expand Up @@ -180,7 +180,7 @@ def test_ns():

# Apply cultivated to match the code in Level4 processing
l4_ctv = l4_cultivated.lc_l4_cultivated(
xx.classes_l3_l4, level3, lifeform, veg_cover
xx.level_3_4, level3, lifeform, veg_cover
)
l4_ctv_ntv = l4_natural_veg.lc_l4_natural_veg(l4_ctv, level3, lifeform, veg_cover)

Expand Down
10 changes: 5 additions & 5 deletions tests/test_lc_l4_nav.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def image_groups(l34, urban, cultivated, woody, pv_pc_50, water_frequency):
}

data_vars = {
"classes_l3_l4": xr.DataArray(
"level_3_4": xr.DataArray(
da.from_array(l34, chunks=(1, -1, -1)),
dims=("spec", "y", "x"),
attrs={"nodata": 255},
Expand Down Expand Up @@ -154,7 +154,7 @@ def test_ntv_classes_woody_herbaceous():

# Apply cultivated to match the code in Level4 processing
l4_ctv = l4_cultivated.lc_l4_cultivated(
xx.classes_l3_l4, level3, lifeform, veg_cover
xx.level_3_4, level3, lifeform, veg_cover
)
l4_ctv_ntv = l4_natural_veg.lc_l4_natural_veg(l4_ctv, level3, lifeform, veg_cover)

Expand Down Expand Up @@ -256,7 +256,7 @@ def test_ntv_herbaceous_seasonal_water_veg_cover():

# Apply cultivated to match the code in Level4 processing
l4_ctv = l4_cultivated.lc_l4_cultivated(
xx.classes_l3_l4, level3, lifeform, veg_cover
xx.level_3_4, level3, lifeform, veg_cover
)
l4_ctv_ntv = l4_natural_veg.lc_l4_natural_veg(l4_ctv, level3, lifeform, veg_cover)

Expand Down Expand Up @@ -358,7 +358,7 @@ def test_ntv_woody_seasonal_water_veg_cover():

# Apply cultivated to match the code in Level4 processing
l4_ctv = l4_cultivated.lc_l4_cultivated(
xx.classes_l3_l4, level3, lifeform, veg_cover
xx.level_3_4, level3, lifeform, veg_cover
)
l4_ctv_ntv = l4_natural_veg.lc_l4_natural_veg(l4_ctv, level3, lifeform, veg_cover)

Expand Down Expand Up @@ -455,7 +455,7 @@ def test_ntv_herbaceous_permanent_water_veg_cover():

# Apply cultivated to match the code in Level4 processing
l4_ctv = l4_cultivated.lc_l4_cultivated(
xx.classes_l3_l4, level3, lifeform, veg_cover
xx.level_3_4, level3, lifeform, veg_cover
)
l4_ctv_ntv = l4_natural_veg.lc_l4_natural_veg(l4_ctv, level3, lifeform, veg_cover)

Expand Down
10 changes: 5 additions & 5 deletions tests/test_lc_l4_ntv.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def image_groups(l34, urban, cultivated, woody, pv_pc_50):
}

data_vars = {
"classes_l3_l4": xr.DataArray(
"level_3_4": xr.DataArray(
da.from_array(l34, chunks=(1, -1, -1)),
dims=("spec", "y", "x"),
attrs={"nodata": 255},
Expand Down Expand Up @@ -137,7 +137,7 @@ def test_ntv_classes_herbaceous():
lifeform = lc_lifeform.lifeform(xx)
veg_cover = l4_veg_cover.canopyco_veg_con(xx, stats_l4.veg_threshold)
l4_ntv = l4_natural_veg.lc_l4_natural_veg(
xx.classes_l3_l4, level3, lifeform, veg_cover
xx.level_3_4, level3, lifeform, veg_cover
)
assert (l4_ntv.compute() == expected_natural_terrestrial_veg_classes).all()

Expand Down Expand Up @@ -218,7 +218,7 @@ def test_ntv_classes_woody():
veg_cover = l4_veg_cover.canopyco_veg_con(xx, stats_l4.veg_threshold)

l4_ntv = l4_natural_veg.lc_l4_natural_veg(
xx.classes_l3_l4, level3, lifeform, veg_cover
xx.level_3_4, level3, lifeform, veg_cover
)
assert (l4_ntv.compute() == expected_natural_terrestrial_veg_classes).all()

Expand Down Expand Up @@ -298,7 +298,7 @@ def test_ntv_classes_no_veg():
lifeform = lc_lifeform.lifeform(xx)
veg_cover = l4_veg_cover.canopyco_veg_con(xx, stats_l4.veg_threshold)
l4_ntv = l4_natural_veg.lc_l4_natural_veg(
xx.classes_l3_l4, level3, lifeform, veg_cover
xx.level_3_4, level3, lifeform, veg_cover
)
assert (l4_ntv.compute() == expected_natural_terrestrial_veg_classes).all()

Expand Down Expand Up @@ -378,6 +378,6 @@ def test_ntv_classes_no_lifeform():
lifeform = lc_lifeform.lifeform(xx)
veg_cover = l4_veg_cover.canopyco_veg_con(xx, stats_l4.veg_threshold)
l4_ntv = l4_natural_veg.lc_l4_natural_veg(
xx.classes_l3_l4, level3, lifeform, veg_cover
xx.level_3_4, level3, lifeform, veg_cover
)
assert (l4_ntv.compute() == expected_natural_terrestrial_veg_classes).all()
2 changes: 1 addition & 1 deletion tests/test_lc_l4_water.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def image_groups(l34, urban, cultivated, woody, bs_pc_50, pv_pc_50, water_freque
}

data_vars = {
"classes_l3_l4": xr.DataArray(
"level_3_4": xr.DataArray(
da.from_array(l34, chunks=(1, -1, -1)),
dims=("spec", "y", "x"),
attrs={"nodata": 255},
Expand Down
2 changes: 1 addition & 1 deletion tests/test_lc_level3.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def image_groups():
}

data_vars = {
"classes_l3_l4": xr.DataArray(
"level_3_4": xr.DataArray(
da.from_array(l34, chunks=(1, -1, -1)),
dims=("spec", "y", "x"),
attrs={"nodata": 255},
Expand Down

0 comments on commit fd6f799

Please sign in to comment.