MassFunction

class hmf.core.mass_function.MassFunction(*, linear_power: LinearPower, filter: object = NOTHING, mass_accuracy: MassAccuracy = NOTHING, truncation_rtol=0.001, fit: object = NOTHING, delta_c=1.686, domain_policy: Literal['ignore', 'warn', 'mask', 'raise'] = 'ignore', mass_definition: MassDefinition | str | None = None, mass_conversion: object | None = None)[source]

The halo mass function and the quantities it is built from, as functions of (m, z).

See the module documentation. build() builds one with hmf 3.x-like defaults.

Examples

>>> import numpy as np
>>> from hmf.core.units import Msun_h
>>> mf = MassFunction.build(**{"transfer.model": "EH"})
>>> m = np.logspace(10, 15, 6) * Msun_h
>>> mf.dndm(m=m[None, :], z=np.array([0.0, 1.0])[:, None]).shape
(2, 6)
>>> mf2 = mf.evolve(sigma_8=0.75)
>>> mf2.variance is mf.variance
True
>>> MassFunction.invalidated_by("sigma_8")
('', 'linear_power')
Parameters:
  • linear_power (hmf.core.linear_power.LinearPower) – The linear power: sigma_8 normalisation and growth.

  • filter (hmf.core.filters.Filter, default computed) – The smoothing filter of the mass variance: a Filter instance, or its alias.

  • mass_accuracy (hmf.core.accuracy.MassAccuracy, default computed) – The settings of the mass variance’s mass lattice.

  • truncation_rtol (float, default 0.001) – The largest bound on the relative error in sigma or its slope, from truncating the mass variance’s k integrals, accepted for a mass.

  • fit (hmf.core.fits.FittingFunction, default computed) – The fitting function: an instance, a class or a registered name (e.g. ‘ST’).

  • delta_c (float, default 1.686) – The critical overdensity for collapse, linearly extrapolated to z = 0.

  • domain_policy (Literal['ignore', 'warn', 'mask', 'raise'], default 'ignore') – What to do with masses and redshifts outside the fit’s calibration domain: ‘ignore’, ‘warn’ (once per stage), ‘mask’ (NaN) or ‘raise’.

  • mass_definition (hmf.core.mass_definitions.MassDefinition | None, default None) – The mass definition of the mass function’s masses: a MassDefinition or its name (e.g. ‘200c’). None: the fit’s measured definition (an SOGeneric’s preferred one).

  • mass_conversion (hmf.core.mass_conversion.MassConversion | None, default None) – How to convert masses from mass_definition to the fit’s measured definition, when that does not accept it (e.g. ProfileConversion()); None: no conversion.

Attributes

linear_power: LinearPower
filter: Filter
mass_accuracy: MassAccuracy
truncation_rtol: float
fit: FittingFunction
delta_c: float
domain_policy: Literal['ignore', 'warn', 'mask', 'raise']
mass_definition: MassDefinition | None
mass_conversion: MassConversion | None
variance
rho_mean0
resolved_mass_definition
converts_masses
fit_mass_definition
m_top
valid_domain
computed_defaults_inputs: ClassVar[tuple[str, ...]] = ('linear_power.transfer.cosmology',)

The dotted paths, below the stage, of the parameters its computed_defaults() are computed from (e.g. the cosmology, for sigma_8 and n_s). evolve() computes the defaults again when one of them changes. A parameter the hook only checks for (whether it is given) is not one of them. A stage that defines computed_defaults() must declare at least one: its routing table raises a TypeError otherwise.

cosmology

The cosmology (the linear power’s).

qualified_parameters: ClassVar[frozenset[str]] = frozenset({})

in a tree holding this stage, parameter_names() lists them with the name of the stage field holding it ("transfer.model"). Any unique name still routes.

Type:

Fields whose names are too generic to stand alone (e.g. model)

shared_parameters: ClassVar[frozenset[str]] = frozenset({})

