[812b901] | 1 | """ |
---|
| 2 | Unit tests for specific models using numpy array input |
---|
| 3 | @author: Gervaise B Alina/ UTK |
---|
| 4 | """ |
---|
| 5 | |
---|
| 6 | import unittest, time, math, numpy |
---|
| 7 | |
---|
| 8 | |
---|
| 9 | class TestSphere(unittest.TestCase): |
---|
[eba9885] | 10 | """ Unit tests for sphere model using evalDistribution function """ |
---|
[812b901] | 11 | |
---|
| 12 | def setUp(self): |
---|
| 13 | from sans.models.SphereModel import SphereModel |
---|
| 14 | self.comp = SphereModel() |
---|
| 15 | self.x = numpy.array([1.0,2.0,3.0, 4.0]) |
---|
| 16 | self.y = self.x +1 |
---|
| 17 | |
---|
| 18 | def test1D(self): |
---|
| 19 | """ Test 1D model for a sphere with vector as input""" |
---|
[eba9885] | 20 | answer = numpy.array([5.63877831e-05,2.57231782e-06,2.73704050e-07,2.54229069e-08]) |
---|
[812b901] | 21 | |
---|
[eba9885] | 22 | |
---|
| 23 | testvector= self.comp.evalDistribution(self.x) |
---|
[812b901] | 24 | |
---|
| 25 | self.assertAlmostEqual(len(testvector),4) |
---|
| 26 | for i in xrange(len(answer)): |
---|
| 27 | self.assertAlmostEqual(testvector[i],answer[i]) |
---|
| 28 | |
---|
| 29 | def test1D_1(self): |
---|
| 30 | """ Test 2D model for a sphere with scalar as input""" |
---|
| 31 | self.assertAlmostEqual(self.comp.run(1.0),5.63877831e-05, 4) |
---|
| 32 | |
---|
| 33 | def test1D_2(self): |
---|
| 34 | """ Test 2D model for a sphere for 2 scalar """ |
---|
| 35 | self.assertAlmostEqual(self.comp.run([1.0, 1.3]), 56.3878e-06, 4) |
---|
[eba9885] | 36 | |
---|
| 37 | def test1D_3(self): |
---|
| 38 | """ Test 2D model for a Shpere for 2 vectors as input """ |
---|
| 39 | x= numpy.reshape(self.x, [len(self.x),1]) |
---|
| 40 | y= numpy.reshape(self.y, [1,len(self.y)]) |
---|
| 41 | vect = self.comp.evalDistribution([x,y]) |
---|
| 42 | self.assertAlmostEqual(vect[0][0],9.2985e-07, 4) |
---|
| 43 | self.assertAlmostEqual(vect[len(self.x)-1][len(self.y)-1],1.3871e-08, 4) |
---|
| 44 | |
---|
[812b901] | 45 | |
---|
| 46 | class TestCylinder(unittest.TestCase): |
---|
[eba9885] | 47 | """ Unit tests for Cylinder model using evalDistribution function """ |
---|
[812b901] | 48 | |
---|
| 49 | def setUp(self): |
---|
| 50 | from sans.models.CylinderModel import CylinderModel |
---|
| 51 | self.comp = CylinderModel() |
---|
| 52 | self.x = numpy.array([1.0,2.0,3.0, 4.0]) |
---|
[eba9885] | 53 | self.y = self.x +1 |
---|
[812b901] | 54 | |
---|
| 55 | def test1D(self): |
---|
[eba9885] | 56 | """ Test 1D model for a cylinder with vector as input""" |
---|
| 57 | |
---|
[812b901] | 58 | answer = numpy.array([1.98860592e-04,7.03686335e-05,2.89144683e-06,2.04282827e-06]) |
---|
[eba9885] | 59 | |
---|
| 60 | testvector= self.comp.evalDistribution(self.x) |
---|
[812b901] | 61 | self.assertAlmostEqual(len(testvector),4) |
---|
| 62 | for i in xrange(len(answer)): |
---|
| 63 | self.assertAlmostEqual(testvector[i],answer[i]) |
---|
| 64 | |
---|
| 65 | def test1D_1(self): |
---|
[eba9885] | 66 | """ Test 2D model for a cylinder with scalar as input""" |
---|
| 67 | self.assertAlmostEqual(self.comp.run(0.2), 0.041761386790780453, 4) |
---|
[812b901] | 68 | |
---|
| 69 | def test1D_2(self): |
---|
[eba9885] | 70 | """ Test 2D model of a cylinder """ |
---|
| 71 | self.comp.setParam('cyl_theta', 1.0) |
---|
| 72 | self.comp.setParam('cyl_phi', 1.0) |
---|
| 73 | self.assertAlmostEqual(self.comp.run([0.2, 2.5]), |
---|
| 74 | 0.038176446608393366, 4) |
---|
[812b901] | 75 | |
---|
| 76 | def test1D_3(self): |
---|
[eba9885] | 77 | """ Test 2D model for a cylinder for 2 vectors as input """ |
---|
| 78 | x= numpy.reshape(self.x, [len(self.x),1]) |
---|
| 79 | y= numpy.reshape(self.y, [1,len(self.y)]) |
---|
| 80 | vect = self.comp.evalDistribution([x,y]) |
---|
| 81 | |
---|
| 82 | self.assertAlmostEqual(vect[0][0],5.06121018e-08,4) |
---|
| 83 | self.assertAlmostEqual(vect[len(self.x)-1][len(self.y)-1],2.5978e-11, 4) |
---|
[812b901] | 84 | |
---|
| 85 | |
---|
| 86 | |
---|
| 87 | if __name__ == '__main__': |
---|
| 88 | unittest.main() |
---|