def contours(table, params): extractor = DBExtractor(table) for par_limit, contour_param in zip(["gamma", "rv"], ["rv", "gamma"]): fixed_params = {"teff_1": params["teff"], "logg_1": params["logg"], "feh_1": params["fe_h"], "logg_2": params["logg"], "feh_2": params["fe_h"]} for chi2_val, npix_val in zip(chi2_names, npix_names): df_min_chi2 = extractor.minimum_value_of(chi2_val) fixed_params.update({par_limit: float(df_min_chi2[par_limit][0])}) df = extractor.fixed_extraction(columns=["teff_2", "rv", "gamma", chi2_val], fixed=fixed_params) params["this_npix"] = params["npix"][npix_val] params["par_limit"] = par_limit pars = [contour_param, "teff_2", chi2_val] dataframe_contour(df, xcol=pars[0], ycol=pars[1], zcol=pars[2], params=params) # Using Reduced chi2 value for par_limit, contour_param in zip(["gamma", "rv"], ["rv", "gamma"]): fixed_params = {"teff_1": params["teff"], "logg_1": params["logg"], "feh_1": params["fe_h"], "logg_2": params["logg"], "feh_2": params["fe_h"]} for chi2_val, npix_val in zip(chi2_names, npix_names): red_chi2 = "red_{0}".format(chi2_val) df_min_chi2 = extractor.minimum_value_of(chi2_val) fixed_params.update({par_limit: float(df_min_chi2[par_limit][0])}) df = extractor.fixed_extraction(columns=["teff_2", "rv", "gamma", chi2_val], fixed=fixed_params) df[red_chi2] = reduced_chi_squared(df[chi2_val], params["npix"][npix_val], params["npars"]) params["this_npix"] = params["npix"][npix_val] params["par_limit"] = par_limit pars = [contour_param, "teff_2", red_chi2] dataframe_contour(df, xcol=pars[0], ycol=pars[1], zcol=pars[2], params=params) # Using teff_1 contour for par_limit, contour_param in zip(["gamma", "rv"], ["rv", "gamma"]): fixed_params = {"logg_1": params["logg"], "feh_1": params["fe_h"], "logg_2": params["logg"], "feh_2": params["fe_h"]} for chi2_val, npix_val in zip(chi2_names, npix_names): df_min_chi2 = extractor.minimum_value_of(chi2_val) fixed_params.update({par_limit: df_min_chi2[par_limit].values[0], "teff_2": df_min_chi2["teff_2"].values[0]}) df = extractor.fixed_extraction(columns=["teff_1", "rv", "gamma", chi2_val], fixed=fixed_params) params["this_npix"] = params["npix"][npix_val] params["par_limit"] = par_limit pars = [contour_param, "teff_1", chi2_val] dataframe_contour(df, xcol=pars[0], ycol=pars[1], zcol=pars[2], params=params)
def contrast_iam_results(table, params): extractor = DBExtractor(table) star_name = params["star"] obsnum = params["obsnum"] ___, host_params, ___ = iam_helper_function(star_name, obsnum, 1) h_temp, h_logg, h_feh = host_params['temp'], host_params['logg'], host_params["fe_h"] c_temp = host_params.get("comp_temp") print( "Observation {4} - {5}\n" "Expected Parameters\n---------------------\n" "teff={0:5.0f} logg={1:3.02f} feh={2:4.01f} \tcompanion_temp={3:5.0f} ".format(h_temp, h_logg, h_feh, c_temp, star_name, obsnum)) print("IAM SOLUTIONS\n---------------------") for ii, chi2_val in enumerate(chi2_names): df = extractor.minimum_value_of(chi2_val) print( "{0:10} solution:\nCompanion: teff_2={1:5.0f} logg2={2:4.02f} ".format(chi2_val, df.teff_2.values[0], df.logg_2.values[0]) + "feh2={0:4.01f} gamma={1:4.01f} rv={2:4.01f} ".format(df.feh_2.values[0], float(df.gamma.values[0]), float(df.rv.values[0])) + "Host: teff={0:5.0f} logg={1:4.02f} feh={2:4.01f} chi2={3:8.02f} median alpha={4:5.03f}".format( df.teff_1.values[0], df.logg_1.values[0], df.feh_1.values[0], df[chi2_val].values[0], np.median([df["alpha_1"].values[0], df["alpha_2"].values[0], df["alpha_3"].values[0], df["alpha_4"].values[0]])))
def contours(table, params): extractor = DBExtractor(table) for chi2_val, npix_val in zip(chi2_names, npix_names): df_min_chi2 = extractor.minimum_value_of(chi2_val) fixed_params = { "logg_1": float(df_min_chi2["logg_1"].values[0]), "feh_1": float(df_min_chi2["feh_1"].values[0]) } df = extractor.fixed_extraction( columns=["teff_1", "feh_1", "logg_1", "gamma", chi2_val], fixed=fixed_params) params["this_npix"] = params["npix"][npix_val] params["chi2_value"] = chi2_val pars = ["gamma", "teff_1", chi2_val] dataframe_contour(df, xcol=pars[0], ycol=pars[1], zcol=pars[2], params=params) # Reduced chi2 for chi2_val, npix_val in zip(chi2_names, npix_names): red_chi2 = "red_{0}".format(chi2_val) df_min_chi2 = extractor.minimum_value_of(chi2_val) fixed_params = { "logg_1": float(df_min_chi2["logg_1"].values[0]), "feh_1": float(df_min_chi2["feh_1"].values[0]) } df = extractor.fixed_extraction( columns=["teff_1", "feh_1", "logg_1", "gamma", chi2_val], fixed=fixed_params) df[red_chi2] = reduced_chi_squared(df[chi2_val], params["npix"][npix_val], params["npars"]) params["this_npix"] = params["npix"][npix_val] params["chi2_value"] = chi2_val pars = ["gamma", "teff_1", red_chi2] dataframe_contour(df, xcol=pars[0], ycol=pars[1], zcol=pars[2], params=params)
def contrast_bhm_results(table, params): extractor = DBExtractor(table) star_name = params["star"] obsnum = params["obsnum"] ___, host_params, ___ = bhm_helper_function(star_name, obsnum, 1) h_temp, h_logg, h_feh = host_params['temp'], host_params[ 'logg'], host_params["fe_h"] print( "Expected Parameters\n---------------------\nteff={0}\tlogg={1}\tfeh={2}" .format(h_temp, h_logg, h_feh)) print("BHM SOLUTIONS\n---------------------") for ii, chi2_val in enumerate(chi2_names): df = extractor.minimum_value_of(chi2_val) print("{0}: teff={1:5}\tlogg={2:3.02}\t".format( chi2_val, df.teff_1.values[0], df.logg_1.values[0]) + "feh={0:4.1}\tgamma={1:3.2},\txcorr={2:3.2},\tchi2={3:8.03}". format(df.feh_1.values[0], float(df.gamma.values[0]), df.xcorr_1.values[0], df[chi2_val].values[0]))
def compare_spectra(table, params): """Plot the min chi2 result against the observations.""" extractor = DBExtractor(table) for ii, chi2_val in enumerate(chi2_names[0:-2]): df = extractor.minimum_value_of(chi2_val) df = df[["teff_1", "logg_1", "feh_1", "gamma", "teff_2", "logg_2", "feh_2", "rv", chi2_val]] params1 = [df["teff_1"].values[0], df["logg_1"].values[0], df["feh_1"].values[0]] params2 = [df["teff_2"].values[0], df["logg_2"].values[0], df["feh_2"].values[0]] params1 = [float(param1) for param1 in params1] params2 = [float(param2) for param2 in params2] gamma = df["gamma"].values rv = df["rv"].values from simulators.iam_module import iam_helper_function obs_name, obs_params, output_prefix = iam_helper_function(params["star"], params["obsnum"], ii + 1) obs_spec = load_spectrum(obs_name) # Mask out bad portion of observed spectra # obs_spec = spectrum_masking(obs_spec, params["star"], params["obsnum"], ii + 1) # Barycentric correct spectrum _obs_spec = barycorr_crires_spectrum(obs_spec, extra_offset=None) normalization_limits = [obs_spec.xaxis[0] - 5, obs_spec.xaxis[-1] + 5] # models # print("params for models", params1, params2) mod1 = load_starfish_spectrum(params1, limits=normalization_limits, hdr=True, normalize=False, area_scale=True, flux_rescale=True) mod2 = load_starfish_spectrum(params2, limits=normalization_limits, hdr=True, normalize=False, area_scale=True, flux_rescale=True) iam_grid_func = inherent_alpha_model(mod1.xaxis, mod1.flux, mod2.flux, rvs=rv, gammas=gamma) iam_grid_model = iam_grid_func(obs_spec.xaxis).squeeze() iam_grid_model_full = iam_grid_func(mod1.xaxis).squeeze() model_spec_full = Spectrum(flux=iam_grid_model_full, xaxis=mod1.xaxis) model_spec = Spectrum(flux=iam_grid_model, xaxis=obs_spec.xaxis) model_spec = model_spec.remove_nans() model_spec = model_spec.normalize(method="exponential") model_spec_full = model_spec_full.remove_nans() model_spec_full = model_spec_full.normalize(method="exponential") fig, ax = plt.subplots(1, 1) plt.plot(obs_spec.xaxis, obs_spec.flux, label="Observation") plt.plot(model_spec.xaxis, model_spec.flux, label="Minimum \chi^2 model") plt.plot(model_spec_full.xaxis, model_spec_full.flux, "--", label="Model_full_res") plt.legend() fig.tight_layout() name = "{0}-{1}_{2}_{3}_min_chi2_spectrum_comparison_{4}.png".format( params["star"], params["obsnum"], params["chip"], chi2_val, params["suffix"]) plt.savefig(os.path.join(params["path"], "plots", name)) plt.close() plt.plot(obs_spec.xaxis, obs_spec.flux, label="Observation") plt.show()