[46d50ca] | 1 | """ |
---|
| 2 | This software was developed by the University of Tennessee as part of the |
---|
| 3 | Distributed Data Analysis of Neutron Scattering Experiments (DANSE) |
---|
| 4 | project funded by the US National Science Foundation. |
---|
| 5 | |
---|
| 6 | See the license text in license.txt |
---|
| 7 | |
---|
| 8 | copyright 2010, University of Tennessee |
---|
| 9 | """ |
---|
| 10 | import unittest |
---|
| 11 | import numpy |
---|
| 12 | from DataLoader.loader import Loader |
---|
| 13 | from DataLoader.data_info import Data1D |
---|
| 14 | from sans.invariant import invariant |
---|
| 15 | |
---|
| 16 | class TestLinearFit(unittest.TestCase): |
---|
| 17 | """ |
---|
| 18 | Test Line fit |
---|
| 19 | """ |
---|
| 20 | def setUp(self): |
---|
| 21 | x = numpy.asarray([1.,2.,3.,4.,5.,6.,7.,8.,9.]) |
---|
| 22 | y = numpy.asarray([1.,2.,3.,4.,5.,6.,7.,8.,9.]) |
---|
| 23 | dy = y/10.0 |
---|
| 24 | |
---|
| 25 | self.data = Data1D(x=x,y=y,dy=dy) |
---|
| 26 | |
---|
| 27 | def test_fit_linear_data(self): |
---|
| 28 | """ |
---|
| 29 | Simple linear fit |
---|
| 30 | """ |
---|
| 31 | |
---|
| 32 | # Create invariant object. Background and scale left as defaults. |
---|
| 33 | fit = invariant.FitFunctor(data=self.data) |
---|
| 34 | a,b = fit.fit() |
---|
| 35 | |
---|
| 36 | # Test results |
---|
| 37 | self.assertAlmostEquals(a, 1.0, 5) |
---|
| 38 | self.assertAlmostEquals(b, 0.0, 5) |
---|
| 39 | |
---|
| 40 | def test_fit_linear_data_with_noise(self): |
---|
| 41 | """ |
---|
| 42 | Simple linear fit with noise |
---|
| 43 | """ |
---|
| 44 | import random, math |
---|
| 45 | |
---|
| 46 | for i in range(len(self.data.y)): |
---|
| 47 | self.data.y[i] = self.data.y[i]+.1*random.random() |
---|
| 48 | |
---|
| 49 | # Create invariant object. Background and scale left as defaults. |
---|
| 50 | fit = invariant.FitFunctor(data=self.data) |
---|
| 51 | a,b = fit.fit() |
---|
| 52 | |
---|
| 53 | # Test results |
---|
| 54 | self.assertTrue(math.fabs(a-1.0)<0.05) |
---|
| 55 | self.assertTrue(math.fabs(b)<0.05) |
---|
| 56 | |
---|
| 57 | |
---|
| 58 | class TestInvariantCalculator(unittest.TestCase): |
---|
| 59 | """ |
---|
| 60 | Test Line fit |
---|
| 61 | """ |
---|
| 62 | def setUp(self): |
---|
| 63 | self.data = Loader().load("latex_smeared.xml")[0] |
---|
| 64 | |
---|
| 65 | def test_initial_data_processing(self): |
---|
| 66 | """ |
---|
| 67 | Test whether the background and scale are handled properly |
---|
| 68 | when creating an InvariantCalculator object |
---|
| 69 | """ |
---|
| 70 | length = len(self.data.x) |
---|
| 71 | self.assertEqual(length, len(self.data.y)) |
---|
| 72 | inv = invariant.InvariantCalculator(self.data) |
---|
| 73 | |
---|
| 74 | self.assertEqual(length, len(inv._data.x)) |
---|
| 75 | self.assertEqual(inv._data.x[0], self.data.x[0]) |
---|
| 76 | |
---|
| 77 | # Now the same thing with a background value |
---|
| 78 | bck = 0.1 |
---|
| 79 | inv = invariant.InvariantCalculator(self.data, background=bck) |
---|
| 80 | self.assertEqual(inv._background, bck) |
---|
| 81 | |
---|
| 82 | self.assertEqual(length, len(inv._data.x)) |
---|
| 83 | self.assertEqual(inv._data.y[0]+bck, self.data.y[0]) |
---|
| 84 | |
---|
| 85 | # Now the same thing with a scale value |
---|
| 86 | scale = 0.1 |
---|
| 87 | inv = invariant.InvariantCalculator(self.data, scale=scale) |
---|
| 88 | self.assertEqual(inv._scale, scale) |
---|
| 89 | |
---|
| 90 | self.assertEqual(length, len(inv._data.x)) |
---|
| 91 | self.assertAlmostEqual(inv._data.y[0]/scale, self.data.y[0],7) |
---|
| 92 | |
---|
| 93 | |
---|
| 94 | def test_incompatible_data_class(self): |
---|
| 95 | """ |
---|
| 96 | Check that only classes that inherit from Data1D are allowed as data. |
---|
| 97 | """ |
---|
| 98 | class Incompatible(): |
---|
| 99 | pass |
---|
| 100 | self.assertRaises(ValueError, invariant.InvariantCalculator, Incompatible()) |
---|
| 101 | |
---|
| 102 | |
---|