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