Skip to main content

A library for estimating Biological Age using classical and ML methods

Project description

tse_ba_comp

PyPI version Python 3.7+

tse_ba_comp stands for Tse Biological Age Comparator (named after Prof. Gary Tse's Research Group) is a robust, easy-to-use Python library for estimating Biological Age (BA) from clinical biomarkers.

Developed by Mehrdad S. Beni & Gary Tse, this package evaluates and ensembles classical mathematical approaches against modern Machine Learning models to provide highly accurate, cross-validated age estimations.

Key Features

  • Classical Models: Fast, vectorized implementations of the Klemera-Doubal Method (KDM) and PCA-Dubina.
  • Machine Learning: Automated pipelines for Elastic Net, Random Forest, and XGBoost.
  • Smart Ensembling: Automatically combine predictions using Mean or Median strategies to smooth out variance.
  • Automated Preprocessing: Handles train/test splitting, scaling, and missing data imputation safely to prevent data leakage.
  • Built-in Visualization: Generates standardized, publication-ready scatter plots of Biological Age vs. Chronological Age.

Installation

Install directly from PyPI:

pip install tse-ba-comp

Examples & Usage

Example 1: Quick Start (Default & Shortest)

The easiest way to use the library is to pass a CSV dataset file path directly to the run function. The library uses highly optimized defaults out-of-the-box.

import tse_ba_comp

my_biomarkers = ["albumin", "alp", "bun", "creat", "hba1c", "glucose", "sbp"]

results = tse_ba_comp.run(
    data="nhanes4_model_input.csv",
    biomarkers=my_biomarkers,
    out="my_results_folder"  # Automatically saves plots and CSVs here
)

print(results["metrics"])

Example 2: ML Auto Tuning & Control

Pass a list of models to ml and set tune=True to let the library automatically grid search the best hyperparameters for those specific models.

import tse_ba_comp

my_biomarkers = ["albumin", "alp", "bun", "creat", "hba1c", "glucose", "sbp"]

results = tse_ba_comp.run(
    data="nhanes4_model_input.csv",
    biomarkers=my_biomarkers,
    impute="knn",              # switch imputation to knn
    seed=101,                  # fix random seed for reproducibility
    ml=["rf", "xgb"],          # only run Random Forest & XGBoost
    tune=True,                 # trigger automated Grid Search
    out="advanced_results"
)

print(results["metrics"])

Example 3: Comprehensive Pipeline (All parameters that our lib offer)

For full programmatic control, you can utilize every argument the library offers and pass an exact dictionary of fixed hyperparameters to bypass the automated grid search.

import tse_ba_comp

my_biomarkers = ["albumin", "alp", "bun", "creat", "hba1c", "glucose", "sbp"]

# Define exact parameters to pass to scikit-learn / XGBoost
custom_ml_settings = {
    "en": {"l1_ratio": 0.7},
    "rf": {"n_estimators": 200, "max_depth": 10},
    "xgb": {"n_estimators": 150, "learning_rate": 0.05, "max_depth": 4}
}

results = tse_ba_comp.run(
    data="nhanes4_model_input.csv",
    biomarkers=my_biomarkers,
    age="age",                      # target column name
    impute="mean",                  # imputation strategy
    test_size=None,                 # set to None to evaluate on the ENTIRE dataset without splitting
    seed=42,                        # random seed
    kdm=True,                       # enable Klemera-Doubal Method
    kdm_s2_floor=0.05,              # tweak KDM variance floor
    pca=True,                       # enable PCA-Dubina
    ml=custom_ml_settings,          # apply custom ML settings directly
    tune=False,                     # skip grid search
    cv_folds=5,                     # cross-validation folds
    ensemble="mean",                # use arithmetic mean for ensemble
    out="all_keywords_results"
)

print(results["metrics"])

Complete API Reference

Below is the complete list of arguments accepted by the run function.

Core Data Settings

  • data (str or pandas.DataFrame): Path to your CSV file, or a loaded Pandas DataFrame.
  • biomarkers (list of str): List of column names representing the biomarkers to be used.
  • age (str): The column name containing chronological age. Default: "age".

Processing & Splitting

  • impute (str): How to handle missing data. Options: "median", "mean", "zero", "knn". Default: "median".
  • test_size (float or None): The fraction of the dataset to hold out for testing and evaluation. Set to None to bypass splitting and train/evaluate on the entire dataset (traditional for classical BA methods). Default: 0.2.
  • seed (int): Random seed to ensure reproducible train/test splits. Default: 42.

Classical Model Toggles

  • kdm (bool): Toggle the Klemera-Doubal Method. Default: True.
  • kdm_s2_floor (float): Minimum variance floor for KDM calculations to prevent division by near-zero. Default: 0.1.
  • pca (bool): Toggle the PCA-Dubina method. Default: True.

Machine Learning Settings

  • ml (bool, list, or dict): Controls the ML models.
    • True: Runs Elastic Net ("en"), Random Forest ("rf"), and XGBoost ("xgb") with defaults.
    • False: Skips ML entirely.
    • list: E.g., ["rf", "en"] runs only specific models.
    • dict: Passes explicit kwargs to the underlying model constructors.
  • tune (bool): If True, runs a GridSearchCV over a predefined parameter grid for the active ML models. Default: False.
  • cv_folds (int): Number of cross-validation folds used during training/tuning. Default: 5.

Ensemble & Outputs

  • ensemble (str or None): How to combine the model predictions. Options: "median", "mean", or None (to skip). Default: "median".
  • out (str or None): Directory path to save the generated scatter plots and prediction CSVs. If None, no files are saved to the disk.

Outputs

The run function returns a dictionary with two keys:

  1. results["metrics"]: A Pandas DataFrame containing the Pearson r, R², RMSE, and MAE for all executed models. Evaluated strictly on the test set (or the full dataset if test_size=None).
  2. results["predictions"]: A Pandas DataFrame mapping the Chronological Age to the estimated Biological Ages for every patient in the test set (or full dataset).

Developers

Developed by Dr. Mehrdad S. Beni and Prof. Gary Tse at Hong Kong Metropolitan University, 2026.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

tse_ba_comp-0.1.6.tar.gz (23.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

tse_ba_comp-0.1.6-py3-none-any.whl (22.7 kB view details)

Uploaded Python 3

File details

Details for the file tse_ba_comp-0.1.6.tar.gz.

File metadata

  • Download URL: tse_ba_comp-0.1.6.tar.gz
  • Upload date:
  • Size: 23.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.3

File hashes

Hashes for tse_ba_comp-0.1.6.tar.gz
Algorithm Hash digest
SHA256 e1c5a0452144ed83c4d6058aa54386ef713aeaa823ee058e8d327da83a270bf6
MD5 e4c6789b900d79006de5309b013c4370
BLAKE2b-256 999426c5253ada632418506a781b7e2065021f069ccf1177cbca34343ecc9cce

See more details on using hashes here.

File details

Details for the file tse_ba_comp-0.1.6-py3-none-any.whl.

File metadata

  • Download URL: tse_ba_comp-0.1.6-py3-none-any.whl
  • Upload date:
  • Size: 22.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.3

File hashes

Hashes for tse_ba_comp-0.1.6-py3-none-any.whl
Algorithm Hash digest
SHA256 7c30529b638b798d58a886aa2326b1c5e7c0615d568cb0c3f43f9bf710c9b7e7
MD5 3b98026da6b44eac588edf64a852fe00
BLAKE2b-256 81e66e950ca82dd090b9fcf5c30db8883270f5decf51c538ab9f550b411605b5

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page