Пример #1
0
def parabola_plots(table, params):
    extractor = DBExtractor(table)
    parabola_list = ["teff_2", "gamma", "rv"]
    for par in parabola_list:
        df = extractor.simple_extraction(columns=[par])
        unique_par = list(set(df[par].values))
        unique_par.sort()
        print("Unique ", par, " values =", unique_par)

        for chi2_val, npix_val in zip(chi2_names, npix_names):
            min_chi2 = []
            for unique_val in unique_par:
                df_chi2 = extractor.fixed_ordered_extraction(columns=[par, chi2_val], order_by=chi2_val,
                                                             fixed={par: float(unique_val)}, limit=3, asc=True)
                min_chi2.append(df_chi2[chi2_val].values[0])

            plt.plot(unique_par, min_chi2, ".-", label=chi2_val)
            # popt, _ = curve_fit(parabola, unique_par, min_chi2)
            popt, _ = fit_chi2_parabola(unique_par, min_chi2)

            x = np.linspace(unique_par[0], unique_par[-1], 40)
            plt.plot(x, parabola(x, *popt), "--")
            plt.xlabel(r"${0}$".format(par))
            plt.ylabel(r"$\chi^2$")

        plt.legend()
        filename = "Parabola_fit_{0}-{1}_{2}_param_{3}_{4}.png".format(
            params["star"], params["obsnum"], params["chip"], par, params["suffix"])

        plt.savefig(os.path.join(params["path"], "plots", filename))
        plt.close()
        print("saved parabolas for ", par)
    plt.close()
Пример #2
0
def display_bhm_xcorr_values(table, params):
    extractor = DBExtractor(table)
    fig, axarr = plt.subplots(3)
    for ii, xcorr in enumerate(
        [r"xcorr_1", r"xcorr_2", r"xcorr_3", r"xcorr_4"]):
        df = extractor.simple_extraction(["gamma", xcorr, "teff_1"])

        c = axarr[0].scatter(xshift(df["gamma"], ii),
                             df[xcorr],
                             c=df[r"teff_1"].values,
                             alpha=0.9,
                             label=xcorr)

        axarr[0].set_xlabel(r'host rv offset', fontsize=12)
        axarr[0].set_ylabel(r'Arbitrary norm', fontsize=12)
        axarr[0].set_title(r'Arbitrary normalization.')

        d = axarr[1].scatter(xshift(df[r"gamma"], ii),
                             df[xcorr],
                             c=df[r"teff_1"].values,
                             alpha=0.9,
                             label=xcorr)
        axarr[1].set_xlabel(r'$\gamma$ offset', fontsize=12)
        axarr[1].set_ylabel(r'Arbitrary norm', fontsize=12)
        axarr[1].set_title(r'$\gamma$.')

        e = axarr[2].scatter(xshift(df[r"teff_1"], ii),
                             df[xcorr],
                             c=df[r"gamma"].values,
                             alpha=0.9,
                             label=xcorr)
        axarr[2].set_xlabel(r'Companion temperature', fontsize=15)
        axarr[2].set_ylabel(r'Arbitrary norm ', fontsize=15)
        axarr[2].set_title(r'Companion Temperature')
    axarr[0].grid(True)
    axarr[1].grid(True)
    axarr[2].grid(True)

    cbar0 = plt.colorbar(c)
    cbar0.ax.set_ylabel(r" teff_1")
    cbar1 = plt.colorbar(d)
    cbar1.ax.set_ylabel(r" teff_1")
    cbar2 = plt.colorbar(e)
    cbar1.ax.set_ylabel(r"$\gamma$")
    fig.tight_layout()
    fig.suptitle("Arbitrary normalization used, \n slight shift with detetor")
    name = "{0}-{1}_{2}_plot_xcorr_value_{3}.pdf".format(
        params["star"], params["obsnum"], params["chip"], params["suffix"])
    plt.savefig(os.path.join(params["path"], "plots", name))
    plt.savefig(
        os.path.join(params["path"], "plots", name.replace(".pdf", ".png")))
    plt.close()
