From 247f72cb51260a6888128a769546c9ea55bc84d2 Mon Sep 17 00:00:00 2001 From: foefl Date: Thu, 23 Oct 2025 09:45:15 +0200 Subject: [PATCH] add additional edge case to find models --- src/dopt_sensor_anomalies/_find_paths.py | 23 ++++++++++++++++++++--- tests/test_find_paths.py | 16 ++++++++++++++-- 2 files changed, 34 insertions(+), 5 deletions(-) diff --git a/src/dopt_sensor_anomalies/_find_paths.py b/src/dopt_sensor_anomalies/_find_paths.py index 7033ac3..e2c0ac7 100644 --- a/src/dopt_sensor_anomalies/_find_paths.py +++ b/src/dopt_sensor_anomalies/_find_paths.py @@ -7,26 +7,43 @@ from dopt_sensor_anomalies.constants import LIB_ROOT_PATH, MODEL_FOLDER_NAME, ST def get_model_folder() -> Path: + """retrieves the folder which contains the trained and needed models for anomaly detection + + Returns + ------- + Path + the path containing the models + + Raises + ------ + FileNotFoundError + raised if the application's root folder is not found or the model folder is not present + """ + path_found = dopt_basics.io.search_folder_path( starting_path=LIB_ROOT_PATH, stop_folder_name=STOP_FOLDER_NAME ) if path_found is None: + raise FileNotFoundError("The application's root directory could not be determined.") + + model_folder = path_found / MODEL_FOLDER_NAME + if not model_folder.exists(): raise FileNotFoundError( "The model folder was not found in the application's root directory." ) - return path_found / MODEL_FOLDER_NAME + return model_folder def get_detection_models(model_folder: Path) -> t.DetectionModels: left_model_search = tuple(model_folder.glob("*left_hand_side*.pth")) - if not left_model_search: + if len(left_model_search) == 0: raise ValueError("No model for the left hand side found.") if len(left_model_search) > 1: raise ValueError("Too many models for the left hand side found.") right_model_search = tuple(model_folder.glob("*right_hand_side*.pth")) - if not right_model_search: + if len(right_model_search) == 0: raise ValueError("No model for the right hand side found.") elif len(right_model_search) > 1: raise ValueError("Too many models for the right hand side found.") diff --git a/tests/test_find_paths.py b/tests/test_find_paths.py index 3a6d785..82d8eb1 100644 --- a/tests/test_find_paths.py +++ b/tests/test_find_paths.py @@ -56,8 +56,20 @@ def temp_model_folder_only_right(tmp_path_factory) -> Path: @patch("dopt_sensor_anomalies._find_paths.STOP_FOLDER_NAME", "not-found") -def test_get_model_folder_Fail_NotFound(): - with pytest.raises(FileNotFoundError): +def test_get_model_folder_Fail_NotFoundApplicationRoot(): + with pytest.raises( + FileNotFoundError, match="application's root directory could not be determined" + ): + _ = _find_paths.get_model_folder() + + +@patch("dopt_sensor_anomalies._find_paths.STOP_FOLDER_NAME", "lib") +@patch("dopt_sensor_anomalies._find_paths.MODEL_FOLDER_NAME", "not-found") +def test_get_model_folder_Fail_NotFoundModelFolder(): + with pytest.raises( + FileNotFoundError, + match="model folder was not found in the application's root directory", + ): _ = _find_paths.get_model_folder()