diff --git a/inst/scripts/spatialdata-python/main.ipynb b/inst/scripts/spatialdata-python/main.ipynb new file mode 100644 index 00000000..dcb1aac5 --- /dev/null +++ b/inst/scripts/spatialdata-python/main.ipynb @@ -0,0 +1,2519 @@ +{ + "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": null, + "id": "56975714", + "metadata": {}, + "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" + ] + }, + { + "cell_type": "markdown", + "id": "c1f2f65c", + "metadata": {}, + "source": [ + "# Accession" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "id": "517a3be4", + "metadata": {}, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "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", + "/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": 18, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "sdata = sd.read_zarr(\"../../extdata/blobs.zarr\")\n", + "sdata" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "770341a0", + "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: 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: 98kB\n", + "dask.array\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 }" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "sdata.images[\"blobs_image\"]\n", + "sdata.get(\"blobs_image\")\n", + "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": [ + "# Coord. Systems and transformations" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "id": "5ef10d9e", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['sequence', 'global', 'affine', 'scale', 'translation']" + ] + }, + "execution_count": 5, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "sdata.coordinate_systems" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "b654ddb3", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "Identity " + ] + }, + "execution_count": 6, + "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": 8, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "from spatialdata.transformations import get_transformation\n", + "get_transformation(sdata.images[\"blobs_image\"], get_all=True)\n" + ] + }, + { + "cell_type": "code", + "execution_count": 31, + "id": "2d36e918", + "metadata": {}, + "outputs": [ + { + "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" + ] + }, + { + "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\")" + ] + } + ], + "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 +} 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