{ "cells": [ { "cell_type": "markdown", "id": "e64c332b", "metadata": {}, "source": [ "# GliaSin" ] }, { "cell_type": "markdown", "id": "beac6e08", "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": "b253ebc7", "metadata": {}, "source": [ "Let's first import some packages:" ] }, { "cell_type": "code", "execution_count": 1, "id": "de93ca98", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:34:20.364487Z", "iopub.status.busy": "2026-07-04T19:34:20.364369Z", "iopub.status.idle": "2026-07-04T19:34:22.461975Z", "shell.execute_reply": "2026-07-04T19:34:22.461400Z" } }, "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": "6d2bccb5", "metadata": {}, "source": [ "## Instantiate model class" ] }, { "cell_type": "code", "execution_count": 2, "id": "087c7d05", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:34:22.463752Z", "iopub.status.busy": "2026-07-04T19:34:22.463539Z", "iopub.status.idle": "2026-07-04T19:34:22.467043Z", "shell.execute_reply": "2026-07-04T19:34:22.466550Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "class GliaSin(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.GliaSin)" ] }, { "cell_type": "code", "execution_count": 3, "id": "31c87d84", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:34:22.468263Z", "iopub.status.busy": "2026-07-04T19:34:22.468180Z", "iopub.status.idle": "2026-07-04T19:34:22.470069Z", "shell.execute_reply": "2026-07-04T19:34:22.469651Z" } }, "outputs": [], "source": [ "model = pya.models.GliaSin()" ] }, { "cell_type": "markdown", "id": "75807bc8", "metadata": {}, "source": [ "## Define clock metadata" ] }, { "cell_type": "code", "execution_count": 4, "id": "2551a4be", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:34:22.471251Z", "iopub.status.busy": "2026-07-04T19:34:22.471162Z", "iopub.status.idle": "2026-07-04T19:34:22.473223Z", "shell.execute_reply": "2026-07-04T19:34:22.472759Z" } }, "outputs": [], "source": [ "model.metadata[\"clock_name\"] = \"gliasin\"\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\"] = \"Glia-Sin is a glia semi-intrinsic chronological-age clock: elastic-net regression was restricted to glia age-DMCTs but fitted to methylation values not adjusted for brain cell fractions.\"\n", "model.metadata[\"research_only\"] = None\n", "model.metadata[\"tissue\"] = [\"brain cortex\"] # 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\"] = \"elastic net regression\" # Paper: The clock was fitted using Elastic net.\n", "model.metadata[\"platform\"] = [\"Illumina 450K\"] # Paper: Training/selection used Illumina 450K.\n", "model.metadata[\"population\"] = \"adults\" # Paper: adults aged 18–97 years (Jaffe prefrontal-cortex training data)\n", "model.metadata[\"journal\"] = \"Aging\"\n", "model.metadata[\"last_author\"] = \"Andrew E. Teschendorff\"\n", "model.metadata[\"n_features\"] = 220\n", "model.metadata[\"citations\"] = 25\n", "model.metadata[\"citations_date\"] = \"2026-07-05\"\n" ] }, { "cell_type": "markdown", "id": "04625e1c", "metadata": {}, "source": [ "## Download clock dependencies" ] }, { "cell_type": "code", "execution_count": 5, "id": "7234da5b", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:34:22.475319Z", "iopub.status.busy": "2026-07-04T19:34:22.475208Z", "iopub.status.idle": "2026-07-04T19:34:22.614231Z", "shell.execute_reply": "2026-07-04T19:34:22.613731Z" } }, "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/Glia-Sin.csv\"\n", "supplementary_file_name = \"coefficients.csv\"\n", "os.system(f\"curl -sL -o {supplementary_file_name} {supplementary_url}\")" ] }, { "cell_type": "markdown", "id": "09c6f9e8", "metadata": {}, "source": [ "## Load features" ] }, { "cell_type": "code", "execution_count": 6, "id": "6e39b70a", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:34:22.615864Z", "iopub.status.busy": "2026-07-04T19:34:22.615738Z", "iopub.status.idle": "2026-07-04T19:34:22.621398Z", "shell.execute_reply": "2026-07-04T19:34:22.620887Z" } }, "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": "1369e2ae", "metadata": {}, "source": [ "## Load weights into base model" ] }, { "cell_type": "code", "execution_count": 7, "id": "0436eee4", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:34:22.622936Z", "iopub.status.busy": "2026-07-04T19:34:22.622818Z", "iopub.status.idle": "2026-07-04T19:34:22.625269Z", "shell.execute_reply": "2026-07-04T19:34:22.624854Z" } }, "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": "98b28904", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:34:22.626458Z", "iopub.status.busy": "2026-07-04T19:34:22.626366Z", "iopub.status.idle": "2026-07-04T19:34:22.628717Z", "shell.execute_reply": "2026-07-04T19:34:22.628281Z" } }, "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": "2d247753", "metadata": {}, "source": [ "## Load reference values" ] }, { "cell_type": "code", "execution_count": 9, "id": "3a92e5b7", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:34:22.630113Z", "iopub.status.busy": "2026-07-04T19:34:22.630003Z", "iopub.status.idle": "2026-07-04T19:34:22.631616Z", "shell.execute_reply": "2026-07-04T19:34:22.631210Z" } }, "outputs": [], "source": [ "model.reference_values = None" ] }, { "cell_type": "markdown", "id": "e49be664", "metadata": {}, "source": [ "## Load preprocess and postprocess objects" ] }, { "cell_type": "code", "execution_count": 10, "id": "c5ee230c", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:34:22.632592Z", "iopub.status.busy": "2026-07-04T19:34:22.632525Z", "iopub.status.idle": "2026-07-04T19:34:22.634291Z", "shell.execute_reply": "2026-07-04T19:34:22.633844Z" } }, "outputs": [], "source": [ "model.preprocess_name = None\n", "model.preprocess_dependencies = None" ] }, { "cell_type": "code", "execution_count": 11, "id": "1ef7e07b", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:34:22.635329Z", "iopub.status.busy": "2026-07-04T19:34:22.635264Z", "iopub.status.idle": "2026-07-04T19:34:22.636805Z", "shell.execute_reply": "2026-07-04T19:34:22.636528Z" } }, "outputs": [], "source": [ "model.postprocess_name = None\n", "model.postprocess_dependencies = None" ] }, { "cell_type": "markdown", "id": "174d5b47", "metadata": {}, "source": [ "## Check all clock parameters" ] }, { "cell_type": "code", "execution_count": 12, "id": "f3037dd5", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:34:22.638168Z", "iopub.status.busy": "2026-07-04T19:34:22.638076Z", "iopub.status.idle": "2026-07-04T19:34:22.641516Z", "shell.execute_reply": "2026-07-04T19:34:22.641125Z" } }, "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': 'gliasin',\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: ['cg10501210', 'cg13782884', 'cg14419975', 'cg16909962', 'cg16361302', 'cg21122225', 'cg23606718', 'cg04187185', 'cg18336674', 'cg02578944', 'cg04190475', 'cg11744777', 'cg14848772', 'cg16695576', 'cg18468088', 'cg20223728', 'cg01217984', 'cg12001630', 'cg26830108', 'cg05066959', 'cg15627457', 'cg13327545', 'cg14334161', 'cg17485681', 'cg01560972', 'cg08715791', 'cg26942121', 'cg10378521', 'cg10906284', 'cg24894584']... [Total elements: 220]\n", "base_model_features: None\n", "\n", "%==================================== Model Details ====================================%\n", "Model Structure:\n", "\n", "base_model: LinearModel(\n", " (linear): Linear(in_features=220, out_features=1, bias=True)\n", ")\n", "\n", "%==================================== Model Details ====================================%\n", "Model Parameters and Weights:\n", "\n", "base_model.linear.weight: [-6.912019729614258, 19.981544494628906, 2.626999855041504, 13.658905029296875, 1.2430390119552612, 1.00424325466156, 12.733586311340332, 2.4791877269744873, -0.152785524725914, 1.1130852699279785, -2.2575085163116455, 11.418535232543945, 1.824793815612793, -1.495959758758545, 2.821787118911743, -7.900047302246094, 5.732859134674072, 2.7955639362335205, 1.5466865301132202, 0.026381725445389748, 2.8932950496673584, 5.647821426391602, 12.655052185058594, 0.6008855700492859, 0.8880432844161987, 3.119521141052246, -6.521735191345215, -2.816964626312256, 2.2651326656341553, 8.25953197479248]... [Tensor of shape torch.Size([1, 220])]\n", "base_model.linear.bias: tensor([0.9233])\n", "\n", "%==================================== Model Details ====================================%\n", "\n" ] } ], "source": [ "pya.utils.print_model_details(model)" ] }, { "cell_type": "markdown", "id": "d55e5719", "metadata": {}, "source": [ "## Basic test" ] }, { "cell_type": "code", "execution_count": 13, "id": "3bee47d9", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:34:22.642730Z", "iopub.status.busy": "2026-07-04T19:34:22.642660Z", "iopub.status.idle": "2026-07-04T19:34:22.647348Z", "shell.execute_reply": "2026-07-04T19:34:22.646813Z" } }, "outputs": [ { "data": { "text/plain": [ "tensor([[ -20.1317],\n", " [ -18.5452],\n", " [ -99.1609],\n", " [ -98.8837],\n", " [ 9.2466],\n", " [-205.0112],\n", " [-147.6273],\n", " [ 118.3328],\n", " [-114.6954],\n", " [-142.6733]], 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": "3aa8710e", "metadata": {}, "source": [ "## Save torch model" ] }, { "cell_type": "code", "execution_count": 14, "id": "3717ec3c", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:34:22.648884Z", "iopub.status.busy": "2026-07-04T19:34:22.648771Z", "iopub.status.idle": "2026-07-04T19:34:22.653308Z", "shell.execute_reply": "2026-07-04T19:34:22.652881Z" } }, "outputs": [], "source": [ "torch.save(model, f\"../weights/{model.metadata['clock_name']}.pt\")" ] }, { "cell_type": "markdown", "id": "68d5cd98", "metadata": {}, "source": [ "## Clear directory\n", "" ] }, { "cell_type": "code", "execution_count": 15, "id": "391af4d4", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:34:22.654647Z", "iopub.status.busy": "2026-07-04T19:34:22.654572Z", "iopub.status.idle": "2026-07-04T19:34:22.658049Z", "shell.execute_reply": "2026-07-04T19:34:22.657634Z" } }, "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 }