Download gmnet/code/journal_exp/tests/test_e3_evaluation.py from YFanwang/Backup: direct link, hf CLI and curl.
- Browser
- Download file 2.12 kB
-
https://huggingface.co/datasets/YFanwang/Backup/resolve/main/gmnet/code/journal_exp/tests/test_e3_evaluation.py
- Command line
-
hf download hf://datasets/YFanwang/Backup/gmnet/code/journal_exp/tests/test_e3_evaluation.py
-
curl -L -o test_e3_evaluation.py https://huggingface.co/datasets/YFanwang/Backup/resolve/main/gmnet/code/journal_exp/tests/test_e3_evaluation.py
2.12 kB
| from __future__ import annotations | |
| import numpy as np | |
| import torch | |
| from gmnet.evaluation.corruptions import apply_corruption | |
| from gmnet.evaluation.metrics import ( | |
| classification_metrics, | |
| expected_calibration_error, | |
| paired_hierarchical_bootstrap, | |
| ) | |
| def test_noise_is_deterministic_per_sample() -> None: | |
| image = torch.full((3, 32, 32), 0.5) | |
| first = apply_corruption(image, "gaussian_noise", 17) | |
| second = apply_corruption(image, "gaussian_noise", 17) | |
| different = apply_corruption(image, "gaussian_noise", 18) | |
| assert torch.equal(first, second) | |
| assert not torch.equal(first, different) | |
| def test_shift_has_fixed_zero_filled_geometry() -> None: | |
| image = torch.zeros(3, 32, 32) | |
| image[:, 1, 1] = 1.0 | |
| shifted = apply_corruption(image, "shift_2px", 0) | |
| assert torch.equal(shifted[:, 3, 3], torch.ones(3)) | |
| assert shifted.sum().item() == 3.0 | |
| def test_classification_metrics_and_ece() -> None: | |
| metrics = classification_metrics( | |
| np.array([1, 0, 1, 0]), | |
| np.array([1, 1, 1, 0]), | |
| np.array([0.1, 1.0, 0.2, 2.0]), | |
| np.array([0.9, 0.8, 0.7, 0.2]), | |
| ece_bins=2, | |
| ) | |
| assert metrics["top1"] == 50.0 | |
| assert metrics["top5"] == 75.0 | |
| assert metrics["nll"] == 0.825 | |
| assert expected_calibration_error(np.array([1.0]), np.array([1])) == 0.0 | |
| def test_paired_bootstrap_preserves_exact_constant_difference() -> None: | |
| reference = np.zeros((3, 6, 20), dtype=np.float64) | |
| candidate = np.ones_like(reference) | |
| rows = paired_hierarchical_bootstrap( | |
| candidate, | |
| reference, | |
| statistics={ | |
| "clean_top1": "clean_top1", | |
| "mean": "mean_corruption_top1", | |
| "raw": "clean_raw_mean", | |
| }, | |
| samples=100, | |
| seed=3, | |
| ) | |
| scaled = [row for row in rows if row["metric"] != "raw"] | |
| raw = next(row for row in rows if row["metric"] == "raw") | |
| assert all(row["difference"] == 100.0 for row in scaled) | |
| assert all(row["ci_low"] == 100.0 and row["ci_high"] == 100.0 for row in scaled) | |
| assert raw["difference"] == raw["ci_low"] == raw["ci_high"] == 1.0 | |