Skip to content

Commit 45fb34e

Browse files
authored
Merge pull request #202 from astrofrog/viewer-fixes
Fix compatibility of interactive viewer with recent versions of Matplotlib
2 parents 22ad813 + 689fec9 commit 45fb34e

File tree

4 files changed

+49
-7
lines changed

4 files changed

+49
-7
lines changed

.github/workflows/main.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ jobs:
1515
- macos: py39-test
1616
- windows: py39-test
1717
- linux: py310-test
18-
- macos: py311-test
18+
- macos: py311-test-viewer
1919
- windows: py312-test
2020
- linux: py313-test-devdeps
2121
coverage: 'codecov'

astrodendro/tests/test_viewer.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import pytest
2+
import numpy as np
3+
4+
import matplotlib.pyplot as plt
5+
from ..dendrogram import Dendrogram
6+
from matplotlib.backend_bases import MouseEvent
7+
8+
9+
DATA = np.array([[1, 3, 4, 4, 1, 4],
10+
[1, 2, 3, 2, 1, 3],
11+
[2, 1, 1, 3, 1, 2],
12+
[1, 1, 1, 1, 1, 1],
13+
[2, 3, 2, 1, 1, 2],
14+
[2, 3, 5, 3, 1, 1]])
15+
16+
17+
def test_viewer(capsys):
18+
19+
original_backend = plt.get_backend()
20+
21+
try:
22+
plt.switch_backend('qtagg')
23+
except ImportError:
24+
pytest.skip("This test requires Qt to be installed")
25+
26+
d = Dendrogram.compute(DATA)
27+
viewer = d.viewer()
28+
29+
plt.show(block=False)
30+
31+
cb = viewer.fig.canvas.callbacks
32+
33+
cb.process('button_press_event', MouseEvent('button_press_event', viewer.fig.canvas, 660, 520, 1))
34+
cb.process('button_press_event', MouseEvent('button_press_event', viewer.fig.canvas, 890, 800, 1))
35+
cb.process('button_press_event', MouseEvent('button_press_event', viewer.fig.canvas, 700, 700, 1))
36+
37+
plt.switch_backend(original_backend)
38+
39+
captured = capsys.readouterr()
40+
assert captured.out == ""
41+
assert captured.err == ""

astrodendro/viewer.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@ def _update_lines(self, selection_id):
277277

278278
# Remove previously selected collection
279279
if selection_id in self.selected_lines:
280-
self.ax_dendrogram.collections.remove(self.selected_lines[selection_id])
280+
self.selected_lines[selection_id].remove()
281281
del self.selected_lines[selection_id]
282282

283283
if structure is None:
@@ -308,15 +308,13 @@ def _update_lines(self, selection_id):
308308
self.ax_dendrogram.add_collection(self.selected_lines[selection_id])
309309

310310
def remove_contour(self, selection_id):
311-
312311
if selection_id in self.selected_contour:
313-
for collection in self.selected_contour[selection_id].collections:
314-
self.ax_image.collections.remove(collection)
312+
self.selected_contour[selection_id].remove()
315313
del self.selected_contour[selection_id]
316314

317315
def remove_all_contours(self):
318316
""" Remove all selected contours. """
319-
for key in self.selected_contour.keys():
317+
for key in list(self.selected_contour):
320318
self.remove_contour(key)
321319

322320
def update_contours(self):

tox.ini

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tox]
22
envlist =
3-
py{38,39,310,311}-test{,-alldeps,-devdeps}{,-cov}
3+
py{38,39,310,311}-test{,-alldeps,-devdeps,-viewer}{,-cov}
44
build_docs
55
linkcheck
66
codestyle
@@ -10,6 +10,8 @@ requires =
1010
isolated_build = true
1111

1212
[testenv]
13+
passenv =
14+
DISPLAY
1315
setenv =
1416
MPLBACKEND=agg
1517
devdeps: PIP_EXTRA_INDEX_URL = https://pypi.anaconda.org/astropy/simple https://pypi.anaconda.org/scientific-python-nightly-wheels/
@@ -30,6 +32,7 @@ deps =
3032
oldestdeps: matplotlib==3.3.*
3133
oldestdeps: numpy==1.20.*
3234
oldestdeps: pillow==8.0.*
35+
viewer: PyQt6
3336

3437
extras =
3538
test

0 commit comments

Comments
 (0)