Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ENH: Validate inputs with pydantic #779

Merged
merged 10 commits into from
Aug 15, 2023
Merged

Conversation

larsoner
Copy link
Member

Before merging …

  • Validate inputs with pydantic after adding as a dependency (2.0 seems to be the min useable version for us based on local testing)
  • Fix bugs where defaults for decoding_csp_times and decoding_csp_freqs didn't do what we said they did (they were set once statically based on the defaults rather than being computed dynamically based on the variable values)
  • Changelog has been updated (docs/source/changes.md)

Locally it seems to work -- adding config_validation = "foo" to config_ds003392.py for example produces:

$ pytest mne_bids_pipeline -xk 003392
...
mne_bids_pipeline/_config_import.py:410: in _pydantic_validate
    raise ValueError(str(err)) from None
E   ValueError: 1 validation error for user configuration from /home/larsoner/python/mne-bids-pipeline/mne_bids_pipeline/tests/configs/config_ds003392.py
E   config_validation
E     Input should be 'raise', 'warn' or 'ignore' [type=literal_error, input_value='foo', input_type=str]
E       For further information visit https://errors.pydantic.dev/2.1/v/literal_error

or when setting mf_destination = "foo" in config_ds003392.py which allows a specific Literal or a FloatArrayLike:

$ pytest mne_bids_pipeline -xk 004229
...
mne_bids_pipeline/_config_import.py:410: in _pydantic_validate
    raise ValueError(str(err)) from None
E   ValueError: 2 validation errors for user configuration from /home/larsoner/python/mne-bids-pipeline/mne_bids_pipeline/tests/configs/config_ds004229.py
E   mf_destination.literal['reference_run']
E     Input should be 'reference_run' [type=literal_error, input_value='foo', input_type=str]
E       For further information visit https://errors.pydantic.dev/2.1/v/literal_error
E   mf_destination.function-plain[assert_float_array_like()]
E     Value error, could not convert string to float: 'foo' [type=value_error, input_value='foo', input_type=str]
E       For further information visit https://errors.pydantic.dev/2.1/v/value_error

Because of the strict=True I suspect there will be some Lists that we will want to change to FloatArrayLike (or create a generic CheckedArrayLike that allows arbitrary dtypes or something) but this is maybe a good enough start!

Closes #766

@larsoner
Copy link
Member Author

Already found either a usage error or documentation / type-hinting error:

mne_bids_pipeline/_config_import.py:411: in _pydantic_validate
    raise ValueError(str(err)) from None
E   ValueError: 1 validation error for user configuration from /home/circleci/project/mne_bids_pipeline/tests/configs/config_ERP_CORE.py
E   eeg_template_montage
E     Input should be a valid string [type=string_type, input_value=<DigMontage | 0 extras (h...fiducials, 343 channels>, input_type=DigMontage]
E       For further information visit https://errors.pydantic.dev/2.1/v/string_type

Changing to just be eeg_template_montage = "standard_1005" but maybe someday we want to extend support to arbitrary DigMontage...

@hoechenberger
Copy link
Member

Very nice otherwise!!

Copy link
Member

@agramfort agramfort left a comment

Choose a reason for hiding this comment

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

Nice !

@dengemann
Copy link
Member

dengemann commented Aug 15, 2023 via email

@larsoner larsoner enabled auto-merge (squash) August 15, 2023 17:14
@larsoner larsoner merged commit 7a5d01d into mne-tools:main Aug 15, 2023
47 of 48 checks passed
@larsoner larsoner deleted the validate branch August 15, 2023 18:05
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.

MAINT: Validate inputs
4 participants