From 42570c9664804fccb784e18774c4505e6ccad711 Mon Sep 17 00:00:00 2001 From: Clemens Brunner Date: Tue, 30 May 2023 07:48:40 +0200 Subject: [PATCH 1/6] Add "array-like" to `_validate_type()` --- mne/baseline.py | 30 +++++++++++++++--------------- mne/utils/check.py | 7 ++++--- 2 files changed, 19 insertions(+), 18 deletions(-) diff --git a/mne/baseline.py b/mne/baseline.py index 21aebdde807..0842095dabe 100644 --- a/mne/baseline.py +++ b/mne/baseline.py @@ -6,7 +6,7 @@ import numpy as np -from .utils import logger, verbose, _check_option +from .utils import logger, verbose, _check_option, _validate_type def _log_rescale(baseline, mode="mean"): @@ -143,14 +143,14 @@ def fun(d, m): def _check_baseline(baseline, times, sfreq, on_baseline_outside_data="raise"): - """Check if the baseline is valid, and adjust it if requested. + """Check if the baseline is valid and adjust it if requested. - ``None`` values inside the baseline parameter will be replaced with - ``times[0]`` and ``times[-1]``. + ``None`` values inside ``baseline`` will be replaced with ``times[0]`` and + ``times[-1]``. Parameters ---------- - baseline : tuple | None + baseline : array-like | None Beginning and end of the baseline period, in seconds. If ``None``, assume no baseline and return immediately. times : array @@ -158,27 +158,27 @@ def _check_baseline(baseline, times, sfreq, on_baseline_outside_data="raise"): sfreq : float The sampling rate. on_baseline_outside_data : 'raise' | 'info' | 'adjust' - What do do if the baseline period exceeds the data. + What to do if the baseline period exceeds the data. If ``'raise'``, raise an exception (default). If ``'info'``, log an info message. - If ``'adjust'``, adjust the baseline such that it's within the data - range again. + If ``'adjust'``, adjust the baseline such that it is within the data range. Returns ------- (baseline_tmin, baseline_tmax) | None - The baseline with ``None`` values replaced with times, and with - adjusted times if ``on_baseline_outside_data='adjust'``; or ``None`` - if the ``baseline`` parameter is ``None``. - + The baseline with ``None`` values replaced with times, and with adjusted times + if ``on_baseline_outside_data='adjust'``; or ``None``, if ``baseline`` is + ``None``. """ if baseline is None: return None - if not isinstance(baseline, tuple) or len(baseline) != 2: + _validate_type(baseline, "array-like") + baseline = tuple(baseline) + + if len(baseline) != 2: raise ValueError( - f"`baseline={baseline}` is an invalid argument, must " - f"be a tuple of length 2 or None" + f"baseline must have exactly two elements (got {len(baseline)})." ) tmin, tmax = times[0], times[-1] diff --git a/mne/utils/check.py b/mne/utils/check.py index db42a21f98b..9ee3e317470 100644 --- a/mne/utils/check.py +++ b/mne/utils/check.py @@ -4,6 +4,7 @@ # License: BSD-3-Clause from builtins import input # no-op here but facilitates testing +from collections.abc import Sequence from difflib import get_close_matches from importlib import import_module import operator @@ -525,6 +526,7 @@ def __instancecheck__(cls, other): "path-like": path_like, "int-like": (int_like,), "callable": (_Callable(),), + "array-like": (Sequence, np.ndarray), } @@ -538,9 +540,8 @@ def _validate_type(item, types=None, item_name=None, type_name=None, *, extra="" types : type | str | tuple of types | tuple of str The types to be checked against. If str, must be one of {'int', 'int-like', 'str', 'numeric', 'info', - 'path-like', 'callable'}. - If a tuple of str is passed, use 'int-like' and not 'int' for - integers. + 'path-like', 'callable', 'array-like'}. + If a tuple of str is passed, use 'int-like' and not 'int' for integers. item_name : str | None Name of the item to show inside the error message. type_name : str | None From 7855606a377cfe50a4afd26983388a004a88a813 Mon Sep 17 00:00:00 2001 From: Clemens Brunner Date: Tue, 30 May 2023 07:52:25 +0200 Subject: [PATCH 2/6] Fix style --- mne/baseline.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mne/baseline.py b/mne/baseline.py index 0842095dabe..dde30f52d3f 100644 --- a/mne/baseline.py +++ b/mne/baseline.py @@ -175,7 +175,7 @@ def _check_baseline(baseline, times, sfreq, on_baseline_outside_data="raise"): _validate_type(baseline, "array-like") baseline = tuple(baseline) - + if len(baseline) != 2: raise ValueError( f"baseline must have exactly two elements (got {len(baseline)})." From 81e41505881484feae114039a04cfe9fd47e60de Mon Sep 17 00:00:00 2001 From: Clemens Brunner Date: Tue, 30 May 2023 08:19:00 +0200 Subject: [PATCH 3/6] Update check in test --- mne/tests/test_epochs.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mne/tests/test_epochs.py b/mne/tests/test_epochs.py index 996cd3d2468..4c248e2d2dd 100644 --- a/mne/tests/test_epochs.py +++ b/mne/tests/test_epochs.py @@ -1349,7 +1349,7 @@ def test_epochs_io_preload(tmp_path, preload): epochs_no_bl.save(temp_fname_no_bl, overwrite=True) epochs_read = read_epochs(temp_fname) epochs_no_bl_read = read_epochs(temp_fname_no_bl) - with pytest.raises(ValueError, match="invalid"): + with pytest.raises(ValueError, match="exactly two elements"): epochs.apply_baseline(baseline=[1, 2, 3]) epochs_with_bl = epochs_no_bl_read.copy().apply_baseline(baseline) assert isinstance(epochs_with_bl, BaseEpochs) From 29172d3ebdc6a74f54bcbf1b4a97d1f02777c771 Mon Sep 17 00:00:00 2001 From: Clemens Brunner Date: Tue, 30 May 2023 11:38:41 +0200 Subject: [PATCH 4/6] Add shape --- mne/baseline.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mne/baseline.py b/mne/baseline.py index dde30f52d3f..e4c43317ee0 100644 --- a/mne/baseline.py +++ b/mne/baseline.py @@ -150,7 +150,7 @@ def _check_baseline(baseline, times, sfreq, on_baseline_outside_data="raise"): Parameters ---------- - baseline : array-like | None + baseline : array-like, shape (2,) | None Beginning and end of the baseline period, in seconds. If ``None``, assume no baseline and return immediately. times : array From c51e392b5c6c491c4bb4bb58a2cb77bafd3311a7 Mon Sep 17 00:00:00 2001 From: Clemens Brunner Date: Tue, 30 May 2023 11:40:04 +0200 Subject: [PATCH 5/6] Add changelog entry --- doc/changes/latest.inc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/changes/latest.inc b/doc/changes/latest.inc index 931ec76c7cc..53865d148a2 100644 --- a/doc/changes/latest.inc +++ b/doc/changes/latest.inc @@ -34,4 +34,4 @@ Bugs API changes ~~~~~~~~~~~ -- None yet +- :func:`mne.utils._validate_type` can now check if a given object is `"array-like"` (:gh:`11713` by `Clemens Brunner`_) From 63023654bdc9866aa98d1591ae0cb118cea403fc Mon Sep 17 00:00:00 2001 From: Clemens Brunner Date: Tue, 30 May 2023 11:54:53 +0200 Subject: [PATCH 6/6] Only list public changes in entry --- doc/changes/latest.inc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/changes/latest.inc b/doc/changes/latest.inc index 53865d148a2..73287e2688e 100644 --- a/doc/changes/latest.inc +++ b/doc/changes/latest.inc @@ -34,4 +34,4 @@ Bugs API changes ~~~~~~~~~~~ -- :func:`mne.utils._validate_type` can now check if a given object is `"array-like"` (:gh:`11713` by `Clemens Brunner`_) +- The ``baseline`` argument can now be array-like (e.g. ``list``, ``tuple``, ``np.ndarray``, ...) instead of only a ``tuple`` (:gh:`11713` by `Clemens Brunner`_)