{ "cells": [ { "cell_type": "markdown", "id": "45dd50f8", "metadata": {}, "source": [ "# CompIL6" ] }, { "cell_type": "markdown", "id": "6df251e6", "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": "2a87a779", "metadata": {}, "source": [ "Let's first import some packages:" ] }, { "cell_type": "code", "execution_count": 1, "id": "e186a2b1", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:34:11.018016Z", "iopub.status.busy": "2026-07-04T19:34:11.017767Z", "iopub.status.idle": "2026-07-04T19:34:14.056505Z", "shell.execute_reply": "2026-07-04T19:34:14.056083Z" } }, "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": "7a6f12f2", "metadata": {}, "source": [ "## Instantiate model class" ] }, { "cell_type": "code", "execution_count": 2, "id": "ab2e35f1", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:34:14.058255Z", "iopub.status.busy": "2026-07-04T19:34:14.058068Z", "iopub.status.idle": "2026-07-04T19:34:14.060929Z", "shell.execute_reply": "2026-07-04T19:34:14.060519Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "class CompIL6(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.CompIL6)" ] }, { "cell_type": "code", "execution_count": 3, "id": "d8b1b2e2", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:34:14.062015Z", "iopub.status.busy": "2026-07-04T19:34:14.061930Z", "iopub.status.idle": "2026-07-04T19:34:14.063652Z", "shell.execute_reply": "2026-07-04T19:34:14.063282Z" } }, "outputs": [], "source": [ "model = pya.models.CompIL6()" ] }, { "cell_type": "markdown", "id": "24ddaba4", "metadata": {}, "source": [ "## Define clock metadata" ] }, { "cell_type": "code", "execution_count": 4, "id": "a5c9683f", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:34:14.064840Z", "iopub.status.busy": "2026-07-04T19:34:14.064770Z", "iopub.status.idle": "2026-07-04T19:34:14.066911Z", "shell.execute_reply": "2026-07-04T19:34:14.066494Z" } }, "outputs": [], "source": [ "model.metadata[\"clock_name\"] = \"compil6\"\n", "model.metadata[\"data_type\"] = \"DNA methylation\" # Paper: A DNA-methylation predictor of IL-6 was developed from CpG methylation levels.\n", "model.metadata[\"species\"] = \"Homo sapiens\" # Paper: The training cohort comprised Lothian Birth Cohort 1936 participants.\n", "model.metadata[\"year\"] = 2021\n", "model.metadata[\"approved_by_author\"] = \"⌛\"\n", "model.metadata[\"citation\"] = \"Stevenson, A. J., et al. “Creating and Validating a DNA Methylation-Based Proxy for Interleukin-6.” The Journals of Gerontology: Series A 76(12): 2284–2292 (2021).\"\n", "model.metadata[\"doi\"] = \"https://doi.org/10.1093/gerona/glab046\"\n", "model.metadata[\"notes\"] = \"Thirty-five-CpG whole-blood proxy score for persistent IL-6-related inflammatory burden, fitted against covariate-adjusted normalized plasma IL-6.\"\n", "model.metadata[\"research_only\"] = None\n", "model.metadata[\"tissue\"] = [\"whole blood\"] # Paper: DNA extracted from whole blood supplied the methylation predictors.\n", "model.metadata[\"predicts\"] = [\"interleukin-6\"] # Paper: The 35-CpG weighted sum was generated as a DNAm proxy for IL-6.\n", "model.metadata[\"training_target\"] = [\"interleukin-6\"] # Paper: IL-6 NPX was inverse normalized and adjusted for age, sex, genetic principal components, and Olink plate; standardized residuals entered penalized regression.\n", "model.metadata[\"unit\"] = [\"unitless\"] # Paper: The proxy is computed by multiplying beta values by coefficients and summing into one score.\n", "model.metadata[\"model_type\"] = \"elastic net regression\" # Paper: The elastic net model used glmnet with 12-fold cross-validation and alpha 0.5.\n", "model.metadata[\"platform\"] = [\"Illumina 450K\"] # Paper: Training whole-blood DNA was analyzed on the Illumina 450K BeadChip.\n", "model.metadata[\"population\"] = \"older adults\" # Paper: The predictor was developed in 875 LBC1936 older adults with mean age 70.\n", "model.metadata[\"journal\"] = \"The Journals of Gerontology: Series A\"\n", "model.metadata[\"last_author\"] = \"Riccardo E. Marioni\"\n", "model.metadata[\"n_features\"] = 35\n", "model.metadata[\"citations\"] = 55\n", "model.metadata[\"citations_date\"] = \"2026-07-05\"\n" ] }, { "cell_type": "markdown", "id": "8e694012", "metadata": {}, "source": [ "## Download clock dependencies" ] }, { "cell_type": "code", "execution_count": 5, "id": "89bbc4f4", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:34:14.068166Z", "iopub.status.busy": "2026-07-04T19:34:14.068091Z", "iopub.status.idle": "2026-07-04T19:34:14.321523Z", "shell.execute_reply": "2026-07-04T19:34:14.320849Z" } }, "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/CompIL6.csv\"\n", "supplementary_file_name = \"coefficients.csv\"\n", "os.system(f\"curl -sL -o {supplementary_file_name} {supplementary_url}\")" ] }, { "cell_type": "markdown", "id": "55f18405", "metadata": {}, "source": [ "## Load features" ] }, { "cell_type": "code", "execution_count": 6, "id": "b58c5283", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:34:14.323210Z", "iopub.status.busy": "2026-07-04T19:34:14.323083Z", "iopub.status.idle": "2026-07-04T19:34:14.332127Z", "shell.execute_reply": "2026-07-04T19:34:14.331594Z" } }, "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": "3aeba73f", "metadata": {}, "source": [ "## Load weights into base model" ] }, { "cell_type": "code", "execution_count": 7, "id": "c7ced5d7", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:34:14.333838Z", "iopub.status.busy": "2026-07-04T19:34:14.333709Z", "iopub.status.idle": "2026-07-04T19:34:14.340415Z", "shell.execute_reply": "2026-07-04T19:34:14.340057Z" } }, "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": "865a0a8b", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:34:14.342025Z", "iopub.status.busy": "2026-07-04T19:34:14.341944Z", "iopub.status.idle": "2026-07-04T19:34:14.347150Z", "shell.execute_reply": "2026-07-04T19:34:14.346791Z" } }, "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": "2f4a9c28", "metadata": {}, "source": [ "## Load reference values" ] }, { "cell_type": "code", "execution_count": 9, "id": "13c91d76", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:34:14.348491Z", "iopub.status.busy": "2026-07-04T19:34:14.348416Z", "iopub.status.idle": "2026-07-04T19:34:14.350239Z", "shell.execute_reply": "2026-07-04T19:34:14.349790Z" } }, "outputs": [], "source": [ "model.reference_values = None" ] }, { "cell_type": "markdown", "id": "1d094df1", "metadata": {}, "source": [ "## Load preprocess and postprocess objects" ] }, { "cell_type": "code", "execution_count": 10, "id": "35a710fa", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:34:14.351513Z", "iopub.status.busy": "2026-07-04T19:34:14.351434Z", "iopub.status.idle": "2026-07-04T19:34:14.353262Z", "shell.execute_reply": "2026-07-04T19:34:14.352840Z" } }, "outputs": [], "source": [ "model.preprocess_name = None\n", "model.preprocess_dependencies = None" ] }, { "cell_type": "code", "execution_count": 11, "id": "c2885a53", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:34:14.354410Z", "iopub.status.busy": "2026-07-04T19:34:14.354339Z", "iopub.status.idle": "2026-07-04T19:34:14.356028Z", "shell.execute_reply": "2026-07-04T19:34:14.355627Z" } }, "outputs": [], "source": [ "model.postprocess_name = None\n", "model.postprocess_dependencies = None" ] }, { "cell_type": "markdown", "id": "e03bd9ff", "metadata": {}, "source": [ "## Check all clock parameters" ] }, { "cell_type": "code", "execution_count": 12, "id": "bbfcc7aa", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:34:14.357213Z", "iopub.status.busy": "2026-07-04T19:34:14.357146Z", "iopub.status.idle": "2026-07-04T19:34:14.370170Z", "shell.execute_reply": "2026-07-04T19:34:14.369691Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "%==================================== Model Details ====================================%\n", "Model Attributes:\n", "\n", "training: True\n", "metadata: {'approved_by_author': '⌛',\n", " 'citation': 'Stevenson, Anna J., et al. \"Creating and validating a DNA '\n", " 'methylation-based proxy for interleukin-6.\" The Journals of '\n", " 'Gerontology: Series A 76.12 (2021): 2284-2292.',\n", " 'clock_name': 'compil6',\n", " 'data_type': 'methylation',\n", " 'doi': 'https://doi.org/10.1093/gerona/glab046',\n", " 'notes': None,\n", " 'research_only': None,\n", " 'species': 'Homo sapiens',\n", " 'version': None,\n", " 'year': 2021}\n", "reference_values: None\n", "preprocess_name: None\n", "preprocess_dependencies: None\n", "postprocess_name: None\n", "postprocess_dependencies: None\n", "features: ['cg26144437', 'cg03076319', 'cg05575921', 'cg21368161', 'cg04642923', 'cg21123519', 'cg04583842', 'cg16357224', 'cg24455236', 'cg25323809', 'cg03885055', 'cg17412005', 'cg19638572', 'cg04921989', 'cg14965639', 'cg04381957', 'cg20789595', 'cg24935598', 'cg26230601', 'cg16508480', 'cg12503394', 'cg18925601', 'cg19584649', 'cg03245734', 'cg04366687', 'cg14044707', 'cg25250132', 'cg04468741', 'cg23729763', 'cg10195814']... [Total elements: 35]\n", "base_model_features: None\n", "\n", "%==================================== Model Details ====================================%\n", "Model Structure:\n", "\n", "base_model: LinearModel(\n", " (linear): Linear(in_features=35, out_features=1, bias=True)\n", ")\n", "\n", "%==================================== Model Details ====================================%\n", "Model Parameters and Weights:\n", "\n", "base_model.linear.weight: [0.05400000140070915, 0.3799999952316284, -0.14000000059604645, 0.11999999731779099, 0.25, 0.004999999888241291, 0.00019999999494757503, 0.003599999938160181, 0.06400000303983688, 0.1899999976158142, -0.44999998807907104, -0.08799999952316284, 0.36000001430511475, 0.0860000029206276, -0.10999999940395355, -0.01899999938905239, -0.3100000023841858, 0.09099999815225601, -0.10999999940395355, 0.028999999165534973, 0.0010999999940395355, -0.017999999225139618, 0.08100000023841858, 0.020999999716877937, 0.0820000022649765, 0.007499999832361937, 0.23999999463558197, 0.02500000037252903, 0.10000000149011612, 0.06300000101327896]... [Tensor of shape torch.Size([1, 35])]\n", "base_model.linear.bias: tensor([0.])\n", "\n", "%==================================== Model Details ====================================%\n", "\n" ] } ], "source": [ "pya.utils.print_model_details(model)" ] }, { "cell_type": "markdown", "id": "2c60f25e", "metadata": {}, "source": [ "## Basic test" ] }, { "cell_type": "code", "execution_count": 13, "id": "a4306b82", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:34:14.371479Z", "iopub.status.busy": "2026-07-04T19:34:14.371392Z", "iopub.status.idle": "2026-07-04T19:34:14.386277Z", "shell.execute_reply": "2026-07-04T19:34:14.385777Z" } }, "outputs": [ { "data": { "text/plain": [ "tensor([[-1.3984],\n", " [ 0.5055],\n", " [-1.2893],\n", " [ 0.7687],\n", " [ 1.4874],\n", " [ 1.2169],\n", " [-0.0179],\n", " [ 0.4686],\n", " [ 1.8998],\n", " [-1.8617]], 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": "89025fd8", "metadata": {}, "source": [ "## Save torch model" ] }, { "cell_type": "code", "execution_count": 14, "id": "d45fc9cb", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:34:14.387585Z", "iopub.status.busy": "2026-07-04T19:34:14.387487Z", "iopub.status.idle": "2026-07-04T19:34:14.392445Z", "shell.execute_reply": "2026-07-04T19:34:14.392006Z" } }, "outputs": [], "source": [ "torch.save(model, f\"../weights/{model.metadata['clock_name']}.pt\")" ] }, { "cell_type": "markdown", "id": "69f9096f", "metadata": {}, "source": [ "## Clear directory\n", "" ] }, { "cell_type": "code", "execution_count": 15, "id": "160a3ba7", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:34:14.393713Z", "iopub.status.busy": "2026-07-04T19:34:14.393641Z", "iopub.status.idle": "2026-07-04T19:34:14.397496Z", "shell.execute_reply": "2026-07-04T19:34:14.397024Z" } }, "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 }