Skip to main content

spatialdata-js-util

Python utilities for producing — and reading back — SpatialData stores tuned for incremental reading in the browser by SpatialData.js.

Three optimizations, all aimed at making a viewport cost a few small ranged reads instead of a whole-element download:

Area What it does Why
images Recompress rasters as JPEG 2000 or HTJ2K, and add multiscale pyramids where they are missing Tiles decode in the browser; HTJ2K is markedly cheaper to decode than JPEG 2000. Without a pyramid, a zoomed-out view still costs full-resolution chunks
points Morton-sort Points Parquet with sentinel bbox rows and controlled row groups A viewport maps to a few Parquet row-group reads
tables Convert X/layers from CSR to CSC Reading one gene becomes one contiguous range read, not a scan of every row

Installing the package also registers the image codecs with zarr, so stores written here open in ordinary spatialdata.read_zarr — see Reading stores back in Python.

Every command is available three ways: an interactive TUI (start here), the spatialdata-js-util CLI, and the Python API.

Status: experimental. The HTJ2K codec identifier is not a standard OME-Zarr codec. Stores using it are readable by the SpatialData.js runtime and by Python with this package installed. It is published in the hope that, if useful, the approach can be adopted more broadly.

Install

pip install spatialdata-js-util

That base install is enough to read stores. To write them, add the write extra, which pulls in spatialdata, anndata, dask and scipy:

pip install 'spatialdata-js-util[write]'

Requires Python 3.12+ and spatialdata >= 0.8.0.

Interactive TUI

If you would rather not memorise flags, everything below is also available as a terminal UI. It is the easiest way to get started.

pip install 'spatialdata-js-util[tui]'
spatialdata-js-util tui

# Or pre-fill the store path so you skip the first prompt
spatialdata-js-util tui ~/data/xenium.zarr

Pick a command from the home menu and the UI walks you through it:

IMAGES   Recompress rasters (JPEG 2000 / HTJ2K)
POINTS   List Points elements in a Zarr store
POINTS   Morton-sort Points from Zarr
POINTS   Morton-sort CSV/Parquet file
POINTS   Write multiscale Points Parquet
POINTS   Write index permutations derivative store
TABLES   List table elements in a Zarr store
TABLES   Convert table matrices to CSC
CODECS   Show HTJ2K backend availability

The flow is the same for every command: guided form → confirm → run → report.

  • Forms are pre-filled with sensible defaults, and with the store path you already used this session. Enter advances between fields and submits on the last one; Escape goes back; q quits.

  • Anything that overwrites asks first. Rewriting points/<key>/points.parquet in place, converting tables in place, or replacing an existing output store all stop on a confirmation screen naming the exact path before touching anything.

  • Runs stream their JSON output to a log pane, so a long recompression shows progress rather than appearing to hang.

  • The recompress form carries the pyramid controls, so single-resolution input can be made browser-ready without leaving the UI.

  • Writes are verified afterwards and the report screen shows a pass/fail table. For Morton writes that means:

    Check Meaning
    column_present morton_code_2d column exists
    sentinel_prefix First 2–4 rows have morton_code_2d == 0
    sentinel_bbox Sentinel rows encode the full dataset x/y bounds
    morton_monotonic Morton codes non-decreasing after sentinels
    row_group_sentinels Row group 0 contains only sentinel rows
    no_uint_intermediates No persisted *_uint staging columns

    Multiscale and index-permutation runs show schema/manifest checks instead.

CODECS → Show HTJ2K backend availability is a good first stop: it tells you which codec backend your environment resolved to, and whether it passed the multi-component probe described below.

Reading stores back in Python

import spatialdata as sd

sdata = sd.read_zarr("store-htj2k.zarr")   # HTJ2K/JP2K images decode transparently

No import of this package and no register_codecs() call is needed — the codecs are advertised through zarr.codecs entry points, so zarr finds them as soon as the distribution is installed. (spatialdata_js_util.codecs.register_codecs() exists only for running from a source tree with no installed metadata.)

HTJ2K backends, and why there is a probe

HTJ2K encode/decode can go through either of two OpenJPH builds:

  • imagecodecs — native OpenJPH, pure pip install, no Node.js. Preferred.
  • openjph-wasm — the vendored WASM the JS reader uses, driven by Node.js worker processes. Requires node on PATH.

A WASM build previously used in this project silently decoded every component of a multi-component codestream as component 0 — losing volumetric planes with no error (see docs/multi-component-codec-findings.md). Because that failure is silent, a backend is not trusted on the strength of its library name. imagecodecs is admitted only if it decodes a committed multi-component codestream — produced by the WASM encoder — to exactly the expected samples. Any mismatch, and it is rejected in favour of the WASM path.

Check what your environment resolved to:

spatialdata-js-util codecs info

Pin a backend explicitly with SPATIALDATA_JS_UTIL_HTJ2K_BACKEND=imagecodecs or =openjph-wasm (the probe still gates imagecodecs).

Images

