Exemple #1
0
    def test_draw_bars_return_rectangles(self):
        """
        Test that when drawing bars, they are returned as matplotlib rectangles.
        """

        viz = drawable.Drawable(plt.figure(figsize=(10, 10)))
        bar = Bar100(viz)
        values = bar._to_dict(list(range(10)))
        bars = bar._draw_bars(values)
        self.assertTrue(bars)
        self.assertTrue(all( matplotlib.patches.Rectangle == type(bar) for bar in bars ))
Exemple #2
0
    def test_draw_bars_override_style(self):
        """
        Test that when drawing bars, the style can be overriden.
        """

        viz = drawable.Drawable(plt.figure(figsize=(10, 10)))
        bar = Bar100(viz)
        values = bar._to_dict([{ 'value': 10, 'style': { 'color': '#0000FF' } }, { 'value': 10 }])
        bars = bar._draw_bars(values, color='#FF0000')
        self.assertEqual(bars[0].get_facecolor(), (0, 0, 1, 1))
        self.assertEqual(bars[1].get_facecolor(), (1, 0, 0, 1))
    def test_get_elevation_same_x(self):
        """
		Test that when getting the elevation of two points with the same x-coordinate, an angle of 90 degrees is returned.
		"""

        viz = drawable.Drawable(plt.figure(figsize=(10, 5)))
        graph = Graph(viz)
        self.assertEqual(math.pi / 2., graph._get_elevation((0, 0), (0, 1)))
        self.assertEqual(math.pi / 2., graph._get_elevation((1, 1), (1, 2)))
        self.assertEqual(math.pi / 2., graph._get_elevation((0, 0), (0, -1)))
        self.assertEqual(math.pi / 2., graph._get_elevation((1, 1), (1, -2)))
Exemple #4
0
    def test_annotate_marker_copy(self):
        """
		Test that when drawing a marker and a marker style is given as a dictionary, it is not overwritten.
		"""

        marker = {}
        annotation_style = {'color': 'blue'}

        viz = drawable.Drawable(plt.figure(figsize=(10, 5)))
        viz.annotate('Text', (0, 0), 0, marker=marker, **annotation_style)
        self.assertEqual({}, marker)
    def test_label(self):
        """
		Test that when a label is drawn with normal alignment, it is drawn at the given position.
		"""

        viz = LabelledVisualization(
            drawable.Drawable(plt.figure(figsize=(10, 10))))
        label = viz.draw_label('A', 4, 10)
        self.assertEqual(4, label.get_virtual_bb().x0)
        self.assertEqual(
            10, (label.get_virtual_bb().y0 + label.get_virtual_bb().y1) / 2.)
    def test_get_direction_symmetric(self):
        """
		Test that the distance between two points is symmetric.
		"""

        viz = drawable.Drawable(plt.figure(figsize=(10, 5)))
        graph = Graph(viz)
        self.assertEqual(round(graph._get_distance((-1, -1), (0, 0)), 5),
                         round(graph._get_distance((0, 0), (-1, -1)), 5))
        self.assertEqual(round(graph._get_distance((-2, -1), (0, 0)), 5),
                         round(graph._get_distance((0, 0), (-2, -1)), 5))
    def test_to_100_fold(self):
        """
		Test that when providing a minimum percentage that fills up the bar, all returned values are the same.
		"""

        viz = drawable.Drawable(plt.figure(figsize=(10, 10)))
        bar = Bar100(viz)
        self.assertTrue(
            all(
                round(percentage, 7) == round(1 / 3 * 100, 7)
                for percentage in bar._to_100([10, 0, 5], 1 / 3 * 100)))
    def test_to_100_min_percentage(self):
        """
		Test that when providing a minimum percentage, all returned percentages meet that value.
		"""

        viz = drawable.Drawable(plt.figure(figsize=(10, 10)))
        bar = Bar100(viz)
        self.assertTrue(
            all(
                round(percentage, 10) >= 10
                for percentage in bar._to_100([1, 0, 1], 10)))
