[ae3ce4e] | 1 | """ |
---|
| 2 | Unit tests for specific models |
---|
| 3 | @author: Mathieu Doucet / UTK |
---|
| 4 | """ |
---|
| 5 | |
---|
| 6 | import unittest, time, math |
---|
| 7 | |
---|
| 8 | # Disable "missing docstring" complaint |
---|
| 9 | # pylint: disable-msg=C0111 |
---|
| 10 | # Disable "too many methods" complaint |
---|
| 11 | # pylint: disable-msg=R0904 |
---|
| 12 | # Disable "could be a function" complaint |
---|
| 13 | # pylint: disable-msg=R0201 |
---|
| 14 | |
---|
[a1b2471] | 15 | class TestSpherew(unittest.TestCase): |
---|
| 16 | """ Unit tests for sphere model """ |
---|
| 17 | |
---|
| 18 | def setUp(self): |
---|
| 19 | from sans.models.CoreFourShellModel import CoreFourShellModel |
---|
| 20 | from sans.models.CoreMultiShellModel import CoreMultiShellModel |
---|
| 21 | self.comp = CoreMultiShellModel(4) |
---|
| 22 | |
---|
| 23 | def test1D(self): |
---|
| 24 | """ Test 1D model for a sphere """ |
---|
| 25 | self.comp._set_dispersion() |
---|
[ae3ce4e] | 26 | |
---|
| 27 | class TestSphere(unittest.TestCase): |
---|
| 28 | """ Unit tests for sphere model """ |
---|
| 29 | |
---|
| 30 | def setUp(self): |
---|
| 31 | from sans.models.SphereModel import SphereModel |
---|
| 32 | self.comp = SphereModel() |
---|
| 33 | |
---|
| 34 | def test1D(self): |
---|
| 35 | """ Test 1D model for a sphere """ |
---|
[9ce41c6] | 36 | self.assertAlmostEqual(self.comp.run(1.0), 5.6387e-5, 4) |
---|
[ae3ce4e] | 37 | |
---|
| 38 | def test1D_2(self): |
---|
| 39 | """ Test 2D model for a sphere """ |
---|
[9ce41c6] | 40 | self.assertAlmostEqual(self.comp.run([1.0, 1.3]), 5.6387e-5, 4) |
---|
[ae3ce4e] | 41 | |
---|
| 42 | class TestCyl(unittest.TestCase): |
---|
| 43 | """Unit tests for cylinder""" |
---|
| 44 | |
---|
| 45 | def setUp(self): |
---|
| 46 | |
---|
| 47 | from sans.models.CylinderModel import CylinderModel |
---|
| 48 | self.comp = CylinderModel() |
---|
| 49 | |
---|
| 50 | def test1D(self): |
---|
| 51 | """ Test 1D model of a cylinder """ |
---|
| 52 | self.assertAlmostEqual(self.comp.run(0.2), 0.041761386790780453, 4) |
---|
| 53 | |
---|
| 54 | def testTime(self): |
---|
| 55 | """ Time profiling """ |
---|
| 56 | self.comp.run(2.0) |
---|
| 57 | t0 = time.clock() |
---|
| 58 | self.assertTrue(time.clock()-t0<1e-5) |
---|
| 59 | |
---|
| 60 | def test2D(self): |
---|
| 61 | """ Test 2D model of a cylinder """ |
---|
[f1abc57] | 62 | self.comp.setParam('cyl_theta', 1*180/math.pi) |
---|
| 63 | self.comp.setParam('cyl_phi', 1*180/math.pi) |
---|
[ae3ce4e] | 64 | self.assertAlmostEqual(self.comp.run([0.2, 2.5]), |
---|
[0abf7bf] | 65 | 0.038176446608393366, 4) |
---|
[ae3ce4e] | 66 | |
---|
| 67 | class TestGaussian(unittest.TestCase): |
---|
| 68 | """Unit tests for Gaussian function""" |
---|
| 69 | |
---|
| 70 | def setUp(self): |
---|
| 71 | from sans.models.Gaussian import Gaussian |
---|
| 72 | self.gauss= Gaussian() |
---|
| 73 | |
---|
| 74 | def test1D(self): |
---|
| 75 | self.gauss.setParam('scale', 2.0) |
---|
| 76 | self.gauss.setParam('center', 1.0) |
---|
| 77 | self.gauss.setParam('sigma', 1.0) |
---|
| 78 | self.assertEqual(self.gauss.run(1.0), 2.0) |
---|
| 79 | value = math.exp(-0.5) |
---|
| 80 | self.assertEqual(self.gauss.run(2.0), 2.0*value) |
---|
| 81 | |
---|
| 82 | def test2D(self): |
---|
| 83 | self.gauss.setParam('scale', 2.0) |
---|
| 84 | self.gauss.setParam('center', 1.0) |
---|
| 85 | self.gauss.setParam('sigma', 1.0) |
---|
| 86 | self.assertEqual(self.gauss.runXY([1.0,1.0]), 2.0*2.0) |
---|
| 87 | value = math.exp(-0.5) |
---|
| 88 | self.assertEqual(self.gauss.runXY([1.0,2.0]), 2.0*2.0*value) |
---|
| 89 | self.assertEqual(self.gauss.runXY([2.0,2.0]), 2.0*2.0*value*value) |
---|
| 90 | |
---|
| 91 | def test2Dphi(self): |
---|
| 92 | self.gauss.setParam('scale', 2.0) |
---|
| 93 | self.gauss.setParam('center', 1.0) |
---|
| 94 | self.gauss.setParam('sigma', 1.0) |
---|
| 95 | value = math.exp(-0.5) |
---|
[e52eeea] | 96 | self.assertAlmostEqual(self.gauss.run([math.sqrt(8.0), math.pi/4.0]), 2.0*2.0*value*value, 5) |
---|
[ae3ce4e] | 97 | |
---|
[5401f2a] | 98 | class TestStarPolymer(unittest.TestCase): |
---|
| 99 | """Unit tests for Star Polymer""" |
---|
| 100 | def setUp(self): |
---|
| 101 | from sans.models.StarPolymer import StarPolymer |
---|
| 102 | self.star = StarPolymer() |
---|
| 103 | |
---|
| 104 | def test1D(self): |
---|
| 105 | self.star.setParam('scale', 3.0) |
---|
| 106 | self.star.setParam('arms', 5.0) |
---|
| 107 | self.star.setParam('R2', 100.0) |
---|
| 108 | self.star.setParam('background', 1.0) |
---|
[1e40ed1] | 109 | self.assertAlmostEqual(self.star.run(0.01), 3.9900214150405686) |
---|
[5401f2a] | 110 | |
---|
| 111 | def test2D(self): |
---|
| 112 | x = 0.009539392014169456 |
---|
| 113 | y = 0.003 |
---|
| 114 | # together these have modulus 1 |
---|
| 115 | self.star.setParam('scale', 3.0) |
---|
| 116 | self.star.setParam('arms', 5.0) |
---|
| 117 | self.star.setParam('R2', 100.0) |
---|
| 118 | self.star.setParam('background', 1.0) |
---|
[1e40ed1] | 119 | self.assertAlmostEqual(self.star.runXY([x, y]), 3.9900214150405686) |
---|
[5401f2a] | 120 | |
---|
| 121 | def test2Dphi(self): |
---|
| 122 | self.star.setParam('scale', 3.0) |
---|
| 123 | self.star.setParam('arms', 5.0) |
---|
| 124 | self.star.setParam('R2', 100.0) |
---|
| 125 | self.star.setParam('background', 1.0) |
---|
| 126 | |
---|
[1e40ed1] | 127 | self.assertAlmostEqual(self.star.run([0.01, math.pi/4.0]), 3.9900214150405686) |
---|
[5401f2a] | 128 | |
---|
| 129 | |
---|
| 130 | |
---|
[ae3ce4e] | 131 | class TestLorentzian(unittest.TestCase): |
---|
| 132 | """Unit tests for Lorentzian function""" |
---|
| 133 | |
---|
| 134 | def setUp(self): |
---|
| 135 | from sans.models.Lorentzian import Lorentzian |
---|
[4680e75] | 136 | self.lor = Lorentzian() |
---|
[ae3ce4e] | 137 | |
---|
| 138 | def test1D(self): |
---|
| 139 | self.lor.setParam('scale', 2.0) |
---|
| 140 | self.lor.setParam('center', 1.0) |
---|
| 141 | self.lor.setParam('gamma', 1.0) |
---|
| 142 | self.assertEqual(self.lor.run(1.0), 4.0/math.pi) |
---|
| 143 | value = 1/math.pi*0.5/(1+.25) |
---|
| 144 | self.assertEqual(self.lor.run(2.0), 2.0*value) |
---|
| 145 | |
---|
| 146 | def test2D(self): |
---|
| 147 | self.lor.setParam('scale', 0.5*math.pi) |
---|
| 148 | self.lor.setParam('center', 1.0) |
---|
| 149 | self.lor.setParam('gamma', 1.0) |
---|
| 150 | self.assertEqual(self.lor.run(1.0), 1.0) |
---|
| 151 | value = 0.25/(1+.25) |
---|
| 152 | self.assertEqual(self.lor.runXY([1.0,2.0]), value) |
---|
| 153 | self.assertEqual(self.lor.runXY([2.0,2.0]), value*value) |
---|
| 154 | |
---|
| 155 | def test2Dphi(self): |
---|
| 156 | self.lor.setParam('scale', 0.5*math.pi) |
---|
| 157 | self.lor.setParam('center', 1.0) |
---|
| 158 | self.lor.setParam('gamma', 1.0) |
---|
| 159 | self.assertEqual(self.lor.run(1.0), 1.0) |
---|
| 160 | value = 0.25/(1+.25) |
---|
[e52eeea] | 161 | self.assertAlmostEqual(self.lor.run([math.sqrt(8.0), math.pi/4.0]), value*value, 5) |
---|
[ae3ce4e] | 162 | |
---|
| 163 | class TestSin(unittest.TestCase): |
---|
| 164 | """Unit tests for Sin(x) function""" |
---|
| 165 | |
---|
| 166 | def setUp(self): |
---|
| 167 | from sans.models.Sin import Sin |
---|
| 168 | self.sin = Sin() |
---|
| 169 | |
---|
| 170 | def test1D(self): |
---|
| 171 | self.assertEqual(self.sin.run(1.13), math.sin(1.13)) |
---|
| 172 | |
---|
| 173 | def test2D(self): |
---|
| 174 | self.assertEqual(self.sin.run([1.13,0.56]), math.sin(1.13*math.cos(0.56))*math.sin(1.13*math.sin(0.56))) |
---|
| 175 | self.assertEqual(self.sin.runXY([1.13,0.56]), math.sin(1.13)*math.sin(0.56)) |
---|
| 176 | |
---|
| 177 | class TestCos(unittest.TestCase): |
---|
| 178 | """Unit tests for Cos(x) function""" |
---|
| 179 | |
---|
| 180 | def setUp(self): |
---|
| 181 | from sans.models.Cos import Cos |
---|
| 182 | self.cos = Cos() |
---|
| 183 | |
---|
| 184 | def test1D(self): |
---|
| 185 | self.assertEqual(self.cos.run(1.13), math.cos(1.13)) |
---|
| 186 | |
---|
| 187 | def test2D(self): |
---|
| 188 | self.assertEqual(self.cos.run([1.13,0.56]), math.cos(1.13*math.cos(0.56))*math.cos(1.13*math.sin(0.56))) |
---|
| 189 | self.assertEqual(self.cos.runXY([1.13,0.56]), math.cos(1.13)*math.cos(0.56)) |
---|
| 190 | |
---|
| 191 | class TestConstant(unittest.TestCase): |
---|
| 192 | """Unit tests for Cos(x) function""" |
---|
| 193 | |
---|
| 194 | def setUp(self): |
---|
| 195 | from sans.models.Constant import Constant |
---|
| 196 | self.const = Constant() |
---|
| 197 | self.const.setParam('value',56.1) |
---|
| 198 | |
---|
| 199 | def test1D(self): |
---|
| 200 | self.assertEqual(self.const.run(1.13), 56.1) |
---|
| 201 | |
---|
| 202 | def test2D(self): |
---|
| 203 | self.assertEqual(self.const.run([1.13,0.56]), 56.1) |
---|
| 204 | self.assertEqual(self.const.runXY([1.13,0.56]), 56.1) |
---|
| 205 | |
---|
| 206 | def testFunction(self): |
---|
[ae60f86] | 207 | # version 0.5.0: No longer supported |
---|
| 208 | return |
---|
[ae3ce4e] | 209 | from sans.models.Sin import Sin |
---|
| 210 | s = Sin() |
---|
| 211 | A = self.const |
---|
| 212 | A.setParam('Value',1.5) |
---|
| 213 | B = self.const.clone() |
---|
| 214 | B.setParam('value',2.0) |
---|
| 215 | C = self.const.clone() |
---|
| 216 | C.setParam('value',3.0) |
---|
| 217 | |
---|
| 218 | f = A+B*s*s+C |
---|
| 219 | answer = 1.5+2.0*math.sin(1.1)**2+3.0 |
---|
| 220 | self.assertEqual(f.run(1.1), answer) |
---|
| 221 | |
---|
| 222 | |
---|
| 223 | |
---|
| 224 | if __name__ == '__main__': |
---|
[5401f2a] | 225 | unittest.main() |
---|