{ "cells": [ { "cell_type": "markdown", "id": "0ffcfb02", "metadata": {}, "source": [ "# ADBahadoSingh" ] }, { "cell_type": "markdown", "id": "1f7d79fb", "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": "9a7dfb8c", "metadata": {}, "source": [ "Let's first import some packages:" ] }, { "cell_type": "code", "execution_count": 1, "id": "2fed02ab", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T20:25:23.516525Z", "iopub.status.busy": "2026-07-04T20:25:23.516388Z", "iopub.status.idle": "2026-07-04T20:25:26.060894Z", "shell.execute_reply": "2026-07-04T20:25:26.060447Z" } }, "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": "e57c051e", "metadata": {}, "source": [ "## Instantiate model class" ] }, { "cell_type": "code", "execution_count": 2, "id": "0b7ec122", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T20:25:26.062543Z", "iopub.status.busy": "2026-07-04T20:25:26.062344Z", "iopub.status.idle": "2026-07-04T20:25:26.066252Z", "shell.execute_reply": "2026-07-04T20:25:26.065457Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "class ADBahadoSingh(LinearReferenceClock):\n", " def postprocess(self, x):\n", " \"\"\"Logistic transform to an Alzheimer's disease risk probability.\n", "\n", " The published logit constant (-0.072) is carried in the linear intercept.\n", " \"\"\"\n", " return torch.sigmoid(x)\n", "\n" ] } ], "source": [ "def print_entire_class(cls):\n", " source = inspect.getsource(cls)\n", " print(source)\n", "\n", "print_entire_class(pya.models.ADBahadoSingh)" ] }, { "cell_type": "code", "execution_count": 3, "id": "f419d49e", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T20:25:26.068511Z", "iopub.status.busy": "2026-07-04T20:25:26.068372Z", "iopub.status.idle": "2026-07-04T20:25:26.070387Z", "shell.execute_reply": "2026-07-04T20:25:26.070016Z" } }, "outputs": [], "source": [ "model = pya.models.ADBahadoSingh()" ] }, { "cell_type": "markdown", "id": "a7d830f3", "metadata": {}, "source": [ "## Define clock metadata" ] }, { "cell_type": "code", "execution_count": 4, "id": "3192412b", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T20:25:26.071716Z", "iopub.status.busy": "2026-07-04T20:25:26.071645Z", "iopub.status.idle": "2026-07-04T20:25:26.073738Z", "shell.execute_reply": "2026-07-04T20:25:26.073337Z" } }, "outputs": [], "source": [ "model.metadata[\"clock_name\"] = \"adbahadosingh\"\n", "model.metadata[\"data_type\"] = \"DNA methylation\" # Paper: Genome-wide DNA methylation analysis\n", "model.metadata[\"species\"] = \"Homo sapiens\" # Paper: 24 late-onset AD and 24 cognitively healthy subjects\n", "model.metadata[\"year\"] = 2021\n", "model.metadata[\"approved_by_author\"] = \"⌛\"\n", "model.metadata[\"citation\"] = \"Bahado-Singh, R. O., Vishweswaraiah, S., Aydas, B., et al. (2021). Artificial intelligence and leukocyte epigenomics: Evaluation and prediction of late-onset Alzheimer's disease. PLOS ONE, 16(4), e0248375.\"\n", "model.metadata[\"doi\"] = \"https://doi.org/10.1371/journal.pone.0248375\"\n", "model.metadata[\"notes\"] = \"PyAging implements the paper’s conventional four-CpG logistic-regression equation and applies a sigmoid to return LOAD case probability; it does not implement the separate high-dimensional deep-learning classifiers also evaluated in the paper.\"\n", "model.metadata[\"research_only\"] = None\n", "model.metadata[\"tissue\"] = [\"whole blood\"] # Paper: Genomic DNA was extracted from whole blood samples; leukocyte epigenomic biomarkers\n", "model.metadata[\"predicts\"] = [\"late-onset Alzheimer's disease\"] # Paper: where P is Pr(y = 1|x)\n", "model.metadata[\"training_target\"] = [\"late-onset Alzheimer's disease\"] # Paper: 24 late-onset AD and 24 cognitively healthy subjects\n", "model.metadata[\"unit\"] = [\"probability\"] # Paper: return torch.sigmoid(x)\n", "model.metadata[\"model_type\"] = \"logistic regression\" # Paper: The logistic regression model is represented below\n", "model.metadata[\"platform\"] = [\"Illumina EPIC\"] # Paper: Infinium MethylationEPIC array in 24 LOAD and 24 healthy subjects\n", "model.metadata[\"population\"] = \"older adults\" # Paper: Cases: 83.17 (7.97); Controls: 80.04 (8.42)\n", "model.metadata[\"journal\"] = \"PLOS ONE\"\n", "model.metadata[\"last_author\"] = \"Uppala Radhakrishna\"\n", "model.metadata[\"n_features\"] = 4\n", "model.metadata[\"citations\"] = 33\n", "model.metadata[\"citations_date\"] = \"2026-07-05\"\n" ] }, { "cell_type": "markdown", "id": "83246005", "metadata": {}, "source": [ "## Download clock dependencies" ] }, { "cell_type": "code", "execution_count": 5, "id": "182bc2c1", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T20:25:26.074885Z", "iopub.status.busy": "2026-07-04T20:25:26.074814Z", "iopub.status.idle": "2026-07-04T20:25:26.345655Z", "shell.execute_reply": "2026-07-04T20:25:26.343440Z" } }, "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/AD_Bahado-Singh.csv\")" ] }, { "cell_type": "markdown", "id": "3a9680f0", "metadata": {}, "source": [ "## Load features" ] }, { "cell_type": "code", "execution_count": 6, "id": "d4699f05", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T20:25:26.350516Z", "iopub.status.busy": "2026-07-04T20:25:26.350127Z", "iopub.status.idle": "2026-07-04T20:25:26.359398Z", "shell.execute_reply": "2026-07-04T20:25:26.358217Z" } }, "outputs": [], "source": [ "df = pd.read_csv('coefficients.csv')\n", "mask = df['CpGmarker'].astype(str).str.lower().isin(['intercept', '(intercept)'])\n", "coef_df = df.loc[~mask].reset_index(drop=True)\n", "model.features = coef_df['CpGmarker'].tolist()" ] }, { "cell_type": "markdown", "id": "123e9bba", "metadata": {}, "source": [ "## Load weights into base model" ] }, { "cell_type": "code", "execution_count": 7, "id": "e268eb33", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T20:25:26.362892Z", "iopub.status.busy": "2026-07-04T20:25:26.362583Z", "iopub.status.idle": "2026-07-04T20:25:26.366289Z", "shell.execute_reply": "2026-07-04T20:25:26.365442Z" } }, "outputs": [], "source": [ "weights = torch.tensor(coef_df['CoefficientTraining'].tolist()).unsqueeze(0).float()\n", "# Published logistic-regression constant (Bahado-Singh 2021, eq. e001)\n", "intercept = torch.tensor([-0.072]).float()" ] }, { "cell_type": "code", "execution_count": 8, "id": "319ef876", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T20:25:26.368300Z", "iopub.status.busy": "2026-07-04T20:25:26.368187Z", "iopub.status.idle": "2026-07-04T20:25:26.370906Z", "shell.execute_reply": "2026-07-04T20:25:26.370469Z" } }, "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": "9e022528", "metadata": {}, "source": [ "## Load reference values" ] }, { "cell_type": "code", "execution_count": 9, "id": "8c128c95", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T20:25:26.372269Z", "iopub.status.busy": "2026-07-04T20:25:26.372195Z", "iopub.status.idle": "2026-07-04T20:25:26.373829Z", "shell.execute_reply": "2026-07-04T20:25:26.373491Z" } }, "outputs": [], "source": [ "model.reference_values = None" ] }, { "cell_type": "markdown", "id": "398afb53", "metadata": {}, "source": [ "## Load preprocess and postprocess objects" ] }, { "cell_type": "code", "execution_count": 10, "id": "c91c6c17", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T20:25:26.375177Z", "iopub.status.busy": "2026-07-04T20:25:26.375079Z", "iopub.status.idle": "2026-07-04T20:25:26.376723Z", "shell.execute_reply": "2026-07-04T20:25:26.376259Z" } }, "outputs": [], "source": [ "model.preprocess_name = None\n", "model.preprocess_dependencies = None" ] }, { "cell_type": "code", "execution_count": 11, "id": "34369a68", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T20:25:26.377825Z", "iopub.status.busy": "2026-07-04T20:25:26.377750Z", "iopub.status.idle": "2026-07-04T20:25:26.379437Z", "shell.execute_reply": "2026-07-04T20:25:26.378962Z" } }, "outputs": [], "source": [ "model.postprocess_name = 'sigmoid'\n", "model.postprocess_dependencies = None" ] }, { "cell_type": "markdown", "id": "ff8801c7", "metadata": {}, "source": [ "## Check all clock parameters" ] }, { "cell_type": "code", "execution_count": 12, "id": "34823472", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T20:25:26.383261Z", "iopub.status.busy": "2026-07-04T20:25:26.382974Z", "iopub.status.idle": "2026-07-04T20:25:26.390843Z", "shell.execute_reply": "2026-07-04T20:25:26.390123Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "%==================================== Model Details ====================================%\n", "Model Attributes:\n", "\n", "training: True\n", "metadata: {'approved_by_author': '⌛',\n", " 'citation': 'Bahado-Singh, Ray O., et al. \"Artificial intelligence and '\n", " 'leukocyte epigenomics: Evaluation and prediction of late-onset '\n", " 'Alzheimer\\'s disease.\" PLOS ONE 16.4 (2021): e0248375.',\n", " 'clock_name': 'adbahadosingh',\n", " 'data_type': 'methylation',\n", " 'doi': 'https://doi.org/10.1371/journal.pone.0248375',\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: 'sigmoid'\n", "postprocess_dependencies: None\n", "features: ['cg02356786', 'cg04515524', 'cg00613827', 'cg07509935']\n", "base_model_features: None\n", "\n", "%==================================== Model Details ====================================%\n", "Model Structure:\n", "\n", "base_model: LinearModel(\n", " (linear): Linear(in_features=4, out_features=1, bias=True)\n", ")\n", "\n", "%==================================== Model Details ====================================%\n", "Model Parameters and Weights:\n", "\n", "base_model.linear.weight: tensor([[-0.9920, -1.5000, -1.9010, -1.3580]])\n", "base_model.linear.bias: tensor([-0.0720])\n", "\n", "%==================================== Model Details ====================================%\n", "\n" ] } ], "source": [ "pya.utils.print_model_details(model)" ] }, { "cell_type": "markdown", "id": "dd46c341", "metadata": {}, "source": [ "## Basic test" ] }, { "cell_type": "code", "execution_count": 13, "id": "1f86769b", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T20:25:26.392620Z", "iopub.status.busy": "2026-07-04T20:25:26.392500Z", "iopub.status.idle": "2026-07-04T20:25:26.397490Z", "shell.execute_reply": "2026-07-04T20:25:26.396912Z" } }, "outputs": [ { "data": { "text/plain": [ "tensor([[0.2779],\n", " [0.9795],\n", " [0.1451],\n", " [0.7133],\n", " [0.0025],\n", " [0.1557],\n", " [0.9681],\n", " [0.7552],\n", " [0.9825],\n", " [0.5411]], 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": "db3d55f5", "metadata": {}, "source": [ "## Save torch model" ] }, { "cell_type": "code", "execution_count": 14, "id": "afc5f9b7", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T20:25:26.399340Z", "iopub.status.busy": "2026-07-04T20:25:26.399216Z", "iopub.status.idle": "2026-07-04T20:25:26.403718Z", "shell.execute_reply": "2026-07-04T20:25:26.403323Z" } }, "outputs": [], "source": [ "torch.save(model, f\"../weights/{model.metadata['clock_name']}.pt\")" ] }, { "cell_type": "markdown", "id": "620bbb0b", "metadata": {}, "source": [ "## Clear directory\n", "" ] }, { "cell_type": "code", "execution_count": 15, "id": "77fd8481", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T20:25:26.405201Z", "iopub.status.busy": "2026-07-04T20:25:26.405118Z", "iopub.status.idle": "2026-07-04T20:25:26.408698Z", "shell.execute_reply": "2026-07-04T20:25:26.408230Z" } }, "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 }