{ "cells": [ { "cell_type": "markdown", "id": "3404bf4b", "metadata": {}, "source": [ "# Bocklandt" ] }, { "cell_type": "markdown", "id": "5be08441", "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": "b86ff88a", "metadata": {}, "source": [ "Let's first import some packages:" ] }, { "cell_type": "code", "execution_count": 1, "id": "a0350a8c", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:51:44.798790Z", "iopub.status.busy": "2026-07-04T19:51:44.798594Z", "iopub.status.idle": "2026-07-04T19:51:46.788880Z", "shell.execute_reply": "2026-07-04T19:51:46.788370Z" } }, "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": "437360aa", "metadata": {}, "source": [ "## Instantiate model class" ] }, { "cell_type": "code", "execution_count": 2, "id": "f3c51b8c", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:51:46.790778Z", "iopub.status.busy": "2026-07-04T19:51:46.790590Z", "iopub.status.idle": "2026-07-04T19:51:46.793335Z", "shell.execute_reply": "2026-07-04T19:51:46.792938Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "class Bocklandt(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.Bocklandt)" ] }, { "cell_type": "code", "execution_count": 3, "id": "d98fcf32", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:51:46.794721Z", "iopub.status.busy": "2026-07-04T19:51:46.794635Z", "iopub.status.idle": "2026-07-04T19:51:46.796144Z", "shell.execute_reply": "2026-07-04T19:51:46.795832Z" } }, "outputs": [], "source": [ "model = pya.models.Bocklandt()" ] }, { "cell_type": "markdown", "id": "4b99faa7", "metadata": {}, "source": [ "## Define clock metadata" ] }, { "cell_type": "code", "execution_count": 4, "id": "3244b4c2", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:51:46.797346Z", "iopub.status.busy": "2026-07-04T19:51:46.797276Z", "iopub.status.idle": "2026-07-04T19:51:46.799277Z", "shell.execute_reply": "2026-07-04T19:51:46.798934Z" } }, "outputs": [], "source": [ "model.metadata[\"clock_name\"] = \"bocklandt\"\n", "model.metadata[\"data_type\"] = \"DNA methylation\" # Paper: The predictor uses CpG methylation percentages.\n", "model.metadata[\"species\"] = \"Homo sapiens\" # Paper: The study analyzed human saliva.\n", "model.metadata[\"year\"] = 2011\n", "model.metadata[\"approved_by_author\"] = \"⌛\"\n", "model.metadata[\"citation\"] = \"Bocklandt, S., Lin, W., Sehl, M.E. et al. Epigenetic predictor of age. PLoS ONE 6, e14821 (2011).\"\n", "model.metadata[\"doi\"] = \"https://doi.org/10.1371/journal.pone.0014821\"\n", "model.metadata[\"notes\"] = \"Package-facing one-CpG identity score: pyaging returns raw cg09809672 methylation with coefficient 1 and zero intercept. The published saliva age regression instead uses EDARADD and NPTX2, including an EDARADD-squared basis term; that published age model is not implemented.\"\n", "model.metadata[\"research_only\"] = None\n", "model.metadata[\"tissue\"] = [\"saliva\"] # Paper: Both discovery and validation samples were saliva.\n", "model.metadata[\"predicts\"] = [\"EDARADD methylation\"] # Paper: The packaged object contains only cg09809672 with coefficient 1.\n", "model.metadata[\"training_target\"] = [\"chronological age\"] # Paper: Chronological age was the regression outcome.\n", "model.metadata[\"unit\"] = [\"beta value\"] # Paper: The packaged single-CpG output is raw cg09809672/EDARADD methylation on the beta-value scale.\n", "model.metadata[\"model_type\"] = \"single-CpG score\" # Paper: The packaged object contains only cg09809672 with coefficient 1.\n", "model.metadata[\"platform\"] = [\"Illumina 27K\"] # Paper: Feature discovery used the Illumina HumanMethylation27 array.\n", "model.metadata[\"population\"] = \"adults\" # Paper: Discovery used 34 male monozygotic twin pairs aged 21–55; validation included unrelated adults aged 18–70.\n", "model.metadata[\"journal\"] = \"PLoS ONE\"\n", "model.metadata[\"last_author\"] = \"Éric Vilain\"\n", "model.metadata[\"n_features\"] = 1\n", "model.metadata[\"citations\"] = 1057\n", "model.metadata[\"citations_date\"] = \"2026-07-05\"\n" ] }, { "cell_type": "markdown", "id": "1969c45d", "metadata": {}, "source": [ "## Download clock dependencies" ] }, { "cell_type": "code", "execution_count": 5, "id": "fbec7754", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:51:46.800635Z", "iopub.status.busy": "2026-07-04T19:51:46.800565Z", "iopub.status.idle": "2026-07-04T19:51:47.096539Z", "shell.execute_reply": "2026-07-04T19:51:47.095755Z" } }, "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/bio-learn/biolearn/180852e2bab473303cb85da627178b1695ee9d86/biolearn/data/Bocklandt.csv\")" ] }, { "cell_type": "markdown", "id": "10132cba", "metadata": {}, "source": [ "## Load features" ] }, { "cell_type": "code", "execution_count": 6, "id": "9ed051ad", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:51:47.099015Z", "iopub.status.busy": "2026-07-04T19:51:47.098817Z", "iopub.status.idle": "2026-07-04T19:51:47.104628Z", "shell.execute_reply": "2026-07-04T19:51:47.104201Z" } }, "outputs": [], "source": [ "df = pd.read_csv('coefficients.csv')\n", "mask = df['CpGmarker'].astype(str).str.lower().isin(['intercept', '(intercept)'])\n", "intercept_value = float(df.loc[mask, 'CoefficientTraining'].iloc[0]) if mask.any() else 0.0\n", "coef_df = df.loc[~mask].reset_index(drop=True)\n", "model.features = coef_df['CpGmarker'].tolist()" ] }, { "cell_type": "markdown", "id": "ccd7b0d1", "metadata": {}, "source": [ "## Load weights into base model" ] }, { "cell_type": "code", "execution_count": 7, "id": "5419b258", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:51:47.106553Z", "iopub.status.busy": "2026-07-04T19:51:47.106419Z", "iopub.status.idle": "2026-07-04T19:51:47.109264Z", "shell.execute_reply": "2026-07-04T19:51:47.108836Z" } }, "outputs": [], "source": [ "weights = torch.tensor(coef_df['CoefficientTraining'].tolist()).unsqueeze(0).float()\n", "intercept = torch.tensor([intercept_value]).float()" ] }, { "cell_type": "code", "execution_count": 8, "id": "37219f91", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:51:47.110948Z", "iopub.status.busy": "2026-07-04T19:51:47.110788Z", "iopub.status.idle": "2026-07-04T19:51:47.113691Z", "shell.execute_reply": "2026-07-04T19:51:47.113220Z" } }, "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": "d011f410", "metadata": {}, "source": [ "## Load reference values" ] }, { "cell_type": "code", "execution_count": 9, "id": "93e047a7", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:51:47.115284Z", "iopub.status.busy": "2026-07-04T19:51:47.115168Z", "iopub.status.idle": "2026-07-04T19:51:47.117052Z", "shell.execute_reply": "2026-07-04T19:51:47.116600Z" } }, "outputs": [], "source": [ "model.reference_values = None" ] }, { "cell_type": "markdown", "id": "d644fecc", "metadata": {}, "source": [ "## Load preprocess and postprocess objects" ] }, { "cell_type": "code", "execution_count": 10, "id": "336cb3dd", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:51:47.118469Z", "iopub.status.busy": "2026-07-04T19:51:47.118361Z", "iopub.status.idle": "2026-07-04T19:51:47.120131Z", "shell.execute_reply": "2026-07-04T19:51:47.119690Z" } }, "outputs": [], "source": [ "model.preprocess_name = None\n", "model.preprocess_dependencies = None" ] }, { "cell_type": "code", "execution_count": 11, "id": "b82cc9eb", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:51:47.121556Z", "iopub.status.busy": "2026-07-04T19:51:47.121443Z", "iopub.status.idle": "2026-07-04T19:51:47.123251Z", "shell.execute_reply": "2026-07-04T19:51:47.122825Z" } }, "outputs": [], "source": [ "model.postprocess_name = None\n", "model.postprocess_dependencies = None" ] }, { "cell_type": "markdown", "id": "7435a8fd", "metadata": {}, "source": [ "## Check all clock parameters" ] }, { "cell_type": "code", "execution_count": 12, "id": "bfd61c96", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:51:47.124707Z", "iopub.status.busy": "2026-07-04T19:51:47.124606Z", "iopub.status.idle": "2026-07-04T19:51:47.128446Z", "shell.execute_reply": "2026-07-04T19:51:47.127896Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "%==================================== Model Details ====================================%\n", "Model Attributes:\n", "\n", "training: True\n", "metadata: {'approved_by_author': '⌛',\n", " 'citation': 'Bocklandt, Sven, et al. \"Epigenetic predictor of age.\" PloS one '\n", " '6.6 (2011): e14821.',\n", " 'clock_name': 'bocklandt',\n", " 'data_type': 'methylation',\n", " 'doi': 'https://doi.org/10.1371/journal.pone.0014821',\n", " 'notes': None,\n", " 'research_only': None,\n", " 'species': 'Homo sapiens',\n", " 'version': None,\n", " 'year': 2011}\n", "reference_values: None\n", "preprocess_name: None\n", "preprocess_dependencies: None\n", "postprocess_name: None\n", "postprocess_dependencies: None\n", "features: ['cg09809672']\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": "ffa4bf22", "metadata": {}, "source": [ "## Basic test" ] }, { "cell_type": "code", "execution_count": 13, "id": "14e5bafe", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:51:47.130040Z", "iopub.status.busy": "2026-07-04T19:51:47.129942Z", "iopub.status.idle": "2026-07-04T19:51:47.134584Z", "shell.execute_reply": "2026-07-04T19:51:47.134136Z" } }, "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": "20017283", "metadata": {}, "source": [ "## Save torch model" ] }, { "cell_type": "code", "execution_count": 14, "id": "bbd4a9b0", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:51:47.136020Z", "iopub.status.busy": "2026-07-04T19:51:47.135936Z", "iopub.status.idle": "2026-07-04T19:51:47.139291Z", "shell.execute_reply": "2026-07-04T19:51:47.138858Z" } }, "outputs": [], "source": [ "torch.save(model, f\"../weights/{model.metadata['clock_name']}.pt\")" ] }, { "cell_type": "markdown", "id": "6f090274", "metadata": {}, "source": [ "## Clear directory\n", "" ] }, { "cell_type": "code", "execution_count": 15, "id": "dacbc85b", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:51:47.140562Z", "iopub.status.busy": "2026-07-04T19:51:47.140481Z", "iopub.status.idle": "2026-07-04T19:51:47.143967Z", "shell.execute_reply": "2026-07-04T19:51:47.143640Z" } }, "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 }