Skip to content

Conversation

@zhijingz
Copy link

…es #13364)

Reference issue (if any)

Fixes #13364

What does this implement/fix?

psd_array_welch

  • Added an explicit early check for Inf values inside the analyzed span.
  • Replaced an internal assert with a user-facing ValueError when NaN masks are not aligned across channels.
  • Retained existing intended behavior that allows “sentinel” NaNs — aligned across channels — to mark annotation boundaries without error.

ICA.fit

  • Added an early ValueError guard when input data contain any NaN or Inf, producing a clear message instead of downstream runtime warnings.

Additional information

@welcome
Copy link

welcome bot commented Nov 13, 2025

Hello! 👋 Thanks for opening your first pull request here! ❤️ We will try to get back to you soon. 🚴

@zhijingz
Copy link
Author

Hi! First-time contributor here. I’ve rebased on current main, pre-commit passes locally, and targeted tests are green:

pytest -q mne/time_frequency/tests -k psd → 21 passed, 6 skipped
pytest -q mne/preprocessing/tests -k ica → 1 passed

Could someone please approve CI to run on this PR? Thanks!

@@ -0,0 +1,2 @@
Fail early with a clear error when non-finite values (NaN/Inf) are present
in PSD (Welch) and in ICA.fit, avoiding deep assertion failures (GH-13364).
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
in PSD (Welch) and in ICA.fit, avoiding deep assertion failures (GH-13364).
in PSD (Welch) and in ICA.fit, avoiding deep assertion failures, by :newcontrib:`First Last`

then also add to doc/changes/names.inc

@larsoner larsoner added this to the 1.11 milestone Nov 13, 2025
@zhijingz
Copy link
Author

Hi, I revised the contritbution doc - anything else I need to do for the workflow to be approved?

Copy link
Member

@drammock drammock left a comment

Choose a reason for hiding this comment

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

I made a few comments/suggestions about specific bits of the code/tests. Additional high-level comments:

#13364 (comment) suggested that we error for ICA.fit when there are NaNs present, but for PSD we should warn if NaNs are on specific channels and compute the PSD for the other channels that have good data. I see two new ValueErrors and no new warnings in the time_frequency code, so it seems like you're doing something different from what @larsoner recommended.

Comment on lines +1 to +2
Fail early with a clear error when non-finite values (NaN/Inf) are present
in PSD (Welch) and in ICA.fit, avoiding deep assertion failures by :newcontrib: `Emma Zhang`
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
Fail early with a clear error when non-finite values (NaN/Inf) are present
in PSD (Welch) and in ICA.fit, avoiding deep assertion failures by :newcontrib: `Emma Zhang`
Improved error message when non-finite values (NaN/Inf) are detected in calls to
:meth:`inst.compute_psd(method="welch") <mne.io.Raw.compute_psd>` or
:meth:`ICA.fit() <mne.preprocessing.ICA.fit>`, by :newcontrib:`Emma Zhang`.

Comment on lines +1760 to +1761
rng = np.random.RandomState(1)
data = rng.randn(4, 1000)
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
rng = np.random.RandomState(1)
data = rng.randn(4, 1000)
rng = np.random.default_rng(1)
data = rng.standard_normal(size=(4, 1000))

this is the pattern recommended for new code; see https://numpy.org/doc/stable/reference/random/index.html#random-quick-start

raw = RawArray(data.copy(), info)
raw._data[1, 50] = np.inf
ica = ICA(n_components=2, random_state=0, method="fastica", max_iter="auto")
with pytest.raises(ValueError, match=r"Input data contains non[- ]?finite values"):
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
with pytest.raises(ValueError, match=r"Input data contains non[- ]?finite values"):
with pytest.raises(ValueError, match=r"Input data contains non-finite values"):

since you wrote the error message, we know exactly what it says; no need for the [- ]?

Comment on lines +234 to +235
rng = np.random.RandomState(0)
x = rng.randn(1, n_samples)
Copy link
Member

Choose a reason for hiding this comment

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

see prior comment re: default_rng and standard_normal

x = rng.randn(2, n_samples)
# NaN only in ch0; ch1 has no NaN => masks not aligned -> should raise
x[0, 500] = np.nan
with pytest.raises(ValueError, match="aligned|not aligned|non[- ]?finite|NaN|Inf"):
Copy link
Member

Choose a reason for hiding this comment

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

similar comment as above: avoid complicated regex in the match parameter; we wrote the error message so we can pull out a distinctive string of words from it and match on that. That approach makes maintenance easier (you can copy/paste/search on the match string to find the place in the codebase where the error is raised)

Copy link
Member

Choose a reason for hiding this comment

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

the filename needs to match the PR, not the issue. so move this file to 13486.bugfix.rst

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.

BUG: Insufficient checking for isfinite in compute_psd and ica.fit

3 participants