{ "cells": [ { "cell_type": "markdown", "id": "fc18810b", "metadata": {}, "source": [ "# Hep" ] }, { "cell_type": "markdown", "id": "bcf9dcec", "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": "d3d7ef81", "metadata": {}, "source": [ "Let's first import some packages:" ] }, { "cell_type": "code", "execution_count": 1, "id": "4689ddbd", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:34:24.666670Z", "iopub.status.busy": "2026-07-04T19:34:24.666492Z", "iopub.status.idle": "2026-07-04T19:34:26.832242Z", "shell.execute_reply": "2026-07-04T19:34:26.831704Z" } }, "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": "b794a094", "metadata": {}, "source": [ "## Instantiate model class" ] }, { "cell_type": "code", "execution_count": 2, "id": "19d052eb", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:34:26.834058Z", "iopub.status.busy": "2026-07-04T19:34:26.833839Z", "iopub.status.idle": "2026-07-04T19:34:26.836908Z", "shell.execute_reply": "2026-07-04T19:34:26.836438Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "class Hep(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.Hep)" ] }, { "cell_type": "code", "execution_count": 3, "id": "669a6c89", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:34:26.838145Z", "iopub.status.busy": "2026-07-04T19:34:26.838060Z", "iopub.status.idle": "2026-07-04T19:34:26.840052Z", "shell.execute_reply": "2026-07-04T19:34:26.839531Z" } }, "outputs": [], "source": [ "model = pya.models.Hep()" ] }, { "cell_type": "markdown", "id": "928c43e4", "metadata": {}, "source": [ "## Define clock metadata" ] }, { "cell_type": "code", "execution_count": 4, "id": "cbfd0448", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:34:26.841248Z", "iopub.status.busy": "2026-07-04T19:34:26.841173Z", "iopub.status.idle": "2026-07-04T19:34:26.843304Z", "shell.execute_reply": "2026-07-04T19:34:26.842938Z" } }, "outputs": [], "source": [ "model.metadata[\"clock_name\"] = \"hep\"\n", "model.metadata[\"data_type\"] = \"DNA methylation\" # Paper: The model is based on DNA methylation measurements.\n", "model.metadata[\"species\"] = \"Homo sapiens\" # Paper: The study samples are Homo sapiens.\n", "model.metadata[\"year\"] = 2024\n", "model.metadata[\"approved_by_author\"] = \"⌛\"\n", "model.metadata[\"citation\"] = \"Tong, Huige, et al. \\\"Cell-type specific epigenetic clocks to quantify biological age at cell-type resolution.\\\" Aging 16 (2024): 14204–14237.\"\n", "model.metadata[\"doi\"] = \"https://doi.org/10.18632/aging.206184\"\n", "model.metadata[\"notes\"] = \"HepClock is a hepatocyte-specific chronological-age clock trained by lasso on hepatocyte age-DMCTs identified with CellDMC after estimating five liver cell fractions.\"\n", "model.metadata[\"research_only\"] = None\n", "model.metadata[\"tissue\"] = [\"liver\"] # Paper: The listed tissue is the model-development sample material.\n", "model.metadata[\"predicts\"] = [\"chronological age\"] # Paper: The reported predictor output is chronological age.\n", "model.metadata[\"training_target\"] = [\"chronological age\"] # Paper: The fitting outcome is chronological age.\n", "model.metadata[\"unit\"] = [\"years\"] # Paper: The returned construct is expressed as years.\n", "model.metadata[\"model_type\"] = \"LASSO regression\" # Paper: The clock was fitted using LASSO.\n", "model.metadata[\"platform\"] = [\"Illumina EPIC\"] # Paper: Training/selection used Illumina EPIC.\n", "model.metadata[\"population\"] = \"adults\" # Paper: adults aged 18–75 years (210 normal liver samples)\n", "model.metadata[\"journal\"] = \"Aging\"\n", "model.metadata[\"last_author\"] = \"Andrew E. Teschendorff\"\n", "model.metadata[\"n_features\"] = 70\n", "model.metadata[\"citations\"] = 25\n", "model.metadata[\"citations_date\"] = \"2026-07-05\"\n" ] }, { "cell_type": "markdown", "id": "1c4e05d5", "metadata": {}, "source": [ "## Download clock dependencies" ] }, { "cell_type": "code", "execution_count": 5, "id": "9f1e4bf0", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:34:26.844513Z", "iopub.status.busy": "2026-07-04T19:34:26.844448Z", "iopub.status.idle": "2026-07-04T19:34:27.149115Z", "shell.execute_reply": "2026-07-04T19:34:27.148384Z" } }, "outputs": [ { "data": { "text/plain": [ "0" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "supplementary_url = \"https://raw.githubusercontent.com/Duzhaozhen/OmniAge/c10fbe8cb92957520fbff1d55ae1def0691252e5/OmniAgePy/src/omniage/data/CTS/Hep.csv\"\n", "supplementary_file_name = \"coefficients.csv\"\n", "os.system(f\"curl -sL -o {supplementary_file_name} {supplementary_url}\")" ] }, { "cell_type": "markdown", "id": "d0d52a00", "metadata": {}, "source": [ "## Load features" ] }, { "cell_type": "code", "execution_count": 6, "id": "2b87ea26", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:34:27.150502Z", "iopub.status.busy": "2026-07-04T19:34:27.150388Z", "iopub.status.idle": "2026-07-04T19:34:27.155515Z", "shell.execute_reply": "2026-07-04T19:34:27.154921Z" } }, "outputs": [], "source": [ "df = pd.read_csv('coefficients.csv')\n", "if str(df.columns[0]).startswith('Unnamed'):\n", " df = df.iloc[:, 1:]\n", "mask = df['probe'].astype(str).str.lower().isin(['intercept', '(intercept)'])\n", "intercept_value = float(df.loc[mask, 'coef'].iloc[0]) if mask.any() else 0.0\n", "coef_df = df.loc[~mask].reset_index(drop=True)\n", "model.features = coef_df['probe'].tolist()" ] }, { "cell_type": "markdown", "id": "d47389c4", "metadata": {}, "source": [ "## Load weights into base model" ] }, { "cell_type": "code", "execution_count": 7, "id": "85236875", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:34:27.158514Z", "iopub.status.busy": "2026-07-04T19:34:27.158357Z", "iopub.status.idle": "2026-07-04T19:34:27.160978Z", "shell.execute_reply": "2026-07-04T19:34:27.160492Z" } }, "outputs": [], "source": [ "weights = torch.tensor(coef_df['coef'].tolist()).unsqueeze(0).float()\n", "intercept = torch.tensor([intercept_value]).float()" ] }, { "cell_type": "code", "execution_count": 8, "id": "5379658c", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:34:27.162229Z", "iopub.status.busy": "2026-07-04T19:34:27.162119Z", "iopub.status.idle": "2026-07-04T19:34:27.164327Z", "shell.execute_reply": "2026-07-04T19:34:27.163895Z" } }, "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": "c4316823", "metadata": {}, "source": [ "## Load reference values" ] }, { "cell_type": "code", "execution_count": 9, "id": "459b363b", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:34:27.165762Z", "iopub.status.busy": "2026-07-04T19:34:27.165650Z", "iopub.status.idle": "2026-07-04T19:34:27.167307Z", "shell.execute_reply": "2026-07-04T19:34:27.166948Z" } }, "outputs": [], "source": [ "model.reference_values = None" ] }, { "cell_type": "markdown", "id": "290ec07c", "metadata": {}, "source": [ "## Load preprocess and postprocess objects" ] }, { "cell_type": "code", "execution_count": 10, "id": "653b9034", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:34:27.168593Z", "iopub.status.busy": "2026-07-04T19:34:27.168507Z", "iopub.status.idle": "2026-07-04T19:34:27.170761Z", "shell.execute_reply": "2026-07-04T19:34:27.170194Z" } }, "outputs": [], "source": [ "model.preprocess_name = None\n", "model.preprocess_dependencies = None" ] }, { "cell_type": "code", "execution_count": 11, "id": "cb727c9c", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:34:27.172041Z", "iopub.status.busy": "2026-07-04T19:34:27.171935Z", "iopub.status.idle": "2026-07-04T19:34:27.173792Z", "shell.execute_reply": "2026-07-04T19:34:27.173419Z" } }, "outputs": [], "source": [ "model.postprocess_name = None\n", "model.postprocess_dependencies = None" ] }, { "cell_type": "markdown", "id": "b64b0919", "metadata": {}, "source": [ "## Check all clock parameters" ] }, { "cell_type": "code", "execution_count": 12, "id": "c012f5d9", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:34:27.175219Z", "iopub.status.busy": "2026-07-04T19:34:27.175099Z", "iopub.status.idle": "2026-07-04T19:34:27.178806Z", "shell.execute_reply": "2026-07-04T19:34:27.178350Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "%==================================== Model Details ====================================%\n", "Model Attributes:\n", "\n", "training: True\n", "metadata: {'approved_by_author': '⌛',\n", " 'citation': 'Tong, Huige, et al. \"Cell-type-specific and '\n", " 'cell-type-independent DNA methylation clocks.\" Aging 16 (2024).',\n", " 'clock_name': 'hep',\n", " 'data_type': 'methylation',\n", " 'doi': 'https://doi.org/10.18632/aging.206184',\n", " 'notes': None,\n", " 'research_only': None,\n", " 'species': 'Homo sapiens',\n", " 'version': None,\n", " 'year': 2024}\n", "reference_values: None\n", "preprocess_name: None\n", "preprocess_dependencies: None\n", "postprocess_name: None\n", "postprocess_dependencies: None\n", "features: ['cg02928345', 'cg05694089', 'cg06095777', 'cg06852461', 'cg10501210', 'cg15491247', 'cg26885220', 'cg21245277', 'cg25004441', 'cg03553786', 'cg04472001', 'cg10727673', 'cg11320499', 'cg14317285', 'cg17078152', 'cg11970349', 'cg04671476', 'cg08272151', 'cg09458096', 'cg11830096', 'cg14314729', 'cg16049690', 'cg26921969', 'cg05460965', 'cg13719443', 'cg14119792', 'cg09832613', 'cg10548492', 'cg10774647', 'cg14668632']... [Total elements: 70]\n", "base_model_features: None\n", "\n", "%==================================== Model Details ====================================%\n", "Model Structure:\n", "\n", "base_model: LinearModel(\n", " (linear): Linear(in_features=70, out_features=1, bias=True)\n", ")\n", "\n", "%==================================== Model Details ====================================%\n", "Model Parameters and Weights:\n", "\n", "base_model.linear.weight: [-6.017436981201172, -5.4592413902282715, -2.176126003265381, -1.1635955572128296, -11.279679298400879, -2.4325146675109863, 0.5495437979698181, 34.79977798461914, 0.5566549301147461, 5.5801191329956055, -7.796560287475586, -7.169039249420166, 1.542575716972351, 15.734872817993164, 0.35905373096466064, 6.515843868255615, 19.468814849853516, 1.418412446975708, 0.03253895044326782, 3.5211238861083984, -0.3575636148452759, 3.376959800720215, 21.540037155151367, -7.9151692390441895, -2.348267078399658, -1.2504611015319824, -3.796130418777466, 13.8594388961792, 4.52095365524292, 0.2238227277994156]... [Tensor of shape torch.Size([1, 70])]\n", "base_model.linear.bias: tensor([16.7101])\n", "\n", "%==================================== Model Details ====================================%\n", "\n" ] } ], "source": [ "pya.utils.print_model_details(model)" ] }, { "cell_type": "markdown", "id": "feeaa7e7", "metadata": {}, "source": [ "## Basic test" ] }, { "cell_type": "code", "execution_count": 13, "id": "24319b56", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:34:27.180479Z", "iopub.status.busy": "2026-07-04T19:34:27.180367Z", "iopub.status.idle": "2026-07-04T19:34:27.185941Z", "shell.execute_reply": "2026-07-04T19:34:27.185482Z" } }, "outputs": [ { "data": { "text/plain": [ "tensor([[103.2090],\n", " [ 59.8991],\n", " [ 7.7645],\n", " [-52.8674],\n", " [115.0976],\n", " [-25.3601],\n", " [148.0905],\n", " [ 40.7066],\n", " [104.7873],\n", " [-31.2725]], 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": "4cc6e5f5", "metadata": {}, "source": [ "## Save torch model" ] }, { "cell_type": "code", "execution_count": 14, "id": "e99ad0a7", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:34:27.187214Z", "iopub.status.busy": "2026-07-04T19:34:27.187127Z", "iopub.status.idle": "2026-07-04T19:34:27.191580Z", "shell.execute_reply": "2026-07-04T19:34:27.190922Z" } }, "outputs": [], "source": [ "torch.save(model, f\"../weights/{model.metadata['clock_name']}.pt\")" ] }, { "cell_type": "markdown", "id": "115afba3", "metadata": {}, "source": [ "## Clear directory\n", "" ] }, { "cell_type": "code", "execution_count": 15, "id": "8af87b0e", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:34:27.192862Z", "iopub.status.busy": "2026-07-04T19:34:27.192771Z", "iopub.status.idle": "2026-07-04T19:34:27.196390Z", "shell.execute_reply": "2026-07-04T19:34:27.195986Z" } }, "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 }