def prepare_iam_model_spectra(params1: Union[List[float], List[Union[int, float]]],
                              params2: Union[List[float], List[Union[int, float]], Tuple[int, float, float]],
                              limits: Union[List[float64], Tuple[int, int], List[int]], area_scale: bool = True,
                              wav_scale: bool = True) -> Tuple[Spectrum, Spectrum]:
    """Load spectra with same settings."""
    if not area_scale:
        warnings.warn("Not using area_scale. This is incorrect for paper.")
    if not wav_scale:
        warnings.warn("Not using wav_scale. This is incorrect for paper.")
    mod1_spec = load_starfish_spectrum(params1, limits=limits,
                                       hdr=True, normalize=False, area_scale=area_scale,
                                       flux_rescale=True, wav_scale=wav_scale)
    mod2_spec = load_starfish_spectrum(params2, limits=limits,
                                       hdr=True, normalize=False, area_scale=area_scale,
                                       flux_rescale=True, wav_scale=wav_scale)
    assert len(mod1_spec.xaxis) > 0 and len(mod2_spec.xaxis) > 0
    assert np.allclose(mod1_spec.xaxis, mod2_spec.xaxis)
    # Check correct models are loaded
    assert mod1_spec.header["PHXTEFF"] == params1[0]
    assert mod1_spec.header["PHXLOGG"] == params1[1]
    assert mod1_spec.header["PHXM_H"] == params1[2]
    assert mod2_spec.header["PHXTEFF"] == params2[0]
    assert mod2_spec.header["PHXLOGG"] == params2[1]
    assert mod2_spec.header["PHXM_H"] == params2[2]
    return mod1_spec, mod2_spec
def fake_obs(simnum, snr=200, suffix=None, plot=False, mode="iam"):
    snr = 200

    params1 = [5300, 4.5, 0.0]
    params2 = [2500, 4.5, 0.0]
    gamma = 5
    rv = -3
    normalization_limits = [2100, 2180]

    mod1_spec = load_starfish_spectrum(params1,
                                       limits=normalization_limits,
                                       hdr=True,
                                       normalize=False,
                                       area_scale=True,
                                       flux_rescale=True)

    mod2_spec = load_starfish_spectrum(params2,
                                       limits=normalization_limits,
                                       hdr=True,
                                       normalize=False,
                                       area_scale=True,
                                       flux_rescale=True)

    if broadcast:
        broadcast_result = inherent_alpha_model(mod1_spec.xaxis,
                                                mod1_spec.flux,
                                                mod2_spec.flux,
                                                rvs=rv,
                                                gammas=gamma)

        broadcast_values = broadcast_result(obs_spec.xaxis)
        result_spectrum = Spectrum(flux=broadcast_values,
                                   xaxis=mod1_spec.xaxis)
    else:
        # Manually redo the join
        mod2_spec.doppler_shift(rv)
        mod_combine = mod1_spec.copy()
        mod_combine += mod2_spec
        mod_combine.doppler_shift(gamma)
        mod_combine.normalize(method="exponential")
        mod_combine.interpolate(obs_spec.xaxis)
        result_spectrum = mod_combine
        # result_spectrum = Spectrum(flux=broadcast_values, xaxis=obs_spec.xaxis)

    result_spectrum.add_noise(snr)

    # Save as
    # Detector limits
    dect_limits = [(2112, 2123), (2127, 2137), (2141, 2151), (2155, 2165)]

    for ii, dect in enumerate(dect_limits):
        spec = result_spectrum.copy()
        spec.wav_select(*dect)
        spec.resample(1024)

        name = "HDsim-{0}_{1}_snr_{2}".format(sim_num, ii, snr)
def load_starfish_hd211847(limits=None, normalize=False, hdr=False):
    """Load in the phoenix spectra of HD211847 and HD211847b.

    Returns:
    star spectrum for hd211847
    bd spectrum for hd211847b
    """
    bd_model = [3100, 4.50, 0.0]
    star_model = [5700, 4.50, 0.0]
    bd_spec = load_starfish_spectrum(bd_model, limits=limits, hdr=hdr, normalize=normalize)
    star_spec = load_starfish_spectrum(star_model, limits=limits, hdr=hdr, normalize=normalize)

    return star_spec, bd_spec