Exemple #9
0
    def test_draw_correct_rows_uneven(self):
        """
        Test that when drawing a population, the number of rows in each column is equal to the given number of rows, except for the last column when the population is not a factor of the rows.
        """

        viz = drawable.Drawable(plt.figure(figsize=(10, 10)))
        rows = 8
        drawn = viz.draw_population(30, rows, '')
        self.assertTrue(all(rows == len(column) for column in drawn[:-1]))
        self.assertTrue(
            all(len(drawn[-1]) < len(column) for column in drawn[:-1]))
	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()))
	def test_x_pad_justify_end(self):
		"""
		Test that when padding is applied with `justify-end` alignment, the block moves to the left.
		"""

		text = 'Memphis Depay, commonly known simply as Memphis, is a Dutch professional footballer and music artist who plays as a forward and captains French club Lyon and plays for the Netherlands national team. He is known for his pace, ability to cut inside, dribbling, distance shooting and ability to play the ball off the ground.'
		viz = drawable.Drawable(plt.figure(figsize=(10, 10)))
		annotation = Annotation(viz)
		annotation.draw(text, (0, 1), 0, pad=0.2, align='justify-end')
		bb = annotation.get_virtual_bb()
		self.assertGreaterEqual(0.8, round(bb.x1, 10))
Exemple #12
0
    def test_draw_legend_no_general_label(self):
        """
        Test that when no general label is given, no legend is added.
        """

        viz = drawable.Drawable(plt.figure(figsize=(10, 10)))
        drawn = viz.draw_population(10, 5, '', label='')
        self.assertEqual([], viz.legend.lines[0])

        drawn = viz.draw_population(10, 5, '', label=None)
        self.assertEqual([], viz.legend.lines[0])
Exemple #13
0
    def test_draw_min_percentage_exceeds_100(self):
        """
		Test that when the minimum percentage multiplied by the number of values exceeds 100, drawing raises a ValueError.
		"""

        viz = drawable.Drawable(plt.figure(figsize=(10, 10)))
        bar = Bar100(viz)
        self.assertRaises(ValueError,
                          bar.draw, [1, 1],
                          'label',
                          min_percentage=75)
Exemple #14
0
    def test_to_dict_other_keys(self):
        """
		Test that when converting values to dictionaries, any additional keys are retained.
		"""

        viz = drawable.Drawable(plt.figure(figsize=(10, 10)))
        bar = Bar100(viz)
        values = [{'value': 2, 'style': {'color': 'blue'}, 'other': 23}]
        dicts = bar._to_dict(values)
        self.assertTrue('other' in dicts[0])
        self.assertEqual(23, dicts[0]['other'])
    def test_text(self):
        """
		Test that the text is written correctly.
		"""

        text = 'Memphis Depay, commonly known simply as Memphis, is a Dutch professional footballer and music artist who plays as a forward and captains French club Lyon and plays for the Netherlands national team.'
        viz = drawable.Drawable(plt.figure(figsize=(10, 5)))
        lines = viz.draw_text_annotation(text)

        drawn_text = self._reconstruct_text(lines)
        self.assertEqual(text, drawn_text)
