{ "cells": [ { "cell_type": "markdown", "id": "3afe26c6", "metadata": {}, "source": [ "# DunedinPoAm38" ] }, { "cell_type": "markdown", "id": "c56dac27", "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": "ace60ef5", "metadata": {}, "source": [ "Let's first import some packages:" ] }, { "cell_type": "code", "execution_count": 1, "id": "ffccb098", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:51:49.069875Z", "iopub.status.busy": "2026-07-04T19:51:49.069673Z", "iopub.status.idle": "2026-07-04T19:51:51.022943Z", "shell.execute_reply": "2026-07-04T19:51:51.022492Z" } }, "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": "7f22e588", "metadata": {}, "source": [ "## Instantiate model class" ] }, { "cell_type": "code", "execution_count": 2, "id": "052ed739", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:51:51.024730Z", "iopub.status.busy": "2026-07-04T19:51:51.024525Z", "iopub.status.idle": "2026-07-04T19:51:51.027389Z", "shell.execute_reply": "2026-07-04T19:51:51.026894Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "class DunedinPoAm38(LinearReferenceClock):\n", " pass\n", "\n" ] } ], "source": [ "def print_entire_class(cls):\n", " source = inspect.getsource(cls)\n", " print(source)\n", "\n", "print_entire_class(pya.models.DunedinPoAm38)" ] }, { "cell_type": "code", "execution_count": 3, "id": "86c09545", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:51:51.028528Z", "iopub.status.busy": "2026-07-04T19:51:51.028441Z", "iopub.status.idle": "2026-07-04T19:51:51.029926Z", "shell.execute_reply": "2026-07-04T19:51:51.029614Z" } }, "outputs": [], "source": [ "model = pya.models.DunedinPoAm38()" ] }, { "cell_type": "markdown", "id": "e61817d7", "metadata": {}, "source": [ "## Define clock metadata" ] }, { "cell_type": "code", "execution_count": 4, "id": "f8acc42c", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:51:51.031083Z", "iopub.status.busy": "2026-07-04T19:51:51.031006Z", "iopub.status.idle": "2026-07-04T19:51:51.033025Z", "shell.execute_reply": "2026-07-04T19:51:51.032623Z" } }, "outputs": [], "source": [ "model.metadata[\"clock_name\"] = \"dunedinpoam38\"\n", "model.metadata[\"data_type\"] = \"DNA methylation\" # Paper: DunedinPoAm uses blood DNA methylation.\n", "model.metadata[\"species\"] = \"Homo sapiens\" # Paper: The model was developed in members of the human Dunedin Study.\n", "model.metadata[\"year\"] = 2020\n", "model.metadata[\"approved_by_author\"] = \"⌛\"\n", "model.metadata[\"citation\"] = \"Belsky, D.W., Caspi, A., Arseneault, L. et al. Quantification of the pace of biological aging in humans through a blood test, the DunedinPoAm DNA methylation algorithm. eLife 9, e54870 (2020).\"\n", "model.metadata[\"doi\"] = \"https://doi.org/10.7554/elife.54870\"\n", "model.metadata[\"notes\"] = \"Whole-blood elastic-net estimator of the rate of biological aging, trained at age 38 against a longitudinal 18-biomarker Pace-of-Aging composite measured over ages 26–38.\"\n", "model.metadata[\"research_only\"] = None\n", "model.metadata[\"tissue\"] = [\"whole blood\"] # Paper: DNA methylation at age 38 was measured in whole blood.\n", "model.metadata[\"predicts\"] = [\"pace of aging\"] # Paper: DunedinPoAm is a rate measure of how fast biological aging is occurring.\n", "model.metadata[\"training_target\"] = [\"pace of aging\"] # Paper: Elastic net was fitted to the Pace-of-Aging composite derived from 18 biomarkers at ages 26, 32 and 38.\n", "model.metadata[\"unit\"] = [\"biological years per chronological year\"] # Paper: A value near 1 represents one year of biological aging per calendar year.\n", "model.metadata[\"model_type\"] = \"elastic net regression\" # Paper: Elastic-net regression was used to derive the methylation algorithm.\n", "model.metadata[\"platform\"] = [\"Illumina 450K\"] # Paper: Feature selection and model fitting used age-38 Dunedin whole-blood methylation measured on the 450K array; EPIC was used for later external application.\n", "model.metadata[\"population\"] = \"adults\" # Paper: The target was developed in 954 members of the same-year Dunedin birth cohort.\n", "model.metadata[\"journal\"] = \"eLife\"\n", "model.metadata[\"last_author\"] = \"Terrie E. Moffitt\"\n", "model.metadata[\"n_features\"] = 46\n", "model.metadata[\"citations\"] = 666\n", "model.metadata[\"citations_date\"] = \"2026-07-05\"\n" ] }, { "cell_type": "markdown", "id": "1b09d5f0", "metadata": {}, "source": [ "## Download clock dependencies" ] }, { "cell_type": "code", "execution_count": 5, "id": "b5e0115b", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:51:51.034144Z", "iopub.status.busy": "2026-07-04T19:51:51.034075Z", "iopub.status.idle": "2026-07-04T19:51:51.325931Z", "shell.execute_reply": "2026-07-04T19:51:51.325297Z" } }, "outputs": [ { "data": { "text/plain": [ "0" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "os.system(f\"curl -sL -o coefficients.csv https://raw.githubusercontent.com/bio-learn/biolearn/180852e2bab473303cb85da627178b1695ee9d86/biolearn/data/DunedinPoAm38.csv\")" ] }, { "cell_type": "markdown", "id": "247c122d", "metadata": {}, "source": [ "## Load features" ] }, { "cell_type": "code", "execution_count": 6, "id": "0ffb3b23", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:51:51.328578Z", "iopub.status.busy": "2026-07-04T19:51:51.328383Z", "iopub.status.idle": "2026-07-04T19:51:51.336297Z", "shell.execute_reply": "2026-07-04T19:51:51.335340Z" } }, "outputs": [], "source": [ "df = pd.read_csv('coefficients.csv')\n", "mask = df['CpGmarker'].astype(str).str.lower().isin(['intercept', '(intercept)'])\n", "intercept_value = float(df.loc[mask, 'CoefficientTraining'].iloc[0]) if mask.any() else 0.0\n", "coef_df = df.loc[~mask].reset_index(drop=True)\n", "model.features = coef_df['CpGmarker'].tolist()" ] }, { "cell_type": "markdown", "id": "3c7dd300", "metadata": {}, "source": [ "## Load weights into base model" ] }, { "cell_type": "code", "execution_count": 7, "id": "ea267115", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:51:51.338817Z", "iopub.status.busy": "2026-07-04T19:51:51.338610Z", "iopub.status.idle": "2026-07-04T19:51:51.342005Z", "shell.execute_reply": "2026-07-04T19:51:51.341198Z" } }, "outputs": [], "source": [ "weights = torch.tensor(coef_df['CoefficientTraining'].tolist()).unsqueeze(0).float()\n", "intercept = torch.tensor([intercept_value]).float()" ] }, { "cell_type": "code", "execution_count": 8, "id": "7bc4f1ef", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:51:51.343916Z", "iopub.status.busy": "2026-07-04T19:51:51.343750Z", "iopub.status.idle": "2026-07-04T19:51:51.346700Z", "shell.execute_reply": "2026-07-04T19:51:51.346151Z" } }, "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": "d70fc713", "metadata": {}, "source": [ "## Load reference values" ] }, { "cell_type": "code", "execution_count": 9, "id": "5b6afea1", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:51:51.348488Z", "iopub.status.busy": "2026-07-04T19:51:51.348346Z", "iopub.status.idle": "2026-07-04T19:51:51.350333Z", "shell.execute_reply": "2026-07-04T19:51:51.349761Z" } }, "outputs": [], "source": [ "model.reference_values = None" ] }, { "cell_type": "markdown", "id": "ccaaa9b4", "metadata": {}, "source": [ "## Load preprocess and postprocess objects" ] }, { "cell_type": "code", "execution_count": 10, "id": "4f106911", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:51:51.352278Z", "iopub.status.busy": "2026-07-04T19:51:51.352139Z", "iopub.status.idle": "2026-07-04T19:51:51.354172Z", "shell.execute_reply": "2026-07-04T19:51:51.353586Z" } }, "outputs": [], "source": [ "model.preprocess_name = None\n", "model.preprocess_dependencies = None" ] }, { "cell_type": "code", "execution_count": 11, "id": "018e94b7", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:51:51.355855Z", "iopub.status.busy": "2026-07-04T19:51:51.355720Z", "iopub.status.idle": "2026-07-04T19:51:51.357805Z", "shell.execute_reply": "2026-07-04T19:51:51.357221Z" } }, "outputs": [], "source": [ "model.postprocess_name = None\n", "model.postprocess_dependencies = None" ] }, { "cell_type": "markdown", "id": "7500ab2b", "metadata": {}, "source": [ "## Check all clock parameters" ] }, { "cell_type": "code", "execution_count": 12, "id": "2554719c", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:51:51.359348Z", "iopub.status.busy": "2026-07-04T19:51:51.359226Z", "iopub.status.idle": "2026-07-04T19:51:51.363894Z", "shell.execute_reply": "2026-07-04T19:51:51.363440Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "%==================================== Model Details ====================================%\n", "Model Attributes:\n", "\n", "training: True\n", "metadata: {'approved_by_author': '⌛',\n", " 'citation': 'Belsky, Daniel W., et al. \"Quantification of the pace of '\n", " 'biological aging in humans through a blood test, the DunedinPoAm '\n", " 'DNA methylation algorithm.\" eLife 9 (2020): e54870.',\n", " 'clock_name': 'dunedinpoam38',\n", " 'data_type': 'methylation',\n", " 'doi': 'https://doi.org/10.7554/eLife.54870',\n", " 'notes': None,\n", " 'research_only': None,\n", " 'species': 'Homo sapiens',\n", " 'version': None,\n", " 'year': 2020}\n", "reference_values: None\n", "preprocess_name: None\n", "preprocess_dependencies: None\n", "postprocess_name: None\n", "postprocess_dependencies: None\n", "features: ['cg02582848', 'cg03730474', 'cg03922834', 'cg04480708', 'cg05227215', 'cg05513157', 'cg05575921', 'cg06133392', 'cg07045089', 'cg07185119', 'cg07986378', 'cg08376310', 'cg09349128', 'cg09404119', 'cg10727171', 'cg10919522', 'cg11574055', 'cg11674508', 'cg11897887', 'cg13074055', 'cg13121699', 'cg14485633', 'cg14775114', 'cg15018359', 'cg19422687', 'cg19510038', 'cg19743820', 'cg20451986', 'cg21079030', 'cg21370522']... [Total elements: 46]\n", "base_model_features: None\n", "\n", "%==================================== Model Details ====================================%\n", "Model Structure:\n", "\n", "base_model: LinearModel(\n", " (linear): Linear(in_features=46, out_features=1, bias=True)\n", ")\n", "\n", "%==================================== Model Details ====================================%\n", "Model Parameters and Weights:\n", "\n", "base_model.linear.weight: [0.16865555942058563, -0.030930202454328537, 0.10762208700180054, 0.32811808586120605, 0.20570090413093567, -0.19139723479747772, -0.2568254768848419, -0.013769027777016163, 0.002585785463452339, 0.007155308965593576, -0.3933064937591553, 0.0372597873210907, -0.6891953945159912, 0.010446280241012573, -0.014004146680235863, -0.32410237193107605, 0.06089318171143532, 0.07774978131055832, 0.0017560641281306744, -0.0569249764084816, -0.05277020111680031, 0.030463986098766327, -0.1716403365135193, -0.19539450109004974, 0.2227468192577362, -0.16681736707687378, -0.10328859090805054, 0.01552193146198988, 0.008607408963143826, 0.1834435760974884]... [Tensor of shape torch.Size([1, 46])]\n", "base_model.linear.bias: tensor([-0.0693])\n", "\n", "%==================================== Model Details ====================================%\n", "\n" ] } ], "source": [ "pya.utils.print_model_details(model)" ] }, { "cell_type": "markdown", "id": "7912cf69", "metadata": {}, "source": [ "## Basic test" ] }, { "cell_type": "code", "execution_count": 13, "id": "b4cdd204", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:51:51.365454Z", "iopub.status.busy": "2026-07-04T19:51:51.365348Z", "iopub.status.idle": "2026-07-04T19:51:51.370117Z", "shell.execute_reply": "2026-07-04T19:51:51.369506Z" } }, "outputs": [ { "data": { "text/plain": [ "tensor([[ 2.5557],\n", " [ 1.7208],\n", " [ 1.9728],\n", " [ 0.4137],\n", " [-1.7117],\n", " [-1.8462],\n", " [-2.8839],\n", " [-2.1433],\n", " [-0.0711],\n", " [-1.1731]], 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": "d8b09202", "metadata": {}, "source": [ "## Save torch model" ] }, { "cell_type": "code", "execution_count": 14, "id": "f7d23e7b", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:51:51.372012Z", "iopub.status.busy": "2026-07-04T19:51:51.371857Z", "iopub.status.idle": "2026-07-04T19:51:51.375788Z", "shell.execute_reply": "2026-07-04T19:51:51.375344Z" } }, "outputs": [], "source": [ "torch.save(model, f\"../weights/{model.metadata['clock_name']}.pt\")" ] }, { "cell_type": "markdown", "id": "e5eccd9a", "metadata": {}, "source": [ "## Clear directory\n", "" ] }, { "cell_type": "code", "execution_count": 15, "id": "c55c1ce2", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:51:51.377229Z", "iopub.status.busy": "2026-07-04T19:51:51.377132Z", "iopub.status.idle": "2026-07-04T19:51:51.381019Z", "shell.execute_reply": "2026-07-04T19:51:51.380617Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Deleted file: coefficients.csv\n" ] } ], "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 }