def fake_bhm_simulation(wav,
                        params,
                        gamma,
                        limits=(2070, 2180),
                        noise=None,
                        header=False):
    """Make a fake spectrum with binary params and radial velocities."""

    mod_spec = load_starfish_spectrum(params,
                                      limits=limits,
                                      hdr=True,
                                      normalize=True,
                                      wav_scale=True)

    bhm_grid_func = one_comp_model(mod_spec.xaxis, mod_spec.flux, gammas=gamma)

    if wav is None:
        delta = spec_max_delta(mod_spec, 0, gamma)
        assert np.all(np.isfinite(mod_spec.xaxis))
        mask = (mod_spec.xaxis > mod_spec.xaxis[0] + 2 * delta) * (
            mod_spec.xaxis < mod_spec.xaxis[-1] - 2 * delta)
        wav = mod_spec.xaxis[mask]

    bhm_grid_values = bhm_grid_func(wav).squeeze()

    logging.debug(
        __("number of bhm nans = {}", np.sum(~np.isfinite(bhm_grid_values))))

    if noise is not None or noise is not 0:
        bhm_grid_values = add_noise(bhm_grid_values, noise)
    else:
        logging.warning(
            "\n!!!\n\nNot adding any noise to bhm fake simulation!!!!!\n\n!!!!!\n"
        )
        print("\n!!!!\n\nNot adding any noise to fake simulation!!!!\n\n!\n")

    if header:
        return wav, bhm_grid_values.squeeze(), mod_spec.header
    else:
        return wav, bhm_grid_values.squeeze()
# In[3]:

snr = 300
sim_num = 3
starname = "HDSIM4"
params1 = [6000, 4.5, 0.0]
params2 = [5800, 4.5, 0.0]
gamma = 45
rv = -30

normalization_limits = [2000, 2300]

mod1_spec_scaled = load_starfish_spectrum(params1,
                                          limits=normalization_limits,
                                          hdr=True,
                                          normalize=False,
                                          area_scale=True,
                                          flux_rescale=True)
mod1_spec_unscaled = load_starfish_spectrum(params1,
                                            limits=normalization_limits,
                                            hdr=True,
                                            normalize=False,
                                            area_scale=False,
                                            flux_rescale=True)

mod2_spec_scaled = load_starfish_spectrum(params2,
                                          limits=normalization_limits,
                                          hdr=True,
                                          normalize=False,
                                          area_scale=True,
                                          flux_rescale=True)