Exemple #16
0
    def test_draw_min_percentage_above_100(self):
        """
		Test that when the minimum percentage is above 100, drawing raises a ValueError.
		"""

        viz = drawable.Drawable(plt.figure(figsize=(10, 10)))
        bar = Bar100(viz)
        self.assertRaises(ValueError,
                          bar.draw, [1],
                          'label',
                          min_percentage=101)
    def test_align_justify_right(self):
        """
		Test that when justifying text with the last line being right-aligned, the last line ends at the farthest right.
		"""

        text = 'Memphis Depay, commonly known simply as Memphis, is a Dutch professional footballer and music artist who plays as a forward and captains French club Lyon and plays for the Netherlands national team. He is known for his pace, ability to cut inside, dribbling, distance shooting and ability to play the ball off the ground.'
        viz = drawable.Drawable(plt.figure(figsize=(10, 10)))
        lines = viz.draw_text_annotation(text, align='justify-end')

        bb = util.get_bb(viz.figure, viz.axis, lines[0][-1][-1])
        self.assertEqual(viz.axis.get_xlim()[1], bb.x1)
    def test_align_invalid(self):
        """
		Test that when an invalid alignment is given, a :class:`~ValueError` is raised.
		"""

        text = 'Memphis Depay, commonly known simply as Memphis, is a Dutch professional footballer and music artist who plays as a forward and captains French club Lyon and plays for the Netherlands national team. He is known for his pace, ability to cut inside, dribbling, distance shooting and ability to play the ball off the ground.'
        viz = drawable.Drawable(plt.figure(figsize=(10, 10)))
        self.assertRaises(ValueError,
                          viz.draw_text_annotation,
                          text,
                          align='invalid')
	def test_y_pad_bottom(self):
		"""
		Test that when applying padding with `bottom` vertical alignment, the block moves up.
		"""

		text = 'Memphis Depay, commonly known simply as Memphis, is a Dutch professional footballer and music artist who plays as a forward and captains French club Lyon and plays for the Netherlands national team. He is known for his pace, ability to cut inside, dribbling, distance shooting and ability to play the ball off the ground.'
		viz = drawable.Drawable(plt.figure(figsize=(10, 10)))
		annotation = Annotation(viz)
		annotation.draw(text, (0, 1), 0, pad=0.2, va='bottom')
		bb = annotation.get_virtual_bb()
		self.assertEqual(0.2, round(bb.y0, 10))
Exemple #20
0
    def test_draw_save_multiple_population(self):
        """
        Test that when drawing multiple populations, all of them are saved in the class.
        """

        viz = drawable.Drawable(plt.figure(figsize=(10, 10)))
        pop1 = viz.draw_population(5, 10, '', height=1)
        self.assertEqual([pop1], viz.population.populations)

        pop2 = viz.draw_population(5, 10, '', height=1)
        self.assertEqual([pop1, pop2], viz.population.populations)
Exemple #21
0
    def test_draw_bars_100_pad(self):
        """
		Test that when drawing bars, they all end at 100 even when padding is given.
		"""

        viz = drawable.Drawable(plt.figure(figsize=(10, 10)))
        bar = Bar100(viz)
        values = bar._to_dict(list(range(10)))
        bars = bar._draw_bars(values, pad=1)
        self.assertEqual(
            100, round(util.get_bb(viz.figure, viz.axes, bars[-1]).x1, 7))
Exemple #22
0
    def test_draw_all_dict_values_zero(self):
        """
		Test that when drawing a dictionary list made up of only zeroes, a ValueError is raised.
		"""

        viz = drawable.Drawable(plt.figure(figsize=(10, 10)))
        self.assertRaises(ValueError, viz.draw_bar_100, [{
            'value': 0
        }], 'label')
        self.assertRaises(ValueError, viz.draw_bar_100, [{
            'value': 0
        }] * 10, 'label')
Exemple #23
0
    def test_to_dict_all_dicts_default_style(self):
        """
		Test that when converting a list of dictionaries to a dictionary, their default style is an empty dictionary if they do not have a style already.
		"""

        viz = drawable.Drawable(plt.figure(figsize=(10, 10)))
        bar = Bar100(viz)
        values = [{}, {'style': {'color': 'red'}}]
        dicts = bar._to_dict(values)
        self.assertEqual([{}, {
            'color': 'red'
        }], [value['style'] for value in dicts])
