Skip to content

Commit befc418

Browse files
authored
fix "auto" scalings for flat or mostly-zero channels (#13376)
1 parent 637ab4e commit befc418

File tree

3 files changed

+9
-0
lines changed

3 files changed

+9
-0
lines changed

doc/changes/dev/13376.bugfix.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Ensure auto-computed plot scalings are always non-zero, by `Daniel McCloy`_.

mne/viz/tests/test_utils.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,10 @@ def test_auto_scale():
129129
raw = read_raw_fif(raw_fname)
130130
epochs = Epochs(raw, read_events(ev_fname))
131131
rand_data = np.random.randn(10, 100)
132+
# make a stim channel all zeros (gh 13376)
133+
ix = raw.get_channel_types().index("stim")
134+
raw.load_data()
135+
raw._data[ix] = 0.0
132136

133137
for inst in [raw, epochs]:
134138
scale_grad = 1e10
@@ -142,6 +146,8 @@ def test_auto_scale():
142146
scalings_new = _compute_scalings(scalings_def, inst)
143147
assert scale_grad == scalings_new["grad"]
144148
assert scalings_new["eeg"] != "auto"
149+
# make sure an all-zero channel doesn't cause scaling=0 (gh 13376)
150+
assert scalings_new["stim"] > 0
145151

146152
with pytest.raises(ValueError, match="Must supply either Raw or Epochs"):
147153
_compute_scalings(scalings_def, rand_data)

mne/viz/utils.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1423,6 +1423,8 @@ def _compute_scalings(scalings, inst, remove_dc=False, duration=10):
14231423
this_data = this_data[np.isfinite(this_data)]
14241424
if this_data.size:
14251425
iqr = np.diff(np.percentile(this_data, [25, 75]))[0]
1426+
if iqr == 0: # e.g. sparse stim channels, flat channels
1427+
iqr = 1.0
14261428
else:
14271429
iqr = 1.0
14281430
scalings[key] = iqr

0 commit comments

Comments
 (0)