コード例 #1
0
    def test_redraw_unchanged_axes(self):
        """
        Test that when redrawing without changing the axes, the labels do not move.
        """

        viz = DummyLabelledVisualization(
            drawable.Drawable(plt.figure(figsize=(10, 10))))
        l1 = viz.draw_label('Label 1', (0, 1), 0)
        l2 = viz.draw_label('Label 2', (0, 1), 1)
        viz.redraw()
        pre_bb1, pre_bb2 = l1.get_virtual_bb(), l2.get_virtual_bb()
        self.assertFalse(util.overlapping_bb(pre_bb1, pre_bb2))

        # redraw and make sure that the labels did not move
        viz.redraw()
        post_bb1, post_bb2 = l1.get_virtual_bb(), l2.get_virtual_bb()
        self.assertFalse(util.overlapping_bb(post_bb1, post_bb2))

        self.assertEqual(pre_bb1.x0, post_bb1.x0)
        self.assertEqual(pre_bb1.y0, post_bb1.y0)
        self.assertEqual(pre_bb1.x1, post_bb1.x1)
        self.assertEqual(pre_bb1.y1, post_bb1.y1)

        self.assertEqual(pre_bb2.x0, post_bb2.x0)
        self.assertEqual(pre_bb2.y0, post_bb2.y0)
        self.assertEqual(pre_bb2.x1, post_bb2.x1)
        self.assertEqual(pre_bb2.y1, post_bb2.y1)
コード例 #2
0
    def test_overlapping_corner_top_left(self):
        """
        Test that when a bounding box is at the top-left corner of another bounding box, the two do not overlap.
        """

        bb1, bb2 = Bbox(((0, 0), (1, 1))), Bbox(((-1, 1), (0, 2)))
        self.assertFalse(util.overlapping_bb(bb1, bb2))
コード例 #3
0
    def test_overlapping_contains(self):
        """
        Test that when a bounding box contains the other, they overlap.
        """

        bb1, bb2 = Bbox(((0, 0), (1, 1))), Bbox(((-1, -1), (2, 2)))
        self.assertTrue(util.overlapping_bb(bb1, bb2))
コード例 #4
0
    def test_overlapping_exact(self):
        """
        Test that when two bounding boxes are the same, they overlap.
        """

        bb1, bb2 = Bbox(((0, 0), (1, 1))), Bbox(((0, 0), (1, 1)))
        self.assertTrue(util.overlapping_bb(bb1, bb2))
コード例 #5
0
    def test_overlapping_left(self):
        """
        Test that when a bounding box overlaps at the left of another bounding box, the function returns true.
        """

        bb1, bb2 = Bbox(((0, 0), (1, 1))), Bbox(((-0.5, 0), (0.5, 1)))
        self.assertTrue(util.overlapping_bb(bb1, bb2))
コード例 #6
0
    def test_overlapping_corner_bottom_right(self):
        """
        Test that when a bounding box is at the bottom-right corner of another bounding box, the two do not overlap.
        """

        bb1, bb2 = Bbox(((0, 0), (1, 1))), Bbox(((1, -1), (2, 0)))
        self.assertFalse(util.overlapping_bb(bb1, bb2))
コード例 #7
0
    def test_overlapping_left_border(self):
        """
        Test that when a bounding box is at the left of another bounding box, the two do not overlap.
        """

        bb1, bb2 = Bbox(((0, 0), (1, 1))), Bbox(((-1, 0), (0, 1)))
        self.assertFalse(util.overlapping_bb(bb1, bb2))
コード例 #8
0
    def test_redraw_top_xaxes(self):
        """
        Test that when the x-axis label is at the top, the caption moves up.
        """

        viz = drawable.Drawable(plt.figure(figsize=(10, 5)))
        caption = viz.set_caption("sample caption")
        caption_bb = caption.get_virtual_bb(transform=viz.axes.transAxes)
        self.assertFalse(
            util.overlapping_bb(caption_bb,
                                viz._get_xlabel(transform=viz.axes.transAxes)))
        """
        Move the x-axis label and ticks to the top.
        """
        viz.axes.xaxis.set_label_position('top')
        viz.axes.xaxis.tick_top()
        viz.axes.spines['top'].set_visible(True)
        viz.axes.spines['bottom'].set_visible(False)
        """
        After adding a label, the caption should move up.
        """
        viz.set_xlabel('label')
        viz.redraw()
        self.assertLess(
            caption_bb.y0,
            viz.caption.get_virtual_bb(transform=viz.axes.transAxes).y0)