Exemple #24
0
    def test_draw_legend_inherits_general_bar_style(self):
        """
		Test that the legend labels inherit the general bar style.
		"""

        viz = drawable.Drawable(plt.figure(figsize=(10, 10)))
        bar = Bar100(viz)
        values = [{'value': 10, 'label': 'label'}]
        bars = bar.draw(values, 'label', color='#FF0000')
        self.assertEqual(1, len(viz.legend.lines[0]))
        _, annotation = viz.legend.lines[0][0]
        self.assertEqual('#FF0000', annotation.lines[0][0].get_color())
	def test_set_position_left(self):
		"""
		Test that when moving an annotation with a `left` horizontal alignment, all lines start at the given position.
		"""

		text = 'Memphis Depay, commonly known simply as Memphis, is a Dutch professional footballer and music artist who plays as a forward and captains French club Lyon and plays for the Netherlands national team. He is known for his pace, ability to cut inside, dribbling, distance shooting and ability to play the ball off the ground.'
		viz = drawable.Drawable(plt.figure(figsize=(10, 10)))
		annotation = Annotation(viz)
		annotation.draw(text, (0, 1), 0, align='left')
		annotation.set_position((2, 0), ha='left')
		for tokens in annotation.lines:
			self.assertEqual(2, round(util.get_bb(viz.figure, viz.axis, tokens[0]).x0, 10))
    def test_xpad_bounds(self):
        """
		Test that the left and right padding cannot occupy the entire axis.
		"""

        text = 'Memphis Depay, commonly known simply as Memphis, is a Dutch professional footballer and music artist who plays as a forward and captains French club Lyon and plays for the Netherlands national team. He is known for his pace, ability to cut inside, dribbling, distance shooting and ability to play the ball off the ground.'
        viz = drawable.Drawable(plt.figure(figsize=(10, 10)))
        self.assertRaises(ValueError,
                          viz.draw_text_annotation,
                          text,
                          lpad=0.5,
                          rpad=0.5)
	def test_set_position_horizontal_center(self):
		"""
		Test that when moving an annotation with a `center` horizontal alignment, the block is centered around the given point.
		"""

		text = 'Memphis Depay, commonly known simply as Memphis, is a Dutch professional footballer and music artist who plays as a forward and captains French club Lyon and plays for the Netherlands national team. He is known for his pace, ability to cut inside, dribbling, distance shooting and ability to play the ball off the ground.'
		viz = drawable.Drawable(plt.figure(figsize=(10, 10)))
		annotation = Annotation(viz)
		annotation.draw(text, (0, 1), 0, align='center')
		annotation.set_position((2, 0), ha='center')
		bb = annotation.get_virtual_bb()
		self.assertEqual(2, (bb.x0 + bb.x1)/2.)
Exemple #28
0
    def test_get_wordspacing(self):
        """
        Test that the wordspacing is based on the width of a letter.
        """

        viz = drawable.Drawable(plt.figure(figsize=(10, 5)))
        figure, axes = viz.figure, viz.axes
        wordspacing = text_util.get_wordspacing(figure, axes)

        token = axes.text(0, 0, '—')
        bb = util.get_bb(figure, axes, token)
        self.assertEqual(wordspacing, bb.width / 4.)
Exemple #29
0
    def test_to_100_same_order(self):
        """
		Test that when converting values to percentages, the percentages are returned in the same order as the input.
		"""

        viz = drawable.Drawable(plt.figure(figsize=(10, 10)))
        bar = Bar100(viz)
        values = [10, 20, 30]
        percentages = bar._to_100(values)
        self.assertEqual(100, sum(percentages))
        self.assertLess(percentages[0], percentages[1])
        self.assertLess(percentages[1], percentages[2])
	def test_set_position_bottom(self):
		"""
		Test that when moving an annotation with a `bottom` vertical alignment, the bottom of the last line is the given position.
		"""

		text = 'Memphis Depay, commonly known simply as Memphis, is a Dutch professional footballer and music artist who plays as a forward and captains French club Lyon and plays for the Netherlands national team. He is known for his pace, ability to cut inside, dribbling, distance shooting and ability to play the ball off the ground.'
		viz = drawable.Drawable(plt.figure(figsize=(10, 10)))
		annotation = Annotation(viz)
		annotation.draw(text, (0, 1), 0, va='bottom')
		annotation.set_position((0, 2), va='bottom')
		for token in annotation.lines[-1]:
			self.assertEqual(2, util.get_bb(viz.figure, viz.axis, token).y0)