Пример #3
0
def host_parameters_reduced_gamma_individual(table, params):
    print("Fixed host analysis with reduced gamma individual plots.")
    extractor = DBExtractor(table)
    d_gamma = 5
    nrows, ncols = 1, 1

    for jj, (chi2_val, npix_val) in enumerate(zip(chi2_names, npix_names)):
        red_chi2 = "red_{0}".format(chi2_val)
        if jj == 4:
            chi2legend = "coadd"
        else:
            chi2legend = "det {0}".format(jj + 1)

        # Select lowest chi square gamma values.
        df = extractor.ordered_extraction(order_by=chi2_val,
                                          columns=["gamma", chi2_val],
                                          limit=1)

        min_chi2_gamma = df.loc[0, "gamma"]
        upper_lim = min_chi2_gamma + d_gamma
        lower_lim = min_chi2_gamma - d_gamma
        columns = ["teff_1", "logg_1", "feh_1", "gamma"]

        for ii, col in enumerate(columns):
            df = extractor.simple_extraction(
                columns={col, chi2_val, "gamma", "teff_1"})
            df = df[[
                (df.gamma > float(lower_lim)) & (df.gamma < float(upper_lim))
            ]]

            df[red_chi2] = reduced_chi_squared(df[chi2_val],
                                               params["npix"][npix_val],
                                               params["npars"])
            fig, axes = plt.subplots(nrows, ncols)
            fig.tight_layout()
            df.plot(x=col,
                    y=red_chi2,
                    kind="scatter",
                    ax=axes,
                    label=chi2legend)

            plt.suptitle("Coadd {2}-reduced Chi**2 Results: {0}-{1}".format(
                params["star"], params["obsnum"], col))
            name = "{0}-{1}_coadd_fixed_host_params_{2}_{3}_individual_{4}.png".format(
                params["star"], params["obsnum"], params["suffix"], chi2_val,
                col)
            fig.savefig(os.path.join(params["path"], "plots", name))
            fig.savefig(
                os.path.join(params["path"], "plots",
                             name.replace(".pdf", ".png")))
            plt.close()
        plt.close()
Пример #4
0
def gamma_plot(table, params):
    extractor = DBExtractor(table)
    for chi2_val, npix_val in zip(chi2_names, npix_names):
        df = extractor.simple_extraction(["gamma", chi2_val, "teff_1"])
        fig, ax = plt.subplots()
        c = ax.scatter(df["gamma"], df[chi2_val], c=df["teff_1"], alpha=0.8)
        cbar = plt.colorbar(c)
        cbar.ax.set_ylabel(r"teff_1")
        ax.set_xlabel(r'Host RV offset', fontsize=12)
        ax.set_ylabel(r"${0}$".format(chi2_val), fontsize=12)
        ax.set_title(r'$teff_1$ (color) and companion temperature.')
        ax.grid(True)
        fig.tight_layout()
        name = "{0}-{1}_{2}_temp_gamma_plot_{3}_{4}.pdf".format(
            params["star"], params["obsnum"], params["chip"], chi2_val,
            params["suffix"])
        plt.savefig(os.path.join(params["path"], "plots", name))
        plt.savefig(
            os.path.join(params["path"], "plots", name.replace(".pdf",
                                                               ".png")))
        plt.close()

        red_chi2 = "red_{0}".format(chi2_val)
        df[red_chi2] = reduced_chi_squared(df[chi2_val],
                                           params["npix"][npix_val],
                                           params["npars"])
        fig, ax = plt.subplots()
        c = ax.scatter(df["gamma"], df[red_chi2], c=df["teff_1"], alpha=0.8)
        cbar = plt.colorbar(c)
        cbar.ax.set_ylabel(r"teff_1")
        ax.set_xlabel(r'Host RV offset', fontsize=12)
        ax.set_ylabel(r"${0}$".format(red_chi2), fontsize=12)
        ax.set_title(r'$teff_1$ (color) and companion temperature.')
        ax.grid(True)
        fig.tight_layout()
        name = "{0}-{1}_{2}_temp_gamma_plot_{3}_{4}.pdf".format(
            params["star"], params["obsnum"], params["chip"], red_chi2,
            params["suffix"])
        plt.savefig(os.path.join(params["path"], "plots", name))
        plt.savefig(
            os.path.join(params["path"], "plots", name.replace(".pdf",
                                                               ".png")))
        plt.close()
