diff --git a/docs/_static/img/spatialdata_horizontal.png b/docs/_static/img/spatialdata_horizontal.png index 5683233e5..30b14a7e5 100644 Binary files a/docs/_static/img/spatialdata_horizontal.png and b/docs/_static/img/spatialdata_horizontal.png differ diff --git a/docs/changelog.md b/docs/changelog.md index 4b6c2186f..87f818afc 100644 --- a/docs/changelog.md +++ b/docs/changelog.md @@ -1,17 +1,4 @@ # Changelog Please refer directly to the [Releases](https://github.com/scverse/spatialdata/releases) section on GitHub, where you can find curated release notes for each release. -For developers, please consult the [contributing guide](https://github.com/scverse/spatialdata/blob/main/docs/contributing.md), which explains how to keep release notes are up-to-date at each release. - -## Unreleased - -### Changed - -- `SpatialData.read()` now only accepts a path or URL (`str`, `Path` or `UPath`) for `file_path`; passing an already-open `zarr.Group` raises a `TypeError`. Use `spatialdata.read_zarr()` to read from a `zarr.Group`. -- `SpatialData.attrs` is now typed as `dict[str, JSONValue]` instead of `dict[Any, Any]`, making explicit the pre-existing invariant that the attrs must be JSON-serializable (they are stored as Zarr attributes). Accordingly, `SpatialData.get_attrs()` is now typed as returning `JSONValue | pd.DataFrame`: the previous annotation did not cover attrs values such as lists, numbers, booleans and `None`, which have always been valid and are returned as-is when `return_as=None`. This is a typing-only change, the runtime behaviour is unchanged. -- `SpatialData.locate_element()` now accepts a table (`AnnData`) in addition to a `SpatialElement`; tables were already located correctly, but the annotation did not allow passing them. This is a typing-only change, the runtime behaviour is unchanged. - -### Fixed - -- `aggregate(values=, by=)` no longer crashes with `AttributeError: 'DataTree' object has no attribute 'dtype'`. Multiscale labels passed as `by` now aggregate to the same table as their full-resolution counterpart. -- Querying a `SpatialData` object with multiple (batched) bounding boxes now raises an explicit `NotImplementedError` instead of failing with an opaque `AssertionError`. Batched queries are supported when querying a `SpatialElement` directly, but not when querying a `SpatialData` object, since that would require returning one `SpatialData` object per bounding box. +For developers, please consult the [contributing guide](https://github.com/scverse/spatialdata/blob/main/docs/contributing.md), which explains how to keep release notes up-to-date at each release. diff --git a/docs/conf.py b/docs/conf.py index 681558756..f97b5d77c 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -41,6 +41,10 @@ } autodoc_process_signature = True myst_url_schemes = ("http", "https", "mailto") +myst_enable_extensions = [*DEFAULTS.myst_enable_extensions, "substitution"] +myst_substitutions = { + "requires_python": info["Requires-Python"], +} typehints_defaults = "braces" always_use_bars_union = True # use `|` instead of `Union` in types even when building with Python ≤3.14 diff --git a/docs/index.md b/docs/index.md index 58f1bb4e3..2797559d8 100644 --- a/docs/index.md +++ b/docs/index.md @@ -6,12 +6,16 @@ # An open and universal framework for processing spatial omics data. -SpatialData is a data framework, a schema and a serialization format for uni- and multi-modal spatial omics datasets. It is supported by a growing suite of software libraries for performant access, manipulation and analysis in multiple languages, including Python, R and JavaScript. +SpatialData is a data framework, a schema and a serialization format for uni- and multi-modal spatial omics datasets. +It is supported by a growing suite of software libraries for performant access, manipulation and analysis in multiple languages, including Python, R and JavaScript. -This site documents how to install, use and extend the Python `spatialdata` library. For language-independent definitions of SpatialData datasets and their on-disk representation, see the [SpatialData specifications](https://spatialdata-spec.scverse.org/). +The Python `spatialdata` library holds images, segmentation masks, points, shapes and annotation tables together in one {class}`~spatialdata.SpatialData` object, aligned through shared coordinate systems. +It lets you query, crop and aggregate across these elements, read and write the SpatialData Zarr format with lazy access to large data, and exchange data with AnnData, scanpy, squidpy and other tools ({doc}`interoperability`). -See the links below to learn more about other packages in the SpatialData ecosystem. +This site documents how to install, use and extend it. +See the links below to learn more about the specifications and other packages in the SpatialData ecosystem. +- [SpatialData specifications](https://spatialdata-spec.scverse.org/): language-independent definitions of SpatialData datasets and their on-disk representation. - `spatialdata-io`: load data from common spatial omics technologies into `spatialdata` ([repository][spatialdata-io-repo], [documentation][spatialdata-io-docs]). - `spatialdata-plot`: Static plotting library for `spatialdata` ([repository][spatialdata-plot-repo], [documentation][spatialdata-plot-docs]). - `napari-spatialdata`: napari plugin for interactive exploration and annotation of `spatialdata` ([repository][napari-spatialdata-repo], [documentation][napari-spatialdata-docs]). @@ -21,20 +25,17 @@ See the links below to learn more about other packages in the SpatialData ecosys Please see our publication {cite}`marconatoSpatialDataOpenUniversal2024` for citation and to learn more. :::{note} -With dask >= 2025.2.0, users can get an error as described in [#1077](https://github.com/scverse/spatialdata/issues/1064). While we tried implementing fixes in SpatialData, it can be that -users perform operations on the `Points` data themselves and get this error. In order to prevent it, users can use a context manager we created. +With dask’s partition fusion enabled (the default), user code that operates on multi-partition `Points` dataframes can fail as described in [#1064](https://github.com/scverse/spatialdata/issues/1064) and [dask#12193](https://github.com/dask/dask/issues/12193). +`spatialdata` avoids this internally; for your own operations, wrap them in `spatialdata.disable_dask_tune_optimization`: ```python -from spatialdata import disable_dask_tune_optimization import contextlib -... + +from spatialdata import disable_dask_tune_optimization with disable_dask_tune_optimization() if data.npartitions > 1 else contextlib.nullcontext(): - + ... # your operation on the points dask dataframe ``` - -This will disable dask graph optimization if the dataframe has more than 1 partition and otherwise keep it enabled. This solves -the problem discussed in this [dask issue](https://github.com/dask/dask/issues/12193). We are looking into an upstream fix. ::: [//]: # "numfocus-fiscal-sponsor-attribution" @@ -51,58 +52,6 @@ If you like scverse® and want to support our mission, please consider making a -```{eval-rst} -.. note:: - This library is currently under active development. We may make changes to the API between versions as the community provides feedback. To ensure reproducibility, please make note of the version you are developing against. -``` - - -```{eval-rst} -.. card:: Installation - :link: installation - :link-type: doc - - Learn how to install ``spatialdata``. - -.. card:: How-to guides - :link: user_guide - :link-type: doc - - Get help with common questions and tasks in ``spatialdata``. - -.. card:: Tutorials - :link: tutorials/notebooks/notebooks - :link-type: doc - - Learn how to use ``spatialdata`` with detailed hands-on examples. - -.. card:: API - :link: api - :link-type: doc - - Find a detailed documentation of ``spatialdata``. - -.. card:: Datasets - :link: tutorials/notebooks/datasets/README - :link-type: doc - - Example datasets from 8 different technologies. - -.. card:: Design document - :link: design_doc - :link-type: doc - - Learn about the design approach behind ``spatialdata``. - -.. card:: Contributing - :link: contributing - :link-type: doc - - Learn how to contribute to ``spatialdata``. - -``` - - ```{toctree} :caption: Getting started :hidden: true diff --git a/docs/installation.md b/docs/installation.md index a75cde83f..292ece56f 100644 --- a/docs/installation.md +++ b/docs/installation.md @@ -1,13 +1,14 @@ # Installation -`spatialdata` requires Python (the minimum required version is specified in PyPI) to run and the installation time requires a few minutes on a standard desktop computer. +`spatialdata` requires Python {{ requires_python }}. +Installation takes a few minutes on a standard desktop computer. ## PyPI Install `spatialdata` by running: ```bash - pip install spatialdata +pip install spatialdata ``` ## Visualization and readers diff --git a/docs/quickstart.md b/docs/quickstart.md index 3b9c4e537..2b4b41314 100644 --- a/docs/quickstart.md +++ b/docs/quickstart.md @@ -1,32 +1,54 @@ # Quickstart A {class}`~spatialdata.SpatialData` object holds the images, labels, points, shapes and tables of a dataset, aligned through shared coordinate systems. -The example below loads a small synthetic dataset, crops it, counts points per shape, and writes it to and reads it back from Zarr. +The example below builds one from scratch out of an image, a segmentation mask and a gene expression table, then crops it to a region of interest (ROI), measures image intensity per cell in the ROI, and writes the ROI to and reads it back from Zarr. ```python +import anndata as ad +import numpy as np +import pandas as pd + import spatialdata as sd -from spatialdata.datasets import blobs +from spatialdata.models import Image2DModel, Labels2DModel, TableModel + +rng = np.random.default_rng(0) + +# A 3-channel image (c, y, x) ... +image = rng.random((3, 100, 100)) -sdata = blobs() +# ... a segmentation mask with three cells, labelled 1, 2, 3 (0 is background) ... +mask = np.zeros((100, 100), dtype=np.int32) +mask[10:30, 10:30] = 1 +mask[40:70, 15:45] = 2 +mask[30:60, 60:90] = 3 + +# ... and a cells x genes expression matrix, one row per cell in the mask. +expression = ad.AnnData( + X=rng.poisson(5, size=(3, 4)).astype(float), + obs=pd.DataFrame({"region": pd.Categorical(["cells"] * 3), "cell_id": [1, 2, 3]}, index=["a", "b", "c"]), + var=pd.DataFrame(index=["GeneA", "GeneB", "GeneC", "GeneD"]), +) -crop = sd.bounding_box_query( +sdata = sd.SpatialData( + images={"image": Image2DModel.parse(image, dims=("c", "y", "x"))}, + labels={"cells": Labels2DModel.parse(mask, dims=("y", "x"))}, + tables={"table": TableModel.parse(expression, region="cells", region_key="region", instance_key="cell_id")}, +) + +# Crop all elements to a region of interest; the table keeps only the cells inside it. +sdata_ROI = sd.bounding_box_query( sdata, axes=("x", "y"), min_coordinate=[0, 0], - max_coordinate=[200, 200], + max_coordinate=[50, 50], target_coordinate_system="global", ) -counts = sd.aggregate( - values=sdata["blobs_points"], - by=sdata["blobs_circles"], - value_key="genes", - agg_func="count", - target_coordinate_system="global", -) +# Mean intensity of each image channel per cell in the ROI. +intensities = sd.aggregate(values=sdata_ROI["image"], by=sdata_ROI["cells"], agg_func="mean") -sdata.write("blobs.zarr") -sdata = sd.read_zarr("blobs.zarr") +sdata_ROI.write("my_roi.zarr") +sdata_ROI = sd.read_zarr("my_roi.zarr") ``` To load data from a specific technology, see [spatialdata-io](https://spatialdata.scverse.org/projects/io/en/stable/), and to plot it, [spatialdata-plot](https://spatialdata.scverse.org/projects/plot/en/stable/).