Visualization of a SpatialData object, blobs example
Import dependencies
[ ]:
import spatialdata
from os.path import join
import pandas as pd
from anndata import AnnData
[ ]:
sdata = spatialdata.datasets.blobs()
sdata
[ ]:
# This blobs dataset only contains a table with a var.index corresponding to the shapes features.
# We need to construct a table with a var.index corresponding to the points features:
ddf = sdata.points["blobs_points"]
unique_gene_ids = ddf["genes"].unique().compute().tolist()
points_var_df = pd.DataFrame(index=unique_gene_ids, data=[], columns=[])
points_table = AnnData(var=points_var_df, obs=None, X=None)
sdata.tables['table_points'] = points_table
[ ]:
spatialdata_filepath = join("data", "blobs.spatialdata.zarr")
[ ]:
sdata.write(spatialdata_filepath, overwrite=True)
[ ]:
from vitessce import (
VitessceConfig,
ViewType as vt,
CoordinationType as ct,
CoordinationLevel as CL,
SpatialDataWrapper,
get_initial_coordination_scope_prefix
)
Configure Vitessce
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.
[ ]:
vc = VitessceConfig(
schema_version="1.0.18",
name='Visium SpatialData Demo (blobs)',
)
# Add data to the configuration:
wrapper = SpatialDataWrapper(
sdata_path=spatialdata_filepath,
# The following paths are relative to the root of the SpatialData zarr store on-disk.
image_path="images/blobs_image",
obs_segmentations_path="labels/blobs_labels",
obs_embedding_paths=["tables/table/obsm/X_umap"],
obs_feature_matrix_path="tables/table/X",
coordinate_system="global",
coordination_values={
"obsType": "blob",
"featureType": "channel",
"fileUid": "my_unique_id"
}
)
points_wrapper = SpatialDataWrapper(
sdata_path=spatialdata_filepath,
# The following paths are relative to the root of the SpatialData zarr store on-disk.
obs_points_path="points/blobs_points",
obs_feature_matrix_path="tables/table_points/X", # TODO
coordinate_system="global",
coordination_values={
"obsType": "point",
"featureType": "gene",
"fileUid": "other_unique_id"
}
)
dataset = vc.add_dataset(name='Blobs').add_object(wrapper).add_object(points_wrapper)
# Add views (visualizations) to the configuration:
spatial = vc.add_view("spatialBeta", dataset=dataset)
layer_controller = vc.add_view("layerControllerBeta", dataset=dataset)
vc.link_views_by_dict([spatial, layer_controller], {
'imageLayer': CL([{
"fileUid": "my_unique_id",
'photometricInterpretation': 'BlackIsZero',
'spatialLayerOpacity': 0.9,
'imageChannel': CL([
{
"spatialTargetC": 0,
"spatialChannelColor": [255, 0, 0],
"spatialChannelOpacity": 1.0
},
{
"spatialTargetC": 1,
"spatialChannelColor": [0, 255, 0],
"spatialChannelOpacity": 1.0
},
{
"spatialTargetC": 2,
"spatialChannelColor": [0, 0, 255],
"spatialChannelOpacity": 1.0
}
])
}]),
}, scope_prefix=get_initial_coordination_scope_prefix("A", "image"))
vc.link_views_by_dict([spatial, layer_controller], {
'segmentationLayer': CL([{
"fileUid": "my_unique_id",
'segmentationChannel': CL([{
'spatialChannelVisible': True,
'obsType': 'blob',
"featureType": "channel",
}]),
}]),
}, scope_prefix=get_initial_coordination_scope_prefix("A", "obsSegmentations"))
vc.link_views_by_dict([spatial, layer_controller], {
'pointLayer': CL([{
"fileUid": "other_unique_id",
"obsType": "point",
"featureType": "gene",
"obsHighlight": None,
"obsColorEncoding": "randomByFeature",
"spatialLayerOpacity": 1.0,
}]),
}, scope_prefix=get_initial_coordination_scope_prefix("A", "obsPoints"))
# Layout the views
vc.layout(spatial | layer_controller);
Render the widget
[ ]:
vw = vc.widget()
vw