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