Skip to content

Commit 6d43b6a

Browse files
committed
Change all artist removals to use remove_artist()
It's not safe to remove artists the old way, because they may raise an exception if the figure they were in was cleared. Instead, we have to do it this way and just ignore any errors that occur. It would be nice if we figured out a cleaner way at some point. Signed-off-by: Patrick Avery <[email protected]>
1 parent d24473d commit 6d43b6a

16 files changed

+69
-52
lines changed

hexrdgui/blit_manager.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
from matplotlib.artist import Artist
44

5+
from hexrdgui.utils.matplotlib import remove_artist
6+
57

68
class BlitManager:
79
def __init__(self, canvas):
@@ -63,7 +65,7 @@ def remove_artists(self, *path):
6365
d = d[key]
6466

6567
for artist in _recursive_yield_artists(d):
66-
artist.remove()
68+
remove_artist(artist)
6769

6870
if parent:
6971
del parent[key]

hexrdgui/calibration/calibration_dialog_callbacks.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
from hexrdgui.hexrd_config import HexrdConfig
1919
from hexrdgui.utils import instr_to_internal_dict
2020
from hexrdgui.utils.abc_qobject import ABCQObject
21+
from hexrdgui.utils.matplotlib import remove_artist
2122

2223

2324
# Number of decimal places to round calibration parameters
@@ -312,7 +313,7 @@ def on_undo_run_clicked(self):
312313

313314
def clear_drawn_picks(self):
314315
while self.draw_picks_lines:
315-
self.draw_picks_lines.pop(0).remove()
316+
remove_artist(self.draw_picks_lines.pop(0))
316317

317318
self.canvas.draw_idle()
318319

hexrdgui/calibration/hedm/calibration_results_dialog.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
from hexrdgui.navigation_toolbar import NavigationToolbar
99
from hexrdgui.ui_loader import UiLoader
10+
from hexrdgui.utils.matplotlib import remove_artist
1011

1112

1213
DEFAULT_STYLE = 'rx'
@@ -134,7 +135,7 @@ def grain_ids_to_plot(self):
134135

135136
def clear_artists(self):
136137
while self.artists:
137-
self.artists.pop(0).remove()
138+
remove_artist(self.artists.pop(0))
138139

139140
def update_canvas(self):
140141
self.clear_artists()

hexrdgui/hand_picked_fibers_widget.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from hexrdgui.fiber_pick_utils import _angles_from_orientation, _pick_to_fiber
1010
from hexrdgui.ui_loader import UiLoader
1111
from hexrdgui.utils import block_signals
12+
from hexrdgui.utils.matplotlib import remove_artist
1213

1314

1415
class HandPickedFibersWidget(QObject):
@@ -177,7 +178,7 @@ def general_spots(self, fibers):
177178

178179
def clear_current_plot(self):
179180
if hasattr(self, '_current_lines'):
180-
self._current_lines.remove()
181+
remove_artist(self._current_lines)
181182
del self._current_lines
182183

183184
def update_current_plot(self):
@@ -269,7 +270,7 @@ def picked(self, v):
269270
def clear_selected_artists(self):
270271
lines = getattr(self, '_selected_artists', [])
271272
while lines:
272-
lines.pop(0).remove()
273+
remove_artist(lines.pop(0))
273274

274275
@property
275276
def selected_rows(self):

hexrdgui/image_canvas.py

Lines changed: 13 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
from PySide6.QtCore import QThreadPool, QTimer, Signal, Qt
77
from PySide6.QtWidgets import QFileDialog, QMessageBox, QProgressDialog
88

9-
from matplotlib.artist import Artist
109
from matplotlib.axes import Axes
1110
from matplotlib.backends.backend_qtagg import FigureCanvas
1211
from matplotlib.figure import Figure
@@ -44,6 +43,7 @@
4443
q_to_tth,
4544
tth_to_q,
4645
)
46+
from hexrdgui.utils.matplotlib import remove_artist
4747
from hexrdgui.utils.tth_distortion import apply_tth_distortion_if_needed
4848
from hexrdgui.waterfall_plot import WaterfallPlotDialog
4949

@@ -249,24 +249,24 @@ def clear_azimuthal_integral_axis(self):
249249

250250
def clear_wppf_plot(self):
251251
if self.wppf_plot:
252-
self.wppf_plot.remove()
252+
remove_artist(self.wppf_plot)
253253
self.wppf_plot = None
254254

255255
if self.wppf_background_plot:
256-
self.wppf_background_plot.remove()
256+
remove_artist(self.wppf_background_plot)
257257
self.wppf_background_plot = None
258258

