{ "cells": [ { "cell_type": "markdown", "id": "ef375ab3", "metadata": {}, "source": [ "# Wu" ] }, { "cell_type": "markdown", "id": "37ab0032", "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": "837f458e", "metadata": {}, "source": [ "Let's first import some packages:" ] }, { "cell_type": "code", "execution_count": 1, "id": "36c174fc", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T20:25:14.023480Z", "iopub.status.busy": "2026-07-04T20:25:14.023303Z", "iopub.status.idle": "2026-07-04T20:25:16.159946Z", "shell.execute_reply": "2026-07-04T20:25:16.159488Z" } }, "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": "2f093b20", "metadata": {}, "source": [ "## Instantiate model class" ] }, { "cell_type": "code", "execution_count": 2, "id": "7194c9c3", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T20:25:16.161621Z", "iopub.status.busy": "2026-07-04T20:25:16.161446Z", "iopub.status.idle": "2026-07-04T20:25:16.164487Z", "shell.execute_reply": "2026-07-04T20:25:16.163945Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "class Wu(LinearReferenceClock):\n", " def postprocess(self, x):\n", " \"\"\"Horvath anti-log (adult age = 48) giving months, then months to years.\"\"\"\n", " adult_age = 48\n", " mask_negative = x < 0\n", " mask_non_negative = ~mask_negative\n", " age = torch.empty_like(x)\n", " age[mask_negative] = (1 + adult_age) * torch.exp(x[mask_negative]) - 1\n", " age[mask_non_negative] = (1 + adult_age) * x[mask_non_negative] + adult_age\n", " return age / 12.0\n", "\n" ] } ], "source": [ "def print_entire_class(cls):\n", " source = inspect.getsource(cls)\n", " print(source)\n", "\n", "print_entire_class(pya.models.Wu)" ] }, { "cell_type": "code", "execution_count": 3, "id": "b29cdb41", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T20:25:16.165955Z", "iopub.status.busy": "2026-07-04T20:25:16.165842Z", "iopub.status.idle": "2026-07-04T20:25:16.167651Z", "shell.execute_reply": "2026-07-04T20:25:16.167264Z" } }, "outputs": [], "source": [ "model = pya.models.Wu()" ] }, { "cell_type": "markdown", "id": "1d9ed58a", "metadata": {}, "source": [ "## Define clock metadata" ] }, { "cell_type": "code", "execution_count": 4, "id": "c6f00388", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T20:25:16.169167Z", "iopub.status.busy": "2026-07-04T20:25:16.169042Z", "iopub.status.idle": "2026-07-04T20:25:16.170964Z", "shell.execute_reply": "2026-07-04T20:25:16.170647Z" } }, "outputs": [], "source": [ "model.metadata[\"clock_name\"] = \"wu\"\n", "model.metadata[\"data_type\"] = \"DNA methylation\" # Paper: The model predicts child age from blood DNA methylation.\n", "model.metadata[\"species\"] = \"Homo sapiens\" # Paper: The model was built from healthy children's blood samples.\n", "model.metadata[\"year\"] = 2019\n", "model.metadata[\"approved_by_author\"] = \"⌛\"\n", "model.metadata[\"citation\"] = \"Wu, X., et al. “DNA methylation profile is a quantitative measure of biological aging in children.” Aging 11(22): 10031–10051 (2019).\"\n", "model.metadata[\"doi\"] = \"https://doi.org/10.18632/aging.102399\"\n", "model.metadata[\"notes\"] = \"Child-specific 111-CpG blood age predictor built by sure independence screening followed by elastic net; pyaging converts the published month-scale output to years.\"\n", "model.metadata[\"research_only\"] = None\n", "model.metadata[\"tissue\"] = [\"whole blood\"] # Paper: Healthy children's peripheral blood datasets were used to build the model.\n", "model.metadata[\"predicts\"] = [\"chronological age\"] # Paper: The model's DNA methylation age closely predicted chronological age.\n", "model.metadata[\"training_target\"] = [\"chronological age\"] # Paper: Age was converted to months and transformed by function F before elastic-net fitting.\n", "model.metadata[\"unit\"] = [\"years\"] # Paper: The packaged postprocess applies the published inverse age transform in months and divides by 12.\n", "model.metadata[\"model_type\"] = \"screened elastic net regression\" # Paper: Sure independence screening reduced dimensionality before glmnet elastic-net regression.\n", "model.metadata[\"platform\"] = [\"Illumina 27K\", \"Illumina 450K\"] # Paper: Five training datasets used 27K and six used 450K; fitting used shared probes.\n", "model.metadata[\"population\"] = \"children\" # Paper: The model used 716 child blood samples aged 9–212 months.\n", "model.metadata[\"journal\"] = \"Aging\"\n", "model.metadata[\"last_author\"] = \"Huiying Liang\"\n", "model.metadata[\"n_features\"] = 111\n", "model.metadata[\"citations\"] = 82\n", "model.metadata[\"citations_date\"] = \"2026-07-05\"\n" ] }, { "cell_type": "markdown", "id": "577d6c30", "metadata": {}, "source": [ "## Download clock dependencies" ] }, { "cell_type": "code", "execution_count": 5, "id": "4b0f6969", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T20:25:16.172316Z", "iopub.status.busy": "2026-07-04T20:25:16.172226Z", "iopub.status.idle": "2026-07-04T20:25:16.502433Z", "shell.execute_reply": "2026-07-04T20:25:16.501259Z" } }, "outputs": [ { "data": { "text/plain": [ "0" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "os.system(f\"curl -sL -o wu.xlsx https://cdn.aging-us.com/article/102399/supplementary/SD3/0/aging-v11i22-102399-supplementary-material-SD3.xlsx\")" ] }, { "cell_type": "markdown", "id": "5eeae20d", "metadata": {}, "source": [ "## Load features" ] }, { "cell_type": "code", "execution_count": 6, "id": "89167494", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T20:25:16.506344Z", "iopub.status.busy": "2026-07-04T20:25:16.506003Z", "iopub.status.idle": "2026-07-04T20:25:16.643268Z", "shell.execute_reply": "2026-07-04T20:25:16.642753Z" } }, "outputs": [], "source": [ "df = pd.read_excel('wu.xlsx', sheet_name='CpGs_information')\n", "mask = df['CpGs'].astype(str).str.lower().isin(['intercept', '(intercept)'])\n", "intercept_value = float(df.loc[mask, 'Active.coefficients'].iloc[0]) if mask.any() else 0.0\n", "coef_df = df.loc[~mask].reset_index(drop=True)\n", "model.features = coef_df['CpGs'].tolist()" ] }, { "cell_type": "markdown", "id": "6849edd5", "metadata": {}, "source": [ "## Load weights into base model" ] }, { "cell_type": "code", "execution_count": 7, "id": "71d7fcc0", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T20:25:16.644695Z", "iopub.status.busy": "2026-07-04T20:25:16.644619Z", "iopub.status.idle": "2026-07-04T20:25:16.646808Z", "shell.execute_reply": "2026-07-04T20:25:16.646247Z" } }, "outputs": [], "source": [ "weights = torch.tensor(coef_df['Active.coefficients'].tolist()).unsqueeze(0).float()\n", "intercept = torch.tensor([intercept_value]).float()" ] }, { "cell_type": "code", "execution_count": 8, "id": "57ba4354", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T20:25:16.648075Z", "iopub.status.busy": "2026-07-04T20:25:16.647990Z", "iopub.status.idle": "2026-07-04T20:25:16.650460Z", "shell.execute_reply": "2026-07-04T20:25:16.649968Z" } }, "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": "1ea86ce2", "metadata": {}, "source": [ "## Load reference values" ] }, { "cell_type": "code", "execution_count": 9, "id": "91bd25eb", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T20:25:16.651805Z", "iopub.status.busy": "2026-07-04T20:25:16.651724Z", "iopub.status.idle": "2026-07-04T20:25:16.653446Z", "shell.execute_reply": "2026-07-04T20:25:16.653113Z" } }, "outputs": [], "source": [ "model.reference_values = None" ] }, { "cell_type": "markdown", "id": "55812aeb", "metadata": {}, "source": [ "## Load preprocess and postprocess objects" ] }, { "cell_type": "code", "execution_count": 10, "id": "548f9777", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T20:25:16.654632Z", "iopub.status.busy": "2026-07-04T20:25:16.654562Z", "iopub.status.idle": "2026-07-04T20:25:16.656059Z", "shell.execute_reply": "2026-07-04T20:25:16.655708Z" } }, "outputs": [], "source": [ "model.preprocess_name = None\n", "model.preprocess_dependencies = None" ] }, { "cell_type": "code", "execution_count": 11, "id": "9a949fec", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T20:25:16.657128Z", "iopub.status.busy": "2026-07-04T20:25:16.657047Z", "iopub.status.idle": "2026-07-04T20:25:16.658605Z", "shell.execute_reply": "2026-07-04T20:25:16.658275Z" } }, "outputs": [], "source": [ "model.postprocess_name = 'anti_log_linear'\n", "model.postprocess_dependencies = None" ] }, { "cell_type": "markdown", "id": "36bc88a6", "metadata": {}, "source": [ "## Check all clock parameters" ] }, { "cell_type": "code", "execution_count": 12, "id": "e71c82bd", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T20:25:16.659875Z", "iopub.status.busy": "2026-07-04T20:25:16.659791Z", "iopub.status.idle": "2026-07-04T20:25:16.662811Z", "shell.execute_reply": "2026-07-04T20:25:16.662415Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "%==================================== Model Details ====================================%\n", "Model Attributes:\n", "\n", "training: True\n", "metadata: {'approved_by_author': '⌛',\n", " 'citation': 'Wu, Xiaohui, et al. \"DNA methylation profile is a quantitative '\n", " 'measure of biological aging in children.\" Aging 11.22 (2019): '\n", " '10031-10051.',\n", " 'clock_name': 'wu',\n", " 'data_type': 'methylation',\n", " 'doi': 'https://doi.org/10.18632/aging.102399',\n", " 'notes': None,\n", " 'research_only': None,\n", " 'species': 'Homo sapiens',\n", " 'version': None,\n", " 'year': 2019}\n", "reference_values: None\n", "preprocess_name: None\n", "preprocess_dependencies: None\n", "postprocess_name: 'anti_log_linear'\n", "postprocess_dependencies: None\n", "features: ['cg00343092', 'cg00563932', 'cg00571634', 'cg00629217', 'cg01511567', 'cg01515426', 'cg01756060', 'cg01899253', 'cg02385474', 'cg02489552', 'cg02626929', 'cg02789485', 'cg03224418', 'cg03340261', 'cg03970609', 'cg04458548', 'cg04460372', 'cg04474832', 'cg04527989', 'cg04784672', 'cg05073035', 'cg05228408', 'cg05294455', 'cg05352668', 'cg05921699', 'cg05995267', 'cg06204948', 'cg06495803', 'cg07408456', 'cg08032971']... [Total elements: 111]\n", "base_model_features: None\n", "\n", "%==================================== Model Details ====================================%\n", "Model Structure:\n", "\n", "base_model: LinearModel(\n", " (linear): Linear(in_features=111, out_features=1, bias=True)\n", ")\n", "\n", "%==================================== Model Details ====================================%\n", "Model Parameters and Weights:\n", "\n", "base_model.linear.weight: [-1.1359976530075073, -2.2549679279327393, 1.2051302194595337, -1.4431147575378418, 0.5572546720504761, -4.753889083862305, -2.9755256175994873, -0.8770837783813477, -1.1014573574066162, 0.5342934727668762, 0.128916397690773, 0.35315999388694763, -0.16534864902496338, -6.689713954925537, 0.563336193561554, -0.28343427181243896, -0.9679408669471741, -1.2036683559417725, 0.6776095032691956, 1.7862359285354614, 1.216035008430481, -0.6090858578681946, 0.9103692173957825, -1.5713691711425781, 1.8002407550811768, 0.6004332900047302, 0.6391267776489258, 0.3963273763656616, -2.025444269180298, 0.6885870695114136]... [Tensor of shape torch.Size([1, 111])]\n", "base_model.linear.bias: tensor([2.3769])\n", "\n", "%==================================== Model Details ====================================%\n", "\n" ] } ], "source": [ "pya.utils.print_model_details(model)" ] }, { "cell_type": "markdown", "id": "cd69350f", "metadata": {}, "source": [ "## Basic test" ] }, { "cell_type": "code", "execution_count": 13, "id": "58893a28", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T20:25:16.664243Z", "iopub.status.busy": "2026-07-04T20:25:16.664152Z", "iopub.status.idle": "2026-07-04T20:25:16.668877Z", "shell.execute_reply": "2026-07-04T20:25:16.668505Z" } }, "outputs": [ { "data": { "text/plain": [ "tensor([[ 7.1793e+01],\n", " [ 3.4194e+00],\n", " [-8.3333e-02],\n", " [ 2.7208e-01],\n", " [-8.3328e-02],\n", " [ 4.4315e+01],\n", " [ 1.1845e+02],\n", " [-8.2270e-02],\n", " [-8.3325e-02],\n", " [ 2.5722e+01]], 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": "c6056a6d", "metadata": {}, "source": [ "## Save torch model" ] }, { "cell_type": "code", "execution_count": 14, "id": "6ef6fad9", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T20:25:16.670245Z", "iopub.status.busy": "2026-07-04T20:25:16.670166Z", "iopub.status.idle": "2026-07-04T20:25:16.673302Z", "shell.execute_reply": "2026-07-04T20:25:16.672898Z" } }, "outputs": [], "source": [ "torch.save(model, f\"../weights/{model.metadata['clock_name']}.pt\")" ] }, { "cell_type": "markdown", "id": "844dc2b1", "metadata": {}, "source": [ "## Clear directory\n", "" ] }, { "cell_type": "code", "execution_count": 15, "id": "b01cfca1", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T20:25:16.674654Z", "iopub.status.busy": "2026-07-04T20:25:16.674579Z", "iopub.status.idle": "2026-07-04T20:25:16.677995Z", "shell.execute_reply": "2026-07-04T20:25:16.677656Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Deleted file: wu.xlsx\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 }