示例#6
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()
示例#7
0
def main():
    """Main function."""
    star = "HD211847"
    param_file = os.path.join(simulators.paths["parameters"], "{}_params.dat".format(star))
    params = parse_paramfile(param_file, path=None)
    host_params = [params["temp"], params["logg"], params["fe_h"]]
    # comp_params = [params["comp_temp"], params["logg"], params["fe_h"]]

    obsnum = 2
    chip = 4

    obs_name = os.path.join(
        simulators.paths["spectra"], "{}-{}-mixavg-tellcorr_{}.fits".format(star, obsnum, chip))
    logging.info("The observation used is ", obs_name, "\n")

    closest_host_model = closest_model_params(*host_params)  # unpack temp, logg, fe_h with *
    # closest_comp_model = closest_model_params(*comp_params)

    # original_model = "Z-0.0/lte05700-4.50-0.0.PHOENIX-ACES-AGSS-COND-2011-HiRes.fits"
    # logging.debug(pv("closest_host_model"))
    # logging.debug(pv("closest_comp_model"))
    # logging.debug(pv("original_model"))

    # Function to find the good models I need
    # models = find_phoenix_model_names(model_base_dir, original_model)
    # Function to find the good models I need from parameters
    # model_par_gen = generate_close_params(closest_host_model)
    model_pars = list(generate_close_params(closest_host_model))  # Turn to list

    print("Model parameters", model_pars)

    # Load observation
    obs_spec = load_spectrum(obs_name)
    _obs_spec = barycorr_crires_spectrum(obs_spec, extra_offset=None)
    obs_spec.flux /= 1.02
    # Mask out bad portion of observed spectra ## HACK
    if chip == 4:
        # Ignore first 40 pixels
        obs_spec.wav_select(obs_spec.xaxis[40], obs_spec.xaxis[-1])

    import simulators
    gammas = np.arange(*simulators.sim_grid["gammas"])

    ####
    chi2_grids = bhm_analysis(obs_spec, model_pars, gammas, verbose=True)
    ####
    (model_chisqr_vals, model_xcorr_vals, model_xcorr_rv_vals,
     broadcast_chisqr_vals, broadcast_gamma, broadcast_chisquare) = chi2_grids

    TEFF = [par[0] for par in model_pars]
    LOGG = [par[1] for par in model_pars]
    FEH = [par[2] for par in model_pars]

    plt.plot(TEFF, broadcast_chisqr_vals, "+", label="broadcast")
    plt.plot(TEFF, model_chisqr_vals, ".", label="org")
    plt.title("TEFF vs Broadcast chisqr_vals")
    plt.legend()
    plt.show()
    plt.plot(TEFF, broadcast_gamma, "o")
    plt.title("TEFF vs Broadcast gamma grid")
    plt.show()

    plt.plot(LOGG, broadcast_chisqr_vals, "+", label="broadcast")
    plt.plot(LOGG, model_chisqr_vals, ".", label="org")
    plt.title("LOGG verse Broadcast chisqr_vals")
    plt.legend()
    plt.show()
    plt.plot(LOGG, broadcast_gamma, "o")
    plt.title("LOGG verse Broadcast gamma grid")
    plt.show()

    plt.plot(FEH, broadcast_chisqr_vals, "+", label="broadcast")
    plt.plot(FEH, model_chisqr_vals, ".", label="org")
    plt.title("FEH vs Broadcast chisqr_vals")
    plt.legend()
    plt.show()
    plt.plot(FEH, broadcast_gamma, "o")
    plt.title("FEH vs Broadcast gamma grid")
    plt.show()

    TEFFS_unique = np.array(set(TEFF))
    LOGG_unique = np.array(set(LOGG))
    FEH_unique = np.array(set(FEH))
    X, Y, Z = np.meshgrid(TEFFS_unique, LOGG_unique, FEH_unique)  # set sparse=True for memory efficency
    print("Teff grid", X)
    print("Logg grid", Y)
    print("FEH grid", Z)
    assert len(TEFF) == sum(len(x) for x in (TEFFS_unique, LOGG_unique, FEH_unique))

    chi_ND = np.empty_like(X.shape)
    print("chi_ND.shape", chi_ND.shape)
    print("len(TEFFS_unique)", len(TEFFS_unique))
    print("len(LOGG_unique)", len(LOGG_unique))
    print("len(FEH_unique)", len(FEH_unique))

    for i, tf in enumerate(TEFFS_unique):
        for j, lg in enumerate(LOGG_unique):
            for k, fh in enumerate(FEH_unique):
                print("i,j,k", (i, j, k))
                print("num = t", np.sum(TEFF == tf))
                print("num = lg", np.sum(LOGG == lg))
                print("num = fh", np.sum(FEH == fh))
                mask = (TEFF == tf) * (LOGG == lg) * (FEH == fh)
                print("num = tf, lg, fh", np.sum(mask))
                chi_ND[i, j, k] = broadcast_chisqr_vals[mask]
                print("broadcast val", broadcast_chisqr_vals[mask],
                      "\norg val", model_chisqr_vals[mask])

    # logging.debug(pv("model_chisqr_vals"))
    # logging.debug(pv("model_xcorr_vals"))
    chisqr_argmin_indx = np.argmin(model_chisqr_vals)
    xcorr_argmax_indx = np.argmax(model_xcorr_vals)

    print("Minimum  Chisqr value =", model_chisqr_vals[chisqr_argmin_indx])  # , min(model_chisqr_vals)
    print("Chisqr at max correlation value", model_chisqr_vals[chisqr_argmin_indx])

    print("model_xcorr_vals = {}".format(model_xcorr_vals))
    print("Maximum Xcorr value =", model_xcorr_vals[xcorr_argmax_indx])  # , max(model_xcorr_vals)
    print("Xcorr at min Chiqsr", model_xcorr_vals[chisqr_argmin_indx])

    # logging.debug(pv("model_xcorr_rv_vals"))
    print("RV at max xcorr =", model_xcorr_rv_vals[xcorr_argmax_indx])
    # print("Meadian RV val =", np.median(model_xcorr_rv_vals))
    print(pv("model_xcorr_rv_vals[chisqr_argmin_indx]"))
    print(pv("sp.stats.mode(np.around(model_xcorr_rv_vals))"))

    # print("Max Correlation model = ", models[xcorr_argmax_indx].split("/")[-2:])
    # print("Min Chisqr model = ", models[chisqr_argmin_indx].split("/")[-2:])
    print("Max Correlation model = ", model_pars[xcorr_argmax_indx])
    print("Min Chisqr model = ", model_pars[chisqr_argmin_indx])

    limits = [2110, 2160]

    best_model_params = model_pars[chisqr_argmin_indx]
    best_model_spec = load_starfish_spectrum(best_model_params, limits=limits, normalize=True)

    best_xcorr_model_params = model_pars[xcorr_argmax_indx]
    best_xcorr_model_spec = load_starfish_spectrum(best_xcorr_model_params, limits=limits, normalize=True)

    close_model_spec = load_starfish_spectrum(closest_model_params, limits=limits, normalize=True)

    plt.plot(obs_spec.xaxis, obs_spec.flux, label="Observations")
    plt.plot(best_model_spec.xaxis, best_model_spec.flux, label="Best Model")
    plt.plot(best_xcorr_model_spec.xaxis, best_xcorr_model_spec.flux, label="Best xcorr Model")
    plt.plot(close_model_spec.xaxis, close_model_spec.flux, label="Close Model")
    plt.legend()
    plt.xlim(*limits)
    plt.show()

    logging.debug("After plot")
