Transfer

class hmf.core.transfer.Transfer(*, cosmology: FLRW = FlatLambdaCDM(name='Planck18', H0=<Quantity 67.66 km / (Mpc s)>, Om0=0.30966, Tcmb0=<Quantity 2.7255 K>, Neff=3.046, m_nu=<Quantity [0., 0., 0.06] eV>, Ob0=0.04897), model: object = NOTHING, n_s=0.9665, k_accuracy: KAccuracy = NOTHING, disk_cache: DiskCache | bool | str | os.PathLike[str] | None = None)[source]

The transfer function and the shape of the linear power spectrum at z = 0.

See the module documentation.

Examples

>>> from hmf.core.transfer import Transfer
>>> from hmf.core.units import h_Mpc
>>> t = Transfer(model="EH_NoBAO")
>>> float(round(t.transfer_function(1e-7 * h_Mpc), 6))
1.0
Parameters:
  • cosmology (FLRW, default FlatLambdaCDM(name='Planck18', H0=<Quantity 67.66 km / (Mpc s)>, Om0=0.30966, Tcmb0=<Quantity 2.7255 K>, Neff=3.046, m_nu=<Quantity [0. , 0. , 0.06] eV>, Ob0=0.04897)) – The cosmology, an astropy FLRW. It is compared by its class and parameter values (not its name or metadata).

  • model (hmf.core.transfer_models.TransferModel, default computed) – The transfer model: an instance, a class or a registered name (e.g. ‘EH’).

  • n_s (float, default 0.9665) – The spectral index of the primordial power spectrum (default: Planck18).

  • k_accuracy (hmf.core.accuracy.KAccuracy, default computed) – The wavenumber accuracy; it sets the sampling of the Boltzmann codes.

  • disk_cache (hmf.core.cache.DiskCache | None, default None) – Where to cache the Boltzmann code’s output on disk: a DiskCache, True (the default directory), a directory, or None (no disk cache). It does not change any result, so it is not part of the stage’s value.

Attributes

model: TransferModel
n_s: float
k_accuracy: KAccuracy
disk_cache: DiskCache | None
solution
boltzmann_run

The Boltzmann run behind the solution, if the model has one.

computed_defaults_inputs: ClassVar[tuple[str, ...]] = ()

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: FLRW
k_max_table

The largest tabulated wavenumber, above which T is extrapolated.

A Quantity in h/Mpc; None for a model that is not a table (a fitting formula).

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

In a tree, the transfer model is listed as "transfer.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

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

Defaults of from_flat() that are computed from the given parameters.

A hook for stages whose defaults depend on other parameters (e.g. sigma_8 from the cosmology); the base stage has none. Defaults for parameters (or below stages) that are given are ignored.

A stage that defines it must list the parameters its defaults are computed from in computed_defaults_inputs (a TypeError is raised when its routing table is first built if it lists none). evolve() calls the hook again when one of them changes, with the parameters changed in that call and the new values of the inputs, and every parameter it gives a default for, and that the call does not give, takes that default. So the hook must compute its defaults from its inputs alone, and give a default for each parameter it computes whenever that parameter is not given. undeclared_inputs() checks, in a test, that it reads no other parameter.

Parameters:

given – The parameters given, by dotted path.

Returns:

Values by name or dotted path.

Return type:

Mapping

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.

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.

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

power_source(species: str = 'cb') → UnnormalisedPower[source]

The kernel-level shape of the power spectrum of species, a PowerSource.

For library code working in canonical units (see hmf.core._kernels), e.g. as the power of a MassVariance.

Parameters:

species – "cb" (CDM + baryons) or "tot" (total matter).

Returns:

\(k^{n_s} T(k)^2\) of species on plain arrays (k in h/Mpc), with the mean density of CDM + baryons and this stage’s units context.

Return type:

UnnormalisedPower

Raises:

ValueError – If species is not a matter species.

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

same_cosmology(other: CosmologyStage) → bool

Whether other has the same cosmology as this stage.

Cosmologies are compared by class and parameter values (see cosmology_key()), not by name or metadata.

Parameters:

other – Another stage with a cosmology.

Return type:

bool

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

transfer_function(k: Any, species: str = 'cb') → Any[source]

The transfer function T(k) at z = 0, normalised to 1 as k -> 0.

Parameters:
  • k – Wavenumbers: a Quantity in h/Mpc, or in 1/Mpc (converted with the cosmology’s H0).

  • species – "cb" (CDM + baryons) or "tot" (total matter).

Returns:

Dimensionless, with the shape of k (a scalar for a scalar k).

Return type:

numpy.float64 or numpy.ndarray

Raises:

DomainError – If a k is outside the model’s valid domain (k > 0).

Warns:

HMFExtrapolationWarning – If a k is outside a table the user supplied (once per stage).

unnormalised_power(k: Any, species: str = 'cb') → Any[source]

The shape of the linear power spectrum at z = 0, \(k^{n_s} T(k)^2\).

Parameters:
  • k – Wavenumbers: a Quantity in h/Mpc, or in 1/Mpc.

  • species – "cb" or "tot".

Returns:

A plain, dimensionless array (not a Quantity), with the shape of k (a scalar for a scalar k): \(k^{n_s} T(k)^2\) with k in h/Mpc, so its amplitude is arbitrary. LinearPower normalises it (to sigma_8) and gives it the units of a power spectrum, (Mpc/h)³.

Return type:

numpy.float64 or numpy.ndarray

Raises:

DomainError – As for transfer_function().

Warns:

HMFExtrapolationWarning – As for transfer_function().