hmf.core.units

The units boundary of the v4 core.

hmf.core follows one units policy (“option B” of issue #389):

  • Quantities at every public boundary. Every dimensional field of a stage or model, dimensional argument of a public method, and dimensional output is an astropy.units.Quantity. Dimensionless ones are plain numbers and arrays.

  • Unit-free kernels inside. The numerics in hmf.core._kernels work on plain arrays in the canonical units of CANONICAL_UNITS.

  • h-units are explicit, through littleh (astropy.cosmology.units.littleh). Outputs are in h-units. Inputs may be in h-units or in physical units; physical units are converted with the H0 of the object whose method is called.

The unit_boundary() decorator is the bridge between the two. It strips the units off dimensional arguments, converting them to the canonical unit first if needed, and puts the canonical unit on the outputs. Its fixed cost is budgeted at 2 µs per call (benchmarks/test_core_units.py measures it), so it uses Quantity views and per-instance cached conversion factors, not astropy.units.quantity_input().

Rules

  • Use the constants in this module (Msun_h, Mpc_h, …) whenever you attach a unit. A freshly built u.Msun / cu.littleh equals Msun_h, but it is a different object, so it misses the boundary’s identity fast path.

  • A bare float or ndarray for a dimensional argument is an error (UnitBoundaryError). hmf never guesses units.

  • Dimensionless arguments (z, sigma, peak height, delta_c, Omega, …) are plain floats or arrays, and pass through the boundary unchanged.

  • One conversion. Every conversion of a dimensional value to its canonical unit goes through unit_boundary() or, for code that converts a value itself, to_canonical(); both raise the same errors, with the same messages. Errors name the class of the object (not the class that defines the method) and the argument.

  • H0. Physical units are converted with the H0 of a UnitContext, which must be a scalar Quantity in km/s/Mpc. An object with a cosmology provides one (see HasUnitContext). An object without one (a model, a domain) accepts h-units only. A model method that needs h, or physical units converted, takes an H0 argument and builds its context per call.

  • Unit-carrying fields of models are declared with quantity_field(): they take a Quantity, are stored as a Quantity in the canonical unit (so evolve() round-trips them), and compare and hash by their value in it. Kernels read their plain values through a private cached property.

  • The scalar rule. A scalar input gives a scalar output: a 0-d Quantity for a dimensional output, a numpy scalar (numpy.float64, which is also a float) for a dimensionless one. Arrays keep their shape. Every public method is decorated with unit_boundary() (with no arguments if it has no dimensional ones), which applies the rule. Dimensionless outputs are plain arrays, never dimensionless Quantities.

  • Never enable with_H0() globally (with astropy.units.set_enabled_equivalencies()): inside it, h-scaled and physical values convert into each other silently. hmf only ever passes it explicitly, for one conversion at a time.

  • Take logarithms of values in an explicit unit: np.log10(m / Msun_h), never np.log10(m.value) (whose unit is whatever the caller used).

Classes

HasUnitContext(*args, **kwargs)

An object whose unit_boundary() methods can convert physical units.

UnitContext([H0, where])

Per-instance unit-conversion state: the H0, and cached conversion factors.

Functions

littleh_power(unit)

Return the power of littleh in unit (0 if it has none).

littleh_units_enabled()

Enable parsing of unit strings that contain littleh.

parse_unit(text)

Parse a unit string that may contain littleh, e.g. from a config file.

quantity_field(unit, *, doc[, optional, ndim])

Define an attrs field (of a model or settings class) that carries a unit.

to_canonical(value, unit, *[, context])

Convert a dimensional value to a plain array in a canonical unit.

unit_boundary(*[, returns])

Make a method a units boundary: Quantities outside, plain arrays inside.

Exceptions

UnitBoundaryError

A dimensional argument was given without units (a bare float or array).