示例#8
0
"""Try out the models and how they look.
"""

import matplotlib.pyplot as plt

from mingle.utilities.phoenix_utils import load_starfish_spectrum
from obsolete.models.alpha_model import alpha_model2
from obsolete.models.alpha_model import no_alpha

# Parameters of models
# host_params = [6000, 4.5, 0]
# companion_params = [3100, 4.5, 0.5]
host_params = [5000, 4.5, 0]
companion_params = [2300, 4.5, 0.5]

host_unnorm = load_starfish_spectrum(host_params)
comp_unnorm = load_starfish_spectrum(companion_params)

plt.plot(host_unnorm.xaxis, host_unnorm.flux, label="host un-norm")
plt.plot(comp_unnorm.xaxis, comp_unnorm.flux, label="companion un-norm")
plt.legend()
plt.title("Unormalized spectra")
plt.show()

host_norm = load_starfish_spectrum(host_params, normalize=True)
comp_norm = load_starfish_spectrum(companion_params, normalize=True)

plt.plot(host_norm.xaxis, host_norm.flux, label="host norm")
plt.plot(comp_norm.xaxis, comp_norm.flux, label="companion norm")
plt.legend()
plt.title("Normalized spectra")
示例#9
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()
def comp():
    """Normalized Companion spectrum fixture."""
    mod_spec = load_starfish_spectrum([2600, 4.50, 0.0],
                                      limits=[2110, 2165],
                                      normalize=True)
    return mod_spec
def host():
    """Host spectrum fixture."""
    mod_spec = load_starfish_spectrum([5200, 4.50, 0.0],
                                      limits=[2110, 2165],
                                      normalize=True)
    return mod_spec
