Esempio n. 1
0
def test_mass_per_bin():
    x = np.linspace(0, 100, 101)
    y = np.zeros(101) + 1

    def sfh_func(i):
        return np.interp(i, x, y)

    mass_per_bin = utils.mass_per_bin(sfh_func, np.linspace(0, 10, 11))
    assert np.allclose(mass_per_bin, np.zeros(10) + 1),\
        "mass_per_bin calculation wrong."
Esempio n. 2
0
def test_mass_per_bin_vector_input():
    x = np.linspace(0, 100, 101)
    y = np.zeros(101) + 1

    def sfh_func(i):
        return np.zeros(len(i)) + 1

    mass_per_bin = utils.mass_per_bin(sfh_func, np.linspace(0, 10, 11))
    assert np.allclose(mass_per_bin, np.zeros(10) + 1),\
        "mass_per_bin calculation wrong."
Esempio n. 3
0
File: sfh.py Progetto: Krytic/hoki
    def mass_per_bin(self, time_edges, sample_rate=25):
        """
        Gives the mass per bin for the given edges in time.

        Input
        -----
        time_edges : `numpy.ndarray`
            The edges of the bins in which the mass per bin is wanted in yrs.

        Output
        ------
        `numpy.ndarray`
            The mass per time bin given the time edges.
        """

        return mass_per_bin(self._sfh_calculator,
                            time_edges,
                            sample_rate=sample_rate)
Esempio n. 4
0
    def at_time(self, SFH, ZEH, event_type_list, t0, sample_rate=1000):
        """
        Calculates the event rates at lookback time `t0` for functions as input.

        Parameters
        ----------
        SFH : `python callable`,
               `hoki.csp.sfh.SFH`,
               `list(hoki.csp.sfh.SFH, )`,
               `list(callable, )`

            SFH can be the following things:
            - A python callable (function) which takes the lookback time and
              returns the stellar formation rate in units M_\\odot per yr at
              the given time.
            - A list of python callables with the above requirement.
            - A `hoki.csp.sfh.SFH` object.
            - A list of `hoki.csp.sfh.SFH` objects.

        ZEH : callable, `list(callable, )`

            ZEH can be the following things:
            - A python callable (function) which takes the lookback time and
              returns the metallicity at the given time.
            - A list of python callables with the above requirement.

        event_type_list : `list(str, )`
            A list of BPASS event types.
            The available types are:
            - Ia
            - IIP
            - II
            - Ib
            - Ic
            - LGRB
            - PISNe
            - low_mass

        t0 : `float`
            The moment in lookback time, where to calculate the the event rate

        sample_rate : `int`
            The number of samples to take from the SFH and metallicity evolutions.
            Default = 1000.
            If a negative value is given, the BPASS binning to calculate
            the event rates.

        Returns
        -------
        `numpy.ndarray` (N, M) [nr_sfh, nr_event_types]
            Returns a `numpy.ndarray` containing the event rates for each sfh
            and metallicity pair (N) and event type (M) at the requested lookback time.
            Usage: event_rates[1]["Ia"]
            (Gives the Ia event rates for the second sfh and metallicity history pair)
        """

        SFH, ZEH = self._type_check_histories(SFH, ZEH)

        if isinstance(event_type_list, type(list)):
            raise HokiFatalError(
                "event_type_list is not a list. Only a list is taken as input."
            )

        nr_events = len(event_type_list)
        nr_sfh = len(SFH)

        output_dtype = np.dtype([(i, np.float64) for i in event_type_list])
        event_rates = np.empty(nr_sfh, dtype=output_dtype)

        # Define time edges
        time_edges = BPASS_LINEAR_TIME_EDGES if sample_rate < 0 else np.linspace(
            0, self.now, sample_rate + 1)

        bpass_rates = self._numpy_bpass_rates[[
            BPASS_EVENT_TYPES.index(i) for i in event_type_list
        ]]

        mass_per_bin_list = np.array(
            [utils.mass_per_bin(i, t0 + time_edges) for i in SFH])
        metallicity_per_bin_list = np.array(
            [utils.metallicity_per_bin(i, t0 + time_edges) for i in ZEH])

        for counter, (mass_per_bin, Z_per_bin) in enumerate(
                zip(mass_per_bin_list, metallicity_per_bin_list)):
            for count in range(nr_events):

                event_rates[counter][count] = utils._at_time(
                    Z_per_bin, mass_per_bin, time_edges, bpass_rates[count])

        return event_rates
