@@ -2556,145 +2556,6 @@ def newfunc(val, mask, mval):
25562556 fh .close ()
25572557
25582558
2559- ##################################################
2560- # Linear interpolation algorithms
2561- ##################################################
2562- @cbook .deprecated ("2.2" )
2563- def slopes (x , y ):
2564- """
2565- :func:`slopes` calculates the slope *y*'(*x*)
2566-
2567- The slope is estimated using the slope obtained from that of a
2568- parabola through any three consecutive points.
2569-
2570- This method should be superior to that described in the appendix
2571- of A CONSISTENTLY WELL BEHAVED METHOD OF INTERPOLATION by Russel
2572- W. Stineman (Creative Computing July 1980) in at least one aspect:
2573-
2574- Circles for interpolation demand a known aspect ratio between
2575- *x*- and *y*-values. For many functions, however, the abscissa
2576- are given in different dimensions, so an aspect ratio is
2577- completely arbitrary.
2578-
2579- The parabola method gives very similar results to the circle
2580- method for most regular cases but behaves much better in special
2581- cases.
2582-
2583- Norbert Nemec, Institute of Theoretical Physics, University or
2584- Regensburg, April 2006 Norbert.Nemec at physik.uni-regensburg.de
2585-
2586- (inspired by a original implementation by Halldor Bjornsson,
2587- Icelandic Meteorological Office, March 2006 halldor at vedur.is)
2588- """
2589- # Cast key variables as float.
2590- x = np .asarray (x , float )
2591- y = np .asarray (y , float )
2592-
2593- yp = np .zeros (y .shape , float )
2594-
2595- dx = x [1 :] - x [:- 1 ]
2596- dy = y [1 :] - y [:- 1 ]
2597- dydx = dy / dx
2598- yp [1 :- 1 ] = (dydx [:- 1 ] * dx [1 :] + dydx [1 :] * dx [:- 1 ])/ (dx [1 :] + dx [:- 1 ])
2599- yp [0 ] = 2.0 * dy [0 ]/ dx [0 ] - yp [1 ]
2600- yp [- 1 ] = 2.0 * dy [- 1 ]/ dx [- 1 ] - yp [- 2 ]
2601- return yp
2602-
2603-
2604- @cbook .deprecated ("2.2" )
2605- def stineman_interp (xi , x , y , yp = None ):
2606- """
2607- Given data vectors *x* and *y*, the slope vector *yp* and a new
2608- abscissa vector *xi*, the function :func:`stineman_interp` uses
2609- Stineman interpolation to calculate a vector *yi* corresponding to
2610- *xi*.
2611-
2612- Here's an example that generates a coarse sine curve, then
2613- interpolates over a finer abscissa::
2614-
2615- x = linspace(0,2*pi,20); y = sin(x); yp = cos(x)
2616- xi = linspace(0,2*pi,40);
2617- yi = stineman_interp(xi,x,y,yp);
2618- plot(x,y,'o',xi,yi)
2619-
2620- The interpolation method is described in the article A
2621- CONSISTENTLY WELL BEHAVED METHOD OF INTERPOLATION by Russell
2622- W. Stineman. The article appeared in the July 1980 issue of
2623- Creative Computing with a note from the editor stating that while
2624- they were:
2625-
2626- not an academic journal but once in a while something serious
2627- and original comes in adding that this was
2628- "apparently a real solution" to a well known problem.
2629-
2630- For *yp* = *None*, the routine automatically determines the slopes
2631- using the :func:`slopes` routine.
2632-
2633- *x* is assumed to be sorted in increasing order.
2634-
2635- For values ``xi[j] < x[0]`` or ``xi[j] > x[-1]``, the routine
2636- tries an extrapolation. The relevance of the data obtained from
2637- this, of course, is questionable...
2638-
2639- Original implementation by Halldor Bjornsson, Icelandic
2640- Meteorolocial Office, March 2006 halldor at vedur.is
2641-
2642- Completely reworked and optimized for Python by Norbert Nemec,
2643- Institute of Theoretical Physics, University or Regensburg, April
2644- 2006 Norbert.Nemec at physik.uni-regensburg.de
2645- """
2646-
2647- # Cast key variables as float.
2648- x = np .asarray (x , float )
2649- y = np .asarray (y , float )
2650- if x .shape != y .shape :
2651- raise ValueError ("'x' and 'y' must be of same shape" )
2652-
2653- if yp is None :
2654- yp = slopes (x , y )
2655- else :
2656- yp = np .asarray (yp , float )
2657-
2658- xi = np .asarray (xi , float )
2659-
2660- # calculate linear slopes
2661- dx = x [1 :] - x [:- 1 ]
2662- dy = y [1 :] - y [:- 1 ]
2663- s = dy / dx # note length of s is N-1 so last element is #N-2
2664-
2665- # find the segment each xi is in
2666- # this line actually is the key to the efficiency of this implementation
2667- idx = np .searchsorted (x [1 :- 1 ], xi )
2668-
2669- # now we have generally: x[idx[j]] <= xi[j] <= x[idx[j]+1]
2670- # except at the boundaries, where it may be that xi[j] < x[0] or
2671- # xi[j] > x[-1]
2672-
2673- # the y-values that would come out from a linear interpolation:
2674- sidx = s .take (idx )
2675- xidx = x .take (idx )
2676- yidx = y .take (idx )
2677- xidxp1 = x .take (idx + 1 )
2678- yo = yidx + sidx * (xi - xidx )
2679-
2680- # the difference that comes when using the slopes given in yp
2681- # using the yp slope of the left point
2682- dy1 = (yp .take (idx ) - sidx ) * (xi - xidx )
2683- # using the yp slope of the right point
2684- dy2 = (yp .take (idx + 1 )- sidx ) * (xi - xidxp1 )
2685-
2686- dy1dy2 = dy1 * dy2
2687- # The following is optimized for Python. The solution actually
2688- # does more calculations than necessary but exploiting the power
2689- # of numpy, this is far more efficient than coding a loop by hand
2690- # in Python
2691- yi = yo + dy1dy2 * np .choose (np .array (np .sign (dy1dy2 ), np .int32 )+ 1 ,
2692- ((2 * xi - xidx - xidxp1 )/ ((dy1 - dy2 )* (xidxp1 - xidx )),
2693- 0.0 ,
2694- 1 / (dy1 + dy2 ),))
2695- return yi
2696-
2697-
26982559class GaussianKDE (object ):
26992560 """
27002561 Representation of a kernel-density estimate using Gaussian kernels.
0 commit comments