Recompress a store, preserving everything that is not an image. Path sources are copied first, so tables, shapes, points and unconfigured rasters survive without the whole object being loaded.

spatialdata-js-util images recompress input.zarr output-htj2k.zarr \
  --codec experimental.openjph_htj2k --preset balanced --chunks auto --overwrite

With --sibling, each original image is kept and a compressed copy added alongside it (morphology_focus:htj2k_balanced), so tools without the codec keep working.

Presets and --quality

--quality is the OpenJPH quantization step: lower means higher fidelity and a larger codestream — not a JPEG-style 0–100 scale. The step is relative to the dtype's full dynamic range, so one input LSB is 1/256 for 8-bit data and 1/65536 for 16-bit.

HTJ2K presets are therefore expressed as a multiple of the input's LSB, and resolve to a concrete step from the raster's dtype:

Preset JPEG 2000 HTJ2K uint8 uint16
lossless reversible, round-trip verified reversible=True — —
balanced level=100 2 LSB 0.0078125 0.000030518
small level=75 5 LSB 0.01953125 0.000076294

A step below one input LSB is strictly worse than lossless: it cannot buy fidelity the data does not have, so the irreversible transform returns a bit-identical image while encoding larger than --preset lossless. Passing such a --quality warns and names the floor. The resolved step is recorded in the manifest under encode_options.

An explicit --quality is an absolute step, not an LSB multiple, so pick it for the raster's dtype — 0.0005 below is ~33 LSB of uint16 morphology, but would be 0.13 LSB (worse than lossless) on 8-bit input:

spatialdata-js-util images recompress input.zarr out.zarr \
  --codec experimental.openjph_htj2k --quality 0.0005 --sibling --overwrite

Multiscale pyramids

Acquisition output is often written at a single resolution. A browser then has no choice but to fetch full-resolution chunks however far out the user is zoomed — which defeats the image codecs rather than complementing them. --pyramid gives any single-level image a pyramid before recompressing it, so one command turns single-resolution input into a browser-ready store:

spatialdata-js-util images recompress input.zarr output.zarr \
  --codec experimental.openjph_htj2k --preset balanced --pyramid --overwrite

To add pyramids without touching codecs:

spatialdata-js-util images add-pyramid input.zarr output.zarr --overwrite

By default levels are chosen automatically: keep halving until the largest spatial axis is --pyramid-min-size (1024) or smaller, so the coarsest level is roughly one chunk. Override with --pyramid-levels N (a total count including full resolution) and --pyramid-downscale K.

Images that already have more than one level are skipped, and the manifest says so; --pyramid-force rebuilds them anyway. Pass --labels to add-pyramid to include label elements — they are downsampled by the label model, so ids are never averaged into values that identify no object.

Full resolution is copied through unchanged; only the added levels are new. Levels are generated through SpatialData's own parsers, which get the per-level scale and half-pixel translation right — hand-written multiscales metadata tends to misalign every level against the full-resolution image.

Pyramids are always written to a different store than the source. SpatialData refuses to delete the files backing a live element, so there is no in-place mode.

Other image notes

Labels are written with Blosc/zstd. Browser image codecs support uint8, int8, uint16 and int16 only. A sidecar manifest records the expanded config, per-raster stats, the codec backend used, and decoded checksums:

spatialdata-js-util images inspect output-htj2k.manifest.json

For repeatable multi-image runs, use a JSON config:

{
  "default_image": { "codec": "imagecodecs_jpeg2k", "preset": "lossless", "chunks": "auto" },
  "images": {
    "morphology_focus": { "preset": "balanced" },
    "he_image": { "codec": "experimental.openjph_htj2k", "quality": 0.001 }
  },
  "default_labels": { "codec": "blosc", "clevel": 5 }
}

Points

Morton-sorted Points Parquet, Vitessce-compatible:

  • morton_code_2d added using 16 bits per axis
  • the first 2–4 rows are sentinels with morton_code_2d == 0 encoding the full point bounding box, so readers can get bounds without a scan
  • {feature_key}_codes added when the element declares a feature_key
  • string/categorical columns moved to the right of the table
  • row-group size controlled, so a viewport maps to a few row-group reads
# List Points elements
spatialdata-js-util points list ~/data/xenium.zarr

# Morton-sort transcripts in place on points/<key>/points.parquet
spatialdata-js-util points morton-from-zarr ~/data/xenium.zarr --points-key transcripts

# Morton-sort a CSV or Parquet file
spatialdata-js-util points morton input.csv output.parquet --feature-key feature_name

Morton v1 belongs on the canonical element path points/<key>/points.parquet. Use --experimental only for layouts standard readers cannot consume (see ADR 0002).

points index-permutations builds a derivative store with several sort layouts side by side for benchmarking. All of these are also available from the TUI, which verifies the output after each write.

Multiscale Zarr points (experimental)

points zarr writes the layout of merfisheyes' points-zarr spec draft 0.1: levels of uniform random samples (each a quarter of the one below), each stored in tile order and in feature order, with a row index. A viewer can then draw any zoom from a bounded read, and read only the rows of the genes selected.

