Ejemplo n.º 1
0
    def _interpolate(self, x, z, **kwds):
        """interpolate data (x,z) to generate response function z=f(*x)

        Input:
          x: an array of shape (npts, dim) or (npts,)
          z: an array of shape (npts,) or (npts, N)

        Additional Inputs:
          method: string for kind of interpolator
          extrap: if True, extrapolate a bounding box (can reduce # of nans)
          arrays: if True, return a numpy array; otherwise don't return arrays
          axis: int in [0,N], index of z to interpolate (all, by default)

        Output:
          interpolated response function, where z=f(*x.T)

        NOTE:
          if scipy is not installed, will use np.interp for 1D (non-rbf),
          or mystic's rbf otherwise. default method is 'nearest' for
          1D and 'linear' otherwise. method can be one of ('rbf','linear',
          'nearest','cubic','inverse','gaussian','quintic','thin_plate').

        NOTE:
          additional keyword arguments (epsilon, smooth, norm) are avaiable
          for use with a Rbf interpolator. See mystic.math.interpolate.Rbf
          for more details.
        """
        import numpy as np
        from mystic.math.interpolate import interpf
        with np.warnings.catch_warnings():
            np.warnings.filterwarnings('ignore')
            f = interpf(x, z, **kwds)
        return f
Ejemplo n.º 2
0
def read(archives):  # method?
    """read stored functions from the list of dbs

    Args:
        archives (list[string]): list of names of function archives

    Returns:
        a klepto.archive instance

    Notes:
        The order of the dbs is important, with the index of ``archives`` 
        corresponding to the desired axis. If a db is empty, returns ``None``
        for the empty db. Also, a klepto.archive instance can be provided
        instead of the ``name`` of the db.
    """
    if isinstance(archives, (str, (u'').__class__)):
        archives = db(archives)
    if type(archives).__module__.startswith('klepto.'):
        f = rb.read_func(db(archives))
        if f is None:
            return f
        f = f[0]
        if not hasattr(f, '__axis__'):
            f.__axis__ = None
        return f
    from mystic.math.interpolate import interpf
    f = interpf([[1, 2], [2, 3]], [[1, 2], [2, 3]], method='thin_plate')
    f.__axis__[:] = [(i if i is None else i[0])
                     for i in map(rb.read_func, map(db, archives))]
    return f
Ejemplo n.º 3
0
    def __init__(self, x, z=None, function=None, **kwds):
        """scatter plotter for data (x,z) and response surface function(*x)

        Input:
          x: an array of shape (npts, dim) or (npts,)
          z: an array of shape (npts,)
          function: function f, where z=f(*x.T), or str (interpolation method)

        Additional Inputs:
          step: int, plot every 'step' points on the grid [default: 200]
          scale: float, scaling factor for the z-axis [default: False]
          shift: float, additive shift for the z-axis [default: False]
          density: int, density of wireframe for the plot surface [default: 9]
          axes: tuple, indicies of the axes to plot [default: ()]
          vals: list of values (one per axis) for unplotted axes [default: ()]
          maxpts: int, maximum number of (x,z) points to use [default: None]
          kernel: function transforming x to x', where x' = kernel(x)
          vtol: float, maximum distance outside bounds hypercube to plot data

        NOTE:
          if scipy is not installed, will use np.interp for 1D (non-rbf),
          or mystic's rbf otherwise. default method is 'nearest' for
          1D and 'linear' otherwise. method can be one of ('rbf','linear',
          'nearest','cubic','inverse','gaussian','quintic','thin_plate').
        """
        self.x = getattr(x, '_x', x)  # params (x)
        self.z = x._y if z is None else z  # cost (f(x))
        if function is None:
            function = 'linear'
        if type(function) is str:
            from mystic.math.interpolate import interpf
            function = interpf(self.x, self.z, method=function,
                               arrays=True)  #XXX: kwds?
        self.function = function
        #self.dim = kwds.pop('dim', None) #XXX: or len(x)?
        # interpolator configuration
        self.args = dict(step=200, scale=False, shift=False, vtol=None, \
            kernel=None, density=9, axes=(), vals=(), maxpts=None)
        self.args.update(kwds)
        self.maxpts = self.args.pop('maxpts')
        return
