spateo.tools.cluster.utils ========================== .. py:module:: spateo.tools.cluster.utils Attributes ---------- .. autoapisummary:: spateo.tools.cluster.utils.to_dense_matrix Functions --------- .. autoapisummary:: spateo.tools.cluster.utils.compute_pca_components spateo.tools.cluster.utils.pca_spateo spateo.tools.cluster.utils.pearson_residuals spateo.tools.cluster.utils.integrate spateo.tools.cluster.utils.ecp_silhouette spateo.tools.cluster.utils.spatial_adj Module Contents --------------- .. py:data:: to_dense_matrix .. py:function:: compute_pca_components(matrix: Union[numpy.ndarray, scipy.sparse.spmatrix], random_state: Optional[int] = 1, save_curve_img: Optional[str] = None) -> Tuple[Any, int, float] Calculate the inflection point of the PCA curve to obtain the number of principal components that the PCA should retain. :param matrix: A dense or sparse matrix. :param save_curve_img: If save_curve_img != None, save the image of the PCA curve and inflection points. :returns: The number of principal components that PCA should retain. new_components_stored: Percentage of variance explained by the retained principal components. :rtype: new_n_components .. py:function:: pca_spateo(adata: anndata.AnnData, X_data: Optional[numpy.ndarray] = None, n_pca_components: Optional[int] = None, pca_key: Optional[str] = 'X_pca', genes: Union[list, None] = None, layer: Union[str, None] = None, random_state: Optional[int] = 1) Do PCA for dimensional reduction. :param adata: An Anndata object. :param X_data: The user supplied data that will be used for dimension reduction directly. :param n_pca_components: The number of principal components that PCA will retain. If none, will Calculate the inflection point of the PCA curve to obtain the number of principal components that the PCA should retain. :param pca_key: Add the PCA result to :attr:`obsm` using this key. :param genes: The list of genes that will be used to subset the data for dimension reduction and clustering. If `None`, all genes will be used. :param layer: The layer that will be used to retrieve data for dimension reduction and clustering. If `None`, will use ``adata.X``. :returns: The processed AnnData, where adata.obsm[pca_key] stores the PCA result. :rtype: adata_after_pca .. py:function:: pearson_residuals(adata: anndata.AnnData, n_top_genes: Optional[int] = 3000, subset: bool = False, theta: float = 100, clip: Optional[float] = None, check_values: bool = True) Preprocess UMI count data with analytic Pearson residuals. Pearson residuals transform raw UMI counts into a representation where three aims are achieved: 1.Remove the technical variation that comes from differences in total counts between cells; 2.Stabilize the mean-variance relationship across genes, i.e. ensure that biological signal from both low and high expression genes can contribute similarly to downstream processing 3.Genes that are homogeneously expressed (like housekeeping genes) have small variance, while genes that are differentially expressed (like marker genes) have high variance :param adata: An anndata object. :param n_top_genes: Number of highly-variable genes to keep. :param subset: Inplace subset to highly-variable genes if `True` otherwise merely indicate highly variable genes. :param theta: The negative binomial overdispersion parameter theta for Pearson residuals. Higher values correspond to less overdispersion (var = mean + mean^2/theta), and `theta=np.Inf` corresponds to a Poisson model. :param clip: Determines if and how residuals are clipped: * If `None`, residuals are clipped to the interval [-sqrt(n), sqrt(n)], where n is the number of cells in the dataset (default behavior). * If any scalar c, residuals are clipped to the interval [-c, c]. Set `clip=np.Inf` for no clipping. :param check_values: Check if counts in selected layer are integers. A Warning is returned if set to True. :returns: Updates adata with the field ``adata.obsm["pearson_residuals"]``, containing pearson_residuals. .. py:function:: integrate(adatas: List[anndata.AnnData], batch_key: str = 'slices', fill_value: Union[int, float] = 0) -> anndata.AnnData Concatenating all anndata objects. :param adatas: AnnData matrices to concatenate with. :param batch_key: Add the batch annotation to :attr:`obs` using this key. :param fill_value: Scalar value to fill newly missing values in arrays with. :returns: The concatenated AnnData, where adata.obs[batch_key] stores a categorical variable labeling the batch. :rtype: integrated_adata .. py:function:: ecp_silhouette(matrix: Union[numpy.ndarray, scipy.sparse.spmatrix], cluster_labels: numpy.ndarray) -> float Here we evaluate the clustering performance by calculating the Silhouette Coefficient. The silhouette analysis is used to choose an optimal value for clustering resolution. The Silhouette Coefficient is a widely used method for evaluating clustering performance, where a higher Silhouette Coefficient score relates to a model with better defined clusters and indicates a good separation between the celltypes. Advantages of the Silhouette Coefficient: * The score is bounded between -1 for incorrect clustering and +1 for highly dense clustering. Scores around zero indicate overlapping clusters. * The score is higher when clusters are dense and well separated, which relates to a standard concept of a cluster. Original Code Repository: https://scikit-learn.org/stable/modules/clustering.html#silhouette-coefficient :param matrix: A dense or sparse matrix of feature. :param cluster_labels: A array of labels for each cluster. :returns: Mean Silhouette Coefficient for all clusters. .. rubric:: Examples >>> silhouette_score(matrix=adata.obsm["X_pca"], cluster_labels=adata.obs["leiden"].values) .. py:function:: spatial_adj(adata: anndata.AnnData, spatial_key: str = 'spatial', pca_key: str = 'pca', e_neigh: int = 30, s_neigh: int = 6, n_pca_components: int = 30) Calculate the adjacent matrix based on a neighborhood graph of gene expression space and a neighborhood graph of physical space.