diff --git a/tests/_img/failures/window_15_fail_electrode.bmp b/tests/_img/failures/window_15_fail_electrode.bmp new file mode 100644 index 0000000..daf5513 Binary files /dev/null and b/tests/_img/failures/window_15_fail_electrode.bmp differ diff --git a/tests/_img/failures/window_19_fail_model.bmp b/tests/_img/failures/window_19_fail_model.bmp new file mode 100644 index 0000000..3c2b7fe Binary files /dev/null and b/tests/_img/failures/window_19_fail_model.bmp differ diff --git a/tests/_models/patchcore_model_left_hand_side.pth b/tests/_models/patchcore_model_left_hand_side.pth new file mode 100644 index 0000000..e19f6ef Binary files /dev/null and b/tests/_models/patchcore_model_left_hand_side.pth differ diff --git a/tests/_models/patchcore_model_right_hand_side.pth b/tests/_models/patchcore_model_right_hand_side.pth new file mode 100644 index 0000000..c2cade9 Binary files /dev/null and b/tests/_models/patchcore_model_right_hand_side.pth differ diff --git a/tests/_results/window_19_fail_model.csv b/tests/_results/window_19_fail_model.csv new file mode 100644 index 0000000..6d73f41 --- /dev/null +++ b/tests/_results/window_19_fail_model.csv @@ -0,0 +1 @@ +1177,318;804,803;947509,0;876,575;808,853;709020,9;952,191;804,781;766305,3;944,223;792,829;748607,2;838,797;804,902;675148,9;1203,187;792,829;953921,4;0;0 diff --git a/tests/_results/window_19_fail_model_Heatmap.png b/tests/_results/window_19_fail_model_Heatmap.png new file mode 100644 index 0000000..6b4f430 Binary files /dev/null and b/tests/_results/window_19_fail_model_Heatmap.png differ diff --git a/tests/conftest.py b/tests/conftest.py new file mode 100644 index 0000000..a690261 --- /dev/null +++ b/tests/conftest.py @@ -0,0 +1,62 @@ +import os +import shutil +from pathlib import Path +from unittest.mock import patch + +import pytest + +from dopt_sensor_anomalies.constants import MODEL_FOLDER_NAME + + +@pytest.fixture(scope="session", autouse=True) +def setup_temp_dir(tmp_path_factory): + tmp_dir = tmp_path_factory.mktemp("root") + folder_structure = "lib/folder" + pth = tmp_dir / folder_structure + pth.mkdir(parents=True, exist_ok=True) + # models + pth_models = tmp_dir / MODEL_FOLDER_NAME + pth_models.mkdir(parents=True, exist_ok=True) + _root_imgs = (Path(__file__).parent / "_models").glob("*.pth") + for model in _root_imgs: + dst = pth_models / model.name + shutil.copy(model, dst) + # images + pth_img = tmp_dir / "images" + pth_img.mkdir(parents=True, exist_ok=True) + _root_imgs = (Path(__file__).parent / "_img").glob("**/*.bmp") + for img in _root_imgs: + dst = pth_img / img.name + shutil.copy(img, dst) + + with patch("dopt_sensor_anomalies._find_paths.LIB_ROOT_PATH", pth): + yield tmp_dir + + +@pytest.fixture(scope="session", autouse=True) +def results_folder(setup_temp_dir) -> Path: + if os.getenv("DOPT_WRITE_RESULTS", False): + results_base = Path(__file__).parent + else: + results_base = setup_temp_dir + results = results_base / "_results" + if not results.exists(): + results.mkdir() + + return results + + +@pytest.fixture(scope="session") +def path_img_with_failure_ElectrodeCount(setup_temp_dir) -> Path: + filename = "window_15_fail_electrode.bmp" + pth_img = setup_temp_dir / f"images/{filename}" + assert pth_img.exists(), "failure image not existing" + return pth_img + + +@pytest.fixture(scope="session") +def path_img_with_failure_TrainedModel(setup_temp_dir) -> Path: + filename = "window_19_fail_model.bmp" + pth_img = setup_temp_dir / f"images/{filename}" + assert pth_img.exists(), "failure image not existing" + return pth_img diff --git a/tests/test_detection.py b/tests/test_detection.py index 338a3ba..df0873c 100644 --- a/tests/test_detection.py +++ b/tests/test_detection.py @@ -1,10 +1,14 @@ +import shutil from pathlib import Path +from unittest.mock import patch import numpy as np import pytest +import dopt_sensor_anomalies._find_paths import dopt_sensor_anomalies.detection as detect import dopt_sensor_anomalies.types as t +from dopt_sensor_anomalies import constants @pytest.fixture(scope="module") @@ -84,3 +88,40 @@ def test_measure_length(single_img_path): assert 235 < img_right.shape[0] < 260 assert 910 < img_right.shape[1] < 960 assert img_right.shape[2] == 3 + + +@pytest.mark.new +@patch("dopt_sensor_anomalies._find_paths.STOP_FOLDER_NAME", "lib") +def test_isolated_pipeline(results_folder, path_img_with_failure_TrainedModel): + pixels_per_metric_X: float = 0.251 + pixels_per_metric_Y: float = 0.251 + MODEL_FOLDER = dopt_sensor_anomalies._find_paths.get_model_folder() + assert MODEL_FOLDER.exists(), "model folder not existing" + DETECTION_MODELS = dopt_sensor_anomalies._find_paths.get_detection_models(MODEL_FOLDER) + assert DETECTION_MODELS["left"].exists() + assert DETECTION_MODELS["right"].exists() + data_csv, sensor_images = detect.measure_length( + path_img_with_failure_TrainedModel, + pixels_per_metric_X, + pixels_per_metric_Y, + ) + print(">>>>>>> Data: ", data_csv) + # measured sizes + assert len(data_csv) == 18 + assert sensor_images["left"] is not None + assert sensor_images["right"] is not None + detect.anomaly_detection( + file_path=path_img_with_failure_TrainedModel, + detection_models=DETECTION_MODELS, + data_csv=data_csv, + sensor_images=sensor_images, + ) + # check files for existence + root_img = path_img_with_failure_TrainedModel.parent + file_stem = path_img_with_failure_TrainedModel.stem + csv_file = root_img / f"{file_stem}.csv" + heatmap_file = root_img / f"{file_stem}{constants.HEATMAP_FILENAME_SUFFIX}.png" + assert csv_file.exists() + assert heatmap_file.exists() + shutil.copy(csv_file, (results_folder / csv_file.name)) + shutil.copy(heatmap_file, (results_folder / heatmap_file.name)) diff --git a/tests/test_find_paths.py b/tests/test_find_paths.py index 696cd73..3a6d785 100644 --- a/tests/test_find_paths.py +++ b/tests/test_find_paths.py @@ -5,16 +5,23 @@ import pytest from dopt_sensor_anomalies import _find_paths +# @pytest.fixture(scope="session", autouse=True) +# def setup_temp_dir(tmp_path_factory): +# tmp_dir = tmp_path_factory.mktemp("root") +# folder_structure = "lib/folder" +# pth = tmp_dir / folder_structure +# pth.mkdir(parents=True, exist_ok=True) +# # models +# folder_models = "lib/models" +# pth_models = tmp_dir / folder_models +# pth_models.mkdir(parents=True, exist_ok=True) +# _root_models = (Path(__file__).parent / "_models").glob("*.pth") +# for model in _root_models: +# dst = pth_models / model.name +# shutil.copy(model, dst) -@pytest.fixture(scope="module", autouse=True) -def setup_temp_dir(tmp_path_factory): - tmp_dir = tmp_path_factory.mktemp("root") - folder_structure = "lib/folder" - pth = tmp_dir / folder_structure - pth.mkdir(parents=True, exist_ok=True) - - with patch("dopt_sensor_anomalies._find_paths.LIB_ROOT_PATH", pth): - yield +# with patch("dopt_sensor_anomalies._find_paths.LIB_ROOT_PATH", pth): +# yield @pytest.fixture()