Skip to content

Demo: layout-aware relationalizer for scipy.sparse matrices#116

Open
sidprasad wants to merge 1 commit into
mainfrom
demo/sparse-matrices
Open

Demo: layout-aware relationalizer for scipy.sparse matrices#116
sidprasad wants to merge 1 commit into
mainfrom
demo/sparse-matrices

Conversation

@sidprasad

Copy link
Copy Markdown
Owner

What

A demo + reusable relationalizer that visualizes the physical storage layout of scipy.sparse matrices (COO / CSR / CSC) and overlays the path an element-access walks through it — answering Rachit's question: which indices do I have to touch across all the data structures to read A[i, j]?

  • demos/sparse_layout.py — a priority-100 relationalizer for scipy.sparse.
    • layout(A) draws the indptr[] / indices[] / data[] arrays (CSR/CSC) or the row[] / col[] / data[] triplets (COO).
    • access(A, i, j) overlays the lookup, colored by step: 🟠 bound (read the two slice pointers) · ⚪ scan (columns skipped) · 🟢 hit (the match and its value).
  • demos/06-sparse-matrices.ipynb — the narrative: the crash → COO triplets → CSR compression → the access path → CSC (a different layout touches different indices).

Why

diagram(A) on a raw sparse matrix today RecursionErrors: the generic relationalizer walks attributes with inspect.getmembers, and numpy arrays expose endlessly many array-valued properties (.T, .real, …). The interesting thing to see isn't the logical grid anyway — it's the array bundle and what an access costs.

Notes

  • Directives are applied per-object, not as static class decorators: spytial-core rejects a selector naming a type with zero atoms, so a COO diagram can't carry CSR's group rules and vice versa (_decorated() builds the graph once and attaches only the rules whose types/relations are present).
  • Verified each format's access against the dense ground truth; confirmed diagrams render without a layout error in spytial-core.
  • The demo needs numpy + scipy (like the z3/protobuf demos, these are demo-only deps not in requirements.txt).

🤖 Generated with Claude Code

Adds a demo + reusable relationalizer that visualizes the *physical* storage
layout of sparse matrices (COO / CSR / CSC) and overlays the path an element
access walks through it — "which indices do I touch across all the arrays to
read A[i, j]?"

- demos/sparse_layout.py: a priority-100 relationalizer for scipy.sparse.
  layout(A) draws the indptr/indices/data arrays; access(A, i, j) overlays the
  lookup (bound / scan / hit). Fixes diagram() on a raw sparse matrix, which
  otherwise RecursionErrors through the generic relationalizer (numpy arrays
  expose endlessly many array-valued attributes).
- demos/06-sparse-matrices.ipynb: the narrative — the crash, COO triplets, CSR
  compression, the access path, and CSC (a different layout touches different
  indices).

Directives are applied per-object, not as static class decorators: spytial-core
rejects a selector naming a type with zero atoms, so a COO diagram can't carry
CSR's group rules and vice versa. Verified each format's access against the
dense ground truth and confirmed the diagrams render without error.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant