def sgr_sim_density(stripes, params): SGA = fi.read_data("../sgrnorth_paper/stream_gen_analysis.txt", ",") low_lam, high_lam = 200.0, 314.8 # from Sgr stripes: (200.025901082, 314.707561185) size = (high_lam - low_lam) / 15.0 N_stars = [19386, 18734, 11096, 17044, 18736, 15409, 12519, 12248, 8853, 7328, 5479, 4450, 3486, 2425, 971, 9511, 16119, 16603] south = [79, 82, 86] out = [] for i in range(15): print "# - Lambda wedge {0}:".format(i) num = str(i) name = "./sim_streams/lamsim_out_"+num+".txt" data = fi.read_data(name, echo=1) total = len(data[:,0]) other = sc.sum(data[:,3]) length = 7.653333333 # bin size out.append([(low_lam+(size*i)+(size*0.5)), total-other, total, 0.0, total/length]) for i in range(15,18): print "# - Southern wedge {0}:".format(south[i-15]) data = fi.read_data("./sim_streams/sim_stripe_"+str(south[i-15])+".txt") total = len(data[:,0]) other = sc.sum(data[:,3]) length = SGA[i,3] mu, r, theta, phi, wedge = eval(params[i][4]), eval(params[i][5]), \ eval(params[i][6]), eval(params[i][7]), stripes[i] x,y,z = coor.stream2xyz(0.0, 0.0, 0.0, mu, r, theta, phi, wedge) Xs, Ys, Zs, lam, beta, r = sl.law_xyz2sgr_sun(x,y,z) out.append([lam, total-other, total, 0.0, total/length]) fi.write_data(sc.array(out), "sgrNorth_density_sim.txt", ",", " lam(mid), count, SDSS Deff, SDSS/FTO Deff, per degree")
def sgr_sim_density(stripes, params): SGA = fi.read_data("../sgrnorth_paper/stream_gen_analysis.txt", ",") low_lam, high_lam = 200.0, 314.8 # from Sgr stripes: (200.025901082, 314.707561185) size = (high_lam - low_lam) / 15.0 N_stars = [ 19386, 18734, 11096, 17044, 18736, 15409, 12519, 12248, 8853, 7328, 5479, 4450, 3486, 2425, 971, 9511, 16119, 16603 ] south = [79, 82, 86] out = [] for i in range(15): print "# - Lambda wedge {0}:".format(i) num = str(i) name = "./sim_streams/lamsim_out_" + num + ".txt" data = fi.read_data(name, echo=1) total = len(data[:, 0]) other = sc.sum(data[:, 3]) length = 7.653333333 # bin size out.append([(low_lam + (size * i) + (size * 0.5)), total - other, total, 0.0, total / length]) for i in range(15, 18): print "# - Southern wedge {0}:".format(south[i - 15]) data = fi.read_data("./sim_streams/sim_stripe_" + str(south[i - 15]) + ".txt") total = len(data[:, 0]) other = sc.sum(data[:, 3]) length = SGA[i, 3] mu, r, theta, phi, wedge = eval(params[i][4]), eval(params[i][5]), \ eval(params[i][6]), eval(params[i][7]), stripes[i] x, y, z = coor.stream2xyz(0.0, 0.0, 0.0, mu, r, theta, phi, wedge) Xs, Ys, Zs, lam, beta, r = sl.law_xyz2sgr_sun(x, y, z) out.append([lam, total - other, total, 0.0, total / length]) fi.write_data(sc.array(out), "sgrNorth_density_sim.txt", ",", " lam(mid), count, SDSS Deff, SDSS/FTO Deff, per degree")
def do_cuts(infile, delimiter, outfile, cuts): # Get data data = fi.read_data(infile, delimiter) # cut data for i in range(len(cuts)): data = do_one_cut(data, cuts[i]) # write data fi.write_data(data, outfile, delimiter) print "# - Finished with cuts"
def rejection(filename, min=None, max=None, bin=0.2, reject=2.0, thresh=1000.0): data = fi.read_data(filename, ",") if min == None: min = np.ma.min(data[:,2]) if max == None: max = np.ma.max(data[:,2]) data_out = [] steps = int((max-min)/bin)+1 for i in range(steps): # build a data set for each bin data_temp = [] min_temp, max_temp = (min + i*bin), (min + (i+1)*bin) for j in range(len(data[:,0])): if (data[j,2] >= min_temp) and (data[j,2] < max_temp): data_temp.append(data[j,:]) print "number of stars in bin {0}: {1}".format(i, len(data_temp)) if len(data_temp)==0: continue #skip empty sets # continue checking for rejections until no rejections occur kills = [1] while len(kills) > 0: kills, new_temp = [], [] # pack the g-r values into a single list g_minus_r = [] for j in range(len(data_temp)): g_minus_r.append(data_temp[j][2]-data_temp[j][3]) g_minus_r = sc.array(g_minus_r) avg = sc.mean(g_minus_r) stdev = sc.std(g_minus_r) #print avg, stdev # Find the values that lie outside 'reject' sigma for j in range(len(g_minus_r)): diff = ma.fabs( g_minus_r[j] - avg ) if diff > (reject*stdev): kills.append(data_temp[j]) elif diff > thresh: kills.append(data_temp[j]) else: new_temp.append(data_temp[j]) data_temp = new_temp data_out = data_out + data_temp print "Stars kept in bin: {0}".format(len(data_temp)) # restructure data data_out = sc.array(data_out) print " {0} stars remaining out of {1} original stars".format(len(data_out[:,0]), len(data[:,0])) g_minus_r_ori = data[:,2] - data[:,3] g_minus_r_out = data_out[:,2] - data_out[:,3] # show plot, then ask to save data set fig = plt.figure() #plt.scatter(g_minus_r_ori, data[:,2], 2, marker='o', facecolor="r", edgecolor='face') plt.scatter(g_minus_r_out, data_out[:,2], 1, 'k', 'o') plt.ylim(max, min) plt.xlim(-1.0, 2.0) plt.ylabel(r'$g_0$') plt.xlabel(r"$(g-r)_0$") plt.show() fname = raw_input("save data? [filename to save, empty to skip]") if fname != "": fi.write_data(data_out, fname, header="#") print "# --- Done"
def all_stripes(stripes, file_in, out_form="stripe_"): t1 = time.time() for stripe in stripes: fileout = out_form + str(stripe) + ".txt" out, map = get_stripe(stripe, file_in) fi.write_data(out, fileout) plot_sky(map, stripe) t2 = time.time() print "Time elapsed, stripe {0}: {1} minutes".format( stripe, ((t2 - t1) / 60.0))
def sgr_lam_split(stripes, params): # get lambda for stripes holder = [] for i in range(len(stripes)): mu, r, theta, phi, wedge = eval(params[i][4]), eval(params[i][5]), \ eval(params[i][6]), eval(params[i][7]), stripes[i] x, y, z = coor.stream2xyz(0.0, 0.0, 0.0, mu, r, theta * deg, phi * deg, wedge) Xs, Ys, Zs, lam, beta, r = sl.law_xyz2sgr_sun(x, y, z) #if lam > 180.0: lam = lam - 360.0 #lam = -lam print lam, beta, r, stripes[i] holder.append([lam, beta, r]) Xs, Ys, Zs, lam, beta, r = sl.law_xyz2sgr_sun(14.655646, 2.270431, -5.887377) print lam, beta, r # get lambda range for northern Sgr 15 N stripes, 0-14 -> 9-23 low_lam, high_lam = 200.0, 314.8 # from Sgr stripes: (200.025901082, 314.707561185) size = (high_lam - low_lam) / 15.0 print "# - LAMBDA BIN SIZE: {0}".format(size) for h in range(15): array = [] lam_min, lam_max = low_lam + (size * h), low_lam + (size * (h + 1)) for i in range(15): if stripes[i] == 9: num = "09" else: num = str(stripes[i]) name = "./sim_streams/sim_stripe_" + num + ".txt" Sgr = open(name, "r") for line in Sgr: if line.strip() == "": continue if line.strip()[0] == "#": continue l, b, r, f = line.strip().split() x, y, z = coor.lbr2xyz(float(l), float(b), float(r)) Xs, Ys, Zs, lam, beta, r = sl.law_xyz2sgr_sun(x, y, z) #if lam > high_lam: high_lam = lam #if lam < low_lam: low_lam = lam if lam > lam_min: if lam <= lam_max: array.append([lam, beta, r, eval(f)]) Sgr.close() print "# - Stripe " + num + " successfully read in" fi.write_data(sc.array(array), "lamsim_out_" + str(h) + ".txt", header="Lambda {0}:{1}, lam, beta, r".format( lam_min, lam_max)) #print low_lam, high_lam # bin stripes in lambda, g # correct stripes for detection, FTO efficiencies # reflect stars about midpoint to correct for missing data on far end print "### --- Done"
def sgr_lam_split(stripes, params): # get lambda for stripes holder = [] for i in range(len(stripes)): mu, r, theta, phi, wedge = eval(params[i][4]), eval(params[i][5]), \ eval(params[i][6]), eval(params[i][7]), stripes[i] x,y,z = coor.stream2xyz(0.0, 0.0, 0.0, mu, r, theta*deg, phi*deg, wedge) Xs, Ys, Zs, lam, beta, r = sl.law_xyz2sgr_sun(x,y,z) #if lam > 180.0: lam = lam - 360.0 #lam = -lam print lam, beta, r, stripes[i] holder.append([lam, beta, r]) Xs, Ys, Zs, lam, beta, r = sl.law_xyz2sgr_sun(14.655646, 2.270431, -5.887377) print lam, beta, r # get lambda range for northern Sgr 15 N stripes, 0-14 -> 9-23 low_lam, high_lam = 200.0, 314.8 # from Sgr stripes: (200.025901082, 314.707561185) size = (high_lam - low_lam) / 15.0 print "# - LAMBDA BIN SIZE: {0}".format(size) for h in range(15): array = [] lam_min, lam_max = low_lam+(size*h), low_lam+(size*(h+1)) for i in range(15): if stripes[i] == 9: num = "09" else: num = str(stripes[i]) name = "./sim_streams/sim_stripe_"+num+".txt" Sgr = open(name, "r") for line in Sgr: if line.strip() == "": continue if line.strip()[0] == "#": continue l,b,r,f = line.strip().split() x,y,z = coor.lbr2xyz(float(l), float(b), float(r)) Xs, Ys, Zs, lam, beta, r = sl.law_xyz2sgr_sun(x,y,z) #if lam > high_lam: high_lam = lam #if lam < low_lam: low_lam = lam if lam > lam_min: if lam <= lam_max: array.append([lam, beta, r, eval(f)]) Sgr.close() print "# - Stripe "+num+" successfully read in" fi.write_data(sc.array(array), "lamsim_out_"+str(h)+".txt", header="Lambda {0}:{1}, lam, beta, r".format(lam_min, lam_max)) #print low_lam, high_lam # bin stripes in lambda, g # correct stripes for detection, FTO efficiencies # reflect stars about midpoint to correct for missing data on far end print "### --- Done"
def convert(filename, RaDec=0): readfile = open(filename, "r") holder, MW = [], [] for line in readfile: if line[0] == "#": continue if line.strip() == "": continue holder = line.split(",") for i in range(len(holder)): holder[i] = eval(holder[i]) if RaDec==1: l,b = coor.EqTolb(holder[0], holder[1]) holder[0], holder[1] = round(l,6) , round(b,6) r = coor.getr(holder[2], 4.2) holder[2] = round(r, 6) MW.append(holder[:3]) readfile.close() nstars = len(MW) out = sc.array(MW) fi.write_data(out, filename[:-4]+"_new.txt", " ", str(nstars))
def convert(filename, RaDec=0): readfile = open(filename, "r") holder, MW = [], [] for line in readfile: if line[0] == "#": continue if line.strip() == "": continue holder = line.split(",") for i in range(len(holder)): holder[i] = eval(holder[i]) if RaDec == 1: l, b = coor.EqTolb(holder[0], holder[1]) holder[0], holder[1] = round(l, 6), round(b, 6) r = coor.getr(holder[2], 4.2) holder[2] = round(r, 6) MW.append(holder[:3]) readfile.close() nstars = len(MW) out = sc.array(MW) fi.write_data(out, filename[:-4] + "_new.txt", " ", str(nstars))
def separate_stars(sort, files=["matched.txt", "nomatch1.txt", "nomatch2.txt"]): # Matched stars holder = [] for i in range(sort.l1): if sort.flags1[i] == 1: holder.append(sort.data1[i, :]) fi.write_data(sc.array(holder), files[0]) s1 = len(holder) # Unmatched 1 holder = [] for i in range(sort.l1): if sort.flags1[i] == 0: holder.append(sort.data1[i, :]) fi.write_data(sc.array(holder), files[1]) s2 = len(holder) # Unmatched 2 holder = [] for i in range(sort.l2): if sort.flags2[i] == 0: holder.append(sort.data2[i, :]) fi.write_data(sc.array(holder), files[2]) s3 = len(holder) print "# - {0} matched stars, {1} unmatched in file 1, {2} unmatched in file 2".format( s1, s2, s3)
def save_hists(l_hist=None, red_hist=None, dered_hist=None): if l_hist != None: print "# --- length of l_hist: {0}".format(len(l_hist)) l_out = sc.zeros((360, 2),float) for i in range(len(l_out[:,0])): l_out[i,0] = float(i) l_out[i,1] = l_hist[i] fi.write_data(l_out, "l_hist_data.csv", delimiter=",", header="angle, number of stars") if red_hist != None: print "# --- shape of red_hist: {0}".format(red_hist.shape) fi.write_data(red_hist, "red_hist_data.csv", delimiter=",") if dered_hist != None: print "# --- shape of dered_hist: {0}".format(dered_hist.shape) fi.write_data(dered_hist, "dered_hist_data.csv", delimiter=",")
def save_hists(l_hist=None, red_hist=None, dered_hist=None): if l_hist != None: print "# --- length of l_hist: {0}".format(len(l_hist)) l_out = sc.zeros((360, 2), float) for i in range(len(l_out[:, 0])): l_out[i, 0] = float(i) l_out[i, 1] = l_hist[i] fi.write_data(l_out, "l_hist_data.csv", delimiter=",", header="angle, number of stars") if red_hist != None: print "# --- shape of red_hist: {0}".format(red_hist.shape) fi.write_data(red_hist, "red_hist_data.csv", delimiter=",") if dered_hist != None: print "# --- shape of dered_hist: {0}".format(dered_hist.shape) fi.write_data(dered_hist, "dered_hist_data.csv", delimiter=",")
def separate_stars(sort, files=["matched.txt", "nomatch1.txt", "nomatch2.txt"]): # Matched stars holder = [] for i in range(sort.l1): if sort.flags1[i] == 1: holder.append(sort.data1[i,:]) fi.write_data(sc.array(holder), files[0]) s1 = len(holder) # Unmatched 1 holder = [] for i in range(sort.l1): if sort.flags1[i] == 0: holder.append(sort.data1[i,:]) fi.write_data(sc.array(holder), files[1]) s2 = len(holder) # Unmatched 2 holder = [] for i in range(sort.l2): if sort.flags2[i] == 0: holder.append(sort.data2[i,:]) fi.write_data(sc.array(holder), files[2]) s3 = len(holder) print "# - {0} matched stars, {1} unmatched in file 1, {2} unmatched in file 2".format(s1,s2,s3)
def get_dist(point, center): # both ra, dec tuples x = (point[0] - center[0])*(point[0] - center[0]) y = (point[1] - center[1])*(point[1] - center[1]) return np.sqrt(x + y) def make_annulus_series(data, step, limits, center, c_cut=None): """ """ incs = int((limits[1] - limits[0]) / step) + 1 an_hist = [] for i in range(incs): d_cut = ( (i*step)+limits[0], ((i+1)*step)+limits[0]) height = get_annulus(data, c_cut, center, d_cut) pos = (i*step)+limits[0] an_hist.append([pos, height]) return sc.array(an_hist) if __name__ == "__main__": data = fi.read_data('HR_NGC_5053.csv', ",") step = 0.005 limits = (0.0, 0.14) center = (199.109, 17.697) #Ra, dec c_cut = (0.2, 0.5) gc_prof = make_annulus_series(data, step, limits, center, c_cut) fi.write_data(gc_prof, "annuli_out_med.txt") fig = plt.figure(1) plt.bar(gc_prof[:,0], gc_prof[:,1], step) plt.xlabel("Radius, degrees") plt.ylabel("counts") plt.show() print "# --- Done"
def compare_plot(out, size): #Make plot fig = plt.figure() plt.bar(out[:, 0], out[:, 1], size) #x = sc.arange(0.0, 47.0, 1.0) #y = func.SDSS_error_function(x, [1.02, 0.119, -3.53]) #plt.plot(x, y, 'k-') plt.xlabel(r'Sun-centered $r$ (kpc)') plt.ylabel(r'$(\rho_{1} / \rho_{2})$ (stars/kpc)') #plt.savefig(("diff.ps"), papertype='letter') plt.show() plt.close('all') if __name__ == "__main__": file1 = "stars-82.txt" file2 = "stars-82-coadd2.txt" file3 = "matched-82-1arcsec.txt" mag = 0 size = 1.0 out1 = do_compare_wedges(file1, file3, stripe=82, mag=mag, size=size) out2 = do_compare_wedges(file3, file2, stripe=82, mag=mag, size=size) out = sc.zeros((len(out2[:, 0]), 2)) for i in range(len(out[:, 0])): out[i, 0], out[i, 1] = out2[i, 0], (out1[i, 1] * out2[i, 1]) compare_plot(out, size) for i in range(len(out[:, 0])): out[i, 0] = out[i, 0] + (size / 2.0) fi.write_data(out, "eps1_eps2.txt")
errors = hes.get_hessian_errors(gfe.get_2gauss_y, best_parameters, hist_data[:, 0], hist_data[:, 1], sig_in, step_size) print '#- Distance, fwhm, mu, sigma_l, sigma_r, amplitude, errors for run', gc_name print '$ ', con_dist, (gfe.full_width_half_max(best_parameters[1],best_parameters[2])), \ best_parameters[0], best_parameters[1], best_parameters[2], best_parameters[3], \ errors[0], errors[1], errors[2], errors[3] if (gcp.plot_hist(hist_data, gc_name, 1, best_parameters, 0, plot_file) == 1): print '#-Histogram plotted' if (gcp.plot_HR_diagram(con_clus, real_dist, gc_name, [1.0, 7.0], [0.0, 0.6], plot_file) == 1): #if (gcp.plot_HR_diagram(gce.select_stars_with_sigmoid(con_clus, con_dist, low_limit=0.0,high_limit=0.6), # real_dist, gc_name,[1.0,7.0],[0.0,0.6], plot_file) == 1): print '#-HR diagram plotted' if run == 0: if (gcp.plot_infiles(main_data, back_data, gc_name, plot_file) == 1): print '-Skyplot finished' print '******', gc_name, '-process complete*****' """ EXTRA""" #print cluster, run f.write_data(hist_data, (NAME[cluster] + '_' + str(round(CONVOLVE[cluster][run])) + '_' + '.txt'), delimiter='\t', header=str(best_parameters)) print '#---ALL RUNS COMPLETE---'
no_g = no_g + 1 if (sigma_r > 0.0): r[i] = np.random.normal(y[i,3], sigma_r) else: r[i] = y[i,3] no_r = no_r + 1 #print sigma_g, sigma_r, g[i], r[i] ###Two columns, absolute g and g-r print '-uncon g:', no_g, 'uncon r:', no_r if columns == 2: m = sc.zeros((l,2)) for i in range(l): m[i,0] = g[i] - 5.*(np.log10(distance*1000) - 1.) m[i,1] = g[i] - r[i] ###Four columns: Ra, Dec, app_g, app_r; usually used for .csv output elif columns == 4: m = sc.zeros((l,4)) for i in range(l): m[i,0] = y[i,0] m[i,1] = y[i,1] m[i,2] = g[i] m[i,3] = r[i] else: m = [0, 0, 0, 0] print "-Please use a valid column number" if f.write_data(m, outfile, ' ') == 1: print '-Convolved data written successfully'
for j in range(len(kill_list)): star_list.pop(kill_list[j]) g_list.pop(kill_list[j]) for k in range(len(kill_list)): kill_list[k] = (kill_list[k]-1) #Fit strips to Girardi isocrones using different fit functions #now have 'iso_array' and 'fit_array' - save to file! if (save_data==1): outname = cluster_name + '_out.txt' out_data = sc.zeros((iso_l, 4),float) out_data[:,0], out_data[:,1], out_data[:,2], out_data[:,3] = \ iso_array[:,0], iso_array[:,1], fit_array[:,1], fit_array[:,2] dist_str = (str(distance)) header_txt = 'g, iso g-r, fiducial g-r, uncertainty mean, from file ' + iso_file + ' with distance ' + dist_str #print out_data #print out_data.shape if (f.write_data(out_data, outname, header=header_txt) == 1): print 'iso g, iso g-r, fid g-r, fiducial sigma, data successfully saved' #fit distances? Hard! Need to start from beginning...""" plt.figure() A = -0.01463 B = 0.08929 y = iso_array[:,0] x = (fit_array[:,1] - iso_array[:,1]) plt.scatter(x,y, c='black', marker='+', label='Subtracted data') plt.plot([0.0,0.0], [np.ma.min(iso_array[:,0]),np.ma.max(iso_array[:,0])], 'r-', label='_nolegend_') plt.plot([(A*np.ma.min(y)+B),(A*np.ma.max(y)+B)], [np.ma.min(y), np.ma.max(y)], 'g:', label='Line Fit') #plt.plot([(A*np.ma.min(y)+B),np.ma.min(y)], [(A*np.ma.max(y)+B), np.ma.max(y)], 'g:', label='Line Fit') #plt.errorbar(x,y,xerr=fit_array[:,2]) plt.errorbar(iso_array[:,1],iso_array[:,0], fmt='o', mfc='c', mec='c', mew=1, label='Girardi isocrone') plt.errorbar(fit_array[:,1],iso_array[:,0],xerr=fit_array[:,2], fmt=None, ecolor='k') plt.scatter(fit_array[:,1],iso_array[:,0], marker='d', c='black', label='Fiducial sequence')
points = [] for i in range(len(margins)): holder = [] for j in range(len(avg_in[:, 0])): if ((avg_in[j, 0] > (margins[i] - 2)) and avg_in[j, 0] < margins[i]): holder.append(avg_in[j, y]) data_point = [(margins[i] - 1.0), sc.mean(holder), sc.std(holder)] points.append(data_point) averages = sc.array(points) plt.errorbar(averages[:, 0], averages[:, 1], yerr=averages[:, 2], fmt='o', color='b', ecolor='b', markersize=4) f.write_data(averages, averages_file, header=('Distance, Average, Standard Deviation for ' + axes_names[y])) print '#-Printed and plotted averages and standard deviations' #plt.title('Plot of convolved data') ax1.set_xlabel(x_label) ax2.set_xlabel(r'$\bar{g}_0$') ax1.set_ylabel(y_label) fig.savefig(plot_file, papertype='letter') plt.close('all') print '#-plot successfully created' print '#---All Plots sucessfully created'
#newer_far = far_data[:,1] - sc.mean(y) #for i in range(len(newer_far)): # if newer_far[i] < 0.0: newer_far[i] = 0.0 out_array = sc.zeros((len(far_data), 3), float) out_array[:,0] = avg_data[:,0] out_array[:,1] = new_far[:] #h.plot_histogram(out_array, 0.2, limits = [], name='far_clus_renormed', x_label=r'$M_g$') h.plot_multiple_hist(out_array[:,0], [avg_data[:,1], new_far], 0.2, limits = [], name=(clus_name+'_and_avg_two'), x_label=r'$M_g$') ratio_data = [] for i in range(len(new_far)): ratio_data.append( [ far_data[i,0], (new_far[i] / avg_data[i,1]), error_array[i] ]) f.write_data(sc.array(ratio_data), (clus_name+"_ratio_data_new.txt") ) """ """ ### Get histogram files in, convert to absolute magnitude, then average file_names = ['6205_normed_hist.txt', '5904_normed_hist.txt', '5272_normed_hist.txt'] dist = [7.7, 8.0, 10.4] counts = [2164, 3698, 4364] data = [] for cluster in file_names: data.append(f.read_data(cluster)) # Get absolute magnitudes and check that they are equal abs_mags = [] for i in range(len(data)): modulus = (5.*(m.log10(dist[i]*1000) - 1.)) abs_mags.append(data[i][:,0] - modulus) for i in range(len(abs_mags[0])): if (round(abs_mags[0][i], 3) == round(abs_mags[1][i], 3) == round(abs_mags[2][i], 3)): print "#--Absolute magnitudes check out"
import matplotlib.pyplot as plt import files as f '''selects out only a portion of a data set Used for f-turnoff stars right now. ---Matthew Newby ''' starfile = "convolved_5904_cluster_lilcon.txt" outfile = 'convolved_5904_cluster_lilcon_f.txt' low_limit = 0.0 high_limit = 0.3 y = f.read_data(starfile) l, w = y.shape index = sc.zeros(l, int) for i in range(l): if (y[i,2] - y[i,3]) >= low_limit: if (y[i,2] - y[i,3]) <= high_limit: index[i] = 1 s = sc.sum(index) print '-Stars in data set:', l, 'Stars in f-turnoff:', s new_data = sc.zeros((s,w)) j = 0 for i in range(l): if index[i] == 1: new_data[j,:] = y[i,:] j = j + 1 if (j != s): print '-!!! stars out not equal to f-turnoff:', j, s if f.write_data(new_data, outfile, ' ') == 1: print '-F-turnoff stars written successfully'
for i in range(len(x1)): if np.random.uniform() < 0.2: x2[i] = x1[i] + 0.05 else: x2[i] = x1[i] y1, z1 = np.histogram(x1, 40, range=(0.0, 0.4)) y2, z2 = np.histogram(x2, 40, range=(0.0, 0.4)) m1 = sc.zeros((len(y1), 2)) m1[:, 0] = (z1[:-1] + 0.005) m1[:, 1] = y1 m2 = sc.zeros((len(y2), 2)) m2[:, 0] = (z2[:-1] + 0.005) m2[:, 1] = y2 fi.write_data(m1, "ori_color.txt") fi.write_data(m2, "new_color.txt") plt.figure(1) plt.bar(z2[:-1], y2, 0.01, color="blue") plt.bar(z1[:-1], y1, 0.01, color="green", fill=None) plt.show() ''' For magnitude study """ For double-sided Gaussian """ N1 = 642 #N2*(0.36/0.76) N2 = 1357 #2000.0 / ((0.36/0.76)+1) a1 = np.random.normal(4.2, 0.36, N1) a2 = np.random.normal(4.2, 0.76, N2) holder = [] for i in range(len(a1)):
r = sc.zeros(l) for i in range(l): #print y[i,2], y[i,3] sigma_g = a_g + np.exp(b_g*(y[i,2] + diff_mag) + c_g) sigma_r = a_r + np.exp(b_r*(y[i,3] + diff_mag) + c_r) g[i] = np.random.normal(y[i,2], sigma_g) r[i] = np.random.normal(y[i,3], sigma_r) #print sigma_g, sigma_r, g[i], r[i] ###Two columns, absolute g and g-r if columns == 2: m = sc.zeros((l,2)) for i in range(l): m[i,0] = g[i] - 5.*(np.log10(distance*1000) - 1.) m[i,1] = g[i] - r[i] ###Four columns: Ra, Dec, app_g, app_r elif columns == 4: m = sc.zeros((l,4)) for i in range(l): m[i,0] = y[i,0] m[i,1] = y[i,1] m[i,2] = g[i] m[i,3] = r[i] else: m = [0, 0, 0, 0] print "-Please use a valid column number" if f.write_data(m, 'convolved_5904_cluster.csv', ',') == 1: print '-Convolved data written succesfully'
x = (point[0] - center[0]) * (point[0] - center[0]) y = (point[1] - center[1]) * (point[1] - center[1]) return np.sqrt(x + y) def make_annulus_series(data, step, limits, center, c_cut=None): """ """ incs = int((limits[1] - limits[0]) / step) + 1 an_hist = [] for i in range(incs): d_cut = ((i * step) + limits[0], ((i + 1) * step) + limits[0]) height = get_annulus(data, c_cut, center, d_cut) pos = (i * step) + limits[0] an_hist.append([pos, height]) return sc.array(an_hist) if __name__ == "__main__": data = fi.read_data('HR_NGC_5053.csv', ",") step = 0.005 limits = (0.0, 0.14) center = (199.109, 17.697) #Ra, dec c_cut = (0.2, 0.5) gc_prof = make_annulus_series(data, step, limits, center, c_cut) fi.write_data(gc_prof, "annuli_out_med.txt") fig = plt.figure(1) plt.bar(gc_prof[:, 0], gc_prof[:, 1], step) plt.xlabel("Radius, degrees") plt.ylabel("counts") plt.show() print "# --- Done"
print '#-GD app mu:', (best_parameters[0] + 5.*(m.log10(real_dist*1000) - 1.)) print '#-GD fwhm:', (gfe.full_width_half_max(best_parameters[1],best_parameters[2])) lh,wh = hist_data.shape sig_in = sc.ones(lh) step_size = [0.01, 0.001, 0.001, 0.01] errors = hes.get_hessian_errors(gfe.get_2gauss_y, best_parameters, hist_data[:,0], hist_data[:,1], sig_in, step_size) print '#- Distance, fwhm, mu, sigma_l, sigma_r, amplitude, errors for run', gc_name print '$ ', con_dist, (gfe.full_width_half_max(best_parameters[1],best_parameters[2])), \ best_parameters[0], best_parameters[1], best_parameters[2], best_parameters[3], \ errors[0], errors[1], errors[2], errors[3] if (gcp.plot_hist(hist_data, gc_name, 1, best_parameters, 0, plot_file) == 1): print '#-Histogram plotted' if (gcp.plot_HR_diagram(con_clus, real_dist, gc_name,[1.0,7.0],[0.0,0.6], plot_file) == 1): #if (gcp.plot_HR_diagram(gce.select_stars_with_sigmoid(con_clus, con_dist, low_limit=0.0,high_limit=0.6), # real_dist, gc_name,[1.0,7.0],[0.0,0.6], plot_file) == 1): print '#-HR diagram plotted' if run == 0: if (gcp.plot_infiles(main_data, back_data, gc_name, plot_file) == 1): print '-Skyplot finished' print '******', gc_name, '-process complete*****' """ EXTRA""" #print cluster, run f.write_data(hist_data, (NAME[cluster]+'_'+str(round(CONVOLVE[cluster][run]))+'_'+'.txt'), delimiter='\t', header=str(best_parameters)) print '#---ALL RUNS COMPLETE---'
if high_cut != None: if data[i, col] > high_cut: continue holder.append(data[i, :]) return sc.array(holder) """ Main Cutting Function """ def cut_data(data, *args): new_data = [] if len(args) == 0: print "!!! NO ARGUMENTS DEFINING CUT !!!\n" usage() sys.exit(2) for arg in args: if new_data == []: new_data = cut_once(data, arg) else: new_data = cut_once(new_data, arg) return new_data if __name__ == "__main__": import files as fi file_in = "noU_NGC_5466_background.csv" data = fi.read_data(file_in, ",") data_out = cut_data(data, (2, 20.0, 21.0), (3, None, 20.0)) fi.write_data(data_out, (file_in[:-4] + "_cut.txt")) #print data_out #print "length of data:", len(data[:,0]), "; length of cut data:", len(data_out[:,0])
if data[j,2] > (mu+spread): continue if data[j,2] < (mu-spread): continue holder.append(data[j,2]-data[j,3]) histogram = h.make_hist(holder, 0.01) h.plot_histogram(histogram, 0.01, name=('hist_'+str(i)), x_label=r'$(g-r)_0$') #h.plot_multiple_hist(histogram[:,0], [histogram[:,1]], 0.01, name=('hist_'+str(i)), x_label=r'$(g-r)_0$') #x_col, y_col, sig_col = histogram[:,0], (histogram[:,1]/len(holder)), (func.poisson_errors(histogram[:,1])/len(holder) ) x_col, y_col, sig_col = histogram[:,0], (histogram[:,1]), (func.poisson_errors(histogram[:,1])) #print x_col, y_col, sig_col start_params = sc.array([0.2, 0.1, sc.ma.max(y_col)]) steps = sc.array([0.001, 0.001, 0.01]) function = func.gaussian_function #Fit function MCMC_fit, errors, best_fit = mc.do_MCMC(function, start_params, steps, x_col, y_col, sig_col, name=('histfit_'+str(i)), number_steps=10000, save=0) best_fit = gd.gradient_descent(function, best_fit, x_col, y_col, sig_col) errors = he.get_hessian_errors(function, best_fit, x_col, y_col, sig_col, steps) turnoff.append([best_fit[0], best_fit[1], errors[0], errors[1]]) # if (pdf.plot_function(x_col, y_col, function, best_fit, save_name=('histfitplot_'+str(i)), # title=['', r'$(g-r)_0$', 'Normalized Counts'], save=1) == 1): plt.figure() plt.scatter(x_col, y_col) func_x = sc.arange( (sc.ma.min(x_col)*0.9), (sc.ma.max(x_col)*1.1), 0.01) func_y = function(func_x, best_fit) plt.plot(func_x, func_y) plt.savefig(('histfitplot_'+str(i)+'.ps'), papertype='letter') print "#-Fit dataplot saved" #print turnoff print turnoff if f.write_data(sc.array(turnoff), 'turnoff_out_fixed.txt') == 1: print "#-All Results Successfully saved"
l, w = y.shape g = sc.zeros(l) r = sc.zeros(l) for i in range(l): #print y[i,2], y[i,3] sigma_g = a_g + np.exp(b_g * (y[i, 2] + diff_mag) + c_g) sigma_r = a_r + np.exp(b_r * (y[i, 3] + diff_mag) + c_r) g[i] = np.random.normal(y[i, 2], sigma_g) r[i] = np.random.normal(y[i, 3], sigma_r) #print sigma_g, sigma_r, g[i], r[i] ###Two columns, absolute g and g-r if columns == 2: m = sc.zeros((l, 2)) for i in range(l): m[i, 0] = g[i] - 5. * (np.log10(distance * 1000) - 1.) m[i, 1] = g[i] - r[i] ###Four columns: Ra, Dec, app_g, app_r elif columns == 4: m = sc.zeros((l, 4)) for i in range(l): m[i, 0] = y[i, 0] m[i, 1] = y[i, 1] m[i, 2] = g[i] m[i, 3] = r[i] else: m = [0, 0, 0, 0] print "-Please use a valid column number" if f.write_data(m, 'convolved_5904_cluster.csv', ',') == 1: print '-Convolved data written succesfully'
import math as m import numpy as np import scipy as sc import files as f import gradient_descent as gd import monte_carlo as mc import Hessian_errors as he import plot_data_function as pdf import time import functions as func '''python script for running gc scripts Matthew Newby, Sept 6, 2010''' '''Add Limits to parameters and data! ''' """ # Generate Gaussian Data sig = 10.0 x = sc.arange(0.0, 10.0, 0.1) y = (func.gaussian_function(x, [10.0, 3.5, 0.8]) )#+ np.random.normal(0.0, sig, len(x))) out = sc.zeros((len(x),3)) for i in range(len(x)): out[i,0], out[i,1], out[i,2] = x[i], y[i], sig f.write_data(out, "test_out_p.txt") """ #MAIN #Setup variables filename = 'Pal13_backsub_cluscut.txt' function = func.get_2gauss_y identifier = ['text', 'A', 'B', 'C', 'D', 'F', 'G', 'H'] #Load data
def rejection(filename, min=None, max=None, bin=0.2, reject=2.0, thresh=1000.0): data = fi.read_data(filename, ",") if min == None: min = np.ma.min(data[:, 2]) if max == None: max = np.ma.max(data[:, 2]) data_out = [] steps = int((max - min) / bin) + 1 for i in range(steps): # build a data set for each bin data_temp = [] min_temp, max_temp = (min + i * bin), (min + (i + 1) * bin) for j in range(len(data[:, 0])): if (data[j, 2] >= min_temp) and (data[j, 2] < max_temp): data_temp.append(data[j, :]) print "number of stars in bin {0}: {1}".format(i, len(data_temp)) if len(data_temp) == 0: continue #skip empty sets # continue checking for rejections until no rejections occur kills = [1] while len(kills) > 0: kills, new_temp = [], [] # pack the g-r values into a single list g_minus_r = [] for j in range(len(data_temp)): g_minus_r.append(data_temp[j][2] - data_temp[j][3]) g_minus_r = sc.array(g_minus_r) avg = sc.mean(g_minus_r) stdev = sc.std(g_minus_r) #print avg, stdev # Find the values that lie outside 'reject' sigma for j in range(len(g_minus_r)): diff = ma.fabs(g_minus_r[j] - avg) if diff > (reject * stdev): kills.append(data_temp[j]) elif diff > thresh: kills.append(data_temp[j]) else: new_temp.append(data_temp[j]) data_temp = new_temp data_out = data_out + data_temp print "Stars kept in bin: {0}".format(len(data_temp)) # restructure data data_out = sc.array(data_out) print " {0} stars remaining out of {1} original stars".format( len(data_out[:, 0]), len(data[:, 0])) g_minus_r_ori = data[:, 2] - data[:, 3] g_minus_r_out = data_out[:, 2] - data_out[:, 3] # show plot, then ask to save data set fig = plt.figure() #plt.scatter(g_minus_r_ori, data[:,2], 2, marker='o', facecolor="r", edgecolor='face') plt.scatter(g_minus_r_out, data_out[:, 2], 1, 'k', 'o') plt.ylim(max, min) plt.xlim(-1.0, 2.0) plt.ylabel(r'$g_0$') plt.xlabel(r"$(g-r)_0$") plt.show() fname = raw_input("save data? [filename to save, empty to skip]") if fname != "": fi.write_data(data_out, fname, header="#") print "# --- Done"
import matplotlib as mpl import matplotlib.pyplot as plt import files as f '''selects out only a portion of a data set Used for f-turnoff stars right now. ---Matthew Newby ''' starfile = "convolved_5904_cluster_lilcon.txt" outfile = 'convolved_5904_cluster_lilcon_f.txt' low_limit = 0.0 high_limit = 0.3 y = f.read_data(starfile) l, w = y.shape index = sc.zeros(l, int) for i in range(l): if (y[i, 2] - y[i, 3]) >= low_limit: if (y[i, 2] - y[i, 3]) <= high_limit: index[i] = 1 s = sc.sum(index) print '-Stars in data set:', l, 'Stars in f-turnoff:', s new_data = sc.zeros((s, w)) j = 0 for i in range(l): if index[i] == 1: new_data[j, :] = y[i, :] j = j + 1 if (j != s): print '-!!! stars out not equal to f-turnoff:', j, s if f.write_data(new_data, outfile, ' ') == 1: print '-F-turnoff stars written successfully'
holder.append(yellow_stars) holder.append(red_stars) '''stick it in the value holder''' print '#---', holder values.append(holder) print "### DEAD", killed, "###" '''Average all the values in 'values' ''' hold_array = sc.array(values) holder = [] for i in range(10): holder.append(sc.mean(hold_array[:, i])) print holder data_out.append(holder) f.write_data( sc.array(data_out), 'new_output.txt', header= "distance (kpc), blue-in-blue, b-y, b-r; y-b, y-y, y-r; r-b, r-y, r-r") print '#---All Done!' """ Probably crap! def convolve(data_in, real_dist, con_dist): a_u = 2.71e-03 b_u = 0.80 c_u = -19.2 a_g = 3.11e-04 b_g = 0.79 c_g = -20.0 a_r = -2.63e-05 b_r = 0.80 c_r = -19.8 diff_mag = 5.0*(np.log10(con_dist / real_dist))
# 'k--') #now finds the average y of all points in dist. bins of size 2.0, # and outputs the set of points if (find_average == 1): avg_in = f.read_data(file_to_average) distances = avg_in[:,x] values = avg_in[:,y] margins = [9,11,13,15,17,19,21,23,25,27,29] points = [] for i in range(len(margins)): holder = [] for j in range(len(avg_in[:,0])): if ( (avg_in[j,0] > (margins[i]-2) ) and avg_in[j,0] < margins[i]): holder.append(avg_in[j,y]) data_point = [ (margins[i]-1.0), sc.mean(holder), sc.std(holder) ] points.append(data_point) averages = sc.array(points) plt.errorbar(averages[:,0], averages[:,1], yerr=averages[:,2], fmt='o', color='b', ecolor='b', markersize=4) f.write_data(averages, averages_file, header=('Distance, Average, Standard Deviation for '+axes_names[y]) ) print '#-Printed and plotted averages and standard deviations' #plt.title('Plot of convolved data') ax1.set_xlabel(x_label) ax2.set_xlabel(r'$\bar{g}_0$') ax1.set_ylabel(y_label) fig.savefig(plot_file, papertype='letter') plt.close('all') print '#-plot successfully created' print '#---All Plots sucessfully created'
reject = 0 else: print 'kill length', len(kill_list), 'bin', (i) for j in range(len(kill_list)): star_list.pop(kill_list[j]) g_list.pop(kill_list[j]) for k in range(len(kill_list)): kill_list[k] = (kill_list[k] - 1) #now have 'iso_array' and 'fit_array' - save to file! if (save_data == 1): outname = cluster_name + '_fiducial_out.txt' dist_str = (str(distance)) header_txt = 'g, g-r, uncertainty mean, from file ' + data_file + ' with distance ' + dist_str #print out_data #print out_data.shape if (f.write_data(fit_array, outname, header=header_txt) == 1): print 'iso g, g-r, sigma, data successfully saved' #fit distances? Hard! Need to start from beginning...""" plt.figure() y = gc_array[:, 0] x = gc_array[:, 1] plt.scatter(x, y, c='black', marker='+', label='HR data') #plt.errorbar(x,y,xerr=fit_array[:,2]) plt.errorbar(fit_array[:, 1], fit_array[:, 0], xerr=fit_array[:, 2], fmt=None, ecolor='g') plt.scatter(fit_array[:, 1], fit_array[:, 0], c='green',
l, w = y.shape g = sc.zeros(l) r = sc.zeros(l) for i in range(l): #print y[i,2], y[i,3] sigma_g = a_g + np.exp(b_g * (y[i, 2] + diff_mag) + c_g) sigma_r = a_r + np.exp(b_r * (y[i, 3] + diff_mag) + c_r) g[i] = np.random.normal(y[i, 2], sigma_g) r[i] = np.random.normal(y[i, 3], sigma_r) #print sigma_g, sigma_r, g[i], r[i] ###Two columns, absolute g and g-r if columns == 2: m = sc.zeros((l, 2)) for i in range(l): m[i, 0] = g[i] - 5. * (np.log10(distance * 1000) - 1.) m[i, 1] = g[i] - r[i] ###Four columns: Ra, Dec, app_g, app_r; usually used for .csv output elif columns == 4: m = sc.zeros((l, 4)) for i in range(l): m[i, 0] = y[i, 0] m[i, 1] = y[i, 1] m[i, 2] = g[i] m[i, 3] = r[i] else: m = [0, 0, 0, 0] print "-Please use a valid column number" if f.write_data(m, outfile, ',') == 1: print '-Convolved data written successfully'
# Read arguments filename = sys.argv[1] column = int(sys.argv[2]) size = float(sys.argv[3]) if len(sys.argv) > 4: spread = [float(sys.argv[4]), float(sys.argv[5])] else: spread = [] if len(sys.argv) > 6: name = sys.argv[6] else: name = 'quick' # Load Data suffix = filename[-4:] if suffix == '.txt': data = f.read_data(filename) elif suffix == '.csv': data = f.read_csv(filename) # Run scripts to_bin = data[:, column] reg_hist = make_hist(to_bin, size, spread) plot_histogram(reg_hist, size, name=(name + '_normal')) f.write_data(reg_hist, fileout=(name + '_normal.txt'), header='# Centers, Counts') cum_hist = cumulative_hist(to_bin, size, spread) plot_histogram(cum_hist, size, name=(name + '_cumulative')) f.write_data(cum_hist, fileout=(name + '_cumulative.txt'), header='# Centers, Counts') print '#done with quick histograms'
for i in range(len(data[:,0])): if low_cut != None: if data[i,col] < low_cut: continue if high_cut != None: if data[i,col] > high_cut: continue holder.append(data[i,:]) return sc.array(holder) """ Main Cutting Function """ def cut_data(data, *args): new_data = [] if len(args) == 0: print "!!! NO ARGUMENTS DEFINING CUT !!!\n" usage() sys.exit(2) for arg in args: if new_data == []: new_data = cut_once(data, arg) else: new_data = cut_once(new_data, arg) return new_data if __name__ == "__main__": import files as fi file_in = "noU_NGC_5466_background.csv" data = fi.read_data(file_in, ",") data_out = cut_data(data, (2, 20.0, 21.0), (3, None, 20.0)) fi.write_data(data_out, (file_in[:-4]+"_cut.txt")) #print data_out #print "length of data:", len(data[:,0]), "; length of cut data:", len(data_out[:,0])
plt.close('all') """ """ for randomly selecting stars from the unmatched star file import astro_coordinates as coor data = fi.read_data("no_matches_82.txt", ",") samples = np.random.uniform(0, len(data[:,0]), 20) for i in range(len(samples)): l, b = data[samples[i],0], data[samples[i],1] ra, dec = coor.lbToEq(l,b) print float(ra), float(dec) """ ''' """ For double-sided Gaussian """ import matplotlib.pyplot as plt N1 = 642 #N2*(0.36/0.76) N2 = 1357 #2000.0 / ((0.36/0.76)+1) a1 = np.random.normal(4.2, 0.36, N1) a2 = np.random.normal(4.2, 0.76, N2) holder = [] for i in range(len(a1)): if a1[i] < 4.2: holder.append(a1[i]) for i in range(len(a2)): if a2[i] > 4.2: holder.append(a2[i]) x1 = sc.array(holder) #END OF DOBLE_GAUSSIAN
import functions as func import time '''python script for quickly creating test data sets. Matthew Newby, May 26, 2011''' out_file = "test_poly_out.txt" function = func.Nth_order_polynomial parameters = [11.0, -1.0, 0.5] x = sc.arange(-4.0, 4.0, 0.1) y = function(x, parameters) sig = 1.0 #1 sigma value for errors, takes: None or float out = sc.ones((len(x), 3)) out[:, 0] = x if (sig == None): out[:, 1] = y else: np.random.seed(int(time.time())) for i in range(len(y)): out[i, 1] = np.random.normal(y[i], sig) out[i, 2] = float(sig) if fi.write_data(out, out_file, header="#-x, y, sig"): print "#- File output succeeded" plt.figure(1) plt.scatter(out[:, 0], out[:, 1], 2, 'k', 'o') plt.show() plt.close('all') print "# - Done"
return out def compare_plot(out, size): #Make plot fig = plt.figure() plt.bar(out[:,0], out[:,1], size) #x = sc.arange(0.0, 47.0, 1.0) #y = func.SDSS_error_function(x, [1.02, 0.119, -3.53]) #plt.plot(x, y, 'k-') plt.xlabel(r'Sun-centered $r$ (kpc)') plt.ylabel(r'$(\rho_{1} / \rho_{2})$ (stars/kpc)') #plt.savefig(("diff.ps"), papertype='letter') plt.show() plt.close('all') if __name__ == "__main__": file1="stars-82.txt" file2="stars-82-coadd2.txt" file3="matched-82-1arcsec.txt" mag = 0 size = 1.0 out1 = do_compare_wedges(file1, file3, stripe=82, mag=mag, size=size) out2 = do_compare_wedges(file3, file2, stripe=82, mag=mag, size=size) out = sc.zeros((len(out2[:,0]),2)) for i in range(len(out[:,0])): out[i,0], out[i,1] = out2[i,0], (out1[i,1]*out2[i,1]) compare_plot(out, size) for i in range(len(out[:,0])): out[i,0] = out[i,0] + (size/2.0) fi.write_data(out, "eps1_eps2.txt")
red_stars = red_stars + 1 else: yellow_stars = yellow_stars + 1 holder.append(blue_stars) holder.append(yellow_stars) holder.append(red_stars) '''Count Data - red''' blue_stars, yellow_stars, red_stars = 0, 0, 0 for i in range(len(red_con)): g_minus_r = (red_con[i][2] - red_con[i][3]) if g_minus_r < BLUE_LIMIT: blue_stars = blue_stars + 1 elif g_minus_r > RED_LIMIT: red_stars = red_stars + 1 else: yellow_stars = yellow_stars + 1 holder.append(blue_stars) holder.append(yellow_stars) holder.append(red_stars) '''stick it in the value holder''' print '#---', holder values.append(holder) '''Average all the values in 'values' ''' hold_array = sc.array(values) holder = [] for i in range(10): holder.append(sc.mean(hold_array[:,i])) print holder data_out.append(holder) f.write_data(sc.array(data_out), 'new_output.txt') print '#---All Done!'
fig = plt.figure(i+1) plt.bar(histograms[i][1][:-1], histograms[i][0], 0.1) plt.bar(ref[1][:-1], ref[0], 0.1, fill=False, edgecolor='r', ls="dashed") plt.xlabel(r"[Fe/H]") plt.ylabel("counts") if ids != None: out = "{0} kpc, {1} stars,\n{2} of original".format(ids[i], t_stars, round( (float(t_stars)/float(o_stars)), 2 ) ) plt.text(0.0, 120.0, out, fontsize=12) plt.savefig(tag+"_"+str(ids[i])[:5]+".ps", papertype='letter') #plt.show() if __name__ == "__main__": d = sc.array([7.7, 10.0, 15.0, 20.0, 25.0, 30.0, 40.0]) #d = sc.arange(1.0, 82.0, 10.0) tag = "real_noFx" averages = 10 limits = (-4.0, 2.0) bins = (limits[1] - limits[0]) / 0.1 print "limits = {0}, bins = {1}, averages = {2}".format(limits, bins, averages) output = metal_con("NGC_6205_nofield.txt", d, 7.7, bins=bins, limits=limits, avgs=averages, detection=1, tag=tag) for i in range(len(output)): out = sc.zeros( (len(output[i][0]),2), float) out[:,0], out[:,1] = output[i][0], output[i][1][:-1] #print out.shape fi.write_data(out, "hist_"+tag+"_"+str(d[i])[:5]+".txt", header=tag+"_"+str(d[i])[:5]) #do background as well? """ Address distance, color cuts? Distances only make sense when convolving a cluster, need to modify for convolution of background.""" print "# --- Done"
start_params = sc.array([0.2, 0.1, sc.ma.max(y_col)]) steps = sc.array([0.001, 0.001, 0.01]) function = func.gaussian_function #Fit function MCMC_fit, errors, best_fit = mc.do_MCMC(function, start_params, steps, x_col, y_col, sig_col, name=('histfit_' + str(i)), number_steps=10000, save=0) best_fit = gd.gradient_descent(function, best_fit, x_col, y_col, sig_col) errors = he.get_hessian_errors(function, best_fit, x_col, y_col, sig_col, steps) turnoff.append([best_fit[0], best_fit[1], errors[0], errors[1]]) # if (pdf.plot_function(x_col, y_col, function, best_fit, save_name=('histfitplot_'+str(i)), # title=['', r'$(g-r)_0$', 'Normalized Counts'], save=1) == 1): plt.figure() plt.scatter(x_col, y_col) func_x = sc.arange((sc.ma.min(x_col) * 0.9), (sc.ma.max(x_col) * 1.1), 0.01) func_y = function(func_x, best_fit) plt.plot(func_x, func_y) plt.savefig(('histfitplot_' + str(i) + '.ps'), papertype='letter') print "#-Fit dataplot saved" #print turnoff print turnoff if f.write_data(sc.array(turnoff), 'turnoff_out_fixed.txt') == 1: print "#-All Results Successfully saved"
fit_array[(i-1),2] = (fit_array[(i-1),2] / float(len(star_list))) reject = 0 else: print 'kill length', len(kill_list), 'bin', (i) for j in range(len(kill_list)): star_list.pop(kill_list[j]) g_list.pop(kill_list[j]) for k in range(len(kill_list)): kill_list[k] = (kill_list[k]-1) #now have 'iso_array' and 'fit_array' - save to file! if (save_data==1): outname = cluster_name + '_fiducial_out.txt' dist_str = (str(distance)) header_txt = 'g, g-r, uncertainty mean, from file ' + data_file + ' with distance ' + dist_str #print out_data #print out_data.shape if (f.write_data(fit_array, outname, header=header_txt) == 1): print 'iso g, g-r, sigma, data successfully saved' #fit distances? Hard! Need to start from beginning...""" plt.figure() y = gc_array[:,0] x = gc_array[:,1] plt.scatter(x,y, c='black', marker='+', label='HR data') #plt.errorbar(x,y,xerr=fit_array[:,2]) plt.errorbar(fit_array[:,1],fit_array[:,0],xerr=fit_array[:,2], fmt=None, ecolor='g') plt.scatter(fit_array[:,1],fit_array[:,0], c='green', label='fiducial sequence') plt.title(plot_title, fontsize='small') plt.xlabel('g-r') plt.ylabel('Mg') leg = plt.legend(loc='best') for t in leg.get_texts(): t.set_fontsize('small')
import time '''python script for quickly creating test data sets. Matthew Newby, May 26, 2011''' out_file = "test_poly_out.txt" function = func.Nth_order_polynomial parameters = [11.0, -1.0, 0.5] x = sc.arange(-4.0, 4.0, 0.1) y = function(x, parameters) sig = 1.0 #1 sigma value for errors, takes: None or float out = sc.ones((len(x), 3)) out[:,0] = x if (sig == None): out[:,1] = y else: np.random.seed( int(time.time()) ) for i in range(len(y)): out[i,1] = np.random.normal(y[i], sig) out[i,2] = float(sig) if fi.write_data(out, out_file, header="#-x, y, sig"): print "#- File output succeeded" plt.figure(1) plt.scatter(out[:,0],out[:,1],2,'k','o') plt.show() plt.close('all') print "# - Done"
plt.close('all') return 1 if __name__ == '__main__': print '#- number of arguments:', (len(sys.argv)-1) # Read arguments filename = sys.argv[1] column = int(sys.argv[2]) size = float(sys.argv[3]) if len(sys.argv) > 4: spread = [float(sys.argv[4]), float(sys.argv[5])] else: spread = [] if len(sys.argv) > 6: name = sys.argv[6] else: name = 'quick' # Load Data suffix = filename[-4:] if suffix == '.txt': data = f.read_data(filename) elif suffix == '.csv': data = f.read_csv(filename) # Run scripts to_bin = data[:,column] reg_hist = make_hist(to_bin, size, spread) plot_histogram(reg_hist, size, name=(name+'_normal') ) f.write_data(reg_hist, fileout=(name+'_normal.txt'), header='# Centers, Counts') cum_hist = cumulative_hist(to_bin, size, spread) plot_histogram(cum_hist, size, name=(name+'_cumulative') ) f.write_data(cum_hist, fileout=(name+'_cumulative.txt'), header='# Centers, Counts') print '#done with quick histograms'
def sgr_xz_plot(folder="./sep_lbr/", bin_size=1.0, outfile=None, infile=None, primary=1, color=1, scale=1): x_min, x_max, x_size = -50.0, 50.0, bin_size z_min, z_max, z_size = 0.0, 100.0, bin_size x_bins, z_bins = int((x_max - x_min) / x_size) + 1, int( (z_max - z_min) / z_size) + 1 if infile == None: H = sc.zeros((z_bins, x_bins), float) files = glob.glob(folder + "/*.txt") for file in files: data = fi.read_data(file) wedge = int(file[-6:-4]) for i in range(len(data[:, 0])): if primary == 1: if test_primary(data[i, 0], data[i, 1], wedge, low=9, high=23) == 0: continue x, y, z = coor.lbr2xyz(data[i, 0], data[i, 1], data[i, 2]) if x < x_min or x > x_max: continue if z < z_min or z > z_max: continue #FLAG xi, zi = int((x - x_min) / x_size), int( (z - z_min) / z_size) #FLAG H[zi, xi] = H[zi, xi] + 1.0 if outfile != None: fi.write_data(H, outfile) else: H = fi.read_data(infile) if scale == 1: H = sc.sqrt(H) #Make the plot plt.figure(1) sp = plt.subplot(111) if color == 1: cmap = spectral_wb else: cmap = binary plt.imshow(H, interpolation='nearest', cmap=cmap) #vmin=0.0, vmax=15.0) bar = plt.colorbar(orientation='vertical') if scale == 1: bar_ticks = sc.arange(0.0, 17.0, 4.0) bar_labels = [] for i in range(len(bar_ticks)): bar_labels.append(str(int(bar_ticks[i]**2))) bar.set_ticks(bar_ticks) bar.set_ticklabels(bar_labels, update_ticks=True) Earth = [-8.5, 0.0, 0.0] plt.scatter((Earth[0] - x_min) / x_size, (Earth[2] - z_min) / z_size, c='yellow', s=5) # Clean up y_lim = plt.ylim() plt.ylim(y_lim[1], y_lim[0]) #plt.xlim(-25.0, 270.0) #plt.ylim(-25.0, 270.0) #plt.setp(sp.get_xticklabels(), visible=False) #plt.setp(sp.get_yticklabels(), visible=False) #plt.setp(sp.get_xticklines(), visible=False) #plt.setp(sp.get_yticklines(), visible=False) # Show plot plt.show() plt.close('all')
plt.text(-0.2, 16.0, "NGC 5024 - dirty", fontsize=8) plt.show() plt.close('all') """ """ for randomly selecting stars from the unmatched star file import astro_coordinates as coor data = fi.read_data("no_matches_82.txt", ",") samples = np.random.uniform(0, len(data[:,0]), 20) for i in range(len(samples)): l, b = data[samples[i],0], data[samples[i],1] ra, dec = coor.lbToEq(l,b) print float(ra), float(dec) """ ''' """ For double-sided Gaussian """ import matplotlib.pyplot as plt N1 = 642 #N2*(0.36/0.76) N2 = 1357 #2000.0 / ((0.36/0.76)+1) a1 = np.random.normal(4.2, 0.36, N1) a2 = np.random.normal(4.2, 0.76, N2) holder = [] for i in range(len(a1)): if a1[i] < 4.2: holder.append(a1[i]) for i in range(len(a2)): if a2[i] > 4.2: holder.append(a2[i]) x1 = sc.array(holder) #END OF DOBLE_GAUSSIAN
def lbpolar_plot(folder, hemi='N', bin_size=1.0, outfile=None, infile=None, color=1, scale=1, primary=1): """ Makes a polar plot centered at the north or south Galactic caps, using all files in a given folder. Files should be in l,b,r format. hemi: 'N' for Northern Galactic cap, 'S' for Southern Galactic cap bin_size: size of bins, in (roughly) degrees outfile: if not None, outputs the derived histogram to the file infile: if not None, reads in the histogram from the file color: 1 for spectral color map, 0 for black and white scale: 1 to sqrt bin counts, 0 to leave the bin counts primary: 1 to test wedges for primary stars (SDSS), 0 to skip test """ x_size, y_size, b_min = bin_size, bin_size, 30.0 b_min = 90.0 - b_min # Flips it around so that 90.0 is at the origin # Initialize custom histogram x_bins, y_bins = int(2.0 * b_min / x_size) + 1, int( 2.0 * b_min / y_size) + 1 if infile == None: H = sc.zeros((y_bins, x_bins), float) # Iterate over files files = glob.glob(folder + "/*.txt") for file in files: data = fi.read_data(file) wedge = int(file.split("/")[-1][6:8]) if hemi == 'S': data[:, 1] = -1.0 * data[:, 1] for i in range(len(data[:, 0])): if primary == 1: if test_primary(data[i, 0], data[i, 1], wedge, low=9, high=23) == 0: continue xi = int( ((90.0 - data[i, 1]) * sc.cos(data[i, 0] * rad) + b_min) / x_size) yi = int( ((90.0 - data[i, 1]) * sc.sin(data[i, 0] * rad) + b_min) / y_size) if xi < 0 or xi > x_bins - 1: continue if yi < 0 or yi > y_bins - 1: continue H[yi, xi] = H[yi, xi] + 1.0 if outfile != None: fi.write_data(H, outfile) else: H = fi.read_data(infile, delimiter=None) if scale == 1: H = sc.sqrt(H) # plot data plt.figure(1) sp = plt.subplot(111) if color == 1: cmap = spectral_wb else: cmap = binary plt.imshow(H, interpolation='nearest', cmap=cmap, vmin=0.0, vmax=12.0) #extent=extent, bar = plt.colorbar(orientation='vertical') if scale == 1: bar_ticks = sc.arange(0.0, 17.0, 4.0) bar_labels = [] #for i in range(len(bar_ticks)): # bar_labels.append(str(int(bar_ticks[i]**2))) #bar.set_ticks(bar_ticks) #bar.set_ticklabels(bar_labels, update_ticks=True) # plot axes - l for i in [30.0, 45.0, 60.0, 75.0]: l = sc.arange(0.0, 361.0, 1.0) x = ((90.0 - i) / (bin_size)) * sc.cos(l * rad) + (b_min / x_size) # FLAG x_size y = ((90.0 - i) / (bin_size)) * sc.sin(l * rad) + (b_min / y_size) # FLAG y_size if i % 2 == 0: plt.plot(x, y, 'k-') else: plt.plot(x, y, 'k:', linewidth=0.5) if hemi == 'S': plt.text(x[89] + 1.0, y[89] + 1.0, "$b=" + str(-1.0 * int(i)) + "^{\circ}$", horizontalalignment='left', verticalalignment='bottom', fontsize=12) else: plt.text(x[89] + 1.0, y[89] + 1.0, r"$b=" + str(int(i)) + r"^{\circ}$", horizontalalignment='left', verticalalignment='bottom', fontsize=12) # plot axes - b for i in sc.arange(0.0, 180.0, 15.0): b = sc.arange(-1.0 * (b_min + 5.0), (b_min + 5.0) + 1.0, 10.0) / bin_size # FLAG x = b * sc.cos(i * rad) + (b_min / x_size) # FLAG x_size y = b * sc.sin(i * rad) + (b_min / y_size) # FLAG y_size if i % 90 == 0: plt.plot(x, y, 'k-') else: plt.plot(x, y, 'k:') if i % 30 == 0: if i == 0.0: pre = r"$l=" else: pre = r"$" x0 = (b[0] - 4.0) * sc.cos(i * rad) + (b_min / x_size ) # FLAG x_size y0 = (b[0] - 4.0) * sc.sin(i * rad) + (b_min / y_size ) # FLAG y_size plt.text(x0, y0, r"$" + str(int(i + 180.0)) + r"^{\circ}$", horizontalalignment='center', verticalalignment='center', fontsize=12, rotation=(i + 90.0)) x1 = (b[-1] + 4.0) * sc.cos(i * rad) + (b_min / x_size ) # FLAG x_size y1 = (b[-1] + 4.0) * sc.sin(i * rad) + (b_min / y_size ) # FLAG y_size plt.text(x1, y1, pre + str(int(i)) + r"^{\circ}$", horizontalalignment='center', verticalalignment='center', fontsize=12, rotation=(i - 90.0)) # Clean up #y_lim = plt.ylim() #plt.ylim(y_lim[1], y_lim[0]) plt.xlim(-25.0, 270.0) plt.ylim(-25.0, 270.0) plt.setp(sp.get_xticklabels(), visible=False) plt.setp(sp.get_yticklabels(), visible=False) plt.setp(sp.get_xticklines(), visible=False) plt.setp(sp.get_yticklines(), visible=False) # Show plot plt.show() plt.close('all')
star_list.pop(kill_list[j]) g_list.pop(kill_list[j]) for k in range(len(kill_list)): kill_list[k] = (kill_list[k] - 1) #Fit strips to Girardi isocrones using different fit functions #now have 'iso_array' and 'fit_array' - save to file! if (save_data == 1): outname = cluster_name + '_out.txt' out_data = sc.zeros((iso_l, 4), float) out_data[:,0], out_data[:,1], out_data[:,2], out_data[:,3] = \ iso_array[:,0], iso_array[:,1], fit_array[:,1], fit_array[:,2] dist_str = (str(distance)) header_txt = 'g, iso g-r, fiducial g-r, uncertainty mean, from file ' + iso_file + ' with distance ' + dist_str #print out_data #print out_data.shape if (f.write_data(out_data, outname, header=header_txt) == 1): print 'iso g, iso g-r, fid g-r, fiducial sigma, data successfully saved' #fit distances? Hard! Need to start from beginning...""" plt.figure() A = -0.01463 B = 0.08929 y = iso_array[:, 0] x = (fit_array[:, 1] - iso_array[:, 1]) plt.scatter(x, y, c='black', marker='+', label='Subtracted data') plt.plot([0.0, 0.0], [np.ma.min(iso_array[:, 0]), np.ma.max(iso_array[:, 0])], 'r-', label='_nolegend_') plt.plot([(A * np.ma.min(y) + B), (A * np.ma.max(y) + B)], [np.ma.min(y), np.ma.max(y)], 'g:',
#plt.show() if __name__ == "__main__": d = sc.array([7.7, 10.0, 15.0, 20.0, 25.0, 30.0, 40.0]) #d = sc.arange(1.0, 82.0, 10.0) tag = "real_noFx" averages = 10 limits = (-4.0, 2.0) bins = (limits[1] - limits[0]) / 0.1 print "limits = {0}, bins = {1}, averages = {2}".format( limits, bins, averages) output = metal_con("NGC_6205_nofield.txt", d, 7.7, bins=bins, limits=limits, avgs=averages, detection=1, tag=tag) for i in range(len(output)): out = sc.zeros((len(output[i][0]), 2), float) out[:, 0], out[:, 1] = output[i][0], output[i][1][:-1] #print out.shape fi.write_data(out, "hist_" + tag + "_" + str(d[i])[:5] + ".txt", header=tag + "_" + str(d[i])[:5]) #do background as well? """ Address distance, color cuts? Distances only make sense when convolving a cluster, need to modify for convolution of background.""" print "# --- Done"