示例#12
0
def tcm_analysis(obs_spec,
                 model1_pars,
                 model2_pars,
                 alphas=None,
                 rvs=None,
                 gammas=None,
                 verbose=False,
                 norm=False,
                 chip=None,
                 prefix=None):
    """Run two component model over all parameter cobinations in model1_pars and model2_pars."""
    if chip is None:
        chip = ""

    if alphas is None:
        alphas = np.array([0])
    elif isinstance(alphas, (float, int)):
        alphas = np.asarray(alphas, dtype=np.float32)
    if rvs is None:
        rvs = np.array([0])
    elif isinstance(rvs, (float, int)):
        rvs = np.asarray(rvs, dtype=np.float32)
    if gammas is None:
        gammas = np.array([0])
    elif isinstance(gammas, (float, int)):
        gammas = np.asarray(gammas, dtype=np.float32)

    if isinstance(model1_pars, list):
        logging.debug(
            __("Number of close model_pars returned {0}", len(model1_pars)))
    if isinstance(model2_pars, list):
        logging.debug(
            __("Number of close model_pars returned {0}", len(model2_pars)))

    print("host params", model1_pars)
    print("companion params", model2_pars)

    # Solution Grids to return
    # model_chisqr_vals = np.empty((len(model1_pars), len(model2_pars)))
    # model_xcorr_vals = np.empty(len(model1_pars), len(model2_pars))
    # model_xcorr_rv_vals = np.empty(len(model1_pars), len(model2_pars))
    broadcast_chisqr_vals = np.empty((len(model1_pars), len(model2_pars)))
    # broadcast_gamma = np.empty((len(model1_pars), len(model2_pars)))
    # full_broadcast_chisquare = np.empty((len(model1_pars), len(model2_pars), len(alphas), len(rvs), len(gammas)))

    normalization_limits = [2105, 2185]  # small as possible?
    # combined_params = itertools.product(model1_pars, model2_pars)
    for ii, params1 in enumerate(tqdm(model1_pars)):
        if prefix is None:
            sf = (
                "Analysis/{0}/tc_{0}_{1}-{2}_part{6}_host_pars_[{3}_{4}_{5}]_par"
                ".csv").format(obs_spec.header["OBJECT"].upper(),
                               int(obs_spec.header["MJD-OBS"]), chip,
                               params1[0], params1[1], params1[2], ii)

        else:
            sf = "{0}_part{4}_host_pars_[{1}_{2}_{3}]_par.csv".format(
                prefix, params1[0], params1[1], params1[2], ii)
        save_filename = sf

        for jj, params2 in enumerate(model2_pars):
            if verbose:
                print("Starting iteration with parameters:\n{0}={1},{2}={3}".
                      format(ii, params1, jj, params2))
            mod1_spec = load_starfish_spectrum(params1,
                                               limits=normalization_limits,
                                               hdr=True,
                                               normalize=True)
            mod2_spec = load_starfish_spectrum(params2,
                                               limits=normalization_limits,
                                               hdr=True,
                                               normalize=True)

            # TODO WHAT IS THE MAXIMUM (GAMMA + RV POSSIBLE? LIMIT IT TO THAT SHIFT?

            # Wavelength selection
            mod1_spec.wav_select(
                np.min(obs_spec.xaxis) - 5,
                np.max(obs_spec.xaxis) + 5)  # +- 5nm of obs for convolution
            mod2_spec.wav_select(
                np.min(obs_spec.xaxis) - 5,
                np.max(obs_spec.xaxis) + 5)
            obs_spec = obs_spec.remove_nans()

            # One component model with broadcasting over gammas
            # two_comp_model(wav, model1, model2, alphas, rvs, gammas)
            assert np.allclose(mod1_spec.xaxis, mod2_spec.xaxis)

            broadcast_result = two_comp_model(mod1_spec.xaxis,
                                              mod1_spec.flux,
                                              mod2_spec.flux,
                                              alphas=alphas,
                                              rvs=rvs,
                                              gammas=gammas)
            broadcast_values = broadcast_result(obs_spec.xaxis)

            assert ~np.any(np.isnan(obs_spec.flux)), "Observation is nan"

            #### NORMALIZATION NEEDED HERE
            if norm:
                obs_flux = chi2_model_norms(obs_spec.xaxis, obs_spec.flux,
                                            broadcast_values)

            else:
                obs_flux = obs_spec.flux[:, np.newaxis, np.newaxis, np.newaxis]
            #####

            broadcast_chisquare = chi_squared(obs_flux, broadcast_values)
            sp_chisquare = sp.stats.chisquare(obs_flux,
                                              broadcast_values,
                                              axis=0).statistic

            assert np.all(sp_chisquare == broadcast_chisquare)

            # print(broadcast_chisquare.shape)
            # print("chi squaresd = ", broadcast_chisquare.ravel()[np.argmin(broadcast_chisquare)])
            # New parameters to explore
            broadcast_chisqr_vals[ii, jj] = broadcast_chisquare.ravel()[
                np.argmin(broadcast_chisquare)]
            # broadcast_gamma[ii, jj] = gammas[np.argmin(broadcast_chisquare)]
            # full_broadcast_chisquare[ii, jj, :] = broadcast_chisquare

            save_full_chisqr(save_filename,
                             params1,
                             params2,
                             alphas,
                             rvs,
                             gammas,
                             broadcast_chisquare,
                             verbose=verbose)

    return broadcast_chisqr_vals  # Just output the best value for each model pair
