Download gmnet/code/journal_exp/tests/test_e1_trained_features.py from YFanwang/Backup: direct link, hf CLI and curl.
- Browser
- Download file 2.08 kB
-
https://huggingface.co/datasets/YFanwang/Backup/resolve/main/gmnet/code/journal_exp/tests/test_e1_trained_features.py
- Command line
-
hf download hf://datasets/YFanwang/Backup/gmnet/code/journal_exp/tests/test_e1_trained_features.py
-
curl -L -o test_e1_trained_features.py https://huggingface.co/datasets/YFanwang/Backup/resolve/main/gmnet/code/journal_exp/tests/test_e1_trained_features.py
2.08 kB
| from __future__ import annotations | |
| import sys | |
| from pathlib import Path | |
| SCRIPTS = Path(__file__).resolve().parents[1] / "scripts" | |
| if str(SCRIPTS) not in sys.path: | |
| sys.path.insert(0, str(SCRIPTS)) | |
| from merge_e1_trained_features import _nonfinite_diagnostics | |
| from run_e1_trained_features import _aggregate | |
| def test_aggregate_ignores_nonfinite_and_records_counts() -> None: | |
| rows = [ | |
| {"gate": "identity", "value": 2.0}, | |
| {"gate": "identity", "value": float("nan")}, | |
| {"gate": "identity", "value": float("inf")}, | |
| {"gate": "identity", "value": None}, | |
| ] | |
| result = _aggregate(rows, ("gate",), ("value",))[0] | |
| assert result["value_mean"] == 2.0 | |
| assert result["value_std"] == 0.0 | |
| assert result["value_finite_count"] == 1 | |
| assert result["value_nonfinite_count"] == 2 | |
| assert result["value_missing_count"] == 1 | |
| def test_aggregate_all_nonfinite_returns_null_not_zero() -> None: | |
| rows = [ | |
| {"layer": "stage3", "value": float("nan")}, | |
| {"layer": "stage3", "value": -float("inf")}, | |
| ] | |
| result = _aggregate(rows, ("layer",), ("value",))[0] | |
| assert result["value_mean"] is None | |
| assert result["value_std"] is None | |
| assert result["value_finite_count"] == 0 | |
| assert result["value_nonfinite_count"] == 2 | |
| def test_nonfinite_diagnostic_serializes_values_as_strings() -> None: | |
| cells, summary = _nonfinite_diagnostics( | |
| { | |
| "feature_metrics": [ | |
| { | |
| "gate": "identity", | |
| "seed": 2, | |
| "cutoff": 0.25, | |
| "layer": "stage3", | |
| "centroid": float("nan"), | |
| "energy": float("inf"), | |
| "finite": 1.0, | |
| } | |
| ] | |
| } | |
| ) | |
| assert {cell["nonfinite_value"] for cell in cells} == {"nan", "inf"} | |
| assert all(not isinstance(cell["nonfinite_value"], float) for cell in cells) | |
| assert len(summary) == 1 | |
| assert summary[0]["nonfinite_field_count"] == 2 | |
| assert summary[0]["nonfinite_metrics"] == "centroid;energy" | |