every field of that name below this stage (e.g. a cosmology held by two stages it composes) is set together by the flat name, so that they stay equal. The stage that composes the others declares it, whatever the stages it holds declare.

Type:

Names that are one parameter in this stage’s tree

Methods

at(*, z: float, m: Quantity | None = None) → MassFunctionView[source]

The quantities of the mass function at one redshift and fixed masses.

Parameters:
  • z – The redshift, a scalar.

  • m – The masses, a 1D Quantity in Msun/h (or Msun). By default, the nodes of the variance’s lattice from 10**log10_m_min to m_top.

Returns:

Its attributes are cached arrays at z and m.

Return type:

MassFunctionView

Raises:
  • DomainError – If z or a mass is outside valid_domain.

  • ValueError – If z is not a scalar, or m is not 1D.

classmethod build(**parameters: Any) → MassFunction[source]

Build the whole stage tree from parameters by name, with hmf 3.x-like defaults.

The same as from_flat(), whose names are those of evolve(): the flat names below, dotted paths ("linear_power.transfer.n_s") or unique suffixes of them ("transfer.model"), dotted paths into model fields ("fit.A") or nested mappings. Models may be given as instances, classes or registered names. The growth and the mass variance are built from the transfer stage, with its cosmology, k_accuracy and disk cache, so a CAMB or CLASS growth model shares the transfer’s Boltzmann run.

Parameters:
  • cosmology – The astropy cosmology (default Planck18).

  • transfer.model – The transfer model (default CAMB); also model while it is unique.

  • growth_model – The growth model (default the growth ODE).

  • n_s – The spectral index; by default the cosmology’s (cosmology.meta["n"], which astropy’s realisations give: 0.9665 for Planck18).

  • sigma_8 – The rms of the sigma_8_species field at 8 Mpc/h today; by default the cosmology’s (cosmology.meta["sigma8"]: 0.8102 for Planck18).

  • species – The matter species of the power: "cb" (CDM + baryons, default) or "tot".

  • sigma_8_species – The matter species sigma_8 is the rms of: "tot" (default, as hmf 3.x) or "cb".

  • fit – The fitting function (default Tinker08).

  • filter – The filter of the mass variance (default TopHat).

  • delta_c – The critical overdensity (default 1.686).

  • domain_policy – The calibration-domain policy (default "ignore").

  • mass_definition – The mass definition of the masses (default None: the fit’s own).

  • mass_conversion – The conversion of masses to the fit’s definition, needed when that does not accept mass_definition (default None: no conversion).

  • k_accuracy – The wavenumber accuracy (default KAccuracy()).

  • mass_accuracy – The mass lattice’s accuracy (default MassAccuracy()).

  • truncation_rtol – The mass variance’s tolerance for truncating its k integrals (default 1e-3).

  • disk_cache – Where to cache Boltzmann-code output on disk (see Transfer.disk_cache).

Return type:

MassFunction

Raises:
  • TypeError – For a name that is not a parameter (with suggestions), or is ambiguous.

  • ValueError – If n_s or sigma_8 is not given and the cosmology’s meta does not have it, or for any invalid argument.

classmethod computed_defaults(given: GivenParameters) → Mapping[str, Any][source]

The defaults of n_s and sigma_8: the cosmology’s (see build()).

They are computed from the cosmology alone (its computed_defaults_inputs), so evolve() computes them again whenever it changes the cosmology (or a stage holding it): mf.evolve(cosmology=c) takes both from c, and mf.evolve(cosmology=c, sigma_8=0.8) takes only n_s from it.

Parameters:

given – The parameters given, by dotted path.

Returns:

n_s and sigma_8, from cosmology.meta.

Return type:

Mapping

Raises:

ValueError – If one of them is not given and the cosmology’s meta does not have it (the message names it, and asks to set it explicitly).

dlnsigma_dlnm(*, m: Any, z: Any) → ndarray[tuple[Any, ...], dtype[float64]][source]

The slope dln(sigma)/dln(m), the same at every z.

