[c9636f7] | 1 | """ |
---|
| 2 | Unit tests for specific models |
---|
| 3 | @author: Gervaise Alina / UTK |
---|
| 4 | """ |
---|
| 5 | |
---|
| 6 | import unittest, numpy,math |
---|
| 7 | |
---|
| 8 | from sans.models.MultiplicationModel import MultiplicationModel |
---|
| 9 | from sans.models.SphereModel import SphereModel |
---|
| 10 | from sans.models.SquareWellStructure import SquareWellStructure |
---|
| 11 | |
---|
| 12 | class TestDisperser(unittest.TestCase): |
---|
| 13 | """ Unit tests for sphere model * SquareWellStructure""" |
---|
| 14 | model1= SphereModel() |
---|
| 15 | model2= SquareWellStructure() |
---|
| 16 | model3= MultiplicationModel(model1, model2) |
---|
| 17 | details={} |
---|
| 18 | details['scale'] = ['', None, None] |
---|
| 19 | details['radius'] = ['A', None, None] |
---|
| 20 | details['contrast'] = ['A-2', None, None] |
---|
| 21 | details['background'] = ['cm-1', None, None] |
---|
| 22 | details['volfraction'] = ['', None, None] |
---|
| 23 | details['welldepth'] = ['kT', None, None] |
---|
| 24 | details['wellwidth'] = ['', None, None] |
---|
| 25 | |
---|
| 26 | ## fittable parameters |
---|
| 27 | fixed=[] |
---|
| 28 | fixed=['radius.width'] |
---|
[b22748b] | 29 | def test_multplication_radius(self): |
---|
[24415e9] | 30 | """ test multiplication model""" |
---|
| 31 | from sans.models.MultiplicationModel import MultiplicationModel |
---|
[b22748b] | 32 | from sans.models.CylinderModel import CylinderModel |
---|
| 33 | from sans.models.HardsphereStructure import HardsphereStructure |
---|
| 34 | model1 = CylinderModel() |
---|
| 35 | model1.setParam("radius", 3) |
---|
| 36 | model2 = HardsphereStructure() |
---|
| 37 | model = MultiplicationModel(model1,model2 ) |
---|
| 38 | model.setParam("radius", 1) |
---|
| 39 | self.assertEqual(model.getParam("radius"), 1) |
---|
| 40 | self.assertEqual(model.model1.getParam("radius"),3) |
---|
| 41 | from sans.models.DiamCylFunc import DiamCylFunc |
---|
| 42 | model4= DiamCylFunc() |
---|
| 43 | radius= model4.run(0.0) |
---|
| 44 | self.assertEqual(model.model2.getParam("radius"),radius) |
---|
[24415e9] | 45 | |
---|
[b22748b] | 46 | |
---|
[8cfdd5e] | 47 | def test_multiplication(self): |
---|
| 48 | """ test multiplication model""" |
---|
| 49 | from sans.models.MultiplicationModel import MultiplicationModel |
---|
| 50 | from sans.models.SphereModel import SphereModel |
---|
| 51 | from sans.models.NoStructure import NoStructure |
---|
| 52 | model1 = MultiplicationModel(SphereModel(),NoStructure()) |
---|
| 53 | model2 = SphereModel() |
---|
| 54 | x= 2 |
---|
| 55 | a = model1.run(x) |
---|
| 56 | |
---|
| 57 | b= model2.run(x) |
---|
| 58 | self.assertEqual(a, b) |
---|
| 59 | model2.setParam("scale", 10) |
---|
| 60 | c= model2.run(x) |
---|
| 61 | self.assertEqual(c, 10*b) |
---|
| 62 | model1.setParam("scale", 10) |
---|
| 63 | d= model1.run(x) |
---|
| 64 | self.assertEqual(d, 10*a) |
---|
| 65 | self.assertEqual(model1.getParam("scale"), 10) |
---|
| 66 | |
---|
| 67 | |
---|
[c9636f7] | 68 | def testMultiplicationModel(self): |
---|
| 69 | """ Test Multiplication sphere with SquareWellStructure""" |
---|
| 70 | ## test details dictionary |
---|
| 71 | self.assertEqual(self.model3.details, self.details) |
---|
| 72 | |
---|
| 73 | ## test parameters list |
---|
| 74 | list3= self.model3.getParamList() |
---|
| 75 | for item in self.model1.getParamList(): |
---|
| 76 | self.assert_(item in list3) |
---|
| 77 | for item in self.model2.getParamList(): |
---|
| 78 | self.assert_(item in list3) |
---|
| 79 | |
---|
| 80 | ## test set value for parameters and get paramaters |
---|
| 81 | self.model3.setParam("scale", 15) |
---|
| 82 | self.assertEqual(self.model3.getParam("scale"), 15) |
---|
| 83 | self.model3.setParam("radius", 20) |
---|
| 84 | self.assertEqual(self.model3.getParam("radius"), 20) |
---|
| 85 | self.model3.setParam("radius.width", 15) |
---|
| 86 | self.assertEqual(self.model3.getParam("radius.width"), 15) |
---|
| 87 | |
---|
| 88 | ## Dispersity |
---|
| 89 | list3= self.model3.getDispParamList() |
---|
| 90 | self.assertEqual(list3, ['radius.npts', 'radius.nsigmas', 'radius.width']) |
---|
| 91 | |
---|
| 92 | from sans.models.dispersion_models import ArrayDispersion |
---|
| 93 | disp_th = ArrayDispersion() |
---|
| 94 | |
---|
| 95 | values_th = numpy.zeros(100) |
---|
| 96 | weights = numpy.zeros(100) |
---|
| 97 | for i in range(100): |
---|
| 98 | values_th[i]=(math.pi/99.0*i) |
---|
| 99 | weights[i]=(1.0) |
---|
| 100 | |
---|
| 101 | disp_th.set_weights(values_th, weights) |
---|
| 102 | |
---|
| 103 | self.model3.set_dispersion('radius', disp_th) |
---|
| 104 | |
---|
| 105 | val_1d = self.model3.run(math.sqrt(0.0002)) |
---|
| 106 | val_2d = self.model3.runXY([0.01,0.01]) |
---|
| 107 | |
---|
| 108 | self.assertTrue(math.fabs(val_1d-val_2d)/val_1d < 0.02) |
---|
| 109 | model4= self.model3.clone() |
---|
| 110 | self.assertEqual(model4.getParam("radius"), 20) |
---|
| 111 | |
---|
| 112 | if __name__ == '__main__': |
---|
| 113 | unittest.main() |
---|