Skip to content

Commit cd3b38e

Browse files
committed
Applied formatting and code improvements
1 parent 9a6c987 commit cd3b38e

11 files changed

+65
-96
lines changed

odc/stats/plugins/l34_utils/l4_bare_gradation.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import xarray as xr
22
from odc.stats._algebra import expr_eval
3+
from . import utils
4+
35

46
NODATA = 255
57

@@ -51,5 +53,8 @@ def bare_gradation(xx: xr.Dataset, bare_threshold, veg_cover):
5153
dtype="uint8",
5254
**{"m": bare_threshold[0]},
5355
)
54-
56+
# Apply bare gradation expected output classes
57+
# Map bare gradation classes
58+
bs_mapping = {100: 10, 120: 12, 150: 15}
59+
bs_mask = utils.apply_mapping(bs_mask, bs_mapping)
5560
return bs_mask

odc/stats/plugins/l34_utils/l4_natural_aquatic.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
"""
2-
Define Natural Aquatic Classes in Level-4
1+
"""
2+
Define Natural Aquatic Classes in Level-4
33
"""
44

55
from odc.stats._algebra import expr_eval

odc/stats/plugins/l34_utils/l4_natural_veg.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
NODATA = 255
2-
31
from odc.stats._algebra import expr_eval
42

3+
NODATA = 255
4+
55

66
def lc_l4_natural_veg(l4, l3, lifeform, veg_cover):
77

odc/stats/plugins/l34_utils/l4_veg_cover.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# from typing import Tuple, Optional, Dict, List
22
import xarray as xr
33
from odc.stats._algebra import expr_eval
4+
from . import utils
45

56
NODATA = 255
67

@@ -79,4 +80,9 @@ def canopyco_veg_con(xx: xr.Dataset, veg_threshold):
7980
**{"m": veg_threshold[4], "n": veg_threshold[5]},
8081
)
8182

83+
# Define mapping from current output to expected a3 output
84+
# Map vegetation cover classes
85+
veg_mapping = {160: 16, 150: 15, 130: 13, 120: 12, 100: 10}
86+
veg_mask = utils.apply_mapping(veg_mask, veg_mapping)
87+
8288
return veg_mask

odc/stats/plugins/l34_utils/l4_water_persistence.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import xarray as xr
22

33
from odc.stats._algebra import expr_eval
4+
from . import utils
45

56
NODATA = 255
67

@@ -51,6 +52,11 @@ def water_persistence(xx: xr.Dataset, watper_threshold):
5152
**{"m": watper_threshold[0], "n": watper_threshold[1]},
5253
)
5354

55+
# Apply water persistence expcted classes
56+
# Map values to the classes expected in water persistence in land cover Level-4 output
57+
waterper_wat_mapping = {100: 1, 70: 7, 80: 8, 90: 9}
58+
water_mask = utils.apply_mapping(water_mask, waterper_wat_mapping)
59+
5460
# # water_frequency < 1 --> 0
5561
# water_mask = expr_eval(
5662
# "where(a<1, 0, a)",
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import xarray as xr
2+
3+
4+
def apply_mapping(data, class_mapping):
5+
"""
6+
Utility function to apply mapping on dictionaries
7+
"""
8+
for o_val, n_val in class_mapping.items():
9+
data = xr.where(data == o_val, n_val, data)
10+
return data

odc/stats/plugins/lc_level34.py

Lines changed: 28 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
Plugin of Module A3 in LandCover PipeLine
33
"""
44

5-
from typing import Tuple, Optional, Dict, List
5+
from typing import Tuple, Optional, List
66

77
import numpy as np
88
import xarray as xr
@@ -20,11 +20,12 @@
2020
l4_surface,
2121
l4_bare_gradation,
2222
l4_water,
23+
utils,
2324
)
2425

2526

2627
NODATA = 255
27-
water_frequency_nodata = -999
28+
WATER_FREQ_NODATA = -999
2829

2930

3031
class StatsLccsLevel4(StatsPluginInterface):
@@ -38,10 +39,6 @@ def __init__(
3839
veg_threshold: Optional[List] = None,
3940
bare_threshold: Optional[List] = None,
4041
watper_threshold: Optional[List] = None,
41-
veg_mapping: Optional[Dict[int, int]] = None,
42-
bs_mapping: Optional[Dict[int, int]] = None,
43-
waterper_wat_mapping: Optional[Dict[int, int]] = None,
44-
l3_to_l4_mapping: Optional[Dict[int, int]] = None,
4542
water_seasonality_threshold: int = None,
4643
**kwargs,
4744
):
@@ -58,14 +55,6 @@ def __init__(
5855
water_seasonality_threshold if water_seasonality_threshold else 3
5956
)
6057

