File: test_interpolate.py

package info (click to toggle)
python-scipy 0.6.0-12
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 32,016 kB
  • ctags: 46,675
  • sloc: cpp: 124,854; ansic: 110,614; python: 108,664; fortran: 76,260; objc: 424; makefile: 384; sh: 10
file content (205 lines) | stat: -rw-r--r-- 6,509 bytes parent folder | download
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
from numpy.testing import *
from numpy import mgrid, pi, sin, ogrid
import numpy as np

set_package_path()
from interpolate import interp1d, interp2d
restore_path()


class test_interp2d(NumpyTestCase):
    def test_interp2d(self):
        y, x = mgrid[0:pi:20j, 0:pi:21j]
        z = sin(x+y)
        I = interp2d(x, y, z)
        assert_almost_equal(I(1.0, 1.0), sin(2.0), decimal=2)

	v,u = ogrid[0:pi:24j, 0:pi:25j]
        assert_almost_equal(I(u.ravel(), v.ravel()), sin(v+u), decimal=2)


class test_interp1d(NumpyTestCase):

    def setUp(self):
        self.x10 = np.arange(10.)
        self.y10 = np.arange(10.)
        self.x25 = self.x10.reshape((2,5))
        self.x2 = np.arange(2.)
        self.y2 = np.arange(2.)
        self.x1 = np.array([0.])
        self.y1 = np.array([0.])

        self.y210 = np.arange(20.).reshape((2, 10))
        self.y102 = np.arange(20.).reshape((10, 2))

        self.fill_value = -100.0

    def test_validation(self):
        """ Make sure that appropriate exceptions are raised when invalid values
        are given to the constructor.
        """

        # These should all work. 
        interp1d(self.x10, self.y10, kind='linear')
        interp1d(self.x10, self.y10, kind='cubic')
        interp1d(self.x10, self.y10, kind='slinear')
        interp1d(self.x10, self.y10, kind='quadratic')
        interp1d(self.x10, self.y10, kind='zero')
        interp1d(self.x10, self.y10, kind=0)
        interp1d(self.x10, self.y10, kind=1)
        interp1d(self.x10, self.y10, kind=2)
        interp1d(self.x10, self.y10, kind=3)                

        # x array must be 1D.
        self.assertRaises(ValueError, interp1d, self.x25, self.y10)

        # y array cannot be a scalar.
        self.assertRaises(ValueError, interp1d, self.x10, np.array(0))

        # Check for x and y arrays having the same length.
        self.assertRaises(ValueError, interp1d, self.x10, self.y2)
        self.assertRaises(ValueError, interp1d, self.x2, self.y10)
        self.assertRaises(ValueError, interp1d, self.x10, self.y102)
        interp1d(self.x10, self.y210)
        interp1d(self.x10, self.y102, axis=0)

        # Check for x and y having at least 1 element.
        self.assertRaises(ValueError, interp1d, self.x1, self.y10)
        self.assertRaises(ValueError, interp1d, self.x10, self.y1)
        self.assertRaises(ValueError, interp1d, self.x1, self.y1)


    def test_init(self):
        """ Check that the attributes are initialized appropriately by the
        constructor.
        """

        self.assert_(interp1d(self.x10, self.y10).copy)
        self.assert_(not interp1d(self.x10, self.y10, copy=False).copy)
        self.assert_(interp1d(self.x10, self.y10).bounds_error)
        self.assert_(not interp1d(self.x10, self.y10, bounds_error=False).bounds_error)
        self.assert_(np.isnan(interp1d(self.x10, self.y10).fill_value))
        self.assertEqual(
            interp1d(self.x10, self.y10, fill_value=3.0).fill_value, 
            3.0,
        )
        self.assertEqual(
            interp1d(self.x10, self.y10).axis,
            0,
        )
        self.assertEqual(
            interp1d(self.x10, self.y210).axis,
            1,
        )
        self.assertEqual(
            interp1d(self.x10, self.y102, axis=0).axis,
            0,
        )
        assert_array_equal(
            interp1d(self.x10, self.y10).x,
            self.x10,
        )
        assert_array_equal(
            interp1d(self.x10, self.y10).y,
            self.y10,
        )
        assert_array_equal(
            interp1d(self.x10, self.y210).y,
            self.y210,
        )


    def test_linear(self):
        """ Check the actual implementation of linear interpolation.
        """

        interp10 = interp1d(self.x10, self.y10)
        assert_array_almost_equal(
            interp10(self.x10),
            self.y10,
        )
        assert_array_almost_equal(
            interp10(1.2),
            np.array([1.2]),
        )
        assert_array_almost_equal(
            interp10([2.4, 5.6, 6.0]),
            np.array([2.4, 5.6, 6.0]),
        )


    def test_bounds(self):
        """ Test that our handling of out-of-bounds input is correct.
        """

        extrap10 = interp1d(self.x10, self.y10, fill_value=self.fill_value,
            bounds_error=False)
        assert_array_equal(
            extrap10(11.2),
            np.array([self.fill_value]),
        )
        assert_array_equal(
            extrap10(-3.4),
            np.array([self.fill_value]),
        )
        assert_array_equal(
            extrap10._check_bounds(np.array([-1.0, 0.0, 5.0, 9.0, 11.0])),
            np.array([True, False, False, False, True]),
        )

        raises_bounds_error = interp1d(self.x10, self.y10, bounds_error=True)
        self.assertRaises(ValueError, raises_bounds_error, -1.0)
        self.assertRaises(ValueError, raises_bounds_error, 11.0)
        raises_bounds_error([0.0, 5.0, 9.0])


    def test_nd(self):
        """ Check the behavior when the inputs and outputs are multidimensional.
        """

        # Multidimensional input.
        interp10 = interp1d(self.x10, self.y10)
        assert_array_almost_equal(
            interp10(np.array([[3.4, 5.6], [2.4, 7.8]])),
            np.array([[3.4, 5.6], [2.4, 7.8]]),
        )

        # Multidimensional outputs.
        interp210 = interp1d(self.x10, self.y210)
        assert_array_almost_equal(
            interp210(1.5),
            np.array([[1.5], [11.5]]),
        )
        assert_array_almost_equal(
            interp210(np.array([1.5, 2.4])),
            np.array([[1.5, 2.4],
                      [11.5, 12.4]]),
        )
        
        interp102 = interp1d(self.x10, self.y102, axis=0)
        assert_array_almost_equal(
            interp102(1.5),
            np.array([[3.0, 4.0]]),
        )
        assert_array_almost_equal(
            interp102(np.array([1.5, 2.4])),
            np.array([[3.0, 4.0],
                      [4.8, 5.8]]),
        )

        # Both at the same time!
        x_new = np.array([[3.4, 5.6], [2.4, 7.8]])
        assert_array_almost_equal(
            interp210(x_new),
            np.array([[[3.4, 5.6], [2.4, 7.8]],
                      [[13.4, 15.6], [12.4, 17.8]]]),
        )
        assert_array_almost_equal(
            interp102(x_new),
            np.array([[[6.8, 7.8], [11.2, 12.2]],
                      [[4.8, 5.8], [15.6, 16.6]]]),
        )


if __name__ == "__main__":
    NumpyTest().run()