Parameters:
  • m (Quantity) – Masses, in Msun/h (or Msun).

  • z (float or array_like) – Redshifts, broadcast with m (they set the shape only; they are checked).

Returns:

Dimensionless, with the broadcast shape.

Return type:

numpy.float64 or numpy.ndarray

Raises:

DomainError – As sigma().

dndlnm(*, m: Any, z: Any) → ndarray[tuple[Any, ...], dtype[float64]][source]

The mass function per unit ln m, m dn/dm.

Parameters:
Returns:

In h^3 / Mpc^3, with the broadcast shape.

Return type:

Quantity

Raises:

DomainError – As fsigma().

Warns:

HMFExtrapolationWarning – As fsigma().

dndlog10m(*, m: Any, z: Any) → ndarray[tuple[Any, ...], dtype[float64]][source]

The mass function per unit log10 m, ln(10) m dn/dm.

Parameters:
Returns:

In h^3 / Mpc^3, with the broadcast shape.

Return type:

Quantity

Raises:

DomainError – As fsigma().

Warns:

HMFExtrapolationWarning – As fsigma().

dndm(*, m: Any, z: Any) → ndarray[tuple[Any, ...], dtype[float64]][source]

The mass function dn/dm at (m, z).

Parameters:
Returns:

In h^4 / (Msun Mpc^3), with the broadcast shape.

Return type:

Quantity

Raises:

DomainError – As fsigma().

Warns:

HMFExtrapolationWarning – As fsigma().

dndm_kernel(*, m: ArrayLike, z: ArrayLike) → ndarray[tuple[Any, ...], dtype[float64]][source]

The mass function dn/dm at (m, z), at kernel level: see dndm().

Parameters:
  • m – Masses in Msun/h: a plain array.

  • z – Redshifts, broadcast with m.

Returns:

dn/dm in h^4 / (Msun Mpc^3), with the broadcast shape.

Return type:

numpy.ndarray

Raises:

DomainError – As fsigma_kernel() (and, for a fit that uses n(>m), as ngtm_kernel()).

evolve(**changes: Any) → Self

Return a copy of this stage tree with some parameters changed.

Parameters are named by their flat name, a shared name, their dotted path or a dotted suffix of it unique in the tree (see hmf.core.routing and parameter_names()); a dotted path into a model field ("fit.A") changes that field of the model, and a stage’s dotted path replaces that stage. Only the stages on the path from each changed field to this one are rebuilt, each by its evolve_own(); every other stage is shared, with its cached results, and so is every derived_property() whose inputs do not change.

A change to a parameter that a stage’s defaults are computed from (its computed_defaults_inputs, e.g. a mass function’s cosmology) computes them again: every parameter that computed_defaults() gives a default for takes the new default, unless it is given in the same call. So mf.evolve(cosmology=Planck15) takes sigma_8 and n_s from Planck15, and mf.evolve(cosmology=Planck15, sigma_8=0.8) keeps sigma_8 = 0.8. Other changes compute no default again.

Every name is checked before anything is built. The new stages are built, and so validated, by their constructors; if that fails, the error propagates and this stage, which is immutable, is unchanged.

Parameters:

**changes – New values of parameters, by name or dotted path.

Returns:

A new stage of the same class (this one, if nothing changes).

Return type:

Stage

Raises:
  • TypeError – If a name is not a parameter (with suggestions), is ambiguous (with the dotted paths to choose from), is a derived property, or is given twice.

  • ValueError – If a default computed again cannot be computed from the new parameters (e.g. the new cosmology does not give sigma_8, and sigma_8 is not given), or for an invalid value.

evolve_own(**changes: Any) → Self

Return a copy of this stage with some of its own fields changed.

No routing: each name is a field of this stage, and nothing else changes. It is the step evolve() takes at each stage it rebuilds. The value of each derived_property() already computed is carried to the new stage if none of its inputs changes; every other cached result is computed again.

Parameters:

**changes – New values of fields, by name.

Returns:

A new stage of the same class.

Return type:

Stage

Raises:

