Download src/coherence/stats.py from pratik-250620/MultiModal-Coherence-AI: direct link, hf CLI and curl.
- Browser
- Download file 1.31 kB
-
https://huggingface.co/spaces/pratik-250620/MultiModal-Coherence-AI/resolve/main/src/coherence/stats.py
- Command line
-
hf download hf://spaces/pratik-250620/MultiModal-Coherence-AI/src/coherence/stats.py
-
curl -L -o stats.py https://huggingface.co/spaces/pratik-250620/MultiModal-Coherence-AI/resolve/main/src/coherence/stats.py
1.31 kB
| from __future__ import annotations | |
| import json | |
| from pathlib import Path | |
| from typing import Dict, List | |
| import numpy as np | |
| METRICS = ["st_i", "st_a", "si_a", "msci"] | |
| class CoherenceStats: | |
| """ | |
| Learns statistical thresholds from previous run logs. | |
| """ | |
| def __init__(self) -> None: | |
| self.stats: Dict[str, Dict[str, float]] = {} | |
| def fit(self, runs: List[Dict]) -> None: | |
| """ | |
| Learn mean, std, and percentiles from past runs. | |
| """ | |
| for metric in METRICS: | |
| values = [ | |
| r["scores"][metric] | |
| for r in runs | |
| if metric in r.get("scores", {}) | |
| ] | |
| if not values: | |
| continue | |
| arr = np.array(values) | |
| self.stats[metric] = { | |
| "mean": float(np.mean(arr)), | |
| "std": float(np.std(arr)), | |
| "p25": float(np.percentile(arr, 25)), | |
| "p50": float(np.percentile(arr, 50)), | |
| "p75": float(np.percentile(arr, 75)), | |
| } | |
| def save(self, path: Path) -> None: | |
| path.write_text(json.dumps(self.stats, indent=2)) | |
| def load(cls, path: Path) -> "CoherenceStats": | |
| obj = cls() | |
| obj.stats = json.loads(path.read_text()) | |
| return obj | |