Esempio n. 5
0
    def over_time(self,
                  SFH,
                  ZEH,
                  event_type_list,
                  nr_time_bins,
                  return_time_edges=False):
        """
        Calculates the event rates over lookback time for functions as input.

        Parameters
        ----------
        SFH : `python callable`,
              `hoki.csp.sfh.SFH`,
              `list(hoki.csp.sfh.SFH, )`,
              `list(callable, )`

            SFH can be the following things:
            - A python callable (function) which takes the lookback time and
              returns the stellar formation rate in units M_\\odot per yr at
              the given time.
            - A list of python callables with the above requirement.
            - A `hoki.csp.sfh.SFH` object.
            - A list of `hoki.csp.sfh.SFH` objects.

        ZEH : callable, `list(callable, )`

            ZEH can be the following thins:
            - A python callable (function) which takes the lookback time and
              returns the metallicity at the given time.
            - A list of python callables with the above requirement.

        event_type_list : `list(str, )`
            A list of BPASS event types.

            The available types are:
            - Ia
            - IIP
            - II
            - Ib
            - Ic
            - LGRB
            - PISNe
            - low_mass

        nr_time_bins : `int`
            The number of bins to split the lookback time into.

        return_time_edges : `bool`
            If `True`, also returns the edges of the lookback time bins.
            Default=False

        Returns
        -------
        `numpy.ndarray` (nr_sfh, nr_event_types, nr_time_bins),
                        ((nr_sfh, nr_event_types, nr_time_bins), nr_time_bins)

            The event rates in a 3D matrix with sides, the number of SFH-Z pairs,
            the number of events selected, and the number of time bins choosen.

            If `return_time_edges=False`, returns a `numpy.ndarray` containing the event
            rates.
            Usage: event_rates[1]["Ia"][10]
                (Gives the Ia event rates in bin number 11 for the second
                sfh and metallicity history pair)

            If `return_time_edges=True`, returns a numpy array containing the event
            rates and the edges, eg. `out[0]=event_rates` `out[1]=time_edges`.
        """

        # check and transform the input to the righ type
        SFH, ZEH = self._type_check_histories(SFH, ZEH)

        if isinstance(event_type_list, type(list)):
            raise HokiFatalError(
                "event_type_list is not a list. Only a list is taken as input."
            )

        nr_events = len(event_type_list)
        nr_sfh = len(SFH)

        output_dtype = np.dtype([(i, np.float64, nr_time_bins)
                                 for i in event_type_list])
        event_rates = np.empty(nr_sfh, dtype=output_dtype)

        time_edges = np.linspace(0, self.now, nr_time_bins + 1)

        bpass_rates = self._numpy_bpass_rates[[
            BPASS_EVENT_TYPES.index(i) for i in event_type_list
        ]]

        mass_per_bin_list = np.array(
            [utils.mass_per_bin(i, time_edges) for i in SFH])
        metallicity_per_bin_list = np.array(
            [utils.metallicity_per_bin(i, time_edges) for i in ZEH])

        for counter, (mass_per_bin, Z_per_bin) in enumerate(
                zip(mass_per_bin_list, metallicity_per_bin_list)):
            for count in range(nr_events):
                event_rate = utils._over_time(Z_per_bin, mass_per_bin,
                                              time_edges, bpass_rates[count])
                event_rates[counter][count] = event_rate / np.diff(time_edges)

        if return_time_edges:
            return np.array([event_rates, time_edges], dtype=object)
        else:
            return event_rates