Skip to content

Fix to_json() crashing on the test batteries' numpy scalars - #8

Merged
tprest merged 2 commits into
masterfrom
saga-fix-tojson-numpy
Jul 9, 2026
Merged

Fix to_json() crashing on the test batteries' numpy scalars#8
tprest merged 2 commits into
masterfrom
saga-fix-tojson-numpy

Conversation

@tprest

@tprest tprest commented Jul 9, 2026

Copy link
Copy Markdown
Collaborator

Found by exercising the multivariate API the way a user would (follow-up to #7, same method).

The bug

to_json() raises on both classes as soon as the extended battery has been run:

mv = MultivariateSamples(sigma, data)
mv.run_multivariate_battery()
mv.to_json()   # TypeError: Object of type bool is not JSON serializable

The battery results carry numpy scalars that json.dumps refuses:

Key Type
extended_tests/squared_norm/passes np.bool_
extended_tests/henze_zirkler/passes np.bool_
extended_tests/tail_exceedance/thresholds/*/passes np.bool_
extended_tests/tail_exceedance/thresholds/*/expected np.float64

This makes the advertised programmatic API — to_dict()/to_json() for batch analysis — unusable in exactly the case it exists for. Note the scripts already work around it with their own local encoders (NumpyEncoder in run_baseline.py, SafeEncoder in calibration.py); the methods never got one.

The fix

A recursive _jsonable() helper, applied inside both to_dict() methods. So to_dict() returns plain Python, and both to_dict() and to_json() are safe — a user calling json.dumps(obj.to_dict()) themselves now works too.

Rest of the multivariate API

Audited the same way; everything else is healthy: cross_key_homogeneity, two_sample_test, mardia(), show_covariance(), __repr__, and the continuous path (is_integer=False, nb_gaussian_coord=None) all behave.

Verification

Added test_to_json_after_battery on both classes. They fail on all seven good vectors without this fix, pass with it. Suite: 141 passed, 1 skipped.

Independent of #7 (different regions of saga.py); the two merge cleanly in either order.

🤖 Generated with Claude Code

tprest and others added 2 commits July 9, 2026 23:55
to_json() raised `TypeError: Object of type bool is not JSON serializable` on
both UnivariateSamples and MultivariateSamples as soon as the extended battery
had been run: the battery results carry numpy scalars (np.bool_ for `passes`,
np.float64 for statistics like tail_exceedance's `expected`), which json.dumps
refuses.

This made the advertised programmatic API -- to_dict()/to_json() for batch
analysis -- unusable in exactly the case it exists for. The scripts already
worked around it with their own local encoders (NumpyEncoder in run_baseline.py,
SafeEncoder in calibration.py); the methods never got one.

Add a recursive _jsonable() helper and apply it in both to_dict() methods, so
to_dict() returns plain Python and both to_dict() and to_json() are safe (a user
calling json.dumps(obj.to_dict()) themselves now works too).

Added test_to_json_after_battery on both classes; they fail on all seven good
vectors without this fix.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@tprest
tprest merged commit 5dfeb84 into master Jul 9, 2026
4 checks passed
@tprest
tprest deleted the saga-fix-tojson-numpy branch July 9, 2026 22:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant