Skip to content

Commit deb88ea

Browse files
committed
Fix remove()
1 parent 740e728 commit deb88ea

File tree

1 file changed

+69
-43
lines changed

1 file changed

+69
-43
lines changed

mne/report/report.py

Lines changed: 69 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1139,61 +1139,87 @@ def add_ssp_projs(self, *, info, projs=None, title, tags=('ssp',),
11391139
replace=replace
11401140
)
11411141

1142-
def remove(self, caption, section=None):
1143-
"""Remove a figure from the report.
1142+
def remove(self, title, tags=None, all=False, caption=None, section=None):
1143+
"""Remove elements from the report.
11441144
1145-
The figure to remove is searched for by its caption. When searching by
1146-
caption, the section label can be specified as well to narrow down the
1147-
search. If multiple figures match the search criteria, the last one
1148-
will be removed.
1149-
1150-
Any empty sections will be removed as well.
1145+
The element to remove is searched for by its title. Optionally, tags
1146+
may be specified as well to narrow down the search to elements that
1147+
have the supplied tags.
11511148
11521149
Parameters
11531150
----------
1151+
title : str
1152+
The title of the element(s) to remove.
1153+
1154+
.. versionadded:: 0.24.0
1155+
tags : collection of str | None
1156+
If supplied, restrict the operation to elements with the supplied
1157+
tags.
1158+
1159+
.. versionadded:: 0.24.0
1160+
all : bool
1161+
Controls the behavior if multiple elements match the search
1162+
criteria. If ``False`` (default) only the element last added to the
1163+
report will be removed. If ``True``, all matches will be removed.
1164+
1165+
.. versionadded:: 0.24.0
11541166
caption : str
1155-
If set, search for the figure by caption.
1167+
The caption of the element to remove.
1168+
1169+
.. deprecated:: 0.24.0
1170+
Use ``title`` instead.
11561171
section : str | None
11571172
If set, limit the search to the section with the given label.
11581173
1174+
.. deprecated:: 0.24.0
1175+
Use ``tags`` instead.
1176+
11591177
Returns
11601178
-------
1161-
removed_index : int | None
1162-
The integer index of the figure that was removed, or ``None`` if no
1163-
figure matched the search criteria.
1179+
removed_index : int | tuple of int | None
1180+
The indices of the elements that were removed, or ``None`` if no
1181+
element matched the search criteria. A tuple will always be
1182+
returned if ``all`` was set to ``True`` and at least one element
1183+
was removed.
1184+
1185+
.. versionchanged:: 0.24.0
1186+
Returns tuple if ``all`` is ``True``.
11641187
"""
1165-
# Construct the search pattern
1166-
pattern = r'^%s-#-.*-#-custom$' % caption
1167-
1168-
# Search for figures matching the search pattern, regardless of
1169-
# section
1170-
matches = [i for i, fname_ in enumerate(self.fnames)
1171-
if re.match(pattern, fname_)]
1188+
if caption is not None:
1189+
warn(
1190+
message='The "caption" parameter has been deprecated. Please '
1191+
'use "title" instead',
1192+
category=DeprecationWarning
1193+
)
1194+
title = caption
11721195
if section is not None:
1173-
# Narrow down the search to the given section
1174-
svar = self._sectionvars[section]
1175-
matches = [i for i in matches
1176-
if self._sectionlabels[i] == svar]
1177-
if len(matches) == 0:
1178-
return None
1179-
1180-
# Remove last occurrence
1181-
index = max(matches)
1182-
1183-
# Remove the figure
1184-
del self.fnames[index]
1185-
del self._sectionlabels[index]
1186-
del self.html[index]
1187-
1188-
# Remove any (now) empty sections.
1189-
# We use a list() to copy the _sectionvars dictionary, since we are
1190-
# removing elements during the loop.
1191-
for section_, sectionlabel_ in list(self._sectionvars.items()):
1192-
if sectionlabel_ not in self._sectionlabels:
1193-
self.tags.remove(section_)
1194-
del self._sectionvars[section_]
1195-
1196-
return index
1196+
warn(
1197+
message='The "section" parameter has been deprecated. Please '
1198+
'use "tags" instead',
1199+
category=DeprecationWarning
1200+
)
1201+
tags = _clean_tags(section)
1202+
1203+
remove_idx = []
1204+
for idx, element in enumerate(self._content):
1205+
if element['toc_entry']['name'] == title:
1206+
if (tags is not None and
1207+
not all(t in element['toc_entry']['tags']
1208+
for t in tags)):
1209+
continue
1210+
remove_idx.append(idx)
1211+
1212+
if not remove_idx:
1213+
remove_idx = None
1214+
elif not all: # only remove last occurrence
1215+
remove_idx = remove_idx[-1]
1216+
del self._content[remove_idx]
1217+
else: # remove all occurrences
1218+
remove_idx = tuple(remove_idx)
1219+
self._content = [e for idx, e in enumerate(self._content)
1220+
if idx not in remove_idx]
1221+
1222+
return remove_idx
11971223

11981224
def _add_or_replace(self, *, toc_entry_name, dom_id, tags, html,
11991225
replace=False):

0 commit comments

Comments
 (0)