コード例 #9
0
    def test_overlapping_non_overlapping(self):
        """
        Test that when two bounding boxes do not overlap at all, they do not overlap.
        """

        bb1, bb2 = Bbox(((0, 0), (1, 1))), Bbox(((2, 2), (3, 3)))
        self.assertFalse(util.overlapping_bb(bb1, bb2))
コード例 #10
0
    def test_overlapping_within(self):
        """
        Test that when a bounding box is within the other, they overlap.
        """

        bb1, bb2 = Bbox(((0, 0), (1, 1))), Bbox(((0.25, 0.25), (0.75, 0.75)))
        self.assertTrue(util.overlapping_bb(bb1, bb2))
コード例 #11
0
    def test_visual_annotation_do_not_overlap(self):
        """
        Test that when drawing a legend, the visual and the annotation do not overlap.
        """

        viz = drawable.Drawable(plt.figure(figsize=(10, 10)))
        line, annotation = viz.legend.draw_line('A')
        linebb = util.get_bb(viz.figure, viz.axes, line)
        self.assertFalse(util.overlapping_bb(linebb, annotation.get_virtual_bb()))
コード例 #12
0
    def test_overlapping_example(self):
        """
        Test that when a bounding box overlaps at the left of another bounding box, the function returns true.
        """

        bb1 = Bbox(((-1.093238251561293, 957636.3708609273),
                    (-0.8867432792684633, 930347.6291390731)))
        bb2 = Bbox(((-1.2513544017740887, 946495.3708609274),
                    (-0.8867432792684635, 919206.6291390731)))
        self.assertTrue(util.overlapping_bb(bb1, bb2))
コード例 #13
0
	def test_overlapping_labels(self):
		"""
		Test that when two labels overlap, they are distributed vertically.
		"""

		viz = DummyLabelledVisualization(drawable.Drawable(plt.figure(figsize=(10, 10))))
		label1 = viz.draw_label('A', 4, 10)
		label2 = viz.draw_label('B', 4, 10)

		self.assertEqual(label1.get_virtual_bb().x0, label2.get_virtual_bb().x0)
		self.assertFalse(util.overlapping_bb(label1.get_virtual_bb(), label2.get_virtual_bb()))
コード例 #14
0
    def _get_overlapping_labels(self, labels=None):
        """
        Get groups of overlapping labels.
        The function returns a list of lists.
        Each inner list contains the labels that overlap.
        The function automatically excludes labels that do not overlap with other labels.

        :param labels: The labels to check for overlaps.
                       When adding a new label, it can be assumed that the other labels on the plot already do not overlap.
                       Therefore checks among the existing labels are not required.
                       If given, this function only checks for any other labels that overlap with the given label.
        :type labels: None or :class:`matplotlib.text.Text` or list of :class:`matplotlib.text.Text`

        :return: A list of lists.
                 Each inner list represents overlapping labels.
        :rtype: list of lists of :class:`matplotlib.text.Text`
        """

        figure = self.drawable.figure
        axes = self.drawable.axes

        all = sorted(self.labels, key=lambda label: label.get_virtual_bb().y0)
        labels = labels or []  # change `None` to an empty list
        labels = [labels] if type(
            labels) is not list else labels  # change a single label to a list
        labels = labels or all

        overlapping_labels = [[label] for label in all if label not in labels]
        for label in labels:
            assigned = False
            """
            Go through each label and visit each group of overlapping labels.
            If the label overlaps with any label in that group, add it to that group.
            That group would have to be distributed entirely.
            """
            for group in overlapping_labels:
                if (any([
                        util.overlapping_bb(label.get_virtual_bb(),
                                            other.get_virtual_bb())
                        for other in group
                ])):
                    group.append(label)
                    assigned = True
                    break
            """
            If the label does not overlap with any other label, add it to its own group.
            Groups with a single label overlap with no other group and require no distribution.
            """
            if not assigned:
                overlapping_labels.append([label])

        return [group for group in overlapping_labels if len(group) > 1]
