diff --git a/src/dopt_sensor_anomalies/_csharp_interface.py b/src/dopt_sensor_anomalies/_csharp_interface.py new file mode 100644 index 0000000..3602143 --- /dev/null +++ b/src/dopt_sensor_anomalies/_csharp_interface.py @@ -0,0 +1,14 @@ +from dopt_sensor_anomalies import detection + + +def sensor_anomalies_detection( + user_img_path: str, + pixels_per_metric_X: float, + pixels_per_metric_Y: float, +) -> None: + res = detection.pipeline( + user_img_path=user_img_path, + pixels_per_metric_X=pixels_per_metric_X, + pixels_per_metric_Y=pixels_per_metric_Y, + ) + res.unwrap() diff --git a/tests/_results/csharp_interface/window_19_fail_model.csv b/tests/_results/csharp_interface/window_19_fail_model.csv new file mode 100644 index 0000000..6d73f41 --- /dev/null +++ b/tests/_results/csharp_interface/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/csharp_interface/window_19_fail_model_Heatmap.png b/tests/_results/csharp_interface/window_19_fail_model_Heatmap.png new file mode 100644 index 0000000..6b4f430 Binary files /dev/null and b/tests/_results/csharp_interface/window_19_fail_model_Heatmap.png differ diff --git a/tests/test_csharp_interface.py b/tests/test_csharp_interface.py new file mode 100644 index 0000000..7f16b7c --- /dev/null +++ b/tests/test_csharp_interface.py @@ -0,0 +1,64 @@ +import shutil +from unittest.mock import patch + +import pytest + +from dopt_sensor_anomalies import _csharp_interface, constants, errors + + +@patch("dopt_sensor_anomalies._find_paths.STOP_FOLDER_NAME", "lib") +def test_sensor_anomalies_detection_FailImagePath(setup_temp_dir): + img_path = str(setup_temp_dir / "not-existing.bmp") + pixels_per_metric_X: float = 0.251 + pixels_per_metric_Y: float = 0.251 + + MESSAGE = "The provided path seems not to exist" + + with pytest.raises(FileNotFoundError, match=MESSAGE): + _csharp_interface.sensor_anomalies_detection( + img_path, pixels_per_metric_X, pixels_per_metric_Y + ) + + +@patch("dopt_sensor_anomalies._find_paths.STOP_FOLDER_NAME", "lib") +def test_sensor_anomalies_detection_FailElectrodeCount(path_img_with_failure_ElectrodeCount): + img_path = str(path_img_with_failure_ElectrodeCount) + pixels_per_metric_X: float = 0.251 + pixels_per_metric_Y: float = 0.251 + + MESSAGE = "Number of counted electrodes does not match the" + + with pytest.raises(errors.InvalidElectrodeCount, match=MESSAGE): + _csharp_interface.sensor_anomalies_detection( + img_path, pixels_per_metric_X, pixels_per_metric_Y + ) + + +@patch("dopt_sensor_anomalies._find_paths.STOP_FOLDER_NAME", "lib") +def test_sensor_anomalies_detection_Success( + results_folder, path_img_with_failure_TrainedModel +): + # paths: check files for existence and delete because of other tests + 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" + if csv_file.exists(): + csv_file.unlink() + if heatmap_file.exists(): + heatmap_file.unlink() + + img_path = str(path_img_with_failure_TrainedModel) + pixels_per_metric_X: float = 0.251 + pixels_per_metric_Y: float = 0.251 + + _csharp_interface.sensor_anomalies_detection( + img_path, pixels_per_metric_X, pixels_per_metric_Y + ) + + assert csv_file.exists() + assert heatmap_file.exists() + target_folder = results_folder / "csharp_interface" + target_folder.mkdir(exist_ok=True) + shutil.copy(csv_file, (target_folder / csv_file.name)) + shutil.copy(heatmap_file, (target_folder / heatmap_file.name)) diff --git a/tests/test_detection.py b/tests/test_detection.py index 18874cd..9481fe4 100644 --- a/tests/test_detection.py +++ b/tests/test_detection.py @@ -150,6 +150,16 @@ def test_full_pipeline_wrapped_FailElectrodeCount(path_img_with_failure_Electrod @patch("dopt_sensor_anomalies._find_paths.STOP_FOLDER_NAME", "lib") def test_full_pipeline_wrapped_Success(results_folder, path_img_with_failure_TrainedModel): + # paths: check files for existence and delete because of other tests + 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" + if csv_file.exists(): + csv_file.unlink() + if heatmap_file.exists(): + heatmap_file.unlink() + img_path = str(path_img_with_failure_TrainedModel) pixels_per_metric_X: float = 0.251 pixels_per_metric_Y: float = 0.251 @@ -159,11 +169,6 @@ def test_full_pipeline_wrapped_Success(results_folder, path_img_with_failure_Tra assert ret.status.code == 0 assert ret.status.ExceptionType is None - # 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))