TypeError – If a name is not a field of this stage (with suggestions).

classmethod fields_info() → tuple[FieldInfo, ...]

Describe this class’s fields, without creating an instance.

Returns:

The name, type, default and documentation of each field.

Return type:

tuple of FieldInfo

classmethod from_flat(mapping: Mapping[str, Any]) → Self

Build the stage tree from parameters by name.

Names are those of evolve(); a mapping value of a stage’s dotted path ({"linear_power": {"sigma_8": 0.8}}) gives the fields below that stage. Every stage of the tree that is not given is built from its parameters, and every derived field is computed. Parameters not given take their field’s default, or the one computed_defaults() gives.

Parameters:

mapping – Values of parameters, by name or dotted path, or nested mappings.

Return type:

Stage

Raises:

TypeError – For an unknown, ambiguous, derived or repeated name, or a required parameter that is not given.

fsigma(*, m: Any, z: Any) → ndarray[tuple[Any, ...], dtype[float64]][source]

The fit’s multiplicity function f(sigma) at (m, z).

Parameters:
  • m (Quantity) – Masses, in Msun/h (or Msun).

  • z (float or array_like) – Redshifts, broadcast with m.

Returns:

Dimensionless, with the broadcast shape; NaN outside the calibration domain with domain_policy="mask".

Return type:

numpy.float64 or numpy.ndarray

Raises:

DomainError – As sigma(), outside the fit’s valid domain, or outside its calibration domain with domain_policy="raise".

Warns:

HMFExtrapolationWarning – Outside the calibration domain with domain_policy="warn" (once per stage).

fsigma_kernel(*, m: ArrayLike, z: ArrayLike) → ndarray[tuple[Any, ...], dtype[float64]][source]

f(sigma) at (m, z), with the domain policy, at kernel level: see fsigma().

Parameters:
  • m – Masses in Msun/h: a plain array.

  • z – Redshifts, broadcast with m.

Returns:

f(sigma), dimensionless, with the broadcast shape (NaN outside the calibration domain with domain_policy="mask").

Return type:

numpy.ndarray

Raises:

DomainError – As ln_sigma_and_slope_kernel(), or outside the fit’s valid domain, or its calibration domain with domain_policy="raise".

in_calibration_domain(*, m: Any, z: Any) → ndarray[tuple[Any, ...], dtype[bool]][source]

Whether each (m, z) is inside the fit’s calibration domain.

Parameters:
  • m (Quantity) – Masses, in Msun/h (or Msun).

  • z (float or array_like) – Redshifts, broadcast with m.

Returns:

With the broadcast shape (a numpy bool for scalar inputs); True everywhere for a fit without a calibration domain.

Return type:

bool or numpy.ndarray of bool

Raises:

DomainError – As sigma().

in_calibration_domain_kernel(*, m: ArrayLike, z: ArrayLike) → ndarray[tuple[Any, ...], dtype[bool]][source]

Whether (m, z) is in the fit’s calibration domain, at kernel level.

Parameters:
  • m – Masses in Msun/h: a plain array.

  • z – Redshifts, broadcast with m.

Returns:

With the broadcast shape; all True if the fit has no calibration domain.

Return type:

numpy.ndarray of bool

Raises:

DomainError – As ln_sigma_and_slope_kernel().

classmethod invalidated_by(name: str) → tuple[str, ...]

The stages of the tree that a change of a parameter rebuilds.

The stage holding it and every stage above it, and the stages holding a derived field that is computed from it (and the stages above those), if the derived links hold. Every other stage is shared with the original tree. It is an upper bound: evolve() keeps a stage whose derived field does not change (e.g. after a disk_cache change, which is not part of any stage’s value).

Parameters:

name – A parameter, by any name evolve() accepts.

Returns:

The dotted paths of the stages ("" for this one), from the top.

Return type:

tuple of str

Raises:

TypeError – If the name is not a parameter, or is ambiguous.

