<commit_before>from matplotlib import pyplot as plt
from matplotlib.backend_bases import KeyEvent, MouseEvent
import mplcursors
import numpy as np
import pytest


_eps = .001


@pytest.yield_fixture
def ax():
    fig, ax = plt.subplots()
    try:
        yield ax
    finally:
        plt.close(fig)


def _process_event(name, ax, coords, *args):
    if name == "__mouse_click__":
        # So that the dragging callbacks don't go crazy.
        _process_event("button_press_event", ax, coords, *args)
        _process_event("button_release_event", ax, coords, *args)
        return
    display_coords = ax.transData.transform_point(coords)
    if name in ["button_press_event", "button_release_event",
                "motion_notify_event", "scroll_event"]:
        event = MouseEvent(name, ax.figure.canvas, *display_coords, *args)
    elif name in ["key_press_event", "key_release_event"]:
        event = KeyEvent(name, ax.figure.canvas, *args, *display_coords)
    else:
        raise ValueError("Unknown event name {!r}".format(name))
    ax.figure.canvas.callbacks.process(name, event)


def _remove_selection(sel):
    ax = sel.artist.axes
    # Text bounds are found only upon drawing.
    ax.figure.canvas.draw()
    bbox = sel.annotation.get_window_extent()
    center = ax.transData.inverted().transform_point(
        ((bbox.x0 + bbox.x1) / 2, (bbox.y0 + bbox.y1) / 2))
    _process_event("__mouse_click__", ax, center, 3)


def test_line(ax):
    ax.plot([0, .2, 1], [0, .8, 1], label="foo")
    cursor = mplcursors.cursor(multiple=True)
    # Far, far away.
    _process_event("__mouse_click__", ax, (0, 1), 1)
    assert len(cursor.selections) == len(ax.texts) == 0
    # On the line.
    _process_event("__mouse_click__", ax, (.1, .4), 1)
    assert len(cursor.selections) == len(ax.texts) == 1
    # `format_{x,y}data` space-pads its output.
    assert cursor.selections[0].annotation.get_text() == "foo\nx: 0.1\ny: 0.4"
    # Not removing it.
    _process_event("__mouse_click__", ax, (0, 1), 3)
    assert len(cursor.selections) == len(ax.texts) == 1
    # Remove it.
    _remove_selection(cursor.selections[0])
    assert len(cursor.selections) == len(ax.texts) == 0
    # Will project on the vertex at (.2, .8).
    _process_event("__mouse_click__", ax, (.2 - _eps, .8 + _eps), 1)
    assert len(cursor.selections) == len(ax.texts) == 1


@pytest.mark.parametrize(
    "plotter",
    [lambda ax, data: ax.plot(*data, "o"),
     lambda ax, data: ax.scatter(*data)])
def test_scatter(ax, plotter):
    plotter(ax, [[0, .5, 1], [0, .5, 1]])
    cursor = mplcursors.cursor()
    _process_event("__mouse_click__", ax, (.2, .2), 1)
    assert len(cursor.selections) == len(ax.texts) == 0
    _process_event("__mouse_click__", ax, (.5, .5), 1)
    assert len(cursor.selections) == len(ax.texts) == 1


def test_steps_index():
    from mplcursors._pick_info import Index
    index = Index(0, .5, .5)
    assert np.floor(index) == 0 and np.ceil(index) == 1
    assert str(index) == "0.(x=0.5, y=0.5)"


def test_steps_pre(ax):
    ax.plot([0, 1], [0, 1], drawstyle="steps-pre")
    cursor = mplcursors.cursor()
    _process_event("__mouse_click__", ax, (1, 0), 1)
    assert len(cursor.selections) == 0
    _process_event("__mouse_click__", ax, (0, .5), 1)
    index = cursor.selections[0].target.index
    assert np.allclose((index.int, index.x, index.y), (0, 0, .5))
    _process_event("__mouse_click__", ax, (.5, 1), 1)
    index = cursor.selections[0].target.index
    assert np.allclose((index.int, index.x, index.y), (0, .5, 1))


def test_steps_mid(ax):
    ax.plot([0, 1], [0, 1], drawstyle="steps-mid")
    cursor = mplcursors.cursor()
    _process_event("__mouse_click__", ax, (0, 1), 1)
    assert len(cursor.selections) == 0
    _process_event("__mouse_click__", ax, (1, 0), 1)
    assert len(cursor.selections) == 0
    _process_event("__mouse_click__", ax, (.25, 0), 1)
    index = cursor.selections[0].target.index
    assert np.allclose((index.int, index.x, index.y), (0, .25, 0))
    _process_event("__mouse_click__", ax, (.5, .5), 1)
    index = cursor.selections[0].target.index
    assert np.allclose((index.int, index.x, index.y), (0, .5, .5))
    _process_event("__mouse_click__", ax, (.75, 1), 1)
    index = cursor.selections[0].target.index
    assert np.allclose((index.int, index.x, index.y), (0, .75, 1))


