{ "cells": [ { "cell_type": "markdown", "id": "136553c7", "metadata": {}, "source": [ "# Weidner" ] }, { "cell_type": "markdown", "id": "13408b5f", "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": "d3a9aaea", "metadata": {}, "source": [ "Let's first import some packages:" ] }, { "cell_type": "code", "execution_count": 1, "id": "1f91650f", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:14:50.757904Z", "iopub.status.busy": "2026-07-04T19:14:50.757632Z", "iopub.status.idle": "2026-07-04T19:14:53.618509Z", "shell.execute_reply": "2026-07-04T19:14:53.617898Z" } }, "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": "a636e6cd", "metadata": {}, "source": [ "## Instantiate model class" ] }, { "cell_type": "code", "execution_count": 2, "id": "df72b135", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:14:53.620223Z", "iopub.status.busy": "2026-07-04T19:14:53.620026Z", "iopub.status.idle": "2026-07-04T19:14:53.622809Z", "shell.execute_reply": "2026-07-04T19:14:53.622444Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "class Weidner(pyagingModel):\n", " def __init__(self):\n", " super().__init__()\n", "\n", " def preprocess(self, x):\n", " if self.reference_values is None:\n", " return x\n", " if isinstance(self.reference_values, torch.Tensor):\n", " reference = self.reference_values.to(device=x.device, dtype=x.dtype)\n", " else:\n", " reference = torch.tensor(self.reference_values, device=x.device, dtype=x.dtype)\n", " return torch.where(torch.isnan(x), reference, x)\n", "\n", " def postprocess(self, x):\n", " return x\n", "\n" ] } ], "source": [ "def print_entire_class(cls):\n", " source = inspect.getsource(cls)\n", " print(source)\n", "\n", "print_entire_class(pya.models.Weidner)" ] }, { "cell_type": "code", "execution_count": 3, "id": "07f61977", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:14:53.624076Z", "iopub.status.busy": "2026-07-04T19:14:53.623984Z", "iopub.status.idle": "2026-07-04T19:14:53.625725Z", "shell.execute_reply": "2026-07-04T19:14:53.625282Z" } }, "outputs": [], "source": [ "model = pya.models.Weidner()" ] }, { "cell_type": "markdown", "id": "06626889", "metadata": {}, "source": [ "## Define clock metadata" ] }, { "cell_type": "code", "execution_count": 4, "id": "a9aca4a3", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:14:53.626915Z", "iopub.status.busy": "2026-07-04T19:14:53.626845Z", "iopub.status.idle": "2026-07-04T19:14:53.628900Z", "shell.execute_reply": "2026-07-04T19:14:53.628504Z" } }, "outputs": [], "source": [ "model.metadata[\"clock_name\"] = \"weidner\"\n", "model.metadata[\"data_type\"] = \"DNA methylation\" # Paper: DNA methylation changes at just three CpG sites\n", "model.metadata[\"species\"] = \"Homo sapiens\" # Paper: blood samples from healthy human donors\n", "model.metadata[\"year\"] = 2014\n", "model.metadata[\"approved_by_author\"] = \"⌛\"\n", "model.metadata[\"citation\"] = \"Weidner, C. I., Lin, Q., Koch, C. M., et al. (2014). Aging of blood can be tracked by DNA methylation changes at just three CpG sites. Genome Biology, 15, R24.\"\n", "model.metadata[\"doi\"] = \"https://doi.org/10.1186/gb-2014-15-2-r24\"\n", "model.metadata[\"notes\"] = \"Three-site whole-blood epigenetic-age estimator. The sites were selected from Illumina 27K blood profiles, and the final multivariate linear equation was fitted on targeted bisulfite-pyrosequencing beta values from 82 blood samples and validated in 69 independent samples.\"\n", "model.metadata[\"research_only\"] = None\n", "model.metadata[\"tissue\"] = [\"whole blood\"] # Paper: DNAm profiles derived from blood samples; model fitted on blood samples\n", "model.metadata[\"predicts\"] = [\"biological age\"] # Paper: signature provides a simple biomarker to estimate the state of aging in blood\n", "model.metadata[\"training_target\"] = [\"chronological age\"] # Paper: multivariate linear model to predict donor age\n", "model.metadata[\"unit\"] = [\"years\"] # Paper: Predicted age (in years)\n", "model.metadata[\"model_type\"] = \"linear regression\" # Paper: multivariate linear regression model\n", "model.metadata[\"platform\"] = [\"Illumina 27K\", \"bisulfite sequencing\"] # Paper: HumanMethylation27 BeadChip feature selection followed by pyrosequencing after bisulfite conversion\n", "model.metadata[\"population\"] = \"adults\" # Paper: 82 blood samples for training and independent validation set of 69 blood samples\n", "model.metadata[\"journal\"] = \"Genome Biology\"\n", "model.metadata[\"last_author\"] = \"Wolfgang Wagner\"\n", "model.metadata[\"n_features\"] = 3\n", "model.metadata[\"citations\"] = 973\n", "model.metadata[\"citations_date\"] = \"2026-07-05\"\n" ] }, { "cell_type": "markdown", "id": "6f5db821", "metadata": {}, "source": [ "## Download clock dependencies" ] }, { "cell_type": "code", "execution_count": 5, "id": "97c175ff", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:14:53.630002Z", "iopub.status.busy": "2026-07-04T19:14:53.629937Z", "iopub.status.idle": "2026-07-04T19:14:53.946424Z", "shell.execute_reply": "2026-07-04T19:14:53.945623Z" } }, "outputs": [ { "data": { "text/plain": [ "0" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "supplementary_url = \"https://raw.githubusercontent.com/bio-learn/biolearn/180852e2bab473303cb85da627178b1695ee9d86/biolearn/data/Weidner.csv\"\n", "supplementary_file_name = \"Weidner.csv\"\n", "os.system(f\"curl -sL -o {supplementary_file_name} {supplementary_url}\")" ] }, { "cell_type": "markdown", "id": "d02d3ed4", "metadata": {}, "source": [ "## Load features" ] }, { "cell_type": "code", "execution_count": 6, "id": "eea1287d", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:14:53.948674Z", "iopub.status.busy": "2026-07-04T19:14:53.948497Z", "iopub.status.idle": "2026-07-04T19:14:53.956296Z", "shell.execute_reply": "2026-07-04T19:14:53.955522Z" } }, "outputs": [], "source": [ "df = pd.read_csv('Weidner.csv')\n", "model.features = df['CpGmarker'].tolist()" ] }, { "cell_type": "markdown", "id": "fb85d648", "metadata": {}, "source": [ "## Load weights into base model" ] }, { "cell_type": "code", "execution_count": 7, "id": "7c425874", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:14:53.958418Z", "iopub.status.busy": "2026-07-04T19:14:53.958253Z", "iopub.status.idle": "2026-07-04T19:14:53.964725Z", "shell.execute_reply": "2026-07-04T19:14:53.964251Z" } }, "outputs": [], "source": [ "weights = torch.tensor(df['CoefficientTraining'].tolist()).unsqueeze(0).float()\n", "intercept = torch.tensor([38.0]).float()" ] }, { "cell_type": "code", "execution_count": 8, "id": "6ad22e5c", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:14:53.966402Z", "iopub.status.busy": "2026-07-04T19:14:53.966287Z", "iopub.status.idle": "2026-07-04T19:14:53.971793Z", "shell.execute_reply": "2026-07-04T19:14:53.971343Z" } }, "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": "d86287f3", "metadata": {}, "source": [ "## Load reference values" ] }, { "cell_type": "code", "execution_count": 9, "id": "440614a6", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:14:53.973725Z", "iopub.status.busy": "2026-07-04T19:14:53.973595Z", "iopub.status.idle": "2026-07-04T19:14:53.975627Z", "shell.execute_reply": "2026-07-04T19:14:53.975046Z" } }, "outputs": [], "source": [ "model.reference_values = None" ] }, { "cell_type": "markdown", "id": "e8393289", "metadata": {}, "source": [ "## Load preprocess and postprocess objects" ] }, { "cell_type": "code", "execution_count": 10, "id": "611995fc", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:14:53.977018Z", "iopub.status.busy": "2026-07-04T19:14:53.976914Z", "iopub.status.idle": "2026-07-04T19:14:53.978624Z", "shell.execute_reply": "2026-07-04T19:14:53.978253Z" } }, "outputs": [], "source": [ "model.preprocess_name = None\n", "model.preprocess_dependencies = None" ] }, { "cell_type": "code", "execution_count": 11, "id": "9a95009e", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:14:53.980136Z", "iopub.status.busy": "2026-07-04T19:14:53.980032Z", "iopub.status.idle": "2026-07-04T19:14:53.981732Z", "shell.execute_reply": "2026-07-04T19:14:53.981275Z" } }, "outputs": [], "source": [ "model.postprocess_name = None\n", "model.postprocess_dependencies = None" ] }, { "cell_type": "markdown", "id": "2e741416", "metadata": {}, "source": [ "## Check all clock parameters" ] }, { "cell_type": "code", "execution_count": 12, "id": "b960b269", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:14:53.982989Z", "iopub.status.busy": "2026-07-04T19:14:53.982906Z", "iopub.status.idle": "2026-07-04T19:14:54.000670Z", "shell.execute_reply": "2026-07-04T19:14:54.000248Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "%==================================== Model Details ====================================%\n", "Model Attributes:\n", "\n", "training: True\n", "metadata: {'approved_by_author': '⌛',\n", " 'citation': 'Weidner, Carola I., et al. \"Aging of blood can be tracked by DNA '\n", " 'methylation changes at just three CpG sites.\" Genome biology '\n", " '15.2 (2014): R24.',\n", " 'clock_name': 'weidner',\n", " 'data_type': 'methylation',\n", " 'doi': 'https://doi.org/10.1186/gb-2014-15-2-r24',\n", " 'notes': None,\n", " 'research_only': None,\n", " 'species': 'Homo sapiens',\n", " 'version': None,\n", " 'year': 2014}\n", "reference_values: None\n", "preprocess_name: None\n", "preprocess_dependencies: None\n", "postprocess_name: None\n", "postprocess_dependencies: None\n", "features: ['cg02228185', 'cg25809905', 'cg17861230']\n", "base_model_features: None\n", "\n", "%==================================== Model Details ====================================%\n", "Model Structure:\n", "\n", "base_model: LinearModel(\n", " (linear): Linear(in_features=3, out_features=1, bias=True)\n", ")\n", "\n", "%==================================== Model Details ====================================%\n", "Model Parameters and Weights:\n", "\n", "base_model.linear.weight: tensor([[-26.4000, -23.7000, 164.7000]])\n", "base_model.linear.bias: tensor([38.])\n", "\n", "%==================================== Model Details ====================================%\n", "\n" ] } ], "source": [ "pya.utils.print_model_details(model)" ] }, { "cell_type": "markdown", "id": "73775bb5", "metadata": {}, "source": [ "## Basic test" ] }, { "cell_type": "code", "execution_count": 13, "id": "c7c0c320", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:14:54.001912Z", "iopub.status.busy": "2026-07-04T19:14:54.001835Z", "iopub.status.idle": "2026-07-04T19:14:54.010665Z", "shell.execute_reply": "2026-07-04T19:14:54.010206Z" } }, "outputs": [ { "data": { "text/plain": [ "tensor([[ 70.9916],\n", " [157.0004],\n", " [155.2728],\n", " [ 58.9885],\n", " [ 38.9402],\n", " [143.5184],\n", " [396.4215],\n", " [ 66.8971],\n", " [130.9565],\n", " [-26.1560]], 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": "7a8e340f", "metadata": {}, "source": [ "## Save torch model" ] }, { "cell_type": "code", "execution_count": 14, "id": "9890f370", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:14:54.011882Z", "iopub.status.busy": "2026-07-04T19:14:54.011804Z", "iopub.status.idle": "2026-07-04T19:14:54.016889Z", "shell.execute_reply": "2026-07-04T19:14:54.016460Z" } }, "outputs": [], "source": [ "torch.save(model, f\"../weights/{model.metadata['clock_name']}.pt\")" ] }, { "cell_type": "markdown", "id": "910e42e3", "metadata": {}, "source": [ "## Clear directory\n", "" ] }, { "cell_type": "code", "execution_count": 15, "id": "2ca0b462", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:14:54.018235Z", "iopub.status.busy": "2026-07-04T19:14:54.018143Z", "iopub.status.idle": "2026-07-04T19:14:54.021578Z", "shell.execute_reply": "2026-07-04T19:14:54.021146Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Deleted file: Weidner.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 }