ln_sigma_and_slope_kernel(*, m: ArrayLike, z: ArrayLike) → tuple[ndarray[tuple[Any, ...], dtype[float64]], ndarray[tuple[Any, ...], dtype[float64]]][source]

Ln sigma(m, z) and dln(sigma)/dln(m), at kernel level.

For library code working in plain arrays (see hmf.core._kernels). It checks m as MassVariance.ln_sigma_and_slope_kernel and z as LinearPower.sigma_scale_kernel do.

Parameters:
  • m – Masses in Msun/h: a plain array.

  • z – Redshifts (dimensionless), broadcast with m like numpy.

Returns:

ln_sigma, dlnsigma_dlnm – Dimensionless, with the broadcast shape of m and z.

Return type:

numpy.ndarray

Raises:

DomainError – If a mass or redshift is outside its domain (from their extent), or a mass can’t be resolved by the variance’s k grid.

m_from_peak_height(*, peak_height: Any, z: Any) → ndarray[tuple[Any, ...], dtype[float64]][source]

The mass of a peak height nu at z: the inverse of peak_height().

Parameters:
  • peak_height (float or array_like) – Values of nu = delta_c / sigma, dimensionless.

  • z (float or array_like) – Redshifts, broadcast with peak_height.

Returns:

Masses, in Msun/h, with the broadcast shape.

Return type:

Quantity

Raises:

DomainError – If a value is not finite and > 0, or as m_from_sigma() for sigma = delta_c / nu.

m_from_radius(*, r: Any) → ndarray[tuple[Any, ...], dtype[float64]][source]

The mass of a filter radius, m = (4π/3) rho_cb (cR)³, with the filter’s c.

See MassVariance.m_from_radius; it does not depend on z.

Parameters:

r (Quantity) – Radii, in Mpc/h (or Mpc).

Returns:

Masses, in Msun/h.

Return type:

Quantity

Raises:

DomainError – If a radius is not finite and > 0.

m_from_sigma(*, sigma: Any, z: Any) → ndarray[tuple[Any, ...], dtype[float64]][source]

The mass at which sigma(m, z) takes the given values: the inverse of sigma().

sigma(m, z) / (sqrt(A) D(z)) is inverted on the variance’s lattice (see MassVariance.m_from_sigma), so m_from_sigma(sigma=sigma(m=m, z=z), z=z) returns m to about 1e-12.

Parameters:
  • sigma (float or array_like) – Values of sigma, dimensionless.

  • z (float or array_like) – Redshifts, broadcast with sigma.

Returns:

Masses, in Msun/h, with the broadcast shape.

Return type:

Quantity

Raises:

DomainError – If a value is not finite and > 0, is outside the range of sigma(m, z) on the variance’s default lattice range, or sigma is not monotonic there.

m_from_sigma_kernel(*, sigma: ArrayLike, z: ArrayLike) → ndarray[tuple[Any, ...], dtype[float64]][source]

The mass at which sigma(m, z) takes the given values, at kernel level.

Parameters:
  • sigma – Values of sigma(m, z), dimensionless: a plain array.

  • z – Redshifts, broadcast with sigma.

Returns:

Masses in Msun/h, with the broadcast shape.

Return type:

numpy.ndarray

Raises:

DomainError – As MassVariance.m_from_sigma_kernel, for sigma(m, z) / (sqrt(A) D(z)), or if a z is outside its domain.

ngtm(*, m: Any, z: Any) → ndarray[tuple[Any, ...], dtype[float64]][source]

The cumulative mass function: the number density of haloes above m.

Integrated on the mass lattice up to m_top (see “The cumulative mass function” in the module documentation): bit-for-bit the same for a mass whatever else is asked for with it.

Parameters:
Returns:

In h^3 / Mpc^3, with the broadcast shape; NaN at (m, z) outside the calibration domain with domain_policy="mask".

Return type:

Quantity

Raises:

DomainError – If a mass is above m_top, or as fsigma() (the policy applies at (m, z)).

Warns:

HMFExtrapolationWarning – As fsigma().

