Skip to content

Make the sparse path observable and controllable (.is_sparse, densify warning, per-call sparse=) #969

Description

@FabianHofmann

Note

The following content was generated by AI.

Describe the feature you'd like to see

Make the sparse path observable and controllable. Child of #756.

Right now a user cannot tell whether their build stayed sparse. There is no .is_sparse property, no marker in the repr, and no warning when the backing is dropped. Four separate code paths fall back to dense by returning None, all silently: _sparse_matmul (linopy/expressions.py:2570), _try_csr_merge (3319), _aligned (3291) and csr_rhs (linopy/constraints.py:1427).

Combined with the fact that reading repr() or .sizes destroys the backing in place (filed separately as a bug), this makes the feature very hard to use deliberately on anything larger than a toy model.

Three parts:

  1. Expose the state. A public .is_sparse on LinearExpression, and a marker in __repr__ — provided the repr itself stops densifying.
  2. Explain the fallbacks. An opt-in warning, for example options["warn_on_densify"], naming the operation that dropped the backing. Off by default, so nothing changes for existing users.
  3. Per-call control. A sparse= flag on @/dot and merge, mirroring groupby(...).sum(sparse=True). Today @ contracts sparsely but returns a dense object unless the input was already CSR-backed or the global options["sparse_groupby"] is set (expressions.py:2606-2609), which is a surprising coupling between an unrelated option and the result's representation. feat(expressions): sparse @/dot for CSR-backed expressions #961 explicitly deferred this flag.

Implementation ideas

Serving shape, sizes, coords, dims and isnull from the existing Grid is a prerequisite for a non-densifying repr, and is worth doing on its own: Grid already holds all of it.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or requestsparseSparse / CSR-backed expressions and constraints

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions