{ "cells": [ { "cell_type": "markdown", "id": "9e72fe82", "metadata": {}, "source": [ "# SenMortalityAge" ] }, { "cell_type": "markdown", "id": "4c38243a", "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": "0fc4495f", "metadata": {}, "source": [ "Let's first import some packages:" ] }, { "cell_type": "code", "execution_count": 1, "id": "0c8b1d1c", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:46:11.502413Z", "iopub.status.busy": "2026-07-04T19:46:11.502320Z", "iopub.status.idle": "2026-07-04T19:46:13.721725Z", "shell.execute_reply": "2026-07-04T19:46:13.721159Z" } }, "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": "1658388d", "metadata": {}, "source": [ "## Instantiate model class" ] }, { "cell_type": "code", "execution_count": 2, "id": "788f3fe0", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:46:13.723500Z", "iopub.status.busy": "2026-07-04T19:46:13.723271Z", "iopub.status.idle": "2026-07-04T19:46:13.726166Z", "shell.execute_reply": "2026-07-04T19:46:13.725805Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "class SenMortalityAge(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.SenMortalityAge)" ] }, { "cell_type": "code", "execution_count": 3, "id": "a3f638d8", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:46:13.727415Z", "iopub.status.busy": "2026-07-04T19:46:13.727326Z", "iopub.status.idle": "2026-07-04T19:46:13.728917Z", "shell.execute_reply": "2026-07-04T19:46:13.728631Z" } }, "outputs": [], "source": [ "model = pya.models.SenMortalityAge()" ] }, { "cell_type": "markdown", "id": "6d182a40", "metadata": {}, "source": [ "## Define clock metadata" ] }, { "cell_type": "code", "execution_count": 4, "id": "7c190867", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:46:13.730569Z", "iopub.status.busy": "2026-07-04T19:46:13.730445Z", "iopub.status.idle": "2026-07-04T19:46:13.732822Z", "shell.execute_reply": "2026-07-04T19:46:13.732421Z" } }, "outputs": [], "source": [ "model.metadata[\"clock_name\"] = \"senmortalityage\"\n", "model.metadata[\"data_type\"] = \"DNA methylation\" # Paper: The predictors use CpG DNA-methylation beta values.\n", "model.metadata[\"species\"] = \"Homo sapiens\" # Paper: All three assigned predictors were developed from human cell or human whole-blood datasets.\n", "model.metadata[\"year\"] = 2026\n", "model.metadata[\"approved_by_author\"] = \"⌛\"\n", "model.metadata[\"citation\"] = \"Kasamoto, K., Gibson, J., Moqri, M., Smith, R. & Higgins-Chen, A.T. DNA methylation signatures of cellular senescence are not reversed by senolytic treatment. Aging Cell 25, e70430 (2026).\"\n", "model.metadata[\"doi\"] = \"https://doi.org/10.1111/acel.70430\"\n", "model.metadata[\"notes\"] = \"Senescence-enriched elastic-net Cox predictor of mortality, restricted to direction-concordant senescence/age/mortality CpGs and trained in the Framingham Heart Study.\"\n", "model.metadata[\"research_only\"] = None\n", "model.metadata[\"tissue\"] = [\"whole blood\"] # Paper: FHS whole-blood methylation was used for model training.\n", "model.metadata[\"predicts\"] = [\"mortality risk\"] # Paper: SenMortalityAge is the mortality predictor among the three clocks.\n", "model.metadata[\"training_target\"] = [\"mortality\"] # Paper: Elastic-net Cox regression was fitted to time-to-death.\n", "model.metadata[\"unit\"] = [\"log hazard\"] # Paper: Pyaging returns the Cox linear predictor directly without exponentiation or another postprocess.\n", "model.metadata[\"model_type\"] = \"elastic net Cox regression\" # Paper: The mortality model used elastic-net Cox regression with cross-validated lambda.\n", "model.metadata[\"platform\"] = [\"Illumina 450K\", \"Illumina EPIC\"] # Paper: Feature discovery and predictor training used human 450K and EPIC methylation datasets.\n", "model.metadata[\"population\"] = \"adults\" # Paper: The FHS cohorts comprised 2,748 Offspring and 1,457 Third Generation participants; the model used a 70/30 split.\n", "model.metadata[\"journal\"] = \"Aging Cell\"\n", "model.metadata[\"last_author\"] = \"Albert T. Higgins-Chen\"\n", "model.metadata[\"n_features\"] = 91\n", "model.metadata[\"citations\"] = 0\n", "model.metadata[\"citations_date\"] = \"2026-07-05\"\n" ] }, { "cell_type": "markdown", "id": "81a7de29", "metadata": {}, "source": [ "## Download clock dependencies" ] }, { "cell_type": "code", "execution_count": 5, "id": "2aab6355", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:46:13.734491Z", "iopub.status.busy": "2026-07-04T19:46:13.734385Z", "iopub.status.idle": "2026-07-04T19:46:14.019130Z", "shell.execute_reply": "2026-07-04T19:46:14.018656Z" } }, "outputs": [ { "data": { "text/plain": [ "0" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "os.system(f\"curl -sL -o SenMortalityAge_CpGs.csv https://raw.githubusercontent.com/HigginsChenLab/methylCIPHER/19b12296b0d7eb7055a97d068064df635f44ce3e/data-raw/SenescenceAge/SenMortalityAge_CpGs.csv\")" ] }, { "cell_type": "markdown", "id": "7abb5d9f", "metadata": {}, "source": [ "## Load features" ] }, { "cell_type": "code", "execution_count": 6, "id": "305df9de", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:46:14.020598Z", "iopub.status.busy": "2026-07-04T19:46:14.020497Z", "iopub.status.idle": "2026-07-04T19:46:14.024178Z", "shell.execute_reply": "2026-07-04T19:46:14.023715Z" } }, "outputs": [], "source": [ "coef_df = pd.read_csv('SenMortalityAge_CpGs.csv')\n", "model.features = coef_df['CpG'].tolist()" ] }, { "cell_type": "markdown", "id": "499392c1", "metadata": {}, "source": [ "## Load weights into base model" ] }, { "cell_type": "code", "execution_count": 7, "id": "86fb7f12", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:46:14.025573Z", "iopub.status.busy": "2026-07-04T19:46:14.025491Z", "iopub.status.idle": "2026-07-04T19:46:14.027677Z", "shell.execute_reply": "2026-07-04T19:46:14.027199Z" } }, "outputs": [], "source": [ "weights = torch.tensor(coef_df['Coefficient'].tolist()).unsqueeze(0).float()\n", "intercept = torch.tensor([0.0]).float()" ] }, { "cell_type": "code", "execution_count": 8, "id": "a389a978", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:46:14.028774Z", "iopub.status.busy": "2026-07-04T19:46:14.028696Z", "iopub.status.idle": "2026-07-04T19:46:14.031095Z", "shell.execute_reply": "2026-07-04T19:46:14.030519Z" } }, "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": "654dfcb1", "metadata": {}, "source": [ "## Load reference values" ] }, { "cell_type": "code", "execution_count": 9, "id": "c7afa51d", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:46:14.032685Z", "iopub.status.busy": "2026-07-04T19:46:14.032571Z", "iopub.status.idle": "2026-07-04T19:46:14.034395Z", "shell.execute_reply": "2026-07-04T19:46:14.033938Z" } }, "outputs": [], "source": [ "model.reference_values = None" ] }, { "cell_type": "markdown", "id": "1de7e5b2", "metadata": {}, "source": [ "## Load preprocess and postprocess objects" ] }, { "cell_type": "code", "execution_count": 10, "id": "6d533085", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:46:14.035724Z", "iopub.status.busy": "2026-07-04T19:46:14.035639Z", "iopub.status.idle": "2026-07-04T19:46:14.037428Z", "shell.execute_reply": "2026-07-04T19:46:14.036896Z" } }, "outputs": [], "source": [ "model.preprocess_name = None\n", "model.preprocess_dependencies = None" ] }, { "cell_type": "code", "execution_count": 11, "id": "aa76d691", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:46:14.039206Z", "iopub.status.busy": "2026-07-04T19:46:14.039070Z", "iopub.status.idle": "2026-07-04T19:46:14.041298Z", "shell.execute_reply": "2026-07-04T19:46:14.040716Z" } }, "outputs": [], "source": [ "model.postprocess_name = None\n", "model.postprocess_dependencies = None" ] }, { "cell_type": "markdown", "id": "972fd395", "metadata": {}, "source": [ "## Check all clock parameters" ] }, { "cell_type": "code", "execution_count": 12, "id": "5fb0410a", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:46:14.042850Z", "iopub.status.busy": "2026-07-04T19:46:14.042767Z", "iopub.status.idle": "2026-07-04T19:46:14.048290Z", "shell.execute_reply": "2026-07-04T19:46:14.046482Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "%==================================== Model Details ====================================%\n", "Model Attributes:\n", "\n", "training: True\n", "metadata: {'approved_by_author': '⌛',\n", " 'citation': 'Kasamoto, Kotaro, et al. \"DNA methylation clocks for estimating '\n", " 'replicative senescence in human cells.\" Aging Cell (2026): '\n", " 'e70430.',\n", " 'clock_name': 'senmortalityage',\n", " 'data_type': 'methylation',\n", " 'doi': 'https://doi.org/10.1111/acel.70430',\n", " 'notes': None,\n", " 'research_only': None,\n", " 'species': 'Homo sapiens',\n", " 'version': None,\n", " 'year': 2026}\n", "reference_values: None\n", "preprocess_name: None\n", "preprocess_dependencies: None\n", "postprocess_name: None\n", "postprocess_dependencies: None\n", "features: ['cg00006787', 'cg00054496', 'cg00458878', 'cg00459119', 'cg03175417', 'cg03366574', 'cg04094193', 'cg04234014', 'cg05396212', 'cg06015525', 'cg06507987', 'cg06542681', 'cg07458308', 'cg08332990', 'cg08726900', 'cg08770961', 'cg10682299', 'cg11974796', 'cg12132563', 'cg12506165', 'cg13029847', 'cg13315970', 'cg13714749', 'cg14205663', 'cg15163603', 'cg16086130', 'cg16181396', 'cg16374999', 'cg16552271', 'cg17340519']... [Total elements: 91]\n", "base_model_features: None\n", "\n", "%==================================== Model Details ====================================%\n", "Model Structure:\n", "\n", "base_model: LinearModel(\n", " (linear): Linear(in_features=91, out_features=1, bias=True)\n", ")\n", "\n", "%==================================== Model Details ====================================%\n", "Model Parameters and Weights:\n", "\n", "base_model.linear.weight: [0.1905025690793991, 2.4848039150238037, 0.8850484490394592, 0.7116715908050537, 2.5755298137664795, 3.6517186164855957, 1.0995451211929321, 0.43131953477859497, 1.3369412422180176, 1.197675108909607, 0.7618207931518555, 0.18213650584220886, 3.231579303741455, 0.2401280403137207, 0.8895606994628906, 0.3462097942829132, 2.011119842529297, 0.7437288761138916, 0.5966653823852539, 0.6629067659378052, 0.5791985988616943, 1.4628826379776, 0.20292864739894867, 1.2046616077423096, -0.05603138357400894, 1.015661358833313, 0.6152376532554626, 0.8949704766273499, 0.13805358111858368, 0.4781338572502136]... [Tensor of shape torch.Size([1, 91])]\n", "base_model.linear.bias: tensor([0.])\n", "\n", "%==================================== Model Details ====================================%\n", "\n" ] } ], "source": [ "pya.utils.print_model_details(model)" ] }, { "cell_type": "markdown", "id": "f89d9d61", "metadata": {}, "source": [ "## Basic test" ] }, { "cell_type": "code", "execution_count": 13, "id": "a28245c2", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:46:14.050565Z", "iopub.status.busy": "2026-07-04T19:46:14.050406Z", "iopub.status.idle": "2026-07-04T19:46:14.055127Z", "shell.execute_reply": "2026-07-04T19:46:14.054736Z" } }, "outputs": [ { "data": { "text/plain": [ "tensor([[-32.2390],\n", " [ 10.5977],\n", " [ 19.5190],\n", " [ -3.3155],\n", " [ 12.2869],\n", " [ 26.4824],\n", " [ 3.7673],\n", " [-23.1682],\n", " [-18.8041],\n", " [ -3.3204]], 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": "91cb23c0", "metadata": {}, "source": [ "## Save torch model" ] }, { "cell_type": "code", "execution_count": 14, "id": "46a099de", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:46:14.056716Z", "iopub.status.busy": "2026-07-04T19:46:14.056614Z", "iopub.status.idle": "2026-07-04T19:46:14.059979Z", "shell.execute_reply": "2026-07-04T19:46:14.059538Z" } }, "outputs": [], "source": [ "torch.save(model, f\"../weights/{model.metadata['clock_name']}.pt\")" ] }, { "cell_type": "markdown", "id": "39acf362", "metadata": {}, "source": [ "## Clear directory\n", "" ] }, { "cell_type": "code", "execution_count": 15, "id": "e3b401c2", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:46:14.061181Z", "iopub.status.busy": "2026-07-04T19:46:14.061113Z", "iopub.status.idle": "2026-07-04T19:46:14.064477Z", "shell.execute_reply": "2026-07-04T19:46:14.064068Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Deleted file: SenMortalityAge_CpGs.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 }