# A new store: a standard points element, with the copy beside it
spatialdata-js-util points zarr transcripts.parquet transcripts.zarr \
  --feature-key feature_name --coordinates x_location,y_location

# Add the copy to an existing store's points element, from its own points.parquet
spatialdata-js-util points add-zarr ~/data/xenium.zarr --points-key transcripts

The copy is the element's experimental.points-zarr subgroup; the element itself stays an ordinary SpatialData points element, so spatialdata and other standard readers read the store as before (ADR 0006). Writing works out of core: Parquet input is read in batches, spilled to temporary files beside the output (about 18 bytes a point, plus one level's sorted copy) and sorted one band of tile rows at a time.

On exFAT (and other filesystems without extended attributes) macOS writes a ._name file beside each file that carries one, which doubles the file count and breaks readers that list a directory (pyarrow reads ._part.0.parquet as a part). Delete them after writing: find STORE -name '._*' -delete.

Tables

# Rewrite every table's X (and named layers) as CSC, in place
spatialdata-js-util tables to-csc ~/data/xenium.zarr

# Or write a converted copy, leaving the source alone
spatialdata-js-util tables to-csc input.zarr output-csc.zarr --overwrite

The conversion changes only the sparse layout, not the values, and the result stays readable by any AnnData client. Dense matrices are left alone unless you pass --densify, since sparsifying a dense matrix can make it bigger.

Only the matrices are rewritten — obs, var, uns and the rest are left byte-for-byte alone, and what is written is pinned to the store's existing encodings (no sharding, no nullable-string-array index). Re-serialising a whole table would otherwise silently re-encode it with whatever conventions the installed AnnData currently prefers, which can leave readers unable to find the variable names even though every value is still present.

from spatialdata_js_util import convert_store_tables_to_csc, to_csc

convert_store_tables_to_csc("store.zarr")     # whole store
csc = to_csc(adata.X)                         # or just a matrix

Python API

from spatialdata_js_util import (
    recompress_spatialdata,
    add_pyramids,
    has_pyramid,
    write_morton_points_parquet,
    convert_store_tables_to_csc,
    backend_report,
)

result = recompress_spatialdata(
    "input.zarr", "output-htj2k.zarr",
    codec="experimental.openjph_htj2k",
    preset="balanced",
    pyramid=True,          # add levels to single-resolution images first
    overwrite=True,
)
print(result.manifest_path)

# Or pyramids on their own
has_pyramid("input.zarr", "images", "morphology")   # -> False
add_pyramids("input.zarr", "output.zarr", levels=4)

Monorepo development

From the repository root:

pnpm test:python

The vendored openjph-wasm assets under src/spatialdata_js_util/codecs/vendor/openjph/ are gitignored and produced by node scripts/vendor-openjph-for-python.mjs, which pnpm test:python runs first. They are only needed for the WASM backend; the imagecodecs backend works without them.

Fixture-generation scripts under scripts/ are repo-local and not shipped in the wheel. To regenerate the backend probe fixture (requires Node.js):

uv run --directory python/spatialdata-js-util python scripts/build_htj2k_probe.py

TypeScript callers can use encodeHtj2kPlane() from zarrextra directly; see docs/htj2k-wasm-encode-design.md.

Metadata

Release files for spatialdata-js-util 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for spatialdata-js-util 0.1.0
File Size Uploaded Provenance Actions
spatialdata_js_util-0.1.0.tar.gz 301.7 kB Without provenance Details for spatialdata_js_util-0.1.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for spatialdata-js-util 0.1.0
File Interpreter ABI Platform Actions
spatialdata_js_util-0.1.0-py3-none-any.whl Python 3 none any Details for spatialdata_js_util-0.1.0-py3-none-any.whl

Total release size: 576.6 kB

Release files / spatialdata_js_util-0.1.0.tar.gz

Download URL spatialdata_js_util-0.1.0.tar.gz
Size 301.7 kB
Tags Source
SHA-256 checksum
How to use checksums
b384a97d89e8410d4d6720200d8df2ea47f3a4bbd1f89a7f4869994a3edba7f7
BLAKE2b-256 checksum
How to use checksums
71fd79b2aa7b579d3664fdd252c7b901d09f9bf31247be079fd41595fff8fea1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.13.5 {"installer":{"name":"uv","version":"0.13.5","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Release files / spatialdata_js_util-0.1.0-py3-none-any.whl

Download URL spatialdata_js_util-0.1.0-py3-none-any.whl
Size 274.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
8f249e6182637a8d0a8bd90f0f84d10e92911281a28d1196ffd274fb6521068f
BLAKE2b-256 checksum
How to use checksums
f5791d739c4fc3ecde308c140f4d1c3f447d601440985706656111ef4567902c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.13.5 {"installer":{"name":"uv","version":"0.13.5","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Security for spatialdata-js-util 0.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page