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