259259
if self.wppf_amorphous_plot:
260-
self.wppf_amorphous_plot.remove()
260+
remove_artist(self.wppf_amorphous_plot)
261261
self.wppf_amorphous_plot = None
262262

263263
if self.wppf_difference_plot:
264-
self.wppf_difference_plot.remove()
264+
remove_artist(self.wppf_difference_plot)
265265
self.wppf_difference_plot = None
266266

267267
def clear_auto_picked_data_artists(self):
268268
while self.auto_picked_data_artists:
269-
self.auto_picked_data_artists.pop(0).remove()
269+
remove_artist(self.auto_picked_data_artists.pop(0))
270270

271271
def load_images(self, image_names):
272272
HexrdConfig().emit_update_status_bar('Loading image view...')
@@ -852,7 +852,7 @@ def update_overlays(self):
852852

853853
def clear_detector_borders(self):
854854
while self.cached_detector_borders:
855-
_safe_remove_artist(self.cached_detector_borders.pop(0))
855+
remove_artist(self.cached_detector_borders.pop(0))
856856

857857
self.draw_idle()
858858

@@ -876,7 +876,7 @@ def draw_detector_borders(self):
876876

877877
def clear_stereo_border_artists(self):
878878
while self.stereo_border_artists:
879-
_safe_remove_artist(self.stereo_border_artists.pop(0))
879+
remove_artist(self.stereo_border_artists.pop(0))
880880

881881
self.draw_idle()
882882

@@ -917,7 +917,7 @@ def draw_auto_picked_data(self):
917917

918918
def clear_saturation(self):
919919
for t in self.saturation_texts:
920-
t.remove()
920+
remove_artist(t)
921921
self.saturation_texts.clear()
922922
self.draw_idle()
923923

@@ -982,7 +982,7 @@ def compute_saturation_and_size(detector_name):
982982

983983
def clear_beam_marker(self):
984984
while self.beam_marker_artists:
985-
self.beam_marker_artists.pop(0).remove()
985+
remove_artist(self.beam_marker_artists.pop(0))
986986

987987
def update_beam_marker(self):
988988
self.clear_beam_marker()
@@ -1767,7 +1767,7 @@ def clear_azimuthal_overlay_artists(self):
17671767
while self.azimuthal_overlay_artists:
17681768
item = self.azimuthal_overlay_artists.pop(0)
17691769
for artist in item['artists'].values():
1770-
artist.remove()
1770+
remove_artist(artist)
17711771

17721772
def save_azimuthal_plot(self):
17731773
if self.mode != ViewType.polar:
@@ -1836,7 +1836,7 @@ def update_azimuthal_plot_overlays(self):
18361836
self.azimuthal_integral_axis.legend()
18371837
elif (axis := self.azimuthal_integral_axis) and axis.get_legend():
18381838
# Only remove the legend if the axis exists and it has a legend
1839-
axis.get_legend().remove()
1839+
remove_artist(axis.get_legend())
18401840
self.draw_idle()
18411841

18421842
def update_azimuthal_integral_plot(self):
@@ -2269,7 +2269,7 @@ def update_mask_boundaries(self, axis):
22692269

22702270
def clear_mask_boundaries(self):
22712271
for artist in self._mask_boundary_artists:
2272-
artist.remove()
2272+
remove_artist(artist)
22732273

22742274
self._mask_boundary_artists.clear()
22752275

@@ -2550,14 +2550,3 @@ def to_stereo(xys, panel, iviewer):
25502550
raise Exception(f'Unknown mode: {mode}')
25512551

25522552
return funcs[mode]
2553-
2554-
2555-
def _safe_remove_artist(artist: Artist):
2556-
# Starting in matplotlib 3.10, we cannot remove artists from a figure
2557-
# that has already been cleared. I don't know of any easy way to check
2558-
# if the axis has been cleared, though, so for now, we just try to
2559-
# remove the artist and ignore the relevant exception if it occurs.
2560-
try:
2561-
artist.remove()
2562-
except NotImplementedError:
2563-
pass

hexrdgui/indexing/fit_grains_results_dialog.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
from hexrdgui.ui_loader import UiLoader
2727
from hexrdgui.utils import block_signals
2828
from hexrdgui.utils.dialog import add_help_url
29+
from hexrdgui.utils.matplotlib import remove_artist
2930

3031

3132
COORDS_SLICE = slice(6, 9)
@@ -219,15 +220,15 @@ def update_enable_states(self):
219220
def clear_artists(self):
220221
# Colorbar must be removed before the scatter artist
221222
if self.colorbar is not None:
222-
self.colorbar.remove()
223+
remove_artist(self.colorbar)
223224
self.colorbar = None
224225

