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

minimum/maximum value of the all-positive/negative data #12383

Merged
merged 12 commits into from
Jan 24, 2024
Merged
8 changes: 6 additions & 2 deletions mne/evoked.py
Original file line number Diff line number Diff line change
Expand Up @@ -1965,10 +1965,12 @@ def _get_peak(data, times, tmin=None, tmax=None, mode="abs"):
The minimum point in time to be considered for peak getting.
tmax : float | None
The maximum point in time to be considered for peak getting.
mode : {'pos', 'neg', 'abs'}
mode : {'pos', 'neg', 'abs', 'min', 'max'}
Copy link
Member

Choose a reason for hiding this comment

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

confused; this doesn't match the API in the PR description (as was discussed in #12381)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sorry about that! I didn't push my changes yet.

How to deal with the sign of the data. If 'pos' only positive
values will be considered. If 'neg' only negative values will
be considered. If 'abs' absolute values will be considered.
If 'min' the smallest value will be returned. If 'max'
the largest value will be returned.
Defaults to 'abs'.

Returns
Expand All @@ -1980,7 +1982,7 @@ def _get_peak(data, times, tmin=None, tmax=None, mode="abs"):
max_amp : float
Amplitude of the maximum response.
"""
_check_option("mode", mode, ["abs", "neg", "pos"])
_check_option("mode", mode, ["abs", "neg", "pos", "min", "max"])

if tmin is None:
tmin = times[0]
Expand Down Expand Up @@ -2018,6 +2020,8 @@ def _get_peak(data, times, tmin=None, tmax=None, mode="abs"):
"No negative values encountered. Cannot " "operate in neg mode."
)
maxfun = np.argmin
if mode == "min":
maxfun = np.argmin

masked_index = np.ma.array(np.abs(data) if mode == "abs" else data, mask=mask)

Expand Down
18 changes: 18 additions & 0 deletions mne/tests/test_evoked.py
Original file line number Diff line number Diff line change
Expand Up @@ -589,6 +589,24 @@ def test_get_peak():
with pytest.raises(ValueError, match="No positive values"):
evoked_all_neg.get_peak(mode="pos")

# Test finding minimum and maximum values
evoked_all_neg_outlier = evoked_all_neg.copy()
evoked_all_pos_outlier = evoked_all_pos.copy()

# Add an outlier to the data
evoked_all_neg_outlier.data[0, 15] = -1e-20
evoked_all_pos_outlier.data[0, 15] = 1e-20

ch_name, time_idx, max_amp = evoked_all_neg_outlier.get_peak(
mode="max", return_amplitude=True
)
assert_equal(max_amp, -1e-20)
withmywoessner marked this conversation as resolved.
Show resolved Hide resolved

ch_name, time_idx, min_amp = evoked_all_pos_outlier.get_peak(
mode="min", return_amplitude=True
)
assert_equal(min_amp, 1e-20)
withmywoessner marked this conversation as resolved.
Show resolved Hide resolved

# Test interaction between `mode` and `tmin` / `tmax`
# For the test, create an Evoked where half of the values are negative
# and the rest is positive
Expand Down
Loading