class TestSparseHistogram(object):
    def setup(self):
        data = [(0.0, 0.1), (0.2, 0.7), (0.3, 0.6), (0.6, 0.9)]
        self.histo = SparseHistogram(bin_widths=(0.5, 0.3),
                                     left_bin_edges=(0.0, -0.1))
        self.histo.histogram(data)

    def test_correct(self):
        correct_results = collections.Counter({
            (0, 0): 1,
            (0, 2): 2,
            (1, 3): 1
        })
        assert self.histo._histogram == correct_results

    def test_call(self):
        histo_fcn = self.histo()
        # voxels we have filled
        assert histo_fcn((0.25, 0.65)) == 2
        assert histo_fcn((0.01, 0.09)) == 1
        assert histo_fcn((0.61, 0.89)) == 1
        # empty voxel gives 0
        assert histo_fcn((2.00, 2.00)) == 0

    def test_normalized(self):
        raw_prob_normed = self.histo.normalized(raw_probability=True)
        assert pytest.approx(raw_prob_normed((0.25, 0.65))) == 0.5
        assert pytest.approx(raw_prob_normed((0.01, 0.09))) == 0.25
        assert pytest.approx(raw_prob_normed((0.61, 0.89))) == 0.25
        normed_fcn = self.histo.normalized()
        assert pytest.approx(normed_fcn((0.25, 0.65))) == old_div(0.5, 0.15)
        assert pytest.approx(normed_fcn((0.01, 0.09))) == old_div(0.25, 0.15)
        assert pytest.approx(normed_fcn((0.61, 0.89))) == old_div(0.25, 0.15)
class testSparseHistogram(object):
    def setup(self):
        data = [(0.0, 0.1), (0.2, 0.7), (0.3, 0.6), (0.6, 0.9)]
        self.histo = SparseHistogram(bin_widths=(0.5, 0.3),
                                     left_bin_edges=(0.0, -0.1))
        self.histo.histogram(data)

    def test_correct(self):
        correct_results = collections.Counter({
            (0, 0): 1,
            (0, 2): 2,
            (1, 3): 1
        })
        assert_equal(self.histo._histogram, correct_results)

    def test_call(self):
        histo_fcn = self.histo()
        # voxels we have filled
        assert_equal(histo_fcn((0.25, 0.65)), 2)
        assert_equal(histo_fcn((0.01, 0.09)), 1)
        assert_equal(histo_fcn((0.61, 0.89)), 1)
        # empty voxel gives 0
        assert_equal(histo_fcn((2.00, 2.00)), 0)

    def test_normalized(self):
        raw_prob_normed = self.histo.normalized(raw_probability=True)
        assert_almost_equal(raw_prob_normed((0.25, 0.65)), 0.5)
        assert_almost_equal(raw_prob_normed((0.01, 0.09)), 0.25)
        assert_almost_equal(raw_prob_normed((0.61, 0.89)), 0.25)
        normed_fcn = self.histo.normalized()
        assert_almost_equal(normed_fcn((0.25, 0.65)), 0.5 / 0.15)
        assert_almost_equal(normed_fcn((0.01, 0.09)), 0.25 / 0.15)
        assert_almost_equal(normed_fcn((0.61, 0.89)), 0.25 / 0.15)
