Skip to content

[experimental] Spatial index of points (RFC 3) and shapes (RFC 4) - #13

Draft
LucaMarconato wants to merge 21 commits into
mainfrom
tiled-points-spatial-index
Draft

LucaMarconato wants to merge 21 commits into
mainfrom
tiled-points-spatial-index

Conversation

@LucaMarconato

@LucaMarconato LucaMarconato commented Nov 16, 2026 •

Copy link
Copy Markdown
Owner

Requested by Luca · project thread

Experimental implementation of RFC 3: tiled and multiscale points (draft on scverse/spatialdata-spec#98), to test the spec on real data (10x Atera). napari side: LucaMarconato/napari-spatialdata#1. Proposed spec fixes, benchmarks and sizes: https://claude.ai/artifact/4H6Svd7NRVhE9x1v8inNaM

What's in it

  • build_points_spatial_index(sdata, name, storage="parquet"|"zarr", ...): rewrites a points element of a disk-backed SpatialData sorted and indexed, in points format 0.3 (Parquet, PointsFormatV03) or 0.3-experimental (Zarr, PointsFormatV03Experimental). write_points_with_spatial_index(points, path, ...) does the same for any dask/pandas dataframe.
  • Out-of-core builder (no new dependencies): pass A picks the grid (bbox + fine histogram, tile size so that the densest tile holds about target_points_per_tile), merge rule for the levels and default rates; pass B draws p, assigns bands/tiles and counts; pass C scatters partitions into bucket files; pass D sorts each bucket and writes the tile-major and feature-major tables and their offsets.
  • Parquet storage: points.parquet/ (tile-major, row groups aligned to tiles), tile_offsets/<l>, feature/<key>/{points.parquet,tile_offsets}. Zarr storage: spatial/points (df: + enum: conventions, sharded), same offsets.
  • PointsSpatialIndex / read_points_spatial_index(path): query(box_min, box_max, level=, budget=, features=, use_feature_index=).
  • Feature index: feature_index="dense" follows the RFC (int64[n_features, N_tiles+1] per band); "sparse" is a proposed fix (CSR over features of the non-empty tiles), since the dense one does not scale to whole-transcriptome panels (27k features × ~500k tiles).
  • Plain points (no index) are still written as 0.2.
  • Parquet fixes found on real data (proposed for the spec): per-row-group compact dictionaries + global feature/<key>/categories; part_offsets so readers open only the footers they need.
  • RFC 4: build_shapes_spatial_index (cells sorted by centroid tile on the points grid, GeoArrow, margin, table reordered to the shapes with X CSR + layers["X_csc"], optional indexed centroids), ShapesSpatialIndex; shapes format 0.4.
  • examples/spatial_index/: two notebooks executed on the Atera kidney dataset of spatialdata-db.
  • Benchmarks: benchmarks/spatial_index_benchmark.py (asv, synthetic), spatial_index_real_data.py, spatial_index_shapes_real_data.py.

Testing

tests/io/test_spatial_index.py: Parquet/Zarr × dense/sparse, query vs brute force at every level, budget, feature-major vs tile-major equality, rebuild keeps p, merge rule example of the RFC. tests/io passes. CI tests are small and fast (7 tests, ~7 s); tests/io/test_spatial_index_real_data.py checks real indexed stores locally (skipped unless SPATIALDATA_SPATIAL_INDEX_REAL_DATA is set) and passes on Atera breast (Parquet, Zarr).

🤖 Generated with Claude Code

…ader, points format 0.3

- build_points_spatial_index / write_points_with_spatial_index: random priority p, levels as bands,
  tile-major table and feature-major tables, tile_offsets, in Parquet or Zarr (df:/enum: conventions)
- PointsSpatialIndex / read_points_spatial_index: query by box, level, budget and features
- feature index: dense as in RFC 3, or sparse (CSR over features, a proposed fix for whole-transcriptome panels)
- PointsFormatV03; _read_points reads the Zarr storage

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@LucaMarconato LucaMarconato self-assigned this Nov 16, 2026
@autofix-troubleshooter

Copy link
Copy Markdown

Hi! I'm the autofix logoautofix.ci troubleshooter bot.

It looks like you correctly set up a CI job that uses the autofix.ci GitHub Action, but the autofix.ci GitHub App has not been installed for this repository. This means that autofix.ci unfortunately does not have the permissions to fix this pull request. If you are the repository owner, please install the app and then restart the CI workflow! 😃

@codecov-commenter

Copy link
Copy Markdown

Welcome to Codecov 🎉

Once you merge this PR into your default branch, you're all set! Codecov will compare coverage reports and display results in all future pull requests.

Thanks for integrating Codecov - We've got you covered ☂️

…-data benchmark script

- build_shapes_spatial_index: sort shapes by the tile of their centroid (optionally on the grid of an
  indexed points element), GeoArrow geometry, tile_offsets and margin; reorder the annotating table to
  the shapes order with X as CSR and layers['X_csc'] as CSC; optionally the centroids as indexed points
- ShapesSpatialIndex: query by box (grown by the margin), gene values from the CSC layer, CSR rows
- benchmarks/spatial_index_real_data.py: queries with and without the index on real data

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@LucaMarconato LucaMarconato changed the title [experimental] Spatial index of points (RFC 3) [experimental] Spatial index of points (RFC 3) and shapes (RFC 4) Nov 16, 2026
LucaMarconato and others added 19 commits November 16, 2026 21:41
…data test; asv benchmark

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
… once; vectorized feature index

With row groups of about one tile and 27k features, a full dictionary per row group was 9 GB of the 23 GB
tile-major table of the Atera breast dataset.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…feature column

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ly the footers a query needs; example notebooks generator

Opening the Atera breast index went from 0.45 s (66.7k row-group footers) to 3 ms; a whole-slide query
of one gene from 0.79 s to 0.06 s.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…xed element into a store

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…a tables annotate the labels); resumable

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…a-db): tiled points and tiled cells

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…offsets, area-weighted

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
RFC 3: same layout in Parquet and Zarr (spatial/, feature/<key>/), spatial_index in zarr.json plus a Parquet
sidecar (part files in order, first rows, global categories); sparse feature index only; offsets int32 when
the rows fit; p stored as k with p = k / 2^p_bits (16 bits by default, more if the coarsest level needs it),
an existing p kept; all spatial axes in the grid (a single tile along z); tile size for the median tile;
Parquet row groups of 250k-1M rows holding several tiles.
RFC 4: the grid of the shapes chosen by the rule of thumb (>50% of the shapes within their tile), optionally
nested with the grid of the points; margin and table in spatial_index; spatial/shapes.parquet with the
sidecar; the table must annotate the shapes.
spatialdata: GeoArrow is the default geometry encoding of shapes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…arse tiles extend beyond it)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ts can represent

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…and shapes

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…o p = P(l) is in a finer band

Found by the real-data check on Atera breast (one point with p = 1/4 read at level 1).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ements without spatial_index

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…oints; tiled cells and table)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <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.

3 participants