openghg_inversions.basis.layout#
Grouped basis layout helpers.
This module contains the small internal representation used to combine
partition-local basis labels into one flat basis map while preserving semantic
state metadata. It is intentionally separate from basis-generation algorithms:
algorithms decide how to split cells inside a mask or region class, while
BasisLayout records how those partition-local outputs are assembled into the
single state axis consumed by BucketBasisOperator.
BasisLayout does not infer a catch-all remainder region. Callers that need
one should pass it as an explicit BasisPartition so the semantic group and
local region label are visible in the resulting state metadata.
The current implementation is eager and in-memory. It is intended for small metadata/layout assembly steps after region masks and basis labels have already been loaded or generated. File loading, lazy execution, large-grid chunking, and public RHIME configuration are handled outside this module.
- class openghg_inversions.basis.layout.BasisLayout(partitions: Sequence[BasisPartition], state_dim: str = 'state')#
Bases:
objectCombine disjoint basis partitions into one flat label map.
This first implementation is intentionally eager and in-memory: partition label arrays are materialized as NumPy arrays while building the combined map. File loading, lazy execution, and large-grid chunking policy remain outside this small metadata/layout boundary.
- Variables:
partitions (collections.abc.Sequence[openghg_inversions.basis.layout.BasisPartition]) – Ordered partition definitions to combine. Partitions must cover disjoint grid cells and the combined layout must cover the full grid.
state_dim (str) – Name of the downstream state dimension that will eventually receive the metadata coordinates.
- partitions: Sequence[BasisPartition]#
- to_flat_basis(*, name: str = 'basis') BasisLayoutResult#
Return a positive-label flat basis and metadata keyed by raw labels.
This method materializes partition labels and the combined map in memory.
- Parameters:
name – Name to assign to the returned flat-basis DataArray.
- Returns:
A combined basis map plus metadata indexed by
basis_label.BucketBasisOperatormaps that metadata onto the final state coordinate after its configured label policy is applied.- Raises:
ValueError – If partitions are empty, overlap, contain no mapped cells, or leave grid cells unmapped.
TypeError – If partition labels are not numeric.
- class openghg_inversions.basis.layout.BasisLayoutResult(basis_flat: DataArray, state_metadata: Dataset)#
Bases:
objectCombined flat basis labels plus raw-label keyed state metadata.
- Variables:
basis_flat (xarray.core.dataarray.DataArray) – Positive integer flat basis map covering every grid cell.
state_metadata (xarray.core.dataset.Dataset) – Dataset indexed by
basis_labelwithbasis_group,basis_partition, andregion_in_partition.
- class openghg_inversions.basis.layout.BasisPartition(name: str, labels: DataArray, group: str, attrs: Mapping[str, ~typing.Any]=<factory>)#
Bases:
objectPartition-local basis labels and their semantic group name.
Positive integer values in
labelsare treated as local region labels. Non-positive values and NaNs are treated as cells outside this partition.- Variables:
name (str) – Stable partition name used in
basis_partitionmetadata.labels (xarray.core.dataarray.DataArray) – Partition-local label array. Positive integer values identify regions inside this partition.
group (str) – Semantic group name used in
basis_groupmetadata.attrs (collections.abc.Mapping[str, Any]) – Optional partition metadata reserved for future callers. These attrs are not currently serialized into the layout result.
- class openghg_inversions.basis.layout.BasisStateMetadata(dataset: Dataset)#
Bases:
objectSemantic coordinates describing a basis operator state axis.
The metadata may start indexed by raw
basis_labelvalues from a flat basis layout or by the final operator state dimension. This value object owns validation and alignment so operator classes do not need to know the dataset schema beyond their state coordinate and raw basis-label order.- Variables:
dataset (xarray.core.dataset.Dataset) – Dataset containing
basis_group,basis_partition, andregion_in_partitionvariables indexed by eitherbasis_labelor a final state dimension.
- assign_to_matrix(mat: DataArray, *, state_dim: str) DataArray#
Attach metadata variables as coordinates on a basis matrix.
- Parameters:
mat – Basis matrix whose state coordinate matches this metadata.
state_dim – State dimension used by
matand this metadata.
- Returns:
matwith grouped metadata coordinates attached.- Raises:
ValueError – If this metadata is not already indexed by
state_dimor required variables are malformed.
- classmethod from_dataset(dataset: Dataset | BasisStateMetadata) BasisStateMetadata#
Return
datasetas aBasisStateMetadatainstance.- Parameters:
dataset – Metadata dataset or an existing
BasisStateMetadata.- Returns:
datasetwrapped asBasisStateMetadata.
- classmethod from_matrix(mat: DataArray, *, state_dim: str) BasisStateMetadata | None#
Extract complete basis state metadata from matrix coordinates.
- Parameters:
mat – Basis matrix that may carry grouped metadata coordinates on the state dimension.
state_dim – Name of the state dimension on
mat.
- Returns:
Metadata indexed by
state_dimif all grouped metadata coordinates are present, otherwiseNone.
- on_state_dim(*, state_dim: str, state_coord: DataArray, basis_value_labels: Sequence[int] | ndarray) BasisStateMetadata#
Return metadata indexed by the final operator state dimension.
- Parameters:
state_dim – Final state dimension name.
state_coord – Final state coordinate after the operator label policy has been applied.
basis_value_labels – Raw basis labels ordered to match the operator state columns before final relabeling.
- Returns:
Metadata aligned to
state_coordand indexed bystate_dim.- Raises:
ValueError – If metadata labels do not match basis labels, if raw labels are duplicated, if final-state metadata has the wrong length, or if required variables are missing.