def test(config, log=False, kg='gauss'): if config in ['IC59','IC79']: nfiles = 5 if config=='IC79' else 14 for i in range(nfiles): s = load_sim(config, spline=False, part=i) if i == 0: y, bins = ang_hist(s) x = getMids(bins) else: h = ang_hist(s) y += h[0] if config in ['IC86','IC86-II','IC86-III']: s = load_sim(config, spline=False) y, bins = ang_hist(s) x = getMids(bins) # Normalize values y = y.astype('float') ycut = (y!=0) x = x[ycut] y = y[ycut] dy = 1/np.sqrt(y) y /= y.sum() yerr = y * dy popt, pcov = fitter(x, y, yerr, kg=kg) print config, popt, np.sqrt(np.diagonal(pcov)) f0 = np.array([gauss(i, popt[:2]) for i in x]) f1 = np.array([gauss(i, popt[2:]) for i in x]) fit = np.array([myfunc(i, *popt) for i in x]) # Test fit for gaussian with median angular resolution as sigma #sig = 3.65 #amp = 2 * 1 / (sig * np.sqrt(2*np.pi)) #f2 = np.array([gauss(i, [amp, sig]) for i in x]) # Get area of each gaussian a0, a1 = f0.sum(), f1.sum() atot = float(a0 + a1) #print a0 #print a1 print 'Fraction1: %.2f' % (a0 / atot) #print 'Unaccounted: %.2f' % (y.sum() - atot) fig, ax = plt.subplots() ax.errorbar(x, y, yerr, fmt='.') ax.plot(x, fit) #ax.plot(x, f0) #ax.plot(x, f1) #ax.plot(x, f2) ax.set_xlim(0, 20) if log: ax.set_yscale('log') ax.set_ylim(1e-6, 1) plt.show()
def writeDist(config, infoFile, overwrite=False): # Load simulation s = analysis.load_sim(config) energy = np.log10(s['MC_energy']) weights = analysis.getWeights(s) # Read in existing distribution file lines = [] if os.path.isfile(infoFile): with open(infoFile, 'r') as f: lines = f.readlines() lines = [line.strip() for line in lines] table = [line.split(' ') for line in lines] paramList = [i[:3] for i in table] # Info stored in [config, nmin, nmax, median energy, sigL, sigR, counts] nList = range(3, 16) nList.append(100) newParams = [[config, i, j] for i in nList for j in nList if i < j] for config, nmin, nmax in newParams: # Check to see if the params exists already found = True try: i = paramList.index([config, str(nmin), str(nmax)]) except ValueError: found = False # If it exists, option to overwrite if found and not overwrite: print 'Info for', config, nmin, nmax, 'already exists...' continue if found and overwrite: table.remove(table[i]) print 'Working on', config, nmin, nmax # Calculate information for energy range cut = (s['nstation'] >= nmin) * (s['nstation'] < nmax) values = energy[cut] w = weights[cut] # Weighted median and 68% containment median = analysis.weighted_percentile(values, w, 50) sigL = median - analysis.weighted_percentile(values, w, 16) sigR = analysis.weighted_percentile(values, w, 84) - median # Unbiased weighted variance mean = np.average(values, weights=w) var = (sum(w * (values-mean)**2) / (sum(w) - sum(w**2)/sum(w))) counts = len(values) # Append to file newLine = [config, nmin, nmax, median, sigL, sigR, var, counts] newLine = ['%s' % i for i in newLine] table.append(newLine) # Write as you go lines = [' '.join(line) for line in table] lines = [line + '\n' for line in lines] lines.sort() with open(infoFile, 'w') as f: f.writelines(lines)