|
16 | 16 | from mne.viz import ClickableImage, add_background_image, mne_analyze_colormap |
17 | 17 | from mne.viz.ui_events import ColormapRange, link, subscribe |
18 | 18 | from mne.viz.utils import ( |
| 19 | + SelectFromCollection, |
19 | 20 | _compute_scalings, |
20 | 21 | _fake_click, |
21 | 22 | _fake_keypress, |
@@ -274,3 +275,71 @@ def callback(event): |
274 | 275 | cmap_new1 = fig.axes[0].CB.mappable.get_cmap().name |
275 | 276 | cmap_new2 = fig2.axes[0].CB.mappable.get_cmap().name |
276 | 277 | assert cmap_new1 == cmap_new2 == cmap_want != cmap_old |
| 278 | + |
| 279 | + |
| 280 | +def test_select_from_collection(): |
| 281 | + """Test the lasso selector for matplotlib figures.""" |
| 282 | + fig, ax = plt.subplots() |
| 283 | + collection = ax.scatter([1, 2, 2, 1], [1, 1, 0, 0], color="black", edgecolor="red") |
| 284 | + ax.set_xlim(-1, 4) |
| 285 | + ax.set_ylim(-1, 2) |
| 286 | + lasso = SelectFromCollection(ax, collection, names=["A", "B", "C", "D"]) |
| 287 | + assert lasso.selection == [] |
| 288 | + |
| 289 | + # Make a selection with no patches inside of it. |
| 290 | + _fake_click(fig, ax, (0, 0), xform="data") |
| 291 | + _fake_click(fig, ax, (0.5, 0), xform="data", kind="motion") |
| 292 | + _fake_click(fig, ax, (0.5, 1), xform="data", kind="motion") |
| 293 | + _fake_click(fig, ax, (0.5, 1), xform="data", kind="release") |
| 294 | + assert lasso.selection == [] |
| 295 | + |
| 296 | + # Doing a single click on a patch should not select it. |
| 297 | + _fake_click(fig, ax, (1, 1), xform="data") |
| 298 | + assert lasso.selection == [] |
| 299 | + |
| 300 | + # Make a selection with two patches in it. |
| 301 | + _fake_click(fig, ax, (0, 0.5), xform="data") |
| 302 | + _fake_click(fig, ax, (3, 0.5), xform="data", kind="motion") |
| 303 | + _fake_click(fig, ax, (3, 1.5), xform="data", kind="motion") |
| 304 | + _fake_click(fig, ax, (0, 1.5), xform="data", kind="motion") |
| 305 | + _fake_click(fig, ax, (0, 0.5), xform="data", kind="motion") |
| 306 | + _fake_click(fig, ax, (0, 0.5), xform="data", kind="release") |
| 307 | + assert lasso.selection == ["A", "B"] |
| 308 | + |
| 309 | + # Use Control key to lasso an additional patch. |
| 310 | + _fake_keypress(fig, "control") |
| 311 | + _fake_click(fig, ax, (0.5, -0.5), xform="data") |
| 312 | + _fake_click(fig, ax, (1.5, -0.5), xform="data", kind="motion") |
| 313 | + _fake_click(fig, ax, (1.5, 0.5), xform="data", kind="motion") |
| 314 | + _fake_click(fig, ax, (0.5, 0.5), xform="data", kind="motion") |
| 315 | + _fake_click(fig, ax, (0.5, 0.5), xform="data", kind="release") |
| 316 | + _fake_keypress(fig, "control", kind="release") |
| 317 | + assert lasso.selection == ["A", "B", "D"] |
| 318 | + |
| 319 | + # Use CTRL+SHIFT to remove a patch. |
| 320 | + _fake_keypress(fig, "ctrl+shift") |
| 321 | + _fake_click(fig, ax, (0.5, 0.5), xform="data") |
| 322 | + _fake_click(fig, ax, (1.5, 0.5), xform="data", kind="motion") |
| 323 | + _fake_click(fig, ax, (1.5, 1.5), xform="data", kind="motion") |
| 324 | + _fake_click(fig, ax, (0.5, 1.5), xform="data", kind="motion") |
| 325 | + _fake_click(fig, ax, (0.5, 1.5), xform="data", kind="release") |
| 326 | + _fake_keypress(fig, "ctrl+shift", kind="release") |
| 327 | + assert lasso.selection == ["B", "D"] |
| 328 | + |
| 329 | + # Check that the two selected patches have a different appearance. |
| 330 | + fc = lasso.collection.get_facecolors() |
| 331 | + ec = lasso.collection.get_edgecolors() |
| 332 | + assert (fc[:, -1] == [0.5, 1.0, 0.5, 1.0]).all() |
| 333 | + assert (ec[:, -1] == [0.25, 1.0, 0.25, 1.0]).all() |
| 334 | + |
| 335 | + # Test adding and removing single channels. |
| 336 | + lasso.select_one(2) # should not do anything without modifier keys |
| 337 | + assert lasso.selection == ["B", "D"] |
| 338 | + _fake_keypress(fig, "control") |
| 339 | + lasso.select_one(2) # add to selection |
| 340 | + _fake_keypress(fig, "control", kind="release") |
| 341 | + assert lasso.selection == ["B", "C", "D"] |
| 342 | + _fake_keypress(fig, "ctrl+shift") |
| 343 | + lasso.select_one(1) # remove from selection |
| 344 | + assert lasso.selection == ["C", "D"] |
| 345 | + _fake_keypress(fig, "ctrl+shift", kind="release") |
0 commit comments