Skip to content

Commit

Permalink
Remove np.ma and np.empty statements
Browse files Browse the repository at this point in the history
  • Loading branch information
stijnvanhoey committed Oct 4, 2024
1 parent 5675ac0 commit 53523ac
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 30 deletions.
7 changes: 4 additions & 3 deletions niche_vlaanderen/acidity.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ def __init__(
self._ct_soil_codes = self._ct_soil_codes.set_index("soil_code")

def _calculate_soil_mlw(self, soil_code, mlw):
"""Calculate the soild mlw classes
"""Calculate the soil mlw classes
Parameters
----------
Expand All @@ -102,7 +102,7 @@ def _calculate_soil_mlw(self, soil_code, mlw):
self._ct_soil_codes.loc[self.nodata, "soil_group"] = self.nodata
soil_group = self._ct_soil_codes.soil_group[soil_code].values.astype("uint8")

result = np.ma.empty_like(soil_code)
result = np.ones(soil_code.shape, dtype=self.dtype) * self.nodata
for sel_group, subtable in self._ct_soil_mlw.groupby("soil_group"):
subtable = subtable.copy().reset_index(drop=True)
index = np.digitize(mlw, subtable.mlw_max, right=False)
Expand Down Expand Up @@ -155,7 +155,8 @@ def _get_acidity(self, rainwater, minerality, inundation,
seepage = seepage_class.flatten()
soil_mlw_class = soil_mlw_class.flatten()

result = np.empty_like(soil_mlw_class)
result = np.ones(soil_mlw_class.shape, dtype=self.dtype) * self.nodata


for labels, subtable in self._lnk_acidity.groupby(
["rainwater", "mineral_richness", "inundation", "seepage", "soil_mlw_class"]
Expand Down
2 changes: 1 addition & 1 deletion niche_vlaanderen/codetables.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ def check_codes_used(name, used, allowed):
----------
name : str
Variable name
used : np.ma.MaskedArray
used : np.ndarray
Grid with values to check
allowed : np.array
System table provided values
Expand Down
6 changes: 2 additions & 4 deletions niche_vlaanderen/flooding.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,10 +165,8 @@ def read_depth_to_grid(self, file_name):
self._context = SpatialContext(dst)

# Convert to depth data type and no-data value
band = band.astype(_allowed_input["depth"])
band = np.ma.masked_array(band.filled(255), mask=band.mask,
fill_value=255, dtype="uint8")
return band.filled() # return numpy array instead of masked array
band = band.filled(fill_value=255).astype(_allowed_input["depth"])
return band

def calculate(self, depth_file_path, frequency, period, duration):
"""Calculate a floodplain object
Expand Down
7 changes: 3 additions & 4 deletions niche_vlaanderen/nutrient_level.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def __init__(
# convert the mineralisation system table to float to use np.nan for nodata
self._ct_mineralisation["nitrogen_mineralisation"] = self._ct_mineralisation[
"nitrogen_mineralisation"
].astype("float64")
].astype("float32")

inner = all(v is None for v in self.__init__.__code__.co_varnames[1:])
validate_tables_nutrient_level(
Expand Down Expand Up @@ -110,8 +110,7 @@ def _calculate_mineralisation(self, soil_code, msw):
orig_shape = soil_code.shape
soil_code_array = soil_code.flatten()
msw_array = msw.flatten()
result = np.empty(soil_code_array.shape, dtype="float32")
result[:] = np.nan
result = np.ones(soil_code_array.shape, dtype="float32") * np.nan

for code in self._ct_mineralisation.soil_code.unique():
# We must reset the index because digitize will give indexes
Expand Down Expand Up @@ -156,7 +155,7 @@ def _calculate(self, management, soil_code, nitrogen, inundation):
np.isnan(nitrogen) | (inundation == 255))

# calculate management influence
influence = np.empty_like(management)
influence = np.ones(management.shape, dtype=self.dtype) * self.nodata
for i in self._ct_management.management.unique():
sel_grid = management == i
sel_ct = self._ct_management.management == i
Expand Down
14 changes: 7 additions & 7 deletions tests/test_acidity.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,11 @@ def test_acidity_support(self):

def test_acidity_support_nodata(self):
"""Correct acidity calculated from grids with non-empty mask"""
rainwater = np.ma.array([0, 0, 255], dtype="uint8")
minerality = np.ma.array([1, 1, 255], dtype="uint8")
inundation = np.ma.array([1, 1, 255], dtype="uint8")
seepage = np.ma.array([1, 1, np.nan], dtype="float32")
soil_mlw = np.ma.array([1, 1, 255], dtype="uint8")
rainwater = np.array([0, 0, 255], dtype="uint8")
minerality = np.array([1, 1, 255], dtype="uint8")
inundation = np.array([1, 1, 255], dtype="uint8")
seepage = np.array([1, 1, np.nan], dtype="float32")
soil_mlw = np.array([1, 1, 255], dtype="uint8")

a = niche_vlaanderen.Acidity()
result = a._get_acidity(rainwater, minerality, inundation,
Expand All @@ -81,13 +81,13 @@ def test_seepage(self):

def test_seepage_nodata(self):
"""Correct seepage calculated from grids with non-empty mask"""
seepage = np.ma.array(
seepage = np.array(
[5, 0.3, 0.05, -0.04, -0.2, -5, -0.1, -1, np.nan],
dtype="float32")
a = niche_vlaanderen.Acidity()
result = a._get_seepage(seepage)

expected = np.ma.array([1, 1, 1, 1, 2, 3, 2, 3, 255])
expected = np.array([1, 1, 1, 1, 2, 3, 2, 3, 255])
np.testing.assert_equal(expected, result)
assert result.dtype == np.uint8

Expand Down
22 changes: 11 additions & 11 deletions tests/test_vegetation.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,10 @@ def test_one_value_nomatch(self, single_value_input_arrays):
nutrient_level, acidity)
# no types should match
for vi in veg_predict:
np.testing.assert_equal(np.ma.array([0]), veg_predict[vi])
np.testing.assert_equal(np.array([0]), veg_predict[vi])

for vi in veg_detail:
np.testing.assert_equal(np.ma.array([0]), veg_detail[vi])
np.testing.assert_equal(np.array([0]), veg_detail[vi])

@pytest.mark.parametrize("arr_in, expected_0",
[("single_value_input_arrays", np.array([0])),
Expand Down Expand Up @@ -132,7 +132,7 @@ def test_occurrences_nodata_propagation(self):
inundation = np.array([[1, 1], [1, 1]], dtype="uint8")

# Add masked (no-data value) for acidity
acidity = np.ma.array([[3, 3], [3, 255]], dtype="uint8")
acidity = np.array([[3, 3], [3, 255]], dtype="uint8")

v = niche_vlaanderen.Vegetation()
veg_predict, veg_occurrence, _ = \
Expand Down Expand Up @@ -180,7 +180,7 @@ def test_testcase(self, path_testcase, zwarte_beek_data):
def test_all_nodata(self, path_testdata):
"""Variable with all no-data values raises error"""
soil_code = np.array([14, 14, 14], dtype="uint8")
mlw = np.ma.array([np.nan, np.nan, np.nan], dtype="float32")
mlw = np.array([np.nan, np.nan, np.nan], dtype="float32")
mhw = mlw.copy()

v = niche_vlaanderen.Vegetation()
Expand All @@ -192,24 +192,24 @@ def test_deviation_mhw(self):
v = niche_vlaanderen.Vegetation()

soil_code = np.array([3, 3, 3, 3, 255, 2], dtype="uint8")
mhw = -1 * np.ma.array([66, 16, 5, -5, 5, 5], dtype="float32")
mlw = -1 * np.ma.array([35, 35, 35, 35, 35, 35], dtype="float32")
mhw = -1 * np.array([66, 16, 5, -5, 5, 5], dtype="float32")
mlw = -1 * np.array([35, 35, 35, 35, 35, 35], dtype="float32")
d = v.calculate_deviation(soil_code, mhw, mlw)

# Both a Nan inside the mask as well as a calculated Nan in the data
expected = np.ma.array([46, 0, 0, -6, np.nan, np.nan])
expected = np.array([46, 0, 0, -6, np.nan, np.nan])
np.testing.assert_equal(expected, d["mhw_01"])

def test_deviation_mlw(self):
"""Correct deviation calculated for mhw with mask-nan versus calculated nan"""
v = niche_vlaanderen.Vegetation()

soil_code = np.ma.array([3, 3, 3, 3, 3, 255, 2], dtype="uint8")
mhw = -1 * np.ma.array([5, 5, 5, 5, 5, 5, 5], dtype="float32")
mlw = -1 * np.ma.array([66, 50, 38, 25, 5, 25, 25], dtype="float32")
soil_code = np.array([3, 3, 3, 3, 3, 255, 2], dtype="uint8")
mhw = -1 * np.array([5, 5, 5, 5, 5, 5, 5], dtype="float32")
mlw = -1 * np.array([66, 50, 38, 25, 5, 25, 25], dtype="float32")
d = v.calculate_deviation(soil_code, mhw, mlw)

expected = np.ma.array([28, 12, 0, 0, -15, np.nan, np.nan])
expected = np.array([28, 12, 0, 0, -15, np.nan, np.nan])
np.testing.assert_equal(expected, d["mlw_01"])

def test_detailed_vegetation(self, single_value_input_arrays):
Expand Down

0 comments on commit 53523ac

Please sign in to comment.