Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 18 additions & 9 deletions notebooks/examples/sdata_from_scratch.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -3653,12 +3653,22 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"### Connect the `AnnData` to the `Shapes` object"
"### Connect the `AnnData` to the `Shapes` object\n",
"\n",
"The connection between `AnnData` and `SpatialData` regions is recorded as a dict in `.uns[\"spatialdata_attrs\"]`. \n",
"The following entries are required: \n",
"\n",
" * `region`: This refers to the `SpatialData` element(s) that the `AnnData` object annotates. It is generally a string when the table annotates a single element, and it is a list of strings if multiple elements are annotated. In our case, it is the name of the Shapes element we will use later\n",
" (i.e. the object with centers and radii of the Visium spots).\n",
" * `region_key`: Name of the column in `adata.obs` that will link a given obs to the elements it annotates. This column may only contain\n",
" identifiers that are specified in the `region` key.\n",
" * `instance_key`: Name of the column that matches a given obs in the table to a given circle. It must contain values that are \n",
" in the dataframe index of the points/shapes element. "
]
},
{
"cell_type": "code",
"execution_count": 16,
"execution_count": null,
"metadata": {},
"outputs": [
{
Expand Down Expand Up @@ -3771,12 +3781,11 @@
],
"source": [
"adata_for_sdata.uns[\"spatialdata_attrs\"] = {\n",
" \"region\": \"spots\", # name of the Shapes element we will use later (i.e. the object with centers\n",
" # and radii of the Visium spots)\n",
" \"region_key\": \"region\", # column in adata.obs that will link a given obs to the elements it annotates\n",
" \"instance_key\": \"spot_id\", # column that matches a given obs in the table to a given circle\n",
" \"region\": \"spots\",\n",
" \"region_key\": \"region\",\n",
" \"instance_key\": \"spot_id\",\n",
"}\n",
"# all the rows of adata annotate the same element, called \"spots\" (as we declared above)\n",
"# In this example, all the rows of adata annotate the same element, called \"spots\" (as we declared above)\n",
"adata.obs[\"region\"] = pd.Categorical([\"spots\"] * len(adata))\n",
"adata.obs[\"spot_id\"] = shapes_for_sdata.index\n",
"adata.obs[[\"region\", \"spot_id\"]]"
Expand Down Expand Up @@ -4064,7 +4073,7 @@
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
Expand All @@ -4078,7 +4087,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.9"
"version": "3.13.0"
}
},
"nbformat": 4,
Expand Down