embedding_density
Analyze cell density within embeddings to identify spatial patterns in single-cell RNA sequencing data. Supports UMAP or custom embeddings, group-based density calculation, and integration into existing analysis workflows.
Instructions
Calculate the density of cells in an embedding
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| basis | No | The embedding over which the density will be calculated. This embedded representation should be found in `adata.obsm['X_[basis]']`. | umap |
| components | No | The embedding dimensions over which the density should be calculated. This is limited to two components. | |
| groupby | No | Key for categorical observation/cell annotation for which densities are calculated per category. | |
| key_added | No | Name of the `.obs` covariate that will be added with the density estimates. |
Implementation Reference
- src/scmcp/schema/pl.py:718-749 (schema)Pydantic input schema model for the embedding_density plotting tool.class EmbeddingDensityModel(BaseEmbeddingModel): """Input schema for the embedding_density plotting tool.""" basis: str = Field( ..., # Required field description="Basis to use for embedding." ) key: Optional[str] = Field( default=None, description="Key for annotation of observations/cells or variables/genes." ) convolve: Optional[float] = Field( default=None, description="Sigma for Gaussian kernel used for convolution." ) alpha: float = Field( default=0.5, description="Alpha value for the plot.", ge=0, le=1 ) @field_validator('alpha') def validate_alpha(cls, v: float) -> float: """Validate alpha is between 0 and 1""" if v < 0 or v > 1: raise ValueError("alpha must be between 0 and 1") return v
- src/scmcp/schema/tl.py:239-265 (schema)Pydantic input schema model for the embedding_density computation tool.class EmbeddingDensityModel(JSONParsingModel): """Input schema for the embedding density calculation tool.""" basis: str = Field( default='umap', description="The embedding over which the density will be calculated. This embedded representation should be found in `adata.obsm['X_[basis]']`." ) groupby: Optional[str] = Field( default=None, description="Key for categorical observation/cell annotation for which densities are calculated per category." ) key_added: Optional[str] = Field( default=None, description="Name of the `.obs` covariate that will be added with the density estimates." ) components: Optional[Union[str, List[str]]] = Field( default=None, description="The embedding dimensions over which the density should be calculated. This is limited to two components." ) @field_validator('components') def validate_components(cls, v: Optional[Union[str, List[str]]]) -> Optional[Union[str, List[str]]]: """Validate that components are limited to two dimensions""" if v is not None and isinstance(v, list) and len(v) > 2: raise ValueError("components is limited to two dimensions") return v
- src/scmcp/tool/pl.py:80-84 (registration)MCP tool registration for embedding_density plotting, using pl schema.embedding_density = types.Tool( name="embedding_density", description="Plot the density of cells in an embedding.", inputSchema=EmbeddingDensityModel.model_json_schema(), )
- src/scmcp/tool/tl.py:41-45 (registration)MCP tool registration for embedding_density computation, using tl schema.embedding_density_tool = types.Tool( name="embedding_density", description="Calculate the density of cells in an embedding", inputSchema=EmbeddingDensityModel.model_json_schema(), )
- src/scmcp/tool/pl.py:179-216 (handler)Handler function that executes scanpy.pl.embedding_density for the embedding_density plot tool via pl_func mapping.def run_pl_func(ads, func, arguments): """ Execute a Scanpy plotting function with the given arguments. Parameters ---------- adata : AnnData Annotated data matrix. func : str Name of the plotting function to execute. arguments : dict Arguments to pass to the plotting function. Returns ------- The result of the plotting function. """ adata = ads.adata_dic[ads.active] if func not in pl_func: raise ValueError(f"Unsupported function: {func}") run_func = pl_func[func] parameters = inspect.signature(run_func).parameters kwargs = {k: arguments.get(k) for k in parameters if k in arguments} if "title" not in parameters: kwargs.pop("title", False) kwargs.pop("return_fig", True) kwargs["show"] = False kwargs["save"] = ".png" try: fig = run_func(adata, **kwargs) fig_path = set_fig_path(func, **kwargs) add_op_log(adata, run_func, kwargs) return fig_path except Exception as e: raise e return fig_path