Skip to content

Commit 4f3ad29

Browse files
authored
BUG: Fix bug with report replacement (#11318)
1 parent 1c1cc3f commit 4f3ad29

File tree

3 files changed

+21
-18
lines changed

3 files changed

+21
-18
lines changed

doc/changes/latest.inc

+1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ Bugs
4444
- Fix bug where EEGLAB channel positions were read as meters, while they are commonly in millimeters, leading to head outlies of the size of one channel when plotting topomaps. Now ``montage_units`` argument has been added to :func:`~mne.io.read_raw_eeglab` and :func:`~mne.read_epochs_eeglab` to control in what units EEGLAB channel positions are read. The default is millimeters, ``'mm'`` (:gh:`11283` by `Mikołaj Magnuski`_)
4545
- Fix bug where computing PSD with welch's method with more jobs than channels would fail (:gh:`11298` by `Mathieu Scheltienne`_)
4646
- Fix channel selection edge-cases in `~mne.preprocessing.ICA.find_bads_muscle` (:gh:`11300` by `Mathieu Scheltienne`_)
47+
- Fix bug with :class:`mne.Report` with ``replace=True`` where the wrong content was replaced (:gh:`11318` by `Eric Larson`_)
4748
- Multitaper spectral estimation now uses periodic (rather than symmetric) taper windows. This also necessitated changing the default ``max_iter`` of our cross-spectral density functions from 150 to 250. (:gh:`11293` by `Daniel McCloy`_)
4849
4950
API changes

mne/report/report.py

+5-8
Original file line numberDiff line numberDiff line change
@@ -765,7 +765,7 @@ def _get_state_params():
765765
'baseline', 'cov_fname', 'include', '_content', 'image_format',
766766
'info_fname', '_dom_id', 'raw_psd', 'projs',
767767
'subjects_dir', 'subject', 'title', 'data_path', 'lang',
768-
'fname'
768+
'fname',
769769
)
770770

771771
def _get_dom_id(self, increment=True):
@@ -1810,14 +1810,11 @@ def _add_or_replace(
18101810

18111811
existing_names = [element.name for element in self._content]
18121812
if name in existing_names and replace:
1813-
# Find and replace existing content, starting from the last element
1814-
for idx, content_element in enumerate(self._content[::-1]):
1815-
if content_element.name == name:
1816-
self._content[idx] = new_content
1817-
return
1818-
raise RuntimeError('This should never happen')
1813+
# Find and replace last existing element with the same name
1814+
idx = [ii for ii, element_name in enumerate(existing_names)
1815+
if element_name == name][-1]
1816+
self._content[idx] = new_content
18191817
else:
1820-
# Simply append new content (no replace)
18211818
self._content.append(new_content)
18221819

18231820
def _add_code(self, *, code, title, language, section, tags, replace):

mne/report/tests/test_report.py

+15-10
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,8 @@
7171

7272
def _get_example_figures():
7373
"""Create two example figures."""
74-
fig1 = plt.plot([1, 2], [1, 2])[0].figure
75-
fig2 = plt.plot([3, 4], [3, 4])[0].figure
74+
fig1 = np.zeros((2, 2, 3))
75+
fig2 = np.ones((2, 2, 3))
7676
return [fig1, fig2]
7777

7878

@@ -609,28 +609,33 @@ def test_remove():
609609
assert r2.html[2] == r.html[3]
610610

611611

612-
def test_add_or_replace():
612+
@pytest.mark.parametrize('tags', (True, False)) # shouldn't matter
613+
def test_add_or_replace(tags):
613614
"""Test replacing existing figures in a report."""
615+
# Note that tags don't matter, only titles do!
614616
r = Report()
615617
fig1, fig2 = _get_example_figures()
616-
r.add_figure(fig=fig1, title='duplicate', tags=('foo',))
617-
r.add_figure(fig=fig1, title='duplicate', tags=('foo',))
618-
r.add_figure(fig=fig1, title='duplicate', tags=('bar',))
619-
r.add_figure(fig=fig2, title='nonduplicate', tags=('foo',))
618+
r.add_figure(fig=fig1, title='duplicate', tags=('foo',) if tags else ())
619+
r.add_figure(fig=fig2, title='duplicate', tags=('foo',) if tags else ())
620+
r.add_figure(fig=fig1, title='duplicate', tags=('bar',) if tags else ())
621+
r.add_figure(fig=fig2, title='nonduplicate', tags=('foo',) if tags else ())
620622
# By default, replace=False, so all figures should be there
621623
assert len(r.html) == 4
624+
assert len(r._content) == 4
622625

623626
old_r = copy.deepcopy(r)
624627

625628
# Replace last occurrence of `fig1` tagges as `foo`
626629
r.add_figure(
627-
fig=fig2, title='duplicate', tags=('foo',), replace=True
630+
fig=fig2, title='duplicate', tags=('bar',) if tags else (),
631+
replace=True,
628632
)
629633
assert len(r._content) == len(r.html) == 4
630-
assert r.html[1] != old_r.html[1] # This figure should have changed
634+
# This figure should have changed
635+
assert r.html[2] != old_r.html[2]
631636
# All other figures should be the same
632637
assert r.html[0] == old_r.html[0]
633-
assert r.html[2] == old_r.html[2]
638+
assert r.html[1] == old_r.html[1]
634639
assert r.html[3] == old_r.html[3]
635640

636641

0 commit comments

Comments
 (0)