class TestSparseHistogram(object):
    def setup(self):
        data = [(0.0, 0.1), (0.2, 0.7), (0.3, 0.6), (0.6, 0.9)]
        self.histo = SparseHistogram(bin_widths=(0.5, 0.3),
                                     left_bin_edges=(0.0, -0.1))
        self.histo.histogram(data)

    def test_correct(self):
        correct_results = collections.Counter({
            (0, 0) : 1,
            (0, 2) : 2,
            (1, 3) : 1
        })
        assert_equal(self.histo._histogram, correct_results)

    def test_call(self):
        histo_fcn = self.histo()
        # voxels we have filled
        assert_equal(histo_fcn((0.25, 0.65)), 2)
        assert_equal(histo_fcn((0.01, 0.09)), 1)
        assert_equal(histo_fcn((0.61, 0.89)), 1)
        # empty voxel gives 0
        assert_equal(histo_fcn((2.00, 2.00)), 0)

    def test_normalized(self):
        raw_prob_normed = self.histo.normalized(raw_probability=True)
        assert_almost_equal(raw_prob_normed((0.25, 0.65)), 0.5)
        assert_almost_equal(raw_prob_normed((0.01, 0.09)), 0.25)
        assert_almost_equal(raw_prob_normed((0.61, 0.89)), 0.25)
        normed_fcn = self.histo.normalized()
        assert_almost_equal(normed_fcn((0.25, 0.65)), old_div(0.5,0.15))
        assert_almost_equal(normed_fcn((0.01, 0.09)), old_div(0.25,0.15))
        assert_almost_equal(normed_fcn((0.61, 0.89)), old_div(0.25,0.15))
class TestSparseHistogram(object):
    def setup(self):
        data = [(0.0, 0.1), (0.2, 0.7), (0.3, 0.6), (0.6, 0.9)]
        self.histo = SparseHistogram(bin_widths=(0.5, 0.3),
                                     left_bin_edges=(0.0, -0.1))
        self.histo.histogram(data)

    def test_correct(self):
        correct_results = collections.Counter({
            (0, 0): 1,
            (0, 2): 2,
            (1, 3): 1
        })
        assert self.histo._histogram == correct_results

    def test_call(self):
        histo_fcn = self.histo()
        # voxels we have filled
        assert histo_fcn((0.25, 0.65)) == 2
        assert histo_fcn((0.01, 0.09)) == 1
        assert histo_fcn((0.61, 0.89)) == 1
        # empty voxel gives 0
        assert histo_fcn((2.00, 2.00)) == 0

    def test_normalized(self):
        raw_prob_normed = self.histo.normalized(raw_probability=True)
        assert pytest.approx(raw_prob_normed((0.25, 0.65))) == 0.5
        assert pytest.approx(raw_prob_normed((0.01, 0.09))) == 0.25
        assert pytest.approx(raw_prob_normed((0.61, 0.89))) == 0.25
        normed_fcn = self.histo.normalized()
        assert pytest.approx(normed_fcn((0.25, 0.65))) == old_div(0.5, 0.15)
        assert pytest.approx(normed_fcn((0.01, 0.09))) == old_div(0.25, 0.15)
        assert pytest.approx(normed_fcn((0.61, 0.89))) == old_div(0.25, 0.15)

    def test_mangled_input(self):
        # Sometimes singleton cvs are not unpacked properly
        data = ([0.0], [0.1])
        # This line might return mangled output
        out = self.histo.map_to_bins(data)
        # This raises on modern numpy if this is not 1D
        _ = max(out)
 def setup(self):
     data = [(0.0, 0.1), (0.2, 0.7), (0.3, 0.6), (0.6, 0.9)]
     histo = SparseHistogram(bin_widths=(0.5, 0.3),
                             left_bin_edges=(0.0, -0.1))
     histo.histogram(data)
     self.plotter = HistogramPlotter2D(histo)
 def setup(self):
     data = [(0.0, 0.1), (0.2, 0.7), (0.3, 0.6), (0.6, 0.9)]
     self.histo = SparseHistogram(bin_widths=(0.5, 0.3),
                                  left_bin_edges=(0.0, -0.1))
     self.histo.histogram(data)
 def setup(self):
     data = [(0.0, 0.1), (0.2, 0.7), (0.3, 0.6), (0.6, 0.9)]
     histo = SparseHistogram(bin_widths=(0.5, 0.3),
                             left_bin_edges=(0.0, -0.1))
     histo.histogram(data)
     self.plotter = HistogramPlotter2D(histo)
 def setup(self):
     data = [(0.0, 0.1), (0.2, 0.7), (0.3, 0.6), (0.6, 0.9)]
     self.histo = SparseHistogram(bin_widths=(0.5, 0.3),
                                  left_bin_edges=(0.0, -0.1))
     self.histo.histogram(data)