1 | """ |
---|
2 | Unit tests for data manipulations |
---|
3 | """ |
---|
4 | |
---|
5 | |
---|
6 | import unittest |
---|
7 | import numpy, math |
---|
8 | from sas.dataloader.loader import Loader |
---|
9 | from sas.dataloader.data_info import Data1D, Data2D |
---|
10 | #from DataLoader.qsmearing import SlitSmearer, QSmearer, smear_selection |
---|
11 | from sas.models.qsmearing import SlitSmearer, QSmearer, smear_selection |
---|
12 | from sas.models.SphereModel import SphereModel |
---|
13 | import os.path |
---|
14 | from time import time |
---|
15 | class smear_tests(unittest.TestCase): |
---|
16 | |
---|
17 | def setUp(self): |
---|
18 | data = Loader().load("cansas1d_slit.xml") |
---|
19 | self.data = data[0] |
---|
20 | |
---|
21 | x = 0.001*numpy.arange(1,11) |
---|
22 | y = 12.0-numpy.arange(1,11) |
---|
23 | dxl = 0.00*numpy.ones(10) |
---|
24 | dxw = 0.00*numpy.ones(10) |
---|
25 | dx = 0.00*numpy.ones(10) |
---|
26 | |
---|
27 | self.data.dx = dx |
---|
28 | self.data.x = x |
---|
29 | self.data.y = y |
---|
30 | self.data.dxl = dxl |
---|
31 | self.data.dxw = dxw |
---|
32 | |
---|
33 | def test_slit(self): |
---|
34 | """ |
---|
35 | Test identity smearing |
---|
36 | """ |
---|
37 | # Create smearer for our data |
---|
38 | s = SlitSmearer(self.data) |
---|
39 | |
---|
40 | input = 12.0-numpy.arange(1,11) |
---|
41 | output = s(input) |
---|
42 | for i in range(len(input)): |
---|
43 | self.assertEquals(input[i], output[i]) |
---|
44 | |
---|
45 | def test_slit2(self): |
---|
46 | """ |
---|
47 | Test basic smearing |
---|
48 | """ |
---|
49 | dxl = 0.005*numpy.ones(10) |
---|
50 | dxw = 0.0*numpy.ones(10) |
---|
51 | self.data.dxl = dxl |
---|
52 | self.data.dxw = dxw |
---|
53 | # Create smearer for our data |
---|
54 | s = SlitSmearer(self.data) |
---|
55 | |
---|
56 | input = 12.0-numpy.arange(1,11) |
---|
57 | output = s(input) |
---|
58 | # The following commented line was the correct output for even bins [see smearer.cpp for details] |
---|
59 | #answer = [ 9.666, 9.056, 8.329, 7.494, 6.642, 5.721, 4.774, 3.824, 2.871, 2. ] |
---|
60 | answer = [ 9.0618, 8.6401, 8.1186, 7.1391, 6.1528, 5.5555, 4.5584, 3.5606, 2.5623, 2. ] |
---|
61 | for i in range(len(input)): |
---|
62 | self.assertAlmostEqual(answer[i], output[i], 3) |
---|
63 | |
---|
64 | def test_q(self): |
---|
65 | """ |
---|
66 | Test identity resolution smearing |
---|
67 | """ |
---|
68 | # Create smearer for our data |
---|
69 | s = QSmearer(self.data) |
---|
70 | |
---|
71 | input = 12.0-numpy.arange(1,11) |
---|
72 | output = s(input) |
---|
73 | for i in range(len(input)): |
---|
74 | self.assertAlmostEquals(input[i], output[i], 5) |
---|
75 | |
---|
76 | def test_q2(self): |
---|
77 | """ |
---|
78 | Test basic smearing |
---|
79 | """ |
---|
80 | dx = 0.001*numpy.ones(10) |
---|
81 | self.data.dx = dx |
---|
82 | |
---|
83 | # Create smearer for our data |
---|
84 | s = QSmearer(self.data) |
---|
85 | |
---|
86 | input = 12.0-numpy.arange(1,11) |
---|
87 | output = s(input) |
---|
88 | |
---|
89 | answer = [ 10.44785079, 9.84991299, 8.98101708, |
---|
90 | 7.99906585, 6.99998311, 6.00001689, |
---|
91 | 5.00093415, 4.01898292, 3.15008701, 2.55214921] |
---|
92 | for i in range(len(input)): |
---|
93 | self.assertAlmostEqual(answer[i], output[i], 2) |
---|
94 | |
---|
95 | class smear_test_1Dpinhole(unittest.TestCase): |
---|
96 | |
---|
97 | def setUp(self): |
---|
98 | # NIST sample data |
---|
99 | self.data = Loader().load("CMSphere5.txt") |
---|
100 | # NIST smeared sphere w/ param values below |
---|
101 | self.answer = Loader().load("CMSphere5smearsphere.txt") |
---|
102 | # call spheremodel |
---|
103 | self.model = SphereModel() |
---|
104 | # setparams consistent with Igor default |
---|
105 | self.model.setParam('scale', 1.0) |
---|
106 | self.model.setParam('background', 0.01) |
---|
107 | self.model.setParam('radius', 60.0) |
---|
108 | self.model.setParam('sldSolv', 6.3e-06) |
---|
109 | self.model.setParam('sldSph', 1.0e-06) |
---|
110 | |
---|
111 | def test_q(self): |
---|
112 | """ |
---|
113 | Compare Pinhole resolution smearing with NIST |
---|
114 | """ |
---|
115 | # x values |
---|
116 | input = numpy.zeros(len(self.data.x)) |
---|
117 | # set time |
---|
118 | st1 = time() |
---|
119 | # cal I w/o smear |
---|
120 | input = self.model.evalDistribution(self.data.x) |
---|
121 | # Cal_smear (first call) |
---|
122 | for i in range(1000): |
---|
123 | s = QSmearer(self.data, self.model) |
---|
124 | # stop and record time taken |
---|
125 | first_call_time = time()-st1 |
---|
126 | # set new time |
---|
127 | st = time() |
---|
128 | # cal I w/o smear (this is not neccessary to call but just to be fare |
---|
129 | input = self.model.evalDistribution(self.data.x) |
---|
130 | # smear cal (after first call done above) |
---|
131 | for i in range(1000): |
---|
132 | output = s(input) |
---|
133 | |
---|
134 | # record time taken |
---|
135 | last_call_time = time()-st |
---|
136 | # compare the ratio of ((NIST_answer-SsanView_answer)/NIST_answer) |
---|
137 | # If the ratio less than 1%, pass the test |
---|
138 | for i in range(len(self.data.x)): |
---|
139 | ratio = math.fabs((self.answer.y[i]-output[i])/self.answer.y[i]) |
---|
140 | if ratio > 0.006: |
---|
141 | ratio = 0.006 |
---|
142 | self.assertEqual(math.fabs((self.answer.y[i]-output[i])/ \ |
---|
143 | self.answer.y[i]), ratio) |
---|
144 | # print |
---|
145 | print "\n NIST_time = 10sec:" |
---|
146 | print "Cal_time(1000 times of first_calls; ) = ", first_call_time |
---|
147 | print "Cal_time(1000 times of calls) = ", last_call_time |
---|
148 | |
---|
149 | if __name__ == '__main__': |
---|
150 | unittest.main() |
---|
151 | |
---|