def testCorrectSettingOfMultipleAnnotationPoints(self):
        '''
        Test correct setting of annotations if mouse hovers over multiple scattered points
        '''
        # create mock values
        mocked_ax = plt.gca()
        mocked_ax.set_xlim((0, 1))
        mocked_ax.set_ylim((0, 1))
        mocked_scatter = mocked_ax.scatter(
            pd.Series([.5, .5, .5], dtype=float),
            pd.Series([.5, .5, .5], dtype=float))
        # 3 annotations on same point
        mocked_annotation_points_list = [
            txt.Annotation('dummy-annotation1', (.5, .5), visible=False),
            txt.Annotation('dummy-annotation2', (.5, .5), visible=False),
            txt.Annotation('dummy-annotation3', (.5, .5), visible=False)
        ]
        mocked_last_hov_index = -1
        mocked_mouse_event_on = bb.MouseEvent('mocked-mouse-event-on',
                                              plt.gcf().canvas, 322,
                                              242)  # on point (.5|.5)
        mocked_mouse_event_off = bb.MouseEvent('mocked-mouse-event-off',
                                               plt.gcf().canvas, 100,
                                               100)  # off point (.5|.5)

        # create object
        main_sequence = createUUT()
        with patch.object(main_sequence, '_annotation_points',
                          mocked_annotation_points_list):
            with patch.object(main_sequence, '_last_hov_anno_index',
                              mocked_last_hov_index):
                # call function on point
                main_sequence._annotate_point(mocked_mouse_event_on,
                                              mocked_scatter)

                # assert last hovered index
                self.assertEqual(0, main_sequence._last_hov_anno_index)
                self.assertTrue(
                    main_sequence._annotation_points[0].get_visible())

                # call function off point
                main_sequence._annotate_point(mocked_mouse_event_off,
                                              mocked_scatter)

                # assert last hovered index (no change in index off point)
                self.assertEqual(0, main_sequence._last_hov_anno_index)
                self.assertFalse(
                    main_sequence._annotation_points[0].get_visible())

                # call function on point again (annotation should change)
                main_sequence._annotate_point(mocked_mouse_event_on,
                                              mocked_scatter)

                # assert last hovered index change
                self.assertEqual(1, main_sequence._last_hov_anno_index)
                self.assertTrue(
                    main_sequence._annotation_points[1].get_visible())
