linopy.constraints.CSRConstraint#
- class linopy.constraints.CSRConstraint(csr, con_labels, rhs, sign, coords, model, name='', cindex=None, dual=None)#
Frozen constraint backed by a CSR sparse matrix.
- Parameters:
csr (
scipy.sparse.csr_array) – Shape (n_flat, model._xCounter). Each row is a flat position in the constraint grid (including masked/empty rows).rhs (
np.ndarray) – Shape (n_flat,). Right-hand-side values.sign (
strornp.ndarray) – Constraint sign. Either a single str (‘=’, ‘<=’, ‘>=’) for uniform signs, or a per-row np.ndarray of sign strings for mixed signs.coords (
listofpd.Index) – One index per coordinate dimension defining the constraint grid.model (
Model) – The linopy model this constraint belongs to.name (
str) – Name of the constraint.cindex (
intorNone) – Starting label assigned by the model. None if not yet assigned.dual (
np.ndarrayorNone) – Shape (n_flat,). Dual values after solving, or None.
- __init__(csr, con_labels, rhs, sign, coords, model, name='', cindex=None, dual=None)#
Methods
__init__(csr, con_labels, rhs, sign, coords, ...)active_labels()Active constraint labels in build order, without building the CSR.
freeze()Return self (already immutable).
from_mutable(con[, cindex])Create a CSRConstraint from a Constraint.
from_netcdf_ds(ds, model, name)Reconstruct a Constraint from a netcdf Dataset (CSR format).
has_variable(variable)Check if the constraint references any of the given variable labels.
iterate_slices([slice_size, slice_dims])Yield row-batched sub-Constraints without Dataset reconstruction.
mutable()Convert to a Constraint.
print([display_max_rows, display_max_terms])Print the linear expression.
sanitize_infinities()Mask out rows with invalid infinite RHS values (mutates in-place).
sanitize_missings()No-op: missing rows are already excluded during freezing.
sanitize_zeros()Remove terms with zero or near-zero coefficients (mutates in-place).
to_matrix([label_index])Return the stored CSR matrix and con_labels.
to_matrix_with_rhs(label_index)Return (csr, con_labels, b, sense) — all pre-stored, no recomputation.
to_netcdf_ds()Return a Dataset with raw CSR components for netcdf serialization.
Convert frozen constraint to polars DataFrame directly from CSR.
Attributes
active_positionsFlat positions of active (non-masked) rows in the original coord shape.
attrsGet the attributes of the constraint.
Get coefficients DataArray, shape (*coord_dims, _term).
coord_dimscoord_namesGet the names of the coordinates.
coord_sizescoordsGet the coordinates of the constraint.
dataReconstruct the xarray Dataset from the CSR representation.
dimsGet the dimensions of the constraint.
Get dual values DataArray, shape (*coord_dims).
flatConvert the constraint to a pandas DataFrame.
full_sizeindexesGet the indexes of the constraint.
is_assignedWhether the constraint has been assigned labels by the model.
labelsGet labels DataArray, shape (*coord_dims).
lhsGet LHS as LinearExpression (triggers Dataset reconstruction).
maskGet the mask of the constraint.
modelGet the model reference.
nameGet the constraint name.
Get the number of active constraints (non-masked, with at least one valid variable).
ndimGet the number of dimensions of the constraint.
Get the number of terms in the constraint.
rangeReturn the (start, end) label range of the constraint.
Get RHS DataArray, shape (*coord_dims).
shapeGet the shape of the constraint.
Get sign DataArray.
sizeGet the size of the constraint.
sizesGet the sizes of the constraint.
term_dimReturn the term dimension of the constraint.
typeGet the type string of the constraint.
Get variable labels DataArray, shape (*coord_dims, _term).