225226
if self.scatter_artist is not None:
226-
self.scatter_artist.remove()
227+
remove_artist(self.scatter_artist)
227228
self.scatter_artist = None
228229

229230
if self.highlight_artist is not None:
230-
self.highlight_artist.remove()
231+
remove_artist(self.highlight_artist)
231232
self.highlight_artist = None
232233

233234
def on_colorby_changed(self):
@@ -444,7 +445,7 @@ def highlight_selected_grains(self):
444445
selected_grain_ids = self.ui.table_view.selected_grain_ids
445446

446447
if self.highlight_artist is not None:
447-
self.highlight_artist.remove()
448+
remove_artist(self.highlight_artist)
448449

449450
# Now draw the highlight markers for these selected grains
450451
data = self.converted_data[:, COORDS_SLICE][selected_grain_ids].T

hexrdgui/indexing/indexing_results_dialog.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
from hexrdgui.ui_loader import UiLoader
2121
from hexrdgui.utils import block_signals
2222
from hexrdgui.utils.dialog import add_help_url
23+
from hexrdgui.utils.matplotlib import remove_artist
2324

2425

2526
class IndexingResultsDialog(QObject):
@@ -113,7 +114,7 @@ def export_grains(self, filename):
113114
if filename.suffix.lower() == '.npz':
114115
# Save as an npz
115116
grain_data = GrainData.from_array(grains)
116-
grain_data.save(selected_file)
117+
grain_data.save(filename)
117118
else:
118119
# Save as txt
119120
write_grains_txt(grains, filename)
@@ -299,7 +300,7 @@ def update_plot(self):
299300

300301
def clear_spot_lines(self):
301302
if hasattr(self, '_spot_lines'):
302-
self._spot_lines.remove()
303+
remove_artist(self._spot_lines)
303304
del self._spot_lines
304305

305306
def update_spots(self):

hexrdgui/indexing/ome_maps_viewer_dialog.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
from hexrdgui.ui_loader import UiLoader
2929
from hexrdgui.utils import block_signals
3030
from hexrdgui.utils.dialog import add_help_url
31+
from hexrdgui.utils.matplotlib import remove_artist
3132

3233
import hexrdgui.constants
3334
import hexrdgui.resources.indexing
@@ -540,7 +541,7 @@ def display_spots(self):
540541

541542
def clear_spot_lines(self):
542543
if hasattr(self, '_spot_lines'):
543-
self._spot_lines.remove()
544+
remove_artist(self._spot_lines)
544545
del self._spot_lines
545546

546547
def update_spots(self):

hexrdgui/interactive_template.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
)
1010
from hexrdgui.hexrd_config import HexrdConfig
1111
from hexrdgui.utils import has_nan
12+
from hexrdgui.utils.matplotlib import remove_artist
1213
from hexrdgui.utils.polygon import polygon_to_mask
1314

1415

@@ -160,7 +161,7 @@ def rotation(self):
160161

161162
def clear(self):
162163
if self.shape in self.axis.patches:
163-
self.shape.remove()
164+
remove_artist(self.shape)
164165
self.redraw()
165166
self.total_rotation = 0.
166167

@@ -186,7 +187,7 @@ def toggle_boundaries(self, show):
186187
self.axis.add_patch(shape)
187188
if self.shape:
188189
self.shape = self.axis.patches[-1]
189-
self.shape.remove()
190+
remove_artist(self.shape)
190191
self.shape.set_linestyle(self.shape_styles[-1]['line'])
191192
self.axis.add_patch(self.shape)
192193
self.connect_translate_rotate()

hexrdgui/line_picker_dialog.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from hexrdgui.constants import ViewType
1010
from hexrdgui.markers import igor_marker
1111
from hexrdgui.ui_loader import UiLoader
12+
from hexrdgui.utils.matplotlib import remove_artist
1213
from hexrdgui.zoom_canvas import ZoomCanvas
1314

1415

@@ -143,7 +144,7 @@ def move_dialog_to_left(self):
143144

144145
def clear(self):
145146
while self.lines:
146-
self.lines.pop(0).remove()
147+
remove_artist(self.lines.pop(0))
147148

148149
self.linebuilder = None
149150
self.previous_linebuilders.clear()
@@ -193,7 +194,7 @@ def restore_last_line(self):
193194
self.unused_colors.append(self.lines[-1]._color)
194195

195196
self.linebuilder = self.previous_linebuilders.pop()
196-
self.lines.pop().remove()
197+
remove_artist(self.lines.pop())
197198

198199
self.canvas.draw_idle()
199200

0 commit comments

Comments
 (0)