{ "cells": [ { "cell_type": "markdown", "id": "2f04eee0-5928-4e74-a754-6dc2e528810c", "metadata": {}, "source": [ "# CpGPTGrimAge3" ] }, { "cell_type": "markdown", "id": "a3f514a3-772c-4a14-afdf-5a8376851ff4", "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": "d95fafdc-643a-40ea-a689-200bd132e90c", "metadata": {}, "source": [ "Let's first import some packages:" ] }, { "cell_type": "code", "execution_count": 1, "id": "4adfb4de-cd79-4913-a1af-9e23e9e236c9", "metadata": { "execution": { "iopub.execute_input": "2025-04-07T17:51:41.316681Z", "iopub.status.busy": "2025-04-07T17:51:41.316440Z", "iopub.status.idle": "2025-04-07T17:51:42.738147Z", "shell.execute_reply": "2025-04-07T17:51:42.737780Z" } }, "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\n", "import numpy as np" ] }, { "cell_type": "markdown", "id": "145082e5-ced4-47ae-88c0-cb69773e3c5a", "metadata": {}, "source": [ "## Instantiate model class" ] }, { "cell_type": "code", "execution_count": 2, "id": "8aa77372-7ed3-4da7-abc9-d30372106139", "metadata": { "execution": { "iopub.execute_input": "2025-04-07T17:51:42.740018Z", "iopub.status.busy": "2025-04-07T17:51:42.739761Z", "iopub.status.idle": "2025-04-07T17:51:42.750935Z", "shell.execute_reply": "2025-04-07T17:51:42.750574Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "class CpGPTGrimAge3(pyagingModel):\n", " def __init__(self):\n", " super().__init__()\n", "\n", " def preprocess(self, x):\n", " \"\"\"\n", " Scales an array based on the median and standard deviation.\n", " \"\"\"\n", " median = torch.tensor(self.preprocess_dependencies[0], device=x.device, dtype=x.dtype)\n", " std = torch.tensor(self.preprocess_dependencies[1], device=x.device, dtype=x.dtype)\n", " x = (x - median) / std\n", " return x\n", "\n", " def postprocess(self, x):\n", " \"\"\"\n", " Converts from a Cox parameter to age in units of years.\n", " \"\"\"\n", " cox_mean = self.postprocess_dependencies[0]\n", " cox_std = self.postprocess_dependencies[1]\n", " age_mean = self.postprocess_dependencies[2]\n", " age_std = self.postprocess_dependencies[3]\n", "\n", " # Normalize\n", " x = (x - cox_mean) / cox_std\n", "\n", " # Scale\n", " x = (x * age_std) + age_mean\n", "\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.CpGPTGrimAge3)" ] }, { "cell_type": "code", "execution_count": 3, "id": "78536494-f1d9-44de-8583-c89a310d2307", "metadata": { "execution": { "iopub.execute_input": "2025-04-07T17:51:42.753015Z", "iopub.status.busy": "2025-04-07T17:51:42.752877Z", "iopub.status.idle": "2025-04-07T17:51:42.754599Z", "shell.execute_reply": "2025-04-07T17:51:42.754314Z" } }, "outputs": [], "source": [ "model = pya.models.CpGPTGrimAge3()" ] }, { "cell_type": "markdown", "id": "51f8615e-01fa-4aa5-b196-3ee2b35d261c", "metadata": {}, "source": [ "## Define clock metadata" ] }, { "cell_type": "code", "execution_count": 4, "id": "135ce001-03f7-4025-bceb-01a3e2e2b0ef", "metadata": { "execution": { "iopub.execute_input": "2025-04-07T17:51:42.755964Z", "iopub.status.busy": "2025-04-07T17:51:42.755873Z", "iopub.status.idle": "2025-04-07T17:51:42.758035Z", "shell.execute_reply": "2025-04-07T17:51:42.757775Z" } }, "outputs": [], "source": [ "model.metadata[\"clock_name\"] = \"cpgptgrimage3\"\n", "model.metadata[\"data_type\"] = \"DNA methylation\" # Paper: CpGPT models DNA-methylation profiles and derives methylation-based aging and mortality predictors.\n", "model.metadata[\"species\"] = \"Homo sapiens\" # Paper: The assigned mortality task uses human DNA-methylation cohorts.\n", "model.metadata[\"year\"] = 2024\n", "model.metadata[\"approved_by_author\"] = \"✅\"\n", "model.metadata[\"citation\"] = \"de Lima Camillo, L. P., Sehgal, R., Armstrong, J., Higgins-Chen, A. T., Horvath, S., & Wang, B. CpGPT: a foundation model for DNA methylation. bioRxiv 2024.10.24.619766 (2024).\"\n", "model.metadata[\"doi\"] = \"https://doi.org/10.1101/2024.10.24.619766\"\n", "model.metadata[\"notes\"] = \"CpGPTGrimAge3 implementation combining chronological age, GrimAge2 DNAm proxies, and CpGPT-predicted plasma-protein proxies in a Cox linear predictor that is calibrated to years.\"\n", "model.metadata[\"research_only\"] = True\n", "model.metadata[\"tissue\"] = [\"whole blood\"] # Paper: cpgptgrimage3 and cpgptpcgrimage3 were trained in blood with the 450k array to predict mortality. it was trained in the FHS cohort with methylation data.\n", "model.metadata[\"predicts\"] = [\"biological age\", \"mortality risk\"] # Paper: The implementation converts the Cox parameter to an age-scaled output.\n", "model.metadata[\"training_target\"] = [\"mortality\"] # Paper: The mortality model was trained with a modified Cox proportional-hazards loss against time-to-mortality data.\n", "model.metadata[\"unit\"] = [\"years\"] # Paper: The implementation normalizes a Cox score and rescales it with an age mean and standard deviation.\n", "model.metadata[\"model_type\"] = \"Cox proportional hazards regression\" # Paper: The implementation applies a linear Cox score to standardized age and biomarker proxies and calibrates the score to years.\n", "model.metadata[\"platform\"] = [\"Illumina 450K\"] # Paper: cpgptgrimage3 and cpgptpcgrimage3 were trained in blood with the 450k array to predict mortality. it was trained in the FHS cohort with methylation data.\n", "model.metadata[\"population\"] = \"adults\" # Paper: cpgptgrimage3 and cpgptpcgrimage3 were trained in blood with the 450k array to predict mortality. it was trained in the FHS cohort with methylation data.\n", "model.metadata[\"journal\"] = \"bioRxiv\"\n", "model.metadata[\"last_author\"] = \"Bo Wang\"\n", "model.metadata[\"n_features\"] = 24\n", "model.metadata[\"citations\"] = 30\n", "model.metadata[\"citations_date\"] = \"2026-07-05\"\n" ] }, { "cell_type": "markdown", "id": "74492239-5aae-4026-9d90-6bc9c574c110", "metadata": {}, "source": [ "## Download clock dependencies" ] }, { "cell_type": "code", "execution_count": 5, "id": "95f6ba57", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "|-----------> Data found in ./cpgpt_grimage3_weights_all_datasets_reliable.csv\n", "|-----------> Data found in ./input_scaler_mean_all_datasets_reliable.npy\n", "|-----------> Data found in ./input_scaler_scale_all_datasets_reliable.npy\n" ] } ], "source": [ "logger = pya.logger.Logger()\n", "urls = [\n", " \"https://huggingface.co/lucascamillomd/pyaging-data/resolve/main/supporting_files/cpgpt_grimage3_dependencies/reliable/cpgpt_grimage3_weights_all_datasets_reliable.csv\",\n", " \"https://huggingface.co/lucascamillomd/pyaging-data/resolve/main/supporting_files/cpgpt_grimage3_dependencies/reliable/input_scaler_mean_all_datasets_reliable.npy\",\n", " \"https://huggingface.co/lucascamillomd/pyaging-data/resolve/main/supporting_files/cpgpt_grimage3_dependencies/reliable/input_scaler_scale_all_datasets_reliable.npy\"\n", "]\n", "dir = \".\"\n", "for url in urls:\n", " pya.utils.download(url, dir, logger, indent_level=1)" ] }, { "cell_type": "markdown", "id": "a14c7fc1-abe5-42a3-8bc9-0987521ddf33", "metadata": {}, "source": [ "## Load features" ] }, { "cell_type": "markdown", "id": "3e737582-3a28-4f55-8da9-3e34125362cc", "metadata": {}, "source": [ "#### From CSV" ] }, { "cell_type": "code", "execution_count": 6, "id": "f1486db4", "metadata": {}, "outputs": [], "source": [ "df = pd.read_csv('cpgpt_grimage3_weights_all_datasets_reliable.csv')\n", "model.features = df['feature'].tolist()" ] }, { "cell_type": "code", "execution_count": 7, "id": "8c43149c", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
| \n", " | feature | \n", "coefficient | \n", "
|---|---|---|
| 0 | \n", "age | \n", "0.845167 | \n", "
| 1 | \n", "grimage2timp1 | \n", "0.318954 | \n", "
| 2 | \n", "grimage2packyrs | \n", "0.385882 | \n", "
| 3 | \n", "grimage2logcrp | \n", "0.404675 | \n", "
| 4 | \n", "grimage2adm | \n", "0.180551 | \n", "