def fitwin(x,y): def fit(x, params): a, b, c, d = params O1 = 0.9295 #principal lunar M2 = 1.9324 #principal lunar return a * (numpy.cos(2*pi*O1*x + b)) + c * (numpy.cos(2*pi*M2*x + d)) a = FitParam("O1 Amp", 0., -10., 10.) b = FitParam("O1 Shift", 0., -10., 10.,logscale="True") c = FitParam("M2 Amp", 0., -10., 10.) d = FitParam("M2 Shift", 0., -10., 10.,logscale="True") params = [a, b, c, d] values = guifit(x, y, fit, params, xlabel="Time (s)", ylabel="amplitude (ft)") return values
def test(): x = np.linspace(-10, 10, 1000) y = np.cos(1.5*x)+np.random.rand(x.shape[0])*.2 def fit(x, params): a, b = params return np.cos(b*x)+a a = FitParam("Offset", 1., 0., 2.) b = FitParam("Frequency", 2., 1., 10., logscale=True) params = [a, b] values = guifit(x, y, fit, params, xlabel="Time (s)", ylabel="Power (a.u.)") print(values) print([param.value for param in params])
def gaussfit(x,y): if not isinstance(x,np.ndarray): x=np.array(x) y=np.array(y) from guiqwt.widgets.fit import FitParam, guifit def fit(x, params): A, mu, FWHM,b = params return A*np.exp(-4*math.log(2)*np.power((x-mu),2)/math.pow(FWHM,2))+b A = FitParam("Amplitude", y.max(), 0., y.max()) mu = FitParam("mu", x[len(x)/2], x[0], x[-1]) FWHM = FitParam("FWHM", (x.max()-x.min())/4, 0., x.max()-x.min()) b = FitParam("Offset", 0, 0., y.max()) params = [A, mu, FWHM,b] values = guifit(x, y, fit, params, xlabel="Time (s)", ylabel="Power (a.u.)",auto_fit=True) return(values)
def fitwin(x, y): def fit(x, params): a, b, c, d = params O1 = 0.9295 #principal lunar M2 = 1.9324 #principal lunar return a * (numpy.cos(2 * pi * O1 * x + b)) + c * (numpy.cos(2 * pi * M2 * x + d)) a = FitParam("O1 Amp", 0., -10., 10.) b = FitParam("O1 Shift", 0., -10., 10., logscale="True") c = FitParam("M2 Amp", 0., -10., 10.) d = FitParam("M2 Shift", 0., -10., 10., logscale="True") params = [a, b, c, d] values = guifit(x, y, fit, params, xlabel="Time (s)", ylabel="amplitude (ft)") return values
def gaussfit(x, y): if not isinstance(x, np.ndarray): x = np.array(x) y = np.array(y) from guiqwt.widgets.fit import FitParam, guifit def fit(x, params): A, mu, FWHM, b = params return A * np.exp(-4 * math.log(2) * np.power( (x - mu), 2) / math.pow(FWHM, 2)) + b A = FitParam("Amplitude", y.max(), 0., y.max()) mu = FitParam("mu", x[len(x) / 2], x[0], x[-1]) FWHM = FitParam("FWHM", (x.max() - x.min()) / 4, 0., x.max() - x.min()) b = FitParam("Offset", 0, 0., y.max()) params = [A, mu, FWHM, b] values = guifit(x, y, fit, params, xlabel="Time (s)", ylabel="Power (a.u.)", auto_fit=True) return (values)
def graphicalfit(self): #SHOW = True # Show test in GUI-based test launcher x=self.x() y=self.y() def fitfn(x, params): #ignore the x for index, key in enumerate(self.fit_params): self.fit_params[key] = float(params[index]) res = self.fn(**self.getparams()) if len(x)!=len(self.x()): s = pandas.Series(res, index=self.x()) res = s[x].values return res self.graphical_params=list() for index, key in enumerate(self.fit_params): fp=FitParam(key, self.fit_params[key],0.001*abs(self.fit_params[key]),10.0*abs(self.fit_params[key]),logscale=False,steps=2000,format='%.8f') self.graphical_params.append(fp) values = guifit(x, y, fitfn, self.graphical_params, xlabel="x-axis", ylabel="y-axis") if values is None: self.gfit_concluded = False else: self.gfit_concluded = True self.comment("Graphical fit finished with the following values: ") self.comment(values) self.comment([param.value for param in self.graphical_params]) self.sqerror = self.getsqerror() self.comment("Fit completed with sqerror = " + str(self.sqerror)) self.comment("Obtained parameter values: ") self.comment(dict(self.getparams())) # evaluate the performed fit in fitdata self.fitdata = pandas.Series(data = self.fn(**self.getparams()), index = self.x(), \ name = 'fitfunction: '+ self.func) return values
interactive = InteractiveFitSettings() if not interactive.edit(size=(640, 1)): sys.exit(0) else: config['file'] = interactive.filename config['function'] = interactive.funcname config['xcol'] = interactive.xcol config['ycol'] = interactive.ycol config['skiprows'] = interactive.skiprows with open(config_file, 'w') as f: pickle.dump(config, f) # Open data file name, extension = os.path.splitext(interactive.filename) if extension == 'csv': delimiter = ',' else: delimiter = None xdata, ydata = np.loadtxt( interactive.filename, delimiter=delimiter, usecols=(interactive.xcol, interactive.ycol), unpack=True, skiprows=interactive.skiprows ) # Fit #x = np.linspace(-10, 10, 1000) #y = np.cos(1.5*x) + np.random.rand(x.shape[0])*.2 #func = Sine(x, y) func = getattr(sys.modules[__name__], interactive.funcname)(xdata, ydata) values = guifit(func.xdata, func.ydata, func.func, func.params)