def vs_age_fits(bv, cluster_ages, rhk, scatter, metal, omit_cluster=None): cluster_ages_og, scatter_og = copy.deepcopy(cluster_ages), copy.deepcopy( scatter) if omit_cluster is not None and metal == 'calcium': del cluster_ages[omit_cluster] del rhk[omit_cluster] del scatter[omit_cluster] metal_fit = None mu_lbl = '' if (metal == 'calcium'): const = utils.init_constants('calcium') num_stars = np.array(const.CLUSTER_INDEX)[:, 1] - np.array( const.CLUSTER_INDEX)[:, 0] err = 1.0 / num_stars if omit_cluster is not None: err = np.delete(err, omit_cluster) metal_fit = constrained_poly_fit(np.log10(cluster_ages), rhk, lim=0, sigma=err) #metal_fit = poly_fit(np.log10(cluster_ages),rhk,2,weight=num_stars) mu_lbl = 'polynomial fit' elif (metal == 'lithium'): cluster_ages = np.log10(cluster_ages) bv_cut = [0.65, 1, 1.6] segs = [2, 3, 2, 1] s = segs[bisect.bisect_left(bv_cut, bv)] # hardcode fit to go through hyades during the lithium fit if 0.41 < bv < 0.51: const = utils.init_constants('lithium') scatter[const.CLUSTER_NAMES.index("Hyades") + 1] = 0.01 scatter = scatter[: -1] #del scatter[const.CLUSTER_NAMES.index("M67") + 1] cluster_ages = cluster_ages[: -1] #[const.CLUSTER_NAMES.index("M67") + 1] rhk = rhk[:-1] # [const.CLUSTER_NAMES.index("M67") + 1] #s += 1 metal_fit = general_piecewise(cluster_ages,rhk,s,\ n_pin=2,monotonic=-1,min_length=.2,sigma=scatter) def mu(age): return metal_fit(np.log10(age)) sig = None #find fit to mu of ca/li for grid #5 different methods for handling scatter: # gaussian fit,total detrended mean, mean clusters, best-fit, linear interp if (metal == 'calcium'): sig = fit_gaussian(np.log10(cluster_ages_og), scatter_og) elif (metal == 'lithium'): sig = np.poly1d([np.mean(scatter)]) #placeholder--isn't used return mu, sig, mu_lbl
def cluster_scatter_from_stars(bv_m, fits): const = utils.init_constants('lithium') bv_threshold = 0.03 arr = [] for c in range(len(fits)): num_stars = [] bv_arr = np.array(bv_m[c][0]) for bv in const.BV: mask = (bv_arr >= bv - bv_threshold) & (bv_arr <= bv + bv_threshold) num_stars.append(np.sum(mask)) arr.append(num_stars) arr = np.array(arr) arr = arr / (np.sum(arr, axis=0) + .001) #arr holds % of stars each cluster has at each B-V slice arr = (1 - arr) * .35 + 0.05 for c in range(len(arr)): max_bv, min_bv = max(bv_m[c][0]), min(bv_m[c][0]) dist = np.minimum(np.abs(const.BV - max_bv), np.abs(const.BV - min_bv)) dist = dist * ((const.BV > max_bv) | (const.BV < min_bv)) range_offset = (0.4 / 0.2) * dist arr[c] += range_offset arr = savgol_filter(arr, 51, 3) for c in range(len(fits)): fits[c][1] = piecewise(const.BV, arr[c]) return fits
def make_picklable(fits): const = utils.init_constants('lithium') fits = copy.deepcopy(fits) for c,i in [(c,i) for c in range(len(fits)) for i in range(2)]: if type(fits[c][i]) != type(np.poly1d([1])): fits[c][i] = fits[c][i](const.BV) return fits
def get_CI_hyades_no_ul(): const = utils.init_constants(METAL) baf_default = baffles.age_estimator(METAL) mask = (~np.array(upper_lim[8])) | (bv_m[8][0] > 0.55) print("Total stars:",len(bv_m[8][0])) print("Stars selsected=:",np.sum(mask)) b,l,ul = bv_m[8][0][mask],bv_m[8][1][mask],np.array(upper_lim[8])[mask] baf_default.posterior_product(b,l,upperLim_arr=ul,showPlot=True,showStars=True,title='Hyades B-V > .55',givenAge=700)
def get_fit_residuals(bv_m, fits, metal, upper_limits=None, li_range=None, age_range=None, linSpace=False, scale_by_std=False, vs_age_fit=True, zero_center=True): const = utils.init_constants(metal) allClusters = [] grid_median = np.load(const.DEFAULT_MEDIAN_GRID) mu_interp = interpolate.interp2d(const.AGE,const.BV_S,grid_median) if metal == 'lithium' else \ interpolate.interp1d(const.AGE,grid_median) for c in range(len(fits)): if age_range is not None and not (age_range[0] <= const.CLUSTER_AGES[c]\ <= age_range[1]): allClusters.append([]) continue arr = [] #holds non UL from cluster i resid = None if vs_age_fit: resid = None if metal == 'lithium': resid = np.array(bv_m[c][1]) - mu_interp( const.CLUSTER_AGES[c], bv_m[c][0]).flatten() else: resid = np.array(bv_m[c][1]) - mu_interp(const.CLUSTER_AGES[c]) elif linSpace: resid = np.power(10, bv_m[c][1]) - np.power( 10, fits[c][0](bv_m[c][0])) else: #log space resid = residuals(bv_m[c][0], bv_m[c][1], fits[c][0]) #Log space #now filter out upper-limits from resid for i in range(len(resid)): if (upper_limits is not None and upper_limits[c][i]): continue if (li_range is not None and (bv_m[c][1][i] < li_range[0] or \ li_range[1] < bv_m[c][1][i])): continue arr.append(resid[i]) if scale_by_std: arr = np.array(arr) / np.std(arr) allClusters.append(arr) residual_arr = np.concatenate(allClusters) if zero_center: #print("Subtracting off median of %.5f from residuals, (mean = %.5f)" % (np.median(residual_arr),np.mean(residual_arr))) resid_mean = np.median(residual_arr) residual_arr -= resid_mean for i in range(len(allClusters)): allClusters[i] = np.array(allClusters[i]) - resid_mean return allClusters, residual_arr
def get_CI(): const = utils.init_constants(METAL) baf_default = baffles.age_estimator(METAL) for index,i in enumerate([1,2,7,9]): print("\n", const.CLUSTER_NAMES[i]) bv_arr = bv_m[i][0] p = baf_default.posterior_product(bv_m[i][0],bv_m[i][1],upperLim_arr=upper_lim[i],showStars=True) age,y = const.AGE,p.array givenAge=const.CLUSTER_AGES[i] print('Isochronal age exists within %f %% CI' % prob.get_percentile(age,y,givenAge))
def __init__(self, metal, grid_median=None, default_grids=True, load_pdf_fit=True): self.metal = metal self.grid_median = None self.const = utils.init_constants(metal) if (grid_median): self.set_grids(grid_median) elif (default_grids): self.set_grids(self.const.DEFAULT_MEDIAN_GRID) if load_pdf_fit: #allows refresh.py to make without needing these self.pdf_fit, self.cdf_fit = my_fits.fit_histogram(metal, fromFile=True)
def li_dip_fit(bv, li, upper_lim, dip_bv_range, dip_li_range, dip_fit_range, edge_box): const = utils.init_constants('lithium') # separate out dip stars from non dip bv, li, upper_lim = np.array(bv), np.array(li), np.array(upper_lim) dip_mask = (bv <= dip_bv_range[1]) & (bv >= dip_bv_range[0]) & \ (li <= dip_li_range[1]) & (li >= dip_li_range[0]) mask = np.invert(dip_mask) dip_mask = (dip_mask) | ((bv <= dip_fit_range[1]) & (bv >= dip_fit_range[0])) bv_dip, li_dip, upper_lim_dip = bv[dip_mask], li[dip_mask], upper_lim[ dip_mask] bv_, li_, upper_lim_ = bv[mask], li[mask], upper_lim[mask] no_dip_fit, no_dip_sig_fit = poly_fit(bv_, li_, 2, upper_lim_) edge_mask = (bv_dip > edge_box[0]) & (bv_dip < edge_box[1]) & \ (li_dip > edge_box[2]) & (li_dip < edge_box[3]) sigma = np.where(edge_mask, .02, 1) # makes fit go through edge_box points def dip_poly(x, a, b, c): return a * x**2 + b * x + c popt, pcov = curve_fit(dip_poly, bv_dip, li_dip, sigma=sigma) #,p0=[100,-90,21.25]) #print(popt) #popt,pcov = curve_fit(dip_gaussian,bv_dip,li_dip,p0=[0.45,.04,-.1],sigma=sigma) dip_fit = lambda x: dip_poly(np.array(x), *popt) #dip_fit = lambda x: dip_gaussian(np.array(x),*popt) dip_sig_fit = np.poly1d([.25]) final_fit = lambda x : np.where((np.array(x) <= dip_bv_range[1]) & \ (np.array(x) >= dip_bv_range[0]),dip_fit(x),no_dip_fit(x)) final_sig_fit = lambda x: np.where((np.array(x) <= dip_bv_range[1]) & \ (np.array(x) >= dip_bv_range[0]),dip_sig_fit(x),no_dip_sig_fit(x)) return [final_fit, final_sig_fit]
def main(): date = datetime.datetime.now().strftime("%m%d%y") bv_m,fits = readData.read_calcium(fromFile=False,saveToFile=True) baf = baffles.age_estimator('calcium',default_grids=False,load_pdf_fit=False) baf.make_grids(bv_m,fits,medianSavefile=join('grids','median_rhk_'+date),\ setAsDefaults=True) _,res_arr = my_fits.get_fit_residuals(bv_m,fits,'calcium',None,li_range=None, linSpace=False,scale_by_std= False,vs_age_fit=True,zero_center=True) my_fits.fit_histogram('calcium',residual_arr=res_arr,fromFile=False,saveToFile=True) const = utils.init_constants('lithium') bv_m, upper_lim, fits = readData.read_lithium(fromFile=False,saveToFile=True) baf2 = baffles.age_estimator('lithium',default_grids=False,load_pdf_fit=False) baf2.make_grids(bv_m,fits,upper_lim,join('grids','median_li_'+date),setAsDefaults=True) my_fits.MIST_primordial_li(ngc2264_fit=fits[const.CLUSTER_NAMES.index('NGC2264')][0], fromFile=False, saveToFile=True) _,res_arr= my_fits.get_fit_residuals(bv_m,fits,'lithium',upper_lim,li_range=None,linSpace=False, vs_age_fit=True,zero_center=True) my_fits.fit_histogram('lithium',residual_arr=res_arr,fromFile=False,saveToFile=True)
def main(): bv_m, fits = readData.read_calcium(fromFile=False, saveToFile=True) _, res_arr = my_fits.get_fit_residuals(bv_m, fits, 'calcium', None, li_range=None, linSpace=False, scale_by_std=False, vs_age_fit=True, zero_center=True) my_fits.fit_histogram('calcium', residual_arr=res_arr, fromFile=False, saveToFile=True) const = utils.init_constants('lithium') bv_m, upper_lim, fits = readData.read_lithium(fromFile=False, saveToFile=True) my_fits.MIST_primordial_li( ngc2264_fit=fits[const.CLUSTER_NAMES.index('NGC2264')][0], fromFile=False, saveToFile=True) _, res_arr = my_fits.get_fit_residuals(bv_m, fits, 'lithium', upper_lim, li_range=None, linSpace=False, vs_age_fit=True, zero_center=True) my_fits.fit_histogram('lithium', residual_arr=res_arr, fromFile=False, saveToFile=True)
def combined_validation_subplots(): const = utils.init_constants(METAL) #Omitting each cluster name = join('plots', METAL + '_combined_validation_subplots.pdf') pp = PdfPages(name) baf_default = baffles.age_estimator(METAL) fig, ax = plt.subplots(3, 2, figsize=(14, 15)) cmap = plt.cm.get_cmap('RdYlBu_r') norm = mpl.colors.Normalize(vmin=const.BV_RANGE[0], vmax=const.BV_RANGE[1]) sc = plt.scatter([], [], c=[], norm=norm, cmap=cmap) fig.tight_layout(pad=.4, w_pad=1, h_pad=2) fig.subplots_adjust(left=0.06) fig.subplots_adjust(bottom=0.06) fig.subplots_adjust(top=.95) fig.subplots_adjust(right=0.9) #cbar_ax = fig.add_axes([0.85, 0.15, 0.05, 0.7]) cbar_ax = fig.add_axes([0.92, 0.25, 0.02, 0.5]) fig.colorbar(sc, cax=cbar_ax) for index, i in enumerate([1, 3, 5, 6, 7, 8]): #[0,4,5,6,7,8] for submission 2! print(const.CLUSTER_NAMES[i]) baf = baffles.age_estimator(METAL, default_grids=False) baf.make_grids(bv_m, fits, omit_cluster=i) p_val = baf.posterior_product(bv_m[i][0], bv_m[i][1]) pl = ax[int(index / 2), index % 2] pl.plot(const.AGE, p_val.array, linewidth=2, linestyle='--', label='Posterior with removal') pl.set_title(const.CLUSTER_NAMES[i], size=my_plot.TITLE_SIZE) bv_arr = bv_m[i][0] p = baf_default.posterior_product(bv_m[i][0], bv_m[i][1], upperLim_arr=None, showStars=True) #print(p.stars_posteriors) age, y = const.AGE, p.array givenAge = const.CLUSTER_AGES[i] for star, post in enumerate(p.stars_posteriors): color = cmap(norm(bv_arr[star])) prob.scale_to_height(post, np.max(y)) pl.plot(const.AGE, post, alpha=1, linewidth=1, color=color, zorder=0) if (givenAge): print('Isochronal age exists within %f %% CI' % prob.get_percentile(age, y, givenAge)) pl.axvline(x=givenAge, color='r', label='Isochronal age: %d Myr' % givenAge) #if (givenErr): # if (type(givenErr) == float or type(givenErr) == int): # givenErr = [-1*givenErr,givenErr] # pl.axvspan(givenAge+givenErr[0], givenAge+givenErr[1], alpha=0.2, color='r',zorder=0) #if (mamajekAge): # plt.axvline(x=mamajekAge,color='C2',label='MH08 age: %d' % mamajekAge) pl.plot(age, y, color='C0', linewidth=2) stat = p.stats age2 = np.linspace(stat[0], stat[-1], 500) interp = my_fits.piecewise(age, y) y2 = interp(age2) pl.vlines(x=stat[2],ymin= 0,ymax= interp(stat[2]), \ label='BAFFLES median age: %.3g Myr' % stat[2] ,color = 'orange') pl.fill_between(age2,y2, where= (age2 >= stat[1]) & (age2 <= stat[3]),color='.3', \ label='68%% CI: %.2g - %.2g' % (stat[1],stat[-2])) pl.fill_between(age2,y2, where= (age2 >= stat[0]) & (age2 <= stat[-1]),color='.6',\ alpha=0.5, label='95%% CI: %.2g - %.2g' % (stat[0],stat[-1])) pl.set_ylim([0, np.max(y) * 1.5]) r = my_plot.getAgeRange(p.stats, p.stars_posteriors, givenAge) pl.set_xlim(r) pl.legend() pl.minorticks_on() pl.tick_params(axis='both', which='both', right=True, top=True) # Set common labels fig.text(0.5, 0.02, 'Age (Myr)', size=my_plot.AXIS_LABEL_SIZE, ha='center', va='center') fig.text(0.01, 0.5, 'Probability Density (Myr^-1)', size=my_plot.AXIS_LABEL_SIZE, ha='center', va='center', rotation='vertical') fig.text(0.99, 0.5, 'B-V', size=my_plot.AXIS_LABEL_SIZE, ha='center', va='center', rotation='vertical') pp.savefig() plt.close() printName(name) pp.close()
def notable_stars(): li_const = utils.init_constants('lithium') name = join('plots','notable_stars.pdf') names = ["HR 2562","HD 206893","TW PsA"] bv = [.45,.44,1.1] bv_err = [np.sqrt(.014**2+.01**2),np.sqrt(.02**2+.01**2),.03] # .03 b-v error for TW PsA? rhk = [-4.55,-4.466,None] mamaAge = [utils.getMamaAge(rhk[0]),utils.getMamaAge(rhk[1]),None] li = [21,28.5,33] li_err = [5,7,2] markers = ['*','*','*'] markerSize = 25 colors = ['gold','green','darkmagenta'] age = [None,None,440] age_range = [[300,900],[200,2100],[400,480]] pp=PdfPages(name) my_plot.metal_vs_bv(bv_ca,ca_fits,'calcium',None,False,specific_clusters=[0,5,7,8], legend=False,textlabels=True) plt.plot(bv,rhk,marker='s',markersize=markerSize,color='w',linestyle='None',zorder=9) for i in [0,1]: plt.plot(bv[i],rhk[i],marker=markers[i],markersize=markerSize,color=colors[i], linestyle='None',zorder=10,label=names[i]) plt.legend() plt.xlim([.42,.9]) pp.savefig() #plt.show() plt.close() my_plot.metal_vs_bv(bv_m,fits,'lithium',None,False,upper_lim=upper_lim,specific_clusters=[0,1,4,6,8,9]) plt.plot(bv,li,marker='s',markersize=markerSize,color='w',linestyle='None',zorder=9) for i in [0,1,2]: plt.plot(bv[i],np.log10(li[i]),marker=markers[i],markersize=markerSize,color=colors[i], linestyle='None',zorder=10,label=names[i]) plt.legend() pp.savefig() #plt.show() plt.close() baf_ca = baffles.age_estimator('calcium') baf_li = baffles.age_estimator('lithium') for i in [0,1]: print(names[i]) plt.plot([],[],'C0',linewidth=2,label='Final Age Posterior') p_li = baf_li.get_posterior(bv[i],li[i],bv_uncertainty=bv_err[i],measure_err=li_err[i],upperLim=False) p_ca = baf_ca.get_posterior(None,rhk[i]) product = prob.normalize(const.AGE,p_ca.array*p_li.array) prod_stats=prob.stats(const.AGE,product) my_plot.posterior(const.AGE, product, prod_stats,names[i],logPlot=False) plt.plot(const.AGE, p_ca.array,color='C3',label="Calcium Posterior") plt.plot(const.AGE, p_li.array,color='C2',label="Lithium Posterior") plt.axvspan(age_range[i][0],age_range[i][1], alpha=0.2, color='r', label=r'Literature age: %d - %d Myr' % tuple(age_range[i]),zorder=0) plt.axvline(x=mamaAge[i],color='C5',linestyle='--',label='MH08 age: %d' % mamaAge[i]) #plt.xlim([0,490]) plt.legend() pp.savefig() #plt.show() plt.close() print("%d Myr (68\\%%CI: %d - %d Myr)" % (utils.round_sigs(p_ca.stats[2],2), utils.round_sigs(p_ca.stats[1],2),utils.round_sigs(p_ca.stats[3],2))) print("%.1f Gyr (68\\%%CI: %.1f - %.1f Gyr)" % (p_li.stats[2]/1000,p_li.stats[1]/1000,p_li.stats[3]/1000)) print("%d Myr, with a 68\\%% confidence interval between %d Myr - %d Myr" % (utils.round_sigs(prod_stats[2],2), utils.round_sigs(prod_stats[1],2),utils.round_sigs(prod_stats[3],2))) print("TW PsA") plt.axvspan(age_range[-1][0],age_range[-1][1], alpha=0.2, color='r',zorder = 0) plt.axvspan(360-140,360+140, alpha=0.2, color='b',zorder=0) p_li = baf_li.get_posterior(bv[2],li[2],bv_uncertainty=bv_err[2],measure_err=li_err[2],upperLim=False) print("we report an age of %d Myr with a 68\\%% confidence interval between %d Myr - %d Myr\ (third panel of Fig. \\ref{fig:notable_stars}), consistent with Mamajek's lithium age,\ but a factor of $\sim$%.1f too young for his final adopted age." % (p_li.stats[2],p_li.stats[1],p_li.stats[3], 440/p_li.stats[2])) my_plot.posterior(const.AGE, p_li.array, p_li.stats,names[2],None,False, logPlot=False) plt.axvline(x=age[-1],color='r',label=r'Literature age: %d $\pm$ 40 Myr' % age[-1]) plt.axvline(x=360,color='b',label=r"M'12 Li age: 360 $\pm$ 140 Myr") plt.xlim([-30,510]) plt.legend() pp.savefig() #plt.show() plt.close() plt.axvline(x=age[-1],color='r',label=r'Literature age: %d $\pm$ 40 Myr' % age[-1]) plt.axvspan(age_range[-1][0],age_range[-1][1], alpha=0.2, color='r',zorder = 0) robs_f = robs_fomalhaut() plt.plot(const.AGE,robs_f(const.AGE),'k--',label='Nielsen 2019 Fomalhaut PDF') plt.plot(const.AGE,p_li.array,'g',label='BAFFLES Li posterior') y = prob.normalize(const.AGE,p_li.array*robs_f(const.AGE)) plt.plot(const.AGE,y,color = 'C0',linewidth=2,label='Final Age') stat = prob.stats(const.AGE,y) print("to get a final age for the system, $%d^{+%d}_{%d}$ Myr." % (stat[2],stat[3]-stat[2],stat[1]-stat[2])) plt.vlines(x=stat[2],ymin= 0,ymax= y[bisect.bisect_left(const.AGE,stat[2])], \ label='Final median age: %.3g Myr' % stat[2] ,color = 'orange') plt.fill_between(const.AGE,y, where= (const.AGE >= stat[1]) & (const.AGE <= stat[3]),color='.4', \ label='68%% CI: %.2g - %.2g' % (stat[1],stat[-2])) plt.title(names[2],size=my_plot.TITLE_SIZE) plt.xlim([0,1200]) plt.legend() plt.ylabel('Probability Density (Myr^-1)',size=my_plot.AXIS_LABEL_SIZE) plt.xlabel("Age (Myr)",size=my_plot.AXIS_LABEL_SIZE) plt.tight_layout() plt.minorticks_on() plt.tick_params(axis='both',which='both',right=True,top=True) pp.savefig() #plt.show() plt.close() printName(name) pp.close()
#changes constant file to have these names as default grid names def set_default_grids(self, grid_median): filename = 'ca_constants.py' if (self.metal == 'lithium'): filename = 'li_constants.py' lines = open(filename, 'r').readlines() for l in range(len(lines)): if (lines[l][:14] == 'DEFAULT_MEDIAN'): lines[l] = 'DEFAULT_MEDIAN_GRID = "' + grid_median + '.npy"\n' out = open(filename, 'w') out.writelines(lines) out.close() if __name__ == "__main__": const = utils.init_constants('lithium') err = "Usage: python baffles.py -bmv <B-V> -rhk <Log10(R\'HK)> -li <EWLi> [-bmv_err <> -li_err <> -ul -maxAge <13000> -plot -s -savePlot -filename <> -help]" help_msg = "\n\ -bmv corrected (B-V)o of the star (optional for calcium)\n\ -rhk <> log base 10 of the R'HK value \n\ -li <> EW measure in milli-angstroms (=0.1 pm) \n\ \noptional flags:\n\ -ul indicates that the log(EW/mA) reading is an upper-limit reading. \n\ -maxAge allows user to input max posible age of star (Myr) if upper-limit flag is used. default is %d \n\ -bmv_err <float uncertainity> provides the uncertainty on B-V with default %.2f \n\ -li_err <float uncertainity> provides the uncertainty in LiEW measurement with default %dmA \n\ -s saves posteriors as .csv as age, probability in two 1000 element columns.\n\ -plot plots and shows the PDF. \n\ -savePlot saves the plotted posteriors to a pdf. \n\ -filename <name of file w/o extension> name of files to be saved: name.pdf is graphs, name_calcium.csv/name_lithium.csv/name_product.csv are posterior csv files for calcium/lithium/product respectively. \n\
def fit_histogram(metal, residual_arr=None, fromFile=True, saveToFile=False): if fromFile: [x, pdf, cdf] = np.load(join('grids', metal + '_likelihood_fit.npy')) return piecewise(x, pdf), piecewise(x, cdf) const = utils.init_constants(metal) assert residual_arr is not None or fromFile, "Must provide residuals if not \ reading from file" mu = np.mean(residual_arr) sigma = np.std(residual_arr) x = np.linspace( np.min(residual_arr) - .5, np.max(residual_arr) + .5, 1000) #1000 for linear? lim = 2 if metal == 'calcium': #lim = 5 lim = 1 x = np.linspace( np.min(residual_arr) - .5, np.max(residual_arr) + .1, 800) before, after = np.linspace(-lim, min(x), 50), np.linspace(max(x), lim, 50) x = np.concatenate((before, x, after)) cdf = np.array([(residual_arr < n).sum() for n in x], dtype='float') cdf /= cdf[-1] if metal == 'calcium': smoothed = savgol_filter(cdf, 145, 3) smoothed = savgol_filter(smoothed, 55, 3) else: smoothed = savgol_filter(cdf, 85, 3) smoothed = savgol_filter(smoothed, 55, 3) #smoothed = savgol_filter(cdf, 55, 3) #smoothed = savgol_filter(smoothed, 25, 3) #smoothed = savgol_filter(smoothed, 9, 3) pdf = np.gradient(smoothed) prob.normalize(x, pdf) inds = np.nonzero(pdf > max(pdf) / 2)[0] #inds = np.nonzero(pdf > 1.5)[0] if metal=='lithium' else \ # np.nonzero(pdf > max(pdf)/2)[0] i, j = inds[0], inds[-1] def exp_fit(x, a, b, c): return a * np.exp(b * x + c) popt, pcov = curve_fit(exp_fit, x[:i], pdf[:i], p0=[5, 5, -1]) pdf[:i] = exp_fit(x[:i], *popt) popt, pcov = curve_fit(exp_fit, x[j:], pdf[j:], p0=[.5, -9, 2.5]) pdf[j:] = exp_fit(x[j:], *popt) pdf = savgol_filter(pdf, 9, 3) if metal == 'calcium': m, n = np.nonzero(pdf >= 0.32)[0][0], np.nonzero(pdf >= 0.45)[0][0] pdf[m:n] = piecewise([x[m], x[n]], [pdf[m], pdf[n]])(x[m:n]) pdf = savgol_filter(pdf, 21, 3) pdf[:2] = [0, 0] pdf[-2:] = [0, 0] prob.normalize(x, pdf) cdf = integrate.cumtrapz(pdf, x=x, initial=0) cdf /= cdf[-1] if saveToFile: np.save(join('grids', metal + '_likelihood_fit'), [x, pdf, cdf]) return piecewise(x, pdf), piecewise(x, cdf)
def undo_picklable(fits): const = utils.init_constants('lithium') for c,i in [(c,i) for c in range(len(fits)) for i in range(2)]: if type(fits[c][i]) != type(np.poly1d([1])): fits[c][i] = my_fits.piecewise(const.BV,fits[c][i]) return fits
def make_table(MR=False): ca_const = utils.init_constants('calcium') li_const = utils.init_constants('lithium') empty = '' """ table = [] #[Object,RA,Dec,Sp Type,B-V,R'HK,Li EW,Source] #first read in all the 4 tables and create a single big table, which then I sort and merge t = np.genfromtxt('data/nielsen_2010_table2.csv',delimiter=',',dtype=str,skip_header=1) for row in t: if not utils.isFloat(row[1]) or not (.45 <= float(row[1]) <= 1.9): continue arr = [] arr.append(row[21].strip()) ra,dec = ra_dec(row[22]) arr.append(ra) arr.append(dec) arr.append(row[4].strip()) arr.append(row[1]) arr.append(row[13]) arr.append(row[7]) arr.append("1") if arr[0] == '' or not (utils.isFloat(arr[5]) or utils.isFloat(arr[6])): continue table.append(arr) bv_to_teff = my_fits.magic_table_convert('bv','teff') t = np.genfromtxt('data/brandt_2014_table.csv',delimiter=',',dtype=str,skip_header=2) for row in t: bv = None if utils.isFloat(row[2]) and utils.isFloat(row[3]): bv = float(row[2]) - float(row[3]) if bv is None or not (.45 <= bv <= 1.9): continue arr = [] arr.append(row[14].strip()) ra,dec = ra_dec(row[15]) arr.append(ra) arr.append(dec) arr.append(row[4].strip()) arr.append("%f" % bv) arr.append(row[7]) if row[9].find('A') != -1: nli = float(row[9].split()[-1]) teff = bv_to_teff(bv) ew = 10** my_fits.teff_nli_to_li([teff],[nli])[0] arr.append("%d" % ew) elif utils.isFloat(row[9]): arr.append(row[9]) else: arr.append(empty) arr.append("2") if arr[0] == '' or not (utils.isFloat(arr[5]) or utils.isFloat(arr[6])): continue table.append(arr) t = np.genfromtxt("data/nearbyStars_Boro_Saikia_2018.txt",delimiter='\t',dtype=str,skip_header=58) for row in t: if not utils.isFloat(row[5]) or not (.45 <= float(row[5]) <= 1.9): continue arr = [] arr.append(row[16].strip()) ra,dec = ra_dec(row[17]) arr.append(ra) arr.append(dec) arr.append(row[18].strip()) arr.append(row[5]) arr.append(row[10]) arr.append(empty) arr.append("3") if arr[0] == '' or not (utils.isFloat(arr[5]) or utils.isFloat(arr[6])): continue table.append(arr) t = np.genfromtxt("data/guillot_2009_li_survey.txt",delimiter='\t',dtype=str,skip_header=77) for row in t: if not utils.isFloat(row[7]) or not (.45 <= float(row[7]) <= 1.9): continue arr = [] arr.append(row[22].strip()) ra,dec = ra_dec(row[23]) arr.append(ra) arr.append(dec) arr.append(row[24].strip()) arr.append(row[7]) arr.append(empty) arr.append(row[16]) arr.append("4") if arr[0] == '' or not (utils.isFloat(arr[5]) or utils.isFloat(arr[6])): continue table.append(arr) table = np.array(table) name_sorted = table[table[:,0].argsort()] thinned = [] #averaging b-v,measurements, sources as 1,4 for name in set(name_sorted[:,0]): subset = name_sorted[name_sorted[:,0]==name] if len(subset) == 1: thinned.append(subset[0]) else: arr = copy.deepcopy(subset[0]) arr[4] = average(subset[:,4]) arr[5] = average(subset[:,5]) arr[6] = average(subset[:,6]) x = list(set(subset[:,7])) x.sort() arr[7] = ','.join(x) thinned.append(arr) thinned = np.array(thinned) final_table = thinned[thinned[:,1].argsort()] np.save("final_table",final_table) exit() """ final_table = np.load("data/merged_nielsen_brandt_saikia_guillot.npy") delimiterMR = ',' baf_li = baffles.age_estimator('lithium') baf_ca = baffles.age_estimator('calcium') #[Object,RA,Dec,Sp Type,B-V,R'HK,Li EW,Source] f = open("baffles_table2_latex.txt", 'w+') fMR = open("baffles_table2.csv", 'w+') cdf = ['2.5%', '16%', '50%', '84%', '97.5%'] column_head = [ 'Name', 'RA', 'Dec', 'Sp. Type', 'B-V', "logR'HK", 'Li EW', 'Ref.' ] column_head += ["R'HK Age at CDF=" + x for x in cdf] column_head += ["Li EW Age at CDF=" + x for x in cdf] column_head += ["Final Age at CDF=" + x for x in cdf] units = [ '', 'h m s', 'h m s', '', 'mags', " ", 'mA', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '' ] fMR.write(delimiterMR.join(column_head)) fMR.write('\n') fMR.write(delimiterMR.join(units)) fMR.write('\n') for row in final_table: arr = [] arrMR = [] arr += [x.replace('V* ', '').replace('_', '-') for x in row[0:4]] arrMR += [x.replace('$', '').replace('V* ', '') for x in row[0:4]] bv = float(row[4]) arr.append("%.2f" % bv) arrMR.append("%.3g" % bv) p_ca, p_li = None, None if utils.isFloat(row[5]): rhk = float(row[5]) arr.append('$%.2f$' % rhk) arrMR.append('%.3f' % rhk) if ca_const.inRange(bv, rhk): p_ca = baf_ca.get_posterior(bv, rhk, showPlot=False) else: arr.append(empty) arrMR.append(empty) ew = None if utils.isFloat(row[6]): ew = float(row[6]) arr.append('%d' % ew) arrMR.append('%g' % ew) else: arr.append(empty) arrMR.append(empty) arr.append(row[7]) arrMR.append(row[7].replace(',', ';')) if bv is not None and ew is not None and ew > 0 and li_const.inRange( bv, np.log10(ew)): p_li = baf_li.get_posterior(bv, ew, showPlot=False) if p_ca is not None: arr += printStats(p_ca.stats) arrMR += printStats(p_ca.stats, MR=True) else: arr += [empty] * 5 arrMR += [empty] * 5 if p_li is not None: arr += printStats(p_li.stats) arrMR += printStats(p_li.stats, MR=True) else: arr += [empty] * 5 arrMR += [empty] * 5 if p_ca is None and p_li is None: continue if p_ca is not None and p_li is not None: prod = p_ca.array * p_li.array prob.normalize(ca_const.AGE, prod) stats = prob.stats(ca_const.AGE, prod) arr += printStats(stats) arrMR += printStats(stats, MR=True) elif p_ca is not None: arr += printStats(p_ca.stats) arrMR += printStats(p_ca.stats, MR=True) elif p_li is not None: arr += printStats(p_li.stats) arrMR += printStats(p_li.stats, MR=True) else: arr += [empty] * 5 arrMR += [empty] * 5 f.write(' & '.join(arr) + " \\\\") f.write('\n') fMR.write(delimiterMR.join(arrMR)) fMR.write('\n') f.close() fMR.close()