Example #1
0
    def __call__(self, x):
        """
        Weights based on input model choice

        Parameters
        ----------
        x : float
            values for model evaluation
        """
        # sort the initial mass along this isochrone
        sindxs = np.argsort(x)

        # Compute the mass bin boundaries
        mass_bounds = compute_bin_boundaries(x[sindxs])

        # integrate the IMF over each bin
        if self.model["name"] == "kroupa":
            imf_func = pmfuncs._imf_kroupa
        elif self.model["name"] == "salpeter":
            imf_func = pmfuncs._imf_salpeter
        elif self.model["name"] == "flat":
            imf_func = pmfuncs._imf_flat

        # calculate the average prior in each mass bin
        mass_weights = np.zeros(len(x))
        for i, cindx in enumerate(sindxs):
            # fmt: off
            mass_weights[cindx] = (quad(imf_func, mass_bounds[i], mass_bounds[i + 1]))[0]
            # fmt: on
            mass_weights[cindx] /= mass_bounds[i + 1] - mass_bounds[i]

        # normalize to avoid numerical issues (too small or too large)
        mass_weights /= np.average(mass_weights)

        return mass_weights
def test_bin_boundaries():
    """
    Test bin boundaries
    """
    bin_centers = np.array([1, 2, 5, 10, 50])
    weights = compute_bin_boundaries(bin_centers)
    expected_weights = [0.5, 1.5, 3.5, 7.5, 30.0, 70.0]
    np.testing.assert_allclose(
        weights, expected_weights, err_msg=("Stellar bin boundaries error")
    )
Example #3
0
    def __call__(self, x):
        """
        Weights based on input model choice

        Parameters
        ----------
        x : float
            values for model evaluation
        """
        # sort the initial mass along this isochrone
        sindxs = np.argsort(x)

        # Compute the mass bin boundaries
        mass_bounds = compute_bin_boundaries(x[sindxs])

        # integrate the IMF over each bin
        args = None
        if self.model["name"] == "kroupa":
            if "alpha0" in self.model.keys(
            ):  # assume other alphas also present
                args = (
                    self.model["alpha0"],
                    self.model["alpha1"],
                    self.model["alpha2"],
                    self.model["alpha3"],
                )
            imf_func = pmfuncs._imf_kroupa
        elif self.model["name"] == "salpeter":
            if "slope" in self.model.keys():
                slope = self.model["slope"]
                args = (slope, )
            imf_func = pmfuncs._imf_salpeter
        elif self.model["name"] == "flat":
            imf_func = pmfuncs._imf_flat

        # calculate the average prior in each mass bin
        mass_weights = np.zeros(len(x))
        for i, cindx in enumerate(sindxs):
            # fmt: off
            if args is not None:
                mass_weights[cindx] = (quad(imf_func, mass_bounds[i],
                                            mass_bounds[i + 1], args))[0]
            else:
                mass_weights[cindx] = (quad(imf_func, mass_bounds[i],
                                            mass_bounds[i + 1]))[0]
            # fmt: on
            mass_weights[cindx] /= mass_bounds[i + 1] - mass_bounds[i]

        # normalize to avoid numerical issues (too small or too large)
        mass_weights /= np.average(mass_weights)

        return mass_weights
def compute_mass_prior_weights(masses, mass_prior_model):
    """
    Compute the mass prior for the specificed model

    Parameters
    ----------
    masses : numpy vector
        masses

    mass_prior_model: dict
        dict including prior model name and parameters

    Returns
    -------
    mass_weights : numpy vector
      Unnormalized IMF integral for each input mass
      integration is done between each bin's boundaries
    """

    # sort the initial mass along this isochrone
    sindxs = np.argsort(masses)

    # Compute the mass bin boundaries
    mass_bounds = compute_bin_boundaries(masses[sindxs])

    # compute the weights = mass bin widths
    mass_weights = np.empty(len(masses))

    # integrate the IMF over each bin
    if mass_prior_model["name"] == "kroupa":
        imf_func = imf_kroupa
    elif mass_prior_model["name"] == "salpeter":
        imf_func = imf_salpeter
    elif mass_prior_model["name"] == "flat":
        imf_func = imf_flat
    else:
        raise NotImplementedError("input mass prior function not supported")

    # calculate the average prior in each mass bin
    for i in range(len(masses)):
        mass_weights[sindxs[i]] = (quad(
            imf_func, mass_bounds[i],
            mass_bounds[i + 1]))[0] / (mass_bounds[i + 1] - mass_bounds[i])

    # normalize to avoid numerical issues (too small or too large)
    mass_weights /= np.average(mass_weights)

    return mass_weights