Ejemplo n.º 4
0
    def __init__(self, x, z=None, function=None, **kwds):
        """scatter plotter for data (x,z) and response surface function(*x)

        Input:
          x: an array of shape (npts, dim) or (npts,)
          z: an array of shape (npts,)
          function: function f, where z=f(*x.T), or str (interpolation method)

        Additional Inputs:
          step: int, plot every 'step' points on the grid [default: 200]
          scale: float, scaling factor for the z-axis [default: False]
          shift: float, additive shift for the z-axis [default: False]
          density: int, density of wireframe for the plot surface [default: 9]
          axes: tuple, indicies of the axes to plot [default: ()]
          vals: list of values (one per axis) for unplotted axes [default: ()]
          maxpts: int, maximum number of (x,z) points to use [default: None]
          kernel: function transforming x to x', where x' = kernel(x)

        NOTE:
          if scipy is not installed, will use np.interp for 1D (non-rbf),
          or mystic's rbf otherwise. default method is 'nearest' for
          1D and 'linear' otherwise. method can be one of ('rbf','linear',
          'nearest','cubic','inverse','gaussian','quintic','thin_plate').
        """
        self.x = getattr(x, '_x', x)  # params (x)
        self.z = x._y if z is None else z # cost (f(x))
        if function is None:
            function='linear'
        if type(function) is str:
            from mystic.math.interpolate import interpf
            function = interpf(self.x,self.z, method=function, arrays=True) #XXX: kwds?
        self.function = function
       #self.dim = kwds.pop('dim', None) #XXX: or len(x)?
        # interpolator configuration
        self.args = dict(step=200, scale=False, shift=False, \
            kernel=None, density=9, axes=(), vals=(), maxpts=None)
        self.args.update(kwds)
        self.maxpts = self.args.pop('maxpts')
        return
Ejemplo n.º 5
0
    def _interpolate(self, x, z, **kwds):
        """interpolate data (x,z) to generate response function z=f(*x)

        Input:
          x: an array of shape (npts, dim) or (npts,)
          z: an array of shape (npts,)

        Additional Inputs:
          method: string for kind of interpolator
          extrap: if True, extrapolate a bounding box (can reduce # of nans)
          arrays: if True, return a numpy array; otherwise don't return arrays

        Output:
          interpolated response function, where z=f(*x.T)

        NOTE:
          if scipy is not installed, will use np.interp for 1D (non-rbf),
          or mystic's rbf otherwise. default method is 'nearest' for
          1D and 'linear' otherwise. method can be one of ('rbf','linear',
          'nearest','cubic','inverse','gaussian','quintic','thin_plate').
        """
        from mystic.math.interpolate import interpf
        return interpf(x, z, **kwds)
Ejemplo n.º 6
0
    def _interpolate(self, x, z, **kwds):
        """interpolate data (x,z) to generate response function z=f(*x)

        Input:
          x: an array of shape (npts, dim) or (npts,)
          z: an array of shape (npts,)

        Additional Inputs:
          method: string for kind of interpolator
          extrap: if True, extrapolate a bounding box (can reduce # of nans)
          arrays: if True, return a numpy array; otherwise don't return arrays

        Output:
          interpolated response function, where z=f(*x.T)

        NOTE:
          if scipy is not installed, will use np.interp for 1D (non-rbf),
          or mystic's rbf otherwise. default method is 'nearest' for
          1D and 'linear' otherwise. method can be one of ('rbf','linear',
          'nearest','cubic','inverse','gaussian','quintic','thin_plate').
        """
        from mystic.math.interpolate import interpf
        return interpf(x, z, **kwds)
Ejemplo n.º 7
0
test _to_function and _to_objective in cost and gradient calculations
"""

import numpy as np
x = np.random.rand(10,3)

def cost(x):
   return sum(np.sin(x)**2) + 1

y = cost(x.T)

import mystic.math.interpolate as ip
fx = ip._to_function(cost)
assert (fx(*x.T) - cost(x.T) ).sum() < 0.0001

f = ip.interpf(x, y, method='linear', arrays=True)
cf = ip._to_objective(f)
assert (f(*x.T) - cf(x.T) ).sum() < 0.0001

assert f(*x[0].T) == cf(x[0].T)
assert fx(*x[0].T) == cost(x[0].T)

assert ip.gradient(x, f, method='linear').shape \
    == ip.gradient(x, fx, method='linear').shape


from mystic.models import rosen0der as rosen
y = rosen(x.T)

f = ip.interpf(x, y, method='linear')
fx = ip._to_function(rosen)
Ejemplo n.º 8
0
test _to_function and _to_objective in cost and gradient calculations
"""

import numpy as np
x = np.random.rand(10,3)

def cost(x):
   return sum(np.sin(x)**2) + 1

y = cost(x.T)

import mystic.math.interpolate as ip
fx = ip._to_function(cost)
assert (fx(*x.T) - cost(x.T) ).sum() < 0.0001

f = ip.interpf(x, y, method='linear', arrays=True)
cf = ip._to_objective(f)
assert (f(*x.T) - cf(x.T) ).sum() < 0.0001

assert f(*x[0].T) == cf(x[0].T)
assert fx(*x[0].T) == cost(x[0].T)

assert ip.gradient(x, f, method='linear').shape \
    == ip.gradient(x, fx, method='linear').shape


from mystic.models import rosen0der as rosen
y = rosen(x.T)

f = ip.interpf(x, y, method='linear')
fx = ip._to_function(rosen)