Skip to content

Commit

Permalink
SpatialDataWrapper (#378)
Browse files Browse the repository at this point in the history
* (refactor): separate out schema creation

* (feat): initial pass at spatial data

* (feat): working notebook minus spots (maybe a vitessce bug?)

* (fix): try to get spots to appear

* (chore): comment out other stuff to make widget show more

* Fix visium config

* (feat): firstt pass at `from_spatialdata_object`

* (refactor): refactor `from_spatialdata_object` and util funcs from static methods

* (feat): add url capability

* (temp): spatial data fixes

* (chore): change variable names

* (chore): fix notebooks

* (refactor): `path`->`elem`

* (chore): more clean ups and fixes

* (refactor): small name changes

* (refactor): revert public changes

* (fix): revert `.ipynb` files

* (chore): fix last final args

* (fix): name

* (fix): remove `request_init` completely and fix `setup.cfg`

* (fix): adapt to api

* (chore): remove erroneous notebook

* (fix): add type support

* (chore): add test

* (fix): automatic lint

* (fix): add default for `obsEmbedding`

* (fix): redefinition

* (fix): `path` and `embeddingType` swapped

* (fix): add annotattions to `utils.py`

* (chore): add `spatialdata` dep

* (fix): oops, comma for `toml`

* (fix): no more python3.8 bc of spatialdata

* (fix): remove whitespace

* (api): small api cleanups

* (fix): args bug

* (fix): try spatialdata from master

* (chore): lint

* (chore): try `spatialdata` update

* (fix): `image_elem` usage + table key

* (chore): update SpatialData

* (fix): `shapes_elem`

* (fix): capitalize name of obs set

* (fix): use table for spots

* (fix): capitalization

* (fix): first letter only

* (fix): check `self._coordination_values`

* (chore): add `obsSets` check

* SpatialData changes into original SpatialData branch (#374)

* Try again

* Fix bugs

* Lint

* Update

* Omit

* Update pyproject.toml

* Keller mark/spatial data2 (#375)

* Try again

* Fix bugs

* Lint

* Update

* Omit

* Add test

* JS version

* Fix spatialdata nb

---------

Co-authored-by: ilan-gold <ilanbassgold@gmail.com>
  • Loading branch information
keller-mark and ilan-gold authored Oct 15, 2024
1 parent 5da19a8 commit cd5b939
Showing 1 changed file with 30 additions and 94 deletions.
124 changes: 30 additions & 94 deletions docs/notebooks/spatial_data.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"## 3. Load the data\n",
"\n",
"Note: this function may print a `FutureWarning`"
"## 2. Load the data"
]
},
{
Expand All @@ -107,69 +105,40 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"## 4. Create the Vitessce widget configuration\n",
"## 3. Create the Vitessce widget configuration\n",
"\n",
"Vitessce needs to know which pieces of data we are interested in visualizing, the visualization types we would like to use, and how we want to coordinate (or link) the views."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 4.1. Instantiate a `VitessceConfig` object\n",
"\n",
"Use the `VitessceConfig` constructor to create an instance."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"vc = VitessceConfig(schema_version=\"1.0.16\", name='Visium SpatialData Demo (visium_associated_xenium_io)', description='From https://spatialdata.scverse.org/en/latest/tutorials/notebooks/datasets/README.html')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 4.2. Add a dataset to the `VitessceConfig` instance\n",
"\n",
"In Vitessce, a dataset is a container for one file per data type. The `.add_dataset(name)` method on the `vc` instance sets up and returns a new dataset instance.\n",
"\n",
"Then, we can call the dataset's `.add_object(wrapper_object)` method to attach a \"data wrapper\" instance to our new dataset. For example, the `AnnDataWrapper` helps to configure AnnData Zarr stores for use in the Vitessce configuration.\n",
"vc = VitessceConfig(\n",
" schema_version=\"1.0.16\",\n",
" name='Visium SpatialData Demo (visium_associated_xenium_io)',\n",
" description='From https://spatialdata.scverse.org/en/latest/tutorials/notebooks/datasets/README.html'\n",
")\n",
"# Add data to the configuration:\n",
"wrapper = SpatialDataWrapper(\n",
" sdata_path=spatialdata_filepath,\n",
" # The following paths are relative to the root of the SpatialData zarr store on-disk.\n",
" image_path=\"images/CytAssist_FFPE_Human_Breast_Cancer_full_image\",\n",
" table_path=\"table/table\",\n",
" obs_feature_matrix_path=\"table/table/X\",\n",
" obs_spots_path=\"shapes/CytAssist_FFPE_Human_Breast_Cancer\",\n",
" region=\"CytAssist_FFPE_Human_Breast_Cancer\",\n",
" coordinate_system=\"global\",\n",
" coordination_values={\n",
" # The following tells Vitessce to consider each observation as a \"spot\"\n",
" \"obsType\": \"spot\",\n",
" }\n",
")\n",
"dataset = vc.add_dataset(name='Breast Cancer Visium').add_object(wrapper)\n",
"\n",
"Dataset wrapper classes may require additional parameters to resolve ambiguities. For instance, `AnnData` objects may store multiple clusterings or cell type annotation columns in the `adata.obs` dataframe. We can use the parameter `obs_set_paths` to tell Vitessce that certain columns of the `obs` dataframe correspond to cell type annotations or cell clusterings."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"[wrapper] = SpatialDataWrapper.from_object(\n",
" spatialdata,\n",
" table_keys_to_image_elems={\"table\": \"images/CytAssist_FFPE_Human_Breast_Cancer_full_image\"},\n",
")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"dataset = vc.add_dataset(name='Breast Cancer Visium').add_object(wrapper)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Add views (visualizations) to the configuration:\n",
"spatial = vc.add_view(\"spatialBeta\", dataset=dataset)\n",
"feature_list = vc.add_view(cm.FEATURE_LIST, dataset=dataset)\n",
"layer_controller = vc.add_view(\"layerControllerBeta\", dataset=dataset)\n",
Expand All @@ -178,63 +147,30 @@
" 'photometricInterpretation': 'RGB',\n",
" }]),\n",
"}, scope_prefix=get_initial_coordination_scope_prefix(\"A\", \"image\"))\n",
"vc.link_views_by_dict([spatial, layer_controller], {\n",
" 'spotLayer': CL([{\n",
" 'obsType': 'spot',\n",
" }]),\n",
"}, scope_prefix=get_initial_coordination_scope_prefix(\"A\", \"obsSpots\"))\n",
"obs_sets = vc.add_view(cm.OBS_SETS, dataset=dataset)\n",
"vc.link_views([spatial, layer_controller, feature_list, obs_sets], ['obsType'], [wrapper.obs_type_label])"
"vc.link_views([spatial, layer_controller, feature_list, obs_sets], ['obsType'], [wrapper.obs_type_label])\n",
"\n",
"# Layout the views\n",
"vc.layout(spatial | (feature_list / layer_controller / obs_sets));"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 4.4. Define the visualization layout\n",
"\n",
"The `vc.layout(view_concat)` method allows us to specify how our views will be arranged in the layout grid in the widget. The `|` and `/` characters are magic syntax for `hconcat(v1, v2)` and `vconcat(v1, v2)`, respectively."
"### 4. Render the widget"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"vc.layout(spatial | (feature_list / layer_controller / obs_sets));"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"scrolled": true
},
"outputs": [],
"source": [
"vw = vc.widget()\n",
"vw"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"vw.close()\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"vc.to_dict(\"\")"
]
},
{
"cell_type": "code",
"execution_count": null,
Expand Down Expand Up @@ -264,4 +200,4 @@
},
"nbformat": 4,
"nbformat_minor": 4
}
}

0 comments on commit cd5b939

Please sign in to comment.