Skip to main content

BaCHClue: Bic and Calinski-Harabasz Score Guided Clustering

BaCHClue aims to find the optimal resolution parameter for clustering single-cell RNA-Seq data analyzed with scanpy, by calculating the BIC score or the Calinski-Harabasz score of each clustering, given a range of possible resolutions.

Here, the minimum of the BIC or Calinski-Harabasz score indicates the optimal resolution parameter.

BIC score

The BIC (Bayesian Information Criterion) score is a statistical measure used to assess the goodness of fit of a statistical model. It is often used in the context of model selection among a set of candidate models.

The BIC score derives from Bayesian probability theory and is based on the likelihood function of the data given a particular model. It balances the trade-off between model complexity (the number of parameters in the model) and the goodness of fit to the data. The goal is to find a model that fits the data well while penalizing overly complex models that may overfit the data.

In the function, the BIC score was manually implemented by using the formula:

BIC = -2*log(L) + k*log(N)

Where:

  • L is the likelihood of the data given the model;
  • k is the number of parameters in the model;
  • N is the number of data points.

Further details on BIC score can be found here.

Calinski-Harabasz score

The Calinski-Harabasz (CH) score is a metric used for evaluating the quality of clusters in unsupervised machine learning, particularly in clustering analysis. It aims to determine the optimal number of clusters. The CH score is based on the ratio of the between-cluster variance to the within-cluster variance. It measures the compactness of clusters (small within-cluster variance) relative to their separation (large between-cluster variance). A higher CH score indicates better-defined and well-separated clusters.

The formula for calculating the Calinski-Harabasz score is as follows:

CH = (B / (k - 1)) / (W / (n - k))

Where:

  • CH is the Calinski-Harabasz score;
  • B is the between-cluster variance, which measures the variance between cluster centroids;
  • k is the number of clusters;
  • W is the within-cluster variance, which measures the variance within each cluster;
  • n is the total number of data points.

In the function, the Calinski-Harabasz score was calculated by using the function calinski_harabasz_score() from sklearn library, which takes as an input PCA or UMAP coordinates of each cell and a vector with the claster label of each cell. For the purpose of plotting, -1*CH is defined to be the output, so then the lowest score indicates better-defined and well-separated clusters, as it happens with BIC score.

Nedeed libraries

numpy, scanpy, sklearn

Usage

clustering_score(original_adata, score_value = 'bic', clustering_algorithm='leiden', dim_reduction = 'pca', min_res=0.1, max_res=2.0, step=0.1, plot=True)

To use the function, simply copy it from score_function.py and use in your own script.

Parameters:

  • original_adata: the AnnData file containing the normalized and scaled data and dimensionality reductions (PCA and UMAP or tsne)
  • score_value = the chosen score to evaluate the different clusterings. Possible choices: bic,calinski. Default: 'calinski'
  • clustering_algorithm: the algorithm to use to test the different clustering (string). Possible choices: leiden,louvain. Default: 'leiden'.
  • dim_reduction: coordinates to use for calculating the BIC score (string). Possible choices: pca, umap. Default: 'pca'.
  • min_res: minimum resolution to test (float). Default: 0.1.
  • max_res: maximum resolution to test (float). Default: 2.0.
  • step: step size for resolutions to be tested between min_res and max_res (float). Default: 0.1
  • plot: Whether to plot or not the BIC score as function of the resolution (Boolean). Default: True.

Outputs:

The function returns two outputs:

  • A pandas DataFrame with 3 columns, i.e. BIC score, resolution and corresponding number of clusters;
  • the optimum of the resolution, that can be used for further analysis.

Note that the function does not add any clustering metadata into the original AnnData file.

Example:

The notebook score_function.ipynb contains and example usage of the function. Data consist of 3k PBMCs from a Healthy Donor and are freely available from 10x Genomics, and can be download as follows from a terminal:

wget http://cf.10xgenomics.com/samples/cell-exp/1.1.0/pbmc3k/pbmc3k_filtered_gene_bc_matrices.tar.gz -O data/pbmc3k_filtered_gene_bc_matrices.tar.gz

Alternatively, they can be downloaded here.

Data were preprocessed and analyzed by following this tutorial.

The obtained optimum resolution was 1.0, corresponding to 7 different clusters, as expected from here.

Metadata

Release files for BaCHClue 1.0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for BaCHClue 1.0.1
File Size Uploaded Provenance
bachclue-1.0.1.tar.gz 31.9 MB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for BaCHClue 1.0.1
File Interpreter ABI Platform
bachclue-1.0.1-py3-none-any.whl Python 3 none any Details

Total release size: 31.9 MB

Release files / bachclue-1.0.1.tar.gz

Download URL bachclue-1.0.1.tar.gz
Size 31.9 MB
Tags Source
SHA-256 checksum
How to use checksums
3d78a7c0c3089bf775c162128718db9579293261975109b1e3ce853e8dca0add
BLAKE2b-256 checksum
How to use checksums
c51c8218cce475b2946d38a6ed694278b1cad719b8587ce088eb45ef47365320
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.12.10

Release files / bachclue-1.0.1-py3-none-any.whl

Download URL bachclue-1.0.1-py3-none-any.whl
Size 18.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
5ae250f195d9b2f2ae1b799e551692a558a63f11aa303426acb08bff359fe939
BLAKE2b-256 checksum
How to use checksums
b22b8466ebea6e9192e1c78a2073391c172186a373f63bd763d59a8407ad1f69
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.12.10

Security for BaCHClue 1.0.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

Release history Release notifications | RSS feed

This release

1.0.1 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page