1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
|
#!/usr/bin/env python
"""Unit tests for fit function.
"""
from numpy import array, arange, exp
from numpy.random import rand
from cogent.util.unit_test import TestCase, main
from cogent.maths.fit_function import fit_function
__author__ = "Antonio Gonzalez Pena"
__copyright__ = "Copyright 2007-2012, The Cogent Project"
__credits__ = ["Antonio Gonzalez Pena"]
__license__ = "GPL"
__version__ = "1.5.3"
__maintainer__ = "Antonio Gonzalez Pena"
__email__ = "antgonza@gmail.com"
__status__ = "Prototype"
class fit_function_test(TestCase):
"""Tests of top-level fit functions."""
def test_constant(self):
"""test constant approximation"""
# defining our fitting function
def f(x,a):
return a[0]
exp_params = [2]
x = arange(-1,1,.01)
y = f(x, exp_params)
y_noise = y + rand(len(x))
params = fit_function(x, y_noise, f, 1, 5)
self.assertFloatEqual(params, exp_params , .5)
def test_linear(self):
"""test linear approximation"""
# defining our fitting function
def f(x,a):
return (a[0]+x*a[1])
exp_params = [2, 10]
x = arange(-1,1,.01)
y = f(x, exp_params)
y_noise = y + rand(len(y))
params = fit_function(x, y_noise, f, 2, 5)
self.assertFloatEqual(params, exp_params , .5)
def test_exponential(self):
"""test exponential approximation"""
# defining our fitting function
def f(x,a):
return exp(a[0]+x*a[1])
exp_params = [2, 10]
x = arange(-1,1,.01)
y = f(x, exp_params)
y_noise = y + rand(len(y))
params = fit_function(x, y_noise, f, 2, 5)
self.assertFloatEqual(params, exp_params , .5)
if __name__ == '__main__':
main()
|