Example #5
0
    def __call__(self, x):
        """
        Weights based on input model choice

        Parameters
        ----------
        x : float
            values for model evaluation
        """
        if self.model["name"] == "flat_log":
            weights = 1.0 / np.diff(10**compute_bin_boundaries(x))
            return weights / np.sum(weights)
        elif self.model["name"] == "exponential":
            return pmfuncs._exponential(10.0**x, tau=self.model["tau"] * 1e9)
        else:
            return super().__call__(x)
Example #6
0
def gen_SimObs_from_sedgrid(
    sedgrid,
    sedgrid_noisemodel,
    nsim=100,
    compl_filter="F475W",
    complcut=None,
    magcut=None,
    ranseed=None,
    vega_fname=None,
    weight_to_use="weight",
    age_prior_model=None,
    mass_prior_model=None,
):
    """
    Generate simulated observations using the physics and observation grids.
    The priors are sampled as they give the ensemble model for the stellar
    and dust distributions (IMF, Av distribution etc.).
    The physics model gives the SEDs based on the priors.
    The observation model gives the noise, bias, and completeness all of
    which are used in simulating the observations.

    Currently written to only work for the toothpick noisemodel.

    Parameters
    ----------
    sedgrid: grid.SEDgrid instance
        model grid

    sedgrid_noisemodel: beast noisemodel instance
        noise model data

    nsim : int
        number of observations to simulate

    compl_filter : str
        Filter to use for completeness (required for toothpick model).
        Set to max to use the max value in all filters.

    complcut : float (defualt=None)
        completeness cut for only including model seds above the cut
        where the completeness cut ranges between 0 and 1.

    magcut : float (defualt=None)
        faint-end magnitude cut for only including model seds brighter
        than the given magnitude in compl_filter.

    ranseed : int
        used to set the seed to make the results reproducable,
        useful for testing

    vega_fname : string
        filename for the vega info, useful for testing

    weight_to_use : string (default='weight')
        Set to either 'weight' (prior+grid), 'prior_weight', 'grid_weight',
        or 'uniform' (this option is valid only when nsim is supplied) to
        choose the weighting for SED selection.

    age_prior_model : dict
        age prior model in the BEAST dictonary format

    mass_prior_model : dict
        mass prior model in the BEAST dictonary format

    Returns
    -------
    simtable : astropy Table
        table giving the simulated observed fluxes as well as the
        physics model parmaeters
    """
    n_models, n_filters = sedgrid.seds.shape
    flux = sedgrid.seds

    # get the vega fluxes for the filters
    _, vega_flux, _ = Vega(source=vega_fname).getFlux(sedgrid.filters)

    # cache the noisemodel values
    model_bias = sedgrid_noisemodel["bias"]
    model_unc = np.fabs(sedgrid_noisemodel["error"])
    model_compl = sedgrid_noisemodel["completeness"]

    # only use models that have non-zero completeness in all filters
    # zero completeness means the observation model is not defined for that filters/flux
    ast_defined = model_compl > 0.0
    sum_ast_defined = np.sum(ast_defined, axis=1)
    goodobsmod = sum_ast_defined >= n_filters

    # completeness from toothpick model so n band completeness values
    # require only 1 completeness value for each model
    # max picked to best "simulate" how the photometry detection is done
    if compl_filter.lower() == "max":
        model_compl = np.max(model_compl, axis=1)
    else:
        short_filters = [
            filter.split(sep="_")[-1].upper() for filter in sedgrid.filters
        ]
        if compl_filter.upper() not in short_filters:
            raise NotImplementedError(
                "Requested completeness filter not present:" +
                compl_filter.upper() + "\nPossible filters:" +
                "\n".join(short_filters))

        filter_k = short_filters.index(compl_filter.upper())
        print("Completeness from %s" % sedgrid.filters[filter_k])
        model_compl = model_compl[:, filter_k]

    # if complcut is provided, only use models above that completeness cut
    # in addition to the non-zero completeness criterion
    if complcut is not None:
        goodobsmod = (goodobsmod) & (model_compl >= complcut)

    # if magcut is provided, only use models brighter than the magnitude cut
    # in addition to the non-zero completeness criterion
    if magcut is not None:
        fluxcut_compl_filter = 10**(-0.4 * magcut) * vega_flux[filter_k]
        goodobsmod = (goodobsmod) & (flux[:, filter_k] >= fluxcut_compl_filter)

    # initialize the random number generator
    rangen = default_rng(ranseed)

    # if the age and mass prior models are given, use them to determine the
    # total number of stars to simulate
    model_indx = np.arange(n_models)
    if (age_prior_model is not None) and (mass_prior_model is not None):
        nsim = 0
        # logage_range = [min(sedgrid["logA"]), max(sedgrid["logA"])]
        mass_range = [min(sedgrid["M_ini"]), max(sedgrid["M_ini"])]

        # compute the total mass and average mass of a star given the mass_prior_model
        nmass = 100
        masspts = np.logspace(np.log10(mass_range[0]), np.log10(mass_range[1]),
                              nmass)
        massprior = compute_mass_prior_weights(masspts, mass_prior_model)
        totmass = np.trapz(massprior, masspts)
        avemass = np.trapz(masspts * massprior, masspts) / totmass

        # compute the mass of the remaining stars at each age and
        # simulate the stars assuming everything is complete
        gridweights = sedgrid[weight_to_use]
        gridweights = gridweights / np.sum(gridweights)

        grid_ages = np.unique(sedgrid["logA"])
        ageprior = compute_age_prior_weights(grid_ages, age_prior_model)
        bin_boundaries = compute_bin_boundaries(grid_ages)
        bin_widths = np.diff(10**(bin_boundaries))
        totsim_indx = np.array([], dtype=int)
        for cage, cwidth, cprior in zip(grid_ages, bin_widths, ageprior):
            gmods = sedgrid["logA"] == cage
            cur_mass_range = [
                min(sedgrid["M_ini"][gmods]),
                max(sedgrid["M_ini"][gmods]),
            ]
            gmass = (masspts >= cur_mass_range[0]) & (masspts <=
                                                      cur_mass_range[1])
            curmasspts = masspts[gmass]
            curmassprior = massprior[gmass]
            totcurmass = np.trapz(curmassprior, curmasspts)

            # compute the mass remaining at each age -> this is the mass to simulate
            simmass = cprior * cwidth * totcurmass / totmass
            nsim_curage = int(round(simmass / avemass))

            # simluate the stars at the current age
            curweights = gridweights[gmods]
            curweights /= np.sum(curweights)
            cursim_indx = rangen.choice(model_indx[gmods],
                                        size=nsim_curage,
                                        p=curweights)

            totsim_indx = np.concatenate((totsim_indx, cursim_indx))

            nsim += nsim_curage
            # totsimcurmass = np.sum(sedgrid["M_ini"][cursim_indx])
            # print(cage, totcurmass / totmass, simmass, totsimcurmass, nsim_curage)

        totsimmass = np.sum(sedgrid["M_ini"][totsim_indx])
        print(f"number total simulated stars = {nsim}; mass = {totsimmass}")
        compl_choice = rangen.random(nsim)
        compl_indx = model_compl[totsim_indx] >= compl_choice
        sim_indx = totsim_indx[compl_indx]
        totcompsimmass = np.sum(sedgrid["M_ini"][sim_indx])
        print(
            f"number of simulated stars w/ completeness = {len(sim_indx)}; mass = {totcompsimmass}"
        )

    else:  # total number of stars to simulate set by command line input

        if weight_to_use == "uniform":
            # sample to get the indices of the picked models
            sim_indx = rangen.choice(model_indx[goodobsmod], nsim)

        else:
            gridweights = sedgrid[weight_to_use][goodobsmod] * model_compl[
                goodobsmod]
            gridweights = gridweights / np.sum(gridweights)

            # sample to get the indexes of the picked models
            sim_indx = rangen.choice(model_indx[goodobsmod],
                                     size=nsim,
                                     p=gridweights)

        print(f"number of simulated stars = {nsim}")

    # setup the output table
    ot = Table()
    qnames = list(sedgrid.keys())
    # simulated data
    for k, filter in enumerate(sedgrid.filters):
        simflux_wbias = flux[sim_indx, k] + model_bias[sim_indx, k]

        simflux = rangen.normal(loc=simflux_wbias,
                                scale=model_unc[sim_indx, k])

        bname = filter.split(sep="_")[-1].upper()
        fluxname = f"{bname}_FLUX"
        colname = f"{bname}_RATE"
        magname = f"{bname}_VEGA"
        ot[fluxname] = Column(simflux)
        ot[colname] = Column(ot[fluxname] / vega_flux[k])
        pindxs = ot[colname] > 0.0
        nindxs = ot[colname] <= 0.0
        ot[magname] = Column(ot[colname])
        ot[magname][pindxs] = -2.5 * np.log10(ot[colname][pindxs])
        ot[magname][nindxs] = 99.999

        # add in the physical model values in a form similar to
        # the output simulated (physics+obs models) values
        # useful if using the simulated data to interpolate ASTs
        #   (e.g. for MATCH)
        fluxname = f"{bname}_INPUT_FLUX"
        ratename = f"{bname}_INPUT_RATE"
        magname = f"{bname}_INPUT_VEGA"
        ot[fluxname] = Column(flux[sim_indx, k])
        ot[ratename] = Column(ot[fluxname] / vega_flux[k])
        pindxs = ot[ratename] > 0.0
        nindxs = ot[ratename] <= 0.0
        ot[magname] = Column(ot[ratename])
        ot[magname][pindxs] = -2.5 * np.log10(ot[ratename][pindxs])
        ot[magname][nindxs] = 99.999

    # model parmaeters
    for qname in qnames:
        ot[qname] = Column(sedgrid[qname][sim_indx])

    return ot