diff --git a/.vscode/settings.json b/.vscode/settings.json index 9b388533..e69de29b 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,7 +0,0 @@ -{ - "python.testing.pytestArgs": [ - "tests" - ], - "python.testing.unittestEnabled": false, - "python.testing.pytestEnabled": true -} \ No newline at end of file diff --git a/eis_toolkit/exceptions.py b/eis_toolkit/exceptions.py index 6d1bcd01..6dec2159 100644 --- a/eis_toolkit/exceptions.py +++ b/eis_toolkit/exceptions.py @@ -2,6 +2,10 @@ class CoordinatesOutOfBoundsException(Exception): """Exception error class for out of bound coordinates.""" +class ClassificationFailedException(Exception): + """Exception error class for classification failures.""" + + class EmptyDataFrameException(Exception): """Exception error class raised if the dataframe is empty.""" @@ -46,16 +50,12 @@ class InvalidWktFormatException(Exception): """Exception error for invalid WKT format.""" -class InvalidColumnIndexException(Exception): - """Exception error for index out of range.""" - - -class UnFavorableClassDoesntExistException(Exception): - """Exception error class for failure to generalize classes using the given studentised contrast threshold value. Class 1 (unfavorable class) doesn't exist""" +class MatchingCrsException(Exception): + """Exception error class for CRS matches.""" -class FavorableClassDoesntExistException(Exception): - """Exception error class for failure to generalize classes using the given studentised contrast threshold value. Class 2 (favorable class) doesn't exist""" +class MatchingRasterGridException(Exception): + """Exception error class for raster grid matches.""" class NotApplicableGeometryTypeException(Exception): diff --git a/eis_toolkit/prediction/weights_of_evidence.py b/eis_toolkit/prediction/weights_of_evidence.py index 66a0143a..f965a2b6 100644 --- a/eis_toolkit/prediction/weights_of_evidence.py +++ b/eis_toolkit/prediction/weights_of_evidence.py @@ -7,6 +7,7 @@ from beartype import beartype from beartype.typing import List, Literal, Optional, Sequence, Tuple, Union +from eis_toolkit import exceptions from eis_toolkit.vector_processing.rasterize_vector import rasterize_vector @@ -89,8 +90,12 @@ def _reclassify_by_studentized_contrast(df: pd.DataFrame, studentized_contrast_t """Create generalized classes based on the studentized contrast threhsold value.""" index = df.idxmax()["Contrast"] - if df.loc[index, "Studentized contrast"] < studentized_contrast_threshold: - raise Exception("Failed, studentized contrast is {}".format(df.loc[index, "Studentized contrast"])) + if df.loc[index, "Studentized contrast"] < studentized_contrast_threshold or index == len(df.index) - 1: + raise exceptions.ClassificationFailedException( + "Failed to create generalized classes with given studentized contrast treshold ({})".format( + df.loc[index, "Studentized contrast"] + ) + ) df["Generalized class"] = 1 for i in range(0, index + 1): diff --git a/tests/prediction/wofe/wofe_calculate_responses_test.py b/tests/prediction/wofe/wofe_calculate_responses_test.py deleted file mode 100644 index 50f1d594..00000000 --- a/tests/prediction/wofe/wofe_calculate_responses_test.py +++ /dev/null @@ -1,46 +0,0 @@ -import pytest -import numpy as np -from pathlib import Path -import rasterio -import pandas as pd -from pandas.testing import assert_frame_equal - -from eis_toolkit.prediction.weights_of_evidence.calculate_responses import calculate_responses - -parent_dir = Path(__file__).parent.parent.parent -print(parent_dir) - -# Paths of files to be used as inputs to the calculate responses function -wgts_rst_un_path = parent_dir.joinpath("data/remote/wofe/Merged_Unique.tif") -wgts_rst_asc_path = parent_dir.joinpath("data/remote/wofe/Merged_Ascending_.tif") -wgts_rst_dsc_path = parent_dir.joinpath("data/remote/wofe/Merged_Descending_.tif") -dep_rst_path = parent_dir.joinpath("data/remote/wofe/wofe_dep_nan_.tif") - -# Path to the file to compare the results of the calculates responses function -merged_pprb_path = parent_dir.joinpath("data/remote/wofe/Merged_pprbs.tif") - -# Actual testing function -def test_calculate_responses(): - """Tests if calculate_responses function works as intended""" - # expected arrays - pprb_rstrs = rasterio.open(merged_pprb_path) - pprb, std, conf = np.array(pprb_rstrs.read(1)), np.array(pprb_rstrs.read(2)), np.array(pprb_rstrs.read(3)) - exp_pprbs = [pprb, std, conf] - - #input to calculate_responses function - test_dep = rasterio.open(dep_rst_path) - wgts_rstr_paths = [wgts_rst_un_path, wgts_rst_asc_path, wgts_rst_dsc_path] - arrys_list_from_wgts = [] - for path_rst in wgts_rstr_paths: - wgts_rst_o = rasterio.open(path_rst) - pprb_, std_, conf_ = np.array(wgts_rst_o.read(1)), np.array(wgts_rst_o.read(2)), np.array(wgts_rst_o.read(3)) - list_one_block = [pprb_, std_, conf_] - arrys_list_from_wgts.append(list_one_block) - - # Call the calculate_responses function - t_pprb_array, t_pprb_std, t_pprb_conf, array_meta = calculate_responses(test_dep, arrys_list_from_wgts) - result_pprbs = [t_pprb_array, t_pprb_std, t_pprb_conf] - - #compare the results - for res, exp in zip(result_pprbs, exp_pprbs): - assert res.all() == exp.all() diff --git a/tests/prediction/wofe/wofe_calculate_weights_test.py b/tests/prediction/wofe/wofe_calculate_weights_test.py deleted file mode 100644 index d0066628..00000000 --- a/tests/prediction/wofe/wofe_calculate_weights_test.py +++ /dev/null @@ -1,46 +0,0 @@ -import pytest -import numpy as np -from pathlib import Path -import rasterio -import pandas as pd -from pandas.testing import assert_frame_equal - -from eis_toolkit.prediction.weights_of_evidence.calculate_weights import calculate_weights - -parent_dir = Path(__file__).parent.parent.parent -print(parent_dir) - -# Paths of files to be used as inputs to the weights_calculations function -ev_rst_path = parent_dir.joinpath("data/remote/wofe/wofe_ev_nan.tif") -dep_rst_path = parent_dir.joinpath("data/remote/wofe/wofe_dep_nan_.tif") - -# Paths to the files to compare the results of the weigths_calculations function to -wgts_rst_un_path = parent_dir.joinpath("data/remote/wofe/Merged_Unique.tif") -wgts_rst_asc_path = parent_dir.joinpath("data/remote/wofe/Merged_Ascending_.tif") -wgts_rst_dsc_path = parent_dir.joinpath("data/remote/wofe/Merged_Descending_.tif") - -wgts_un_path = parent_dir.joinpath("data/remote/wofe/exp_wgts_un.csv") -wgts_asc_path = parent_dir.joinpath("data/remote/wofe/exp_wgts_asc.csv") -wgts_dsc_path = parent_dir.joinpath("data/remote/wofe/exp_wgts_dsc.csv") - -@pytest.mark.parametrize("wgts_type, wgts_df_path, expected", [(0, wgts_un_path, wgts_rst_un_path), (1, wgts_asc_path, wgts_rst_asc_path), (2, wgts_dsc_path, wgts_rst_dsc_path)]) - -def test_calculate_weights( wgts_type, wgts_df_path, expected): - """Tests if calculate_weights function runs as intended; tests for all three weights type""" - #expected results - # weights dataframe - wgts_ = pd.read_csv(wgts_df_path).set_index('Clss') - exp_wgts_df = pd.DataFrame(wgts_) - # weights arrays - wgts_rstr_ = rasterio.open(expected) - clss, wgts, std = np.array(wgts_rstr_.read(1)), np.array(wgts_rstr_.read(2)), np.array(wgts_rstr_.read(3)) - exp_list_arr = [clss, wgts, std] - #inputs to function - ev_rst_ = rasterio.open(ev_rst_path) - dep_rst_ = rasterio.open(dep_rst_path) - #Calling the function - wgts_df, wgts_arr, rst_meta = calculate_weights(ev_rst=ev_rst_, dep_rst= dep_rst_, nan_val = -1000000000.0, w_type=wgts_type, stud_cont=2) - assert_frame_equal(wgts_df, exp_wgts_df, check_dtype=False, check_index_type=False) - for res, exp in zip(wgts_arr, exp_list_arr): - assert res.all() == exp.all() - \ No newline at end of file