diff --git a/niche_vlaanderen/acidity.py b/niche_vlaanderen/acidity.py index ca03944c..51a859cf 100644 --- a/niche_vlaanderen/acidity.py +++ b/niche_vlaanderen/acidity.py @@ -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 ---------- @@ -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) @@ -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"] diff --git a/niche_vlaanderen/codetables.py b/niche_vlaanderen/codetables.py index 6195311b..135927cb 100644 --- a/niche_vlaanderen/codetables.py +++ b/niche_vlaanderen/codetables.py @@ -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 diff --git a/niche_vlaanderen/flooding.py b/niche_vlaanderen/flooding.py index 37a063f5..dfb03f5d 100644 --- a/niche_vlaanderen/flooding.py +++ b/niche_vlaanderen/flooding.py @@ -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 diff --git a/niche_vlaanderen/nutrient_level.py b/niche_vlaanderen/nutrient_level.py index c072df84..4a775664 100644 --- a/niche_vlaanderen/nutrient_level.py +++ b/niche_vlaanderen/nutrient_level.py @@ -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( @@ -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 @@ -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 diff --git a/tests/test_acidity.py b/tests/test_acidity.py index 8a5af0a7..4c602be8 100644 --- a/tests/test_acidity.py +++ b/tests/test_acidity.py @@ -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, @@ -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 diff --git a/tests/test_vegetation.py b/tests/test_vegetation.py index 1aa8a0e4..ec1c34d6 100644 --- a/tests/test_vegetation.py +++ b/tests/test_vegetation.py @@ -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])), @@ -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, _ = \ @@ -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() @@ -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):