source: sasview/Invariant/test/utest_data_handling.py @ f9a1279

ESS_GUIESS_GUI_DocsESS_GUI_batch_fittingESS_GUI_bumps_abstractionESS_GUI_iss1116ESS_GUI_iss879ESS_GUI_iss959ESS_GUI_openclESS_GUI_orderingESS_GUI_sync_sascalccostrafo411magnetic_scattrelease-4.1.1release-4.1.2release-4.2.2release_4.0.1ticket-1009ticket-1094-headlessticket-1242-2d-resolutionticket-1243ticket-1249ticket885unittest-saveload
Last change on this file since f9a1279 was 46d50ca, checked in by Mathieu Doucet <doucetm@…>, 15 years ago

invariant: started cleaning up [not done]

  • Property mode set to 100644
File size: 3.1 KB
Line 
1"""
2This software was developed by the University of Tennessee as part of the
3Distributed Data Analysis of Neutron Scattering Experiments (DANSE)
4project funded by the US National Science Foundation.
5
6See the license text in license.txt
7
8copyright 2010, University of Tennessee
9"""
10import unittest
11import numpy
12from DataLoader.loader import  Loader
13from DataLoader.data_info import Data1D
14from sans.invariant import invariant
15   
16class 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   
58class 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   
Note: See TracBrowser for help on using the repository browser.