{ "cells": [ { "cell_type": "markdown", "id": "8f114a41", "metadata": {}, "source": [ "# Garagnani" ] }, { "cell_type": "markdown", "id": "ba55adb8", "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": "254c2647", "metadata": {}, "source": [ "Let's first import some packages:" ] }, { "cell_type": "code", "execution_count": 1, "id": "6d25f50c", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:51:39.209507Z", "iopub.status.busy": "2026-07-04T19:51:39.209388Z", "iopub.status.idle": "2026-07-04T19:51:42.327000Z", "shell.execute_reply": "2026-07-04T19:51:42.326501Z" } }, "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": "6d621076", "metadata": {}, "source": [ "## Instantiate model class" ] }, { "cell_type": "code", "execution_count": 2, "id": "cb426744", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:51:42.328673Z", "iopub.status.busy": "2026-07-04T19:51:42.328499Z", "iopub.status.idle": "2026-07-04T19:51:42.331052Z", "shell.execute_reply": "2026-07-04T19:51:42.330731Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "class Garagnani(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.Garagnani)" ] }, { "cell_type": "code", "execution_count": 3, "id": "201959bc", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:51:42.332432Z", "iopub.status.busy": "2026-07-04T19:51:42.332343Z", "iopub.status.idle": "2026-07-04T19:51:42.333903Z", "shell.execute_reply": "2026-07-04T19:51:42.333590Z" } }, "outputs": [], "source": [ "model = pya.models.Garagnani()" ] }, { "cell_type": "markdown", "id": "7fd96a8e", "metadata": {}, "source": [ "## Define clock metadata" ] }, { "cell_type": "code", "execution_count": 4, "id": "d75cdf93", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:51:42.335079Z", "iopub.status.busy": "2026-07-04T19:51:42.335012Z", "iopub.status.idle": "2026-07-04T19:51:42.336992Z", "shell.execute_reply": "2026-07-04T19:51:42.336582Z" } }, "outputs": [], "source": [ "model.metadata[\"clock_name\"] = \"garagnani\"\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\"] = 2012\n", "model.metadata[\"approved_by_author\"] = \"⌛\"\n", "model.metadata[\"citation\"] = \"Garagnani, Paolo, et al. \\\"Methylation of ELOVL2 gene as a new epigenetic marker of age.\\\" Aging Cell 11.6 (2012): 1132-1134.\"\n", "model.metadata[\"doi\"] = \"https://doi.org/10.1111/acel.12005\"\n", "model.metadata[\"notes\"] = \"The source study identified age-associated ELOVL2 methylation, but did not publish a one-CpG age equation. Pyaging returns the raw cg16867657 methylation beta value using coefficient 1 and zero intercept; it does not return calibrated chronological age.\"\n", "model.metadata[\"research_only\"] = None\n", "model.metadata[\"tissue\"] = [\"whole blood\"] # Paper: The listed tissue is the model-development sample material.\n", "model.metadata[\"predicts\"] = [\"ELOVL2 methylation\"] # Paper: (Intercept),0; cg16867657,1\n", "model.metadata[\"training_target\"] = [\"not applicable\"] # Paper: cg16867657 methylation is strongly correlated with chronological age in whole blood.\n", "model.metadata[\"unit\"] = [\"beta value\"] # Paper: The packaged single-CpG output is raw cg16867657/ELOVL2 methylation on the beta-value scale.\n", "model.metadata[\"model_type\"] = \"single-CpG score\" # Paper: The sole feature has coefficient 1 and the intercept is 0.\n", "model.metadata[\"platform\"] = [\"Illumina 450K\"] # Paper: Training/selection used Illumina 450K.\n", "model.metadata[\"population\"] = \"all ages\" # Paper: The study includes cord-blood/newborn samples and people through approximately age 99.\n", "model.metadata[\"journal\"] = \"Aging Cell\"\n", "model.metadata[\"last_author\"] = \"Claudio Franceschi\"\n", "model.metadata[\"n_features\"] = 1\n", "model.metadata[\"citations\"] = 500\n", "model.metadata[\"citations_date\"] = \"2026-07-05\"\n" ] }, { "cell_type": "markdown", "id": "ad2c75f1", "metadata": {}, "source": [ "## Download clock dependencies" ] }, { "cell_type": "code", "execution_count": 5, "id": "415253ea", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:51:42.338309Z", "iopub.status.busy": "2026-07-04T19:51:42.338242Z", "iopub.status.idle": "2026-07-04T19:51:42.624168Z", "shell.execute_reply": "2026-07-04T19:51:42.623679Z" } }, "outputs": [ { "data": { "text/plain": [ "0" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "os.system(f\"curl -sL -o coefficients.csv https://raw.githubusercontent.com/Duzhaozhen/OmniAge/c10fbe8cb92957520fbff1d55ae1def0691252e5/OmniAgePy/src/omniage/data/Garagnani.csv\")" ] }, { "cell_type": "markdown", "id": "7ce6a5f7", "metadata": {}, "source": [ "## Load features" ] }, { "cell_type": "code", "execution_count": 6, "id": "aeb80d8a", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:51:42.625774Z", "iopub.status.busy": "2026-07-04T19:51:42.625644Z", "iopub.status.idle": "2026-07-04T19:51:42.636402Z", "shell.execute_reply": "2026-07-04T19:51:42.635794Z" } }, "outputs": [], "source": [ "df = pd.read_csv('coefficients.csv')\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": "83463fb2", "metadata": {}, "source": [ "## Load weights into base model" ] }, { "cell_type": "code", "execution_count": 7, "id": "0d5de156", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:51:42.638966Z", "iopub.status.busy": "2026-07-04T19:51:42.638826Z", "iopub.status.idle": "2026-07-04T19:51:42.647574Z", "shell.execute_reply": "2026-07-04T19:51:42.647030Z" } }, "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": "8693b6ef", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:51:42.649838Z", "iopub.status.busy": "2026-07-04T19:51:42.649711Z", "iopub.status.idle": "2026-07-04T19:51:42.655350Z", "shell.execute_reply": "2026-07-04T19:51:42.654332Z" } }, "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": "d463f672", "metadata": {}, "source": [ "## Load reference values" ] }, { "cell_type": "code", "execution_count": 9, "id": "5f4f18e0", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:51:42.657993Z", "iopub.status.busy": "2026-07-04T19:51:42.657847Z", "iopub.status.idle": "2026-07-04T19:51:42.659932Z", "shell.execute_reply": "2026-07-04T19:51:42.659361Z" } }, "outputs": [], "source": [ "model.reference_values = None" ] }, { "cell_type": "markdown", "id": "2068ed02", "metadata": {}, "source": [ "## Load preprocess and postprocess objects" ] }, { "cell_type": "code", "execution_count": 10, "id": "e74eaf01", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:51:42.661643Z", "iopub.status.busy": "2026-07-04T19:51:42.661527Z", "iopub.status.idle": "2026-07-04T19:51:42.663640Z", "shell.execute_reply": "2026-07-04T19:51:42.663193Z" } }, "outputs": [], "source": [ "model.preprocess_name = None\n", "model.preprocess_dependencies = None" ] }, { "cell_type": "code", "execution_count": 11, "id": "d927c75a", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:51:42.665173Z", "iopub.status.busy": "2026-07-04T19:51:42.665061Z", "iopub.status.idle": "2026-07-04T19:51:42.666799Z", "shell.execute_reply": "2026-07-04T19:51:42.666423Z" } }, "outputs": [], "source": [ "model.postprocess_name = None\n", "model.postprocess_dependencies = None" ] }, { "cell_type": "markdown", "id": "b2f72ef5", "metadata": {}, "source": [ "## Check all clock parameters" ] }, { "cell_type": "code", "execution_count": 12, "id": "a33acb2f", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:51:42.667971Z", "iopub.status.busy": "2026-07-04T19:51:42.667896Z", "iopub.status.idle": "2026-07-04T19:51:42.687550Z", "shell.execute_reply": "2026-07-04T19:51:42.686988Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "%==================================== Model Details ====================================%\n", "Model Attributes:\n", "\n", "training: True\n", "metadata: {'approved_by_author': '⌛',\n", " 'citation': 'Garagnani, Paolo, et al. \"Methylation of ELOVL2 gene as a new '\n", " 'epigenetic marker of age.\" Aging Cell 11.6 (2012): 1132-1134.',\n", " 'clock_name': 'garagnani',\n", " 'data_type': 'methylation',\n", " 'doi': 'https://doi.org/10.1111/acel.12005',\n", " 'notes': None,\n", " 'research_only': None,\n", " 'species': 'Homo sapiens',\n", " 'version': None,\n", " 'year': 2012}\n", "reference_values: None\n", "preprocess_name: None\n", "preprocess_dependencies: None\n", "postprocess_name: None\n", "postprocess_dependencies: None\n", "features: ['cg16867657']\n", "base_model_features: None\n", "\n", "%==================================== Model Details ====================================%\n", "Model Structure:\n", "\n", "base_model: LinearModel(\n", " (linear): Linear(in_features=1, out_features=1, bias=True)\n", ")\n", "\n", "%==================================== Model Details ====================================%\n", "Model Parameters and Weights:\n", "\n", "base_model.linear.weight: tensor([[1.]])\n", "base_model.linear.bias: tensor([0.])\n", "\n", "%==================================== Model Details ====================================%\n", "\n" ] } ], "source": [ "pya.utils.print_model_details(model)" ] }, { "cell_type": "markdown", "id": "26efdca1", "metadata": {}, "source": [ "## Basic test" ] }, { "cell_type": "code", "execution_count": 13, "id": "4da5647d", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:51:42.688862Z", "iopub.status.busy": "2026-07-04T19:51:42.688753Z", "iopub.status.idle": "2026-07-04T19:51:42.698233Z", "shell.execute_reply": "2026-07-04T19:51:42.697761Z" } }, "outputs": [ { "data": { "text/plain": [ "tensor([[ 0.3367],\n", " [ 0.1288],\n", " [ 0.2345],\n", " [ 0.2303],\n", " [-1.1229],\n", " [-0.1863],\n", " [ 2.2082],\n", " [-0.6380],\n", " [ 0.4617],\n", " [ 0.2674]], 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": "1f25a60e", "metadata": {}, "source": [ "## Save torch model" ] }, { "cell_type": "code", "execution_count": 14, "id": "b2949aca", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:51:42.699635Z", "iopub.status.busy": "2026-07-04T19:51:42.699546Z", "iopub.status.idle": "2026-07-04T19:51:42.704531Z", "shell.execute_reply": "2026-07-04T19:51:42.704084Z" } }, "outputs": [], "source": [ "torch.save(model, f\"../weights/{model.metadata['clock_name']}.pt\")" ] }, { "cell_type": "markdown", "id": "353ffcb4", "metadata": {}, "source": [ "## Clear directory\n", "" ] }, { "cell_type": "code", "execution_count": 15, "id": "16696f75", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:51:42.706382Z", "iopub.status.busy": "2026-07-04T19:51:42.706265Z", "iopub.status.idle": "2026-07-04T19:51:42.709805Z", "shell.execute_reply": "2026-07-04T19:51:42.709411Z" } }, "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 }