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._kernelswork on plain arrays in the canonical units ofCANONICAL_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 builtu.Msun / cu.littlehequalsMsun_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 (seeHasUnitContext). An object without one (a model, a domain) accepts h-units only. A model method that needs h, or physical units converted, takes anH0argument 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 (soevolve()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 afloat) for a dimensionless one. Arrays keep their shape. Every public method is decorated withunit_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 (withastropy.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), nevernp.log10(m.value)(whose unit is whatever the caller used).
Classes
|
An object whose |
|
Per-instance unit-conversion state: the H0, and cached conversion factors. |
Functions
|
Return the power of |
Enable parsing of unit strings that contain |
|
|
Parse a unit string that may contain |
|
Define an |
|
Convert a dimensional value to a plain array in a canonical unit. |
|
Make a method a units boundary: Quantities outside, plain arrays inside. |
Exceptions
|
A dimensional argument was given without units (a bare float or array). |