Пример #5
0
def compare_spectra(table, params):
    """Plot the min chi2 result against the observations."""
    extractor = DBExtractor(table)
    gamma_df = extractor.simple_extraction(columns=["gamma"])
    extreme_gammas = [min(gamma_df.gamma.values), max(gamma_df.gamma.values)]
    for ii, chi2_val in enumerate(chi2_names[0:-2]):
        df = extractor.ordered_extraction(
            columns=["teff_1", "logg_1", "feh_1", "gamma", chi2_val],
            order_by=chi2_val,
            limit=1,
            asc=True)

        params1 = [
            df["teff_1"].values[0], df["logg_1"].values[0],
            df["feh_1"].values[0]
        ]

        params1 = [float(param1) for param1 in params1]

        gamma = df["gamma"].values

        obs_name, obs_params, output_prefix = bhm_helper_function(
            params["star"], params["obsnum"], ii + 1)
        print(obs_name)
        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)
        mod1 = load_starfish_spectrum(params1,
                                      limits=normalization_limits,
                                      hdr=True,
                                      normalize=False,
                                      area_scale=True,
                                      flux_rescale=True)

        bhm_grid_func = one_comp_model(mod1.xaxis, mod1.flux, gammas=gamma)
        bhm_upper_gamma = one_comp_model(mod1.xaxis,
                                         mod1.flux,
                                         gammas=extreme_gammas[1])
        bhm_lower_gamma = one_comp_model(mod1.xaxis,
                                         mod1.flux,
                                         gammas=extreme_gammas[0])

        bhm_grid_model = bhm_grid_func(obs_spec.xaxis).squeeze()
        bhm_grid_model_full = bhm_grid_func(mod1.xaxis).squeeze()
        bhm_upper_gamma = bhm_upper_gamma(obs_spec.xaxis).squeeze()
        bhm_lower_gamma = bhm_lower_gamma(obs_spec.xaxis).squeeze()

        model_spec_full = Spectrum(flux=bhm_grid_model_full, xaxis=mod1.xaxis)
        model_spec = Spectrum(flux=bhm_grid_model, xaxis=obs_spec.xaxis)
        bhm_upper_gamma = Spectrum(flux=bhm_upper_gamma, xaxis=obs_spec.xaxis)
        bhm_lower_gamma = Spectrum(flux=bhm_lower_gamma, 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")
        bhm_lower_gamma = bhm_lower_gamma.remove_nans()
        bhm_lower_gamma = bhm_lower_gamma.normalize(method="exponential")
        bhm_upper_gamma = bhm_upper_gamma.remove_nans()
        bhm_upper_gamma = bhm_upper_gamma.normalize(method="exponential")

        from mingle.utilities.chisqr import chi_squared
        chisqr = chi_squared(obs_spec.flux, model_spec.flux)

        print("Recomputed chi^2 = {0}".format(chisqr))
        print("Database chi^2 = {0}".format(df[chi2_val]))
        fig, ax = plt.subplots(1, 1, figsize=(15, 8))
        plt.plot(obs_spec.xaxis,
                 obs_spec.flux + 0.01,
                 label="0.05 + Observation, {}".format(obs_name))
        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.plot(bhm_lower_gamma.xaxis,
                 bhm_lower_gamma.flux,
                 "-.",
                 label="gamma={}".format(extreme_gammas[0]))
        plt.plot(bhm_upper_gamma.xaxis,
                 bhm_upper_gamma.flux,
                 ":",
                 label="gamma={}".format(extreme_gammas[1]))
        plt.title("bhm spectrum")
        plt.legend()

        fig.tight_layout()
        name = "{0}-{1}_{2}_{3}_bhm_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()
Пример #6
0
def chi2_individual_parabola_plots(table, params):
    extractor = DBExtractor(table)
    parabola_list = ["teff_1", "logg_1", "feh_1", "gamma"]
    for par in parabola_list:
        df = extractor.simple_extraction(columns=[par])
        unique_par = list(set(df[par].values))
        unique_par.sort()

        for chi2_val, npix_val in zip(chi2_names, npix_names):
            min_chi2 = []
            for unique_val in unique_par:
                df_chi2 = extractor.fixed_ordered_extraction(
                    columns=[par, chi2_val],
                    order_by=chi2_val,
                    limit=3,
                    fixed={par: float(unique_val)},
                    asc=True)
                min_chi2.append(df_chi2[chi2_val].values[0])

            # min_chi2 = reduced_chi_squared(min_chi2, params["npix"][npix_val], params["npars"])

            min_chi2 = min_chi2 - min(min_chi2)

            plt.plot(unique_par, min_chi2, ".-", label=chi2_val)

            # popt, _ = curve_fit(parabola, unique_par, min_chi2)
            popt, _ = fit_chi2_parabola(unique_par, min_chi2)
            # print("params", popt)
            x = np.linspace(unique_par[0], unique_par[-1], 40)
            plt.plot(x, parabola(x, *popt))  # , label="parabola")
            plt.xlabel(r"${0}$".format(par))
            plt.ylabel(r"$\Delta \chi^2$ from mimimum")
            plt.ylim([-0.05 * np.max(min_chi2), np.max(min_chi2)])
            # Find roots

            try:
                residual = lambda x: parabola(x, *popt) - chi2_at_sigma(
                    1, params["npars"])
                min_chi2_par = unique_par[np.argmin(min_chi2)]
                min_chi2_par.astype(np.float64)
                try:
                    lower_bound = newton(
                        residual,
                        (min_chi2_par + unique_par[0]) / 2) - min_chi2_par
                except RuntimeError as e:
                    print(e)
                    lower_bound = np.nan
                try:
                    upper_bound = newton(
                        residual,
                        (min_chi2_par + unique_par[-1]) / 2) - min_chi2_par
                except RuntimeError as e:
                    print(e)
                    upper_bound = np.nan
                print("min_chi2_par", min_chi2_par,
                      type(min_chi2_par), "\nlower_bound", lower_bound,
                      type(lower_bound), "\nupper_bound", upper_bound,
                      type(upper_bound))
                print("{0} solution {1: 5.3} {2:+5.3} {3:+5.3}".format(
                    chi2_val, float(min_chi2_par), float(lower_bound),
                    float(upper_bound)))
                plt.annotate("{0: 5.3f} {1:+5.3f} {2:+5.3f}".format(
                    float(min_chi2_par), (lower_bound), (upper_bound)),
                             xy=(float(min_chi2_par), 0),
                             xytext=(0.4, 0.5),
                             textcoords="figure fraction",
                             arrowprops={"arrowstyle": "->"})
            except Exception as e:
                print(e)
                logging.warning("Could not Annotate the contour plot")

            plt.axhline(y=chi2_at_sigma(1, params["npars"]), label="1 sigma")
            plt.axhline(y=chi2_at_sigma(2, params["npars"]), label="2 sigma")
            plt.axhline(y=chi2_at_sigma(3, params["npars"]), label="3 sigma")

            plt.legend()
            filename = "Chi2_Parabola_fit_{0}-{1}_{2}_param_{3}_{4}_individual_{5}.png".format(
                params["star"], params["obsnum"], params["chip"], par,
                params["suffix"], chi2_val)

            plt.savefig(os.path.join(params["path"], "plots", filename))
            plt.close()
            print("saved individual chi2 parabolas for ", par)
            plt.close()