示例#13
0
def main(star,
         obsnum,
         teff_1,
         logg_1,
         feh_1,
         teff_2,
         logg_2,
         feh_2,
         gamma,
         rv,
         plot_name=None):
    fig, axis = plt.subplots(2, 2, figsize=(15, 8), squeeze=False)

    for chip, ax in zip(range(1, 5), axis.flatten()):
        # Get observation data
        obs_name, params, output_prefix = iam_helper_function(
            star, obsnum, chip)

        # Load observed spectrum
        obs_spec = load_spectrum(obs_name)

        # Mask out bad portion of observed spectra
        obs_spec = spectrum_masking(obs_spec, star, obsnum, chip)

        # Barycentric correct spectrum
        _obs_spec = barycorr_crires_spectrum(obs_spec, extra_offset=None)

        # Determine Spectrum Errors
        # error_off = False
        # errors = spectrum_error(star, obsnum, chip, error_off=error_off)

        # Create model with given parameters
        host = load_starfish_spectrum([teff_1, logg_1, feh_1],
                                      limits=[2110, 2165],
                                      area_scale=True,
                                      hdr=True)
        if teff_2 is None:
            assert (logg_2 is None) and (feh_2 is None) and (
                rv == 0), "All must be None for bhm case."
            companion = Spectrum(xaxis=host.xaxis,
                                 flux=np.zeros_like(host.flux))
        else:
            companion = load_starfish_spectrum([teff_2, logg_2, feh_2],
                                               limits=[2110, 2165],
                                               area_scale=True,
                                               hdr=True)

        joint_model = inherent_alpha_model(host.xaxis,
                                           host.flux,
                                           companion.flux,
                                           gammas=gamma,
                                           rvs=rv)

        model_spec = Spectrum(xaxis=host.xaxis,
                              flux=joint_model(host.xaxis).squeeze())
        model_spec = model_spec.remove_nans()
        model_spec = model_spec.normalize("exponential")

        # plot
        obs_spec.plot(axis=ax, label="{}-{}".format(star, obsnum))
        model_spec.plot(axis=ax, linestyle="--", label="Chi-squared model")
        ax.set_xlim([obs_spec.xmin() - 0.5, obs_spec.xmax() + 0.5])

        ax.set_title("{} obs {} chip {}".format(star, obsnum, chip))
        ax.legend()
    plt.tight_layout()

    if plot_name is None:
        fig.show()
    else:
        if not (plot_name.endswith(".png") or plot_name.endswith(".pdf")):
            raise ValueError("plot_name does not end with .pdf or .png")
        fig.savefig(plot_name)

    return 0
