{ "cells": [ { "cell_type": "markdown", "id": "1c464267", "metadata": {}, "source": [ "# SenCultureAge" ] }, { "cell_type": "markdown", "id": "c7c79478", "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": "75628fcc", "metadata": {}, "source": [ "Let's first import some packages:" ] }, { "cell_type": "code", "execution_count": 1, "id": "24ab3e42", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:46:07.313415Z", "iopub.status.busy": "2026-07-04T19:46:07.313237Z", "iopub.status.idle": "2026-07-04T19:46:09.238799Z", "shell.execute_reply": "2026-07-04T19:46:09.238380Z" } }, "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": "bd6ccee5", "metadata": {}, "source": [ "## Instantiate model class" ] }, { "cell_type": "code", "execution_count": 2, "id": "e4bf5b46", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:46:09.240448Z", "iopub.status.busy": "2026-07-04T19:46:09.240267Z", "iopub.status.idle": "2026-07-04T19:46:09.242826Z", "shell.execute_reply": "2026-07-04T19:46:09.242516Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "class SenCultureAge(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.SenCultureAge)" ] }, { "cell_type": "code", "execution_count": 3, "id": "44305393", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:46:09.244027Z", "iopub.status.busy": "2026-07-04T19:46:09.243948Z", "iopub.status.idle": "2026-07-04T19:46:09.245526Z", "shell.execute_reply": "2026-07-04T19:46:09.245180Z" } }, "outputs": [], "source": [ "model = pya.models.SenCultureAge()" ] }, { "cell_type": "markdown", "id": "3ee9fcee", "metadata": {}, "source": [ "## Define clock metadata" ] }, { "cell_type": "code", "execution_count": 4, "id": "ea26bb21", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:46:09.246877Z", "iopub.status.busy": "2026-07-04T19:46:09.246810Z", "iopub.status.idle": "2026-07-04T19:46:09.248773Z", "shell.execute_reply": "2026-07-04T19:46:09.248402Z" } }, "outputs": [], "source": [ "model.metadata[\"clock_name\"] = \"sencultureage\"\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\"] = \"Binomial elastic-net classifier of in-vitro cellular senescence, trained after ComBat correction on pooled human fibroblast and mesenchymal-stromal-cell datasets and restricted to direction-concordant senescence/age/mortality CpGs.\"\n", "model.metadata[\"research_only\"] = None\n", "model.metadata[\"tissue\"] = [\"cultured fibroblasts\", \"cultured mesenchymal stromal cells\"] # Paper: Training pooled fibroblasts, diploid skin fibroblasts, bone-marrow MSCs and adipose-derived MSCs.\n", "model.metadata[\"predicts\"] = [\"cellular senescence\"] # Paper: SenCultureAge is a binary senescence predictor.\n", "model.metadata[\"training_target\"] = [\"cellular senescence\"] # Paper: Each in-vitro training sample was labeled senescent or control.\n", "model.metadata[\"unit\"] = [\"log odds\"] # Paper: Pyaging applies the binomial-model coefficients without an inverse-logit postprocess.\n", "model.metadata[\"model_type\"] = \"elastic net logistic regression\" # Paper: The model used glmnet with family binomial and alpha 0.5.\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\"] = \"human cell cultures\" # Paper: Training pooled GSE197723 and GSE227160 senescent and control cultures.\n", "model.metadata[\"journal\"] = \"Aging Cell\"\n", "model.metadata[\"last_author\"] = \"Albert T. Higgins-Chen\"\n", "model.metadata[\"n_features\"] = 142\n", "model.metadata[\"citations\"] = 0\n", "model.metadata[\"citations_date\"] = \"2026-07-05\"\n" ] }, { "cell_type": "markdown", "id": "77896724", "metadata": {}, "source": [ "## Download clock dependencies" ] }, { "cell_type": "code", "execution_count": 5, "id": "58754fb5", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:46:09.249946Z", "iopub.status.busy": "2026-07-04T19:46:09.249880Z", "iopub.status.idle": "2026-07-04T19:46:09.527232Z", "shell.execute_reply": "2026-07-04T19:46:09.526594Z" } }, "outputs": [ { "data": { "text/plain": [ "0" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "os.system(f\"curl -sL -o SenCultureAge_CpGs.csv https://raw.githubusercontent.com/HigginsChenLab/methylCIPHER/19b12296b0d7eb7055a97d068064df635f44ce3e/data-raw/SenescenceAge/SenCultureAge_CpGs.csv\")" ] }, { "cell_type": "markdown", "id": "35686c23", "metadata": {}, "source": [ "## Load features" ] }, { "cell_type": "code", "execution_count": 6, "id": "0d28c823", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:46:09.530404Z", "iopub.status.busy": "2026-07-04T19:46:09.530171Z", "iopub.status.idle": "2026-07-04T19:46:09.536075Z", "shell.execute_reply": "2026-07-04T19:46:09.535155Z" } }, "outputs": [], "source": [ "coef_df = pd.read_csv('SenCultureAge_CpGs.csv')\n", "model.features = coef_df['CpG'].tolist()" ] }, { "cell_type": "markdown", "id": "808175d6", "metadata": {}, "source": [ "## Load weights into base model" ] }, { "cell_type": "code", "execution_count": 7, "id": "e1ef3e63", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:46:09.538283Z", "iopub.status.busy": "2026-07-04T19:46:09.538088Z", "iopub.status.idle": "2026-07-04T19:46:09.541471Z", "shell.execute_reply": "2026-07-04T19:46:09.540707Z" } }, "outputs": [], "source": [ "weights = torch.tensor(coef_df['Coefficient'].tolist()).unsqueeze(0).float()\n", "intercept = torch.tensor([-254.6817]).float()" ] }, { "cell_type": "code", "execution_count": 8, "id": "8a18febb", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:46:09.543328Z", "iopub.status.busy": "2026-07-04T19:46:09.543200Z", "iopub.status.idle": "2026-07-04T19:46:09.546261Z", "shell.execute_reply": "2026-07-04T19:46:09.545741Z" } }, "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": "3375f499", "metadata": {}, "source": [ "## Load reference values" ] }, { "cell_type": "code", "execution_count": 9, "id": "525ac21b", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:46:09.548401Z", "iopub.status.busy": "2026-07-04T19:46:09.548265Z", "iopub.status.idle": "2026-07-04T19:46:09.550469Z", "shell.execute_reply": "2026-07-04T19:46:09.549937Z" } }, "outputs": [], "source": [ "model.reference_values = None" ] }, { "cell_type": "markdown", "id": "ef6dd201", "metadata": {}, "source": [ "## Load preprocess and postprocess objects" ] }, { "cell_type": "code", "execution_count": 10, "id": "231dba63", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:46:09.551971Z", "iopub.status.busy": "2026-07-04T19:46:09.551856Z", "iopub.status.idle": "2026-07-04T19:46:09.553797Z", "shell.execute_reply": "2026-07-04T19:46:09.553277Z" } }, "outputs": [], "source": [ "model.preprocess_name = None\n", "model.preprocess_dependencies = None" ] }, { "cell_type": "code", "execution_count": 11, "id": "76517666", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:46:09.555327Z", "iopub.status.busy": "2026-07-04T19:46:09.555209Z", "iopub.status.idle": "2026-07-04T19:46:09.556946Z", "shell.execute_reply": "2026-07-04T19:46:09.556543Z" } }, "outputs": [], "source": [ "model.postprocess_name = None\n", "model.postprocess_dependencies = None" ] }, { "cell_type": "markdown", "id": "8f6a44cb", "metadata": {}, "source": [ "## Check all clock parameters" ] }, { "cell_type": "code", "execution_count": 12, "id": "1b6dda5e", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:46:09.558677Z", "iopub.status.busy": "2026-07-04T19:46:09.558565Z", "iopub.status.idle": "2026-07-04T19:46:09.562407Z", "shell.execute_reply": "2026-07-04T19:46:09.561888Z" } }, "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': 'sencultureage',\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: ['cg00127591', 'cg00296189', 'cg00518989', 'cg00666978', 'cg00899883', 'cg01587390', 'cg01779934', 'cg01788025', 'cg02389682', 'cg02484633', 'cg03063057', 'cg03917020', 'cg04081392', 'cg04224041', 'cg04514998', 'cg04796775', 'cg04842828', 'cg05076775', 'cg05198969', 'cg05275153', 'cg06955158', 'cg07166216', 'cg07381973', 'cg07390459', 'cg07528772', 'cg07894586', 'cg07959138', 'cg08155338', 'cg08190044', 'cg08600218']... [Total elements: 142]\n", "base_model_features: None\n", "\n", "%==================================== Model Details ====================================%\n", "Model Structure:\n", "\n", "base_model: LinearModel(\n", " (linear): Linear(in_features=142, out_features=1, bias=True)\n", ")\n", "\n", "%==================================== Model Details ====================================%\n", "Model Parameters and Weights:\n", "\n", "base_model.linear.weight: [7.832161903381348, 12.214286804199219, 8.027161598205566, 45.58428192138672, 2.2128779888153076, 3.30290150642395, 3.9877500534057617, 0.7730445265769958, 6.563979625701904, 25.338594436645508, 0.7351537942886353, 2.5286097526550293, 2.230402708053589, 0.2821439504623413, 2.5589730739593506, 9.428633689880371, 21.527307510375977, 35.500648498535156, 2.8819007873535156, 3.544675350189209, 6.508420467376709, 6.3784918785095215, 3.5156803131103516, 14.325957298278809, 1.9541112184524536, -0.49621376395225525, 0.5369631052017212, 8.035040855407715, 10.24377727508545, 0.48844996094703674]... [Tensor of shape torch.Size([1, 142])]\n", "base_model.linear.bias: tensor([-254.6817])\n", "\n", "%==================================== Model Details ====================================%\n", "\n" ] } ], "source": [ "pya.utils.print_model_details(model)" ] }, { "cell_type": "markdown", "id": "1424e10e", "metadata": {}, "source": [ "## Basic test" ] }, { "cell_type": "code", "execution_count": 13, "id": "54ed8eef", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:46:09.563990Z", "iopub.status.busy": "2026-07-04T19:46:09.563874Z", "iopub.status.idle": "2026-07-04T19:46:09.568794Z", "shell.execute_reply": "2026-07-04T19:46:09.568421Z" } }, "outputs": [ { "data": { "text/plain": [ "tensor([[ 77.5496],\n", " [-329.8605],\n", " [-197.1059],\n", " [-396.4821],\n", " [-519.2351],\n", " [-122.0558],\n", " [-104.6654],\n", " [-262.1782],\n", " [-182.1105],\n", " [-254.5931]], 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": "6ab494dd", "metadata": {}, "source": [ "## Save torch model" ] }, { "cell_type": "code", "execution_count": 14, "id": "7b68dbde", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:46:09.570393Z", "iopub.status.busy": "2026-07-04T19:46:09.570291Z", "iopub.status.idle": "2026-07-04T19:46:09.573538Z", "shell.execute_reply": "2026-07-04T19:46:09.573128Z" } }, "outputs": [], "source": [ "torch.save(model, f\"../weights/{model.metadata['clock_name']}.pt\")" ] }, { "cell_type": "markdown", "id": "1e7788c2", "metadata": {}, "source": [ "## Clear directory\n", "" ] }, { "cell_type": "code", "execution_count": 15, "id": "4dc958f9", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:46:09.574851Z", "iopub.status.busy": "2026-07-04T19:46:09.574769Z", "iopub.status.idle": "2026-07-04T19:46:09.578382Z", "shell.execute_reply": "2026-07-04T19:46:09.577941Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Deleted file: SenCultureAge_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 }