Пример #1
0
class TestFreeForm(unittest.TestCase):
    def setUp(self):
        SP.random.seed(1)
        self.n=4
        self.C = FreeFormCov(self.n)
        self.name = 'freeform'
        self.n_params=self.C.getNumberParams()
        params=SP.randn(self.n_params)
        self.C.setParams(params)

    def test_grad(self):
        def func(x, i):
            self.C.setParams(x)
            return self.C.K()

        def grad(x, i):
            self.C.setParams(x)
            return self.C.K_grad_i(i)

        x0 = self.C.getParams()
        err = mcheck_grad(func, grad, x0)

        np.testing.assert_almost_equal(err, 0., decimal=6)

    def test_param_activation(self):
        self.assertEqual(len(self.C.getParams()), 10)
        self.C.act_K = False
        self.assertEqual(len(self.C.getParams()), 0)

        self.C.setParams(np.array([]))
        with self.assertRaises(ValueError):
            self.C.setParams(np.array([0]))

        with self.assertRaises(ValueError):
            self.C.K_grad_i(0)
Пример #2
0
class TestFreeForm(unittest.TestCase):
    def setUp(self):
        SP.random.seed(1)
        self.n = 4
        self.C = FreeFormCov(self.n)
        self.name = 'freeform'
        self.n_params = self.C.getNumberParams()
        params = SP.randn(self.n_params)
        self.C.setParams(params)

    def test_grad(self):
        def func(x, i):
            self.C.setParams(x)
            return self.C.K()

        def grad(x, i):
            self.C.setParams(x)
            return self.C.K_grad_i(i)

        x0 = self.C.getParams()
        err = mcheck_grad(func, grad, x0)

        np.testing.assert_almost_equal(err, 0., decimal=6)

    def test_param_activation(self):
        self.assertEqual(len(self.C.getParams()), 10)
        self.C.act_K = False
        self.assertEqual(len(self.C.getParams()), 0)

        self.C.setParams(np.array([]))
        with self.assertRaises(ValueError):
            self.C.setParams(np.array([0]))

        with self.assertRaises(ValueError):
            self.C.K_grad_i(0)
if __name__=='__main__':

    P = 4

    pdb.set_trace()

    # define col covariances
    C = FreeFormCov(P, jitter=0)
    C0 = covariance.freeform(P)

    t1 = 0
    t0 = 0

    for ti in range(1000):
        C.setRandomParams()
        C0.setParams(C.getParams())

        for i in range(int(0.5*P*(P+1))):
            _t0 = time.time()
            C0.Kgrad_param(i)
            _t1 = time.time()
            C.K_grad_i(i)
            _t2 = time.time()
            t0 += _t1-_t0
            t1 += _t2-_t1

    print 'old:', t0
    print 'new:', t1