def tcm_wrapper(num,
                params1,
                model2_pars,
                alphas,
                rvs,
                gammas,
                obs_spec,
                errors=None,
                norm=True,
                verbose=False,
                save_only=True,
                chip=None,
                prefix=None,
                wav_scale=True,
                norm_method="scalar"):
    """Wrapper for iteration loop of tcm. params1 fixed, model2_pars are many."""
    normalization_limits = [2105, 2185]  # small as possible?

    if prefix is None:
        sf = os.path.join(
            simulators.paths["output_dir"], obs_spec.header["OBJECT"].upper(),
            "tc_{0}_{1}-{2}_part{6}_host_pars_[{3}_{4}_{5}].csv".format(
                obs_spec.header["OBJECT"].upper(),
                int(obs_spec.header["MJD-OBS"]), chip, params1[0], params1[1],
                params1[2], num))
    else:
        sf = "{0}_part{4}_host_pars_[{1}_{2}_{3}].csv".format(
            prefix, params1[0], params1[1], params1[2], num)
    save_filename = sf

    if os.path.exists(save_filename) and save_only:
        print("''{}' exists, so not repeating calculation.".format(
            save_filename))
        return None
    else:
        if not save_only:
            broadcast_chisqr_vals = np.empty(len(model2_pars))
        for jj, params2 in enumerate(model2_pars):
            if verbose:
                print("Starting iteration with parameters:\n {0}={1},{2}={3}".
                      format(num, params1, jj, params2))

            mod1_spec = load_starfish_spectrum(params1,
                                               limits=normalization_limits,
                                               hdr=True,
                                               normalize=True,
                                               wav_scale=wav_scale)
            mod2_spec = load_starfish_spectrum(params2,
                                               limits=normalization_limits,
                                               hdr=True,
                                               normalize=True,
                                               wav_scale=wav_scale)

            # Wavelength selection
            rv_limits = observation_rv_limits(obs_spec, rvs, gammas)
            mod1_spec.wav_select(*rv_limits)
            mod2_spec.wav_select(*rv_limits)

            obs_spec = obs_spec.remove_nans()

            # One component model with broadcasting over gammas
            # two_comp_model(wav, model1, model2, alphas, rvs, gammas)
            assert np.allclose(mod1_spec.xaxis, mod2_spec.xaxis)

            broadcast_result = two_comp_model(mod1_spec.xaxis,
                                              mod1_spec.flux,
                                              mod2_spec.flux,
                                              alphas=alphas,
                                              rvs=rvs,
                                              gammas=gammas)
            broadcast_values = broadcast_result(obs_spec.xaxis)

            assert ~np.any(np.isnan(obs_spec.flux)), "Observation is nan"

            # RE-NORMALIZATION
            if chip == 4:
                # Quadratically renormalize anyway
                obs_spec = renormalization(obs_spec,
                                           broadcast_values,
                                           normalize=True,
                                           method="quadratic")
            obs_flux = renormalization(obs_spec,
                                       broadcast_values,
                                       normalize=norm,
                                       method=norm_method)

            # sp_chisquare is much faster but don't think I can add masking.
            broadcast_chisquare = chi_squared(obs_flux,
                                              broadcast_values,
                                              error=errors)
            # sp_chisquare = stats.chisquare(obs_flux, broadcast_values, axis=0).statistic
            # broadcast_chisquare = sp_chisquare

            if not save_only:
                print(broadcast_chisquare.shape)
                print(broadcast_chisquare.ravel()[np.argmin(
                    broadcast_chisquare)])

                broadcast_chisqr_vals[jj] = broadcast_chisquare.ravel()[
                    np.argmin(broadcast_chisquare)]
            npix = obs_flux.shape[0]
            save_full_tcm_chisqr(save_filename,
                                 params1,
                                 params2,
                                 alphas,
                                 rvs,
                                 gammas,
                                 broadcast_chisquare,
                                 npix,
                                 verbose=verbose)

        if save_only:
            return None
        else:
            return broadcast_chisqr_vals
