Repository navigation
FIX: write undefined scorer statistics as null instead of bare NaN - #2876
Richard Lundeen (richlundeen) merged 2 commits into
Conversation
ScorerEvaluator reports an undefined t-test as NaN, and both serialization paths (ScorerMetrics.to_json and the metrics registry) handed that straight to json.dumps, which writes the bare NaN token. Python reads it back, so the round trip looks fine here, but the checked-in *_metrics.jsonl registries are no longer valid JSON for strict parsers (JSON.parse, .NET, Go). Serialize non-finite floats as null at both boundaries, with allow_nan=False so a future NaN fails loudly instead of corrupting a registry, and let t_statistic/p_value load back as None (the key stays present on purpose).
|
Hi — #2876 has been open since 26 September with CI green and no reply yet, so this is the one nudge I planned. The short version: Two things I would rather ask than assume. First, Happy to close it if you would rather make this change yourself. |
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Fixes #2875
Description
_compute_metricsreports an undefined t-test asNaNon purpose (scorer_evaluator.py:711, pinned bytest_compute_harm_metrics_partial_agreement), butScorerMetrics.to_jsonand both registry writers calljson.dumpswith its defaultallow_nan=True, so the row goes out with bareNaNtokens. RFC 8259 has no such literal: Python reads it back and everything looks fine here, whileJSON.parse, .NET and Go reject the whole*_metrics.jsonldocument — and a reader that does get through evaluatesp_value < 0.05toFalse, so "undefined" is misread as "not significant".Serialization now maps every non-finite float to
null(non_finite_to_none(), applied atto_jsonand_metrics_to_registry_dict) and passesallow_nan=Falseat the two registry write points, so a value that escapes the mapping fails loudly instead of emitting a document other parsers reject.t_statisticandp_valueare typedfloat | None, and theHarmScorerMetricsdocstring says plainly thatNaNis the in-memory signal andNoneis what survives a round trip. Nothing about the computation changes.Stated rather than hidden:
doc/code/scoring/4_scorer_metrics.py:184formatsp_valuewith:.4f, which raisesTypeErroronNone; today it printsnan. I left that file alone because #2860 is editing the same notebook pair — a two-line guard in either place fixes it, and I am happy to add it here if you want it in this PR.Tests and Documentation
Four new tests, all red on base
7b533109and green here:test_harm_metrics_to_json_writes_null_for_undefined_statistics,test_to_json_flattens_numpy_scalars_and_drops_infinite_ones(tests/unit/score/test_scorer_metrics.py) — the first fails on base with"t_statistic": NaNpresent in the output.test_registry_entry_with_undefined_statistics_is_valid_json[add]and[replace](tests/unit/score/test_scorer_metrics_io.py) — assert the written line contains noNaNtoken, parses underjson.loads(..., parse_constant=...)with a constant handler that raises, and still loads back as a metrics row witht_statistic is None.Commands run locally, offline, no API keys:
PYTHONPATH=. python -m pytest -p no:opik tests/unit/score/test_scorer_metrics.py tests/unit/score/test_scorer_metrics_io.py -q→ base (source files reverted, new tests kept): 4 failed, 67 passed; this branch: 71 passed.PYTHONPATH=. python -m pytest -p no:opik tests/unit/score -q→ 2796 passed.ruff@0.16.8 checkonpyrit/score/scorer_evaluation/and both test files, andformat --checkon the four changed files: clean.tytype checking (its pre-commit hook needs the project's--extra allvenv, a torch-sized install that does not complete here) and JupyText (no notebook or documentation file changed). Coverage of the changed lines is 100% bycoverage report -mterm-missing.