hmf.core (experimental)¶
Warning
hmf.core is an experimental preview of the hmf 4.0 API. It may change,
without deprecation, before 4.0. Importing it emits a
HMFCoreExperimentalWarning (a FutureWarning),
which you can silence with:
import warnings
from hmf.exceptions import HMFCoreExperimentalWarning
warnings.simplefilter("ignore", HMFCoreExperimentalWarning)
The v3 API is unaffected, and import hmf does not import hmf.core.
The v4 core is developed alongside the v3 API, as the subpackage hmf.core
(see #396). At 4.0 it becomes
hmf. It contains the conventions the rest of the core is built on, and the
stages and models ported so far (below).
Conventions¶
- Units (
hmf.core.units) Every public input and output that has a dimension is an
astropy.units.Quantity; a bare number for a dimensional input raisesUnitBoundaryError. Outputs are in h-units, written explicitly withastropy.cosmology.units.littleh(e.g.Msun_h). Inputs may be in h-units or in physical units, which are converted with the object’s own H0. Dimensionless inputs and outputs (z, sigma, peak height, T(k), …) are plain numbers and arrays. Theunit_boundary()decorator implements this at a fixed cost of under 2 µs per call, and every conversion, inside or outside it, goes throughto_canonical(), so the errors are the same everywhere.H0. An object with a cosmology (a
Transfer, aTabulatedPowergiven anH0, …) converts physical units with its H0. Models have no cosmology, so they accept h-units only, except in methods that take anH0argument (e.g.FittingFunction.modify_dndm). An H0 is always a scalar Quantity in km/s/Mpc (e.g.70 * hmf.core.units.H0_unit).Model fields with units (e.g.
CAMB.k_max) take a Quantity, and are stored and read back as a Quantity in the canonical unit (quantity_field()), soattrs.evolveround-trips them; they compare and hash by that value. Numeric parameters are stored as floats, soSMT(a=1)andSMT(a=1.0)are the same model, with the same content hash.The scalar rule. A scalar input gives a scalar output: a 0-d Quantity if the output has a dimension, a
numpy.float64(which is also afloat) if it is dimensionless. Arrays keep their shape.
- Kernels (
hmf.core._kernels) The numerics are pure functions on plain arrays, in the canonical unit of each physical kind (
CANONICAL_UNITS). They take no Quantities, do not loop over array elements in Python, never modify their inputs, and give results that do not depend on the batch size.Stages and models expose them to library code (a stage built on other stages) as kernel-level entry points: public methods or functions whose names end in
_kernel, which take and return plain arrays in canonical units, state the unit of each argument in their docstrings, and follow the same rules. Library code calls these, never the unit-checked public methods.Every kernel-level entry point checks its input against its owner’s domain and raises a
DomainErroroutside it (NaN and infinities included), so its caller never gets a silent extrapolation. The check is cheap: it compares only the smallest and largest value with the bounds (check_extent()). The pure functions insidehmf.core._kernelsdo not check: the entry points that call them do.Read-only data holders (
Transfer.solution,Growth.solution, a power source’srho_mean0) keep plain names and do not check their input:Growth.solutionis NaN beyond its table. Library code goes through the entry points (e.g.Growth.growth_factor_kernel).- Models (
hmf.core.model) A model (a fitting function, a transfer function, …) is a frozen, keyword-only
attrsclass whose fields are its parameters. Each belongs to a kind with its own registry, and is registered under its qualified namepackage.module:Classand an optional unique alias. Lookup is per kind (Kind.get(name)), and also finds models by import path and through thehmf.modelsentry-point group.Kind.coerce(value)turns an instance, a name or a class into an instance of the kind: it is the converter of every stage’s model fields.- Stages (
hmf.core.stage) A stage is one immutable step of a calculation. Parameters change through
evolve(), which returns a new stage; expensive results are cached withfunctools.cached_property().evolve()andfrom_flat()take the parameters of the whole stage tree by name, through a routing table built from the classes (hmf.core.routing, see Changing parameters), andfields_info(),parameter_info(),quantities_available()andinvalidated_by()describe a stage tree without creating an instance. A stage computed from a cosmology (Transfer,Growth) subclassesCosmologyStage, which holds thecosmologyfield and converts physical units with its H0.- Accuracy (
hmf.core.accuracy) The internal mass and wavenumber grids are set by
MassAccuracyandKAccuracy, each withfast()andhigh()presets. A stage’s accuracy fields are named for their grid:Transfer.k_accuracy,Growth.k_accuracy,MassVariance.mass_accuracyandMassVariance.k_accuracy.One KAccuracy drives the k-space calculation. Each stage reads only some of its settings:
Transfer:dln_k, which sets the precision and k sampling of a CAMB or CLASS run (finer than the code’s own belowdln_k = 0.02, soKAccuracy.high()runs the codes at high precision);Growth:dln_k, in the same way, for a run its CAMB or CLASS growth model makes itself;MassVariance:dln_k,ln_k_minandk_max_r_min, the k grid of the σ integrals.
A stage composed of these passes one
KAccuracyto all of them (LinearPowerowns it), and checks it withcheck_consistent().Growth.from_transfertakes the transfer stage’s, and a growth stage shares its transfer stage’s Boltzmann run only when their accuracies are equal.- Domains (
hmf.core.domain) Every model declares a valid domain (outside which it always raises a
DomainError) and a calibration domain (outside which a user-chosenDomainPolicyapplies:"ignore","warn","mask"or"raise"), orNonefor no calibration domain. Ask a model instance for its calibration domain,model.calibration(Calibrated): it can depend on the model’s parameters (e.g.Duffy08’s redshift range), and defaults to the class’scalibration_domain, that of its default parameters. ADomainbounds each variable by an interval whose ends are closed (>=,<=) or open (>,<):(0, None, "(]")is> 0.describe()writes it as inequalities, andcheck()raises aDomainErrorthat quotes them. Stages check their inputs against their model’s valid domain (that of the model’s class, so a model can narrow it). A stage whose domain depends on its own data has avalid_domainof its own, on the instance:MassVariance(masses, filter radii and σ; masses are bounded by the lattice withextension="raise") andTabulatedPower(k; the table’s range withextension="raise"). So every stage’s domain can be inspected the same way, and every public method checks its input withcheck().- Out-of-range switches
Two settings say what to do with values out of range, and they are distinct:
Extension("auto"or"raise") is about the numerics: what to do with a request outside an internal grid or a table.MassAccuracy.extension="auto"extends the mass lattice lazily;TabulatedPower.extension="auto"extrapolates the table as a power law. With"raise"both raise aDomainError.DomainPolicyis about the physics: what to do with a result outside a model’s calibration domain, where it is an extrapolation of the fit.
- Errors
Every error about an input is one of three types:
DomainError(aValueError): an input value is outside what can be evaluated. Non-positive, NaN or infinite k, m or sigma; a mass outside the lattice withextension="raise"; k outside a table that is not extrapolated; z beyond a growth table; anything outside a model’s valid domain. Infinite values are outside every domain, so an infinite z ordelta_haloraises aDomainErrortoo.ValueError: a bad option, configuration or combination, or a missing input. A model that does not apply to the cosmology it is given (e.g.Eisenstein97Growthwith a non-flat cosmology) is a configuration error.TypeError: an object of the wrong kind, or a parameter name that is not one: unknown (with the closest names), ambiguous (with the dotted paths to choose from), derived, given twice, or a required one that is missing.
So
except ValueErrorcatches every bad input, andexcept DomainErroronly values out of range.- Extrapolation warnings
Extrapolating beyond a table you supplied (a
TabulatedPower, or aFromArrayorFromFiletransfer model) emits anHMFExtrapolationWarning, once per owner and end of the table (warn_once()). Every path warns through one mechanism,TableRange.warn_outside: the public methods of aTransferstage or aTabulatedPowerwarn for k outside the table, and aMassVariancewarns once if its k grid reaches beyond its power source’s table (itstable_range). The ends of the k grid are rounded outwards onto the lattice, so a table that reaches the unrounded ends (e.g. from exactly 10⁻⁸ h/Mpc) does not warn. A power source’sln_power_kernelitself does not warn. Extrapolation by design is silent: the tail of T(k) beyond a Boltzmann code’sk_max, or the lazy extension of the mass lattice. So the default configurations emit no warnings. Growth tables are never extrapolated: z beyond them raises.- Fitting functions (
hmf.core.fits) Every hmf 3.x halo mass function fit is a
FittingFunctionmodel, registered under its 3.x name (FittingFunction.get("Tinker08")), with the 3.x parameter names and defaults.fsigma()takes already-resolved inputs (sigma, z, Omega_m(z), the overdensity relative to the mean, delta_c, n_eff, and the mass m as a Quantity), never a cosmology or a mass-definition object; each fit lists the inputs it needs. Code that already holds plain arrays in canonical units, such as aStage, passes them in aFitInputsinstead, built byFittingFunction.inputs_from_arrays. Each declares a valid domain (wherefsigmaalways raises outside) and a calibration domain taken from its paper, with the source cited;evaluate_fsigma()applies a domain policy to the latter. Each also records, as metadata, the mass definition it was measured in and the simulations it was calibrated on (SimulationDetails). See Mass definitions and conversion.
Logarithms always name their base: log10_... or ln_..., never log.
Transfer and growth¶
Transfer (a function of k) gives the transfer function
T(k) and the unnormalised power spectrum \(k^{n_s} T^2\) at z = 0 (a plain,
dimensionless array with an arbitrary amplitude, which
LinearPower normalises), and
Growth (a function of z) the growth factor and growth rate,
for each matter species (species): "cb" (CDM + baryons) and
"tot" (total matter, including massive neutrinos). Their models are in
transfer_models (CAMB, CLASS, EH, BBKS, BondEfs, tables) and
growth_models (the growth ODE, the integral form and its closed
forms, GenMF, Carroll et al., CAMB, CLASS):
from hmf.core.growth import Growth
from hmf.core.transfer import Transfer
from hmf.core.units import h_Mpc
transfer = Transfer(model="CAMB") # Planck18 by default
transfer.transfer_function([0.1, 1.0] * h_Mpc, species="tot")
growth = Growth.from_transfer(transfer, model="CAMB")
growth.growth_factor([0.0, 1.0, 2.0])
One Boltzmann run. A CAMB or CLASS run computes every species, and the growth factor at k = 0.01/Mpc, at once. Runs are memoised by a content hash of their input, so choosing different species for P(k) and for sigma_8, changing
n_s, or a growth stage built from the transfer stage, never runs the code again.Smooth extrapolation. Above the code’s largest wavenumber (
k_max, 20 h/Mpc by default), T(k) follows the EH98 shape, matched to the table in value and logarithmic slope, so d ln T / d ln k is continuous at the join.Disk cache. With
disk_cache=True(or aDiskCache), runs are also kept on disk (by default in$HMF_CACHE_DIR, or the platform’s user cache directory, e.g.~/.cache/hmf), keyed by the run’s input and the versions of hmf and of the code.
Mass variance¶
MassVariance gives the mass
variance σ(M) of the unnormalised linear power at z = 0, and its slope
dlnσ/dlnM, for a smoothing filter (hmf.core.filters: TopHat, SharpK,
SmoothK). The power comes from any PowerSource:
the power of one species of a Transfer stage
(power_source()), or a
TabulatedPower. A power source also carries the mean
density of CDM + baryons, \(\bar\rho_{\rm cb}\), whatever its species: it sets
the mass of a filter radius, \(M = \tfrac{4\pi}{3}\bar\rho_{\rm cb}(cR)^3\),
as in hmf 3.x and the calibrations of the fits, so the total-matter and the
CDM + baryon power give the same R(M):
from hmf.core.mass_variance import MassVariance
from hmf.core.power_source import TabulatedPower
from hmf.core.transfer import Transfer
from hmf.core.units import Msun_h, h_Mpc, power_unit, rho_unit
mv = MassVariance(power=Transfer(model="EH").power_source("cb"), filter="SharpK")
mv.sigma(m * Msun_h), mv.dlnsigma_dlnm(m * Msun_h), mv.m_from_sigma(0.5)
source = TabulatedPower(k=k * h_Mpc, pk=pk * power_unit, mean_density=rho * rho_unit)
MassVariance(power=source)
Both are interpolated on a lattice of nodes at \(\log_{10} m = j\Delta\), built
lazily: results do not depend, bit for bit, on the order or batching of requests.
σ uses a quintic Hermite interpolant in ln–ln; dlnσ/dlnM is interpolated
separately, never by differentiating σ. The k grid is a lattice in ln k, fixed by
k_accuracy (and mass_accuracy.log10_m_min). Masses whose integrals the grid can’t resolve (truncated at
either end, or aliased) raise rather than return a wrong value. σ at any filter
radius can also be evaluated directly, without the lattice
(ln_sigma_at_radius_kernel(), e.g. σ8),
and agrees with the lattice to
INTERPOLATION_RTOL at the default settings.
To use your own power spectrum, implement the
PowerSource protocol: ln_power_kernel,
rho_mean0, table_range, and _unit_context, the
UnitContext (with its H0) of the stages built on the
source. _unit_context comes from HasUnitContext and is
required, despite its leading underscore: every
unit_boundary() method reads it by that name on its fast path,
and the underscore keeps it out of a stage’s public outputs, since it is a detail of
the units machinery rather than a result. Provide it as a
functools.cached_property():
@functools.cached_property
def _unit_context(self) -> UnitContext:
return UnitContext(self.H0)
Composing stages¶
MassFunction combines the stages above and a fitting
function into the halo mass function, as a function of mass and redshift. Each stage
holds the stages it is computed from as fields, so together they form a stage tree.
A stage computed from the fields above it, rather than given, is a derived stage
(derived_property()):
MassFunction
├── linear_power: LinearPower σ8 normalisation and P(k, z)
│ ├── transfer: Transfer T(k), from one Boltzmann-code run
│ └── growth (derived): Growth the growth factor D(z), of growth_model
├── variance (derived): MassVariance σ(m) of the power before normalisation
└── fit: FittingFunction f(σ), e.g. Tinker08
plus the variance’s filter, mass_accuracy and truncation_rtol, the
critical overdensity delta_c and the domain_policy as fields of
MassFunction. MassFunction.build
builds the whole tree, with hmf 3.x-like defaults (Planck18, CAMB, the growth ODE, σ8
and n_s from the cosmology, Tinker08, a top-hat filter, and the CDM + baryon power
normalised by the σ8 of total matter). Every method takes its arguments by keyword:
import numpy as np
from hmf.core.mass_function import MassFunction
from hmf.core.units import Msun_h
mf = MassFunction.build(fit="Tinker08")
m = np.logspace(10, 15, 51) * Msun_h
z = np.array([0.0, 0.5, 1.0])
dndm = mf.dndm(m=m[None, :], z=z[:, None]) # shape (3, 51), in h^4 / (Msun Mpc^3)
ngtm = mf.ngtm(m=m, z=0.0)
m_star = mf.m_from_peak_height(peak_height=1.0, z=0.0)
view = mf.at(z=1.0, m=m) # the quantities at z = 1, as arrays: view.dndm, view.ngtm
Changing a parameter gives a new stage, with evolve; the old one is unchanged:
lower = mf.evolve(sigma_8=0.75)
lower.variance is mf.variance # True: the mass variance is reused, not recomputed
What each part does:
LinearPowernormalises the power spectrum to σ8: P(k, z) = A D²(z) P_raw(k), where P_raw is the transfer stage’s k^n_s T²(k) and the amplitude A = (σ8/σ8,raw)² is a single number (σ8,raw is the rms of P_raw in a sphere of 8 Mpc/h). Its growth stage is built from its transfer stage, so the cosmology, the oneKAccuracyof the tree and the disk cache are the transfer stage’s, held in one place.The
MassVarianceis computed from P_raw, the power before it is normalised and scaled to a redshift: σ8 and z only multiply it, by √A D(z). So changing σ8, z, the fit, δc or the domain policy reuses the mass variance already computed (the expensive part of the calculation) and runs no Boltzmann code again.The fit is evaluated in its own mass definition (e.g. SO-mean(200) for Tinker08, FoF for Jenkins), unless
mass_definitionasks for another; see Mass definitions and conversion.n(>m) and ρ(>m) are integrated from 10^17.5 M☉/h down, over a fixed grid of masses, so the value at a mass does not depend on which other masses are asked for with it.
Outside the fit’s valid domain every method raises; outside the range it was calibrated on,
domain_policy("ignore","warn","mask"or"raise") decides what happens at each mass and redshift asked for.
Mass definitions and conversion¶
A halo’s mass depends on where its edge is put. hmf.core.mass_definitions
gives the definitions as frozen attrs values, which compare (symmetrically) and
hash by class and value: SOMean(overdensity=200), SOCritical(overdensity=500),
SOVirial() (Bryan & Norman 1998), FOF(linking_length=0.2), SelfBound(),
and SOGeneric(preferred=...), the definition of a fit that takes the overdensity
as an input. Each has a short name ("200m", "500c", "vir", "fof")
that is accepted wherever a definition is. Overdensities are relative to the mean
density of CDM + baryons, and radii are comoving.
Every fit records the definition it was measured in
(FittingFunction.measured_mass_definition), and
measured.accepts(definition) says whether it can be evaluated in another directly:
every definition accepts itself, and an SOGeneric (Tinker08, Tinker10, Watson)
accepts every SO definition. A MassFunction takes
the definition its masses are in as mass_definition (None, the default, is the
fit’s own). When the fit does not accept it, the masses are converted by
mass_conversion, which must be given: the conversion is opt-in:
from hmf.core.mass_conversion import ProfileConversion
from hmf.core.concentration import Duffy08
mf = MassFunction.build(fit="SMT") # in SMT's own, virial, definition
mf.evolve(mass_definition="200c") # ValueError: needs a mass_conversion
mf200c = mf.evolve(mass_definition="200c", mass_conversion=ProfileConversion())
mf.evolve(fit="Tinker08", mass_definition="200c") # accepted: no conversion
mf200c.evolve(**{"mass_conversion.concentration": Duffy08(sample="relaxed")})
ProfileConversion takes haloes of a
DensityProfile (NFW by
default; another profile, e.g. Einasto, is another DensityProfile) with the
concentrations of a ConcentrationModel (a model kind:
Duffy08 by default, with every parameter set of their
Table 1), converted to the definition of the masses if it was calibrated in another.
The profile solves for the new concentration of every mass at once (for NFW, Newton’s
method from the approximation of Hu & Kravtsov 2003, to round-off), and gives the Jacobian dM’/dM
analytically. The mass function then conserves halo number:
dn/dM(M) = dn’/dM’(M’(M)) dM’/dM and n(>M) = n’(>M’(M)), with the fit evaluated at
the masses M’ in its own definition. Converting to the same definition is the
identity, and A → B → A returns the masses to round-off. The domain policy also
applies to the concentration model’s calibration domain. A profile gives only what
the conversion needs, its enclosed mass \(\mu(x)\); halomod’s profiles, with
their Fourier transforms, build on these.
Changing parameters¶
A stage tree’s parameters are routed by a table that hmf.core.routing builds
once per stage class, from the attrs fields of the classes: it creates no stage
and computes nothing. evolve(),
from_flat() and
MassFunction.build take the same
names, of three kinds:
Flat names, for every field whose name appears once in the tree:
cosmology,n_s,k_accuracy,disk_cache,growth_model,sigma_8,species,sigma_8_species,filter,mass_accuracy,truncation_rtol,fit,delta_c,domain_policy,mass_definition,mass_conversion. More generally, any dotted suffix of a field’s path that ends no other field’s path names it:"transfer.model"is the transfer model however many other stages have amodel. A stage lists the fields whose names are too generic to stand alone inqualified_parameters;parameter_names()gives them with the stage’s name, so the transfer model is listed as"transfer.model"(a baremodelstill works while it is unique).Shared parameters: a name that a stage lists in
shared_parametersis one parameter in its tree, and a change to it changes every field of that name below the stage, so that they stay equal. AGrowthstage with a transfer stage sharescosmology,k_accuracyanddisk_cachewith it. In a mass function each of them is held once, by the transfer stage.Dotted paths, always:
"linear_power.sigma_8","linear_power.transfer.model"; a suffix that ends several paths is ambiguous, and its error lists the names to choose from. A stage’s path replaces the whole stage (mf.evolve(linear_power=lp)), and a path into a model field changes that field of the model ("fit.a","transfer.model.z_max").
So the flat names of a mass function are exactly build’s keywords:
mf = MassFunction.build(**{"transfer.model": "EH"}, fit="ST", sigma_8=0.8)
mf.evolve(sigma_8=0.85, n_s=0.95)
mf.evolve(cosmology=Planck15) # the cosmology of the whole tree, with
# sigma_8 and n_s from Planck15
mf.evolve(**{"fit.a": 0.75}) # a parameter of the fit
mf.evolve(**{"linear_power.transfer.n_s": 0.95})
MassFunction.from_flat({"transfer.model": "EH", "linear_power": {"sigma_8": 0.8}})
Computed defaults. build takes σ8 and n_s, when they are not given, from the
cosmology’s meta (the mass function’s
computed_defaults()). They are computed from the cosmology,
so evolve computes them again whenever it changes the cosmology (or a stage holding
it, such as "linear_power.transfer"): each takes the new cosmology’s value unless it
is given in the same call. Any other change leaves them as they are:
mf = MassFunction.build(sigma_8=0.8, n_s=0.95)
mf.evolve(cosmology=Planck15) # sigma_8 = 0.8159, n_s = 0.9667
mf.evolve(cosmology=Planck15, sigma_8=0.8) # sigma_8 = 0.8, n_s = 0.9667
mf.evolve(sigma_8=0.85) # n_s stays 0.95
So mf.evolve(cosmology=c) equals build with c and every other parameter of
mf except σ8 and n_s, and a cosmology scan done with evolve gives the same models
as one done with build. To keep σ8 fixed through a scan, pass it with each
cosmology. The result depends only on the stage and the changes: a stage read back from
a configuration or a pickle evolves as the original does. If the new cosmology does not
give a parameter that is not passed (its meta has no sigma8, as for a
cosmology made by hand), evolve raises the ValueError build raises,
which names the parameter: set it explicitly.
A name that is not a parameter raises a TypeError at once, before anything
is built, with the closest names (mf.evolve(sigma8=0.8): “Did you mean
‘sigma_8’?”); an ambiguous one lists the dotted paths to choose from.
evolve rebuilds only the stages on the path from each changed field up to the
root, with each stage’s own evolve_own; every other stage is shared, the same
object, with every result it has cached. A derived stage is computed from fields it
declares (the variance from the linear power’s transfer stage and species and the
variance settings; the growth from the transfer stage and growth_model), and
evolve_own carries it, once computed, to the new stage when none of them
changes. So mf.evolve(sigma_8=0.85) rebuilds the linear power and the mass
function, and keeps the transfer, growth and variance stages;
mf.evolve(filter="SharpK") keeps the linear power; mf.evolve(fit="ST") keeps
both; mf.evolve(**{"transfer.model": "BBKS"}) gives a new variance, of the new power.
Each new stage is validated by its constructor; if any fails, evolve raises and
the original tree is unchanged.
Every question about the parameters is answered from the classes, without building a stage or running a Boltzmann code:
MassFunction.parameter_names() # ('cosmology', 'transfer.model', 'n_s', ...)
MassFunction.parameter_info()["sigma_8"].path # 'linear_power.sigma_8'
MassFunction.parameter_defaults()["sigma_8"] # 0.8102, from Planck18
MassFunction.quantities_available()[""] # ('dlnsigma_dlnm', 'dndlnm', ...)
MassFunction.invalidated_by("sigma_8") # ('', 'linear_power')
mf.to_flat() # every parameter's value, for from_flat
- Extension stages
A stage that holds other stages (as fields whose type is a
Stage) gets a routing table of its own, which includes the tables of the stages it holds: a stage holding aMassFunctiontakesevolve(sigma_8=...)with no further code. It declaresthe stages it computes from its fields with
@derived_property(*inputs), naming the fields (dotted paths below the stage) each is computed from, so thatevolvecarries it when none of them changes;the names of its tree that are one parameter in the class attribute
shared_parameters: the stage that composes the others decides, whatever the stages it holds declare;the fields whose names are too generic to stand alone in
qualified_parameters, listed with the stage’s name;and defaults that depend on other parameters (as
buildtakes σ8 and n_s from the cosmology) in the class methodcomputed_defaults, with the parameters they are computed from incomputed_defaults_inputs(dotted paths below the stage), so thatevolvecomputes them again when one of those changes. The inputs are required: a stage that definescomputed_defaultswithout them raises aTypeErrorwhen its routing table is first built.
A derived property’s input, or a computed default’s, that is not a field of the tree raises a
TypeErrorwhen the table is first built.undeclared_inputs()checks, in a test, that each derived property declares every field it reads (and every stage it holds), and thatcomputed_defaultsreads only its declared inputs: run on a stage of the extension, it returns the fields each one reads but does not declare. Declaring an input that is read only on some branches is safe.
Configurations¶
A configuration (hmf.core.config) describes a stage tree completely, as plain
data: the resolved value of every field of every stage, under a header with the
schema version and the versions of hmf, camb, classy, astropy, numpy and scipy. It is
what a stage tree is saved, pickled and hashed as:
from hmf.core import config
mf = MassFunction.build(fit="ST", sigma_8=0.8)
config.dump_toml(mf, "mf.toml") # or config.dumps_toml(mf), a string
same = config.load_toml("mf.toml") # == mf, with bit-identical results
data = config.to_config(mf) # the same, as nested dicts
config.from_config(data) == mf # True
Resolved values. The configuration mirrors the stage tree’s fields: each stage held
as a field is a table ([linear_power.transfer]), and derived stages are not written,
but rebuilt. Every field is written, defaults and computed defaults included: the
default configuration gives sigma_8 = 0.8102 and n_s = 0.9665 (Planck18’s), the
cosmology’s every parameter, and every setting of k_accuracy and mass_accuracy.
So a configuration means the same calculation whatever defaults a later version of hmf
has. A partial configuration, written by hand, also reads: it takes the names of
from_flat() (nested tables, dotted keys, flat names, paths
into a model’s fields), and every field it does not give takes its default, as
build() would give it. Only the header is
required:
sigma_8 = 0.8
fit = "ST"
"transfer.model" = "EH"
[hmf]
schema_version = 1
stage = "hmf.core.mass_function:MassFunction"
Encodings. A model is a table of its registered (qualified) name and every field,
{model = "hmf.core.fits:Tinker08", A_200 = 0.1858659, ...}, and is read through its
kind’s registry, so aliases and plugins work. A Quantity is its value and unit, with
h-units written explicitly: {value = 20.0, unit = "littleh / Mpc"}. Units are parsed
with littleh enabled, and a unit with littleh goes through
to_canonical(), so a wrong power of littleh raises as at the
units boundary. A cosmology is a table of every parameter (m_nu as a Quantity
array), its astropy class, and its name and meta (which are information only: a
meta entry that can’t be written, e.g. a date, is left out with a warning, unless the
stages read it, as n and sigma8).
Mass definitions, and other values whose class is not the field’s declared class, carry
a class key with their import path. TOML has no null: a field that is None is
left out if that is its default, and written {none = true} otherwise.
Schema versions. The header’s schema_version (SCHEMA_VERSION)
versions the layout and the defaults: a change to any default is a schema change (see
Contributing to hmf). Reading a configuration of an older schema applies the registered
migrations (register_migration()) one version at a time, and
logs what each changed; with migrate=False it raises instead. A newer schema, or one
with no migration, raises a ValueError. Package versions that differ from the
running ones are logged as a warning, or raise with strict=True. Nothing is read in
another meaning silently.
Content hash. config.content_hash(stage) is
the SHA-256 hash of the canonical form of the configuration’s physics, plus the
versions of hmf, camb, classy and astropy. It leaves out fields that change no result
(declared with field(physics=False), e.g. disk_cache), and a cosmology’s name
and meta, so it is unchanged by a round trip through a configuration, a pickle or a
TOML file. The disk cache’s keys use the same canonical form
(hmf.core._serialise.canonical()).
Pickling. A stage pickles as its configuration, with the Boltzmann-code runs its
tree has already used, as plain arrays. Unpickling rebuilds the tree and reuses those
runs, so it does not run CAMB or CLASS again, and gives bit-identical results. No CAMB or
CLASS object is ever pickled. A stage is immutable, so copy.copy and
copy.deepcopy return the stage itself, with its caches.
API¶
The modules are listed in the hmf.core API reference.