1#!/usr/bin/env python 2# Author : Pierre Schnizer 3import unittest 4 5import pygsl 6from pygsl import fit 7from pygsl import multifit 8import pygsl._numobj as Numeric 9 10 11class FitTest(unittest.TestCase): 12 _eps = 1e-8 13 def setUp(self): 14 self.a = 1000.1 15 self.b = 100 16 self.x = Numeric.arange(100) 17 self.y = self.a + self.b * self.x 18 self.w = Numeric.ones(100) * 1000000 19 20 21 def test_0_linear(self): 22 c0, c1, c00, c01, c10, sumsq = fit.linear(self.x, self.y) 23 assert(Numeric.absolute(c0 - self.a)<self._eps) 24 assert(Numeric.absolute(c1 - self.b)<self._eps) 25 y, yerr = fit.est(1, c0, c1, c00, c01, c10) 26 assert(Numeric.absolute(y - (self.a+self.b*1)) < self._eps) 27 assert(Numeric.absolute(yerr) < self._eps) 28 29 30 31 def test_1_wlinear(self): 32 c0, c1, c00, c01, c10, chisq = fit.wlinear(self.x, self.w, self.y) 33 assert(Numeric.absolute(c0 - self.a)<self._eps) 34 assert(Numeric.absolute(c1 - self.b)<self._eps) 35 y, yerr = fit.est(1, c0, c1, c00, c01, c10) 36 assert(Numeric.absolute(y - (self.a+self.b*1)) < self._eps) 37 38class FitMulTest(unittest.TestCase): 39 _eps = 1e-8 40 def setUp(self): 41 self.a = 0.0 42 self.b = 100 43 self.x = Numeric.arange(100) 44 self.y = self.a + self.b * self.x 45 self.w = Numeric.ones(100) * 1000000 46 47 48 def test_0_linear(self): 49 c1, c11, sumsq = fit.mul(self.x, self.y) 50 assert(Numeric.absolute(c1 - self.b)<self._eps) 51 y, yerr = fit.mul_est(1, c1, c11) 52 assert(Numeric.absolute(y - (self.a+self.b*1)) < self._eps) 53 assert(Numeric.absolute(yerr) < self._eps) 54 55 56 57 def test_1_wlinear(self): 58 c1, c11, chisq = fit.wmul(self.x, self.w, self.y) 59 assert(Numeric.absolute(c1 - self.b)<self._eps) 60 y, yerr = fit.mul_est(1, c1, c11) 61 assert(Numeric.absolute(y - (self.a+self.b*1)) < self._eps) 62 63 64 65class MultiFitTest(unittest.TestCase): 66 _eps = 1e-10 67 def setUp(self): 68 self.dim = 100 69 self.param = 2 70 self.a = 1000.1 71 self.b = 100 72 self.x = Numeric.arange(self.dim) 73 x = self.x 74 self.y = self.a + self.b * self.x 75 self.w = Numeric.ones((self.dim,)) 76 self.ws = multifit.linear_workspace(self.dim, self.param) 77 self.X = Numeric.transpose(Numeric.array((Numeric.ones(self.dim,), x))) 78 79 80 def test_linear(self): 81 c, cov, chisq = multifit.linear(self.X, self.y, self.ws) 82 assert(Numeric.absolute(c[0] - self.a) < self._eps) 83 assert(Numeric.absolute(c[1] - self.b) < self._eps) 84 85 def test_wlinear(self): 86 c, cov, chisq = multifit.wlinear(self.X, self.w, self.y, self.ws) 87 assert(Numeric.absolute(c[0] - self.a) < self._eps) 88 assert(Numeric.absolute(c[1] - self.b) < self._eps) 89 90 91if __name__ == '__main__': 92 unittest.main() 93