ngtm_kernel(*, m: ArrayLike, z: ArrayLike) → ndarray[tuple[Any, ...], dtype[float64]][source]

The cumulative mass function n(>m) at (m, z), at kernel level: see ngtm().

Parameters:
  • m – Masses in Msun/h: a plain array (at most m_top).

  • z – Redshifts, broadcast with m.

Returns:

n(>m) in h^3 / Mpc^3, with the broadcast shape.

Return type:

numpy.ndarray

Raises:

DomainError – If a mass is above m_top, or as fsigma_kernel().

classmethod parameter_defaults() → dict[str, Any]

The default of each parameter that has one, without creating a stage.

A default computed by a factory that does not depend on the stage is called (it gives a model or a setting, e.g. KAccuracy()), and the computed_defaults() of the stages of the tree, from no given parameters, are included. from_flat(parameter_defaults()) equals from_flat({}).

Return type:

dict

classmethod parameter_info() → dict[str, ParameterInfo]

Describe every parameter of the stage tree, without creating a stage.

Returns:

A ParameterInfo (dotted paths, type, default, doc, whether it is shared) by the name the parameter is routed by.

Return type:

dict

classmethod parameter_names() → tuple[str, ...]

The names of the parameters of the stage tree (see parameter_info()).

Return type:

tuple of str

peak_height(*, m: Any, z: Any) → ndarray[tuple[Any, ...], dtype[float64]][source]

The peak height, nu = delta_c / sigma(m, z).

Parameters:
  • m (Quantity) – Masses, in Msun/h (or Msun).

  • z (float or array_like) – Redshifts, broadcast with m.

Returns:

Dimensionless, with the broadcast shape.

Return type:

numpy.float64 or numpy.ndarray

Raises:

DomainError – As sigma().

classmethod quantities_available() → dict[str, tuple[str, ...]]

The public outputs of each stage of the tree.

From the classes alone: each stage’s unit_boundary() methods (e.g. dndm) and its public properties and cached properties. Other methods (e.g. MassFunction.at, which gives a view, and the plain-array ..._kernel entry points for library code), fields, private names and constants are left out.

Returns:

The names, by the dotted path of the stage ("" for this one).

Return type:

dict

rho_gtm(*, m: Any, z: Any) → ndarray[tuple[Any, ...], dtype[float64]][source]

The mass density in haloes above m (comoving).

Integrated as ngtm().

Parameters:
Returns:

In Msun h^2 / Mpc^3, with the broadcast shape.

Return type:

Quantity

Raises:

DomainError – As ngtm().

Warns:

HMFExtrapolationWarning – As fsigma().

rho_gtm_kernel(*, m: ArrayLike, z: ArrayLike) → ndarray[tuple[Any, ...], dtype[float64]][source]

The mass density in haloes above m at z, at kernel level: see rho_gtm().

Parameters:
  • m – Masses in Msun/h: a plain array (at most m_top).

  • z – Redshifts, broadcast with m.

Returns:

rho(>m) in Msun h^2 / Mpc^3, with the broadcast shape.

Return type:

numpy.ndarray

Raises:

DomainError – As ngtm_kernel().

sigma(*, m: Any, z: Any) → ndarray[tuple[Any, ...], dtype[float64]][source]

The mass variance sigma(m, z) of the linear power, normalised to sigma_8.

Parameters:
  • m (Quantity) – Masses, in Msun/h (or Msun, converted with the cosmology’s H0).

  • z (float or array_like) – Redshifts, dimensionless, broadcast with m like numpy.

Returns:

Dimensionless, with the broadcast shape (a scalar for scalar inputs).

Return type:

numpy.float64 or numpy.ndarray

Raises:

DomainError – If a mass or redshift is outside valid_domain (or the growth’s table), or a mass can’t be resolved by the variance’s k grid.

to_flat() → dict[str, Any]

The value of every parameter of the stage tree, by the name it is routed by.

type(stage).from_flat(stage.to_flat()) equals the stage, and the keys are parameter_names().

Return type:

dict