コード例 #15
0
    def test_redraw_overlapping(self):
        """
        Test that when labels that overlapped no longer overlap after redrawing.
        """

        viz = DummyLabelledVisualization(
            drawable.Drawable(plt.figure(figsize=(10, 10))))
        l1 = viz.draw_label('Label 1', (0, 1), 0)
        l2 = viz.draw_label('Label 2', (0, 1), 1)
        viz.redraw()
        pre_bb1, pre_bb2 = l1.get_virtual_bb(), l2.get_virtual_bb()
        self.assertFalse(util.overlapping_bb(pre_bb1, pre_bb2))

        # increase the size of the y-axis so that the labels overlap
        viz.drawable.set_ylim((0, 100))
        post_bb1, post_bb2 = l1.get_virtual_bb(), l2.get_virtual_bb()
        self.assertTrue(util.overlapping_bb(post_bb1, post_bb2))

        # redraw and make sure that the labels did not move
        viz.redraw()
        post_bb1, post_bb2 = l1.get_virtual_bb(), l2.get_virtual_bb()
        self.assertFalse(util.overlapping_bb(post_bb1, post_bb2))
コード例 #16
0
    def test_overlapping_labels_all(self):
        """
        Test that when all labels are set to overlap, at the end none of them overlap.
        """

        viz = DummyLabelledVisualization(
            drawable.Drawable(plt.figure(figsize=(10, 10))))

        for letter in string.ascii_letters[:4]:
            viz.draw_label(letter, 0, 0)
        viz.redraw()

        for i, l1 in enumerate(viz.labels):
            for l2 in viz.labels[(i + 1):]:
                bb1, bb2 = l1.get_virtual_bb(), l2.get_virtual_bb()
                self.assertFalse(util.overlapping_bb(bb1, bb2))
コード例 #17
0
    def test_overlapping_inverted_bounding_boxes_unchanged(self):
        """
        Test that when the x- or y-axis are inverted, the overlapping test does not change the bounding boxes, but creates new ones.
        """

        # create overlapping bounding boxes that emulate inverted x- and y-axes and test
        bb1, bb2 = Bbox(((1, 1), (0, 0))), Bbox(((1.5, 1.5), (0.5, 0.5)))
        self.assertTrue(util.overlapping_bb(bb1, bb2))
        self.assertEqual(1, bb1.x0)
        self.assertEqual(1, bb1.y0)
        self.assertEqual(0, bb1.x1)
        self.assertEqual(0, bb1.y1)
        self.assertEqual(1.5, bb2.x0)
        self.assertEqual(1.5, bb2.y0)
        self.assertEqual(0.5, bb2.x1)
        self.assertEqual(0.5, bb2.y1)
コード例 #18
0
    def test_text_only_overlap(self):
        """
        Test that when adding text-only annotations, they do not overlap.
        """

        viz = drawable.Drawable(plt.figure(figsize=(10, 10)))
        vocab = string.ascii_uppercase
        labels = [''.join(vocab[i:(i + 3)]) for i in range(len(vocab) - 3)]
        for label in labels:
            line, annotation = viz.legend.draw_text_only(label)

        annotations = [
            annotation for line in viz.legend.lines for _, annotation in line
        ]
        for i in range(len(annotations) - 1):
            bb1 = annotations[i].get_virtual_bb()
            bb2 = annotations[i + 1].get_virtual_bb()
            self.assertFalse(util.overlapping_bb(bb1, bb2))
