Spaces:
Paused
Paused
Download tests/test_eye_models.py from drrobot9/neural-forge: direct link, hf CLI and curl.
- Browser
- Download file 5.23 kB
-
https://huggingface.co/spaces/drrobot9/neural-forge/resolve/main/tests/test_eye_models.py
- Command line
-
hf download hf://spaces/drrobot9/neural-forge/tests/test_eye_models.py
-
curl -L -o test_eye_models.py https://huggingface.co/spaces/drrobot9/neural-forge/resolve/main/tests/test_eye_models.py
5.23 kB
| import json | |
| import shutil | |
| import cv2 | |
| import numpy as np | |
| import pytest | |
| from neural_forge.eye_inference import reference_pipeline | |
| from neural_forge.eye_models import EyeModelStore | |
| def read_rgb(path): | |
| return cv2.cvtColor(cv2.imread(str(path)), cv2.COLOR_BGR2RGB) | |
| def ghana_crop_shape(fixtures_directory): | |
| entry = json.loads((fixtures_directory / "expected_outputs.json").read_text())["ghana_crops"][0] | |
| crop = read_rgb(fixtures_directory / entry["file"]) | |
| return reference_pipeline.region_mask_from_composited_crop(crop) | |
| def fill_shape(texture_rgb, shape_mask): | |
| resized = cv2.resize(texture_rgb, (shape_mask.shape[1], shape_mask.shape[0]), interpolation=cv2.INTER_AREA) | |
| alpha = np.clip(cv2.GaussianBlur(shape_mask.astype(np.float32), (0, 0), 1.0) * 255, 0, 255).astype(np.uint8) | |
| return reference_pipeline.composite_region_on_neutral_background(resized, alpha) | |
| def test_model_b_preprocessing_matches_reference_outputs(eye_models, fixtures_directory): | |
| expected = json.loads((fixtures_directory / "expected_outputs.json").read_text()) | |
| for entry in expected["ghana_crops"]: | |
| crop = read_rgb(fixtures_directory / entry["file"]) | |
| tensor = reference_pipeline.preprocess_for_anaemia(crop) | |
| assert tensor.shape == (1, 3, 224, 224) | |
| assert tensor.sum() == pytest.approx(entry["anaemia_input_tensor_sum"], rel=1e-5, abs=1e-2) | |
| assert eye_models.score_anaemia(crop) == pytest.approx(entry["calibrated_probability"], abs=1e-4) | |
| def test_gate_scores_match_reference_and_ghana_crops_pass(eye_models, fixtures_directory): | |
| expected = json.loads((fixtures_directory / "expected_outputs.json").read_text()) | |
| for entry in expected["ghana_crops"]: | |
| result = eye_models.assess_crop(read_rgb(fixtures_directory / entry["file"])) | |
| assert result["gate_score"] == pytest.approx(entry["gate_score"], abs=1e-4) | |
| assert result["assessable"] is entry["gate_assessable"] | |
| def test_gate_rejects_noise(eye_models, fixtures_directory): | |
| shape = ghana_crop_shape(fixtures_directory) | |
| random_generator = np.random.default_rng(0) | |
| for _ in range(3): | |
| noise = random_generator.integers(0, 256, (shape.shape[0], shape.shape[1], 3), dtype=np.uint8) | |
| assert eye_models.assess_crop(fill_shape(noise, shape))["assessable"] is False | |
| def test_gate_rejects_cheek_skin(eye_models, fixtures_directory): | |
| shape = ghana_crop_shape(fixtures_directory) | |
| expected = json.loads((fixtures_directory / "expected_outputs.json").read_text()) | |
| for entry in expected["face_frames"]: | |
| frame = read_rgb(fixtures_directory / entry["file"]) | |
| height, width = frame.shape[:2] | |
| cheek = frame[int(0.62 * height):int(0.72 * height), int(0.40 * width):int(0.47 * width)] | |
| assert eye_models.assess_crop(fill_shape(cheek, shape))["assessable"] is False | |
| def test_gate_rejects_heavily_blurred_crops(eye_models, fixtures_directory): | |
| expected = json.loads((fixtures_directory / "expected_outputs.json").read_text()) | |
| for entry in expected["ghana_crops"][:4]: | |
| crop = read_rgb(fixtures_directory / entry["file"]) | |
| region = reference_pipeline.region_mask_from_composited_crop(crop) | |
| sigma = 8.0 * max(crop.shape[:2]) / 224.0 | |
| blurred = cv2.GaussianBlur(crop, (0, 0), sigma) | |
| alpha = np.clip(cv2.GaussianBlur(region.astype(np.float32), (0, 0), 1.0) * 255, 0, 255).astype(np.uint8) | |
| assert eye_models.assess_crop(reference_pipeline.composite_region_on_neutral_background(blurred, alpha))["assessable"] is False | |
| def test_gate_rejects_missing_or_tiny_regions(eye_models): | |
| assert eye_models.assess_crop(None)["reason"] == reference_pipeline.GATE_REASON_NO_REGION | |
| tiny = np.full((20, 40, 3), 180, np.uint8) | |
| assert eye_models.assess_crop(tiny)["reason"] == reference_pipeline.GATE_REASON_CROP_TOO_SMALL | |
| def test_segmentation_matches_reference_class_fractions(eye_models, fixtures_directory): | |
| expected = json.loads((fixtures_directory / "expected_outputs.json").read_text()) | |
| for entry in expected["slid_images"]: | |
| labels = eye_models.segment_eye_crop(read_rgb(fixtures_directory / entry["file"])).argmax(axis=0) | |
| fractions = [float((labels == class_index).mean()) for class_index in range(4)] | |
| assert fractions == pytest.approx(entry["class_pixel_fractions"], abs=0.005) | |
| def test_model_with_wrong_sha256_is_refused(tmp_path, monkeypatch, eye_models): | |
| from tests.conftest import eye_model_directory | |
| copy_directory = tmp_path / "models" | |
| shutil.copytree(eye_model_directory(), copy_directory, ignore=shutil.ignore_patterns("test_fixtures")) | |
| gate_entry = eye_models.entries["neural_forge_conjunctiva_assessable_gate"] | |
| with open(copy_directory / gate_entry["onnx_file"], "ab") as onnx_file: | |
| onnx_file.write(b"tampered") | |
| monkeypatch.setenv("EYE_MODEL_LOCAL_DIRECTORY", str(copy_directory)) | |
| store = EyeModelStore() | |
| store.load() | |
| assert store.status["neural_forge_conjunctiva_assessable_gate"] == "failed" | |
| assert "sha256" in store.errors["neural_forge_conjunctiva_assessable_gate"] | |
| assert store.status["neural_forge_anaemia"] == "ready" | |
| assert store.health_status()["assessable_gate"] == "failed" | |