def fitdistribution(self): self.standardize() f = Fitter(self.data.func) f.fit() # may take some time since by default, all distributions are tried # but you call manually provide a smaller set of distributions return f.summary()
def makeSigmaFit(darkTimes, sigmas): dt, sdt = list(zip(*darkTimes)) s, ss = list(zip(*sigmas)) data = DataErrors.fromLists(dt, s, sdt, ss) c = TCanvas('c_sigma', '', 1280, 720) g = data.makeGraph('g_sigma', 'Dunkelzeit t_{D} / ms', 'Verschmierung #sigma / #mus') g.Draw('APX') fit = Fitter('fit_sigma', 'pol1(0)') fit.setParam(0, 'a', 0) fit.setParam(1, 'b', 1) fit.fit(g, 0, 25) fit.saveData('../fit/sigma.txt') l = TLegend(0.6, 0.15, 0.85, 0.5) l.SetTextSize(0.03) l.AddEntry(g, 'Verschmierung #sigma', 'p') l.AddEntry(None, 'der Fermi-Verteilung', '') l.AddEntry(fit.function, 'Fit mit #sigma(t_{D}) = a + b t_{D}', 'l') fit.addParamsToLegend(l, (('%.2f', '%.2f'), ('%.2f', '%.2f')), chisquareformat='%.2f', units=['#mus', '10^{-3}']) l.Draw() g.Draw('P') c.Print('../img/part6/sigmaFit.pdf', 'pdf')
def main(): z, sz = 840, 40 d = [210, 106, 75] sd = [10, 4, 4] calc = list(map(lambda x: -20 * log10(x / z), d)) scalc = list( map(lambda x: 20 * sqrt((x[1] / x[0])**2 + (sz / z)**2) / log(10), zip(*[d, sd]))) data = DataErrors.fromLists(calc, [12, 18, 21], scalc, [0] * 3) c = TCanvas('c', '', 1280, 720) g = data.makeGraph('g', 'measured attenuation m / dB', 'nominal value n / dB') g.Draw('AP') fit = Fitter('fit', 'pol1(0)') fit.setParam(0, 'a', 0) fit.setParam(1, 'b', 1) fit.fit(g, 11, 22) fit.saveData('../fit/attenuator.txt') l = TLegend(0.15, 0.6, 0.5, 0.85) l.SetTextSize(0.03) l.AddEntry(g, 'measured att. vs. nominal value', 'p') l.AddEntry(fit.function, 'fit with n = a + b m', 'l') fit.addParamsToLegend(l, (('%.2f', '%.2f'), ('%.2f', '%.2f')), chisquareformat='%.4f', units=['dB', ''], lang='en') l.Draw() c.Update() c.Print('../img/attenuator.pdf', 'pdf')
def fit_exp(miss): dist = ['expon'] f = Fitter(miss, distributions=dist, timeout=600) f.fit() # logmsg('fitted params exp = %s', str(f.fitted_param)) # f.summary() # plt.show() return f.df_errors['expon']
def __init__(self, V, I, mask=None, **kw): Fitter.__init__(self, V, I, **kw) self.engine = self.magfit self.do_var = np.array( [1, 1, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 'i') self.yfit = np.zeros_like(self.y)
def calculate_text_similarity_distribution(reviewers_df, cdfs): sample = reviewers_df['similarity_index'].tolist() f = Fitter(sample, distributions=cdfs) f.fit() best = f.get_best() key = list(best.keys())[0] dist = eval("sc." + key) distribution_index = dist.pdf(sample, *(f.fitted_param[key])) return distribution_index
def calculate_average_helpfulness_distribution(reviewers_df, cdfs): sample = reviewers_df['avg_helpfulness'].tolist() f = Fitter(sample, distributions=cdfs) f.fit() best = f.get_best() key = list(best.keys())[0] dist = eval("sc." + key) distribution_avg = dist.pdf(sample, *(f.fitted_param[key])) return distribution_avg
def fitLaserVoltage(g, xmin, xmax, file): fit = Fitter('%s-laser' % file[:-4], 'pol1(0)') fit.function.SetLineColor(92) fit.function.SetLineWidth(2) fit.setParam(0, 'a', 0) fit.setParam(1, 'b', 100) fit.fit(g, xmin, xmax, '+') fit.saveData('../fit/part2/%s-laser.txt' % file) return (fit.params[1]['value'], fit.params[1]['error'], fit.function)
def calculate_rating_deviation_distribution(reviewers_df, cdfs): sample = reviewers_df['rating_deviation'].tolist() f = Fitter(sample, distributions=cdfs) f.fit() best = f.get_best() key = list(best.keys())[0] dist = eval("sc." + key) distribution_rating = dist.pdf(sample, *(f.fitted_param[key])) return distribution_rating
def fitT(x0): # get data and make graph data = TData.fromPath('../data/x0_%dmm.txt' % x0) c = TCanvas('c_%d' % x0, '', 1280, 720) g = data.makeGraph('g_%d' % x0, 'Kreisfrequenz #omega / (rad/ms)', 'Differenzenfrequenz #Delta#nu / kHz') g.Draw('AP') # fit fit = Fitter('fit_%d' % x0, 'pol1(0)') fit.setParam(0, 'a') fit.setParam(1, 'b') fit.fit(g, min(data.getX()) - 3, max(data.getX()) + 3) fit.saveData('../calc/fit_x0_%dmm.txt' % x0, 'w') # legend l = TLegend(0.15, 0.625, 0.425, 0.85) l.SetTextSize(0.03) l.AddEntry(g, 'Messreihe bei x_{0}\' = %d mm' % x0, 'p') l.AddEntry(fit.function, 'Fit mit #Delta#nu (#omega) = a + b*#omega', 'l') fit.addParamsToLegend(l, [('%.1f', '%.1f'), ('%.0f', '%.0f')], chisquareformat='%.2f') l.Draw() # print c.Update() c.Print('../img/fit_x0_%dmm.pdf' % x0, 'pdf') return [(fit.params[0]['value'], fit.params[0]['error']), (fit.params[1]['value'], fit.params[1]['error'])]
def multiPeakFit(g, ufunc, uparams, params, xstart, xend): """fits all peaks with one function Arguments: g -- graph ufunc -- underground function uparams -- start parameter for underground function params -- params for peaks xstart -- start x value xend -- end x value """ # build fit function fitfunc = ufunc pstart = len(uparams) for i in xrange(len(params)): fitfunc += ' + gaus(%d)' % (pstart + 3 * i) # create fitter, change npx to high value fit = Fitter('f', fitfunc) fit.function.SetNpx(10000) # set underground params for i, param in enumerate(uparams): fit.setParam(i, chr(97 + i), param) # params of underground are enumerated with a, b, c, ... # set peak params for i, param in enumerate(params): fit.setParam(3 * i + pstart + 0, 'A%d' % (i + 1), param[0]) fit.setParam(3 * i + pstart + 1, 'c%d' % (i + 1), param[1]) fit.setParam(3 * i + pstart + 2, 's%d' % (i + 1), param[3]) # fit fit.fit(g, xstart, xend) return fit
def main(): z, sz = 840, 40 d = [210, 106, 75] sd = [10, 4, 4] calc = list(map(lambda x:-20 * log10(x/z), d)) scalc = list(map(lambda x:20 * sqrt((x[1]/x[0]) ** 2 + (sz/z)**2) / log(10), zip(*[d, sd]))) data = DataErrors.fromLists(calc, [12, 18, 21], scalc, [0]*3) c = TCanvas('c', '', 1280, 720) g = data.makeGraph('g', 'measured attenuation m / dB', 'nominal value n / dB') g.Draw('AP') fit = Fitter('fit', 'pol1(0)') fit.setParam(0, 'a', 0) fit.setParam(1, 'b', 1) fit.fit(g, 11, 22) fit.saveData('../fit/attenuator.txt') l = TLegend(0.15, 0.6, 0.5, 0.85) l.SetTextSize(0.03) l.AddEntry(g, 'measured att. vs. nominal value', 'p') l.AddEntry(fit.function, 'fit with n = a + b m', 'l') fit.addParamsToLegend(l, (('%.2f', '%.2f'), ('%.2f', '%.2f')), chisquareformat='%.4f', units=['dB', ''], lang='en') l.Draw() c.Update() c.Print('../img/attenuator.pdf', 'pdf')
def energyGauge(): dataList = readFileToList('../calc/hg_lines.txt') elemNames = ['Hg'] * len(dataList) dataList += readFileToList('../calc/na_lines.txt') elemNames += ['Na'] * (len(dataList) - len(elemNames)) litVals = readFileToList('../data/hg_litvals.txt') litVals += readFileToList('../data/na_litvals.txt') data = DataErrors() for val, litval in zip(dataList, litVals): data.addPoint(val, litval, I2Data.ERRORBIN, 0) c = TCanvas('c', '', 1280, 720) g = data.makeGraph('g', 'measured wavelength #lambda_{exp} / nm', 'literature value #lambda_{lit} / nm') g.Draw('AP') fit = Fitter('f', '[0]+[1]*x') fit.setParam(0, 'a', 0) fit.setParam(1, 'b', 1) fit.fit(g, 420, 600) fit.saveData('../calc/fit_energy_gauge.txt', 'w') l = TLegend(0.15, 0.6, 0.5, 0.85) l.AddEntry(fit.function, 'y = a + b*x', 'l') fit.addParamsToLegend(l) l.SetTextSize(0.03) l.Draw() c.Update() c.Print('../img/energy_gauge.pdf', 'pdf')
def get_accuracy(params): gnn_graph = utils.build_gnn_graph(dataset, params) model = GNN(gnn_graph).to(device) setting = utils.from_json("json/setting.json")[args.dataset] optimizer = torch.optim.Adam(model.parameters(), lr=setting["learning_rate"], weight_decay=setting["weight_decay"]) fitter = Fitter(model, data, optimizer) history = fitter.run(verbose=args.verbose) reward = max(history.val.acc) return reward
def evaluate(params, dataset, device='cuda:0', val_test='test'): data = Dataset(dataset) gnn_graph = utils.build_gnn_graph(data, params) model = GNN(gnn_graph).to(device) # logger.info(dataset) setting = utils.from_json("json/setting.json")[dataset] optimizer = torch.optim.Adam(model.parameters(), lr=setting["learning_rate"], weight_decay=setting["weight_decay"]) fitter = Fitter(model, data[0].to(device), optimizer) history = fitter.run(val_test=val_test, verbose=False) return max(history.val.acc)
def main(): snu = ERRORS["nu"] # TODO get error sB = ERRORS["B"] sgyrorel = 0 files = ["H", "Glycol", "Teflon"] for file in files: datalist = loadCSVToList("../data/03-%s.txt" % file) if len(datalist) == 1: B, nu = datalist[0] gyro, sgyro = calcGyro(nu, snu, B, sB) if not sgyrorel == 0: sgyro = gyro * sgyrorel with TxtFile("../calc/%s.txt" % file, "w") as f: f.writeline("\t", "gyro", *map(str, (gyro, sgyro))) f.writeline("\t", "mu", *map(str, calcMu(gyro, sgyro))) f.writeline("\t", "gI", *map(str, calcNucGFactor(gyro, sgyro))) else: x, y = zip(*datalist) sx = [0] * len(x) sy = [snu] * len(y) data = DataErrors.fromLists(x, y, sx, sy) data.setXErrorAbs(sB) c = TCanvas("c%s" % file, "", 1280, 720) g = data.makeGraph("g%s" % file, "Magnetfeld B / mT", "Resonanzfrequenz #nu / MHz") g.Draw("AP") fit = Fitter("fit%s" % file, "[0]*x") fit.setParam(0, "m", 0.002) fit.fit(g, datalist[0][0] * 0.95, datalist[-1][0] * 1.05) fit.saveData("../calc/fit-%s.txt" % file, "w") l = TLegend(0.15, 0.60, 0.475, 0.85) l.SetTextSize(0.03) l.AddEntry(g, "Messdaten", "p") l.AddEntry(fit.function, "Fit mit #nu(B) = m*B", "l") l.AddEntry(0, "", "") fit.addParamsToLegend( l, [("%.5f", "%.5f"), ("%.2f", "%.2f")], chisquareformat="%.2f", advancedchi=True, units=["MHz / mT", "MHz"], ) l.Draw() gyro = 2 * np.pi * fit.params[0]["value"] * 1e9 # in Hz / T sgyro = 2 * np.pi * fit.params[0]["error"] * 1e9 # in Hz / T sgyrorel = sgyro / gyro with TxtFile("../calc/%s.txt" % file, "w") as f: f.writeline("\t", "gyro", *map(str, (gyro, sgyro))) f.writeline("\t", "sgyrorel", str(sgyrorel)) f.writeline("\t", "mu", *map(str, calcMu(gyro, sgyro))) f.writeline("\t", "gI", *map(str, calcNucGFactor(gyro, sgyro))) c.Update() c.Print("../img/03-%s.pdf" % file, "pdf")
def compareSpectrum(prefix, spectrum, litvals): xlist = list(zip(*spectrum))[0] sxlist = list(zip(*spectrum))[1] compData = DataErrors.fromLists(xlist, litvals, sxlist, [0] * len(litvals)) c = TCanvas('c_%s_compspectrum' % prefix, '', 1280, 720) g = compData.makeGraph('g_%s_compspectrum' % prefix, 'experimentell bestimmte HFS-Aufspaltung #Delta#nu^{exp}_{%s} / GHz' % prefix, 'theoretische HFS-Aufspaltung #Delta#nu^{theo} / GHz') g.Draw('AP') fit = Fitter('fit_%s_compspectum' % prefix, 'pol1(0)') fit.setParam(0, 'a_{%s}' % prefix, 0) fit.setParam(1, 'b_{%s}' % prefix, 1) fit.fit(g, compData.getMinX() - 0.5, compData.getMaxX() + 0.5) if prefix == "up": l = TLegend(0.15, 0.6, 0.45, 0.85) else: l = TLegend(0.15, 0.6, 0.5, 0.85) l.SetTextSize(0.03) l.AddEntry(g, 'Spektrum', 'p') l.AddEntry(fit.function, 'Fit mit #Delta#nu^{theo} = a_{%s} + b_{%s} #Delta#nu^{exp}_{%s}' % (prefix, prefix, prefix), 'l') fit.addParamsToLegend(l, [('%.2f', '%.2f'), ('%.3f', '%.3f')], chisquareformat='%.2f', units=['GHz', '']) l.Draw() c.Update() if not DEBUG: c.Print('../img/part2/%s-spectrum.pdf' % prefix, 'pdf')
def makePeakFreqGraph(peaks, name): xlist = list(list(zip(*peaks))[0]) sxlist = list(list(zip(*peaks))[1]) ylist = list(map(lambda i: i * 9.924, range(len(peaks)))) sylist = list(map(lambda i: i * 0.03, range(len(peaks)))) direction = name.split('-')[0] tabledata = list(zip(*[list(range(1, len(xlist) + 1)), list(map(lambda x:x * 1000, xlist)), list(map(lambda x:x * 1000, sxlist)), ylist, sylist])) with TxtFile('../src/tab_part2_etalonfreqs_%s.tex' % direction, 'w') as f: f.write2DArrayToLatexTable(tabledata, ["i", r"$x_i$ / ms", r"$0.2 \cdot s_i$ / ms", r"$\nu_i$ / GHz", r"$s_{\nu_i}$ / GHz"], ["%d", "%.3f", "%.3f", "%.2f", "%.2f"], "Zentren $x_i$ der gefitteten Cauchy-Funktionen mit Fehler aus den " + "Breiteparametern $s_i$ und Frequenzdifferenzen zum ersten Peak. ", "tab:etalon:calib:%s" % direction) etalonData = DataErrors.fromLists(xlist, ylist, sxlist, sylist) etalonData.multiplyX(1000) c = TCanvas('c_pf_' + name, '', 1280, 720) g = etalonData.makeGraph('g_pf_' + name, 'Zeit t / ms', 'Frequenzabstand #Delta#nu / GHz') g.SetMinimum(etalonData.getMinY() - 5) g.SetMaximum(etalonData.getMaxY() + 5) g.Draw('AP') fit = Fitter('fit_pf_' + name, 'pol1(0)') fit.setParam(0, 'a') fit.setParam(1, 'r') xmin, xmax = etalonData.getMinX(), etalonData.getMaxX() deltax = (xmax - xmin) / 10 fit.fit(g, xmin - deltax, xmax + deltax) fit.saveData('../fit/part2/%s-etalon_calibration.txt' % name) if fit.params[1]['value'] < 0: l = TLegend(0.575, 0.6, 0.85, 0.85) else: l = TLegend(0.15, 0.6, 0.425, 0.85) l.SetTextSize(0.03) l.AddEntry(g, 'Etalonpeaks', 'p') l.AddEntry(fit.function, 'Fit mit #Delta#nu = a + r * t', 'l') fit.addParamsToLegend(l, [('%.1f', '%.1f'), ('%.2f', '%.2f')], chisquareformat='%.2f', units=('GHz', 'GHz/ms')) l.Draw() c.Update() if not DEBUG: c.Print('../img/part2/%s-etalon_calibration.pdf' % name, 'pdf') return (fit.params[1]['value'], fit.params[1]['error'])
def get_fitter(): range_ = [-50, 250] nbins = 100 initial = dict( norm=None, # Automatically calculated by fitter from histogram eped=-0, eped_sigma=10, spe=38, spe_sigma=2, lambda_=0.7, opct=0.4, pap=0.09, dap1=0.5, dap2=0.5) limit = dict(limit_norm=(0, 100000), limit_eped=(-10, 10), limit_eped_sigma=(2, 20), limit_spe=(30, 50), limit_spe_sigma=(2, 20), limit_lambda_=(0.1, 3), limit_opct=(0, 0.8), limit_pap=(0, 0.8), limit_dap1=(0, 0.8), limit_dap2=(0, 0.8)) fix = dict(fix_norm=True, ) fitter = Fitter() fitter.range = range_ fitter.nbins = nbins fitter.initial = initial fitter.limits = limit fitter.fix = fix return fitter
def print_score(model: str, classifier: Fitter, dataset: Dataset): prediction = classifier.predict(dataset.features) print(f'Model - {model}') print( classification_report(dataset.targets, prediction, zero_division=0.0, digits=4))
def draw_colored_area(grid: np.ndarray, classifier: Fitter): prediction_grid = reshape_grid_for_prediction(grid) labeled_area = classifier.predict(prediction_grid) shape_for_draw = grid[0].shape plt.contourf(grid[0], grid[1], labeled_area.reshape(shape_for_draw), cmap='Paired')
def test_others(): from scipy import stats data = stats.gamma.rvs(2, loc=1.5, scale=2, size=1000) f = Fitter(data, bins=100, distributions="common") f.fit() assert f.df_errors.loc["gamma"].loc['aic'] > 100 f = Fitter(data, bins=100, distributions="gamma") f.fit() assert f.df_errors.loc["gamma"].loc['aic'] > 100
def fitdist(**kwargs): """""" import csv col = kwargs['column_number'] with open(kwargs["filename"], "r") as csvfile: data = csv.reader(csvfile, delimiter=kwargs['delimiter']) data = [float(x[col - 1]) for x in data] from fitter import Fitter distributions = kwargs['distributions'].split(",") distributions = [x.strip() for x in distributions] fit = Fitter(data, distributions=distributions) if kwargs['verbose'] is False: kwargs["progress"] = False fit.fit(progress=kwargs["progress"]) fit.summary() if kwargs['verbose']: print() from pylab import savefig if kwargs['verbose']: print( "Saved image in fitter.png; use --output-image to change the name") tag = kwargs['tag'] savefig("{}.png".format(tag)) best = fit.get_best() bestname = list(best.keys())[0] values = list(best.values())[0] msg = f"Fitter version {version}\nBest fit is {bestname} distribution\nparameters: " msg += f"{values}\n The parameters have to be used in that order in scipy" if kwargs["verbose"]: print(msg) with open("{}.log".format(tag), "w") as fout: fout.write(msg)
def fit(self): Fitter.fit(self) if self.r < 1: Y0 = 0. save = self.X, self.Y while True: P_old, M_old = self.P, self.M self.M *= self.r self.X, self.Y = self.X[:self.M], self.Y[:self.M] Fitter.fit(self) if self.is_old_better(P_old) or (self.Y[-1] > Y0): self.P, self.M = P_old, M_old break self.X, self.Y = save self.check()
def fit_all(miss): dist = get_distributions() f = Fitter(miss, timeout=600, distributions=dist) f.fit() print(f.df_errors.sort_values('sumsquare_error')) logmsg('best fit = %s', str(f.get_best())) f.summary() plt.show()
def HighChart(self,rawData): data = stats.gamma.rvs(2, loc=scipy.mean(rawData), scale=scipy.std(rawData), size=int(self.datal)) f = Fitter(data,distributions=['norm', 't', 'triang', 'lognorm', 'uniform', 'expon', 'weibull_min', 'weibull_max','beta','gamma','logistic','pareto']) f.fit() dist = scipy.stats.gamma param = (f.fitted_param['gamma']) X = linspace(min(rawData), max(rawData), int(self.datal)) pdf_fitted = dist.pdf(X, *param) self.Fit_Summary.insert(0, [{ "graph": { "results": '"' + str(f.summary()) + '"', "Y": str(pdf_fitted), "X": str(X), } }] )
def init_perceptron_from(arguments) -> Fitter: return Fitter( Perceptron(random_state=42, n_jobs=-1, early_stopping=arguments.early_stopping_perceptron, penalty=arguments.penalty_perceptron, alpha=arguments.alpha, eta0=arguments.eta0, tol=arguments.tol, validation_fraction=arguments.validation_fraction))
def __init__( self ) : self.dict = Dictionary() self.spliter = PinyinSpliter() self.fitter = Fitter() self.picker = Picker( self.dict ) #self.picker.set( [], [], True ) self.cache = [ [ 0, [], "" ] ] self.candCacheIndex = 0 self.candStartIndex = 0 self.candList = []
def fit(data, xmin, xmax): """ Determine the distribution that best describes some observations. Parameters: data (numpy array): observation data xmin (float): plot xmin xmax (float): plot xmax Returns: xx (numpy array), pdf_fitted (numpy array): fitted distribution x,y values """ common_distr = ['gamma', 'rayleigh', 'norm', 'expon', 'lognorm', 'beta', \ 'logistic', 'invgamma', 'exponpow', 'chi2'] f = Fitter(data, distributions=common_distr) f.fit() # may take some time since by default, all distributions are tried # but you call manually provide a smaller set of distributions #f.summary() best_fit = f.get_best() best_fit_name = [x for x in best_fit.keys()][0] best_fit_param = [x for x in best_fit.values()][0] #print(f.summary()) print("Distribution name: ", best_fit_name) print("Distribution parameters: ", best_fit_param) dist = eval("scipy.stats." + best_fit_name) xx = np.linspace(xmin, xmax, 200) pdf_fitted = dist.pdf(xx, *best_fit_param) mean = float(dist.stats(*best_fit_param, moments='m')) lwr = dist.ppf(0.025, *best_fit_param) upr = dist.ppf(0.975, *best_fit_param) print("Mean: ", mean, lwr, upr) return xx, pdf_fitted
def calculate_product_count_distribution(reviewers_df, cdfs): sample = reviewers_df['common_products'].tolist() f = Fitter(sample, distributions=cdfs) f.fit() f.summary() best = f.get_best() key = list(best.keys())[0] dist = eval("sc." + key) distribution_common = dist.pdf(sample, *(f.fitted_param[key])) return distribution_common
def fitting_marginals(inp_data): # inp_data: Input is a dataframe # Returns: fits = [] for i in xrange(inp_data.shape[1]): f = Fitter(inp_data.iloc[:, i]) f.fit() # choose best distribution and best parameter fit j = 0 found = False while (found == False): cand_dist = f.df_errors.sort_values('sumsquare_error').iloc[j].name if cand_dist in f.fitted_param.keys(): best_dist = cand_dist best_params = f.fitted_param[cand_dist] found = True j += 1 fits.append((best_dist, best_params)) f.summary() # generate scipy rv objects for marginals marginal_fits = [ eval('scipy.stats.' + fits[i][0])(*fits[i][1]) for i in xrange(len(fits)) ] return marginal_fits
def fit_powerlaw_distribution(data): f = Fitter(data) f.distributions = ['powerlaw'] f.fit() k = f.get_best() alpha = list(k.values())[0][0] return alpha
def distribucion_fitter(data): """ Esta libreria funciona para determinar la distribucion de grupo de datos. Ajusta los datos a cada una de las distribuciones y realiza las prueba pertinentes. Sin embargo, hace uso de la librería scipy para hacer las pruebas, por lo que realmente es una forma alterna a scipy sin usarla directamente. La única diferencia entre este y el anterior método recae en la eficiencia. """ distr = [ "norm", "exponweib", "weibull_max", "weibull_min", "pareto", "uniform", "t", "expon", "lognorm", "beta", "alpha", "cauchy", "f", "loguniform", "chi2", "laplace", "gamma" ] lista = list((data.dtypes == "int64") | (data.dtypes == "float64")) nombres = [ data.columns[i] for i in range(len(lista)) if lista[i] == True ] #almacenamos el nombre de todas las columnas cuyos valores sean numericos dfs, parametros, best_f = [], [], [] for ele in nombres: fitter = Fitter(data[ele], distributions=distr) #metodo principal fitter.fit(n_jobs=multiprocessing.cpu_count() ) #Hacemos que use todos los nucleos del procesador p = fitter.summary( Nbest=1, plot=False) #aqui se almacen todos los resultados de la prueba parametros.append(fitter.get_best(method='sumsquare_error')) dfs.append( p ) #agregamos los dataframes a una lista y ahora tenemos una lista de dataframes full = pd.concat(dfs, ignore_index=False) #agregamos todos los dataframes full.insert(0, "Columna", nombres) full.insert( 2, 'parametros', parametros ) #insertamos la columna en cuestion con los parametros de la mejor distribucion return full
def best_fit_all_continuous(self, data): # Distributions to check all_cd = [ st.alpha, st.anglit, st.arcsine, st.beta, st.betaprime, st.bradford, st.burr, st.cauchy, st.chi, st.chi2, st.cosine, st.dgamma, st.dweibull, st.erlang, st.expon, st.exponnorm, st.exponweib, st.exponpow, st.f, st.fatiguelife, st.fisk, st.foldcauchy, st.foldnorm, st.frechet_r, st.frechet_l, st.genlogistic, st.genpareto, st.gennorm, st.genexpon, st.genextreme, st.gausshyper, st.gamma, st.gengamma, st.genhalflogistic, st.gilbrat, st.gompertz, st.gumbel_r, st.gumbel_l, st.halfcauchy, st.halflogistic, st.halfnorm, st.halfgennorm, st.hypsecant, st.invgamma, st.invgauss, st.invweibull, st.johnsonsb, st.johnsonsu, st.ksone, st.kstwobign, st.laplace, st.levy, st.levy_l, st.levy_stable, st.logistic, st.loggamma, st.loglaplace, st.lognorm, st.lomax, st.maxwell, st.mielke, st.nakagami, st.ncx2, st.ncf, st.nct, st.norm, st.pareto, st.pearson3, st.powerlaw, st.powerlognorm, st.powernorm, st.rdist, st.reciprocal, st.rayleigh, st.rice, st.recipinvgauss, st.semicircular, st.t, st.triang, st.truncexpon, st.truncnorm, st.tukeylambda, st.uniform, st.vonmises, st.vonmises_line, st.wald, st.weibull_min, st.weibull_max, st.wrapcauchy ] with warnings.catch_warnings(): warnings.filterwarnings('ignore') dists = [x.name for x in all_cd] f = Fitter(data, distributions=dists) f.fit() f.summary()
def evalDistance(n, params): xmin, xmax = params[1:] deltax = abs(xmax - xmin) * 0.1 data = P2SemiCon.fromPath('../data/part2/length/ALL%04.d/F%04dCH1.CSV' % (n, n)) g = data.makeGraph('g%d' % n, 'Zeit t / s', 'Spannung U / V') c = TCanvas('c%d' % n, '', 1280, 720) g.SetMarkerStyle(1) g.GetXaxis().SetRangeUser(xmin - deltax, xmax + deltax) closex = min(getByCloseX(data, xmin)[1], getByCloseX(data, xmax)[1]) if closex > 0: ymin = closex * 0.95 else: ymin = closex * 1.1 g.SetMinimum(ymin) g.GetYaxis().SetTitleOffset(1.2) g.Draw('APX') fit = Fitter('f', 'pol1(0) + 1/(sqrt(2*pi*[4]^2))*gaus(2)') fit.function.SetNpx(1000) paramname = ['a', 'b', 'A', 't_{c}', '#sigma'] for i, param in enumerate(params[0]): fit.setParam(i, paramname[i], param) fit.fit(g, xmin, xmax) fit.saveData('../calc/part2/dist%02d.txt' % n, 'w') l = TLegend(0.625, 0.6, 0.99, 0.99) l.SetTextSize(0.03) l.AddEntry(g, 'Messung', 'p') l.AddEntry(fit.function, 'Fit mit U(t) = a + b*t + #frac{A}{#sqrt{2#pi*#sigma^{2}}} e^{- #frac{1}{2} (#frac{t - t_{c}}{#sigma})^{2}}', 'l') l.AddEntry(0, '', '') fit.addParamsToLegend(l, [('%.2e', '%.2e')] * len(params[0]), chisquareformat='%.2f') l.Draw() if PRINTGRAPHS: c.Update() c.Print('../img/part2/dist%02d.pdf' % n, 'pdf') return data, fit.params, fit.getCorrMatrix(), params[1], params[2]
def singlePeakFit(g, debug=False): """fits every peak individually Arguments: g -- graph debut -- if true prints values of found peaks to console (default=False) """ peaks = getStartValues() params = [] for i, peak in enumerate(peaks): fit = Fitter('fit%d' % i, 'pol1(0) + gaus(2)') fit.function.SetLineColor(i % 8 + 2) for param in peak[0]: fit.setParam(*param) fit.fit(g, peak[1], peak[2], '+') params.append([fit.params[2]['value'], fit.params[3]['value'], fit.params[3]['error'], fit.params[4]['value'], fit.function]) if debug: for j, par in fit.params.iteritems(): print(par['name'], par['value'], par['error']) print('') return params
def main(input_path, export_path): data_frame = pandas.read_excel(input_path) # get data from excel unique_data_frame = data_frame.drop_duplicates(['hour', 'unit']) unique_data = unique_data_frame.replace('\n', ' ', regex=True).get_values() data = data_frame.replace('\n', ' ', regex=True).get_values() result = [] for u in unique_data: sum = 0 count = 0 ar = [] for d in data: if u[0] == d[0] and u[3] == d[3]: sum += int(d[1]) count += 1 ar.append(d[1]) avg = sum / count fit = Fitter(ar, min(ar), max(ar), len(ar), distributions=['gamma', 'rayleigh', 'uniform']) fit.fit() result.append([u[0], u[3], avg, str(fit.fitted_param), str(fit.get_best())]) header = ["Hour", "Unit", "Average", "Distribution", "Best Distribution"] pandas.DataFrame(result, None, header).to_excel(export_path) # export data in excel
def get_continuous_dist(var, var_val, cont_dists, alt_name=None): """ Retrives the distribution of continuous alternative specific variables using the Fitter package. """ cont_dict = defaultdict(dict) # Use the Fitter library to fit distributions # to the data fitter_object = Fitter( data=var_val, distributions=cont_dists, timeout=60 ) fitter_object.fit() # Get the best distribution and store in dictionary BestDict = fitter_object.get_best() # Add name of alternative to variable and store distriburion & parameters var_name = ( var if alt_name is None else get_alt_specific_variable_name(var, alt_name) ) cont_dict[var_name]["distribution"] = list(BestDict.items())[0][0] cont_dict[var_name]["parameters"] = list(BestDict.items())[0][1] return cont_dict
def evalFaraday(): data = FaraData.fromPath('../data/fara_data.txt') c = TCanvas('c', '', 1280, 720) g = data.makeGraph('g', 'Strom I / A', 'Winkel #alpha / #circ') g.GetXaxis().SetRangeUser(-6, 6) g.SetMinimum(-15) g.SetMaximum(15) g.Draw('AP') vline = TLine(0, -15, 0, 15) vline.Draw() hline = TLine(-6, 0, 6, 0) hline.Draw() fit = Fitter('f', 'pol1(0)') fit.setParam(0, 'a', 0) fit.setParam(1, 'b', 2) fit.fit(g, -5.5, 5.5) fit.saveData('../calc/faraday.txt', 'w') l = TLegend(0.15, 0.65, 0.35, 0.85) l.AddEntry('g', 'Messwerte', 'p') l.AddEntry(fit.function, 'Fit mit y = a + b*x', 'l') l.AddEntry(0, 'Parameter:', '') l.AddEntry(0, 'a = %.2f #pm %.2f' % (fit.params[0]['value'], fit.params[0]['error']), '') l.AddEntry(0, 'b = %.3f #pm %.3f' % (fit.params[1]['value'], fit.params[1]['error']), '') l.Draw() c.Update() c.Print('../img/faraday.pdf', 'pdf') b = (fit.params[1]['value'], fit.params[1]['error']) c = 2554.85 N = 3600 v = map(lambda x: x / c, b) # verdet constant oe = 60 * 79.58 / 100 # factor for min/(Oe*cm) voe = map(lambda x: x * oe, v) # verdet in min/(Oe*cm) vi = map(lambda x: x / N, b) # ideal vioe = map(lambda x: x * oe, vi) with TxtFile('../calc/faraday_verdet.txt', 'w') as f: f.writeline('\t', *map(str, v)) f.writeline('\t', *map(str, voe)) f.writeline('\t', *map(str, vi)) f.writeline('\t', *map(str, vioe))
class PinyinLookup() : def __init__( self ) : self.dict = Dictionary() self.spliter = PinyinSpliter() self.fitter = Fitter() self.picker = Picker( self.dict ) #self.picker.set( [], [], True ) self.cache = [ [ 0, [], "" ] ] self.candCacheIndex = 0 self.candStartIndex = 0 self.candList = [] def load( self, filePath ) : newKeys = self.dict.load( filePath ) print "start build index" newPinyinSet = set() for key in newKeys : if key.count( "'" ) <= 0 : self.fitter.pinyinSet.add( key ) newPinyinSet.add( key ) self.fitter.dictTree.addKey( key ) for pinyin in newPinyinSet : self.spliter.beginCharSet.add( pinyin[0] ) self.spliter.pinyinTree.addPath( pinyin ) print "built" def update( self, key, word, freq ) : newKey = self.dict.update( key, word, freq ) if newKey : if newKey.count( "'" ) <= 0 : self.fitter.pinyinSet.add( newKey ) self.spliter.beginCharSet.add( newKey[0] ) self.spliter.pinyinTree.addPath( newKey ) self.fitter.dictTree.addKey( newKey ) def subFit( self, fitList, pinyinStringList ) : subFitPoint = -999 #for key in fitList : for i in range( len( fitList ) ) : key = fitList[i] #currentSubFitPoint = len( key ) - key.count( "'" ) - len( self.spliter.code ) currentSubFitPoint = key.count( "'" ) + 1 - len( pinyinStringList[i].string ) #print key, pinyinStringList[i].string, currentSubFitPoint if currentSubFitPoint > 0 : currentSubFitPoint = -998 if currentSubFitPoint > subFitPoint : subFitPoint = currentSubFitPoint #print key, currentSubFitPoint, subFitPoint newFitList = [] preeditList = [] for i in range( len( fitList ) ) : key = fitList[i] currentSubFitPoint = key.count( "'" ) + 1 - len( pinyinStringList[i].string ) if currentSubFitPoint >= subFitPoint : newFitList.append( key ) preeditList.append( str( pinyinStringList[i] ) ) #print newFitList, newPreeditList return newFitList, preeditList def append( self, code ) : #print "append", code self.spliter.append( code ) fitList = [] #preeditList = [] pinyinStringList = [] fitPoint = -999 for pinyinString in self.spliter.stack : #print pinyinString if pinyinString.length < len( self.spliter.code ) : pass else : currentFitPoint, keys = self.fitter.fit( pinyinString.string ) #print currentFitPoint, keys if currentFitPoint > fitPoint : fitPoint = currentFitPoint fitList = [] preeditList = [] fitList.extend( keys ) #preeditList.extend( [ str( pinyinString ) ] * len( keys ) ) pinyinStringList.extend( [ pinyinString ] * len( keys ) ) elif currentFitPoint == fitPoint : fitList.extend( keys ) #preeditList.extend( [ str( pinyinString ) ] * len( keys ) ) pinyinStringList.extend( [ pinyinString ] * len( keys ) ) fitList, preeditList = self.subFit( fitList, pinyinStringList ) #print fitList self.picker.set( fitList, preeditList, True ) cache = [ fitPoint, fitList, preeditList ] self.cache.append( cache ) self.candList = [] self.candCacheIndex = len( self.cache ) - 1 self.candStartIndex = 0 def pop( self ) : if len( self.cache ) > 1 : self.spliter.pop() self.cache = self.cache[:-1] cache = self.cache[-1] fitList = cache[1] preeditList = cache[2] self.picker.set( fitList, preeditList, True ) self.candList = [] self.candCacheIndex = len( self.cache ) - 1 self.candStartIndex = 0 def checkCache( self ) : fitList = [] cache = self.cache[self.candCacheIndex] currentFitPoint = cache[0] while self.candCacheIndex >= 1 : self.candCacheIndex -= 1 cache = self.cache[self.candCacheIndex] fitPoint = cache[0] fitList = cache[1] preeditList = cache[2] #print self.candCacheIndex, fitList if len( fitList ) >= 0 : if len( self.candList ) <= 0 : break elif fitPoint >= currentFitPoint : break if self.candCacheIndex >= 1 : self.picker.set( fitList, preeditList, False ) return True else : return False def getCand( self, index ) : flag = True while flag and len( self.candList ) <= index : key, word, freq, preeditString = self.picker.pick() if key : self.candList.append( [ key, word, freq, preeditString, self.candStartIndex ] ) else : flag = self.checkCache() self.candStartIndex = len( self.candList ) if flag : return self.candList[index] else : return None #print candList def clear( self ) : self.spliter.clear() self.picker.set( [], [], True ) self.cache = [ [ 0, [], "" ] ] self.candList = [] self.candCacheIndex = 0 self.candStartIndex = 0
def makeBFit(darkTimes, Bs): dt, sdt = list(zip(*darkTimes)) b, sb = list(zip(*Bs)) data = DataErrors.fromLists(dt, b, sdt, sb) c = TCanvas('c_B', '', 1280, 720) g = data.makeGraph('g_B', 'Dunkelzeit t_{D} / ms', 'Fitparameter B / V') g.Draw('APX') fit = Fitter('fit_B', '[0] + [1] * (1 - exp(-x/[2]))') fit.function.SetNpx(1000) fit.setParam(0, 'a', 0.1) fit.setParam(1, 'b', 0.1) fit.setParam(2, 'T_{R_{F}}', 6) fit.fit(g, 0, 25) fit.saveData('../fit/B.txt') l = TLegend(0.55, 0.15, 0.85, 0.6) l.SetTextSize(0.03) l.AddEntry(g, 'Fitparameter B', 'p') l.AddEntry(fit.function, 'Fit mit B(t_{D}) = a + b (1 - e^{-x/T_{R_{F}}})', 'l') fit.addParamsToLegend(l, (('%.3f', '%.3f'), ('%.3f', '%.3f'), ('%.1f', '%.1f')), chisquareformat='%.2f', units=['V', 'V', 'ms']) l.Draw() g.Draw('P') c.Print('../img/part6/BFit.pdf', 'pdf')
def makeCSGraph(ctype, startGammaEE=None): data = loadCrossSection(ctype) c = TCanvas('c_%s' % ctype, '', 1280, 720) g = data.makeGraph('g_%s' % ctype, '#sqrt{s} / GeV', '#sigma / nb') g.Draw('AP') if not startGammaEE: fit = Fitter('fit_%s' % ctype, '(12*pi / [0]^2) * (x^2 * [1]^2) / ((x^2-[0]^2)^2 + x^4 * [2]^2 / [0]^2) * 0.3894*10^6') # GeV^-2 = 0.3894 mb fitfuncstring = "#frac{12#pi}{M_{Z}^{2}} #frac{s #Gamma_{%s}^{2}}{(s-M_{Z}^{2})^{2} + s^{2} #Gamma_{Z}^{2} / M_{Z}^{2}}" % ctype[0] else: fit = Fitter('fit_%s' % ctype, '(12*pi / [0]^2) * (x^2 * [1] * [2]) / ((x^2-[0]^2)^2 + x^4 * [3]^2 / [0]^2) * 0.3894*10^6') fitfuncstring = "#frac{12#pi}{M_{Z}^{2}} #frac{s #Gamma_{m} #Gamma_{%s}}{(s-M_{Z}^{2})^{2} + s^{2} #Gamma_{Z}^{2} / M_{Z}^{2}}" % ctype[0] fit.setParam(0, 'M_{Z}', 91.2) fit.setParamLimits(0, 0, 1000) if not startGammaEE: fit.setParam(1, '#Gamma_{%s}' % ctype[0], 0.08) fit.setParamLimits(1, 0, 1000) fit.setParam(2, '#Gamma_{Z}', 2.5) fit.setParamLimits(2, 0, 1000) else: fit.setParam(1, '#Gamma_{m}', startGammaEE, True) fit.setParam(2, '#Gamma_{%s}' % ctype[0], 1.5) fit.setParam(3, '#Gamma_{Z}', 2.5) fit.setParamLimits(2, 0, 1000) fit.fit(g, 88, 94, '') fit.saveData('../fit/crosssections_%s.txt' % ctype) l = TLegend(0.625, 0.6, 0.98, 0.975) l.SetTextSize(0.03) l.AddEntry(g, "%s Wirkungsquerschnitte" % ctype, 'p') l.AddEntry(fit.function, "Fit mit #sigma(s) = %s" % fitfuncstring, 'l') l.AddEntry(None, "", '') if not startGammaEE: fit.addParamsToLegend(l, [('%.3f', '%.3f'), ('%.4f', '%.4f'), ('%.3f', '%.3f')], chisquareformat='%f', units=['GeV / c^{2}', 'GeV', 'GeV']) else: fit.addParamsToLegend(l, [('%.3f', '%.3f'), '%.4f', ('%.3f', '%.3f'), ('%.3f', '%.3f')], chisquareformat='%f', units=['GeV/c^{2}', 'GeV', 'GeV', 'GeV']) l.Draw() c.Update() if not DEBUG: c.Print('../img/crosssections_%s.pdf' % ctype, 'pdf') if not startGammaEE: result = [(fit.params[0]['value'], fit.params[0]['error']), (fit.params[1]['value'], fit.params[1]['error']), (fit.params[2]['value'], fit.params[2]['error'])] else: result = [(fit.params[0]['value'], fit.params[0]['error']), (fit.params[2]['value'], fit.params[2]['error']), (fit.params[3]['value'], fit.params[3]['error'])] return result
import setup from load_data import load_data import numpy as np import scipy as sp import matplotlib as mpl import matplotlib.pyplot as plt from plots import * from all_fits import * import config as cfg from fitter import Fitter from shapes.sigmoid import Sigmoid from shapes.spline import Spline from scalers import LogScaler cfg.verbosity = 1 data = load_data(pathway='serotonin', scaler=LogScaler()) series = data.get_one_series('HTR1A','MD') x = series.ages y = series.single_expression fitter = Fitter(Spline()) theta, sigma, LOO_predictions,_ = fitter.fit(x,y) spline = theta[0] preds = spline(x) print preds
def makeAreaFit(): # calculate ares d_s = [1.000, 0.990, 0.990, 1.005, 1.000, 1.005] d_m = [1.700, 1.690, 1.695, 1.700, 1.705, 1.705] d_l = [2.880, 2.880, 2.875, 2.880, 2.880, 2.880] d = map(avgstd, [d_s, d_m, d_l]) a = map(area, d) diaarea = DataErrors() for i in range(3): diaarea.addPoint(d[i][0], a[i][0], d[i][1], a[i][1]) diaarea.saveDataToLaTeX(['Durchmesser $d$ / cm', '$s_d$ / cm', 'Fl\"ache $F / \\text{cm}^2$', '$s_F / \\text{cm}^2$'], ['%.4f', '%.4f', '%.4f', '%.4f'], 'Verschiedene Fl\"achen f\"ur die Samariummessung', 'tab:data:samarium:area', '../src/data_samarium_areas.tex', 'w') with TxtFile('../fit/samarium.txt', 'w') as f: f.writeline('areas') f.writeline('=====') for b in a: f.writeline('\t', '%e' % b[0], TxtFile.PM, '%e' % b[1]) f.writeline() #read data from files #file=[area, area error, path, time] files = [] files.append([a[0][0], a[0][1], '../data/31_Sm_kl_1600_t3000.txt', 3000]) files.append([a[1][0], a[1][1], '../data/34_Sm_m_1600_t2400.txt', 2400]) files.append([a[2][0], a[2][1], '../data/08_Sm_ggrFl_1600-1600-0.txt', 1200]) u = readSingleEntryFile('../data/09_Untergrund_1600-1600-0.txt') tu = 3600 d = DataErrors() for file in files: n = readSingleEntryFile(file[2]) d.addPoint(file[0], n - u, file[1], np.sqrt(n / file[3] + u / tu)) d.saveDataToLaTeX(['Fl\"ache $F / \\text{cm}^2$', '$s_F / \\text{cm}^2$', 'Z\"ahlrate $n / (1/\\text{s})$', '$s_n / (1/\\text{s})$'], ['%.4f', '%.4f', '%.3f', '%.3f'], 'Z\"ahlraten von \\samarium~f\"ur verschiedene Fl\"achen mit Fehlern', 'tab:data:samarium', '../src/data_samarium.tex', 'w') c = TCanvas('c2', '', 800, 600) g = d.makeGraph('g', 'Fl#ddot{a}che F / cm^{2}', 'Z#ddot{a}hlrate n / (1/s)') g.Draw('AP') fit = Fitter('f', '[0]+[1]*x') fit.setParam(0, 'a', 0) fit.setParam(1, 'b', 0.05) fit.fit(g, 0, 30) fit.saveData('../fit/samarium.txt', 'a') a = fit.params[0]['value'] sa = fit.params[0]['error'] b = fit.params[1]['value'] sb = fit.params[1]['error'] l = TLegend(0.55, 0.15, 0.98, 0.5) l.AddEntry('g', '{}^{147} Samarium ohne Untergrund', 'p') # TODO with error bar? (options +'e') l.AddEntry(fit.function, 'Fit mit n(F)=a+b*F', 'l') l.AddEntry(0, '#chi^{2} / DoF : %f' % fit.getChisquareOverDoF(), '') l.AddEntry(0, 'Paramter:', '') l.AddEntry(0, 'a: %e #pm %e' % (a, sa), '') l.AddEntry(0, 'b: %e #pm %e' % (b, sb), '') l.SetTextSize(0.03) l.Draw() c.Update() c.Print('../img/Samarium147-Flaechenabhaengigkeit.pdf', 'pdf') #calculation with fit parameters c = 0.004025 NA = 6.02214129e23 m = 2*150.36 + 3*15.999 h = 0.1487 t = (np.log(2) * c * NA * h) / (2 * m * b) / (3600 * 24 * 365.242) st = t * (sb / b) with TxtFile('../fit/samarium.txt', 'a') as f: f.writeline('calculation from fit') f.writeline('====================') f.writeline('\t', '%e' % t, TxtFile.PM, '%e' % st) f.writeline() # calculation from single data points sc = map(lambda p: calculateHalfLife(*p), d.points) with TxtFile('../fit/samarium.txt', 'a') as f: f.writeline('calculation from single data points') f.writeline('===================================') for s in sc: f.writeline('\t', '%e' % s[0], TxtFile.PM, '%e' % s[1]) f.writeline()
def makeCSGraphUnderground(ctype, startGammaEE=None): data = loadCrossSection(ctype) c = TCanvas('c_%s' % ctype, '', 1280, 720) g = data.makeGraph('g_%s' % ctype, '#sqrt{s} / GeV', '#sigma / nb') g.Draw('AP') if not startGammaEE: fit = Fitter('fit_%s' % ctype, '[0] + [1] * x + 12*pi / [2]^2 * x^2 * [3]^2 / ((x^2-[2]^2)^2 + x^4 * [4]^2 / [2]^2) * 0.3894e6') fitfuncstring = "a + b#sqrt{s} + #frac{12#pi}{M_{Z}^{2}} #frac{s #Gamma_{%s}^{2}}{(s-M_{Z}^{2})^{2} + s^{2} #Gamma_{Z}^{2} / M_{Z}^{2}}" % ctype[0] else: fit = Fitter('fit_%s' % ctype, '[0] + [1] * x + 12*pi / [2]^2 * x^2 * [3] * [5] / ((x^2-[2]^2)^2 + x^4 * [4]^2 / [2]^2) * 0.3894e6') fitfuncstring = "a + b#sqrt{s} + #frac{12#pi}{M_{Z}^{2}} #frac{s #Gamma_{e} #Gamma_{%s}}{(s-M_{Z}^{2})^{2} + s^{2} #Gamma_{Z}^{2} / M_{Z}^{2}}" % ctype[0] fit.setParam(0, 'a', 0) fit.setParam(1, 'b', 0) fit.setParam(2, 'M_{Z}', 91.2) fit.setParam(4, '#Gamma_{Z}', 2.5) if not startGammaEE: fit.setParam(3, '#Gamma_{%s}' % ctype[0], 0.08) else: fit.setParam(3, '#Gamma_{e}', startGammaEE, True) fit.setParam(5, '#Gamma_{%s}' % ctype[0], 2) fit.fit(g, 88, 94) fit.saveData('../fit/crosssections_%s.txt' % ctype) l = TLegend(0.625, 0.575, 0.98, 0.98) l.SetTextSize(0.03) l.AddEntry(g, "%s Wirkungsquerschnitte" % ctype, 'p') l.AddEntry(fit.function, "Fit mit #sigma(s) = ", 'l') l.AddEntry(None, "", '') l.AddEntry(None, fitfuncstring, '') l.AddEntry(None, "", '') if not startGammaEE: fit.addParamsToLegend(l, [('%.3f', '%.3f'), ('%.3f', '%.3f'), ('%.3f', '%.3f'), ('%.3f', '%.3f'), ('%.3f', '%.3f')], chisquareformat='%f', units=['nb', 'nb/GeV', 'GeV / c^{2}', 'GeV', 'GeV']) else: fit.addParamsToLegend(l, [('%.3f', '%.3f'), ('%.3f', '%.3f'), ('%.3f', '%.3f'), '%.4f', ('%.3f', '%.3f'), ('%.3f', '%.3f')], chisquareformat='%f', units=['nb', 'nb/GeV', 'GeV/c^{2}', 'GeV', 'GeV', 'GeV']) l.Draw() c.Update() c.Print('../img/crosssections_%s.pdf' % ctype, 'pdf') return fit.params[3]['value']
def test_gamma(): from scipy import stats data = stats.gamma.rvs(2, loc=1.5, scale=2, size=10000) f = Fitter(data, bins=100) f.xmin = -10 #should have no effect f.xmax = 1000000 # no effet f.xmin=0.1 f.xmax=10 f.distributions = ['gamma', "alpha"] f.fit() df = f.summary() assert len(df) f.plot_pdf(names=["gamma"]) f.plot_pdf(names="gamma") res = f.get_best() assert "gamma" in res.keys()
def fitLambda(): data = Data() data.addPoint(500, 1.000279) data.addPoint(540, 1.000278) data.addPoint(600, 1.000277) data.addPoint(682, 1.000276) c = TCanvas('c1', '', 1280, 720) g = data.makeGraph('g', 'wavelength #lambda / nm', 'refraction index n') g.GetYaxis().SetLabelSize(0.03) g.GetYaxis().SetTitleOffset(1.39) g.Draw('AP') fit = Fitter('f', '[0]+[1]*x') fit.setParam(0, 'a', 2) fit.setParam(1, 'b', -1) fit.fit(g, 450, 700) fit.saveData('../calc/fit_lambda.txt', 'w') a = fit.params[0]['value'] sa = fit.params[0]['error'] b = fit.params[1]['value'] sb = fit.params[1]['error'] l = TLegend(0.4, 0.6, 0.87, 0.87) l.AddEntry('g', 'refraction index n as a function of wavelength #lambda', 'p') l.AddEntry(fit.function, 'fit with n(#lambda)= a+b*#lambda', 'l') fit.addParamsToLegend(l) l.SetTextSize(0.03) l.Draw() c.Update() c.Print('../img/fit_lambda.pdf', 'pdf')
ticks = ax.get_yticks() ticks = np.array([ticks[0], ticks[-1]]) ax.set_yticks(ticks) ax.set_yticklabels(['{:g}'.format(t) for t in ticks], fontsize=fontsize) return fig cfg.verbosity = 1 age_scaler = LogScaler() data = GeneData.load('both').scale_ages(age_scaler) shapes = [Sigmoid('sigmoid_wide'), Poly(1,'poly1'), Poly(3,'poly3'), Spline()] GRs = [ ('ADRB1','A1C', (5, 8)), ('GLRA2','STC', (5, 12)), ('TUBA1A','V1C', (10, 14)), ] for g,r,yrange in GRs: print 'Doing {}@{}...'.format(g,r) thetas = [] for shape in shapes: series = data.get_one_series(g,r) sigma_prior = 'normal' if not isinstance(shape,Spline) else None fitter = Fitter(shape, sigma_prior=sigma_prior) theta,_,_,_ = fitter.fit(series.ages, series.single_expression) thetas.append(theta) fig = plot_one_series(series,shapes,thetas,yrange) save_figure(fig,'RP/fit-examples-{}-{}.png'.format(g,r), under_results=True)
def makeGraph(channel): data = MyonData.fromPath('../data/energieaufloesung_%s.TKA' % channel) data.convertToCountrate() c = TCanvas('c_%s' % channel, '', 1280, 720) g = data.makeGraph('g%s' % channel, 'channel c', 'countrate n / (1/s)') prepareGraph(g) g.GetXaxis().SetRangeUser(0, 700) g.Draw('APX') ch = int(channel) if ch < 100: xmin, xmax = ch - 25, ch + 25 elif ch < 120: xmin, xmax = ch - 50, ch + 40 elif ch < 200: xmin, xmax = ch - 50, ch + 50 elif ch < 600: xmin, xmax = 0, 700 else: xmin, xmax = 0, ch + 45 fit = Fitter('fit_%s' % channel, '[0] + gaus(1)') fit.function.SetNpx(1000) fit.setParam(0, 'b', 0) # offset fit.setParam(1, 'A', data.getMaxY()) # amplitude fit.setParam(2, 'x', ch) # channel fit.setParam(3, '#sigma', 50) # sigma #fit.setParamLimits(0, 0, 1000) fit.fit(g, xmin, xmax) fit.saveData('../fit/energieaufloesung_%s.txt' % channel) if ch > 350: l = TLegend(0.15, 0.5, 0.45, 0.85) else: l = TLegend(0.55, 0.5, 0.85, 0.85) l.SetTextSize(0.03) l.AddEntry(g, "measurement", 'p') l.AddEntry(fit.function, "fit with n(c) =", 'l') l.AddEntry(None, "b + A gaus(c; x, #sigma)", '') fit.addParamsToLegend(l, (('%.4f', '%.4f'), ('%.2f', '%.2f'), ('%.2f', '%.2f'), ('%.2f', '%.2f'),), chisquareformat='%.2f', units=['1/s', '1/s', '', ''], lang='en') l.Draw() g.Draw('P') c.Update() c.Print('../img/energieaufloesung_%s.pdf' % channel, 'pdf') return (fit.params[2]['value'], fit.params[2]['error'], abs(fit.params[3]['value']), fit.params[3]['error']) # abs(sigma)
def getExcitedStateOscillationConstants(): # plot spectrum data = I2Data.fromPath('../data/04_I2_ngg10_10ms.txt') progression = dict() for i in range(1, 3 + 1): progression[i] = I2Data.fromPath('../data/prog%d.txt' % i) c = TCanvas('c', '', 1280, 720) g = data.makeGraph('spectrum', 'wavelength #lambda / nm', 'intensity / a.u.') g.SetMarkerStyle(1) g.GetXaxis().SetRangeUser(505, 620) g.SetMinimum(18000) g.SetMaximum(49000) myY = TGaxis() myY.ImportAxisAttributes(g.GetYaxis()) myY.SetMaxDigits(3) g.Draw('AL') pg1 = progression[1].makeGraph('prog1') pg1.SetMarkerColor(2) pg1.Draw('P') pg2 = progression[2].makeGraph('prog2') pg2.SetMarkerColor(3) pg2.Draw('P') pg3 = progression[3].makeGraph('prog3') pg3.SetMarkerColor(4) pg3.Draw('P') l = TLegend(0.6, 0.15, 0.85, 0.4) l.AddEntry('spectrum', 'measurement', 'l') l.AddEntry('prog1', 'first progression (#nu\'\' = 1)', 'p') l.AddEntry('prog2', 'second progression (#nu\'\' = 2)', 'p') l.AddEntry('prog3', 'third progression (#nu\'\' = 3)', 'p') l.Draw() c.Update() c.Print('../img/I2_absorption.pdf', 'pdf') # calculations start = [18, 7, 9] prog1ord = {'a': [], 'ae': [], 'b': [], 'be': []} for i, prog in progression.iteritems(): # Calculate vacuum wavelength and create Birge-Sponer plot prog.correctValues() c = TCanvas('c%d' % i, '', 1280, 720) g = makeBirgeSponer(prog, start[i - 1]).makeGraph('prog%d_bs' % i, '#nu\' + 1/2', '#Delta G (#nu\' + 1/2) / (cm^{-1})') g.Draw('AP') # fit 2nd-order fit2ord = Fitter('prog%d_2ord' % i, '[0]-[1]*(2*x+2)+[2]*(3*x^2+6*x+13/4)') fit2ord.setParam(0, 'a', 120) fit2ord.setParam(1, 'b', 1) fit2ord.setParam(2, 'c', 0) fit2ord.fit(g, 4, 50) fit2ord.saveData('../calc/prog%d_fit2ord.txt' % i, 'w') l2 = TLegend(0.6, 0.7, 0.95, 0.95) l2.AddEntry(0, 'Fit 2nd. order', '') l2.AddEntry(fit2ord.function, 'y = a - b*(2*x+2) + c*(3*x^2+6*x+13/4)', 'l') fit2ord.addParamsToLegend(l2) l2.SetTextSize(0.03) l2.Draw() # fit 1st-order fit1ord = Fitter('prog%d_1ord' % i, '[0]-[1]*(2*x+2)') fit1ord.setParam(0, 'a', 120) fit1ord.setParam(1, 'b', 1) fit1ord.fit(g, 4, 50, '+') g.GetFunction('prog%d_1ord' % i).SetLineColor(4) fit1ord.saveData('../calc/prog%d_fit1ord.txt' % i, 'w') prog1ord['a'].append(fit1ord.params[0]['value']) prog1ord['ae'].append(fit1ord.params[0]['error']) prog1ord['b'].append(fit1ord.params[1]['value']) prog1ord['be'].append(fit1ord.params[1]['error']) l1 = TLegend(0.125, 0.15, 0.5, 0.4) l1.AddEntry(0, 'Fit 1st. order', '') l1.AddEntry(g.GetFunction('prog%d_1ord' % i), 'y = a - b*(2*x+2)', 'l') fit1ord.addParamsToLegend(l1) l1.SetTextSize(0.03) l1.Draw() c.Update() c.Print('../img/prog%d_birgesponer.pdf' % i, 'pdf') # save vibrational constants to latex file nus = [0, 1, 2] f = TxtFile('../src/ExcitedStateOscillationConstants.tex', 'w') f.write2DArrayToLatexTable(zip(nus, prog1ord['a'], prog1ord['ae'], prog1ord['b'], prog1ord['be']), ['$\\nu\'\'$', '$\omega_e\' / \\text{cm}^{-1}$', '$s_{\omega_e\'} / \\text{cm}^{-1}$', '$\omega_e\' x_e\' / \\text{cm}^{-1}$', '$s_{\omega_e\' x_e\'} / \\text{cm}^{-1}$'], ['%0.f', '%3.1f', '%.1f', '%.3f', '%.3f'], 'Oscillation constants for first order fit of Birge-Sponer plots', 'tab:prog1ord') f.close() # calculate weighted average for fit 1st- order with TxtFile.fromRelPath('../calc/ExcitedStateOscillationConstants.txt', 'w') as f: f.writeline('\t', *map(lambda x: str(x), avgerrors(prog1ord['a'], prog1ord['ae']))) f.writeline('\t', *map(lambda x: str(x), avgerrors(prog1ord['b'], prog1ord['be'])))
def main(): times = [2.4, 4.5, 6.25, 8.6] times_error = [0.02] * len(times) channels = [113, 223, 315.5, 441] channels_error = [0.5] * len(times) with TxtFile('../src/tab_timecalibration.tex', 'w') as f: f.write2DArrayToLatexTable(list(zip(*[times, times_error, channels, channels_error])), ["$t$ / \\textmu s", "$s_t$ / \\textmu s", "$c$", "$s_c$"], ["%.2f", "%.2f", "%.1f", "%.1f"], "Measured times and channels with errors for the time calibration.", "tab:tcal") data = DataErrors.fromLists(channels, times, channels_error, times_error) c = TCanvas('c', '', 1280, 720) g = data.makeGraph('g', 'channel c', 'time t / #mus') g.Draw('APX') fit = Fitter('fit', 'pol1(0)') fit.setParam(0, 'a', 0) fit.setParam(1, 'b', 50) fit.fit(g, 100, 450) fit.saveData('../fit/timeCalibration.txt') l = TLegend(0.15, 0.6, 0.5, 0.85) l.SetTextSize(0.03) l.AddEntry(g, 'measurement', 'p') l.AddEntry(fit.function, 'fit with t(c) = a + b * c', 'l') fit.addParamsToLegend(l, (('%.2f', '%.2f'), ('%.5f', '%.5f')), chisquareformat='%.2f', units=('#mus', '#mus/channel'), lang='en') l.Draw() g.Draw('P') c.Update() c.Print('../img/timeCalibration.pdf', 'pdf') with TxtFile('../calc/timeCalibration.txt', 'w') as f: f.writeline('\t', str(fit.params[0]['value']), str(fit.params[0]['error'])) f.writeline('\t', str(fit.params[1]['value']), str(fit.params[1]['error'])) f.writeline('\t', str(fit.getCovMatrixElem(0, 1)))
def fitXc(dists, times): listx, listsx = zip(*times) listy, listsy = zip(*dists) data = DataErrors.fromLists(listx, listy, listsx, listsy) c = TCanvas('cXc', '', 1280, 720) g = data.makeGraph('xc', 'Zeit t / s', 'x_{c} / mm') g.SetLineWidth(0) g.Draw('AP') fit = Fitter('fitXc', 'pol1(0)') fit.setParam(0, 'x_{0}', 1) fit.setParam(1, 'm', 0.5e-6) fit.fit(g, 1e-6, 23e-6) fit.saveData('../calc/part2/dist_fit_xc.txt') l = TLegend(0.15, 0.625, 0.4, 0.85) l.SetTextSize(0.03) l.AddEntry('xc', 'Messung', 'p') l.AddEntry(fit.function, 'Fit mit x_{c}(t) = x_{0}+m*t', 'l') l.AddEntry(0, '', '') fit.addParamsToLegend(l, [('%.3f', '%.3f'), ('%.2e', '%.2e')], chisquareformat='%.2f') l.Draw() if PRINTGRAPHS or True: c.Update() c.Print('../img/part2/dist_fitXc.pdf', 'pdf') # calculate mu m, sm = fit.params[1]['value'], fit.params[1]['error'] U, sU = 48.8, P2SemiCon.UERROR l = 30 mu = m * l / U / 100 # /100 -> from mm in cm smu = mu * np.sqrt((sm / m) ** 2 + (sU / U) ** 2) with TxtFile('../calc/part2/dist_mu.txt', 'w') as f: f.writeline('\t', str(mu), str(smu)) return mu, smu
def fitSigma(sigs, times): listx, listsx = zip(*times) listy, listsy = zip(*sigs) listy = map(abs, listy) # fits can yield negative sigma, because it only occurse to data = DataErrors.fromLists(listx, listy, listsx, listsy) c = TCanvas('cSigma', '', 1280, 720) g = data.makeGraph('Sigma', 'Zeit t / s', 'Standardabweichung #sigma / cm') g.Draw('AP') fit = Fitter('fitS', 'sqrt(2*[0]*(x + [1]))') fit.setParam(0, 'D_{n}', 100) fit.setParam(1, 't_{0}', 0) fit.fit(g, 1e-6, 21e-6) fit.saveData('../calc/part2/dist_fit_sigma.txt') l = TLegend(0.15, 0.625, 0.45, 0.85) l.SetTextSize(0.03) l.AddEntry('Sigma', 'Messung', 'p') l.AddEntry(fit.function, 'Fit mit #sigma(t) = #sqrt{2*D_{n}*(t + t_{0})}', 'l') fit.addParamsToLegend(l, [('%.1f', '%.1f'), ('%.2e', '%.2e')], chisquareformat='%.2f') l.Draw() if PRINTGRAPHS or True: c.Update() c.Print('../img/part2/dist_fitSigma.pdf', 'pdf')
def fitA(amps, times): listx, listsx = zip(*times) listy, listsy = zip(*amps) slaser_rel = 0.05 listsy = list(listsy) for i, y in enumerate(listy): listsy[i] = y * np.sqrt(listsy[i] ** 2 + slaser_rel ** 2) data = DataErrors.fromLists(listx, listy, listsx, listsy) c = TCanvas('cA', '', 1280, 720) g = data.makeGraph('A', 'Zeit t / s', 'Amplitude A / V') g.GetYaxis().SetTitleOffset(1.2) g.Draw('AP') fit = Fitter('A', '[0]*exp(-(x)/[1])+[2]') fit.function.SetNpx(1000) fit.setParam(0, 'C', 5e-8) fit.setParam(1, '#tau_{n}', 45e-6) fit.setParam(2, 'a', 0) fit.fit(g, 1e-6, 23e-6) fit.saveData('../calc/part2/dist_fit_A.txt') l = TLegend(0.575, 0.5, 0.85, 0.85) l.SetTextSize(0.03) l.AddEntry('A', 'Messung', 'p') l.AddEntry(fit.function, 'Fit mit A(t) = C*e^{- #frac{t}{#tau_{n}}} + a', 'l') l.AddEntry(0, 'Parameter:', '') fit.addParamsToLegend(l, [('%.2e', '%.1e'), ('%.2e', '%.1e'), ('%.2e', '%.1e')], chisquareformat='%.2f') l.Draw() if PRINTGRAPHS or True: c.Update() c.Print('../img/part2/dist_fitA.pdf', 'pdf') c.SetLogy() c.Update() c.Print('../img/part2/dist_fitA_log.pdf', 'pdf')
def makeFranzenFit(n, plotParams): data = OPData.fromPath(DIR + '%02d.tab' % n, 1) xmin, xmax = plotParams[:2] fitparams = plotParams[2:] c = TCanvas('c_%d' % n, '', 1280, 720) g = data.makeGraph('g_%d' % n, 'Zeit t / s', 'U_{ph} / V') prepareGraph(g, 2) g.GetXaxis().SetRangeUser(xmin, xmax) g.Draw('APX') fit = Fitter('fit%d' % n, franzenFunc, (xmin, xmax, 6)) fit.function.SetNpx(1000) paramnames = ["A", "u", "#mu", "#sigma", "B", "#lambda"] for i in range(6): fit.setParam(i, paramnames[i], fitparams[3 * i]) fit.setParamLimits(i, fitparams[3 * i + 1], fitparams[3 * i + 2]) print(fitparams[3 * i], fitparams[3 * i + 1], fitparams[3 * i + 2]) fit.fit(g, xmin, xmax) fit.saveData('../fit/part6/%02d.txt' % n) fermi = TF1('func', fermiFunc, xmin, xmax, 5) fermi.SetNpx(1000) fermi.SetLineColor(getRootColor(0)) fermi.SetLineWidth(1) for i in range(5): fermi.SetParameter(i, fit.params[i]['value']) fermi.Draw('SAME') g.Draw('P') l = TLegend(0.4, 0.15, 0.85, 0.65) l.SetTextSize(0.03) l.AddEntry(g, "Spannung Photodiode U_{ph}", 'l') l.AddEntry(fit.function, 'Fit mit U_{ph}(t) = U_{F}(t; A, u, #mu, #sigma) + U_{E}(t; #mu, B, #lambda)', 'l') l.AddEntry(fermi, 'Fermi-Verteilung', 'l') fit.addParamsToLegend(l, [('%.4f', '%.4f'), ('%.4f', '%.4f'), ('%.5f', '%.5f'), ('%.2f', '%.2f'), ('%.4f', '%.4f'), ('%.3f', '%.3f')], chisquareformat='%.2f', units=["V", "V", "ms", "#mus", "V", "1/ms"]) l.Draw() c.Update() c.Print('../img/part6/%02d.pdf' % n, 'pdf') result = [] exportvals = [2, 3, 4] # mu, sigma, B for e in exportvals: result.append((fit.params[e]['value'], fit.params[e]['error'])) return result
def test_fitter(): f = Fitter([1,1,1,2,2,2,2,2,3,3,3,3], distributions=['gamma'], xmin=0, xmax=4) try: f.plot_pdf() except: pass f.fit() f.summary() assert f.xmin == 0 assert f.xmax == 4 # reset the range: f.xmin = None f.xmax = None assert f.xmin == 1 assert f.xmax == 3 f = Fitter([1,1,1,2,2,2,2,2,3,3,3,3], distributions=['gamma']) f.fit() f.summary() assert f.xmin == 1 assert f.xmax == 3