コード例 #19
0
    def _get_overlapping_labels(self):
        """
        Get groups of overlapping labels.
        The function returns a list of lists.
        Each inner list contains the labels that overlap.
        The function automatically excludes labels that do not overlap with other labels.

        :return: A list of lists.
                 Each inner list represents overlapping labels.
        :rtype: list of lists of :class:`matplotlib.text.Text`
        """

        figure = self.drawable.figure
        axes = self.drawable.axes

        labels = sorted(self.labels,
                        key=lambda label: label.get_virtual_bb().y0)

        overlapping_labels = []
        for label in labels:
            assigned = False
            """
            Go through each label and visit each group of overlapping labels.
            If the label overlaps with any label in that group, add it to that group.
            That group would have to be distributed entirely.
            """
            for group in overlapping_labels:
                if (any([
                        util.overlapping_bb(label.get_virtual_bb(),
                                            other.get_virtual_bb())
                        for other in group
                ])):
                    group.append(label)
                    assigned = True
                    break
            """
            If the label does not overlap with any other label, add it to its own group.
            Groups with a single label overlap with no other group and require no distribution.
            """
            if not assigned:
                overlapping_labels.append([label])

        return [group for group in overlapping_labels if len(group) > 1]
コード例 #20
0
    def test_new_line_text_only(self):
        """
        Test that when creating a new line for text-only annotations, the new line does not crash because there is no annotation.
        """

        viz = drawable.Drawable(plt.figure(figsize=(10, 10)))
        vocab = string.ascii_uppercase
        labels = [''.join(vocab[i:(i + 3)]) for i in range(len(vocab) - 3)]
        for label in labels:
            line, annotation = viz.legend.draw_text_only(label)
        self.assertGreaterEqual(len(viz.legend.lines), 2)

        annotations = [
            annotation for line in viz.legend.lines for _, annotation in line
        ]
        for i in range(len(annotations) - 1):
            bb1 = annotations[i].get_virtual_bb()
            bb2 = annotations[i + 1].get_virtual_bb()
            self.assertFalse(util.overlapping_bb(bb1, bb2))
コード例 #21
0
    def test_overlapping_inverted_y(self):
        """
        Test that when the y-axis is inverted, the overlapping test adapts the bounding boxes.
        """

        # first test the normal behavior of the bounding box
        viz = drawable.Drawable(plt.figure(figsize=(5, 5)))
        text = viz.text(0, 0, 'piece of text')
        bb = util.get_bb(viz.figure, viz.axes, text)
        self.assertLess(bb.y0, bb.y1)

        # test that when inverting the y-axis, the y-coordinates of the bounding box change
        viz.invert_yaxis()
        bb = util.get_bb(viz.figure, viz.axes, text)
        self.assertGreater(bb.y0, bb.y1)

        # create overlapping bounding boxes that emulate an inverted y-axis and test
        bb1, bb2 = Bbox(((0, 1), (1, 0))), Bbox(((0.5, 1.5), (1.5, 0.5)))
        self.assertTrue(util.overlapping_bb(bb1, bb2))
コード例 #22
0
    def test_footnote_redraw_bottom_xaxes(self):
        """
        Test that when the x-axis label is at the bottom, the footnote moves down.
        """

        viz = drawable.Drawable(plt.figure(figsize=(10, 5)))
        """
        Temporarily move the x-axis label and ticks to the top.
        """
        viz.axes.xaxis.set_label_position('top')
        viz.axes.xaxis.tick_top()
        viz.axes.spines['top'].set_visible(True)
        viz.axes.spines['bottom'].set_visible(False)
        """
        Create the footnote now.
        """
        footnote = viz.set_footnote("sample footnote")
        footnote_bb = footnote.get_virtual_bb(transform=viz.axes.transAxes)
        self.assertFalse(
            util.overlapping_bb(footnote_bb,
                                viz._get_xlabel(transform=viz.axes.transAxes)))
        """
        Move the x-axis label and ticks back to the bottom.
        """
        viz.axes.xaxis.set_label_position('bottom')
        viz.axes.xaxis.tick_bottom()
        viz.axes.spines['top'].set_visible(False)
        viz.axes.spines['bottom'].set_visible(True)
        """
        After adding a label, the footnote should move down.
        """
        viz.set_xlabel('label')
        viz.redraw()
        self.assertGreater(
            footnote_bb.y1,
            viz.footnote.get_virtual_bb(transform=viz.axes.transAxes).y1)