Download tests/test_metrics_stats.py from velvet-pine-22/PULSE-code: direct link, hf CLI and curl.
- Browser
- Download file 3.1 kB
-
https://huggingface.co/velvet-pine-22/PULSE-code/resolve/main/tests/test_metrics_stats.py
- Command line
-
hf download hf://velvet-pine-22/PULSE-code/tests/test_metrics_stats.py
-
curl -L -o test_metrics_stats.py https://huggingface.co/velvet-pine-22/PULSE-code/resolve/main/tests/test_metrics_stats.py
3.1 kB
| import numpy as np | |
| import pytest | |
| from scipy import stats as st | |
| from sklearn.metrics import accuracy_score, f1_score | |
| from pulse import metrics as M | |
| from pulse.stats import compare_rows, fold_means, mean_sd, paired_t | |
| def test_confusion_metrics_match_sklearn(): | |
| rng = np.random.default_rng(0) | |
| y = rng.integers(0, 6, 300) | |
| p = np.where(rng.random(300) < 0.5, y, rng.integers(0, 8, 300)) | |
| cm = M.confusion_stat(y, p, 8) | |
| assert M.macro_f1(cm) == pytest.approx(f1_score(y, p, average="macro")) | |
| assert M.accuracy(cm) == pytest.approx(accuracy_score(y, p)) | |
| def test_per_recording_reduction_and_subsets(): | |
| rng = np.random.default_rng(1) | |
| per = {} | |
| ys, ps = {}, {} | |
| for r in ("v1s1", "v1s2", "v2s1"): | |
| ys[r] = rng.integers(0, 3, 50) | |
| ps[r] = rng.integers(0, 3, 50) | |
| per[r] = {"confusion": M.confusion_stat(ys[r], ps[r], 3)} | |
| d = {"macro_f1": {"fn": "macro_f1", "stat": "confusion"}} | |
| sub = ["v1s1", "v2s1"] | |
| got = M.compute_metric(d["macro_f1"], per, sub) | |
| y = np.concatenate([ys[r] for r in sub]) | |
| p = np.concatenate([ps[r] for r in sub]) | |
| assert got == pytest.approx(f1_score(y, p, average="macro")) | |
| def test_pearson_and_mean_stats(): | |
| rng = np.random.default_rng(2) | |
| a, b = rng.normal(size=(200, 3)), rng.normal(size=(200, 3)) | |
| b[:, 0] += a[:, 0] | |
| s1, s2 = M.pearson_stat(a[:120], b[:120]), M.pearson_stat(a[120:], b[120:]) | |
| tot = M._sum([s1, s2]) | |
| r = [np.corrcoef(a[:, j], b[:, j])[0, 1] for j in range(3)] | |
| assert M.pearson_max(tot) == pytest.approx(max(r)) | |
| assert M.pearson_mean(tot) == pytest.approx(np.mean(r)) | |
| one = M._sum([M.pearson_stat(a[:120, 0], b[:120, 0]), M.pearson_stat(a[120:, 0], b[120:, 0])]) | |
| assert M.pearson(one) == pytest.approx(r[0]) | |
| sc = M._sum([M.sum_count_stat([1.0, 2.0]), M.sum_count_stat([3.0])]) | |
| assert M.mean_value(sc) == pytest.approx(2.0) | |
| def test_topk_and_scores(): | |
| scores = np.array([[0.1, 0.5, 0.4], [0.8, 0.1, 0.1], [0.2, 0.3, 0.5]]) | |
| s = M.topk_stat([2, 0, 1], scores, ks=(1, 2)) | |
| assert s == {"n": 3, "hits@1": 1, "hits@2": 3} | |
| sc = M._concat([M.scores_stat([0, 1], [0.1, 0.9]), M.scores_stat([1, 0], [0.8, 0.3])]) | |
| assert M.roc_auc(sc) == 1.0 and M.positive_fraction(sc) == 0.5 | |
| def test_mean_sd_ddof1_and_fold_means(): | |
| m, s = mean_sd([1.0, 2.0, 3.0, 4.0, 5.0]) | |
| assert m == 3.0 and s == pytest.approx(np.std([1, 2, 3, 4, 5], ddof=1)) | |
| fm = fold_means({(0, 42): 1.0, (0, 43): 3.0, (1, 42): 5.0}) | |
| assert fm == {0: 2.0, 1: 5.0} | |
| def test_paired_tests_match_scipy(): | |
| rng = np.random.default_rng(3) | |
| a, b = rng.normal(size=5), rng.normal(size=5) | |
| r = paired_t(a, b) | |
| ref = st.ttest_rel(a, b) | |
| assert r["t"] == pytest.approx(ref.statistic) and r["p"] == pytest.approx(ref.pvalue) | |
| row = {(k, s): 0.6 + 0.01 * k + 0.001 * s for k in range(5) for s in (42, 43, 44)} | |
| base = {(k, s): 0.5 + 0.02 * ((k * 7 + s) % 3) for k in range(5) for s in (42, 43, 44)} | |
| c = compare_rows(row, base) | |
| assert c["positive_folds"] == 5 and c["n_pairs"] == 15 and c["t15"]["n"] == 15 | |
| assert c["t5"]["n"] == 5 | |