def bhm_analysis(obs_spec, model_pars, gammas=None, errors=None, prefix=None, verbose=False, chip=None, norm=False,
                 wav_scale=True, norm_method="scalar"):
    """Run one component model over all parameter combinations in model_pars."""
    # Gammas
    if gammas is None:
        gammas = np.array([0])
    elif isinstance(gammas, (float, int)):
        gammas = np.asarray(gammas, dtype=np.float32)

    if isinstance(model_pars, list):
        logging.debug(__("Number of close model_pars returned {0}", len(model_pars)))

    # Solution Grids to return
    model_chisqr_vals = np.empty(len(model_pars))
    model_xcorr_vals = np.empty(len(model_pars))
    model_xcorr_rv_vals = np.empty(len(model_pars))
    bhm_grid_chisqr_vals = np.empty(len(model_pars))
    bhm_grid_gamma = np.empty(len(model_pars))
    full_bhm_grid_chisquare = np.empty((len(model_pars), len(gammas)))

    normalization_limits = [2105, 2185]  # small as possible?

    for ii, params in enumerate(tqdm(model_pars)):
        if prefix is None:
            save_name = os.path.join(
                simulators.paths["output_dir"], obs_spec.header["OBJECT"].upper(), "bhm",
                "bhm_{0}_{1}_{3}_part{2}.csv".format(
                    obs_spec.header["OBJECT"].upper(), obs_spec.header["MJD-OBS"], ii, chip))
        else:
            save_name = os.path.join("{0}_part{1}.csv".format(prefix, ii))

        if verbose:
            print("Starting iteration with parameter:s\n{}".format(params))

        mod_spec = load_starfish_spectrum(params, limits=normalization_limits, hdr=True,
                                          normalize=True, wav_scale=wav_scale)

        # Wavelength selection
        mod_spec.wav_select(np.min(obs_spec.xaxis) - 5,
                            np.max(obs_spec.xaxis) + 5)  # +- 5nm of obs

        obs_spec = obs_spec.remove_nans()

        # One component model with broadcasting over gammas
        bhm_grid_func = one_comp_model(mod_spec.xaxis, mod_spec.flux, gammas=gammas)
        bhm_grid_values = bhm_grid_func(obs_spec.xaxis)

        assert ~np.any(np.isnan(obs_spec.flux)), "Observation is nan"

        # RENORMALIZATION
        if chip == 4:
            # Quadratically renormalize anyway
            obs_spec = renormalization(obs_spec, bhm_grid_values, normalize=True, method="quadratic")
        obs_flux = renormalization(obs_spec, bhm_grid_values, normalize=norm, method=norm_method)

        # Simple chi2
        bhm_grid_chisquare_old = chi_squared(obs_flux, bhm_grid_values, error=errors)

        # Applying arbitrary scalar normalization to continuum
        bhm_norm_grid_values, arb_norm = arbitrary_rescale(bhm_grid_values,
                                                           *simulators.sim_grid["arb_norm"])

        # Calculate Chi-squared
        obs_flux = np.expand_dims(obs_flux, -1)  # expand on last axis to match rescale
        bhm_norm_grid_chisquare = chi_squared(obs_flux, bhm_norm_grid_values, error=errors)

        # Take minimum chi-squared value along Arbitrary normalization axis
        bhm_grid_chisquare, arbitrary_norms = arbitrary_minimums(bhm_norm_grid_chisquare, arb_norm)

        assert np.any(
            bhm_grid_chisquare_old >= bhm_grid_chisquare), "All chi2 values are not better or same with arbitrary scaling"

        # Interpolate to obs
        mod_spec.spline_interpolate_to(obs_spec)
        org_model_chi_val = chi_squared(obs_spec.flux, mod_spec.flux)

        model_chisqr_vals[ii] = org_model_chi_val  # This is gamma = 0 version

        # New parameters to explore
        bhm_grid_chisqr_vals[ii] = bhm_grid_chisquare[np.argmin(bhm_grid_chisquare)]
        bhm_grid_gamma[ii] = gammas[np.argmin(bhm_grid_chisquare)]
        full_bhm_grid_chisquare[ii, :] = bhm_grid_chisquare

        ################
        #  Find cross correlation RV
        # Should run though all models and find best rv to apply uniformly
        rvoffset, cc_max = xcorr_peak(obs_spec, mod_spec, plot=False)
        if verbose:
            print("Cross correlation RV = {}".format(rvoffset))
            print("Cross correlation max = {}".format(cc_max))

        model_xcorr_vals[ii] = cc_max
        model_xcorr_rv_vals[ii] = rvoffset
        ###################

        npix = obs_flux.shape[0]
        # print("bhm shape", bhm_grid_chisquare.shape)
        save_full_bhm_chisqr(save_name, params, gammas, bhm_grid_chisquare, arbitrary_norms,
                             npix, rvoffset)

    return (model_chisqr_vals, model_xcorr_vals, model_xcorr_rv_vals,
            bhm_grid_chisqr_vals, bhm_grid_gamma, full_bhm_grid_chisquare)