From f0e67d6e865546af4acbbc5a191dbaee9da30b1c Mon Sep 17 00:00:00 2001 From: Artur-man Date: Sun, 17 May 2026 00:43:25 +0200 Subject: [PATCH 1/4] add python scripts --- inst/scripts/spatialdata-python/main.ipynb | 196 +++++++++++++++++++++ 1 file changed, 196 insertions(+) create mode 100644 inst/scripts/spatialdata-python/main.ipynb diff --git a/inst/scripts/spatialdata-python/main.ipynb b/inst/scripts/spatialdata-python/main.ipynb new file mode 100644 index 00000000..a78adb0d --- /dev/null +++ b/inst/scripts/spatialdata-python/main.ipynb @@ -0,0 +1,196 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "26ec4965", + "metadata": {}, + "source": [ + "Here is a quick way to build and environment to follow the scripts through." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "309770df", + "metadata": {}, + "outputs": [], + "source": [ + "%conda create --name sd_env python=3.13.0\n", + "%conda activate sd_env\n", + "%pip install spatialdata==0.7.3 spatialdata_plot==0.3.4" + ] + }, + { + "cell_type": "markdown", + "id": "f7a51ad4", + "metadata": {}, + "source": [ + "We will need `spatialdata` and `spatialdata-plot`." + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "56975714", + "metadata": {}, + "outputs": [], + "source": [ + "import spatialdata as sd\n", + "import spatialdata_plot as spd\n", + "from spatialdata.datasets import blobs" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "id": "517a3be4", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "SpatialData object\n", + "├── Images\n", + "│ ├── 'blobs_image': DataArray[cyx] (3, 512, 512)\n", + "│ └── 'blobs_multiscale_image': DataTree[cyx] (3, 512, 512), (3, 256, 256), (3, 128, 128)\n", + "├── Labels\n", + "│ ├── 'blobs_labels': DataArray[yx] (512, 512)\n", + "│ └── 'blobs_multiscale_labels': DataTree[yx] (512, 512), (256, 256), (128, 128)\n", + "├── Points\n", + "│ └── 'blobs_points': DataFrame with shape: (, 4) (2D points)\n", + "├── Shapes\n", + "│ ├── 'blobs_circles': GeoDataFrame shape: (5, 2) (2D shapes)\n", + "│ ├── 'blobs_multipolygons': GeoDataFrame shape: (2, 1) (2D shapes)\n", + "│ └── 'blobs_polygons': GeoDataFrame shape: (5, 1) (2D shapes)\n", + "└── Tables\n", + " └── 'table': AnnData (26, 3)\n", + "with coordinate systems:\n", + " ▸ 'global', with elements:\n", + " blobs_image (Images), blobs_multiscale_image (Images), blobs_labels (Labels), blobs_multiscale_labels (Labels), blobs_points (Points), blobs_circles (Shapes), blobs_multipolygons (Shapes), blobs_polygons (Shapes)" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "sdata = blobs()\n", + "sdata" + ] + }, + { + "cell_type": "markdown", + "id": "c1f2f65c", + "metadata": {}, + "source": [ + "# Accession" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "770341a0", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + " Size: 6MB\n", + "dask.array\n", + "Coordinates:\n", + " * c (c) int64 24B 0 1 2\n", + " * y (y) float64 4kB 0.5 1.5 2.5 3.5 4.5 ... 508.5 509.5 510.5 511.5\n", + " * x (x) float64 4kB 0.5 1.5 2.5 3.5 4.5 ... 508.5 509.5 510.5 511.5\n", + "Attributes:\n", + " transform: {'global': Identity }, 'blobs_multiscale_image': \n", + "Group: /\n", + "├── Group: /scale0\n", + "│ Dimensions: (c: 3, y: 512, x: 512)\n", + "│ Coordinates:\n", + "│ * c (c) int64 24B 0 1 2\n", + "│ * y (y) float64 4kB 0.5 1.5 2.5 3.5 4.5 ... 508.5 509.5 510.5 511.5\n", + "│ * x (x) float64 4kB 0.5 1.5 2.5 3.5 4.5 ... 508.5 509.5 510.5 511.5\n", + "│ Data variables:\n", + "│ image (c, y, x) float64 6MB dask.array\n", + "├── Group: /scale1\n", + "│ Dimensions: (c: 3, y: 256, x: 256)\n", + "│ Coordinates:\n", + "│ * c (c) int64 24B 0 1 2\n", + "│ * y (y) float64 2kB 1.0 3.0 5.0 7.0 9.0 ... 505.0 507.0 509.0 511.0\n", + "│ * x (x) float64 2kB 1.0 3.0 5.0 7.0 9.0 ... 505.0 507.0 509.0 511.0\n", + "│ Data variables:\n", + "│ image (c, y, x) float64 2MB dask.array\n", + "└── Group: /scale2\n", + " Dimensions: (c: 3, y: 128, x: 128)\n", + " Coordinates:\n", + " * c (c) int64 24B 0 1 2\n", + " * y (y) float64 1kB 2.0 6.0 10.0 14.0 18.0 ... 498.0 502.0 506.0 510.0\n", + " * x (x) float64 1kB 2.0 6.0 10.0 14.0 18.0 ... 498.0 502.0 506.0 510.0\n", + " Data variables:\n", + " image (c, y, x) float64 393kB dask.array}>" + ] + }, + "execution_count": 15, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "sdata.images[\"blobs_image\"]\n", + "sdata[\"blobs_image\"]" + ] + }, + { + "cell_type": "markdown", + "id": "51784a1c", + "metadata": {}, + "source": [ + "# Transformations" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "id": "b654ddb3", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "Identity " + ] + }, + "execution_count": 16, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "from spatialdata.transformations import get_transformation\n", + "get_transformation(sdata.images[\"blobs_image\"])\n" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "sd_env", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.13.0" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} From 6929d942e3887a4f85f3fd05c3f1979ec8fc99cb Mon Sep 17 00:00:00 2001 From: Artur-man Date: Sun, 17 May 2026 14:25:22 +0200 Subject: [PATCH 2/4] initial script --- inst/scripts/spatialdata-python/main.ipynb | 802 +++++++++++++++++++-- 1 file changed, 755 insertions(+), 47 deletions(-) diff --git a/inst/scripts/spatialdata-python/main.ipynb b/inst/scripts/spatialdata-python/main.ipynb index a78adb0d..2e403ece 100644 --- a/inst/scripts/spatialdata-python/main.ipynb +++ b/inst/scripts/spatialdata-python/main.ipynb @@ -30,32 +30,75 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 1, "id": "56975714", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "/Users/amanuky/miniforge3/envs/sd_env/lib/python3.13/site-packages/tqdm/auto.py:21: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html\n", + " from .autonotebook import tqdm as notebook_tqdm\n" + ] + } + ], "source": [ + "import os\n", "import spatialdata as sd\n", "import spatialdata_plot as spd\n", - "from spatialdata.datasets import blobs" + "from spatialdata.transformations import get_transformation" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "84e2aeda", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'/Users/amanuky/Dropbox/Research/MDC/Projects/SpatialData/Packages/SpatialData/inst/scripts/spatialdata-python'" + ] + }, + "execution_count": 2, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "os.getcwd()" ] }, { "cell_type": "code", - "execution_count": 6, + "execution_count": 3, "id": "517a3be4", "metadata": {}, "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "/var/folders/vf/d8kg507x41xfh6z9vgv9skksdsn29w/T/ipykernel_26344/477556509.py:1: UserWarning: SpatialData is not stored in the most current format. If you want to use Zarr v3, please write the store to a new location using `sdata.write()`.\n", + " sdata = sd.read_zarr(\"../../extdata/blobs.zarr\")\n", + "no parent found for : None\n", + "no parent found for : None\n", + "/Users/amanuky/miniforge3/envs/sd_env/lib/python3.13/site-packages/zarr/core/group.py:3289: ZarrUserWarning: Object at zmetadata is not recognized as a component of a Zarr hierarchy.\n", + " warnings.warn(\n" + ] + }, { "data": { "text/plain": [ - "SpatialData object\n", + "SpatialData object, with associated Zarr store: /Users/amanuky/Dropbox/Research/MDC/Projects/SpatialData/Packages/SpatialData/inst/extdata/blobs.zarr\n", "├── Images\n", - "│ ├── 'blobs_image': DataArray[cyx] (3, 512, 512)\n", - "│ └── 'blobs_multiscale_image': DataTree[cyx] (3, 512, 512), (3, 256, 256), (3, 128, 128)\n", + "│ ├── 'blobs_image': DataArray[cyx] (3, 64, 64)\n", + "│ └── 'blobs_multiscale_image': DataTree[cyx] (3, 64, 64), (3, 32, 32), (3, 16, 16)\n", "├── Labels\n", - "│ ├── 'blobs_labels': DataArray[yx] (512, 512)\n", - "│ └── 'blobs_multiscale_labels': DataTree[yx] (512, 512), (256, 256), (128, 128)\n", + "│ ├── 'blobs_labels': DataArray[yx] (64, 64)\n", + "│ └── 'blobs_multiscale_labels': DataTree[yx] (64, 64), (32, 32), (16, 16)\n", "├── Points\n", "│ └── 'blobs_points': DataFrame with shape: (, 4) (2D points)\n", "├── Shapes\n", @@ -63,19 +106,27 @@ "│ ├── 'blobs_multipolygons': GeoDataFrame shape: (2, 1) (2D shapes)\n", "│ └── 'blobs_polygons': GeoDataFrame shape: (5, 1) (2D shapes)\n", "└── Tables\n", - " └── 'table': AnnData (26, 3)\n", + " └── 'table': AnnData (10, 3)\n", "with coordinate systems:\n", + " ▸ 'affine', with elements:\n", + " blobs_labels (Labels)\n", " ▸ 'global', with elements:\n", - " blobs_image (Images), blobs_multiscale_image (Images), blobs_labels (Labels), blobs_multiscale_labels (Labels), blobs_points (Points), blobs_circles (Shapes), blobs_multipolygons (Shapes), blobs_polygons (Shapes)" + " blobs_image (Images), blobs_multiscale_image (Images), blobs_labels (Labels), blobs_multiscale_labels (Labels), blobs_points (Points), blobs_circles (Shapes), blobs_multipolygons (Shapes), blobs_polygons (Shapes)\n", + " ▸ 'scale', with elements:\n", + " blobs_labels (Labels)\n", + " ▸ 'sequence', with elements:\n", + " blobs_labels (Labels)\n", + " ▸ 'translation', with elements:\n", + " blobs_labels (Labels)" ] }, - "execution_count": 6, + "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "sdata = blobs()\n", + "sdata = sd.read_zarr(\"../../extdata/blobs.zarr\")\n", "sdata" ] }, @@ -95,49 +146,665 @@ "outputs": [ { "data": { + "text/html": [ + "
\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "
<xarray.DataArray 'image' (c: 3, y: 64, x: 64)> Size: 98kB\n",
+       "dask.array<from-zarr, shape=(3, 64, 64), dtype=float64, chunksize=(3, 64, 64), chunktype=numpy.ndarray>\n",
+       "Coordinates:\n",
+       "  * c        (c) int64 24B 0 1 2\n",
+       "  * y        (y) float64 512B 0.5 1.5 2.5 3.5 4.5 ... 59.5 60.5 61.5 62.5 63.5\n",
+       "  * x        (x) float64 512B 0.5 1.5 2.5 3.5 4.5 ... 59.5 60.5 61.5 62.5 63.5\n",
+       "Attributes:\n",
+       "    transform:  {'global': Identity }
" + ], "text/plain": [ - " Size: 6MB\n", - "dask.array\n", + " Size: 98kB\n", + "dask.array\n", "Coordinates:\n", " * c (c) int64 24B 0 1 2\n", - " * y (y) float64 4kB 0.5 1.5 2.5 3.5 4.5 ... 508.5 509.5 510.5 511.5\n", - " * x (x) float64 4kB 0.5 1.5 2.5 3.5 4.5 ... 508.5 509.5 510.5 511.5\n", + " * y (y) float64 512B 0.5 1.5 2.5 3.5 4.5 ... 59.5 60.5 61.5 62.5 63.5\n", + " * x (x) float64 512B 0.5 1.5 2.5 3.5 4.5 ... 59.5 60.5 61.5 62.5 63.5\n", "Attributes:\n", - " transform: {'global': Identity }, 'blobs_multiscale_image': \n", - "Group: /\n", - "├── Group: /scale0\n", - "│ Dimensions: (c: 3, y: 512, x: 512)\n", - "│ Coordinates:\n", - "│ * c (c) int64 24B 0 1 2\n", - "│ * y (y) float64 4kB 0.5 1.5 2.5 3.5 4.5 ... 508.5 509.5 510.5 511.5\n", - "│ * x (x) float64 4kB 0.5 1.5 2.5 3.5 4.5 ... 508.5 509.5 510.5 511.5\n", - "│ Data variables:\n", - "│ image (c, y, x) float64 6MB dask.array\n", - "├── Group: /scale1\n", - "│ Dimensions: (c: 3, y: 256, x: 256)\n", - "│ Coordinates:\n", - "│ * c (c) int64 24B 0 1 2\n", - "│ * y (y) float64 2kB 1.0 3.0 5.0 7.0 9.0 ... 505.0 507.0 509.0 511.0\n", - "│ * x (x) float64 2kB 1.0 3.0 5.0 7.0 9.0 ... 505.0 507.0 509.0 511.0\n", - "│ Data variables:\n", - "│ image (c, y, x) float64 2MB dask.array\n", - "└── Group: /scale2\n", - " Dimensions: (c: 3, y: 128, x: 128)\n", - " Coordinates:\n", - " * c (c) int64 24B 0 1 2\n", - " * y (y) float64 1kB 2.0 6.0 10.0 14.0 18.0 ... 498.0 502.0 506.0 510.0\n", - " * x (x) float64 1kB 2.0 6.0 10.0 14.0 18.0 ... 498.0 502.0 506.0 510.0\n", - " Data variables:\n", - " image (c, y, x) float64 393kB dask.array}>" + " transform: {'global': Identity }" ] }, - "execution_count": 15, + "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sdata.images[\"blobs_image\"]\n", + "sdata.get(\"blobs_image\")\n", "sdata[\"blobs_image\"]" ] }, @@ -151,7 +818,28 @@ }, { "cell_type": "code", - "execution_count": 16, + "execution_count": 8, + "id": "5ef10d9e", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['global', 'scale', 'affine', 'translation', 'sequence']" + ] + }, + "execution_count": 8, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "sdata.coordinate_systems" + ] + }, + { + "cell_type": "code", + "execution_count": 9, "id": "b654ddb3", "metadata": {}, "outputs": [ @@ -161,15 +849,35 @@ "Identity " ] }, - "execution_count": 16, + "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "from spatialdata.transformations import get_transformation\n", "get_transformation(sdata.images[\"blobs_image\"])\n" ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "27b589eb", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'global': Identity }" + ] + }, + "execution_count": 10, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "get_transformation(sdata.images[\"blobs_image\"], get_all=True).keys()\n" + ] } ], "metadata": { From 0da3a461d46febb62de3b199d726e0bb892d96ab Mon Sep 17 00:00:00 2001 From: Artur-man Date: Sun, 31 May 2026 13:33:44 +0200 Subject: [PATCH 3/4] slight updates to sd-py --- inst/scripts/spatialdata-python/main.ipynb | 90 ++++++++++++---------- 1 file changed, 51 insertions(+), 39 deletions(-) diff --git a/inst/scripts/spatialdata-python/main.ipynb b/inst/scripts/spatialdata-python/main.ipynb index 2e403ece..76a16aa7 100644 --- a/inst/scripts/spatialdata-python/main.ipynb +++ b/inst/scripts/spatialdata-python/main.ipynb @@ -50,27 +50,6 @@ "from spatialdata.transformations import get_transformation" ] }, - { - "cell_type": "code", - "execution_count": 2, - "id": "84e2aeda", - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "'/Users/amanuky/Dropbox/Research/MDC/Projects/SpatialData/Packages/SpatialData/inst/scripts/spatialdata-python'" - ] - }, - "execution_count": 2, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "os.getcwd()" - ] - }, { "cell_type": "code", "execution_count": 3, @@ -81,10 +60,10 @@ "name": "stderr", "output_type": "stream", "text": [ - "/var/folders/vf/d8kg507x41xfh6z9vgv9skksdsn29w/T/ipykernel_26344/477556509.py:1: UserWarning: SpatialData is not stored in the most current format. If you want to use Zarr v3, please write the store to a new location using `sdata.write()`.\n", + "/var/folders/vf/d8kg507x41xfh6z9vgv9skksdsn29w/T/ipykernel_69219/477556509.py:1: UserWarning: SpatialData is not stored in the most current format. If you want to use Zarr v3, please write the store to a new location using `sdata.write()`.\n", " sdata = sd.read_zarr(\"../../extdata/blobs.zarr\")\n", - "no parent found for : None\n", - "no parent found for : None\n", + "no parent found for : None\n", + "no parent found for : None\n", "/Users/amanuky/miniforge3/envs/sd_env/lib/python3.13/site-packages/zarr/core/group.py:3289: ZarrUserWarning: Object at zmetadata is not recognized as a component of a Zarr hierarchy.\n", " warnings.warn(\n" ] @@ -92,7 +71,7 @@ { "data": { "text/plain": [ - "SpatialData object, with associated Zarr store: /Users/amanuky/Dropbox/Research/MDC/Projects/SpatialData/Packages/SpatialData/inst/extdata/blobs.zarr\n", + "SpatialData object, with associated Zarr store: /Users/amanuky/Dropbox/Research/MDC/Projects/SpatialData/Packages/spatialdataR/inst/extdata/blobs.zarr\n", "├── Images\n", "│ ├── 'blobs_image': DataArray[cyx] (3, 64, 64)\n", "│ └── 'blobs_multiscale_image': DataTree[cyx] (3, 64, 64), (3, 32, 32), (3, 16, 16)\n", @@ -140,7 +119,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 4, "id": "770341a0", "metadata": {}, "outputs": [ @@ -696,7 +675,7 @@ " * y (y) float64 512B 0.5 1.5 2.5 3.5 4.5 ... 59.5 60.5 61.5 62.5 63.5\n", " * x (x) float64 512B 0.5 1.5 2.5 3.5 4.5 ... 59.5 60.5 61.5 62.5 63.5\n", "Attributes:\n", - " transform: {'global': Identity }" + " 60.5, 61.5, 62.5, 63.5])
  • transform :
    {'global': Identity }
  • " ], "text/plain": [ " Size: 98kB\n", @@ -797,7 +776,7 @@ " transform: {'global': Identity }" ] }, - "execution_count": 6, + "execution_count": 4, "metadata": {}, "output_type": "execute_result" } @@ -818,17 +797,17 @@ }, { "cell_type": "code", - "execution_count": 8, + "execution_count": 5, "id": "5ef10d9e", "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "['global', 'scale', 'affine', 'translation', 'sequence']" + "['sequence', 'global', 'affine', 'scale', 'translation']" ] }, - "execution_count": 8, + "execution_count": 5, "metadata": {}, "output_type": "execute_result" } @@ -839,7 +818,7 @@ }, { "cell_type": "code", - "execution_count": 9, + "execution_count": 6, "id": "b654ddb3", "metadata": {}, "outputs": [ @@ -849,7 +828,7 @@ "Identity " ] }, - "execution_count": 9, + "execution_count": 6, "metadata": {}, "output_type": "execute_result" } @@ -860,7 +839,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 8, "id": "27b589eb", "metadata": {}, "outputs": [ @@ -870,13 +849,46 @@ "{'global': Identity }" ] }, - "execution_count": 10, + "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "get_transformation(sdata.images[\"blobs_image\"], get_all=True).keys()\n" + "get_transformation(sdata.images[\"blobs_image\"], get_all=True)\n" + ] + }, + { + "cell_type": "markdown", + "id": "af0b9bb4", + "metadata": {}, + "source": [ + "# Merge objects" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "6922d9af", + "metadata": {}, + "outputs": [ + { + "ename": "TypeError", + "evalue": "`sdatas` must be a `Iterable`", + "output_type": "error", + "traceback": [ + "\u001b[31m---------------------------------------------------------------------------\u001b[39m", + "\u001b[31mTypeError\u001b[39m Traceback (most recent call last)", + "\u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[13]\u001b[39m\u001b[32m, line 2\u001b[39m\n\u001b[32m 1\u001b[39m \u001b[38;5;28;01mfrom\u001b[39;00m spatialdata \u001b[38;5;28;01mimport\u001b[39;00m concatenate\n\u001b[32m----> \u001b[39m\u001b[32m2\u001b[39m concatenate(sdata)\n\u001b[32m 3\u001b[39m \u001b[38;5;66;03m# concatenate({\"s1\": sdata, \"s2\": sdata})\u001b[39;00m\n", + "\u001b[36mFile \u001b[39m\u001b[32m~/miniforge3/envs/sd_env/lib/python3.13/site-packages/spatialdata/_core/concatenate.py:143\u001b[39m, in \u001b[36mconcatenate\u001b[39m\u001b[34m(sdatas, region_key, instance_key, concatenate_tables, obs_names_make_unique, modify_tables_inplace, merge_coordinate_systems_on_name, attrs_merge, **kwargs)\u001b[39m\n\u001b[32m 92\u001b[39m \u001b[38;5;250m\u001b[39m\u001b[33;03m\"\"\"\u001b[39;00m\n\u001b[32m 93\u001b[39m \u001b[33;03mConcatenate a list of spatial data objects.\u001b[39;00m\n\u001b[32m 94\u001b[39m \n\u001b[32m (...)\u001b[39m\u001b[32m 140\u001b[39m \u001b[33;03m`_fix_ensure_unique_element_names()` directly.\u001b[39;00m\n\u001b[32m 141\u001b[39m \u001b[33;03m\"\"\"\u001b[39;00m\n\u001b[32m 142\u001b[39m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;28misinstance\u001b[39m(sdatas, Iterable):\n\u001b[32m--> \u001b[39m\u001b[32m143\u001b[39m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mTypeError\u001b[39;00m(\u001b[33m\"\u001b[39m\u001b[33m`sdatas` must be a `Iterable`\u001b[39m\u001b[33m\"\u001b[39m)\n\u001b[32m 145\u001b[39m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28misinstance\u001b[39m(sdatas, \u001b[38;5;28mdict\u001b[39m):\n\u001b[32m 146\u001b[39m sdatas = _fix_ensure_unique_element_names(\n\u001b[32m 147\u001b[39m sdatas,\n\u001b[32m 148\u001b[39m rename_tables=\u001b[38;5;129;01mnot\u001b[39;00m concatenate_tables,\n\u001b[32m (...)\u001b[39m\u001b[32m 151\u001b[39m merge_coordinate_systems_on_name=merge_coordinate_systems_on_name,\n\u001b[32m 152\u001b[39m )\n", + "\u001b[31mTypeError\u001b[39m: `sdatas` must be a `Iterable`" + ] + } + ], + "source": [ + "from spatialdata import concatenate\n", + "concatenate([sdata, sdata])\n", + "# concatenate({\"s1\": sdata, \"s2\": sdata})" ] } ], From 99f9c7fcc7b155e7665d1a9c8300480802b7fc50 Mon Sep 17 00:00:00 2001 From: Artur-man Date: Sun, 31 May 2026 18:30:29 +0200 Subject: [PATCH 4/4] interop.rmd --- inst/scripts/spatialdata-python/main.ipynb | 1689 +++++++++++++++++++- vignettes/interop.Rmd | 86 + 2 files changed, 1732 insertions(+), 43 deletions(-) create mode 100644 vignettes/interop.Rmd diff --git a/inst/scripts/spatialdata-python/main.ipynb b/inst/scripts/spatialdata-python/main.ipynb index 76a16aa7..dcb1aac5 100644 --- a/inst/scripts/spatialdata-python/main.ipynb +++ b/inst/scripts/spatialdata-python/main.ipynb @@ -30,7 +30,7 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": null, "id": "56975714", "metadata": {}, "outputs": [ @@ -46,13 +46,20 @@ "source": [ "import os\n", "import spatialdata as sd\n", - "import spatialdata_plot as spd\n", - "from spatialdata.transformations import get_transformation" + "import spatialdata_plot as spd" + ] + }, + { + "cell_type": "markdown", + "id": "c1f2f65c", + "metadata": {}, + "source": [ + "# Accession" ] }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 18, "id": "517a3be4", "metadata": {}, "outputs": [ @@ -62,8 +69,8 @@ "text": [ "/var/folders/vf/d8kg507x41xfh6z9vgv9skksdsn29w/T/ipykernel_69219/477556509.py:1: UserWarning: SpatialData is not stored in the most current format. If you want to use Zarr v3, please write the store to a new location using `sdata.write()`.\n", " sdata = sd.read_zarr(\"../../extdata/blobs.zarr\")\n", - "no parent found for : None\n", - "no parent found for : None\n", + "no parent found for : None\n", + "no parent found for : None\n", "/Users/amanuky/miniforge3/envs/sd_env/lib/python3.13/site-packages/zarr/core/group.py:3289: ZarrUserWarning: Object at zmetadata is not recognized as a component of a Zarr hierarchy.\n", " warnings.warn(\n" ] @@ -99,7 +106,7 @@ " blobs_labels (Labels)" ] }, - "execution_count": 3, + "execution_count": 18, "metadata": {}, "output_type": "execute_result" } @@ -109,14 +116,6 @@ "sdata" ] }, - { - "cell_type": "markdown", - "id": "c1f2f65c", - "metadata": {}, - "source": [ - "# Accession" - ] - }, { "cell_type": "code", "execution_count": 4, @@ -787,12 +786,89 @@ "sdata[\"blobs_image\"]" ] }, + { + "cell_type": "code", + "execution_count": 15, + "id": "6922d9af", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "SpatialData object\n", + "├── Images\n", + "│ ├── 'blobs_image-s1': DataArray[cyx] (3, 64, 64)\n", + "│ ├── 'blobs_image-s2': DataArray[cyx] (3, 64, 64)\n", + "│ ├── 'blobs_multiscale_image-s1': DataTree[cyx] (3, 64, 64), (3, 32, 32), (3, 16, 16)\n", + "│ └── 'blobs_multiscale_image-s2': DataTree[cyx] (3, 64, 64), (3, 32, 32), (3, 16, 16)\n", + "├── Labels\n", + "│ ├── 'blobs_labels-s1': DataArray[yx] (64, 64)\n", + "│ ├── 'blobs_labels-s2': DataArray[yx] (64, 64)\n", + "│ ├── 'blobs_multiscale_labels-s1': DataTree[yx] (64, 64), (32, 32), (16, 16)\n", + "│ └── 'blobs_multiscale_labels-s2': DataTree[yx] (64, 64), (32, 32), (16, 16)\n", + "├── Points\n", + "│ ├── 'blobs_points-s1': DataFrame with shape: (, 4) (2D points)\n", + "│ └── 'blobs_points-s2': DataFrame with shape: (, 4) (2D points)\n", + "├── Shapes\n", + "│ ├── 'blobs_circles-s1': GeoDataFrame shape: (5, 2) (2D shapes)\n", + "│ ├── 'blobs_circles-s2': GeoDataFrame shape: (5, 2) (2D shapes)\n", + "│ ├── 'blobs_multipolygons-s1': GeoDataFrame shape: (2, 1) (2D shapes)\n", + "│ ├── 'blobs_multipolygons-s2': GeoDataFrame shape: (2, 1) (2D shapes)\n", + "│ ├── 'blobs_polygons-s1': GeoDataFrame shape: (5, 1) (2D shapes)\n", + "│ └── 'blobs_polygons-s2': GeoDataFrame shape: (5, 1) (2D shapes)\n", + "└── Tables\n", + " ├── 'table-s1': AnnData (10, 3)\n", + " └── 'table-s2': AnnData (10, 3)\n", + "with coordinate systems:\n", + " ▸ 'affine-s1-s2-s1-s2', with elements:\n", + " blobs_labels-s1 (Labels), blobs_labels-s2 (Labels)\n", + " ▸ 'global-s1-s2-s1-s2', with elements:\n", + " blobs_image-s1 (Images), blobs_image-s2 (Images), blobs_multiscale_image-s1 (Images), blobs_multiscale_image-s2 (Images), blobs_labels-s1 (Labels), blobs_labels-s2 (Labels), blobs_multiscale_labels-s1 (Labels), blobs_multiscale_labels-s2 (Labels), blobs_points-s1 (Points), blobs_points-s2 (Points), blobs_circles-s1 (Shapes), blobs_circles-s2 (Shapes), blobs_multipolygons-s1 (Shapes), blobs_multipolygons-s2 (Shapes), blobs_polygons-s1 (Shapes), blobs_polygons-s2 (Shapes)\n", + " ▸ 'scale-s1-s2-s1-s2', with elements:\n", + " blobs_labels-s1 (Labels), blobs_labels-s2 (Labels)\n", + " ▸ 'sequence-s1-s2-s1-s2', with elements:\n", + " blobs_labels-s1 (Labels), blobs_labels-s2 (Labels)\n", + " ▸ 'translation-s1-s2-s1-s2', with elements:\n", + " blobs_labels-s1 (Labels), blobs_labels-s2 (Labels)" + ] + }, + "execution_count": 15, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "from spatialdata import concatenate\n", + "concatenate({\"s1\": sdata, \"s2\": sdata})" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "3c7d0973", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'transform': {'global': Identity }}" + ] + }, + "execution_count": 44, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "sdata.labels[\"blobs_labels\"].attrs" + ] + }, { "cell_type": "markdown", "id": "51784a1c", "metadata": {}, "source": [ - "# Transformations" + "# Coord. Systems and transformations" ] }, { @@ -818,7 +894,7 @@ }, { "cell_type": "code", - "execution_count": 6, + "execution_count": null, "id": "b654ddb3", "metadata": {}, "outputs": [ @@ -834,12 +910,13 @@ } ], "source": [ + "from spatialdata.transformations import get_transformation\n", "get_transformation(sdata.images[\"blobs_image\"])\n" ] }, { "cell_type": "code", - "execution_count": 8, + "execution_count": null, "id": "27b589eb", "metadata": {}, "outputs": [ @@ -855,40 +932,1566 @@ } ], "source": [ + "from spatialdata.transformations import get_transformation\n", "get_transformation(sdata.images[\"blobs_image\"], get_all=True)\n" ] }, - { - "cell_type": "markdown", - "id": "af0b9bb4", - "metadata": {}, - "source": [ - "# Merge objects" - ] - }, { "cell_type": "code", - "execution_count": null, - "id": "6922d9af", + "execution_count": 31, + "id": "2d36e918", "metadata": {}, "outputs": [ { - "ename": "TypeError", - "evalue": "`sdatas` must be a `Iterable`", - "output_type": "error", - "traceback": [ - "\u001b[31m---------------------------------------------------------------------------\u001b[39m", - "\u001b[31mTypeError\u001b[39m Traceback (most recent call last)", - "\u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[13]\u001b[39m\u001b[32m, line 2\u001b[39m\n\u001b[32m 1\u001b[39m \u001b[38;5;28;01mfrom\u001b[39;00m spatialdata \u001b[38;5;28;01mimport\u001b[39;00m concatenate\n\u001b[32m----> \u001b[39m\u001b[32m2\u001b[39m concatenate(sdata)\n\u001b[32m 3\u001b[39m \u001b[38;5;66;03m# concatenate({\"s1\": sdata, \"s2\": sdata})\u001b[39;00m\n", - "\u001b[36mFile \u001b[39m\u001b[32m~/miniforge3/envs/sd_env/lib/python3.13/site-packages/spatialdata/_core/concatenate.py:143\u001b[39m, in \u001b[36mconcatenate\u001b[39m\u001b[34m(sdatas, region_key, instance_key, concatenate_tables, obs_names_make_unique, modify_tables_inplace, merge_coordinate_systems_on_name, attrs_merge, **kwargs)\u001b[39m\n\u001b[32m 92\u001b[39m \u001b[38;5;250m\u001b[39m\u001b[33;03m\"\"\"\u001b[39;00m\n\u001b[32m 93\u001b[39m \u001b[33;03mConcatenate a list of spatial data objects.\u001b[39;00m\n\u001b[32m 94\u001b[39m \n\u001b[32m (...)\u001b[39m\u001b[32m 140\u001b[39m \u001b[33;03m`_fix_ensure_unique_element_names()` directly.\u001b[39;00m\n\u001b[32m 141\u001b[39m \u001b[33;03m\"\"\"\u001b[39;00m\n\u001b[32m 142\u001b[39m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;28misinstance\u001b[39m(sdatas, Iterable):\n\u001b[32m--> \u001b[39m\u001b[32m143\u001b[39m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mTypeError\u001b[39;00m(\u001b[33m\"\u001b[39m\u001b[33m`sdatas` must be a `Iterable`\u001b[39m\u001b[33m\"\u001b[39m)\n\u001b[32m 145\u001b[39m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28misinstance\u001b[39m(sdatas, \u001b[38;5;28mdict\u001b[39m):\n\u001b[32m 146\u001b[39m sdatas = _fix_ensure_unique_element_names(\n\u001b[32m 147\u001b[39m sdatas,\n\u001b[32m 148\u001b[39m rename_tables=\u001b[38;5;129;01mnot\u001b[39;00m concatenate_tables,\n\u001b[32m (...)\u001b[39m\u001b[32m 151\u001b[39m merge_coordinate_systems_on_name=merge_coordinate_systems_on_name,\n\u001b[32m 152\u001b[39m )\n", - "\u001b[31mTypeError\u001b[39m: `sdatas` must be a `Iterable`" + "name": "stdout", + "output_type": "stream", + "text": [ + "SpatialData object, with associated Zarr store: /Users/amanuky/Dropbox/Research/MDC/Projects/SpatialData/Packages/spatialdataR/inst/extdata/blobs.zarr\n", + "├── Images\n", + "│ ├── 'blobs_image': DataArray[cyx] (3, 64, 64)\n", + "│ └── 'blobs_multiscale_image': DataTree[cyx] (3, 64, 64), (3, 32, 32), (3, 16, 16)\n", + "├── Labels\n", + "│ ├── 'blobs_labels': DataArray[yx] (64, 64)\n", + "│ └── 'blobs_multiscale_labels': DataTree[yx] (64, 64), (32, 32), (16, 16)\n", + "├── Points\n", + "│ └── 'blobs_points': DataFrame with shape: (, 4) (2D points)\n", + "├── Shapes\n", + "│ ├── 'blobs_circles': GeoDataFrame shape: (5, 2) (2D shapes)\n", + "│ ├── 'blobs_multipolygons': GeoDataFrame shape: (2, 1) (2D shapes)\n", + "│ └── 'blobs_polygons': GeoDataFrame shape: (5, 1) (2D shapes)\n", + "└── Tables\n", + " └── 'table': AnnData (10, 3)\n", + "with coordinate systems:\n", + " ▸ 'affine', with elements:\n", + " blobs_labels (Labels)\n", + " ▸ 'global', with elements:\n", + " blobs_image (Images), blobs_multiscale_image (Images), blobs_labels (Labels), blobs_multiscale_labels (Labels), blobs_points (Points), blobs_circles (Shapes), blobs_multipolygons (Shapes), blobs_polygons (Shapes)\n", + " ▸ 'scale', with elements:\n", + " blobs_labels (Labels)\n", + " ▸ 'sequence', with elements:\n", + " blobs_labels (Labels)\n", + " ▸ 'translation', with elements:\n", + " blobs_labels (Labels)\n" ] - } - ], - "source": [ - "from spatialdata import concatenate\n", - "concatenate([sdata, sdata])\n", - "# concatenate({\"s1\": sdata, \"s2\": sdata})" + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "/Users/amanuky/miniforge3/envs/sd_env/lib/python3.13/site-packages/zarr/core/group.py:3289: ZarrUserWarning: Object at zmetadata is not recognized as a component of a Zarr hierarchy.\n", + " warnings.warn(\n" + ] + }, + { + "data": { + "text/html": [ + "
    \n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "
    <xarray.DataArray 'image' (y: 192, x: 128)> Size: 49kB\n",
    +       "dask.array<affine_transform, shape=(192, 128), dtype=int16, chunksize=(64, 64), chunktype=numpy.ndarray>\n",
    +       "Coordinates:\n",
    +       "  * y        (y) float64 2kB 0.5 1.5 2.5 3.5 4.5 ... 188.5 189.5 190.5 191.5\n",
    +       "  * x        (x) float64 1kB 0.5 1.5 2.5 3.5 4.5 ... 124.5 125.5 126.5 127.5\n",
    +       "Attributes:\n",
    +       "    transform:  {'scale': Translation (y, x)\\n    [0. 0.]}
    " + ], + "text/plain": [ + " Size: 49kB\n", + "dask.array\n", + "Coordinates:\n", + " * y (y) float64 2kB 0.5 1.5 2.5 3.5 4.5 ... 188.5 189.5 190.5 191.5\n", + " * x (x) float64 1kB 0.5 1.5 2.5 3.5 4.5 ... 124.5 125.5 126.5 127.5\n", + "Attributes:\n", + " transform: {'scale': Translation (y, x)\\n [0. 0.]}" + ] + }, + "execution_count": 31, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "from spatialdata import transform\n", + "from spatialdata.transformations import Affine\n", + "import math\n", + "theta = math.pi / 6\n", + "rotation = Affine(\n", + " [\n", + " [math.cos(theta), -math.sin(theta), 0],\n", + " [math.sin(theta), math.cos(theta), 0],\n", + " [0, 0, 1],\n", + " ],\n", + " input_axes=(\"x\", \"y\"),\n", + " output_axes=(\"x\", \"y\"),\n", + ")\n", + "print(sdata)\n", + "transform(sdata.labels[\"blobs_labels\"], to_coordinate_system=\"scale\")" + ] + }, + { + "cell_type": "markdown", + "id": "c78892c7", + "metadata": {}, + "source": [ + "# Operations" + ] + }, + { + "cell_type": "code", + "execution_count": 24, + "id": "33854ff9", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'y': (np.float64(0.0), np.float64(64.0)),\n", + " 'x': (np.float64(0.0), np.float64(64.0))}" + ] + }, + "execution_count": 24, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "from spatialdata import get_extent\n", + "# get_extent(sdata)\n", + "get_extent(sdata.images[\"blobs_image\"])" + ] + }, + { + "cell_type": "code", + "execution_count": 32, + "id": "7a6cfb55", + "metadata": {}, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "/Users/amanuky/miniforge3/envs/sd_env/lib/python3.13/site-packages/zarr/core/group.py:3289: ZarrUserWarning: Object at zmetadata is not recognized as a component of a Zarr hierarchy.\n", + " warnings.warn(\n" + ] + }, + { + "data": { + "text/plain": [ + "SpatialData object, with associated Zarr store: /Users/amanuky/Dropbox/Research/MDC/Projects/SpatialData/Packages/spatialdataR/inst/extdata/blobs.zarr\n", + "├── Images\n", + "│ ├── 'blobs_image': DataArray[cyx] (3, 64, 64)\n", + "│ └── 'blobs_multiscale_image': DataTree[cyx] (3, 64, 64), (3, 32, 32), (3, 16, 16)\n", + "├── Labels\n", + "│ ├── 'blobs_labels': DataArray[yx] (64, 64)\n", + "│ └── 'blobs_multiscale_labels': DataTree[yx] (64, 64), (32, 32), (16, 16)\n", + "├── Points\n", + "│ └── 'blobs_points': DataFrame with shape: (, 4) (2D points)\n", + "├── Shapes\n", + "│ ├── 'blobs_circles': GeoDataFrame shape: (5, 2) (2D shapes)\n", + "│ ├── 'blobs_multipolygons': GeoDataFrame shape: (2, 1) (2D shapes)\n", + "│ └── 'blobs_polygons': GeoDataFrame shape: (5, 1) (2D shapes)\n", + "└── Tables\n", + " └── 'table': AnnData (10, 3)\n", + "with coordinate systems:\n", + " ▸ 'affine', with elements:\n", + " blobs_labels (Labels)\n", + " ▸ 'global', with elements:\n", + " blobs_image (Images), blobs_multiscale_image (Images), blobs_labels (Labels), blobs_multiscale_labels (Labels), blobs_points (Points), blobs_circles (Shapes), blobs_multipolygons (Shapes), blobs_polygons (Shapes)\n", + " ▸ 'scale', with elements:\n", + " blobs_labels (Labels)\n", + " ▸ 'sequence', with elements:\n", + " blobs_labels (Labels)\n", + " ▸ 'translation', with elements:\n", + " blobs_labels (Labels)" + ] + }, + "execution_count": 32, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "sdata" + ] + }, + { + "cell_type": "code", + "execution_count": 35, + "id": "9bb9307a", + "metadata": {}, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "/Users/amanuky/miniforge3/envs/sd_env/lib/python3.13/functools.py:929: UserWarning: The object has `points` element. Depending on the number of points, querying MAY suffer from performance issues. Please consider filtering the object before calling this function by calling the `subset()` method of `SpatialData`.\n", + " return dispatch(args[0].__class__)(*args, **kw)\n" + ] + }, + { + "data": { + "text/plain": [ + "{'y': (np.float64(0.0), np.float64(35.211751674196506)),\n", + " 'x': (np.float64(0.0), np.float64(35.688261562846535))}" + ] + }, + "execution_count": 35, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "sdata_cropped = sdata.query.bounding_box(\n", + " min_coordinate=[0, 0],\n", + " max_coordinate=[30, 30],\n", + " axes=(\"x\", \"y\"),\n", + " target_coordinate_system=\"global\",\n", + ")\n", + "sdata_cropped\n", + "get_extent(sdata_cropped)" + ] + }, + { + "cell_type": "code", + "execution_count": 37, + "id": "a3875eca", + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
    \n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "
    <xarray.DataArray 'image' (c: 3, y: 30, x: 30)> Size: 22kB\n",
    +       "dask.array<getitem, shape=(3, 30, 30), dtype=float64, chunksize=(3, 30, 30), chunktype=numpy.ndarray>\n",
    +       "Coordinates:\n",
    +       "  * c        (c) int64 24B 0 1 2\n",
    +       "  * y        (y) float64 240B 0.5 1.5 2.5 3.5 4.5 ... 25.5 26.5 27.5 28.5 29.5\n",
    +       "  * x        (x) float64 240B 0.5 1.5 2.5 3.5 4.5 ... 25.5 26.5 27.5 28.5 29.5\n",
    +       "Attributes:\n",
    +       "    transform:  {'global': Identity }
    " + ], + "text/plain": [ + " Size: 22kB\n", + "dask.array\n", + "Coordinates:\n", + " * c (c) int64 24B 0 1 2\n", + " * y (y) float64 240B 0.5 1.5 2.5 3.5 4.5 ... 25.5 26.5 27.5 28.5 29.5\n", + " * x (x) float64 240B 0.5 1.5 2.5 3.5 4.5 ... 25.5 26.5 27.5 28.5 29.5\n", + "Attributes:\n", + " transform: {'global': Identity }" + ] + }, + "execution_count": 37, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "from spatialdata import bounding_box_query\n", + "bounding_box_query(\n", + " sdata.images[\"blobs_image\"],\n", + " min_coordinate=[0, 0],\n", + " max_coordinate=[30, 30],\n", + " axes=(\"x\", \"y\"),\n", + " target_coordinate_system=\"global\",\n", + ")" + ] + }, + { + "cell_type": "markdown", + "id": "6e92e6eb", + "metadata": {}, + "source": [ + "# Others" + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "id": "d141348c", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "SpatialData object\n", + "├── Labels\n", + "│ └── 'blobs_labels': DataArray[yx] (64, 64)\n", + "└── Tables\n", + " └── 'table': AnnData (10, 3)\n", + "with coordinate systems:\n", + " ▸ 'affine', with elements:\n", + " blobs_labels (Labels)\n", + " ▸ 'global', with elements:\n", + " blobs_labels (Labels)\n", + " ▸ 'scale', with elements:\n", + " blobs_labels (Labels)\n", + " ▸ 'sequence', with elements:\n", + " blobs_labels (Labels)\n", + " ▸ 'translation', with elements:\n", + " blobs_labels (Labels)" + ] + }, + "execution_count": 20, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "sdata.aggregate(values=\"blobs_image\", by=\"blobs_labels\", agg_func=\"mean\")" ] } ], diff --git a/vignettes/interop.Rmd b/vignettes/interop.Rmd new file mode 100644 index 00000000..e17237d8 --- /dev/null +++ b/vignettes/interop.Rmd @@ -0,0 +1,86 @@ +--- +title: "R/Python Interoperability`" +date: "`r format(Sys.Date(), '%B %d, %Y')`" +package: "`r BiocStyle::pkg_ver('spatialdataR')`" +output: + BiocStyle::html_document: + toc: true + toc_depth: 2 + toc_float: true +vignette: | + %\VignetteIndexEntry{Introduction to spatialdataR} + %\VignetteEngine{knitr::rmarkdown} + %\VignetteEncoding{UTF-8} +bibliography: "refs.bib" +--- + + + + + + + +```{r setup, include=FALSE} +knitr::opts_chunk$set(cache=FALSE, message=FALSE, warning=FALSE) +``` + +# spatialdataR vs scverse/spatialdata + +## Read/Access + +| | SpatialData (Bioconductor) | scverse/spatialdata | +| ----------------------- | -------------- | -------------- | +| read | `library(SpatialData)`
    `sd <- readSpatialData("blobs.zarr")` | `import spatialdata as sdata`
    `sd = sdata.read_zarr("blobs.zarr")` | +| element accession | `sd$images$blobs_image`
    `image(sd, "blobs_image")`
    `sd$images[[1]]`
    `image(sd, 1)` | `sd.images["blobs_image"]`
    `sd.get("blobs_image")`
    `sd["blobs_image"]` | +| merge SpatialData objects | `combine(sd, sd)` | `from spatialdata import concatenate`
    `concatenate({"s1": sd, "s2": sd})` | + +## Coord. Systems and transformations + +| | SpatialData (Bioconductor) | scverse/spatialdata | +| ---------- | -------------- | -------------- | +| coordinate systems | `CTname(sd)` | `sd.coordinate_systems` | +| coordinate systems (per element) | `CTname(image(sd,"blobs_image"))` | `from spatialdata.transformations import get_transformation`
    `get_transformation(sd.images["blobs_image"], get_all=True)`) | +| transformations | `CTtype(image(sd,"blobs_image"))` | `get_transformation(sd.images["blobs_image"])` | +| transform | `transform(sd$labels$blobs_labels, i="scale")` | `from spatialdata import transform`
    `transform(sd.labels["blobs_labels"], to_coordinate_system="scale")` | + +## Operations + +| | SpatialData (Bioconductor) | scverse/spatialdata | +| ---------- | -------------- | -------------- | +| extent | `extent(sd)`
    `extent(x$images$blobs_image)`| `get_extent(sd)`
    `get_extent(sdata.images["blobs_image"])` | +| crop | `crop(sd, bbox)`
    `crop(sd, poly)`
    `crop(image(sd)), bbox)`
    `crop(image(sd)), poly)`| `sd.query.bounding_box(...)`
    `sd.query.polygon(...)`

    `from spatialdata import bounding_box_query, polygon_query`
    `bounding_box_query(sd.images[["blobs_image"]], ...)`
    `polygon_query(sd.images[["blobs_image"]], ...)` +| query | `extent(sd)`
    `extent(x$images$blobs_image)`| `get_extent(sd)`
    `get_extent(sdata.images["blobs_image"])` | + +## Integration + +| | SpatialData (Bioconductor) | scverse/spatialdata | +| ---------- | -------------- | -------------- | +| transfer data across elements | `mask(sd, i="blobs_image", j="blobs_labels")` | `sd.aggregate(values="blobs_image", by="blobs_labels", agg_func="mean")` | + +## Scratch + +------------------------------------------------------------- + Centered Default Right Left + Header Aligned Aligned Aligned +----------- ------- --------------- ------------------------- + First row 12.0 Example of a row that + spans multiple lines. + + Second row 5.0 Here's another one. Note + the blank line between + rows. +------------------------------------------------------------- + +--------------------------------------------- + spatialdataR spatialdata + (Bioconductor) (scverse) +---------- ------------------------- --------------- +read `library(SpatialData)` sd + +element `library(SpatialData)` sd +accession + +merge `library(SpatialData)` sd +SpatialData +objects +--------------------------------------------- \ No newline at end of file