示例#1
0
    def test_asline(self):
        h = Histogram(10,[0,10])
        h.data = np.array([1,2,3,4,5,0,1,2,9,0],dtype=np.int64)
        x,y,ext = h.asline()
        assert_array_almost_equal(x,[0,1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8,9,9,10])
        assert_array_almost_equal(y,[1,1,2,2,3,3,4,4,5,5,0,0,1,1,2,2,9,9,0, 0])
        assert_array_almost_equal(ext,[0,10,0,9])

        h = Histogram(10,[0,10])
        h.data = np.array([0,2,3,4,5,0,1,2,9,0],dtype=np.int64)
        x,y,ext = h.asline()
        assert_array_almost_equal(x,[0,1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8,9,9,10])
        assert_array_almost_equal(y,[0,0,2,2,3,3,4,4,5,5,0,0,1,1,2,2,9,9,0, 0])
        assert_array_almost_equal(ext,[0,10,0,9])

        h = Histogram(10,[0,10])
        h.data = np.array([0,2,-4,4,5,0,1,2,9,0],dtype=np.int64)
        x,y,ext = h.asline()
        assert_array_almost_equal(x,[0,1,1,2, 2, 3,3,4,4,5,5,6,6,7,7,8,8,9,9,10])
        assert_array_almost_equal(y,[0,0,2,2,-4,-4,4,4,5,5,0,0,1,1,2,2,9,9,0, 0])
        assert_array_almost_equal(ext,[0,10,-4,9])

        h = Histogram(10,[-1,9])
        h.data = np.array([-1,2,-4,4,5,0,1,2,9,-10],dtype=np.int64)
        x,y,ext = h.asline()
        assert_array_almost_equal(x,[-1, 0,0,1, 1, 2,2,3,3,4,4,5,5,6,6,7,7,8,  8,  9])
        assert_array_almost_equal(y,[-1,-1,2,2,-4,-4,4,4,5,5,0,0,1,1,2,2,9,9,-10,-10])
        assert_array_almost_equal(ext,[-1,9,-10,9])

        h = Histogram(10,[0,10])
        h.data = np.array([1,2,3,4,5,0,1,2,9,0],dtype=np.int64)

        x,y,ext = h.asline(2)
        assert_array_almost_equal(x,[2,3,3,4,4,5,5,6,6,7,7,8,8,9,9,10])
        assert_array_almost_equal(y,[3,3,4,4,5,5,0,0,1,1,2,2,9,9,0, 0])
        assert_array_almost_equal(ext,[2,10,0,9])

        x,y,ext = h.asline(1.9)
        assert_array_almost_equal(x,[2,3,3,4,4,5,5,6,6,7,7,8,8,9,9,10])
        assert_array_almost_equal(y,[3,3,4,4,5,5,0,0,1,1,2,2,9,9,0, 0])
        assert_array_almost_equal(ext,[2,10,0,9])

        x,y,ext = h.asline(2.9,7)
        assert_array_almost_equal(x,[3,4,4,5,5,6])
        assert_array_almost_equal(y,[4,4,5,5,0,0])
        assert_array_almost_equal(ext,[3,6,0,5])
示例#2
0
    def test_1dfit(self):
        p = [100, -20, 1, -0.2]
        h = Histogram(100, [0, 10])
        x = h.axes[0].bincenters
        h.data = poly(p)(x) + rand.normal(0, np.sqrt(p[0]), len(x))

        p0 = [1, 1, 1, 1]
        popt, pcov, ptest = h.fit(lambda x, *p: poly(p)(x), p0)

        assert np.allclose(popt, p, rtol=0.03, atol=0.5)
        assert pcov.shape == (len(p), len(p))
示例#3
0
    def test_1dfit(self):
        p = [100, -20, 1, -0.2]
        h = Histogram(100, [0, 10])
        x = h.axes[0].bincenters
        h.data = poly(p)(x) + rand.normal(0, np.sqrt(p[0]), len(x))

        p0 = [1, 1, 1, 1]
        popt, pcov, ptest = h.fit(lambda x, *p: poly(p)(x), p0)

        assert np.allclose(popt, p, rtol=0.03, atol=0.5)
        assert pcov.shape == (len(p), len(p))
示例#4
0
    def test_2dfit(self):
        fn = lambda xy, *p: poly(p[:4])(xy[0]) + poly([0] + list(p[4:]))(xy[1])
        p = [100, -20, 1, -0.2, 80, -5, 0.8]
        h = Histogram(100, [0, 10], 100, [0, 10])
        xy = h.grid
        h.data = fn(xy, *p)
        h.data += rand.normal(0, np.sqrt(p[0]), h.data.shape)

        p0 = [1, 1, 1, 1, 1, 1, 1]
        popt, pcov, ptest = h.fit(fn, p0)

        assert np.allclose(popt, p, rtol=0.01, atol=0.01)
        assert pcov.shape == (len(p), len(p))
示例#5
0
    def test_2dfit(self):
        fn = lambda xy, *p: poly(p[:4])(xy[0]) + poly([0] + list(p[4:]))(xy[1])
        p = [100, -20, 1, -0.2, 80, -5, 0.8]
        h = Histogram(100, [0, 10], 100, [0, 10])
        xy = h.grid
        h.data = fn(xy, *p)
        h.data += rand.normal(0, np.sqrt(p[0]), h.data.shape)

        p0 = [1, 1, 1, 1, 1, 1, 1]
        popt, pcov, ptest = h.fit(fn, p0)

        assert np.allclose(popt, p, rtol=0.01, atol=0.01)
        assert pcov.shape == (len(p), len(p))
示例#6
0
    def test_1dfit_zeros(self):
        p = [100, -20, 1, -0.2]
        h = Histogram(100, [0, 10])
        x = h.axes[0].bincenters
        h.data = poly(p)(x) + rand.normal(0, np.sqrt(p[0]), len(x))

        ii = rand.randint(0, h.axes[0].nbins, 5)
        h.data[ii] = 0

        p0 = [1, 1, 1, 1]
        popt, pcov, ptest = h.fit(lambda x, *p: poly(p)(x), p0)

        assert np.allclose(popt, p, rtol=0.1, atol=1.0)
        assert pcov.shape == (len(p), len(p))
示例#7
0
    def test_1dfit_zeros(self):
        p = [100, -20, 1, -0.2]
        h = Histogram(100, [0, 10])
        x = h.axes[0].bincenters
        h.data = poly(p)(x) + rand.normal(0, np.sqrt(p[0]), len(x))

        ii = rand.randint(0, h.axes[0].nbins, 5)
        h.data[ii] = 0

        p0 = [1, 1, 1, 1]
        popt, pcov, ptest = h.fit(lambda x, *p: poly(p)(x), p0)

        assert np.allclose(popt, p, rtol=0.1, atol=1.)
        assert pcov.shape == (len(p), len(p))