Fix to_json() crashing on the test batteries' numpy scalars - #8
Merged
Conversation
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>
# Conflicts: # code/tests/test_univariate.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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:The battery results carry numpy scalars that
json.dumpsrefuses:extended_tests/squared_norm/passesnp.bool_extended_tests/henze_zirkler/passesnp.bool_extended_tests/tail_exceedance/thresholds/*/passesnp.bool_extended_tests/tail_exceedance/thresholds/*/expectednp.float64This 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 (NumpyEncoderinrun_baseline.py,SafeEncoderincalibration.py); the methods never got one.The fix
A recursive
_jsonable()helper, applied inside bothto_dict()methods. Soto_dict()returns plain Python, and bothto_dict()andto_json()are safe — a user callingjson.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_batteryon 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