61-
# The mapping below are from the LC KH page
62-
# Map vegetation cover classes
63-
self.veg_mapping = {160: 16, 150: 15, 130: 13, 120: 12, 100: 10}
64-
# Map bare gradation classes
65-
self.bs_mapping = {100: 10, 120: 12, 150: 15}
66-
# Map values to the classes expected in water persistence in land cover Level-4 output
67-
self.waterper_wat_mapping = {100: 1, 70: 7, 80: 8, 90: 9}
68-
6958
@property
7059
def measurements(self) -> Tuple[str, ...]:
7160
_measurements = ["level3", "level4"]
@@ -77,14 +66,13 @@ def native_transform(self, xx):
7766
def fuser(self, xx):
7867
return xx
7968

80-
@staticmethod
81-
def apply_mapping(data, class_mapping):
82-
for o_val, n_val in class_mapping.items():
83-
data = xr.where(data == o_val, n_val, data)
84-
return data
69+
# @staticmethod
70+
# def apply_mapping(data, class_mapping):
71+
# for o_val, n_val in class_mapping.items():
72+
# data = xr.where(data == o_val, n_val, data)
73+
# return data
8574

8675
def define_life_form(self, xx: xr.Dataset):
87-
lifeform = xx.woody_cover.data
8876

8977
# 113 ----> 1 woody
9078
# 114 ----> 2 herbaceous
@@ -128,11 +116,11 @@ def define_water_seasonality(self, xx: xr.Dataset):
128116
dtype="uint8",
129117
**{
130118
"watseas_trh": self.water_seasonality_threshold,
131-
"watersea_nodata": water_frequency_nodata,
119+
"watersea_nodata": WATER_FREQ_NODATA,
132120
},
133121
)
134122
mapping = {100: 1, 200: 2}
135-
water_season_mask = self.apply_mapping(water_season_mask, mapping)
123+
water_season_mask = utils.apply_mapping(water_season_mask, mapping)
136124

137125
return water_season_mask
138126

@@ -142,53 +130,48 @@ def reduce(self, xx: xr.Dataset) -> xr.Dataset:
142130

143131
# Vegetation cover
144132
veg_cover = l4_veg_cover.canopyco_veg_con(xx, self.veg_threshold)
145-
# Define mapping from current output to expected a3 output
146-
veg_cover = self.apply_mapping(veg_cover, self.veg_mapping)
133+
# # Define mapping from current output to expected a3 output
134+
# veg_cover = utils.apply_mapping(veg_cover, self.veg_mapping)
147135
# Define life form
148136
lifeform = self.define_life_form(xx)
149137

150138
# Apply cultivated Level-4 classes (1-18)
151-
l4_ctv = l4_cultivated.lc_l4_cultivated(
139+
l4 = l4_cultivated.lc_l4_cultivated(
152140
xx.classes_l3_l4, level3, lifeform, veg_cover
153141
)
154-
print("***** CULATIVATED: ", np.unique(l4_ctv.compute()))
142+
155143
# Apply terrestrial vegetation classes [19-36]
156-
l4_ctv_ntv = l4_natural_veg.lc_l4_natural_veg(
157-
l4_ctv, level3, lifeform, veg_cover
158-
)
159-
print("***** CULATIVATED NTV : ", np.unique(l4_ctv_ntv.compute()))
144+
l4 = l4_natural_veg.lc_l4_natural_veg(l4, level3, lifeform, veg_cover)
160145

161146
# Bare gradation
162147
bare_gradation = l4_bare_gradation.bare_gradation(
163148
xx, self.bare_threshold, veg_cover
164149
)
165-
# Apply bare gradation expected output classes
166-
bare_gradation = self.apply_mapping(bare_gradation, self.bs_mapping)
150+
# # Apply bare gradation expected output classes
151+
# bare_gradation = utils.apply_mapping(bare_gradation, self.bs_mapping)
167152

168153
# Water persistence
169154
water_persistence = l4_water_persistence.water_persistence(
170155
xx, self.watper_threshold
171156
)
172-
# Apply water persistence expcted classes
173-
water_persistence = self.apply_mapping(
174-
water_persistence, self.waterper_wat_mapping
175-
)
157+
# # Apply water persistence expcted classes
158+
# water_persistence = utils.apply_mapping(
159+
# water_persistence, self.waterper_wat_mapping
160+
# )
176161

177162
water_seasonality = self.define_water_seasonality(xx)
178163

179-
l4_ctv_ntv_nav = l4_natural_aquatic.natural_auquatic_veg(
180-
l4_ctv_ntv, lifeform, veg_cover, water_seasonality
164+
l4 = l4_natural_aquatic.natural_auquatic_veg(
165+
l4, lifeform, veg_cover, water_seasonality
181166
)
182-
print("***** NAV : ", np.unique(l4_ctv_ntv_nav.compute()))
183-
l4_ctv_ntv_nav_surface = l4_surface.lc_l4_surface(
184-
l4_ctv_ntv_nav, level3, bare_gradation
185-
)
186-
print("***** SURFACE : ", np.unique(l4_ctv_ntv_nav_surface.compute()))
167+
168+
l4 = l4_surface.lc_l4_surface(l4, level3, bare_gradation)
169+
187170
# #TODO WATER (99-104)
188171
level4 = l4_water.water_classification(
189-
l4_ctv_ntv_nav_surface, level3, intertidal_mask, water_persistence
172+
l4, level3, intertidal_mask, water_persistence
190173
)
191-
print("***** LEVEL3:", np.unique(level3.compute()))
174+
192175
attrs = xx.attrs.copy()
193176
attrs["nodata"] = NODATA
194177
# l3 = level3.squeeze(dim=["spec"])
@@ -206,7 +189,6 @@ def reduce(self, xx: xr.Dataset) -> xr.Dataset:
206189

207190
coords = dict((dim, xx.coords[dim]) for dim in dims)
208191

209-
print(xr.Dataset(data_vars=data_vars, coords=coords, attrs=xx.attrs))
210192
return xr.Dataset(data_vars=data_vars, coords=coords, attrs=xx.attrs)
211193

212194

tests/test_lc_l4_ctv.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
import numpy as np
22
import xarray as xr
3-
import dask.array as da
43
from odc.stats.plugins.lc_level34 import StatsLccsLevel4
54
from odc.stats.plugins.l34_utils import l4_cultivated, lc_level3, l4_veg_cover
65

7-
import pytest
86
import pandas as pd
97

108
NODATA = 255
@@ -117,7 +115,6 @@ def test_ctv_classes_woody():
117115
intertidal_mask, level3 = lc_level3.lc_level3(xx)
118116
lifeform = stats_l4.define_life_form(xx)
119117
veg_cover = l4_veg_cover.canopyco_veg_con(xx, stats_l4.veg_threshold)
120-
veg_cover = stats_l4.apply_mapping(veg_cover, stats_l4.veg_mapping)
121118

122119
l4_ctv = l4_cultivated.lc_l4_cultivated(
123120
xx.classes_l3_l4, level3, lifeform, veg_cover
@@ -199,7 +196,6 @@ def test_ctv_classes_herbaceous():
199196
intertidal_mask, level3 = lc_level3.lc_level3(xx)
200197
lifeform = stats_l4.define_life_form(xx)
201198
veg_cover = l4_veg_cover.canopyco_veg_con(xx, stats_l4.veg_threshold)
202-
veg_cover = stats_l4.apply_mapping(veg_cover, stats_l4.veg_mapping)
203199

204200
l4_ctv = l4_cultivated.lc_l4_cultivated(
205201
xx.classes_l3_l4, level3, lifeform, veg_cover
@@ -281,7 +277,6 @@ def test_ctv_classes_woody_herbaceous():
281277
intertidal_mask, level3 = lc_level3.lc_level3(xx)
282278
lifeform = stats_l4.define_life_form(xx)
283279
veg_cover = l4_veg_cover.canopyco_veg_con(xx, stats_l4.veg_threshold)
284-
veg_cover = stats_l4.apply_mapping(veg_cover, stats_l4.veg_mapping)
285280

286281
l4_ctv = l4_cultivated.lc_l4_cultivated(
287282
xx.classes_l3_l4, level3, lifeform, veg_cover
@@ -363,7 +358,6 @@ def test_ctv_classes_no_vegcover():
363358
intertidal_mask, level3 = lc_level3.lc_level3(xx)
364359
lifeform = stats_l4.define_life_form(xx)
365360
veg_cover = l4_veg_cover.canopyco_veg_con(xx, stats_l4.veg_threshold)
366-
veg_cover = stats_l4.apply_mapping(veg_cover, stats_l4.veg_mapping)
367361

368362
l4_ctv = l4_cultivated.lc_l4_cultivated(
369363
xx.classes_l3_l4, level3, lifeform, veg_cover

tests/test_lc_l4_natural_surface.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
import numpy as np
66
import xarray as xr
7-
import dask.array as da
7+
88
from odc.stats.plugins.lc_level34 import StatsLccsLevel4
99
from odc.stats.plugins.l34_utils import (
1010
l4_cultivated,
@@ -16,7 +16,6 @@
1616
l4_bare_gradation,
1717
)
1818

19-
import pytest
2019
import pandas as pd
2120

2221
NODATA = 255
@@ -161,7 +160,6 @@ def test_ns():
161160
intertidal_mask, level3 = lc_level3.lc_level3(xx)
162161
lifeform = stats_l4.define_life_form(xx)
163162
veg_cover = l4_veg_cover.canopyco_veg_con(xx, stats_l4.veg_threshold)
164-
veg_cover = stats_l4.apply_mapping(veg_cover, stats_l4.veg_mapping)
165163

166164
# Apply cultivated to match the code in Level4 processing
167165
l4_ctv = l4_cultivated.lc_l4_cultivated(
@@ -178,8 +176,6 @@ def test_ns():
178176
bare_gradation = l4_bare_gradation.bare_gradation(
179177
xx, stats_l4.bare_threshold, veg_cover
180178
)
181-
# Apply bare gradation expected output classes
182-
bare_gradation = stats_l4.apply_mapping(bare_gradation, stats_l4.bs_mapping)
183179

184180
l4_ctv_ntv_nav_surface = l4_surface.lc_l4_surface(
185181
l4_ctv_ntv_nav, level3, bare_gradation

0 commit comments

Comments
 (0)