{ "cells": [ { "cell_type": "markdown", "id": "57deffdc", "metadata": {}, "source": [ "# SenChronoAge" ] }, { "cell_type": "markdown", "id": "7a68ee91", "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": "8be737ca", "metadata": {}, "source": [ "Let's first import some packages:" ] }, { "cell_type": "code", "execution_count": 1, "id": "a99f7596", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:46:02.985821Z", "iopub.status.busy": "2026-07-04T19:46:02.985633Z", "iopub.status.idle": "2026-07-04T19:46:05.086019Z", "shell.execute_reply": "2026-07-04T19:46:05.085533Z" } }, "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": "0b6ff41c", "metadata": {}, "source": [ "## Instantiate model class" ] }, { "cell_type": "code", "execution_count": 2, "id": "e91f870c", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:46:05.087732Z", "iopub.status.busy": "2026-07-04T19:46:05.087566Z", "iopub.status.idle": "2026-07-04T19:46:05.090266Z", "shell.execute_reply": "2026-07-04T19:46:05.089849Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "class SenChronoAge(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.SenChronoAge)" ] }, { "cell_type": "code", "execution_count": 3, "id": "b15d83dc", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:46:05.091489Z", "iopub.status.busy": "2026-07-04T19:46:05.091388Z", "iopub.status.idle": "2026-07-04T19:46:05.093054Z", "shell.execute_reply": "2026-07-04T19:46:05.092657Z" } }, "outputs": [], "source": [ "model = pya.models.SenChronoAge()" ] }, { "cell_type": "markdown", "id": "6182e7d6", "metadata": {}, "source": [ "## Define clock metadata" ] }, { "cell_type": "code", "execution_count": 4, "id": "cc122626", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:46:05.094341Z", "iopub.status.busy": "2026-07-04T19:46:05.094250Z", "iopub.status.idle": "2026-07-04T19:46:05.096371Z", "shell.execute_reply": "2026-07-04T19:46:05.095989Z" } }, "outputs": [], "source": [ "model.metadata[\"clock_name\"] = \"senchronoage\"\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 chronological-age predictor restricted to CpGs whose directions were concordant across in-vitro senescence, age and mortality analyses.\"\n", "model.metadata[\"research_only\"] = None\n", "model.metadata[\"tissue\"] = [\"whole blood\"] # Paper: SenChronoAge was trained in GSE40279 whole blood.\n", "model.metadata[\"predicts\"] = [\"chronological age\"] # Paper: SenChronoAge is the chronological-age predictor among the three clocks.\n", "model.metadata[\"training_target\"] = [\"chronological age\"] # Paper: The model was trained against chronological age.\n", "model.metadata[\"unit\"] = [\"years\"] # Paper: The chronological-age regression output is on the age scale in years.\n", "model.metadata[\"model_type\"] = \"elastic net regression\" # Paper: SenChronoAge used glmnet elastic-net regression with ten-fold cross-validation.\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: GSE40279 contains 656 participants aged 19–101.\n", "model.metadata[\"journal\"] = \"Aging Cell\"\n", "model.metadata[\"last_author\"] = \"Albert T. Higgins-Chen\"\n", "model.metadata[\"n_features\"] = 187\n", "model.metadata[\"citations\"] = 0\n", "model.metadata[\"citations_date\"] = \"2026-07-05\"\n" ] }, { "cell_type": "markdown", "id": "1ca68404", "metadata": {}, "source": [ "## Download clock dependencies" ] }, { "cell_type": "code", "execution_count": 5, "id": "602e8cb7", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:46:05.097543Z", "iopub.status.busy": "2026-07-04T19:46:05.097477Z", "iopub.status.idle": "2026-07-04T19:46:05.380287Z", "shell.execute_reply": "2026-07-04T19:46:05.379846Z" } }, "outputs": [ { "data": { "text/plain": [ "0" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "os.system(f\"curl -sL -o SenChronoAge_CpGs.csv https://raw.githubusercontent.com/HigginsChenLab/methylCIPHER/19b12296b0d7eb7055a97d068064df635f44ce3e/data-raw/SenescenceAge/SenChronoAge_CpGs.csv\")" ] }, { "cell_type": "markdown", "id": "38c3a070", "metadata": {}, "source": [ "## Load features" ] }, { "cell_type": "code", "execution_count": 6, "id": "bb6ec3a5", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:46:05.381742Z", "iopub.status.busy": "2026-07-04T19:46:05.381629Z", "iopub.status.idle": "2026-07-04T19:46:05.386691Z", "shell.execute_reply": "2026-07-04T19:46:05.386214Z" } }, "outputs": [], "source": [ "coef_df = pd.read_csv('SenChronoAge_CpGs.csv')\n", "model.features = coef_df['CpG'].tolist()" ] }, { "cell_type": "markdown", "id": "503fc7fb", "metadata": {}, "source": [ "## Load weights into base model" ] }, { "cell_type": "code", "execution_count": 7, "id": "21d20545", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:46:05.387853Z", "iopub.status.busy": "2026-07-04T19:46:05.387779Z", "iopub.status.idle": "2026-07-04T19:46:05.389899Z", "shell.execute_reply": "2026-07-04T19:46:05.389455Z" } }, "outputs": [], "source": [ "weights = torch.tensor(coef_df['Coefficient'].tolist()).unsqueeze(0).float()\n", "intercept = torch.tensor([-80.0532]).float()" ] }, { "cell_type": "code", "execution_count": 8, "id": "5e90666e", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:46:05.391061Z", "iopub.status.busy": "2026-07-04T19:46:05.390989Z", "iopub.status.idle": "2026-07-04T19:46:05.393027Z", "shell.execute_reply": "2026-07-04T19:46:05.392667Z" } }, "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": "e2d7ca5c", "metadata": {}, "source": [ "## Load reference values" ] }, { "cell_type": "code", "execution_count": 9, "id": "0c62b584", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:46:05.394465Z", "iopub.status.busy": "2026-07-04T19:46:05.394373Z", "iopub.status.idle": "2026-07-04T19:46:05.395979Z", "shell.execute_reply": "2026-07-04T19:46:05.395661Z" } }, "outputs": [], "source": [ "model.reference_values = None" ] }, { "cell_type": "markdown", "id": "8e4ef430", "metadata": {}, "source": [ "## Load preprocess and postprocess objects" ] }, { "cell_type": "code", "execution_count": 10, "id": "d776def2", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:46:05.397178Z", "iopub.status.busy": "2026-07-04T19:46:05.397113Z", "iopub.status.idle": "2026-07-04T19:46:05.398706Z", "shell.execute_reply": "2026-07-04T19:46:05.398306Z" } }, "outputs": [], "source": [ "model.preprocess_name = None\n", "model.preprocess_dependencies = None" ] }, { "cell_type": "code", "execution_count": 11, "id": "cb2eec57", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:46:05.399897Z", "iopub.status.busy": "2026-07-04T19:46:05.399822Z", "iopub.status.idle": "2026-07-04T19:46:05.401416Z", "shell.execute_reply": "2026-07-04T19:46:05.401083Z" } }, "outputs": [], "source": [ "model.postprocess_name = None\n", "model.postprocess_dependencies = None" ] }, { "cell_type": "markdown", "id": "59da477a", "metadata": {}, "source": [ "## Check all clock parameters" ] }, { "cell_type": "code", "execution_count": 12, "id": "ac044512", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:46:05.402537Z", "iopub.status.busy": "2026-07-04T19:46:05.402461Z", "iopub.status.idle": "2026-07-04T19:46:05.405630Z", "shell.execute_reply": "2026-07-04T19:46:05.405266Z" } }, "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': 'senchronoage',\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: ['cg00210473', 'cg00254017', 'cg00296652', 'cg00602326', 'cg00922748', 'cg00964103', 'cg01013171', 'cg01079407', 'cg01141812', 'cg01196788', 'cg01249187', 'cg01263575', 'cg01302656', 'cg01654770', 'cg01675158', 'cg01769968', 'cg01987776', 'cg02716556', 'cg02796545', 'cg03649284', 'cg03660134', 'cg04094193', 'cg04270048', 'cg04432128', 'cg04503600', 'cg04597312', 'cg04840739', 'cg04912316', 'cg05530568', 'cg05818515']... [Total elements: 187]\n", "base_model_features: None\n", "\n", "%==================================== Model Details ====================================%\n", "Model Structure:\n", "\n", "base_model: LinearModel(\n", " (linear): Linear(in_features=187, out_features=1, bias=True)\n", ")\n", "\n", "%==================================== Model Details ====================================%\n", "Model Parameters and Weights:\n", "\n", "base_model.linear.weight: [9.502220153808594, -10.09235954284668, -7.13555383682251, 7.0440497398376465, 0.07385352998971939, 4.94988489151001, 3.2296602725982666, -0.23233868181705475, 13.087000846862793, 6.111741065979004, 3.240180492401123, -5.130921840667725, 5.639341354370117, 4.501803398132324, 0.6832999587059021, -0.25918036699295044, 4.322950839996338, 18.638635635375977, 3.3074159622192383, 5.428537845611572, -0.6449773907661438, 5.176876068115234, 1.967892050743103, 5.64846658706665, -0.2769940495491028, 3.7448530197143555, 7.980226039886475, 3.951530694961548, 12.726526260375977, -9.475204467773438]... [Tensor of shape torch.Size([1, 187])]\n", "base_model.linear.bias: tensor([-80.0532])\n", "\n", "%==================================== Model Details ====================================%\n", "\n" ] } ], "source": [ "pya.utils.print_model_details(model)" ] }, { "cell_type": "markdown", "id": "83683886", "metadata": {}, "source": [ "## Basic test" ] }, { "cell_type": "code", "execution_count": 13, "id": "3c8a58e0", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:46:05.406830Z", "iopub.status.busy": "2026-07-04T19:46:05.406762Z", "iopub.status.idle": "2026-07-04T19:46:05.410369Z", "shell.execute_reply": "2026-07-04T19:46:05.410090Z" } }, "outputs": [ { "data": { "text/plain": [ "tensor([[ -54.5464],\n", " [-218.2588],\n", " [ 143.5234],\n", " [-101.8496],\n", " [-296.5361],\n", " [-180.5059],\n", " [-193.5961],\n", " [-180.5310],\n", " [ -16.9680],\n", " [ 65.8968]], 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": "593b65d0", "metadata": {}, "source": [ "## Save torch model" ] }, { "cell_type": "code", "execution_count": 14, "id": "2714b647", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:46:05.411704Z", "iopub.status.busy": "2026-07-04T19:46:05.411633Z", "iopub.status.idle": "2026-07-04T19:46:05.414454Z", "shell.execute_reply": "2026-07-04T19:46:05.414157Z" } }, "outputs": [], "source": [ "torch.save(model, f\"../weights/{model.metadata['clock_name']}.pt\")" ] }, { "cell_type": "markdown", "id": "6ac2ffdc", "metadata": {}, "source": [ "## Clear directory\n", "" ] }, { "cell_type": "code", "execution_count": 15, "id": "2ab855c3", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:46:05.416110Z", "iopub.status.busy": "2026-07-04T19:46:05.416026Z", "iopub.status.idle": "2026-07-04T19:46:05.419297Z", "shell.execute_reply": "2026-07-04T19:46:05.418935Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Deleted file: SenChronoAge_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 }