def test_vegetation(self): # should not raise niche_vlaanderen.Vegetation() badveg = "tests/data/bad_ct/bad_vegetation.csv" with pytest.warns(UserWarning): with pytest.raises(CodeTableException): niche_vlaanderen.Vegetation(ct_vegetation=badveg)
def test_borders(self): soil_code = np.array([3, 3, 3, 3, 3]) mhw = np.array([21, 20, 10, 1, 0]) mlw = np.array([30, 30, 30, 30, 30]) v = niche_vlaanderen.Vegetation() veg_predict, _ = v.calculate(soil_code, mhw, mlw, full_model=False) expected = [0, 1, 1, 1, 0] np.testing.assert_equal(expected, veg_predict[1])
def test_deviation_mlw(self): v = niche_vlaanderen.Vegetation() soil_code = np.array([3, 3, 3, 3, 3, -99, 2]) mhw = np.array([5, 5, 5, 5, 5, 5, 5]) mlw = np.array([66, 50, 38, 25, 5, 25, 25]) d = v.calculate_deviation(soil_code, mhw, mlw) expected = np.array([28, 12, 0, 0, -15, np.nan, np.nan]) np.testing.assert_equal(expected, d["mlw_01"])
def test_deviation_mhw(self): v = niche_vlaanderen.Vegetation() soil_code = np.array([3, 3, 3, 3, -99, 2]) mhw = np.array([66, 16, 5, -5, 5, 5]) mlw = np.array([35, 35, 35, 35, 35, 35]) d = v.calculate_deviation(soil_code, mhw, mlw) expected = np.array([46, 0, 0, -6, np.nan, np.nan]) np.testing.assert_equal(expected, d["mhw_01"])
def test_all_nodata(self): soil_code = raster_to_numpy("tests/data/small/soil_code.asc") mlw = raster_to_numpy("tests/data/small/mlw.asc") mhw = mlw.copy() mhw.fill(np.nan) v = niche_vlaanderen.Vegetation() with pytest.raises(NicheException): v.calculate(soil_code, mhw, mlw)
def test_one_value(self): nutrient_level = np.array([5]) acidity = np.array([3]) mlw = np.array([50]) mhw = np.array([10]) soil_code = np.array([140000]) v = niche_vlaanderen.Vegetation() veg_predict, veg_occurrence = v.calculate(soil_code, mhw, mlw, nutrient_level, acidity) correct = [] # no types should match for vi in veg_predict: if vi in correct: np.testing.assert_equal(np.array([1]), veg_predict[vi]) else: np.testing.assert_equal(np.array([0]), veg_predict[vi])
def test_one_value_simple(self): mlw = np.array([50]) mhw = np.array([10]) soil_code = np.array([8]) v = niche_vlaanderen.Vegetation() veg_predict, veg_occurrence = v.calculate(soil_code, mhw, mlw, full_model=False) correct = [3, 8, 11, 18, 23, 27] for vi in veg_predict: if vi in correct: np.testing.assert_equal(np.array([1]), veg_predict[vi]) else: np.testing.assert_equal(np.array([0]), veg_predict[vi])
def test_simple_doc_inundation(self): nutrient_level = np.array([4]) acidity = np.array([3]) mlw = np.array([50]) mhw = np.array([10]) soil_code = np.array([14]) inundation = np.array([1]) v = niche_vlaanderen.Vegetation() veg_predict, veg_occurrence = \ v.calculate(soil_code, mhw, mlw, nutrient_level, acidity, inundation=inundation) correct = [7, 12, 16] for vi in veg_predict: if vi in correct: np.testing.assert_equal(np.array([1]), veg_predict[vi]) else: np.testing.assert_equal(np.array([0]), veg_predict[vi])
def test_occurrence(self): nutrient_level = np.array([[4, 4], [4, 5]]) acidity = np.array([[3, 3], [3, 255]]) mlw = np.array([[50, 50], [50, 50]]) mhw = np.array([[31, 30], [10, 4]]) soil_code = np.array([[14, 14], [14, 14]]) inundation = np.array([[1, 1], [1, 1]]) v = niche_vlaanderen.Vegetation() veg_predict, veg_occurrence = \ v.calculate(soil_code=soil_code, mhw=mhw, mlw=mlw, nutrient_level=nutrient_level, acidity=acidity, inundation=inundation) # check no data propagates nicely self.assertEqual(255, veg_predict[1][1, 1]) self.assertEqual(1 / 3, veg_occurrence[12]) self.assertEqual(1, veg_occurrence[7]) self.assertEqual(2 / 3, veg_occurrence[16])
def test_testcase(self): input_dir = "testcase/zwarte_beek/input/" soil_code = raster_to_numpy(input_dir + "soil_code.asc") soil_code_r = soil_code soil_code_r[soil_code > 0] = np.round(soil_code / 10000)[soil_code > 0] msw = raster_to_numpy(input_dir + "msw.asc") mhw = raster_to_numpy(input_dir + "mhw.asc") mlw = raster_to_numpy(input_dir + "mlw.asc") inundation = \ raster_to_numpy(input_dir + "inundation.asc") regenlens = raster_to_numpy(input_dir + "nullgrid.asc") seepage = raster_to_numpy(input_dir + "seepage.asc") conductivity = raster_to_numpy(input_dir + "minerality.asc") nitrogen_deposition = \ raster_to_numpy(input_dir + "nitrogen_atmospheric.asc") nitrogen_animal = raster_to_numpy(input_dir + "nullgrid.asc") nitrogen_fertilizer = raster_to_numpy(input_dir + "nullgrid.asc") management = raster_to_numpy(input_dir + "management.asc") nl = niche_vlaanderen.NutrientLevel() nutrient_level = nl.calculate(soil_code_r, msw, nitrogen_deposition, nitrogen_animal, nitrogen_fertilizer, management, inundation) a = niche_vlaanderen.Acidity() acidity = a.calculate(soil_code_r, mlw, inundation, seepage, conductivity, regenlens) v = niche_vlaanderen.Vegetation() veg_predict, veg_occurrence = v.calculate(soil_code_r, mhw, mlw, nutrient_level, acidity) for i in range(1, 28): vi = raster_to_numpy("testcase/zwarte_beek/vegetation/v%d.asc" % i) # TODO: this is dirty - we apply the same no data filter to the # original set the new set, as this was done incorrectly in the # original set. # this also means that if we predict no data everywhere the test # also works :-) vi[(veg_predict[i] == 255)] = 255 np.testing.assert_equal(vi, veg_predict[i])
def test_unique_mlw(self): badveg = "tests/data/bad_ct/differentmlw.csv" with pytest.raises(CodeTableException): niche_vlaanderen.Vegetation(ct_vegetation=badveg)