From 9acbb46a0c1b3ad61549e134313373fa21fa2d19 Mon Sep 17 00:00:00 2001 From: Sricharan Reddy Varra Date: Mon, 28 Jul 2025 12:41:17 -0700 Subject: [PATCH 1/4] added filter notebook --- notebooks/examples/table-queries.ipynb | 1482 ++++++++++++++++++++++++ 1 file changed, 1482 insertions(+) create mode 100644 notebooks/examples/table-queries.ipynb diff --git a/notebooks/examples/table-queries.ipynb b/notebooks/examples/table-queries.ipynb new file mode 100644 index 0000000..bc04f14 --- /dev/null +++ b/notebooks/examples/table-queries.ipynb @@ -0,0 +1,1482 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "6a1fb5d2", + "metadata": {}, + "source": [ + "# Filtering SpatialData elements with Table Queries\n", + "\n", + "## Introduction\n", + "\n", + "The `spatialdata` framework supports both the representation of `SpatialElement`s (images, labels, points, shapes) and of annotations for these elements. As we explored in the [tables](./tables.ipynb) notebook, some types of `SpatialElement`s can contain annotations within themselves, but the general approach we take is to represent `SpatialElement`s and annotations in separate objects using `AnnData` tables.\n", + "\n", + "In this notebook we introduce **table queries** - a filtering mechanism that allows you to subset both the annotations (tables) and their corresponding spatial elements using expressive query syntax. This functionality is provided by the `filter_table_by_query()` function, which uses the [`annsel`](https://github.com/srivarra/annsel) library for building query expressions. Under the hood, `annsel` uses [`narwhals`](https://narwhals-dev.github.io/narwhals/), an \"*extremely lightweight and extensible compatibility layer between dataframe libraries*\". This notebook assumes that you are have familarized yourself with content in the [tables](./tables.ipynb) notebook." + ] + }, + { + "cell_type": "markdown", + "id": "c50f0610", + "metadata": {}, + "source": [ + "## Setup and Data Loading\n", + "\n", + "Lets start by importing the necessary libraries and loading the example blobs dataset." + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "id": "2b0b3286", + "metadata": {}, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "/Users/srivarra/Dev/Projects/Open Source Contributions/scverse/spatialdata/.venv/lib/python3.12/site-packages/dask/dataframe/__init__.py:31: FutureWarning: The legacy Dask DataFrame implementation is deprecated and will be removed in a future version. Set the configuration option `dataframe.query-planning` to `True` or None to enable the new Dask Dataframe implementation and silence this warning.\n", + " warnings.warn(\n", + "/Users/srivarra/Dev/Projects/Open Source Contributions/scverse/spatialdata/.venv/lib/python3.12/site-packages/xarray_schema/__init__.py:1: UserWarning: pkg_resources is deprecated as an API. See https://setuptools.pypa.io/en/latest/pkg_resources.html. The pkg_resources package is slated for removal as early as 2025-11-30. Refrain from using this package or pin to Setuptools<81.\n", + " from pkg_resources import DistributionNotFound, get_distribution\n", + "/Users/srivarra/Dev/Projects/Open Source Contributions/scverse/spatialdata/src/spatialdata/models/models.py:1144: UserWarning: Converting `region_key: region` to categorical dtype.\n", + " return convert_region_column_to_categorical(adata)\n" + ] + }, + { + "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": 1, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "from pathlib import Path\n", + "\n", + "import annsel as an\n", + "import numpy as np\n", + "\n", + "import spatialdata as sd\n", + "from spatialdata.datasets import blobs\n", + "\n", + "blobs_sdata = blobs()\n", + "blobs_sdata" + ] + }, + { + "cell_type": "markdown", + "id": "facac31d", + "metadata": {}, + "source": [ + "The table in the blobs dataset is rather minimal, so we will artifically add a couple of columns (`cell_type` and `area`) to help illustrate the functionality." + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "65a84100", + "metadata": {}, + "outputs": [], + "source": [ + "rng = np.random.default_rng(123456)\n", + "\n", + "blobs_sdata.tables[\"table\"].obs[\"cell_type\"] = rng.choice(\n", + " [\"A\", \"B\", \"C\", \"C\", \"AA\", \"BB\", \"CC\"], size=blobs_sdata.tables[\"table\"].n_obs\n", + ")\n", + "blobs_sdata.tables[\"table\"].obs[\"cell_type_granular\"] = rng.choice(\n", + " [\"A\", \"B\", \"C\", \"D\", \"E\", \"F\", \"G\", \"H\", \"I\", \"J\"], size=blobs_sdata.tables[\"table\"].n_obs\n", + ")\n", + "blobs_sdata.tables[\"table\"].obs[\"area\"] = rng.choice(\n", + " [10, 20, 30, 40, 50, 60, 70, 80, 90, 100], size=blobs_sdata.tables[\"table\"].n_obs\n", + ")" + ] + }, + { + "cell_type": "markdown", + "id": "56161d84", + "metadata": {}, + "source": [ + "## Supported Operations" + ] + }, + { + "cell_type": "markdown", + "id": "8e2f2314", + "metadata": {}, + "source": [ + "## Basic Filtering Examples\n", + "\n", + "Now let's explore how to filter our blobs `SpatialData` object using table queries.\n", + "\n", + "The most common use case is to filter based on observations (`obs`):" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "7bea46b5", + "metadata": {}, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "/Users/srivarra/Dev/Projects/Open Source Contributions/scverse/spatialdata/src/spatialdata/_core/query/relational_query.py:540: UserWarning: Element type `labels` not supported for 'right' join. Skipping `blobs_labels`\n", + " return self.value(*args)\n", + "/Users/srivarra/Dev/Projects/Open Source Contributions/scverse/spatialdata/src/spatialdata/models/models.py:1142: ImplicitModificationWarning: Trying to modify attribute `._uns` of view, initializing view as actual.\n", + " adata.uns[cls.ATTRS_KEY] = attr\n" + ] + }, + { + "data": { + "text/plain": [ + "SpatialData object\n", + "├── Labels\n", + "│ └── 'blobs_labels': DataArray[yx] (512, 512)\n", + "└── Tables\n", + " └── 'table': AnnData (5, 3)\n", + "with coordinate systems:\n", + " ▸ 'global', with elements:\n", + " blobs_labels (Labels)" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "blobs_sdata_filtered = sd.filter_by_table_query(\n", + " blobs_sdata,\n", + " table_name=\"table\",\n", + " obs_expr=an.col(\"cell_type\") == \"A\"\n", + ")\n", + "blobs_sdata_filtered" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "9990439f", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "Observations reduced from 5 to 5\n" + ] + } + ], + "source": [ + "print(f\"\\nObservations reduced from {blobs_sdata_filtered.tables['table'].n_obs} to {blobs_sdata_filtered.tables['table'].n_obs}\")" + ] + }, + { + "cell_type": "markdown", + "id": "e187bc4e", + "metadata": {}, + "source": [ + "### Breaking Down `an.col(\"cell_type\") == \"A\"`\n", + "\n", + "\n", + "\n", + "**What is `an.col(\"cell_type\")`?**\n", + "\n", + "`an.col(\"cell_type\")` creates a column reference that points to the \"cell_type\" column (doesn't specify if it's in `obs` or `var`). By assigning this to the `obs_expr` argument, you're telling the function to filter the `obs` component of the AnnData table based on this column. Think of it as saying \"I want to work with the cell_type column\".\n", + "\n", + "\n", + "**What does `== \"A\"` do?**\n", + "\n", + "The equality operator `== \"A\"` applies a comparison operator to that column reference, creating a boolean condition that will be `True` for rows where cell_type equals \"A\" and `False` everywhere else.\n", + "\n", + "**Why This Syntax Design?**\n", + "\n", + "These expressions are ran in `narwhals` under the hood to create expressions and run them. If you have a keen eye, you may notice that this syntax is similar to Polars, as the Narwhals API follows as closely as it can to the ergonomics of Polars.\n" + ] + }, + { + "cell_type": "markdown", + "id": "43ac99c0", + "metadata": {}, + "source": [ + "Lets take look at another example, this time we will want to select observations which belong to the `blobs_labels` region." + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "id": "2c9e9bc6", + "metadata": {}, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "/Users/srivarra/Dev/Projects/Open Source Contributions/scverse/spatialdata/src/spatialdata/_core/query/relational_query.py:540: UserWarning: Element type `labels` not supported for 'right' join. Skipping `blobs_labels`\n", + " return self.value(*args)\n", + "/Users/srivarra/Dev/Projects/Open Source Contributions/scverse/spatialdata/src/spatialdata/models/models.py:1142: ImplicitModificationWarning: Trying to modify attribute `._uns` of view, initializing view as actual.\n", + " adata.uns[cls.ATTRS_KEY] = attr\n" + ] + }, + { + "data": { + "text/plain": [ + "SpatialData object\n", + "├── Labels\n", + "│ └── 'blobs_labels': DataArray[yx] (512, 512)\n", + "└── Tables\n", + " └── 'table': AnnData (26, 3)\n", + "with coordinate systems:\n", + " ▸ 'global', with elements:\n", + " blobs_labels (Labels)" + ] + }, + "execution_count": 5, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "blobs_sdata_filtered = sd.filter_by_table_query(\n", + " blobs_sdata,\n", + " table_name=\"table\",\n", + " obs_expr=an.col(\"region\") == \"blobs_labels\",\n", + ")\n", + "blobs_sdata_filtered" + ] + }, + { + "cell_type": "markdown", + "id": "3e3858a1", + "metadata": {}, + "source": [ + "Since all the observations in the table are from the `blobs_labels` element, The table query will return the same `AnnData` object to SpatialDate. But in terms of the other `SpatilaElements` we can see that it's only kept the `blobss_labels` element.\n", + "\n" + ] + }, + { + "cell_type": "markdown", + "id": "a6575841", + "metadata": {}, + "source": [ + "You can also filter based on numeric values, as you'd expect." + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "id": "392bc718", + "metadata": {}, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "/Users/srivarra/Dev/Projects/Open Source Contributions/scverse/spatialdata/src/spatialdata/_core/query/relational_query.py:540: UserWarning: Element type `labels` not supported for 'right' join. Skipping `blobs_labels`\n", + " return self.value(*args)\n", + "/Users/srivarra/Dev/Projects/Open Source Contributions/scverse/spatialdata/src/spatialdata/models/models.py:1142: ImplicitModificationWarning: Trying to modify attribute `._uns` of view, initializing view as actual.\n", + " adata.uns[cls.ATTRS_KEY] = attr\n" + ] + }, + { + "data": { + "text/plain": [ + "SpatialData object\n", + "├── Labels\n", + "│ └── 'blobs_labels': DataArray[yx] (512, 512)\n", + "└── Tables\n", + " └── 'table': AnnData (9, 3)\n", + "with coordinate systems:\n", + " ▸ 'global', with elements:\n", + " blobs_labels (Labels)" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "blobs_sdata_filtered = sd.filter_by_table_query(\n", + " blobs_sdata,\n", + " table_name=\"table\",\n", + " obs_expr=an.col(\"instance_id\") <= 10\n", + ")\n", + "blobs_sdata_filtered" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "id": "1e7d53b3", + "metadata": {}, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "/Users/srivarra/Dev/Projects/Open Source Contributions/scverse/spatialdata/src/spatialdata/_core/query/relational_query.py:540: UserWarning: Element type `labels` not supported for 'right' join. Skipping `blobs_labels`\n", + " return self.value(*args)\n", + "/Users/srivarra/Dev/Projects/Open Source Contributions/scverse/spatialdata/src/spatialdata/models/models.py:1142: ImplicitModificationWarning: Trying to modify attribute `._uns` of view, initializing view as actual.\n", + " adata.uns[cls.ATTRS_KEY] = attr\n" + ] + }, + { + "data": { + "text/plain": [ + "SpatialData object\n", + "├── Labels\n", + "│ └── 'blobs_labels': DataArray[yx] (512, 512)\n", + "└── Tables\n", + " └── 'table': AnnData (5, 3)\n", + "with coordinate systems:\n", + " ▸ 'global', with elements:\n", + " blobs_labels (Labels)" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "blobs_sdata_filtered = sd.filter_by_table_query(\n", + " blobs_sdata,\n", + " table_name=\"table\",\n", + " obs_expr=an.col(\"instance_id\").is_in([1, 3, 5, 8, 13])\n", + ")\n", + "blobs_sdata_filtered" + ] + }, + { + "cell_type": "markdown", + "id": "bb941902", + "metadata": {}, + "source": [ + "## Supported Operators and Expressions\n", + "\n", + "- `an.col(\"column_name\")` - reference a column in `obs` or `var`\n", + " - *Note:* Can be multiple columns, `an.col([\"column_name1\", \"column_name2\"])`\n", + "- Special \"columns\":\n", + " - `an.obs_names` - reference observation names (row indices, aka `AnnData.obs_names`)\n", + " - `an.var_names` - reference variable names (column names, aka `AnnData.var_names`)\n", + "- Comparison operators:\n", + " - `>`, `>=`, `<`, `<=`, `==`, `!=`\n", + "- Membership:\n", + " - `.is_in([list])`\n", + "- String methods:\n", + " - `.str.contains()`, `.str.starts_with()`, `.str.ends_with()`\n", + "- Logical:\n", + " - `&` (and), `|` (or), `~` (not)\n", + "\n", + "As long as an expression does not perform an aggregation under the hood or change length, it can be passed used.\n", + "\n", + "For a full list of supported operators and expressions, see the corersponding [narwhals documentation](https://narwhals-dev.github.io/narwhals/api-reference/expr/)." + ] + }, + { + "cell_type": "markdown", + "id": "290b68ac", + "metadata": {}, + "source": [ + "We can also combine multiple expressions per table component (`obs`, `var`, etc...)\n", + "\n", + "Here we will select observations that have a cell type which starts with `\"A\"`, and observations which whose `cell_type_granular` is in `[\"A\", \"B\", \"C\"]`." + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "id": "534b44ed", + "metadata": {}, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "/Users/srivarra/Dev/Projects/Open Source Contributions/scverse/spatialdata/src/spatialdata/_core/query/relational_query.py:540: UserWarning: Element type `labels` not supported for 'right' join. Skipping `blobs_labels`\n", + " return self.value(*args)\n", + "/Users/srivarra/Dev/Projects/Open Source Contributions/scverse/spatialdata/src/spatialdata/models/models.py:1142: ImplicitModificationWarning: Trying to modify attribute `._uns` of view, initializing view as actual.\n", + " adata.uns[cls.ATTRS_KEY] = attr\n" + ] + }, + { + "data": { + "text/plain": [ + "SpatialData object\n", + "├── Labels\n", + "│ └── 'blobs_labels': DataArray[yx] (512, 512)\n", + "└── Tables\n", + " └── 'table': AnnData (16, 3)\n", + "with coordinate systems:\n", + " ▸ 'global', with elements:\n", + " blobs_labels (Labels)" + ] + }, + "execution_count": 8, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "blobs_sdata_filtered = sd.filter_by_table_query(\n", + " blobs_sdata,\n", + " table_name=\"table\",\n", + " obs_expr=((an.col(\"cell_type\").str.starts_with(\"A\")) | (an.col(\"cell_type_granular\").is_in([\"A\", \"B\", \"C\"]))),\n", + ")\n", + "blobs_sdata_filtered" + ] + }, + { + "cell_type": "markdown", + "id": "e08c41da", + "metadata": {}, + "source": [ + "There are two ways to use \"and\" operators in table queries:\n", + "\n", + "1. Using `&` operator between two expressions\n", + "2. Using a tuple of expressions" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "id": "9902555b", + "metadata": {}, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "/Users/srivarra/Dev/Projects/Open Source Contributions/scverse/spatialdata/src/spatialdata/_core/query/relational_query.py:540: UserWarning: Element type `labels` not supported for 'right' join. Skipping `blobs_labels`\n", + " return self.value(*args)\n", + "/Users/srivarra/Dev/Projects/Open Source Contributions/scverse/spatialdata/src/spatialdata/models/models.py:1142: ImplicitModificationWarning: Trying to modify attribute `._uns` of view, initializing view as actual.\n", + " adata.uns[cls.ATTRS_KEY] = attr\n" + ] + }, + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
instance_idregioncell_typecell_type_granulararea
1818blobs_labelsAAC80
1919blobs_labelsAC60
2626blobs_labelsAAA100
\n", + "
" + ], + "text/plain": [ + " instance_id region cell_type cell_type_granular area\n", + "18 18 blobs_labels AA C 80\n", + "19 19 blobs_labels A C 60\n", + "26 26 blobs_labels AA A 100" + ] + }, + "execution_count": 9, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "blobs_sdata_filtered = sd.filter_by_table_query(\n", + " blobs_sdata,\n", + " table_name=\"table\",\n", + " obs_expr=((an.col(\"cell_type\").str.starts_with(\"A\")), (an.col(\"cell_type_granular\").is_in([\"A\", \"B\", \"C\"]))),\n", + ")\n", + "blobs_sdata_filtered.tables[\"table\"].obs" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "id": "affdf97e", + "metadata": {}, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "/Users/srivarra/Dev/Projects/Open Source Contributions/scverse/spatialdata/src/spatialdata/_core/query/relational_query.py:540: UserWarning: Element type `labels` not supported for 'right' join. Skipping `blobs_labels`\n", + " return self.value(*args)\n", + "/Users/srivarra/Dev/Projects/Open Source Contributions/scverse/spatialdata/src/spatialdata/models/models.py:1142: ImplicitModificationWarning: Trying to modify attribute `._uns` of view, initializing view as actual.\n", + " adata.uns[cls.ATTRS_KEY] = attr\n" + ] + }, + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
instance_idregioncell_typecell_type_granulararea
1818blobs_labelsAAC80
1919blobs_labelsAC60
2626blobs_labelsAAA100
\n", + "
" + ], + "text/plain": [ + " instance_id region cell_type cell_type_granular area\n", + "18 18 blobs_labels AA C 80\n", + "19 19 blobs_labels A C 60\n", + "26 26 blobs_labels AA A 100" + ] + }, + "execution_count": 10, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "blobs_sdata_filtered = sd.filter_by_table_query(\n", + " blobs_sdata,\n", + " table_name=\"table\",\n", + " obs_expr=((an.col(\"cell_type\").str.starts_with(\"A\")) & (an.col(\"cell_type_granular\").is_in([\"A\", \"B\", \"C\"]))),\n", + ")\n", + "blobs_sdata_filtered.tables[\"table\"].obs" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "id": "cc281d89", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "Index(['channel_0_sum', 'channel_1_sum', 'channel_2_sum'], dtype='object')" + ] + }, + "execution_count": 11, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "blobs_sdata_filtered.tables[\"table\"].var_names" + ] + }, + { + "cell_type": "markdown", + "id": "c07836a6", + "metadata": {}, + "source": [ + "In this example, suppose that the `var_name` `channel_0_sum` is of some importance to you when the expression value for some observation is greater than 125. We can also filter based on that matrix's column." + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "id": "8bc5b876", + "metadata": {}, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "/Users/srivarra/Dev/Projects/Open Source Contributions/scverse/spatialdata/src/spatialdata/_core/query/relational_query.py:540: UserWarning: Element type `labels` not supported for 'right' join. Skipping `blobs_labels`\n", + " return self.value(*args)\n", + "/Users/srivarra/Dev/Projects/Open Source Contributions/scverse/spatialdata/src/spatialdata/models/models.py:1142: ImplicitModificationWarning: Trying to modify attribute `._uns` of view, initializing view as actual.\n", + " adata.uns[cls.ATTRS_KEY] = attr\n" + ] + }, + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
instance_idregioncell_typecell_type_granulararea
11blobs_labelsAF20
22blobs_labelsAAF10
33blobs_labelsBBC80
44blobs_labelsCE10
55blobs_labelsCB50
66blobs_labelsAD60
88blobs_labelsAG30
99blobs_labelsCCH50
1010blobs_labelsBI50
1313blobs_labelsCA40
1515blobs_labelsCI90
1616blobs_labelsBC90
1717blobs_labelsCE10
2323blobs_labelsBBH100
2525blobs_labelsCC70
\n", + "
" + ], + "text/plain": [ + " instance_id region cell_type cell_type_granular area\n", + "1 1 blobs_labels A F 20\n", + "2 2 blobs_labels AA F 10\n", + "3 3 blobs_labels BB C 80\n", + "4 4 blobs_labels C E 10\n", + "5 5 blobs_labels C B 50\n", + "6 6 blobs_labels A D 60\n", + "8 8 blobs_labels A G 30\n", + "9 9 blobs_labels CC H 50\n", + "10 10 blobs_labels B I 50\n", + "13 13 blobs_labels C A 40\n", + "15 15 blobs_labels C I 90\n", + "16 16 blobs_labels B C 90\n", + "17 17 blobs_labels C E 10\n", + "23 23 blobs_labels BB H 100\n", + "25 25 blobs_labels C C 70" + ] + }, + "execution_count": 12, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "blobs_sdata_filtered = sd.filter_by_table_query(\n", + " blobs_sdata,\n", + " table_name=\"table\",\n", + " x_expr=an.col(\"channel_0_sum\") > 125,\n", + ")\n", + "blobs_sdata_filtered.tables[\"table\"].obs" + ] + }, + { + "cell_type": "markdown", + "id": "5924fae5", + "metadata": {}, + "source": [ + "And of course you can combine different filters across different `AnnData` Table components." + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "id": "7c8738e1", + "metadata": {}, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "/Users/srivarra/Dev/Projects/Open Source Contributions/scverse/spatialdata/src/spatialdata/_core/query/relational_query.py:540: UserWarning: Element type `labels` not supported for 'right' join. Skipping `blobs_labels`\n", + " return self.value(*args)\n", + "/Users/srivarra/Dev/Projects/Open Source Contributions/scverse/spatialdata/src/spatialdata/models/models.py:1142: ImplicitModificationWarning: Trying to modify attribute `._uns` of view, initializing view as actual.\n", + " adata.uns[cls.ATTRS_KEY] = attr\n" + ] + }, + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
instance_idregioncell_typecell_type_granulararea
1010blobs_labelsBI50
1616blobs_labelsBC90
\n", + "
" + ], + "text/plain": [ + " instance_id region cell_type cell_type_granular area\n", + "10 10 blobs_labels B I 50\n", + "16 16 blobs_labels B C 90" + ] + }, + "execution_count": 13, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "blobs_sdata_filtered = sd.filter_by_table_query(\n", + " blobs_sdata,\n", + " table_name=\"table\",\n", + " obs_expr=an.col(\"cell_type\") == \"B\",\n", + " x_expr=an.col(\"channel_0_sum\") > 125,\n", + ")\n", + "blobs_sdata_filtered.tables[\"table\"].obs" + ] + }, + { + "cell_type": "markdown", + "id": "f69cacac", + "metadata": {}, + "source": [ + "## Using a Real Dataset" + ] + }, + { + "cell_type": "markdown", + "id": "0b76dd36", + "metadata": {}, + "source": [ + "To wrap up the notebook, we'll briefly use the queries \n", + "\n", + "Here we'll take a look querying using the [mibitof dataset](https://spatialdata.scverse.org/en/stable/tutorials/notebooks/datasets/README.html). In addition there is a companion notebook " + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "id": "f475c27a", + "metadata": {}, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "/Users/srivarra/Dev/Projects/Open Source Contributions/scverse/spatialdata/.venv/lib/python3.12/site-packages/zarr/creation.py:610: UserWarning: ignoring keyword argument 'read_only'\n", + " compressor, fill_value = _kwargs_compat(compressor, fill_value, kwargs)\n" + ] + }, + { + "data": { + "text/plain": [ + "SpatialData object, with associated Zarr store: /Users/srivarra/Downloads/mibitof.zarr\n", + "├── Images\n", + "│ ├── 'point8_image': DataArray[cyx] (3, 1024, 1024)\n", + "│ ├── 'point16_image': DataArray[cyx] (3, 1024, 1024)\n", + "│ └── 'point23_image': DataArray[cyx] (3, 1024, 1024)\n", + "├── Labels\n", + "│ ├── 'point8_labels': DataArray[yx] (1024, 1024)\n", + "│ ├── 'point16_labels': DataArray[yx] (1024, 1024)\n", + "│ └── 'point23_labels': DataArray[yx] (1024, 1024)\n", + "└── Tables\n", + " └── 'table': AnnData (3309, 36)\n", + "with coordinate systems:\n", + " ▸ 'point8', with elements:\n", + " point8_image (Images), point8_labels (Labels)\n", + " ▸ 'point16', with elements:\n", + " point16_image (Images), point16_labels (Labels)\n", + " ▸ 'point23', with elements:\n", + " point23_image (Images), point23_labels (Labels)" + ] + }, + "execution_count": 14, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "mibitof_zarr_path = Path(\"~/Downloads/mibitof.zarr\").expanduser()\n", + "\n", + "mibitof_sdata = sd.read_zarr(mibitof_zarr_path)\n", + "mibitof_sdata" + ] + }, + { + "cell_type": "markdown", + "id": "2ec3c23a", + "metadata": {}, + "source": [ + "Lets also get a brief look at the `obs` component of the `AnnData` table. Here are a few columns of interest:\n", + "\n", + "- `point`: This is the name of the Field of View (FOV) that an observation belongs to (in this case it's cells)\n", + "- `cell_size`: The area of a cell\n", + "- `donor`: The donor that the cell is from\n", + "- `Cluster`: The cluster / cell type that the cell belongs to\n", + "- `batch`: The batch that the cell is from (usually with respect to the donor or point / FOV)\n", + "- `library_id`: An identifier pointing to which `SpatialElement` the observation belongs to." + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "id": "9652e669", + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
row_numpointcell_idX1center_rowcoordcenter_colcoordcell_sizecategorydonorClusterbatchlibrary_id
9376-194798265222.037.06.0474.0carcinoma90deEpithelial1point8_labels
9377-194808465224.0314.03.0126.0carcinoma90deEpithelial1point8_labels
9378-194818565225.0407.06.0398.0carcinoma90deEpithelial1point8_labels
9379-194828665226.0439.020.01749.0carcinoma90deEpithelial1point8_labels
9380-194838765227.0479.06.0407.0carcinoma90deImm_other1point8_labels
.......................................
4270-0432223147961793.0519.01018.0125.0carcinoma21d7Tcell_CD40point23_labels
4271-0432323148061794.0929.01018.0190.0carcinoma21d7Imm_other0point23_labels
4272-0432423148161795.0999.01019.0173.0carcinoma21d7Imm_other0point23_labels
4273-0432523148261796.0322.01018.0181.0carcinoma21d7Myeloid_CD11c0point23_labels
4274-0432623148361797.0785.01019.0178.0carcinoma21d7Tcell_CD40point23_labels
\n", + "

3309 rows × 12 columns

\n", + "
" + ], + "text/plain": [ + " row_num point cell_id X1 center_rowcoord center_colcoord \\\n", + "9376-1 9479 8 2 65222.0 37.0 6.0 \n", + "9377-1 9480 8 4 65224.0 314.0 3.0 \n", + "9378-1 9481 8 5 65225.0 407.0 6.0 \n", + "9379-1 9482 8 6 65226.0 439.0 20.0 \n", + "9380-1 9483 8 7 65227.0 479.0 6.0 \n", + "... ... ... ... ... ... ... \n", + "4270-0 4322 23 1479 61793.0 519.0 1018.0 \n", + "4271-0 4323 23 1480 61794.0 929.0 1018.0 \n", + "4272-0 4324 23 1481 61795.0 999.0 1019.0 \n", + "4273-0 4325 23 1482 61796.0 322.0 1018.0 \n", + "4274-0 4326 23 1483 61797.0 785.0 1019.0 \n", + "\n", + " cell_size category donor Cluster batch library_id \n", + "9376-1 474.0 carcinoma 90de Epithelial 1 point8_labels \n", + "9377-1 126.0 carcinoma 90de Epithelial 1 point8_labels \n", + "9378-1 398.0 carcinoma 90de Epithelial 1 point8_labels \n", + "9379-1 1749.0 carcinoma 90de Epithelial 1 point8_labels \n", + "9380-1 407.0 carcinoma 90de Imm_other 1 point8_labels \n", + "... ... ... ... ... ... ... \n", + "4270-0 125.0 carcinoma 21d7 Tcell_CD4 0 point23_labels \n", + "4271-0 190.0 carcinoma 21d7 Imm_other 0 point23_labels \n", + "4272-0 173.0 carcinoma 21d7 Imm_other 0 point23_labels \n", + "4273-0 181.0 carcinoma 21d7 Myeloid_CD11c 0 point23_labels \n", + "4274-0 178.0 carcinoma 21d7 Tcell_CD4 0 point23_labels \n", + "\n", + "[3309 rows x 12 columns]" + ] + }, + "execution_count": 15, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "mibitof_sdata.tables[\"table\"].obs\n" + ] + }, + { + "cell_type": "markdown", + "id": "97e10b27", + "metadata": {}, + "source": [ + "In this example, we're picking donor \"21d7\" and keeping `vars` that either start with `\"CD\"` or are `\"ASCT2\"` or `\"ATP5A\"`." + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "id": "8594cdfd", + "metadata": {}, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "/Users/srivarra/Dev/Projects/Open Source Contributions/scverse/spatialdata/src/spatialdata/_core/query/relational_query.py:540: UserWarning: Element type `labels` not supported for 'right' join. Skipping `point23_labels`\n", + " return self.value(*args)\n", + "/Users/srivarra/Dev/Projects/Open Source Contributions/scverse/spatialdata/src/spatialdata/models/models.py:1142: ImplicitModificationWarning: Trying to modify attribute `._uns` of view, initializing view as actual.\n", + " adata.uns[cls.ATTRS_KEY] = attr\n" + ] + }, + { + "data": { + "text/plain": [ + "SpatialData object\n", + "├── Labels\n", + "│ └── 'point23_labels': DataArray[yx] (1024, 1024)\n", + "└── Tables\n", + " └── 'table': AnnData (1241, 14)\n", + "with coordinate systems:\n", + " ▸ 'point23', with elements:\n", + " point23_labels (Labels)" + ] + }, + "execution_count": 16, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "mibitof_sdata_filtered = sd.filter_by_table_query(\n", + " mibitof_sdata,\n", + " # filter_tables=False,\n", + " table_name=\"table\",\n", + " obs_expr=an.col(\"donor\") == \"21d7\",\n", + " var_names_expr=(\n", + " an.var_names.is_in([\"ASCT2\", \"ATP5A\"])\n", + " | an.var_names.str.starts_with(\"CD\")\n", + " ),\n", + ")\n", + "mibitof_sdata_filtered" + ] + }, + { + "cell_type": "markdown", + "id": "b64edfdf", + "metadata": {}, + "source": [ + "If your spatialdata object has a lot of `SpatialElements` and you only want to apply the filter to a subset of them, you can use the `element_names` parameter to specify which ones you want to use for the filter!\n", + "\n", + "As a final example, let's take it up a few notches and use most of the features of the `filter_by_table_query` function. We will also be using the `method` version of the query instead of the `function`. They behave the same way, except that the `method` version passes in it's own `SpatialData` object.\n", + "\n", + "\n", + "We'll be subsetting of specific `SpatialElements`, and applying filters across `obs`, `var`, and `x` components of the `AnnData` table with a variety of queries." + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "id": "e1e0b5ba", + "metadata": {}, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "/Users/srivarra/Dev/Projects/Open Source Contributions/scverse/spatialdata/src/spatialdata/_core/query/relational_query.py:540: UserWarning: Element type `labels` not supported for 'right' join. Skipping `point23_labels`\n", + " return self.value(*args)\n", + "/Users/srivarra/Dev/Projects/Open Source Contributions/scverse/spatialdata/src/spatialdata/models/models.py:1142: ImplicitModificationWarning: Trying to modify attribute `._uns` of view, initializing view as actual.\n", + " adata.uns[cls.ATTRS_KEY] = attr\n" + ] + }, + { + "data": { + "text/plain": [ + "SpatialData object\n", + "├── Labels\n", + "│ └── 'point23_labels': DataArray[yx] (1024, 1024)\n", + "└── Tables\n", + " └── 'table': AnnData (103, 14)\n", + "with coordinate systems:\n", + " ▸ 'point23', with elements:\n", + " point23_labels (Labels)" + ] + }, + "execution_count": 17, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "mibitof_sdata_filtered = mibitof_sdata_filtered.filter_by_table_query(\n", + " table_name=\"table\",\n", + " element_names=[\"point23_labels\", \"point8_labels\"],\n", + " # Filter observations (obs) based on multiple conditions\n", + " obs_expr=(\n", + " # Cells from donor 21d7 OR 90de\n", + " an.col(\"donor\").is_in([\"21d7\", \"90de\"])\n", + " # AND cells with size greater than 400\n", + " & (an.col(\"cell_size\") > 400)\n", + " # AND cells that are either Epithelial or contain \"Tcell\" in their cluster name\n", + " & (an.col(\"Cluster\") == \"Epithelial\")\n", + " | (an.col(\"Cluster\").str.contains(\"Tcell\"))\n", + " ),\n", + " # Filter variables (var) based on multiple conditions\n", + " var_names_expr=(\n", + " # Select columns that start with CD\n", + " an.var_names.str.starts_with(\"CD\")\n", + " # OR columns that contain \"ATP\"\n", + " | an.var_names.str.contains(\"ATP\")\n", + " # OR specific columns\n", + " | an.var_names.is_in([\"ASCT2\", \"PKM2\", \"SMA\"])\n", + " ),\n", + " # Filter based on expression values\n", + " x_expr=(\n", + " # Keep cells where ASCT2 is greater than 0.1\n", + " (an.col(\"ASCT2\") > 0.1)\n", + " # AND less than 2 for ASCT2\n", + " & (an.col(\"ASCT2\") < 2)\n", + " ),\n", + " how=\"right\",\n", + ")\n", + "mibitof_sdata_filtered" + ] + }, + { + "cell_type": "markdown", + "id": "b81c62ff", + "metadata": {}, + "source": [ + "To wrap up, there are a few things to note:\n", + "\n", + "1. **NOTE:** `SpatialElements` are filtered, but the components within those elements are not.\n", + " 1. For example, when we're filtering by the `obs` table and we get a subset of the Label `SpatialElement`, the individual segmentation masks are not modified, they will have the exact same masks as the original Label `SpatialElement`.\n", + "2. A layer of a given `AnnData` table can be used by specifying the `layer` parameter in the `filter_by_table_query` function.\n", + "3. You can use either the method or the function, they behave exactly the same." + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": ".venv", + "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.12.5" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} From eadfd2c65374e97f5d58b5dd9326b9636e054a2b Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 28 Jul 2025 19:42:01 +0000 Subject: [PATCH 2/4] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- notebooks/examples/table-queries.ipynb | 27 ++++++++------------------ 1 file changed, 8 insertions(+), 19 deletions(-) diff --git a/notebooks/examples/table-queries.ipynb b/notebooks/examples/table-queries.ipynb index bc04f14..be19c57 100644 --- a/notebooks/examples/table-queries.ipynb +++ b/notebooks/examples/table-queries.ipynb @@ -166,11 +166,7 @@ } ], "source": [ - "blobs_sdata_filtered = sd.filter_by_table_query(\n", - " blobs_sdata,\n", - " table_name=\"table\",\n", - " obs_expr=an.col(\"cell_type\") == \"A\"\n", - ")\n", + "blobs_sdata_filtered = sd.filter_by_table_query(blobs_sdata, table_name=\"table\", obs_expr=an.col(\"cell_type\") == \"A\")\n", "blobs_sdata_filtered" ] }, @@ -190,7 +186,9 @@ } ], "source": [ - "print(f\"\\nObservations reduced from {blobs_sdata_filtered.tables['table'].n_obs} to {blobs_sdata_filtered.tables['table'].n_obs}\")" + "print(\n", + " f\"\\nObservations reduced from {blobs_sdata_filtered.tables['table'].n_obs} to {blobs_sdata_filtered.tables['table'].n_obs}\"\n", + ")" ] }, { @@ -319,11 +317,7 @@ } ], "source": [ - "blobs_sdata_filtered = sd.filter_by_table_query(\n", - " blobs_sdata,\n", - " table_name=\"table\",\n", - " obs_expr=an.col(\"instance_id\") <= 10\n", - ")\n", + "blobs_sdata_filtered = sd.filter_by_table_query(blobs_sdata, table_name=\"table\", obs_expr=an.col(\"instance_id\") <= 10)\n", "blobs_sdata_filtered" ] }, @@ -363,9 +357,7 @@ ], "source": [ "blobs_sdata_filtered = sd.filter_by_table_query(\n", - " blobs_sdata,\n", - " table_name=\"table\",\n", - " obs_expr=an.col(\"instance_id\").is_in([1, 3, 5, 8, 13])\n", + " blobs_sdata, table_name=\"table\", obs_expr=an.col(\"instance_id\").is_in([1, 3, 5, 8, 13])\n", ")\n", "blobs_sdata_filtered" ] @@ -1303,7 +1295,7 @@ } ], "source": [ - "mibitof_sdata.tables[\"table\"].obs\n" + "mibitof_sdata.tables[\"table\"].obs" ] }, { @@ -1354,10 +1346,7 @@ " # filter_tables=False,\n", " table_name=\"table\",\n", " obs_expr=an.col(\"donor\") == \"21d7\",\n", - " var_names_expr=(\n", - " an.var_names.is_in([\"ASCT2\", \"ATP5A\"])\n", - " | an.var_names.str.starts_with(\"CD\")\n", - " ),\n", + " var_names_expr=(an.var_names.is_in([\"ASCT2\", \"ATP5A\"]) | an.var_names.str.starts_with(\"CD\")),\n", ")\n", "mibitof_sdata_filtered" ] From cdebd4ba082a98c74b1fe60433fef78de3f50071 Mon Sep 17 00:00:00 2001 From: Sricharan Reddy Varra Date: Mon, 28 Jul 2025 12:46:50 -0700 Subject: [PATCH 3/4] updated a print cell --- notebooks/examples/table-queries.ipynb | 1014 +----------------------- 1 file changed, 33 insertions(+), 981 deletions(-) diff --git a/notebooks/examples/table-queries.ipynb b/notebooks/examples/table-queries.ipynb index be19c57..a4a52d2 100644 --- a/notebooks/examples/table-queries.ipynb +++ b/notebooks/examples/table-queries.ipynb @@ -26,50 +26,10 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": null, "id": "2b0b3286", "metadata": {}, - "outputs": [ - { - "name": "stderr", - "output_type": "stream", - "text": [ - "/Users/srivarra/Dev/Projects/Open Source Contributions/scverse/spatialdata/.venv/lib/python3.12/site-packages/dask/dataframe/__init__.py:31: FutureWarning: The legacy Dask DataFrame implementation is deprecated and will be removed in a future version. Set the configuration option `dataframe.query-planning` to `True` or None to enable the new Dask Dataframe implementation and silence this warning.\n", - " warnings.warn(\n", - "/Users/srivarra/Dev/Projects/Open Source Contributions/scverse/spatialdata/.venv/lib/python3.12/site-packages/xarray_schema/__init__.py:1: UserWarning: pkg_resources is deprecated as an API. See https://setuptools.pypa.io/en/latest/pkg_resources.html. The pkg_resources package is slated for removal as early as 2025-11-30. Refrain from using this package or pin to Setuptools<81.\n", - " from pkg_resources import DistributionNotFound, get_distribution\n", - "/Users/srivarra/Dev/Projects/Open Source Contributions/scverse/spatialdata/src/spatialdata/models/models.py:1144: UserWarning: Converting `region_key: region` to categorical dtype.\n", - " return convert_region_column_to_categorical(adata)\n" - ] - }, - { - "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": 1, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "from pathlib import Path\n", "\n", @@ -93,7 +53,7 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": null, "id": "65a84100", "metadata": {}, "outputs": [], @@ -133,38 +93,10 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": null, "id": "7bea46b5", "metadata": {}, - "outputs": [ - { - "name": "stderr", - "output_type": "stream", - "text": [ - "/Users/srivarra/Dev/Projects/Open Source Contributions/scverse/spatialdata/src/spatialdata/_core/query/relational_query.py:540: UserWarning: Element type `labels` not supported for 'right' join. Skipping `blobs_labels`\n", - " return self.value(*args)\n", - "/Users/srivarra/Dev/Projects/Open Source Contributions/scverse/spatialdata/src/spatialdata/models/models.py:1142: ImplicitModificationWarning: Trying to modify attribute `._uns` of view, initializing view as actual.\n", - " adata.uns[cls.ATTRS_KEY] = attr\n" - ] - }, - { - "data": { - "text/plain": [ - "SpatialData object\n", - "├── Labels\n", - "│ └── 'blobs_labels': DataArray[yx] (512, 512)\n", - "└── Tables\n", - " └── 'table': AnnData (5, 3)\n", - "with coordinate systems:\n", - " ▸ 'global', with elements:\n", - " blobs_labels (Labels)" - ] - }, - "execution_count": 3, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "blobs_sdata_filtered = sd.filter_by_table_query(blobs_sdata, table_name=\"table\", obs_expr=an.col(\"cell_type\") == \"A\")\n", "blobs_sdata_filtered" @@ -172,19 +104,10 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": null, "id": "9990439f", "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "\n", - "Observations reduced from 5 to 5\n" - ] - } - ], + "outputs": [], "source": [ "print(\n", " f\"\\nObservations reduced from {blobs_sdata_filtered.tables['table'].n_obs} to {blobs_sdata_filtered.tables['table'].n_obs}\"\n", @@ -224,38 +147,10 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": null, "id": "2c9e9bc6", "metadata": {}, - "outputs": [ - { - "name": "stderr", - "output_type": "stream", - "text": [ - "/Users/srivarra/Dev/Projects/Open Source Contributions/scverse/spatialdata/src/spatialdata/_core/query/relational_query.py:540: UserWarning: Element type `labels` not supported for 'right' join. Skipping `blobs_labels`\n", - " return self.value(*args)\n", - "/Users/srivarra/Dev/Projects/Open Source Contributions/scverse/spatialdata/src/spatialdata/models/models.py:1142: ImplicitModificationWarning: Trying to modify attribute `._uns` of view, initializing view as actual.\n", - " adata.uns[cls.ATTRS_KEY] = attr\n" - ] - }, - { - "data": { - "text/plain": [ - "SpatialData object\n", - "├── Labels\n", - "│ └── 'blobs_labels': DataArray[yx] (512, 512)\n", - "└── Tables\n", - " └── 'table': AnnData (26, 3)\n", - "with coordinate systems:\n", - " ▸ 'global', with elements:\n", - " blobs_labels (Labels)" - ] - }, - "execution_count": 5, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "blobs_sdata_filtered = sd.filter_by_table_query(\n", " blobs_sdata,\n", @@ -284,38 +179,10 @@ }, { "cell_type": "code", - "execution_count": 6, + "execution_count": null, "id": "392bc718", "metadata": {}, - "outputs": [ - { - "name": "stderr", - "output_type": "stream", - "text": [ - "/Users/srivarra/Dev/Projects/Open Source Contributions/scverse/spatialdata/src/spatialdata/_core/query/relational_query.py:540: UserWarning: Element type `labels` not supported for 'right' join. Skipping `blobs_labels`\n", - " return self.value(*args)\n", - "/Users/srivarra/Dev/Projects/Open Source Contributions/scverse/spatialdata/src/spatialdata/models/models.py:1142: ImplicitModificationWarning: Trying to modify attribute `._uns` of view, initializing view as actual.\n", - " adata.uns[cls.ATTRS_KEY] = attr\n" - ] - }, - { - "data": { - "text/plain": [ - "SpatialData object\n", - "├── Labels\n", - "│ └── 'blobs_labels': DataArray[yx] (512, 512)\n", - "└── Tables\n", - " └── 'table': AnnData (9, 3)\n", - "with coordinate systems:\n", - " ▸ 'global', with elements:\n", - " blobs_labels (Labels)" - ] - }, - "execution_count": 6, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "blobs_sdata_filtered = sd.filter_by_table_query(blobs_sdata, table_name=\"table\", obs_expr=an.col(\"instance_id\") <= 10)\n", "blobs_sdata_filtered" @@ -323,38 +190,10 @@ }, { "cell_type": "code", - "execution_count": 7, + "execution_count": null, "id": "1e7d53b3", "metadata": {}, - "outputs": [ - { - "name": "stderr", - "output_type": "stream", - "text": [ - "/Users/srivarra/Dev/Projects/Open Source Contributions/scverse/spatialdata/src/spatialdata/_core/query/relational_query.py:540: UserWarning: Element type `labels` not supported for 'right' join. Skipping `blobs_labels`\n", - " return self.value(*args)\n", - "/Users/srivarra/Dev/Projects/Open Source Contributions/scverse/spatialdata/src/spatialdata/models/models.py:1142: ImplicitModificationWarning: Trying to modify attribute `._uns` of view, initializing view as actual.\n", - " adata.uns[cls.ATTRS_KEY] = attr\n" - ] - }, - { - "data": { - "text/plain": [ - "SpatialData object\n", - "├── Labels\n", - "│ └── 'blobs_labels': DataArray[yx] (512, 512)\n", - "└── Tables\n", - " └── 'table': AnnData (5, 3)\n", - "with coordinate systems:\n", - " ▸ 'global', with elements:\n", - " blobs_labels (Labels)" - ] - }, - "execution_count": 7, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "blobs_sdata_filtered = sd.filter_by_table_query(\n", " blobs_sdata, table_name=\"table\", obs_expr=an.col(\"instance_id\").is_in([1, 3, 5, 8, 13])\n", @@ -400,38 +239,10 @@ }, { "cell_type": "code", - "execution_count": 8, + "execution_count": null, "id": "534b44ed", "metadata": {}, - "outputs": [ - { - "name": "stderr", - "output_type": "stream", - "text": [ - "/Users/srivarra/Dev/Projects/Open Source Contributions/scverse/spatialdata/src/spatialdata/_core/query/relational_query.py:540: UserWarning: Element type `labels` not supported for 'right' join. Skipping `blobs_labels`\n", - " return self.value(*args)\n", - "/Users/srivarra/Dev/Projects/Open Source Contributions/scverse/spatialdata/src/spatialdata/models/models.py:1142: ImplicitModificationWarning: Trying to modify attribute `._uns` of view, initializing view as actual.\n", - " adata.uns[cls.ATTRS_KEY] = attr\n" - ] - }, - { - "data": { - "text/plain": [ - "SpatialData object\n", - "├── Labels\n", - "│ └── 'blobs_labels': DataArray[yx] (512, 512)\n", - "└── Tables\n", - " └── 'table': AnnData (16, 3)\n", - "with coordinate systems:\n", - " ▸ 'global', with elements:\n", - " blobs_labels (Labels)" - ] - }, - "execution_count": 8, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "blobs_sdata_filtered = sd.filter_by_table_query(\n", " blobs_sdata,\n", @@ -454,89 +265,10 @@ }, { "cell_type": "code", - "execution_count": 9, + "execution_count": null, "id": "9902555b", "metadata": {}, - "outputs": [ - { - "name": "stderr", - "output_type": "stream", - "text": [ - "/Users/srivarra/Dev/Projects/Open Source Contributions/scverse/spatialdata/src/spatialdata/_core/query/relational_query.py:540: UserWarning: Element type `labels` not supported for 'right' join. Skipping `blobs_labels`\n", - " return self.value(*args)\n", - "/Users/srivarra/Dev/Projects/Open Source Contributions/scverse/spatialdata/src/spatialdata/models/models.py:1142: ImplicitModificationWarning: Trying to modify attribute `._uns` of view, initializing view as actual.\n", - " adata.uns[cls.ATTRS_KEY] = attr\n" - ] - }, - { - "data": { - "text/html": [ - "
\n", - "\n", - "\n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - "
instance_idregioncell_typecell_type_granulararea
1818blobs_labelsAAC80
1919blobs_labelsAC60
2626blobs_labelsAAA100
\n", - "
" - ], - "text/plain": [ - " instance_id region cell_type cell_type_granular area\n", - "18 18 blobs_labels AA C 80\n", - "19 19 blobs_labels A C 60\n", - "26 26 blobs_labels AA A 100" - ] - }, - "execution_count": 9, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "blobs_sdata_filtered = sd.filter_by_table_query(\n", " blobs_sdata,\n", @@ -548,89 +280,10 @@ }, { "cell_type": "code", - "execution_count": 10, + "execution_count": null, "id": "affdf97e", "metadata": {}, - "outputs": [ - { - "name": "stderr", - "output_type": "stream", - "text": [ - "/Users/srivarra/Dev/Projects/Open Source Contributions/scverse/spatialdata/src/spatialdata/_core/query/relational_query.py:540: UserWarning: Element type `labels` not supported for 'right' join. Skipping `blobs_labels`\n", - " return self.value(*args)\n", - "/Users/srivarra/Dev/Projects/Open Source Contributions/scverse/spatialdata/src/spatialdata/models/models.py:1142: ImplicitModificationWarning: Trying to modify attribute `._uns` of view, initializing view as actual.\n", - " adata.uns[cls.ATTRS_KEY] = attr\n" - ] - }, - { - "data": { - "text/html": [ - "
\n", - "\n", - "\n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - "
instance_idregioncell_typecell_type_granulararea
1818blobs_labelsAAC80
1919blobs_labelsAC60
2626blobs_labelsAAA100
\n", - "
" - ], - "text/plain": [ - " instance_id region cell_type cell_type_granular area\n", - "18 18 blobs_labels AA C 80\n", - "19 19 blobs_labels A C 60\n", - "26 26 blobs_labels AA A 100" - ] - }, - "execution_count": 10, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "blobs_sdata_filtered = sd.filter_by_table_query(\n", " blobs_sdata,\n", @@ -642,21 +295,10 @@ }, { "cell_type": "code", - "execution_count": 11, + "execution_count": null, "id": "cc281d89", "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "Index(['channel_0_sum', 'channel_1_sum', 'channel_2_sum'], dtype='object')" - ] - }, - "execution_count": 11, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "blobs_sdata_filtered.tables[\"table\"].var_names" ] @@ -671,197 +313,10 @@ }, { "cell_type": "code", - "execution_count": 12, + "execution_count": null, "id": "8bc5b876", "metadata": {}, - "outputs": [ - { - "name": "stderr", - "output_type": "stream", - "text": [ - "/Users/srivarra/Dev/Projects/Open Source Contributions/scverse/spatialdata/src/spatialdata/_core/query/relational_query.py:540: UserWarning: Element type `labels` not supported for 'right' join. Skipping `blobs_labels`\n", - " return self.value(*args)\n", - "/Users/srivarra/Dev/Projects/Open Source Contributions/scverse/spatialdata/src/spatialdata/models/models.py:1142: ImplicitModificationWarning: Trying to modify attribute `._uns` of view, initializing view as actual.\n", - " adata.uns[cls.ATTRS_KEY] = attr\n" - ] - }, - { - "data": { - "text/html": [ - "
\n", - "\n", - "\n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - "
instance_idregioncell_typecell_type_granulararea
11blobs_labelsAF20
22blobs_labelsAAF10
33blobs_labelsBBC80
44blobs_labelsCE10
55blobs_labelsCB50
66blobs_labelsAD60
88blobs_labelsAG30
99blobs_labelsCCH50
1010blobs_labelsBI50
1313blobs_labelsCA40
1515blobs_labelsCI90
1616blobs_labelsBC90
1717blobs_labelsCE10
2323blobs_labelsBBH100
2525blobs_labelsCC70
\n", - "
" - ], - "text/plain": [ - " instance_id region cell_type cell_type_granular area\n", - "1 1 blobs_labels A F 20\n", - "2 2 blobs_labels AA F 10\n", - "3 3 blobs_labels BB C 80\n", - "4 4 blobs_labels C E 10\n", - "5 5 blobs_labels C B 50\n", - "6 6 blobs_labels A D 60\n", - "8 8 blobs_labels A G 30\n", - "9 9 blobs_labels CC H 50\n", - "10 10 blobs_labels B I 50\n", - "13 13 blobs_labels C A 40\n", - "15 15 blobs_labels C I 90\n", - "16 16 blobs_labels B C 90\n", - "17 17 blobs_labels C E 10\n", - "23 23 blobs_labels BB H 100\n", - "25 25 blobs_labels C C 70" - ] - }, - "execution_count": 12, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "blobs_sdata_filtered = sd.filter_by_table_query(\n", " blobs_sdata,\n", @@ -881,80 +336,10 @@ }, { "cell_type": "code", - "execution_count": 13, + "execution_count": null, "id": "7c8738e1", "metadata": {}, - "outputs": [ - { - "name": "stderr", - "output_type": "stream", - "text": [ - "/Users/srivarra/Dev/Projects/Open Source Contributions/scverse/spatialdata/src/spatialdata/_core/query/relational_query.py:540: UserWarning: Element type `labels` not supported for 'right' join. Skipping `blobs_labels`\n", - " return self.value(*args)\n", - "/Users/srivarra/Dev/Projects/Open Source Contributions/scverse/spatialdata/src/spatialdata/models/models.py:1142: ImplicitModificationWarning: Trying to modify attribute `._uns` of view, initializing view as actual.\n", - " adata.uns[cls.ATTRS_KEY] = attr\n" - ] - }, - { - "data": { - "text/html": [ - "
\n", - "\n", - "\n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - "
instance_idregioncell_typecell_type_granulararea
1010blobs_labelsBI50
1616blobs_labelsBC90
\n", - "
" - ], - "text/plain": [ - " instance_id region cell_type cell_type_granular area\n", - "10 10 blobs_labels B I 50\n", - "16 16 blobs_labels B C 90" - ] - }, - "execution_count": 13, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "blobs_sdata_filtered = sd.filter_by_table_query(\n", " blobs_sdata,\n", @@ -985,46 +370,10 @@ }, { "cell_type": "code", - "execution_count": 14, + "execution_count": null, "id": "f475c27a", "metadata": {}, - "outputs": [ - { - "name": "stderr", - "output_type": "stream", - "text": [ - "/Users/srivarra/Dev/Projects/Open Source Contributions/scverse/spatialdata/.venv/lib/python3.12/site-packages/zarr/creation.py:610: UserWarning: ignoring keyword argument 'read_only'\n", - " compressor, fill_value = _kwargs_compat(compressor, fill_value, kwargs)\n" - ] - }, - { - "data": { - "text/plain": [ - "SpatialData object, with associated Zarr store: /Users/srivarra/Downloads/mibitof.zarr\n", - "├── Images\n", - "│ ├── 'point8_image': DataArray[cyx] (3, 1024, 1024)\n", - "│ ├── 'point16_image': DataArray[cyx] (3, 1024, 1024)\n", - "│ └── 'point23_image': DataArray[cyx] (3, 1024, 1024)\n", - "├── Labels\n", - "│ ├── 'point8_labels': DataArray[yx] (1024, 1024)\n", - "│ ├── 'point16_labels': DataArray[yx] (1024, 1024)\n", - "│ └── 'point23_labels': DataArray[yx] (1024, 1024)\n", - "└── Tables\n", - " └── 'table': AnnData (3309, 36)\n", - "with coordinate systems:\n", - " ▸ 'point8', with elements:\n", - " point8_image (Images), point8_labels (Labels)\n", - " ▸ 'point16', with elements:\n", - " point16_image (Images), point16_labels (Labels)\n", - " ▸ 'point23', with elements:\n", - " point23_image (Images), point23_labels (Labels)" - ] - }, - "execution_count": 14, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "mibitof_zarr_path = Path(\"~/Downloads/mibitof.zarr\").expanduser()\n", "\n", @@ -1049,251 +398,10 @@ }, { "cell_type": "code", - "execution_count": 15, + "execution_count": null, "id": "9652e669", "metadata": {}, - "outputs": [ - { - "data": { - "text/html": [ - "
\n", - "\n", - "\n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - "
row_numpointcell_idX1center_rowcoordcenter_colcoordcell_sizecategorydonorClusterbatchlibrary_id
9376-194798265222.037.06.0474.0carcinoma90deEpithelial1point8_labels
9377-194808465224.0314.03.0126.0carcinoma90deEpithelial1point8_labels
9378-194818565225.0407.06.0398.0carcinoma90deEpithelial1point8_labels
9379-194828665226.0439.020.01749.0carcinoma90deEpithelial1point8_labels
9380-194838765227.0479.06.0407.0carcinoma90deImm_other1point8_labels
.......................................
4270-0432223147961793.0519.01018.0125.0carcinoma21d7Tcell_CD40point23_labels
4271-0432323148061794.0929.01018.0190.0carcinoma21d7Imm_other0point23_labels
4272-0432423148161795.0999.01019.0173.0carcinoma21d7Imm_other0point23_labels
4273-0432523148261796.0322.01018.0181.0carcinoma21d7Myeloid_CD11c0point23_labels
4274-0432623148361797.0785.01019.0178.0carcinoma21d7Tcell_CD40point23_labels
\n", - "

3309 rows × 12 columns

\n", - "
" - ], - "text/plain": [ - " row_num point cell_id X1 center_rowcoord center_colcoord \\\n", - "9376-1 9479 8 2 65222.0 37.0 6.0 \n", - "9377-1 9480 8 4 65224.0 314.0 3.0 \n", - "9378-1 9481 8 5 65225.0 407.0 6.0 \n", - "9379-1 9482 8 6 65226.0 439.0 20.0 \n", - "9380-1 9483 8 7 65227.0 479.0 6.0 \n", - "... ... ... ... ... ... ... \n", - "4270-0 4322 23 1479 61793.0 519.0 1018.0 \n", - "4271-0 4323 23 1480 61794.0 929.0 1018.0 \n", - "4272-0 4324 23 1481 61795.0 999.0 1019.0 \n", - "4273-0 4325 23 1482 61796.0 322.0 1018.0 \n", - "4274-0 4326 23 1483 61797.0 785.0 1019.0 \n", - "\n", - " cell_size category donor Cluster batch library_id \n", - "9376-1 474.0 carcinoma 90de Epithelial 1 point8_labels \n", - "9377-1 126.0 carcinoma 90de Epithelial 1 point8_labels \n", - "9378-1 398.0 carcinoma 90de Epithelial 1 point8_labels \n", - "9379-1 1749.0 carcinoma 90de Epithelial 1 point8_labels \n", - "9380-1 407.0 carcinoma 90de Imm_other 1 point8_labels \n", - "... ... ... ... ... ... ... \n", - "4270-0 125.0 carcinoma 21d7 Tcell_CD4 0 point23_labels \n", - "4271-0 190.0 carcinoma 21d7 Imm_other 0 point23_labels \n", - "4272-0 173.0 carcinoma 21d7 Imm_other 0 point23_labels \n", - "4273-0 181.0 carcinoma 21d7 Myeloid_CD11c 0 point23_labels \n", - "4274-0 178.0 carcinoma 21d7 Tcell_CD4 0 point23_labels \n", - "\n", - "[3309 rows x 12 columns]" - ] - }, - "execution_count": 15, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "mibitof_sdata.tables[\"table\"].obs" ] @@ -1308,38 +416,10 @@ }, { "cell_type": "code", - "execution_count": 16, + "execution_count": null, "id": "8594cdfd", "metadata": {}, - "outputs": [ - { - "name": "stderr", - "output_type": "stream", - "text": [ - "/Users/srivarra/Dev/Projects/Open Source Contributions/scverse/spatialdata/src/spatialdata/_core/query/relational_query.py:540: UserWarning: Element type `labels` not supported for 'right' join. Skipping `point23_labels`\n", - " return self.value(*args)\n", - "/Users/srivarra/Dev/Projects/Open Source Contributions/scverse/spatialdata/src/spatialdata/models/models.py:1142: ImplicitModificationWarning: Trying to modify attribute `._uns` of view, initializing view as actual.\n", - " adata.uns[cls.ATTRS_KEY] = attr\n" - ] - }, - { - "data": { - "text/plain": [ - "SpatialData object\n", - "├── Labels\n", - "│ └── 'point23_labels': DataArray[yx] (1024, 1024)\n", - "└── Tables\n", - " └── 'table': AnnData (1241, 14)\n", - "with coordinate systems:\n", - " ▸ 'point23', with elements:\n", - " point23_labels (Labels)" - ] - }, - "execution_count": 16, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "mibitof_sdata_filtered = sd.filter_by_table_query(\n", " mibitof_sdata,\n", @@ -1366,38 +446,10 @@ }, { "cell_type": "code", - "execution_count": 17, + "execution_count": null, "id": "e1e0b5ba", "metadata": {}, - "outputs": [ - { - "name": "stderr", - "output_type": "stream", - "text": [ - "/Users/srivarra/Dev/Projects/Open Source Contributions/scverse/spatialdata/src/spatialdata/_core/query/relational_query.py:540: UserWarning: Element type `labels` not supported for 'right' join. Skipping `point23_labels`\n", - " return self.value(*args)\n", - "/Users/srivarra/Dev/Projects/Open Source Contributions/scverse/spatialdata/src/spatialdata/models/models.py:1142: ImplicitModificationWarning: Trying to modify attribute `._uns` of view, initializing view as actual.\n", - " adata.uns[cls.ATTRS_KEY] = attr\n" - ] - }, - { - "data": { - "text/plain": [ - "SpatialData object\n", - "├── Labels\n", - "│ └── 'point23_labels': DataArray[yx] (1024, 1024)\n", - "└── Tables\n", - " └── 'table': AnnData (103, 14)\n", - "with coordinate systems:\n", - " ▸ 'point23', with elements:\n", - " point23_labels (Labels)" - ] - }, - "execution_count": 17, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "mibitof_sdata_filtered = mibitof_sdata_filtered.filter_by_table_query(\n", " table_name=\"table\",\n", From b3fc3cd9e295a34efb30dac4c8cd6317e7ab0fcd Mon Sep 17 00:00:00 2001 From: Sricharan Reddy Varra Date: Mon, 28 Jul 2025 12:57:29 -0700 Subject: [PATCH 4/4] added queries nb to toctree --- notebooks.md | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/notebooks.md b/notebooks.md index 2877d3b..1294317 100644 --- a/notebooks.md +++ b/notebooks.md @@ -171,6 +171,18 @@ See the bottom of this page for links to analysis tutorials for external methods :maxdepth: 1 notebooks/examples/densenet.ipynb + + .. grid-item:: + + .. container:: custom-card + + .. image:: _static/img/table-queries.jpg + :target: notebooks/examples/table-queries.html + + .. toctree:: + :maxdepth: 1 + + notebooks/examples/table-queries.ipynb ``` ## Technology-specific