Skip to content

FIX: Save numpy scalar and array kwargs as JSON in BaseConcatDataset.save - #1863

Merged
bruAristimunha merged 4 commits into
braindecode:masterfrom
ptlnextdoor:fix/oss-bd-save-json-numpy-kwargs
Dec 1, 2026
Merged

bruAristimunha merged 4 commits into
braindecode:masterfrom
ptlnextdoor:fix/oss-bd-save-json-numpy-kwargs

Conversation

@ptlnextdoor

Copy link
Copy Markdown
Contributor

What was broken. preprocess(..., save_dir=...) and BaseConcatDataset.save() raised TypeError: Object of type int64 is not JSON serializable when a preprocessor or windower kwarg was a numpy scalar or array (for example np.int64 window sizes). The error came after the fif files were written, so the save directory stayed partial and a rerun failed with FileExistsError.

What changed. A small _json_default helper converts numpy scalars and arrays with .tolist(). It is passed to the two json.dump(kwargs, ...) calls in braindecode/datasets/base.py. Other types still raise TypeError. Adds a whats_new entry (the :gh: number is set after the PR opens).

How tested.

  • pytest test/unit_tests/datautil/test_serialization.py -n 2: 43 passed, 1 skipped.
  • Mutation check: with the base.py change reverted, the 4 new tests fail with TypeError: Object of type ndarray/int64 is not JSON serializable.
  • Only the serialization path is touched, so no model gates apply.

AI disclosure: this PR was prepared with AI assistance (Claude, Anthropic).

Copilot AI balanced review requested due to automatic review settings November 30, 2026 23:43

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota.

The helper was inserted between the decorator and the class, so it
registered _json_default itself and BaseConcatDataset disappeared
from the dataset registry (get_dataset_class KeyError, CI shards 1-2
red). Move the decorator back onto the class.
@bruAristimunha

Copy link
Copy Markdown
Collaborator

Maintainer note: moved @register_dataset back onto BaseConcatDataset (3c1c226). The helper had been inserted between the decorator and the class, so the registry gained _json_default and lost BaseConcatDataset - get_dataset_class('BaseConcatDataset') raised KeyError (CI shards 1-2). Registry lookup and test_registry_pattern_works verified locally, plus the numpy kwargs tests (5 passed).

@bruAristimunha
bruAristimunha merged commit 5e18dd1 into braindecode:master Dec 1, 2026
12 checks passed
bruAristimunha added a commit to AxelNoun/braindecode that referenced this pull request Dec 1, 2026
…rrays

Review follow-up (P2/P3): exact-type checks left subclasses of
list/tuple/dict (OrderedDict, custom lists) holding raw NumPy
scalars (JSON then fails or silently drops them), never converted
NumPy dict keys, and dropped 0-d arrays. Widen to isinstance,
normalize keys, and unwrap 0-d arrays with .item() like the dataset
save path (braindecode#1863).
bruAristimunha added a commit that referenced this pull request Dec 1, 2026
* FIX get_config: keep NumPy scalar arguments as Python numbers

build_model_config and the Hugging Face Hub config left out arguments passed as NumPy scalars (np.int64, np.float32), so from_config and from_pretrained rebuilt the default. Convert NumPy scalars, also inside lists, tuples and dicts, to Python numbers before the JSON checks, and in _update_init_kwargs so reset_head(np.int64(n)) keeps the config serializable.

* Add the PR number to the changelog entry

* FIX get_config: traverse container subclasses, NumPy dict keys, 0-d arrays

Review follow-up (P2/P3): exact-type checks left subclasses of
list/tuple/dict (OrderedDict, custom lists) holding raw NumPy
scalars (JSON then fails or silently drops them), never converted
NumPy dict keys, and dropped 0-d arrays. Widen to isinstance,
normalize keys, and unwrap 0-d arrays with .item() like the dataset
save path (#1863).

---------

Co-authored-by: bruAristimunha <[email protected]>
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.

3 participants