def test_steps_post(ax):
    ax.plot([0, 1], [0, 1], drawstyle="steps-post")
    cursor = mplcursors.cursor()
    _process_event("__mouse_click__", ax, (0, 1), 1)
    assert len(cursor.selections) == 0
    _process_event("__mouse_click__", ax, (.5, 0), 1)
    index = cursor.selections[0].target.index
    np.testing.assert_allclose((index.int, index.x, index.y), (0, .5, 0))
    _process_event("__mouse_click__", ax, (1, .5), 1)
    index = cursor.selections[0].target.index
    np.testing.assert_allclose((index.int, index.x, index.y), (0, 1, .5))


def test_line_edge_cases(ax):
    for ls in ["-", "o"]:
        ax.cla()
        ax.plot(0, ls)
        ax.set(xlim=(-1, 1), ylim=(-1, 1))
        cursor = mplcursors.cursor()
        _process_event("__mouse_click__", ax, (_eps, _eps), 1)
        assert len(cursor.selections) == len(ax.texts) == 1
        np.testing.assert_array_equal(
            np.asarray(cursor.selections[0].target), (0, 0))
        cursor.remove()


def test_nan(ax):
    ax.plot([0, 1, np.nan, 3, 4])
    cursor = mplcursors.cursor()
    _process_event("__mouse_click__", ax, (.5, .5), 1)
    assert len(cursor.selections) == len(ax.texts) == 1
    np.testing.assert_array_equal(
        np.asarray(cursor.selections[0].target), (.5, .5))


def test_image(ax):
    ax.imshow(np.arange(4).reshape((2, 2)))
    cursor = mplcursors.cursor()
    # Not picking out-of-axes.
    _process_event("__mouse_click__", ax, (-1, -1), 1)
    assert len(cursor.selections) == 0
    # Annotation text includes image value.
    _process_event("__mouse_click__", ax, (.75, .75), 1)
    assert (cursor.selections[0].annotation.get_text()
            == "x: 0.75\ny: 0.75\nz: 3")
    # Moving has no effect.
    _process_event("key_press_event", ax, (.123, .456), "shift+left")
    assert (cursor.selections[0].annotation.get_text()
            == "x: 0.75\ny: 0.75\nz: 3")<commit_msg>Fix assert_equal vs. assert_allclose.<commit_after>from matplotlib import pyplot as plt
from matplotlib.backend_bases import KeyEvent, MouseEvent
import mplcursors
import numpy as np
from numpy.testing import assert_allclose, assert_array_equal
import pytest


_eps = .001


@pytest.yield_fixture
def ax():
    fig, ax = plt.subplots()
    try:
        yield ax
    finally:
        plt.close(fig)


def _process_event(name, ax, coords, *args):
    if name == "__mouse_click__":
        # So that the dragging callbacks don't go crazy.
        _process_event("button_press_event", ax, coords, *args)
        _process_event("button_release_event", ax, coords, *args)
        return
    display_coords = ax.transData.transform_point(coords)
    if name in ["button_press_event", "button_release_event",
                "motion_notify_event", "scroll_event"]:
        event = MouseEvent(name, ax.figure.canvas, *display_coords, *args)
    elif name in ["key_press_event", "key_release_event"]:
        event = KeyEvent(name, ax.figure.canvas, *args, *display_coords)
    else:
        raise ValueError("Unknown event name {!r}".format(name))
    ax.figure.canvas.callbacks.process(name, event)


def _remove_selection(sel):
    ax = sel.artist.axes
    # Text bounds are found only upon drawing.
    ax.figure.canvas.draw()
    bbox = sel.annotation.get_window_extent()
    center = ax.transData.inverted().transform_point(
        ((bbox.x0 + bbox.x1) / 2, (bbox.y0 + bbox.y1) / 2))
    _process_event("__mouse_click__", ax, center, 3)


def test_line(ax):
    ax.plot([0, .2, 1], [0, .8, 1], label="foo")
    cursor = mplcursors.cursor(multiple=True)
    # Far, far away.
    _process_event("__mouse_click__", ax, (0, 1), 1)
    assert len(cursor.selections) == len(ax.texts) == 0
    # On the line.
    _process_event("__mouse_click__", ax, (.1, .4), 1)
    assert len(cursor.selections) == len(ax.texts) == 1
    # `format_{x,y}data` space-pads its output.
    assert cursor.selections[0].annotation.get_text() == "foo\nx: 0.1\ny: 0.4"
    # Not removing it.
    _process_event("__mouse_click__", ax, (0, 1), 3)
    assert len(cursor.selections) == len(ax.texts) == 1
    # Remove it.
    _remove_selection(cursor.selections[0])
    assert len(cursor.selections) == len(ax.texts) == 0
    # Will project on the vertex at (.2, .8).
    _process_event("__mouse_click__", ax, (.2 - _eps, .8 + _eps), 1)
    assert len(cursor.selections) == len(ax.texts) == 1


