def test_histogram():
        'It test our histogram implementation'
        numbers = [0, 1, 2, 3, 4, 4, 4, 5, 6, 7, 8, 9, 10]
        bins    = 5
        numpy_distrib = ([2, 2, 4, 2, 3], [0, 2, 4, 6, 8, 10])
        our_distrib = histogram(numbers, bins=bins)
        for num1, num2 in zip(numpy_distrib[0], our_distrib[0]):
            assert num1 == num2
        for num1, num2 in zip(numpy_distrib[1], our_distrib[1]):
            assert num1 == num2
            assert isinstance(num2, int)

        #with floats
        numbers = [0.0, 1, 2, 3, 4, 4, 4, 5, 6, 7, 8, 9, 10]
        bins    = 5
        numpy_distrib = ([2, 2, 4, 2, 3], [0.0, 2.0, 4.0, 6.0, 8.0, 10.0])
        our_distrib = histogram(numbers, bins=bins)

        for num1, num2 in zip(numpy_distrib[0], our_distrib[0]):
            assert num1 == num2
        for num1, num2 in zip(numpy_distrib[1], our_distrib[1]):
            assert num1 == num2
            assert isinstance(num2, float)

        #now we draw the histogram
        #draw_histogram(our_distrib[0], our_distrib[1])
        png_file = tempfile.NamedTemporaryFile(suffix='.png')
        #draw_histogram(our_distrib[0], our_distrib[1], fhand=png_file)
        #assert 'PNG' in open(png_file.name).read(4)
        svg_file = tempfile.NamedTemporaryFile(suffix='.svg')
        draw_histogram(our_distrib[0], our_distrib[1], fhand=svg_file)
        svg_file.flush()
        assert 'xml' in open(svg_file.name).read(5).lower()
Пример #2
0
    def _do_diff_distrib_for_numbers(numbers, plot_fhand, distrib_fhand,
                                     dist_type):
        'It creates the diff distribution for the given numbers'

        max_ = max(numbers[0].max, numbers[1].max)
        min_ = min(numbers[0].min, numbers[1].min)

        bins1 = len(numbers[0].calculate_bin_edges(min_, max_)) - 1
        bins2 = len(numbers[1].calculate_bin_edges(min_, max_)) - 1
        bins = bins2 if bins1 >= bins2 else bins1

        # to get the difference we need both distribs
        distrib1 = numbers[0].calculate_distribution(max_=max_, min_=min_,
                                                     bins=bins)
        distrib2 = numbers[1].calculate_distribution(max_=max_, min_=min_,
                                                     bins=bins)
        if not distrib1 or not distrib2:
            return

        # now a subtract distrib1 from distrib2
        diff_distrib   = []
        diff_bin_edges = distrib1['bin_edges']
        for i in range(len(distrib1['distrib'])):
            diff = distrib1['distrib'][i] - distrib2['distrib'][i]
            diff_distrib.append(diff)

        #now we write the result
        write_distribution(distrib_fhand, diff_distrib, diff_bin_edges)

        labels = PLOT_LABELS[dist_type]
        draw_histogram(diff_distrib, diff_bin_edges,
                       title=labels['title'], xlabel=labels['xlabel'],
                       ylabel=labels['ylabel'],
                       fhand=plot_fhand)