def Interpolate(self, **kwds): #XXX: refactor so use self.interpolator ? """interpolate data (x,z) to generate response function z=f(*x) Input: maxpts: int, maximum number of points to use from (x,z) noise: float, amplitude of gaussian noise to remove duplicate x 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 interpolator import Interpolator args = self.args.copy() args.update(kwds) maxpts, noise = self.maxpts, self.noise ii = Interpolator(self.x, self.z, maxpts=maxpts, noise=noise, **args) self.surrogate = ii.Interpolate(**args) # build the surrogate self.surrogate.__doc__ = self.objective.__doc__ return self.surrogate
def fit(self, **kwds): """generate an interpolated model from data Input: data: a mystic legacydata.dataset (or callable model, y = model(x)) rnd: bool, if False, treat the model as deterministic [default: True] cached: bool, if True, use a mystic.cache [default: False] NOTE: any additional keyword arguments will be passed to the interpolator NOTE: if data is a model, interpolator will use model's cached archive """ self.__kwds__.update(kwds) cached = self.__kwds__.pop('cached', False) archive = data = self.__kwds__.pop('data', None) self.rnd = self.__kwds__.pop('rnd', self.rnd) and ( bool(self.__kwds__.get('noise', True)) ) # or callable(data) or isinstance(data, type(''))) if callable(data): #XXX: is a model, allow this? data = sample(data, bounds=None, pts=0) #XXX: axis? multivalue? elif isinstance(data, type('')): #XXX: is a name, allow this? import mystic.cache as mc import dataset as ds data = ds.from_archive(mc.archive.read(data)) x = getattr(data, 'coords', getattr(data, 'x', None)) z = getattr(data, 'values', getattr(data, 'y', None)) from interpolator import Interpolator terp = Interpolator(x, z, **self.__kwds__) self.__func__ = terp.Interpolate() #XXX: ValueError: zero-size self.__model__ = _init_axis(terp.model) self.__model__.__name__ = self.__name__ self.__kwds__['data'] = archive if cached: #FIXME: clear the archive??? generate new uid name? self.__kwds__['cached'] = True self._OUQModel__init_cache() if hasattr(self.__model__, '__cache__'): c = self.__model__.__cache__() c.clear() else: self.__kwds__['cached'] = False return