add additional edge case to find models

This commit is contained in:
Florian Förster 2025-10-23 09:45:15 +02:00
parent 1b44877980
commit 247f72cb51
2 changed files with 34 additions and 5 deletions

View File

@ -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.")

View File

@ -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()