{ "cells": [ { "cell_type": "markdown", "id": "21791569", "metadata": {}, "source": [ "# VidalBralo" ] }, { "cell_type": "markdown", "id": "93a90ac5", "metadata": {}, "source": [ "## Index\n", "1. [Instantiate model class](#Instantiate-model-class)\n", "2. [Define clock metadata](#Define-clock-metadata)\n", "3. [Download clock dependencies](#Download-clock-dependencies)\n", "5. [Load features](#Load-features)\n", "6. [Load weights into base model](#Load-weights-into-base-model)\n", "7. [Load reference values](#Load-reference-values)\n", "8. [Load preprocess and postprocess objects](#Load-preprocess-and-postprocess-objects)\n", "10. [Check all clock parameters](#Check-all-clock-parameters)\n", "10. [Basic test](#Basic-test)\n", "11. [Save torch model](#Save-torch-model)\n", "12. [Clear directory](#Clear-directory)" ] }, { "cell_type": "markdown", "id": "dcd636f4", "metadata": {}, "source": [ "Let's first import some packages:" ] }, { "cell_type": "code", "execution_count": 1, "id": "2350ba0e", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:10:30.379901Z", "iopub.status.busy": "2026-07-04T19:10:30.379559Z", "iopub.status.idle": "2026-07-04T19:10:37.575026Z", "shell.execute_reply": "2026-07-04T19:10:37.574076Z" } }, "outputs": [], "source": [ "import os\n", "import inspect\n", "import shutil\n", "import json\n", "import torch\n", "import pandas as pd\n", "import pyaging as pya" ] }, { "cell_type": "markdown", "id": "345bcbc8", "metadata": {}, "source": [ "## Instantiate model class" ] }, { "cell_type": "code", "execution_count": 2, "id": "26516fe2", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:10:37.578279Z", "iopub.status.busy": "2026-07-04T19:10:37.577926Z", "iopub.status.idle": "2026-07-04T19:10:37.583421Z", "shell.execute_reply": "2026-07-04T19:10:37.582386Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "class VidalBralo(pyagingModel):\n", " def __init__(self):\n", " super().__init__()\n", "\n", " def preprocess(self, x):\n", " if self.reference_values is None:\n", " return x\n", " if isinstance(self.reference_values, torch.Tensor):\n", " reference = self.reference_values.to(device=x.device, dtype=x.dtype)\n", " else:\n", " reference = torch.tensor(self.reference_values, device=x.device, dtype=x.dtype)\n", " return torch.where(torch.isnan(x), reference, x)\n", "\n", " def postprocess(self, x):\n", " return x\n", "\n" ] } ], "source": [ "def print_entire_class(cls):\n", " source = inspect.getsource(cls)\n", " print(source)\n", "\n", "print_entire_class(pya.models.VidalBralo)" ] }, { "cell_type": "code", "execution_count": 3, "id": "c8a99eb0", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:10:37.585260Z", "iopub.status.busy": "2026-07-04T19:10:37.585073Z", "iopub.status.idle": "2026-07-04T19:10:37.588151Z", "shell.execute_reply": "2026-07-04T19:10:37.586971Z" } }, "outputs": [], "source": [ "model = pya.models.VidalBralo()" ] }, { "cell_type": "markdown", "id": "f96ce673", "metadata": {}, "source": [ "## Define clock metadata" ] }, { "cell_type": "code", "execution_count": 4, "id": "61667f70", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:10:37.592761Z", "iopub.status.busy": "2026-07-04T19:10:37.592286Z", "iopub.status.idle": "2026-07-04T19:10:37.599245Z", "shell.execute_reply": "2026-07-04T19:10:37.598192Z" } }, "outputs": [], "source": [ "model.metadata[\"clock_name\"] = \"vidalbralo\"\n", "model.metadata[\"data_type\"] = \"DNA methylation\" # Paper: DNA methylation age measures\n", "model.metadata[\"species\"] = \"Homo sapiens\" # Paper: 390 healthy Caucasian donors\n", "model.metadata[\"year\"] = 2016\n", "model.metadata[\"approved_by_author\"] = \"⌛\"\n", "model.metadata[\"citation\"] = \"Vidal-Bralo, Laura, Yolanda Lopez-Golan, and Antonio Gonzalez. \\\"Simplified assay for epigenetic age estimation in whole blood of adults.\\\" Frontiers in Genetics 7 (2016): 126.\"\n", "model.metadata[\"doi\"] = \"https://doi.org/10.3389/fgene.2016.00126\"\n", "model.metadata[\"notes\"] = \"Eight-CpG whole-blood chronological-age estimator selected by forward stepwise regression in 390 adults and calibrated by multiple linear regression; CpGs were chosen for compatibility with a single multiplex MS-SNuPE assay.\"\n", "model.metadata[\"research_only\"] = None\n", "model.metadata[\"tissue\"] = [\"whole blood\"] # Paper: whole blood (WB)\n", "model.metadata[\"predicts\"] = [\"chronological age\"] # Paper: estimate age from blood DNA\n", "model.metadata[\"training_target\"] = [\"chronological age\"] # Paper: forward stepwise linear regression with age\n", "model.metadata[\"unit\"] = [\"years\"] # Paper: Age range; MAD ... years\n", "model.metadata[\"model_type\"] = \"linear regression\" # Paper: multiple linear regression parameters of the 8 CpG DmAM\n", "model.metadata[\"platform\"] = [\"Illumina 27K\"] # Paper: training ... obtained with the Illumina Human Methylation 27K BeadChip\n", "model.metadata[\"population\"] = \"adults\" # Paper: 390 healthy subjects older than 20 years\n", "model.metadata[\"journal\"] = \"Frontiers in Genetics\"\n", "model.metadata[\"last_author\"] = \"Antonio Gonzalez\"\n", "model.metadata[\"n_features\"] = 8\n", "model.metadata[\"citations\"] = 145\n", "model.metadata[\"citations_date\"] = \"2026-07-05\"\n" ] }, { "cell_type": "markdown", "id": "b49ab90b", "metadata": {}, "source": [ "## Download clock dependencies" ] }, { "cell_type": "code", "execution_count": 5, "id": "63c7ad3c", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:10:37.601499Z", "iopub.status.busy": "2026-07-04T19:10:37.601292Z", "iopub.status.idle": "2026-07-04T19:10:37.604147Z", "shell.execute_reply": "2026-07-04T19:10:37.603043Z" } }, "outputs": [], "source": [ "# The 8-CpG DmAM coefficients are given directly in Table 2 of\n", "# Vidal-Bralo et al. 2016 (Front. Genet. 7:126); they are defined inline below,\n", "# so no external download is required." ] }, { "cell_type": "markdown", "id": "357edd3a", "metadata": {}, "source": [ "## Load features" ] }, { "cell_type": "code", "execution_count": 6, "id": "55100340", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:10:37.607284Z", "iopub.status.busy": "2026-07-04T19:10:37.607099Z", "iopub.status.idle": "2026-07-04T19:10:37.615611Z", "shell.execute_reply": "2026-07-04T19:10:37.614591Z" } }, "outputs": [], "source": [ "# Multiple linear regression parameters of the 8 CpG DmAM (Vidal-Bralo et al. 2016, Table 2)\n", "coef_df = pd.DataFrame({\n", " 'CpG': ['cg16386080', 'cg24768561', 'cg19761273', 'cg25809905',\n", " 'cg09809672', 'cg02228185', 'cg17471102', 'cg10917602'],\n", " 'coefficient': [59.5, 33.9, -44.0, -19.7, -22.8, -16.8, -17.7, -11.4],\n", "})\n", "model.features = coef_df['CpG'].tolist()" ] }, { "cell_type": "markdown", "id": "c9a39d6c", "metadata": {}, "source": [ "## Load weights into base model" ] }, { "cell_type": "code", "execution_count": 7, "id": "a1ec53d8", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:10:37.617562Z", "iopub.status.busy": "2026-07-04T19:10:37.617386Z", "iopub.status.idle": "2026-07-04T19:10:37.625704Z", "shell.execute_reply": "2026-07-04T19:10:37.624815Z" } }, "outputs": [], "source": [ "weights = torch.tensor(coef_df['coefficient'].tolist()).unsqueeze(0).float()\n", "intercept = torch.tensor([84.7]).float()" ] }, { "cell_type": "code", "execution_count": 8, "id": "8d1f4eaa", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:10:37.628313Z", "iopub.status.busy": "2026-07-04T19:10:37.628021Z", "iopub.status.idle": "2026-07-04T19:10:37.635702Z", "shell.execute_reply": "2026-07-04T19:10:37.634965Z" } }, "outputs": [], "source": [ "base_model = pya.models.LinearModel(input_dim=len(model.features))\n", "\n", "base_model.linear.weight.data = weights.float()\n", "base_model.linear.bias.data = intercept.float()\n", "\n", "model.base_model = base_model" ] }, { "cell_type": "markdown", "id": "128eb0a2", "metadata": {}, "source": [ "## Load reference values" ] }, { "cell_type": "code", "execution_count": 9, "id": "355c423c", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:10:37.639378Z", "iopub.status.busy": "2026-07-04T19:10:37.639018Z", "iopub.status.idle": "2026-07-04T19:10:37.643684Z", "shell.execute_reply": "2026-07-04T19:10:37.642477Z" } }, "outputs": [], "source": [ "model.reference_values = None" ] }, { "cell_type": "markdown", "id": "824ad0a5", "metadata": {}, "source": [ "## Load preprocess and postprocess objects" ] }, { "cell_type": "code", "execution_count": 10, "id": "48fbb8c8", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:10:37.645964Z", "iopub.status.busy": "2026-07-04T19:10:37.645805Z", "iopub.status.idle": "2026-07-04T19:10:37.649077Z", "shell.execute_reply": "2026-07-04T19:10:37.647615Z" } }, "outputs": [], "source": [ "model.preprocess_name = None\n", "model.preprocess_dependencies = None" ] }, { "cell_type": "code", "execution_count": 11, "id": "4b4a1619", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:10:37.652166Z", "iopub.status.busy": "2026-07-04T19:10:37.652004Z", "iopub.status.idle": "2026-07-04T19:10:37.655693Z", "shell.execute_reply": "2026-07-04T19:10:37.654613Z" } }, "outputs": [], "source": [ "model.postprocess_name = None\n", "model.postprocess_dependencies = None" ] }, { "cell_type": "markdown", "id": "0faec4a7", "metadata": {}, "source": [ "## Check all clock parameters" ] }, { "cell_type": "code", "execution_count": 12, "id": "dac05a3d", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:10:37.660253Z", "iopub.status.busy": "2026-07-04T19:10:37.659795Z", "iopub.status.idle": "2026-07-04T19:10:37.687764Z", "shell.execute_reply": "2026-07-04T19:10:37.686787Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "%==================================== Model Details ====================================%\n", "Model Attributes:\n", "\n", "training: True\n", "metadata: {'approved_by_author': '⌛',\n", " 'citation': 'Vidal-Bralo, Laura, Yolanda Lopez-Golan, and Antonio Gonzalez. '\n", " '\"Simplified assay for epigenetic age estimation in whole blood '\n", " 'of adults.\" Frontiers in genetics 7 (2016): 126.',\n", " 'clock_name': 'vidalbralo',\n", " 'data_type': 'methylation',\n", " 'doi': 'https://doi.org/10.3389/fgene.2016.00126',\n", " 'notes': None,\n", " 'research_only': None,\n", " 'species': 'Homo sapiens',\n", " 'version': None,\n", " 'year': 2016}\n", "reference_values: None\n", "preprocess_name: None\n", "preprocess_dependencies: None\n", "postprocess_name: None\n", "postprocess_dependencies: None\n", "features: ['cg16386080',\n", " 'cg24768561',\n", " 'cg19761273',\n", " 'cg25809905',\n", " 'cg09809672',\n", " 'cg02228185',\n", " 'cg17471102',\n", " 'cg10917602']\n", "base_model_features: None\n", "\n", "%==================================== Model Details ====================================%\n", "Model Structure:\n", "\n", "base_model: LinearModel(\n", " (linear): Linear(in_features=8, out_features=1, bias=True)\n", ")\n", "\n", "%==================================== Model Details ====================================%\n", "Model Parameters and Weights:\n", "\n", "base_model.linear.weight: tensor([[ 59.5000, 33.9000, -44.0000, -19.7000, -22.8000, -16.8000, -17.7000,\n", " -11.4000]])\n", "base_model.linear.bias: tensor([84.7000])\n", "\n", "%==================================== Model Details ====================================%\n", "\n" ] } ], "source": [ "pya.utils.print_model_details(model)" ] }, { "cell_type": "markdown", "id": "3fca3f6f", "metadata": {}, "source": [ "## Basic test" ] }, { "cell_type": "code", "execution_count": 13, "id": "6297584f", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:10:37.689696Z", "iopub.status.busy": "2026-07-04T19:10:37.689530Z", "iopub.status.idle": "2026-07-04T19:10:37.705864Z", "shell.execute_reply": "2026-07-04T19:10:37.704964Z" } }, "outputs": [ { "data": { "text/plain": [ "tensor([[126.0523],\n", " [104.1466],\n", " [ 75.2432],\n", " [ 58.5473],\n", " [160.9427],\n", " [ 27.2660],\n", " [137.7398],\n", " [262.1763],\n", " [125.6747],\n", " [171.3440]], dtype=torch.float64, grad_fn=)" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "torch.manual_seed(42)\n", "input = torch.randn(10, len(model.features), dtype=float)\n", "model.eval()\n", "model.to(float)\n", "pred = model(input)\n", "pred" ] }, { "cell_type": "markdown", "id": "cbbe2378", "metadata": {}, "source": [ "## Save torch model" ] }, { "cell_type": "code", "execution_count": 14, "id": "7ba9465a", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:10:37.707817Z", "iopub.status.busy": "2026-07-04T19:10:37.707673Z", "iopub.status.idle": "2026-07-04T19:10:37.714323Z", "shell.execute_reply": "2026-07-04T19:10:37.713380Z" } }, "outputs": [], "source": [ "torch.save(model, f\"../weights/{model.metadata['clock_name']}.pt\")" ] }, { "cell_type": "markdown", "id": "619ea422", "metadata": {}, "source": [ "## Clear directory\n", "" ] }, { "cell_type": "code", "execution_count": 15, "id": "bfb7a130", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:10:37.716060Z", "iopub.status.busy": "2026-07-04T19:10:37.715875Z", "iopub.status.idle": "2026-07-04T19:10:37.720797Z", "shell.execute_reply": "2026-07-04T19:10:37.720150Z" } }, "outputs": [], "source": [ "# Function to remove a folder and all its contents\n", "def remove_folder(path):\n", " try:\n", " shutil.rmtree(path)\n", " print(f\"Deleted folder: {path}\")\n", " except Exception as e:\n", " print(f\"Error deleting folder {path}: {e}\")\n", "\n", "# Get a list of all files and folders in the current directory\n", "all_items = os.listdir('.')\n", "\n", "# Loop through the items\n", "for item in all_items:\n", " # Check if it's a file and does not end with .ipynb\n", " if os.path.isfile(item) and not item.endswith('.ipynb'):\n", " os.remove(item)\n", " print(f\"Deleted file: {item}\")\n", " # Check if it's a folder\n", " elif os.path.isdir(item):\n", " remove_folder(item)" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.13.11" } }, "nbformat": 4, "nbformat_minor": 5 }