# [Scipy-svn] r4641 - in branches/Interpolate1D: . docs tests

scipy-svn@scip... scipy-svn@scip...
Mon Aug 11 19:11:34 CDT 2008

```Author: fcady
Date: 2008-08-11 19:11:21 -0500 (Mon, 11 Aug 2008)
New Revision: 4641

Modified:
branches/Interpolate1D/docs/tutorial.rst
branches/Interpolate1D/interpolate1d.py
branches/Interpolate1D/interpolate2d.py
branches/Interpolate1D/interpolateNd.py
branches/Interpolate1D/tests/test_interpolate2d.py
Log:
some small changes after talking to Eric.  More to be made tomorrow

Modified: branches/Interpolate1D/docs/tutorial.rst
===================================================================
--- branches/Interpolate1D/docs/tutorial.rst	2008-08-11 23:12:21 UTC (rev 4640)
+++ branches/Interpolate1D/docs/tutorial.rst	2008-08-12 00:11:21 UTC (rev 4641)
@@ -429,32 +429,38 @@
code for working with splines.  However, Interpolate1d only wraps a part of this functionality.
For some tasks, it is good to be able to directly access this power.

-This section describes the operation of the Spline class.
+This section provides a brief introduction to the mathematics of splines
+and describes the operation of the Spline class.

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Intro to Splines
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

-Splines are a class of functions which
+Splines are a class of functions which
+
#) are easy and quick to evaluate,
#) can be fitted to any 1D data, and
#) are quite smooth
-#) do not show the pathological Runge's phenomenon which mares polynomial fits
+#) do not show the pathological Runge's phenomenon which mars polynomial fits
+
Thus, they are ideal for interpolation if we need something smoother than
a simple linear fit.  This is the barest of mathematical primers on splines;
+a very accessible but reasonably thorough treatment_ of splines and their use
+in interpolation.

+.. _treatment : http://en.wikipedia.org/wiki/Spline_interpolation

Mathematically, a spline function S of order k is defined relative to a sequence of "knots", x1, x2, ..., xn. On
every interval [xi, x_{i-1}], S is a polynomial of order at most k (it is from this that the ease and speed
-of splines arises).  At a knot, where two of the polynomials meet, they are required to agree in the first
-k-1 derivatives (ie all but the last).  A spline is specified by the locations of its knots and the coefficients
-of its polynomial in each interval.
+of splines arises, since polynomials are easy to work with).  At a knot, where two of the polynomials meet,
+they are required to agree in the first k-1 derivatives (ie all but the highest).  A spline is specified by the
+locations of its knots and, for each interval, the coefficients of polynomial that describes it.

For interpolation purposes, the knots are typically chosen to be the known data points. It
is also common for splines to include smoothing of data, so that the curve does not pass
through all the data points but is smoother than it would be if it had to. k=3 is the most
-common order of spline used in interpolation, and is often called a cubic spline.
+common order of spline used in interpolation, and is often called a 'cubic' or 'natural' spline spline.

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Basic Usage

Modified: branches/Interpolate1D/interpolate1d.py
===================================================================
--- branches/Interpolate1D/interpolate1d.py	2008-08-11 23:12:21 UTC (rev 4640)
+++ branches/Interpolate1D/interpolate1d.py	2008-08-12 00:11:21 UTC (rev 4641)
@@ -10,22 +10,22 @@
# dictionary of interpolation functions/classes/objects
method_register = \
{ # functions
-                    'linear' : linear,  'Linear' : linear,
-                    'logarithmic' : logarithmic, 'Logarithmic' : logarithmic,
-                    'block' : block, 'Block' : block,
-                    'block_average_above' : block_average_above,
-                    'Block_average_above' : block_average_above,
-                    'nearest' : nearest, 'Nearest' : nearest,
+                    'linear' : linear,
+                    'logarithmic' : logarithmic,
+                    'block' : block,
+                    'block_average_above' : block_average_above,
+                    'nearest' : nearest,

# Splines
-                    'Spline' : Spline, 'spline' : Spline,
-                    'Cubic' : Spline(k=3), 'cubic' : Spline(k=3),
-                    'Quartic' : Spline(k=4), 'quartic' : Spline(k=4),
-                    'Quar' : Spline(k=4), 'quar' : Spline(k=4),
-                    'Quintic' : Spline(k=5), 'quintic' : Spline(k=5),
-                    'Quin' : Spline(k=5), 'quin' : Spline(k=5)
+                    'spline' : Spline,
+                    'cubic' : Spline(k=3),
+                    'quartic' : Spline(k=4),
+                    'quar' : Spline(k=4),
+                    'quintic' : Spline(k=5),
+                    'quin' : Spline(k=5),
+                    'natural': Spline(k=3),
}

# dictionary of types for casting.  key = possible datatype, value = datatype it is cast to
@@ -56,6 +56,10 @@
interpolate from.  Note that 2-dimensional
y is not currently supported.

+            newx -- list of 1D numpy array
+                x values at which to interpolate the value
+                of the function
+
Optional Arguments
-------------------

@@ -159,6 +163,10 @@
y includes the y-values for the data set  to
interpolate from.  Note that 2-dimensional
y is not supported.
+
+            newx -- list of 1D numpy array
+                x values at which to interpolate the value
+                of the function

Optional Arguments
-------------------
@@ -306,7 +314,7 @@
# primary usage : user passes a string indicating a known function
# pick interpolator accordingly
if isinstance(interp_arg, basestring):
-            interpolator = method_register.setdefault(interp_arg, None )
+            interpolator = method_register.setdefault(interp_arg.lower(), None )
if interpolator is None:
raise TypeError, "input string %s not valid" % interp_arg
else:

Modified: branches/Interpolate1D/interpolate2d.py
===================================================================
--- branches/Interpolate1D/interpolate2d.py	2008-08-11 23:12:21 UTC (rev 4640)
+++ branches/Interpolate1D/interpolate2d.py	2008-08-12 00:11:21 UTC (rev 4641)
@@ -12,16 +12,16 @@
method_register = \
{
'linear' : Spline2d(kx=1, ky=1),
-                    'Linear' :  Spline2d(kx=1, ky=1),
-                    'Spline' : Spline2d(), 'spline' : Spline2d(),
-                    'Cubic' : Spline2d(kx=3, ky=3), 'cubic' : Spline2d(kx=3, ky=3),
-                    'Quartic' : Spline2d(kx=4, ky=4), 'quartic' : Spline2d(kx=4, ky=4),
-                    'Quar' : Spline2d(kx=4, ky=4), 'quar' : Spline2d(kx=4, ky=4),
-                    'Quintic' : Spline2d(kx=5, ky=5), 'quintic' : Spline2d(kx=5, ky=5),
-                    'Quin' : Spline2d(kx=5, ky=5), 'quin' : Spline2d(kx=5, ky=5),
+                    'spline' : Spline2d(),
+                    'cubic' : Spline2d(kx=3, ky=3),
+                    'quartic' : Spline2d(kx=4, ky=4),
+                    'quar' : Spline2d(kx=4, ky=4),
+                    'quintic' : Spline2d(kx=5, ky=5),
+                    'quin' : Spline2d(kx=5, ky=5),
'526' : algorithm526, 'algorithm526':algorithm526,
+                    'natural' : Spline2d(kx=3, ky=3),
}

# dictionary of types for casting.  key = possible datatype, value = datatype it is cast to
@@ -177,7 +177,7 @@
# primary usage : user passes a string indicating a known function
# pick interpolator accordingly
if isinstance(method, basestring):
-            interpolator = method_register.setdefault(method, None )
+            interpolator = method_register.setdefault(method.lower(), None )
if interpolator is None:
raise TypeError, "input string %s not valid" % method
else:

Modified: branches/Interpolate1D/interpolateNd.py
===================================================================
--- branches/Interpolate1D/interpolateNd.py	2008-08-11 23:12:21 UTC (rev 4640)
+++ branches/Interpolate1D/interpolateNd.py	2008-08-12 00:11:21 UTC (rev 4641)
@@ -131,12 +131,21 @@
# The array interpolation is handled by the C extension _nd_image.
def __init__(self, data, starting_coords =None, spacings = None,
kind='linear', out=NaN):
-        """ data = array or list of lists
-            starting_coords = None, list, 1D array or 2D (nx1) array
-            spacings = None, list, 1D array or 2D (nx1) array
-            kind = string or integer
-                0 = block extrapolation between midpoints
-            out = string in 'nearest', 'wrap', 'reflect', 'mirror', 'constant'
+        """ Parameters
+            -----------
+                data = array or list of lists
+
+            Optional Parameters
+            -------------------
+                starting_coords = None, list, 1D array or 2D (nx1) array
+                        spatial coordinate where data[0,..., 0] is measured.
+                        Defaults to zeros.
+                spacings = None, list, 1D array or 2D (nx1) array
+                        spacings[i] is spacing along axis i
+                        defaults to all ones
+                kind = string or integer
+                        indicates type of interpolation to perform
+                out = string in 'nearest', 'wrap', 'reflect', 'mirror', 'constant'
or just NaN
"""

@@ -167,32 +176,25 @@
1:1,
'1':1,
'linear':1,
-                'Linear':1,
2:2,
'2':2,
3:3,
'3':3,
'spline':3,
-                'Spline':3,
'cubic':3,
-                'Cubic':3,
+                'natural':3,
4:4,
'4':4,
'quartic':4,
-                'Quartic':4,
5:5,
'5':5,
'quintic':5,
'quint':5,
-                'Quintic':5,
-                'Quint':5
}
-        if order_dict.has_key(kind):
-            self.order = order_dict[kind]
+        if order_dict.has_key(str(kind).lower()):
+            self.order = order_dict[str(kind).lower()]
elif isinstance(kind, int):
raise ValueError, "Only spline orders 0, 1, ..., 5 are supported"
else:
@@ -289,7 +291,10 @@
return num_indices_in_bounds == self.ndim

def _spline_filter(self, data, order = 3):
-        """ Multi-dimensional spline filter.
+        """ This step is required by the extension module.  I (Field Cady)
+            do not fully understand why.
+
+            Multi-dimensional spline filter.

Note: The multi-dimensional filter is implemented as a sequence of
one-dimensional spline filters. The intermediate arrays are stored

Modified: branches/Interpolate1D/tests/test_interpolate2d.py
===================================================================
--- branches/Interpolate1D/tests/test_interpolate2d.py	2008-08-11 23:12:21 UTC (rev 4640)
+++ branches/Interpolate1D/tests/test_interpolate2d.py	2008-08-12 00:11:21 UTC (rev 4641)
@@ -129,7 +129,7 @@
def test_string_linear(self):
""" make sure : string 'linear' works
"""
-        N = 700
+        N = 7
X, Y = meshgrid(arange(N), arange(N))
Z = X + Y
x, y, z = map(ravel, [X, Y, Z] )
@@ -145,7 +145,7 @@
""" make sure : string 'quadratic' works
"""
-        N = 700
+        N = 7
X, Y = meshgrid(arange(N), arange(N))
Z = X + Y
x, y, z = map(ravel, [X, Y, Z] )
@@ -161,7 +161,7 @@
def test_string_cubic(self):
"""make sure : string "cubic" works
"""
-        N = 700
+        N = 7
X, Y = meshgrid(arange(N), arange(N))
Z = X + Y
x, y, z = map(ravel, [X, Y, Z] )
@@ -178,7 +178,7 @@
""" make sure : keyword '526' works
ie that TOMS algorithm 526 works
"""
-        N = 700
+        N = 7
X, Y = meshgrid(arange(N), arange(N))
Z = X + Y
x, y, z = map(ravel, [X, Y, Z] )

```