summaryrefslogtreecommitdiff
path: root/silx/math/test/test_calibration.py
blob: 5a0c20ebf27d00c7218e10869db1987b7aeb5e5d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# coding: utf-8
# /*##########################################################################
# Copyright (C) 2018 European Synchrotron Radiation Facility
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#
# ############################################################################*/
"""Tests of the calibration module"""

from __future__ import division

__authors__ = ["P. Knobel"]
__license__ = "MIT"
__date__ = "14/05/2018"


import unittest

import numpy

from silx.math.calibration import NoCalibration, LinearCalibration, \
    ArrayCalibration, FunctionCalibration


X = numpy.array([3.14, 2.73, 1337])


class TestNoCalibration(unittest.TestCase):
    def setUp(self):
        self.calib = NoCalibration()

    def testIsAffine(self):
        self.assertTrue(self.calib.is_affine())

    def testSlope(self):
        self.assertEqual(self.calib.get_slope(), 1.)

    def testYIntercept(self):
        self.assertEqual(self.calib(0.),
                         0.)

    def testCall(self):
        self.assertTrue(numpy.array_equal(self.calib(X), X))


class TestLinearCalibration(unittest.TestCase):
    def setUp(self):
        self.y_intercept = 1.5
        self.slope = 2.5
        self.calib = LinearCalibration(y_intercept=self.y_intercept,
                                       slope=self.slope)

    def testIsAffine(self):
        self.assertTrue(self.calib.is_affine())

    def testSlope(self):
        self.assertEqual(self.calib.get_slope(), self.slope)

    def testYIntercept(self):
        self.assertEqual(self.calib(0.),
                         self.y_intercept)

    def testCall(self):
        self.assertTrue(numpy.array_equal(self.calib(X),
                                          self.y_intercept + self.slope * X))


class TestArrayCalibration(unittest.TestCase):
    def setUp(self):
        self.arr = numpy.array([45.2, 25.3, 666., -8.])
        self.calib = ArrayCalibration(self.arr)
        self.affine_calib = ArrayCalibration([0.1, 0.2, 0.3])

    def testIsAffine(self):
        self.assertFalse(self.calib.is_affine())
        self.assertTrue(self.affine_calib.is_affine())

    def testSlope(self):
        with self.assertRaises(AttributeError):
            self.calib.get_slope()
        self.assertEqual(self.affine_calib.get_slope(),
                         0.1)

    def testYIntercept(self):
        self.assertEqual(self.calib(0),
                         self.arr[0])

    def testCall(self):
        with self.assertRaises(ValueError):
            # X is an array with a different shape
            self.calib(X)

        with self.assertRaises(ValueError):
            # floats are not valid indices
            self.calib(3.14)

        self.assertTrue(
            numpy.array_equal(self.calib([1, 2, 3, 4]),
                              self.arr))

        for idx, value in enumerate(self.arr):
            self.assertEqual(self.calib(idx), value)


class TestFunctionCalibration(unittest.TestCase):
    def setUp(self):
        self.non_affine_fun = numpy.sin
        self.non_affine_calib = FunctionCalibration(self.non_affine_fun)

        self.affine_fun = lambda x: 52. * x + 0.01
        self.affine_calib = FunctionCalibration(self.affine_fun,
                                                is_affine=True)

    def testIsAffine(self):
        self.assertFalse(self.non_affine_calib.is_affine())
        self.assertTrue(self.affine_calib.is_affine())

    def testSlope(self):
        with self.assertRaises(AttributeError):
            self.non_affine_calib.get_slope()
        self.assertAlmostEqual(self.affine_calib.get_slope(),
                               52.)

    def testCall(self):
        for x in X:
            self.assertAlmostEqual(self.non_affine_calib(x),
                                   self.non_affine_fun(x))
            self.assertAlmostEqual(self.affine_calib(x),
                                   self.affine_fun(x))


def suite():
    test_suite = unittest.TestSuite()
    loadTests = unittest.defaultTestLoader.loadTestsFromTestCase
    test_suite.addTest(loadTests(TestNoCalibration))
    test_suite.addTest(loadTests(TestArrayCalibration))
    test_suite.addTest(loadTests(TestLinearCalibration))
    test_suite.addTest(loadTests(TestFunctionCalibration))
    return test_suite

if __name__ == '__main__':
    unittest.main(defaultTest="suite")