Пример #2
0
def test_contains():
    import matplotlib.backend_bases as mbackend

    fig = plt.figure()
    ax = plt.axes()

    mevent = mbackend.MouseEvent('button_press_event', fig.canvas, 0.5, 0.5, 1,
                                 None)

    xs = np.linspace(0.25, 0.75, 30)
    ys = np.linspace(0.25, 0.75, 30)
    xs, ys = np.meshgrid(xs, ys)

    txt = plt.text(0.48,
                   0.52,
                   'hello world',
                   ha='center',
                   fontsize=30,
                   rotation=30)
    # uncomment to draw the text's bounding box
    # txt.set_bbox(dict(edgecolor='black', facecolor='none'))

    # draw the text. This is important, as the contains method can only work
    # when a renderer exists.
    fig.canvas.draw()

    for x, y in zip(xs.flat, ys.flat):
        mevent.x, mevent.y = plt.gca().transAxes.transform_point([x, y])
        contains, _ = txt.contains(mevent)
        color = 'yellow' if contains else 'red'

        # capture the viewLim, plot a point, and reset the viewLim
        vl = ax.viewLim.frozen()
        ax.plot(x, y, 'o', color=color)
        ax.viewLim.set(vl)
    def testCorrectFunctionCallsIfSinglePointSelected(self,
                                                      mocked_txt_get_vis_func,
                                                      mocked_txt_set_vis_func,
                                                      mocked_coll_cont_func):
        '''
        Test correct function calls if mouse hovers over a single scattered point
        '''
        # assert mocks
        self.assertIs(txt.Text.get_visible, mocked_txt_get_vis_func)
        self.assertIs(txt.Text.set_visible, mocked_txt_set_vis_func)
        self.assertIs(coll.PathCollection.contains, mocked_coll_cont_func)

        # create mock values
        mocked_ax = plt.gca()
        mocked_ax.set_xlim((0, 1))
        mocked_ax.set_ylim((0, 1))
        mocked_scatter = mocked_ax.scatter(pd.Series([.5], dtype=float),
                                           pd.Series([.5], dtype=float))
        mocked_coll_cont_func.return_value = True, {
            'ind': np.array([0], dtype=int)
        }
        mocked_annotation_points_list = [
            txt.Annotation('dummy-annotation1', (.5, .5), visible=False),
            txt.Annotation('dummy-annotation2', (.1, .5), visible=False),
            txt.Annotation('dummy-annotation3', (.7, .2), visible=False)
        ]
        mocked_mouse_event = bb.MouseEvent('mocked-mouse-event',
                                           plt.gcf().canvas, 322,
                                           242)  # on point (.5|.5)

        # create object
        main_sequence = createUUT()
        with patch.object(main_sequence, '_annotation_points',
                          mocked_annotation_points_list):
            # call function to test
            main_sequence._annotate_point(mocked_mouse_event, mocked_scatter)

            # assert function calls
            mocked_coll_cont_func.assert_called_once()
            call_list = [
                mocked_txt_get_vis_func(),
                mocked_txt_get_vis_func(),
                mocked_txt_get_vis_func()
            ]
            mocked_txt_get_vis_func.has_calls(call_list)  # called three times
            mocked_txt_set_vis_func.assert_called()
    def testCorrectSettingOfMultipleAnnotationPointsOfAnotherGroup(self):
        '''
        Test correct setting of annotations if mouse hovers from one group of annotation points to another
        '''
        # create mock values
        mocked_ax = plt.gca()
        mocked_ax.set_xlim((0, 1))
        mocked_ax.set_ylim((0, 1))
        mocked_scatter = mocked_ax.scatter(
            pd.Series([.5, .5, .25, .25], dtype=float),
            pd.Series([.5, .5, .25, .25], dtype=float))
        # 2 annotations on same point
        mocked_annotation_points_list = [
            txt.Annotation('dummy-annotation1_1', (.5, .5), visible=False),
            txt.Annotation('dummy-annotation1_2', (.5, .5), visible=False),
            txt.Annotation('dummy-annotation2_1', (.25, .25), visible=False),
            txt.Annotation('dummy-annotation2_2', (.25, .25), visible=False),
        ]
        mocked_last_hov_index = -1
        mocked_mouse_event_on_1 = bb.MouseEvent('mocked-mouse-event-on-1',
                                                plt.gcf().canvas, 322,
                                                242)  # on point (.5|.5)
        mocked_mouse_event_on_2 = bb.MouseEvent('mocked-mouse-event-on-2',
                                                plt.gcf().canvas, 205,
                                                146)  # on point (.25|.25)
        mocked_mouse_event_off = bb.MouseEvent('mocked-mouse-event-off',
                                               plt.gcf().canvas, 100,
                                               100)  # off points

        # create object
        main_sequence = createUUT()
        with patch.object(main_sequence, '_annotation_points',
                          mocked_annotation_points_list):
            with patch.object(main_sequence, '_last_hov_anno_index',
                              mocked_last_hov_index):
                # call function on point 1
                main_sequence._annotate_point(mocked_mouse_event_on_1,
                                              mocked_scatter)

                # assert last hovered index
                self.assertEqual(0, main_sequence._last_hov_anno_index)
                self.assertTrue(
                    main_sequence._annotation_points[0].get_visible())

                # call function off point
                main_sequence._annotate_point(mocked_mouse_event_off,
                                              mocked_scatter)

                # assert last hovered index (no change in index off point)
                self.assertEqual(0, main_sequence._last_hov_anno_index)
                self.assertFalse(
                    main_sequence._annotation_points[0].get_visible())

                # call function on point 2
                main_sequence._annotate_point(mocked_mouse_event_on_2,
                                              mocked_scatter)

                # assert last hovered index change
                self.assertEqual(2, main_sequence._last_hov_anno_index)
                self.assertTrue(
                    main_sequence._annotation_points[2].get_visible())