def guess(self, y, x=None, **kwargs): r"""Guess starting values for the parameters of a model. Parameters ---------- y : :class:`~numpy:numpy.ndarray` Intensities x : :class:`~numpy:numpy.ndarray` energy values kwargs : dict additional optional arguments, passed to model function. Returns ------- :class:`~lmfit.parameter.Parameters` parameters with guessed values """ amplitude = 1.0 center = 0.0 tau = 10.0 beta = 1.0 if x is not None: center = x[index_of(y, max(y))] # assumed peak within domain x # Assumptions: # 1. large dynamic range, function decays fast in domain x # 2. x-values are equispaced amplitude = sum(y) * (max(x) - min(x)) / len(x) tau = max(y) / amplitude # assumed beta==1.0 return self.make_params(amplitude=amplitude, center=center, tau=tau, beta=beta)
def guess(self, data, x, **kwargs): """Guess starting values for the parameters of a model. Parameters ---------- data: :class:`~numpy:numpy.ndarray` data to be fitted x: :class:`~numpy:numpy.ndarray` energy domain where the interpolation required kwargs : dict additional optional arguments, passed to model function. Returns ------- :class:`~lmfit.parameter.Parameters` parameters with guessed values """ params = self.make_params() def pset(param, value): params["%s%s" % (self.prefix, param)].set(value=value) x_at_max = x[models.index_of(data, max(data))] ysim = self.eval(x=x_at_max, amplitude=1, center=x_at_max) amplitude = max(data) / ysim pset("amplitude", amplitude) pset("center", x_at_max) return models.update_param_vals(params, self.prefix, **kwargs)
def guess(self, y, x=None, **kwargs): r"""Guess starting values for the parameters of a model. Parameters ---------- y : sequence of floats Intensities x : sequence of floats energy values kwargs : dict additional optional arguments, passed to model function. Returns ------- Parameters parameters with guessed values """ amplitude = 1.0 center = 0.0 tau = 10.0 beta = 1.0 if x is not None: center = x[index_of(y, max(y))] # assumed peak within domain x # Assumptions: # 1. large dynamic range, function decays fast in domain x # 2. x-values are equispaced amplitude = sum(y)* (max(x)-min(x))/len(x) tau = max(y) / amplitude # assumed beta==1.0 return self.make_params(amplitude=amplitude, center=center, tau=tau, beta=beta)
def guess(self, y, x=None, **kwargs): r"""Estimate fitting parameters from input data Parameters ---------- y : :class:`~numpy:numpy.ndarray` Values to fit to, e.g., SANS or SAXS intensity values x : :class:`~numpy:numpy.ndarray` independent variable, e.g., momentum transfer Returns ------- :class:`~lmfit.parameter.Parameters` Parameters with estimated initial values. """ amplitude = 1.0 center = 0.0 if x is not None: center = x[index_of(y, max(y))] # assumed peak within domain x amplitude = max(y) return self.make_params(amplitude=amplitude, center=center)
def guess(self, y, x=None, **kwargs): r"""Guess starting values for the parameters of a model. Parameters ---------- y : :class:`~numpy:numpy.ndarray` Intensities x : :class:`~numpy:numpy.ndarray` energy values kwargs : dict additional optional arguments, passed to model function. Returns ------- :class:`~lmfit.parameter.Parameters` parameters with guessed values """ amplitude = max(y) center = 0.0 if x is not None: center = x[index_of(y, max(y))] dx = (x[-1] - x[0]) / (len(x) - 1) # x-spacing amplitude /= dx return self.make_params(amplitude=amplitude, center=center)
def guess_from_peak_2D(model, y, x, negative, ampscale=1.0, sigscale=1.0, amp_area=True): """Estimate starting paramters 2D Peak fits The parameters are: - Amplitude (can be area or peak, see paramter:amp_area - X Center - Y Center - X Sigma - Y Sigma Parameters ---------- model : instance of lmfit model class Must be a model of a 2D function with has parameters specified above y : y-data to which the model is fitted Must be 1D flattened array x : 2D np.array (or 2 element list of 1D np.array) x-data - must be a 2D array, each of which has the same dimension as y negative: boolean Specify if the peak is an inverted peak ampscale: np.float Scale the amplitude estimated by this factor sigscale: np.float Scale the widths estimated by this factor amp_area: boolean Specify if the multiplicative coefficient of the 2D function is amplitude or area Returns ------- params object (see lmfit Model documentation for more details) """ if x is None: return 1.0, 0.0, 0.0, 1.0, 1.0 x0 = x[0] x1 = x[1] maxy, miny = np.nanmax(y), np.nanmin(y) maxx0, minx0 = max(x0), min(x0) maxx1, minx1 = max(x1), min(x1) imaxy = index_of(y, maxy) #amp = (maxy - miny) amp = maxy - (y[0]+y[-1])/2.0 cen_x = x0[imaxy] cen_y = x1[imaxy] sig_x = (maxx0-minx0)/6.0 sig_y = (maxx1-minx1)/6.0 halfmax_vals = np.where(y > (maxy+miny)/2.0)[0] if negative: imaxy = index_of(y, miny) amp = -(maxy - miny)*2.0 halfmax_vals = np.where(y < (maxy+miny)/2.0)[0] if len(halfmax_vals) > 2: sig_x = abs((x0[halfmax_vals[-1]] - x0[halfmax_vals[0]])/2.0) sig_y = abs((x1[halfmax_vals[-1]] - x1[halfmax_vals[0]])/2.0) cen_x = x0[halfmax_vals].mean() cen_y = x1[halfmax_vals].mean() amp = amp*ampscale if amp_area: amp *= sig_x*sig_y*4.0 sig_x = sig_x*sigscale sig_y = sig_y*sigscale pars = model.make_params(amplitude=amp, center_x=cen_x, center_y=cen_y, sigma_x=sig_x, sigma_y=sig_y) pars['%ssigma_x' % model.prefix].set(min=0.0) pars['%ssigma_y' % model.prefix].set(min=0.0) return pars
def guess_from_peak(model, y, x, negative, ampscale=1.0, sigscale=1.0, amp_area=True): """Estimate starting paramters 2D Peak fits The parameters are: - Amplitude (can be area or peak, see paramter:amp_area - Center - Sigma Parameters ---------- model : instance of lmfit model class Must be a model of a 2D function with has parameters specified above y : 1D np.array y-data to which the model is fitted x : 1D np.array x-data to which the model is fitted negative: boolean Specify if the peak is an inverted peak ampscale: float Scale the amplitude estimated by this factor sigscale: float Scale the widths estimated by this factor amp_area: boolean Specify if the multiplicative coefficient of the function is amplitude or area Returns ------- params object (see lmfit Model documentation for more details) """ if x is None: return 1.0, 0.0, 1.0 maxy, miny = max(y), min(y) maxx, minx = max(x), min(x) imaxy = index_of(y, maxy) #amp = (maxy - miny) amp = maxy - (y[0]+y[-1])/2.0 cen = x[imaxy] sig = (maxx-minx)/6.0 halfmax_vals = np.where(y > (maxy+miny)/2.0)[0] if negative: imaxy = index_of(y, miny) amp = -(maxy - miny)*2.0 halfmax_vals = np.where(y < (maxy+miny)/2.0)[0] if len(halfmax_vals) > 2: sig = (x[halfmax_vals[-1]] - x[halfmax_vals[0]])/2.0 cen = x[halfmax_vals].mean() amp = amp*ampscale if amp_area: amp *= sig*2.0 sig = sig*sigscale pars = model.make_params(amplitude=amp, center=cen, sigma=sig) pars['%ssigma' % model.prefix].set(min=0.0) return pars
def guess_from_peak_2D(model, y, x, negative, ampscale=1.0, sigscale=1.0, amp_area=True): """Estimate starting paramters 2D Peak fits The parameters are: - Amplitude (can be area or peak, see paramter:amp_area - X Center - Y Center - X Sigma - Y Sigma Parameters ---------- model : instance of lmfit model class Must be a model of a 2D function with has parameters specified above y : y-data to which the model is fitted Must be 1D flattened array x : 2D np.array (or 2 element list of 1D np.array) x-data - must be a 2D array, each of which has the same dimension as y negative: boolean Specify if the peak is an inverted peak ampscale: np.float Scale the amplitude estimated by this factor sigscale: np.float Scale the widths estimated by this factor amp_area: boolean Specify if the multiplicative coefficient of the 2D function is amplitude or area Returns ------- params object (see lmfit Model documentation for more details) """ if x is None: return 1.0, 0.0, 0.0, 1.0, 1.0 x0 = x[0] x1 = x[1] maxy, miny = np.nanmax(y), np.nanmin(y) maxx0, minx0 = max(x0), min(x0) maxx1, minx1 = max(x1), min(x1) imaxy = index_of(y, maxy) #amp = (maxy - miny) amp = maxy - (y[0] + y[-1]) / 2.0 cen_x = x0[imaxy] cen_y = x1[imaxy] sig_x = (maxx0 - minx0) / 6.0 sig_y = (maxx1 - minx1) / 6.0 halfmax_vals = np.where(y > (maxy + miny) / 2.0)[0] if negative: imaxy = index_of(y, miny) amp = -(maxy - miny) * 2.0 halfmax_vals = np.where(y < (maxy + miny) / 2.0)[0] if len(halfmax_vals) > 2: sig_x = abs((x0[halfmax_vals[-1]] - x0[halfmax_vals[0]]) / 2.0) sig_y = abs((x1[halfmax_vals[-1]] - x1[halfmax_vals[0]]) / 2.0) cen_x = x0[halfmax_vals].mean() cen_y = x1[halfmax_vals].mean() amp = amp * ampscale if amp_area: amp *= sig_x * sig_y * 4.0 sig_x = sig_x * sigscale sig_y = sig_y * sigscale pars = model.make_params(amplitude=amp, center_x=cen_x, center_y=cen_y, sigma_x=sig_x, sigma_y=sig_y) pars['%ssigma_x' % model.prefix].set(min=0.0) pars['%ssigma_y' % model.prefix].set(min=0.0) return pars
def guess_from_peak(model, y, x, negative, ampscale=1.0, sigscale=1.0, amp_area=True): """Estimate starting paramters 2D Peak fits The parameters are: - Amplitude (can be area or peak, see paramter:amp_area - Center - Sigma Parameters ---------- model : instance of lmfit model class Must be a model of a 2D function with has parameters specified above y : 1D np.array y-data to which the model is fitted x : 1D np.array x-data to which the model is fitted negative: boolean Specify if the peak is an inverted peak ampscale: float Scale the amplitude estimated by this factor sigscale: float Scale the widths estimated by this factor amp_area: boolean Specify if the multiplicative coefficient of the function is amplitude or area Returns ------- params object (see lmfit Model documentation for more details) """ if x is None: return 1.0, 0.0, 1.0 maxy, miny = max(y), min(y) maxx, minx = max(x), min(x) imaxy = index_of(y, maxy) #amp = (maxy - miny) amp = maxy - (y[0] + y[-1]) / 2.0 cen = x[imaxy] sig = (maxx - minx) / 6.0 halfmax_vals = np.where(y > (maxy + miny) / 2.0)[0] if negative: imaxy = index_of(y, miny) amp = -(maxy - miny) * 2.0 halfmax_vals = np.where(y < (maxy + miny) / 2.0)[0] if len(halfmax_vals) > 2: sig = (x[halfmax_vals[-1]] - x[halfmax_vals[0]]) / 2.0 cen = x[halfmax_vals].mean() amp = amp * ampscale if amp_area: amp *= sig * 2.0 sig = sig * sigscale pars = model.make_params(amplitude=amp, center=cen, sigma=sig) pars['%ssigma' % model.prefix].set(min=0.0) return pars