[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]), |
---|
| 65 | 0.038176446608393366, 4) |
---|
| 66 | |
---|
| 67 | def testIO(self): |
---|
| 68 | from sans.models.ModelFactory import ModelFactory |
---|
| 69 | from sans.models.ModelIO import ModelIO |
---|
| 70 | factory = ModelFactory() |
---|
| 71 | io = ModelIO(factory) |
---|
| 72 | io.save(self.comp,"myModel.xml") |
---|
| 73 | value = self.comp.run(1) |
---|
| 74 | loaded = io.load("myModel.xml") |
---|
| 75 | self.assertEqual(value, loaded.run(1)) |
---|
| 76 | |
---|
| 77 | def testIO_add(self): |
---|
[ae60f86] | 78 | # version 0.5.0: No longer supported |
---|
| 79 | return |
---|
[ae3ce4e] | 80 | from sans.models.ModelFactory import ModelFactory |
---|
| 81 | from sans.models.ModelIO import ModelIO |
---|
| 82 | factory = ModelFactory() |
---|
| 83 | io = ModelIO(factory) |
---|
| 84 | sph = factory.getModel("SphereModel") |
---|
| 85 | cyl = factory.getModel("CylinderModel") |
---|
| 86 | combo = sph - cyl |
---|
| 87 | io.save(combo,"myModel.xml") |
---|
| 88 | value = combo.run(1) |
---|
| 89 | loaded = io.load("myModel.xml") |
---|
| 90 | self.assertEqual(value, loaded.run(1)) |
---|
| 91 | |
---|
| 92 | def testIO_add2(self): |
---|
[ae60f86] | 93 | # version 0.5.0: No longer supported |
---|
| 94 | return |
---|
[ae3ce4e] | 95 | from sans.models.ModelFactory import ModelFactory |
---|
| 96 | #from sans.models.ModelIO import ModelIO |
---|
| 97 | factory = ModelFactory() |
---|
| 98 | #io = ModelIO(factory) |
---|
| 99 | sph = factory.getModel("SphereModel") |
---|
| 100 | cyl = factory.getModel("CylinderModel") |
---|
| 101 | sph2 = factory.getModel("SphereModel") |
---|
| 102 | combo1 = cyl - sph |
---|
| 103 | combo = combo1 / sph2 |
---|
| 104 | #combo1 = sph |
---|
| 105 | #io.save(combo,"myModel.xml") |
---|
| 106 | # Just check that we have some output |
---|
| 107 | self.assertTrue(math.fabs(combo.run(1))>0) |
---|
| 108 | #loaded = io.load("myModel.xml") |
---|
| 109 | #self.assertEqual(value, loaded.run(1)) |
---|
| 110 | |
---|
| 111 | |
---|
| 112 | |
---|
| 113 | |
---|
| 114 | class TestFactory(unittest.TestCase): |
---|
| 115 | """Unit tests for Model Factory""" |
---|
| 116 | |
---|
| 117 | def setUp(self): |
---|
| 118 | from sans.models.ModelFactory import ModelFactory |
---|
| 119 | self.comp = ModelFactory().getModel('CylinderModel') |
---|
| 120 | |
---|
| 121 | def test1D(self): |
---|
| 122 | """ Test 1D model of a cylinder """ |
---|
| 123 | self.assertAlmostEqual(self.comp.run(0.2), 0.041761386790780453, 4) |
---|
| 124 | |
---|
| 125 | |
---|
| 126 | class TestGaussian(unittest.TestCase): |
---|
| 127 | """Unit tests for Gaussian function""" |
---|
| 128 | |
---|
| 129 | def setUp(self): |
---|
| 130 | from sans.models.Gaussian import Gaussian |
---|
| 131 | self.gauss= Gaussian() |
---|
| 132 | |
---|
| 133 | def test1D(self): |
---|
| 134 | self.gauss.setParam('scale', 2.0) |
---|
| 135 | self.gauss.setParam('center', 1.0) |
---|
| 136 | self.gauss.setParam('sigma', 1.0) |
---|
| 137 | self.assertEqual(self.gauss.run(1.0), 2.0) |
---|
| 138 | value = math.exp(-0.5) |
---|
| 139 | self.assertEqual(self.gauss.run(2.0), 2.0*value) |
---|
| 140 | |
---|
| 141 | def test2D(self): |
---|
| 142 | self.gauss.setParam('scale', 2.0) |
---|
| 143 | self.gauss.setParam('center', 1.0) |
---|
| 144 | self.gauss.setParam('sigma', 1.0) |
---|
| 145 | self.assertEqual(self.gauss.runXY([1.0,1.0]), 2.0*2.0) |
---|
| 146 | value = math.exp(-0.5) |
---|
| 147 | self.assertEqual(self.gauss.runXY([1.0,2.0]), 2.0*2.0*value) |
---|
| 148 | self.assertEqual(self.gauss.runXY([2.0,2.0]), 2.0*2.0*value*value) |
---|
| 149 | |
---|
| 150 | def test2Dphi(self): |
---|
| 151 | self.gauss.setParam('scale', 2.0) |
---|
| 152 | self.gauss.setParam('center', 1.0) |
---|
| 153 | self.gauss.setParam('sigma', 1.0) |
---|
| 154 | value = math.exp(-0.5) |
---|
[e52eeea] | 155 | self.assertAlmostEqual(self.gauss.run([math.sqrt(8.0), math.pi/4.0]), 2.0*2.0*value*value, 5) |
---|
[ae3ce4e] | 156 | |
---|
| 157 | class TestLorentzian(unittest.TestCase): |
---|
| 158 | """Unit tests for Lorentzian function""" |
---|
| 159 | |
---|
| 160 | def setUp(self): |
---|
| 161 | from sans.models.Lorentzian import Lorentzian |
---|
| 162 | self.lor= Lorentzian() |
---|
| 163 | |
---|
| 164 | def test1D(self): |
---|
| 165 | self.lor.setParam('scale', 2.0) |
---|
| 166 | self.lor.setParam('center', 1.0) |
---|
| 167 | self.lor.setParam('gamma', 1.0) |
---|
| 168 | self.assertEqual(self.lor.run(1.0), 4.0/math.pi) |
---|
| 169 | value = 1/math.pi*0.5/(1+.25) |
---|
| 170 | self.assertEqual(self.lor.run(2.0), 2.0*value) |
---|
| 171 | |
---|
| 172 | def test2D(self): |
---|
| 173 | self.lor.setParam('scale', 0.5*math.pi) |
---|
| 174 | self.lor.setParam('center', 1.0) |
---|
| 175 | self.lor.setParam('gamma', 1.0) |
---|
| 176 | self.assertEqual(self.lor.run(1.0), 1.0) |
---|
| 177 | value = 0.25/(1+.25) |
---|
| 178 | self.assertEqual(self.lor.runXY([1.0,2.0]), value) |
---|
| 179 | self.assertEqual(self.lor.runXY([2.0,2.0]), value*value) |
---|
| 180 | |
---|
| 181 | def test2Dphi(self): |
---|
| 182 | self.lor.setParam('scale', 0.5*math.pi) |
---|
| 183 | self.lor.setParam('center', 1.0) |
---|
| 184 | self.lor.setParam('gamma', 1.0) |
---|
| 185 | self.assertEqual(self.lor.run(1.0), 1.0) |
---|
| 186 | value = 0.25/(1+.25) |
---|
[e52eeea] | 187 | self.assertAlmostEqual(self.lor.run([math.sqrt(8.0), math.pi/4.0]), value*value, 5) |
---|
[ae3ce4e] | 188 | |
---|
| 189 | class TestSin(unittest.TestCase): |
---|
| 190 | """Unit tests for Sin(x) function""" |
---|
| 191 | |
---|
| 192 | def setUp(self): |
---|
| 193 | from sans.models.Sin import Sin |
---|
| 194 | self.sin = Sin() |
---|
| 195 | |
---|
| 196 | def test1D(self): |
---|
| 197 | self.assertEqual(self.sin.run(1.13), math.sin(1.13)) |
---|
| 198 | |
---|
| 199 | def test2D(self): |
---|
| 200 | 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))) |
---|
| 201 | self.assertEqual(self.sin.runXY([1.13,0.56]), math.sin(1.13)*math.sin(0.56)) |
---|
| 202 | |
---|
| 203 | class TestCos(unittest.TestCase): |
---|
| 204 | """Unit tests for Cos(x) function""" |
---|
| 205 | |
---|
| 206 | def setUp(self): |
---|
| 207 | from sans.models.Cos import Cos |
---|
| 208 | self.cos = Cos() |
---|
| 209 | |
---|
| 210 | def test1D(self): |
---|
| 211 | self.assertEqual(self.cos.run(1.13), math.cos(1.13)) |
---|
| 212 | |
---|
| 213 | def test2D(self): |
---|
| 214 | 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))) |
---|
| 215 | self.assertEqual(self.cos.runXY([1.13,0.56]), math.cos(1.13)*math.cos(0.56)) |
---|
| 216 | |
---|
| 217 | class TestConstant(unittest.TestCase): |
---|
| 218 | """Unit tests for Cos(x) function""" |
---|
| 219 | |
---|
| 220 | def setUp(self): |
---|
| 221 | from sans.models.Constant import Constant |
---|
| 222 | self.const = Constant() |
---|
| 223 | self.const.setParam('value',56.1) |
---|
| 224 | |
---|
| 225 | def test1D(self): |
---|
| 226 | self.assertEqual(self.const.run(1.13), 56.1) |
---|
| 227 | |
---|
| 228 | def test2D(self): |
---|
| 229 | self.assertEqual(self.const.run([1.13,0.56]), 56.1) |
---|
| 230 | self.assertEqual(self.const.runXY([1.13,0.56]), 56.1) |
---|
| 231 | |
---|
| 232 | def testFunction(self): |
---|
[ae60f86] | 233 | # version 0.5.0: No longer supported |
---|
| 234 | return |
---|
[ae3ce4e] | 235 | from sans.models.Sin import Sin |
---|
| 236 | s = Sin() |
---|
| 237 | A = self.const |
---|
| 238 | A.setParam('Value',1.5) |
---|
| 239 | B = self.const.clone() |
---|
| 240 | B.setParam('value',2.0) |
---|
| 241 | C = self.const.clone() |
---|
| 242 | C.setParam('value',3.0) |
---|
| 243 | |
---|
| 244 | f = A+B*s*s+C |
---|
| 245 | answer = 1.5+2.0*math.sin(1.1)**2+3.0 |
---|
| 246 | self.assertEqual(f.run(1.1), answer) |
---|
| 247 | |
---|
| 248 | |
---|
| 249 | |
---|
| 250 | if __name__ == '__main__': |
---|
| 251 | unittest.main() |
---|