@pytest.mark.parametrize(
    "plotter",
    [lambda ax, data: ax.plot(*data, "o"),
     lambda ax, data: ax.scatter(*data)])
def test_scatter(ax, plotter):
    plotter(ax, [[0, .5, 1], [0, .5, 1]])
    cursor = mplcursors.cursor()
    _process_event("__mouse_click__", ax, (.2, .2), 1)
    assert len(cursor.selections) == len(ax.texts) == 0
    _process_event("__mouse_click__", ax, (.5, .5), 1)
    assert len(cursor.selections) == len(ax.texts) == 1


def test_steps_index():
    from mplcursors._pick_info import Index
    index = Index(0, .5, .5)
    assert np.floor(index) == 0 and np.ceil(index) == 1
    assert str(index) == "0.(x=0.5, y=0.5)"


def test_steps_pre(ax):
    ax.plot([0, 1], [0, 1], drawstyle="steps-pre")
    cursor = mplcursors.cursor()
    _process_event("__mouse_click__", ax, (1, 0), 1)
    assert len(cursor.selections) == 0
    _process_event("__mouse_click__", ax, (0, .5), 1)
    index = cursor.selections[0].target.index
    assert_allclose((index.int, index.x, index.y), (0, 0, .5))
    _process_event("__mouse_click__", ax, (.5, 1), 1)
    index = cursor.selections[0].target.index
    assert_allclose((index.int, index.x, index.y), (0, .5, 1))


def test_steps_mid(ax):
    ax.plot([0, 1], [0, 1], drawstyle="steps-mid")
    cursor = mplcursors.cursor()
    _process_event("__mouse_click__", ax, (0, 1), 1)
    assert len(cursor.selections) == 0
    _process_event("__mouse_click__", ax, (1, 0), 1)
    assert len(cursor.selections) == 0
    _process_event("__mouse_click__", ax, (.25, 0), 1)
    index = cursor.selections[0].target.index
    assert_allclose((index.int, index.x, index.y), (0, .25, 0))
    _process_event("__mouse_click__", ax, (.5, .5), 1)
    index = cursor.selections[0].target.index
    assert_allclose((index.int, index.x, index.y), (0, .5, .5))
    _process_event("__mouse_click__", ax, (.75, 1), 1)
    index = cursor.selections[0].target.index
    assert_allclose((index.int, index.x, index.y), (0, .75, 1))


def test_steps_post(ax):
    ax.plot([0, 1], [0, 1], drawstyle="steps-post")
    cursor = mplcursors.cursor()
    _process_event("__mouse_click__", ax, (0, 1), 1)
    assert len(cursor.selections) == 0
    _process_event("__mouse_click__", ax, (.5, 0), 1)
    index = cursor.selections[0].target.index
    assert_allclose((index.int, index.x, index.y), (0, .5, 0))
    _process_event("__mouse_click__", ax, (1, .5), 1)
    index = cursor.selections[0].target.index
    assert_allclose((index.int, index.x, index.y), (0, 1, .5))


def test_line_edge_cases(ax):
    for ls in ["-", "o"]:
        ax.cla()
        ax.plot(0, ls)
        ax.set(xlim=(-1, 1), ylim=(-1, 1))
        cursor = mplcursors.cursor()
        _process_event("__mouse_click__", ax, (_eps, _eps), 1)
        assert len(cursor.selections) == len(ax.texts) == 1
        assert_array_equal(np.asarray(cursor.selections[0].target), (0, 0))
        cursor.remove()


def test_nan(ax):
    ax.plot([0, 1, np.nan, 3, 4])
    cursor = mplcursors.cursor()
    _process_event("__mouse_click__", ax, (.5, .5), 1)
    assert len(cursor.selections) == len(ax.texts) == 1
    assert_allclose(np.asarray(cursor.selections[0].target), (.5, .5))


def test_image(ax):
    ax.imshow(np.arange(4).reshape((2, 2)))
    cursor = mplcursors.cursor()
    # Not picking out-of-axes.
    _process_event("__mouse_click__", ax, (-1, -1), 1)
    assert len(cursor.selections) == 0
    # Annotation text includes image value.
    _process_event("__mouse_click__", ax, (.75, .75), 1)
    assert (cursor.selections[0].annotation.get_text()
            == "x: 0.75\ny: 0.75\nz: 3")
    # Moving has no effect.
    _process_event("key_press_event", ax, (.123, .456), "shift+left")
    assert (cursor.selections[0].annotation.get_text()
            == "x: 0.75\ny: 0.75\nz: 3")
{'fraction_non_alphanumeric': 0.1700576738224928, 'fraction_numerical': 0.03220121755847485, 'mean_word_length': 3.371498599439776, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '616154', 'n_tokens_mistral': 4902, 'n_tokens_neox': 4625, 'n_words': 1024}