[959eb01] | 1 | """ |
---|
| 2 | Implementation of the use-case from a usage perspective. |
---|
| 3 | |
---|
| 4 | """ |
---|
| 5 | #TODO: there's no test for smeared extrapolation |
---|
| 6 | import unittest |
---|
| 7 | from sas.sascalc.dataloader.loader import Loader |
---|
| 8 | from sas.sascalc.invariant import invariant |
---|
| 9 | |
---|
[b09095a] | 10 | |
---|
[959eb01] | 11 | class Data1D: |
---|
| 12 | pass |
---|
[b09095a] | 13 | |
---|
| 14 | |
---|
[959eb01] | 15 | class TestLineFit(unittest.TestCase): |
---|
| 16 | """ |
---|
| 17 | Test Line fit |
---|
| 18 | """ |
---|
| 19 | def setUp(self): |
---|
[b09095a] | 20 | self.data_list = Loader().load("linefittest.txt") |
---|
| 21 | self.data = self.data_list[0] |
---|
| 22 | |
---|
[959eb01] | 23 | def test_fit_line_data(self): |
---|
| 24 | """ |
---|
| 25 | Fit_Test_1: test linear fit, ax +b, without fixed |
---|
| 26 | """ |
---|
[b09095a] | 27 | |
---|
[959eb01] | 28 | # Create invariant object. Background and scale left as defaults. |
---|
| 29 | fit = invariant.Extrapolator(data=self.data) |
---|
| 30 | |
---|
[b09095a] | 31 | # Without holding |
---|
[959eb01] | 32 | p, dp = fit.fit(power=None) |
---|
| 33 | |
---|
| 34 | # Test results |
---|
| 35 | self.assertAlmostEquals(p[0], 2.3983,3) |
---|
| 36 | self.assertAlmostEquals(p[1], 0.87833,3) |
---|
| 37 | |
---|
| 38 | def test_fit_line_data_fixed(self): |
---|
| 39 | """ |
---|
| 40 | Fit_Test_2: test linear fit, ax +b, with 'a' fixed |
---|
| 41 | """ |
---|
[b09095a] | 42 | |
---|
[959eb01] | 43 | # Create invariant object. Background and scale left as defaults. |
---|
| 44 | fit = invariant.Extrapolator(data=self.data) |
---|
[b09095a] | 45 | |
---|
| 46 | # With holding a = -power =4 |
---|
[959eb01] | 47 | p, dp = fit.fit(power=-4) |
---|
| 48 | |
---|
| 49 | # Test results |
---|
| 50 | self.assertAlmostEquals(p[0], 4) |
---|
| 51 | self.assertAlmostEquals(p[1], -4.0676,3) |
---|
[b09095a] | 52 | |
---|
| 53 | |
---|
[959eb01] | 54 | class TestLineFitNoweight(unittest.TestCase): |
---|
| 55 | """ |
---|
| 56 | Test Line fit without weight(dy data) |
---|
| 57 | """ |
---|
| 58 | def setUp(self): |
---|
[b09095a] | 59 | self.data_list = Loader().load("linefittest_no_weight.txt") |
---|
| 60 | self.data = self.data_list[0] |
---|
| 61 | |
---|
[959eb01] | 62 | def skip_test_fit_line_data_no_weight(self): |
---|
| 63 | """ |
---|
| 64 | Fit_Test_1: test linear fit, ax +b, without fixed |
---|
| 65 | """ |
---|
[b09095a] | 66 | |
---|
[959eb01] | 67 | # Create invariant object. Background and scale left as defaults. |
---|
| 68 | fit = invariant.Extrapolator(data=self.data) |
---|
[b09095a] | 69 | |
---|
| 70 | # Without holding |
---|
[959eb01] | 71 | p, dp = fit.fit(power=None) |
---|
| 72 | |
---|
| 73 | # Test results |
---|
| 74 | self.assertAlmostEquals(p[0], 2.4727,3) |
---|
| 75 | self.assertAlmostEquals(p[1], 0.6,3) |
---|
| 76 | |
---|
| 77 | def test_fit_line_data_fixed_no_weight(self): |
---|
| 78 | """ |
---|
| 79 | Fit_Test_2: test linear fit, ax +b, with 'a' fixed |
---|
| 80 | """ |
---|
[b09095a] | 81 | |
---|
[959eb01] | 82 | # Create invariant object. Background and scale left as defaults. |
---|
| 83 | fit = invariant.Extrapolator(data=self.data) |
---|
[b09095a] | 84 | |
---|
[959eb01] | 85 | #With holding a = -power =4 |
---|
| 86 | p, dp = fit.fit(power=-4) |
---|
| 87 | |
---|
| 88 | # Test results |
---|
| 89 | self.assertAlmostEquals(p[0], 4) |
---|
| 90 | self.assertAlmostEquals(p[1], -7.8,3) |
---|
[b09095a] | 91 | |
---|
| 92 | |
---|
[959eb01] | 93 | class TestInvPolySphere(unittest.TestCase): |
---|
| 94 | """ |
---|
| 95 | Test unsmeared data for invariant computation |
---|
| 96 | """ |
---|
| 97 | def setUp(self): |
---|
[b09095a] | 98 | self.data_list = Loader().load("PolySpheres.txt") |
---|
| 99 | self.data = self.data_list[0] |
---|
| 100 | |
---|
[959eb01] | 101 | def test_wrong_data(self): |
---|
| 102 | """ test receiving Data1D not of type loader""" |
---|
| 103 | self.assertRaises(ValueError,invariant.InvariantCalculator, Data1D()) |
---|
[b09095a] | 104 | |
---|
[959eb01] | 105 | def test_use_case_1(self): |
---|
| 106 | """ |
---|
| 107 | Invariant without extrapolation |
---|
| 108 | """ |
---|
| 109 | # Create invariant object. Background and scale left as defaults. |
---|
| 110 | inv = invariant.InvariantCalculator(data=self.data) |
---|
[b09095a] | 111 | |
---|
[959eb01] | 112 | # We have to be able to tell the InvariantCalculator whether we want the |
---|
| 113 | # extrapolation or not. By default, when the user doesn't specify, we |
---|
[b09095a] | 114 | # should compute Q* without extrapolation. That's what should be done |
---|
| 115 | # in __init__. |
---|
| 116 | |
---|
[959eb01] | 117 | # We call get_qstar() with no argument, which signifies that we do NOT |
---|
| 118 | # want extrapolation. |
---|
| 119 | qstar = inv.get_qstar() |
---|
[b09095a] | 120 | |
---|
[959eb01] | 121 | # The volume fraction and surface use Q*. That means that the following |
---|
| 122 | # methods should check that Q* has been computed. If not, it should |
---|
| 123 | # compute it by calling get_qstare(), leaving the parameters as default. |
---|
| 124 | v, dv = inv.get_volume_fraction_with_error(contrast=2.6e-6) |
---|
| 125 | s, ds = inv.get_surface_with_error(contrast=2.6e-6, porod_const=2) |
---|
[b09095a] | 126 | |
---|
[959eb01] | 127 | # Test results |
---|
| 128 | self.assertAlmostEquals(qstar, 7.48959e-5,2) |
---|
| 129 | self.assertAlmostEquals(v, 0.005644689, 4) |
---|
| 130 | self.assertAlmostEquals(s , 941.7452, 3) |
---|
[b09095a] | 131 | |
---|
[959eb01] | 132 | def test_use_case_2(self): |
---|
| 133 | """ |
---|
[b09095a] | 134 | Invariant without extrapolation. Invariant, volume fraction and surface |
---|
| 135 | are given with errors. |
---|
[959eb01] | 136 | """ |
---|
| 137 | # Create invariant object. Background and scale left as defaults. |
---|
| 138 | inv = invariant.InvariantCalculator(data=self.data) |
---|
[b09095a] | 139 | |
---|
[959eb01] | 140 | # Get the invariant with errors |
---|
| 141 | qstar, qstar_err = inv.get_qstar_with_error() |
---|
[b09095a] | 142 | |
---|
[959eb01] | 143 | # The volume fraction and surface use Q*. That means that the following |
---|
| 144 | # methods should check that Q* has been computed. If not, it should |
---|
| 145 | # compute it by calling get_qstare(), leaving the parameters as default. |
---|
| 146 | v, dv = inv.get_volume_fraction_with_error(contrast=2.6e-6) |
---|
| 147 | s, ds = inv.get_surface_with_error(contrast=2.6e-6, porod_const=2) |
---|
| 148 | # Test results |
---|
| 149 | self.assertAlmostEquals(qstar, 7.48959e-5,2) |
---|
| 150 | self.assertAlmostEquals(v, 0.005644689, 1) |
---|
| 151 | self.assertAlmostEquals(s , 941.7452, 3) |
---|
[b09095a] | 152 | |
---|
[959eb01] | 153 | def test_use_case_3(self): |
---|
| 154 | """ |
---|
| 155 | Invariant with low-Q extrapolation |
---|
| 156 | """ |
---|
| 157 | # Create invariant object. Background and scale left as defaults. |
---|
| 158 | inv = invariant.InvariantCalculator(data=self.data) |
---|
[b09095a] | 159 | |
---|
[959eb01] | 160 | # Set the extrapolation parameters for the low-Q range |
---|
[b09095a] | 161 | |
---|
[959eb01] | 162 | # The npts parameter should have a good default. |
---|
| 163 | # The range parameter should be 'high' or 'low' |
---|
| 164 | # The function parameter should default to None. If it is None, |
---|
[b09095a] | 165 | # the method should pick a good default |
---|
| 166 | # (Guinier at low-Q and 1/q^4 at high-Q). |
---|
| 167 | # The method should also check for consistency of the extrapolation |
---|
| 168 | # and function parameters. For instance, you might not want to allow |
---|
| 169 | # 'high' and 'guinier'. |
---|
[959eb01] | 170 | # The power parameter (not shown below) should default to 4. |
---|
| 171 | inv.set_extrapolation(range='low', npts=10, function='guinier') |
---|
[b09095a] | 172 | |
---|
[959eb01] | 173 | # The version of the call without error |
---|
[b09095a] | 174 | # At this point, we could still compute Q* without extrapolation by |
---|
| 175 | # calling get_qstar with arguments, or with extrapolation=None. |
---|
[959eb01] | 176 | qstar = inv.get_qstar(extrapolation='low') |
---|
[b09095a] | 177 | |
---|
[959eb01] | 178 | # The version of the call with error |
---|
| 179 | qstar, qstar_err = inv.get_qstar_with_error(extrapolation='low') |
---|
| 180 | |
---|
| 181 | # Get the volume fraction and surface |
---|
| 182 | v, dv = inv.get_volume_fraction_with_error(contrast=2.6e-6) |
---|
| 183 | s, ds = inv.get_surface_with_error(contrast=2.6e-6, porod_const=2) |
---|
[b09095a] | 184 | |
---|
[959eb01] | 185 | # Test results |
---|
| 186 | self.assertAlmostEquals(qstar, 7.49e-5, 1) |
---|
| 187 | self.assertAlmostEquals(v, 0.005648401, 4) |
---|
| 188 | self.assertAlmostEquals(s , 941.7452, 3) |
---|
[b09095a] | 189 | |
---|
[959eb01] | 190 | def test_use_case_4(self): |
---|
| 191 | """ |
---|
| 192 | Invariant with high-Q extrapolation |
---|
| 193 | """ |
---|
| 194 | # Create invariant object. Background and scale left as defaults. |
---|
| 195 | inv = invariant.InvariantCalculator(data=self.data) |
---|
[b09095a] | 196 | |
---|
[959eb01] | 197 | # Set the extrapolation parameters for the high-Q range |
---|
[b09095a] | 198 | inv.set_extrapolation(range='high', npts=10, function='power_law', |
---|
| 199 | power=4) |
---|
[959eb01] | 200 | |
---|
| 201 | # The version of the call without error |
---|
[b09095a] | 202 | # The function parameter defaults to None, then is picked to be |
---|
| 203 | # 'power_law' for extrapolation='high' |
---|
[959eb01] | 204 | qstar = inv.get_qstar(extrapolation='high') |
---|
[b09095a] | 205 | |
---|
[959eb01] | 206 | # The version of the call with error |
---|
| 207 | qstar, qstar_err = inv.get_qstar_with_error(extrapolation='high') |
---|
| 208 | |
---|
| 209 | # Get the volume fraction and surface |
---|
| 210 | v, dv = inv.get_volume_fraction_with_error(contrast=2.6e-6) |
---|
| 211 | s, ds = inv.get_surface_with_error(contrast=2.6e-6, porod_const=2) |
---|
[b09095a] | 212 | |
---|
[959eb01] | 213 | # Test results |
---|
| 214 | self.assertAlmostEquals(qstar, 7.49e-5,2) |
---|
| 215 | self.assertAlmostEquals(v, 0.005952674, 3) |
---|
| 216 | self.assertAlmostEquals(s , 941.7452, 3) |
---|
[b09095a] | 217 | |
---|
[959eb01] | 218 | def test_use_case_5(self): |
---|
| 219 | """ |
---|
| 220 | Invariant with both high- and low-Q extrapolation |
---|
| 221 | """ |
---|
| 222 | # Create invariant object. Background and scale left as defaults. |
---|
| 223 | inv = invariant.InvariantCalculator(data=self.data) |
---|
[b09095a] | 224 | |
---|
[959eb01] | 225 | # Set the extrapolation parameters for the low- and high-Q ranges |
---|
| 226 | inv.set_extrapolation(range='low', npts=10, function='guinier') |
---|
[b09095a] | 227 | inv.set_extrapolation(range='high', npts=10, function='power_law', |
---|
| 228 | power=4) |
---|
| 229 | |
---|
[959eb01] | 230 | # The version of the call without error |
---|
[b09095a] | 231 | # The function parameter defaults to None, then is picked to be |
---|
| 232 | # 'power_law' for extrapolation='high' |
---|
[959eb01] | 233 | qstar = inv.get_qstar(extrapolation='both') |
---|
| 234 | |
---|
| 235 | # The version of the call with error |
---|
| 236 | qstar, qstar_err = inv.get_qstar_with_error(extrapolation='both') |
---|
| 237 | |
---|
| 238 | # Get the volume fraction and surface |
---|
| 239 | v, dv = inv.get_volume_fraction_with_error(contrast=2.6e-6) |
---|
| 240 | s, ds = inv.get_surface_with_error(contrast=2.6e-6, porod_const=2) |
---|
[b09095a] | 241 | |
---|
[959eb01] | 242 | # Test results |
---|
| 243 | self.assertAlmostEquals(qstar, 7.88981e-5,2) |
---|
| 244 | self.assertAlmostEquals(v, 0.005952674, 3) |
---|
| 245 | self.assertAlmostEquals(s , 941.7452, 3) |
---|
[b09095a] | 246 | |
---|
[959eb01] | 247 | def test_use_case_6(self): |
---|
| 248 | """ |
---|
| 249 | Invariant with high-Q extrapolation |
---|
| 250 | """ |
---|
| 251 | # Create invariant object. Background and scale left as defaults. |
---|
| 252 | inv = invariant.InvariantCalculator(data=self.data) |
---|
[b09095a] | 253 | |
---|
[959eb01] | 254 | # Set the extrapolation parameters for the high-Q range |
---|
| 255 | inv.set_extrapolation(range='low', npts=10, function='power_law', power=4) |
---|
[b09095a] | 256 | |
---|
[959eb01] | 257 | # The version of the call without error |
---|
| 258 | # The function parameter defaults to None, then is picked to be 'power_law' for extrapolation='high' |
---|
| 259 | qstar = inv.get_qstar(extrapolation='low') |
---|
[b09095a] | 260 | |
---|
[959eb01] | 261 | # The version of the call with error |
---|
| 262 | qstar, qstar_err = inv.get_qstar_with_error(extrapolation='low') |
---|
| 263 | |
---|
| 264 | # Get the volume fraction and surface |
---|
| 265 | v, dv = inv.get_volume_fraction_with_error(contrast=2.6e-6) |
---|
| 266 | s, ds = inv.get_surface_with_error(contrast=2.6e-6, porod_const=2) |
---|
[b09095a] | 267 | |
---|
[959eb01] | 268 | # Test results |
---|
| 269 | self.assertAlmostEquals(qstar, 7.49e-5,2) |
---|
| 270 | self.assertAlmostEquals(v, 0.005952674, 3) |
---|
| 271 | self.assertAlmostEquals(s , 941.7452, 3) |
---|
[b09095a] | 272 | |
---|
| 273 | |
---|
[959eb01] | 274 | class TestInvPinholeSmear(unittest.TestCase): |
---|
| 275 | """ |
---|
| 276 | Test pinhole smeared data for invariant computation |
---|
| 277 | """ |
---|
| 278 | def setUp(self): |
---|
| 279 | # data with smear info |
---|
| 280 | list = Loader().load("latex_smeared.xml") |
---|
| 281 | self.data_q_smear = list[0] |
---|
[b09095a] | 282 | |
---|
[959eb01] | 283 | def test_use_case_1(self): |
---|
| 284 | """ |
---|
| 285 | Invariant without extrapolation |
---|
| 286 | """ |
---|
| 287 | inv = invariant.InvariantCalculator(data=self.data_q_smear) |
---|
| 288 | qstar = inv.get_qstar() |
---|
[b09095a] | 289 | |
---|
[959eb01] | 290 | v = inv.get_volume_fraction(contrast=2.6e-6) |
---|
| 291 | s = inv.get_surface(contrast=2.6e-6, porod_const=2) |
---|
| 292 | # Test results |
---|
| 293 | self.assertAlmostEquals(qstar, 1.361677e-3, 4) |
---|
| 294 | self.assertAlmostEquals(v, 0.115352622, 2) |
---|
| 295 | self.assertAlmostEquals(s , 941.7452, 3 ) |
---|
[b09095a] | 296 | |
---|
[959eb01] | 297 | def test_use_case_2(self): |
---|
| 298 | """ |
---|
| 299 | Invariant without extrapolation. Invariant, volume fraction and surface |
---|
| 300 | are given with errors. |
---|
| 301 | """ |
---|
| 302 | # Create invariant object. Background and scale left as defaults. |
---|
| 303 | inv = invariant.InvariantCalculator(data=self.data_q_smear) |
---|
[b09095a] | 304 | |
---|
[959eb01] | 305 | # Get the invariant with errors |
---|
| 306 | qstar, qstar_err = inv.get_qstar_with_error() |
---|
| 307 | # Get the volume fraction and surface |
---|
| 308 | v, dv = inv.get_volume_fraction_with_error(contrast=2.6e-6) |
---|
| 309 | s, ds = inv.get_surface_with_error(contrast=2.6e-6, porod_const=2) |
---|
| 310 | # Test results |
---|
| 311 | self.assertAlmostEquals(qstar, 1.361677e-3, 4) |
---|
| 312 | self.assertAlmostEquals(v, 0.115352622, 2) |
---|
| 313 | self.assertAlmostEquals(s , 941.7452, 3 ) |
---|
[b09095a] | 314 | |
---|
[959eb01] | 315 | def test_use_case_3(self): |
---|
| 316 | """ |
---|
| 317 | Invariant with low-Q extrapolation |
---|
| 318 | """ |
---|
| 319 | # Create invariant object. Background and scale left as defaults. |
---|
| 320 | inv = invariant.InvariantCalculator(data=self.data_q_smear) |
---|
| 321 | # Set the extrapolation parameters for the low-Q range |
---|
| 322 | inv.set_extrapolation(range='low', npts=20, function='guinier') |
---|
| 323 | # The version of the call without error |
---|
| 324 | qstar = inv.get_qstar(extrapolation='low') |
---|
| 325 | # The version of the call with error |
---|
| 326 | qstar, qstar_err = inv.get_qstar_with_error(extrapolation='low') |
---|
| 327 | # Get the volume fraction and surface |
---|
| 328 | v, dv = inv.get_volume_fraction_with_error(contrast=2.6e-6) |
---|
| 329 | s, ds = inv.get_surface_with_error(contrast=2.6e-6, porod_const=2) |
---|
[b09095a] | 330 | |
---|
[959eb01] | 331 | # Test results |
---|
| 332 | self.assertAlmostEquals(qstar, 0.00138756,2) |
---|
| 333 | self.assertAlmostEquals(v, 0.117226896,2) |
---|
| 334 | self.assertAlmostEquals(s ,941.7452, 3) |
---|
[b09095a] | 335 | |
---|
[959eb01] | 336 | def test_use_case_4(self): |
---|
| 337 | """ |
---|
| 338 | Invariant with high-Q extrapolation |
---|
| 339 | """ |
---|
| 340 | # Create invariant object. Background and scale left as defaults. |
---|
| 341 | inv = invariant.InvariantCalculator(data=self.data_q_smear) |
---|
| 342 | # Set the extrapolation parameters for the high-Q range |
---|
| 343 | inv.set_extrapolation(range='high', npts=10, function='power_law', power=4) |
---|
| 344 | # The version of the call without error |
---|
| 345 | qstar = inv.get_qstar(extrapolation='high') |
---|
| 346 | # The version of the call with error |
---|
| 347 | qstar, qstar_err = inv.get_qstar_with_error(extrapolation='high') |
---|
| 348 | |
---|
| 349 | # Test results |
---|
| 350 | self.assertAlmostEquals(qstar, 0.0045773,2) |
---|
[b09095a] | 351 | |
---|
[959eb01] | 352 | def test_use_case_5(self): |
---|
| 353 | """ |
---|
| 354 | Invariant with both high- and low-Q extrapolation |
---|
| 355 | """ |
---|
| 356 | # Create invariant object. Background and scale left as defaults. |
---|
| 357 | inv = invariant.InvariantCalculator(data=self.data_q_smear) |
---|
| 358 | # Set the extrapolation parameters for the low- and high-Q ranges |
---|
| 359 | inv.set_extrapolation(range='low', npts=10, function='guinier') |
---|
[b09095a] | 360 | inv.set_extrapolation(range='high', npts=10, function='power_law', |
---|
| 361 | power=4) |
---|
[959eb01] | 362 | # The version of the call without error |
---|
[b09095a] | 363 | # The function parameter defaults to None, then is picked to be |
---|
| 364 | # 'power_law' for extrapolation='high' |
---|
[959eb01] | 365 | qstar = inv.get_qstar(extrapolation='both') |
---|
| 366 | # The version of the call with error |
---|
| 367 | qstar, qstar_err = inv.get_qstar_with_error(extrapolation='both') |
---|
[b09095a] | 368 | |
---|
[959eb01] | 369 | # Test results |
---|
| 370 | self.assertAlmostEquals(qstar, 0.00460319,3) |
---|
| 371 | |
---|
| 372 | |
---|
| 373 | if __name__ == '__main__': |
---|
| 374 | unittest.main(testRunner=unittest.TextTestRunner(verbosity=2)) |
---|
| 375 | |
---|