Skip to content

table_name not automatically inferred #268

@LucaMarconato

Description

@LucaMarconato

In this example created for a user I am annotating the polygons from. blobs with 2 tables. The first table has the categorical column cell_type, while the second has the column cluster_id.

When plotting this into the shapes, in the first case table_name is not required and the plot can be performed anyway, in the second case instead I need to specify table_name otherwise the coloring is not applied.

The expected behavior would be for both plots to be possible without having to specify table_name, since in this case there is no ambiguity.

@melonora I wonder if this is automatically fixed by the new "per-element" parameter refactoring.

##
import spatialdata_plot
import matplotlib.pyplot as plt

from spatialdata.datasets import blobs_annotating_element
from spatialdata.models import TableModel
from anndata import AnnData
import pandas as pd

sdata = blobs_annotating_element("blobs_polygons").subset(["blobs_polygons"])
print(sdata)

##
# plotting the data without annotations
sdata.pl.render_shapes("blobs_polygons").pl.show()
plt.show()

##
# defining the annotations
celltype_dataframe = pd.DataFrame(
    {
        "cell_type": ["A", "B", "C", "A", "B"],
        "cell_id": sdata["blobs_polygons"].index,
    }
)

##
# option 1: merge the dataframe with the existing table
sdata["table"].obs = pd.merge(
    sdata["table"].obs, celltype_dataframe, left_on="instance_id", right_on="cell_id", how="left"
)

##
sdata.pl.render_shapes("blobs_polygons", color='cell_type').pl.show()
plt.show()

##
# option 2: add a new table
celltype_dataframe["region"] = ["blobs_polygons"] * 5
celltype_dataframe['cluster_id'] = [1, 1, 3, 2, 2]

annotations = AnnData(obs=celltype_dataframe)
annotations = TableModel.parse(annotations, region="blobs_polygons", region_key="region", instance_key="cell_id")
sdata["annotations"] = annotations
print(sdata)

##
sdata.pl.render_shapes("blobs_polygons", color="cluster_id", table_name='annotations').pl.show()
plt.show()

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions