{ "cells": [ { "cell_type": "markdown", "id": "e58b07ae", "metadata": {}, "source": [ "# DepressionBarbu" ] }, { "cell_type": "markdown", "id": "a5b2dfff", "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": "42cecc19", "metadata": {}, "source": [ "Let's first import some packages:" ] }, { "cell_type": "code", "execution_count": 1, "id": "bfee4ece", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T20:00:46.229738Z", "iopub.status.busy": "2026-07-04T20:00:46.229459Z", "iopub.status.idle": "2026-07-04T20:00:49.765438Z", "shell.execute_reply": "2026-07-04T20:00:49.764986Z" } }, "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": "e368d8d0", "metadata": {}, "source": [ "## Instantiate model class" ] }, { "cell_type": "code", "execution_count": 2, "id": "f15496a1", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T20:00:49.767636Z", "iopub.status.busy": "2026-07-04T20:00:49.767354Z", "iopub.status.idle": "2026-07-04T20:00:49.770430Z", "shell.execute_reply": "2026-07-04T20:00:49.770010Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "class DepressionBarbu(pyagingModel):\n", " def __init__(self):\n", " super().__init__()\n", "\n", " def preprocess(self, x):\n", " return 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.DepressionBarbu)" ] }, { "cell_type": "code", "execution_count": 3, "id": "81dd476a", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T20:00:49.771697Z", "iopub.status.busy": "2026-07-04T20:00:49.771614Z", "iopub.status.idle": "2026-07-04T20:00:49.773340Z", "shell.execute_reply": "2026-07-04T20:00:49.772952Z" } }, "outputs": [], "source": [ "model = pya.models.DepressionBarbu()" ] }, { "cell_type": "markdown", "id": "34e56746", "metadata": {}, "source": [ "## Define clock metadata" ] }, { "cell_type": "code", "execution_count": 4, "id": "82fe3ca8", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T20:00:49.774676Z", "iopub.status.busy": "2026-07-04T20:00:49.774569Z", "iopub.status.idle": "2026-07-04T20:00:49.776834Z", "shell.execute_reply": "2026-07-04T20:00:49.776372Z" } }, "outputs": [], "source": [ "model.metadata[\"clock_name\"] = \"depressionbarbu\"\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\"] = 2021\n", "model.metadata[\"approved_by_author\"] = \"⌛\"\n", "model.metadata[\"citation\"] = \"Barbu, Miruna C., et al. \\\"Epigenetic prediction of major depressive disorder.\\\" Molecular Psychiatry 26.9 (2021): 5112-5123.\"\n", "model.metadata[\"doi\"] = \"https://doi.org/10.1038/s41380-020-0808-3\"\n", "model.metadata[\"notes\"] = \"Blood methylation risk score for major depressive disorder built with penalised regression on genome-wide EPIC-array CpGs, trained on over 1,200 cases and 1,800 controls. Discriminates prevalent from incident MDD independently of polygenic risk, with a smoking-independent variant also derived.\"\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\"] = [\"major depressive disorder\"] # Paper: The reported predictor output is major depressive disorder (MDD) status/risk.\n", "model.metadata[\"training_target\"] = [\"major depressive disorder\"] # Paper: The fitting outcome is major depressive disorder (MDD) status/risk.\n", "model.metadata[\"unit\"] = [\"unitless\"] # Paper: The returned construct is expressed as score (arbitrary).\n", "model.metadata[\"model_type\"] = \"elastic net regression\" # Paper: The clock was fitted using Elastic net.\n", "model.metadata[\"platform\"] = [\"Illumina EPIC\"] # Paper: Training/selection used Illumina EPIC.\n", "model.metadata[\"population\"] = \"adults\" # Paper: adults (Generation Scotland cohort)\n", "model.metadata[\"journal\"] = \"Molecular Psychiatry\"\n", "model.metadata[\"last_author\"] = \"Andrew M. McIntosh\"\n", "model.metadata[\"n_features\"] = 196\n", "model.metadata[\"citations\"] = 93\n", "model.metadata[\"citations_date\"] = \"2026-07-05\"\n" ] }, { "cell_type": "markdown", "id": "5e4d5532", "metadata": {}, "source": [ "## Download clock dependencies" ] }, { "cell_type": "code", "execution_count": 5, "id": "a42a7858", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T20:00:49.778064Z", "iopub.status.busy": "2026-07-04T20:00:49.777968Z", "iopub.status.idle": "2026-07-04T20:00:49.993181Z", "shell.execute_reply": "2026-07-04T20:00:49.992441Z" } }, "outputs": [ { "data": { "text/plain": [ "0" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "os.system(f\"curl -sL -o depressionbarbu.xlsx https://static-content.springer.com/esm/art%3A10.1038%2Fs41380-020-0808-3/MediaObjects/41380_2020_808_MOESM4_ESM.xlsx\")" ] }, { "cell_type": "markdown", "id": "3ad1b660", "metadata": {}, "source": [ "## Load features" ] }, { "cell_type": "code", "execution_count": 6, "id": "416a8640", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T20:00:49.994772Z", "iopub.status.busy": "2026-07-04T20:00:49.994633Z", "iopub.status.idle": "2026-07-04T20:00:50.099976Z", "shell.execute_reply": "2026-07-04T20:00:50.099045Z" } }, "outputs": [], "source": [ "df = pd.read_excel('depressionbarbu.xlsx', sheet_name='MRS - CpG sites and weights')\n", "model.features = df['CpG site'].tolist()" ] }, { "cell_type": "markdown", "id": "0d99f553", "metadata": {}, "source": [ "## Load weights into base model" ] }, { "cell_type": "code", "execution_count": 7, "id": "e934cab8", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T20:00:50.101935Z", "iopub.status.busy": "2026-07-04T20:00:50.101819Z", "iopub.status.idle": "2026-07-04T20:00:50.108965Z", "shell.execute_reply": "2026-07-04T20:00:50.108074Z" } }, "outputs": [], "source": [ "weights = torch.tensor(df['Beta'].tolist()).unsqueeze(0).float()\n", "# Methylation risk score calibration intercept\n", "intercept = torch.tensor([12.2169841]).float()" ] }, { "cell_type": "code", "execution_count": 8, "id": "a597cfd9", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T20:00:50.110269Z", "iopub.status.busy": "2026-07-04T20:00:50.110176Z", "iopub.status.idle": "2026-07-04T20:00:50.115858Z", "shell.execute_reply": "2026-07-04T20:00:50.115359Z" } }, "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": "d21caa34", "metadata": {}, "source": [ "## Load reference values" ] }, { "cell_type": "code", "execution_count": 9, "id": "9ab1c357", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T20:00:50.117544Z", "iopub.status.busy": "2026-07-04T20:00:50.117437Z", "iopub.status.idle": "2026-07-04T20:00:50.119228Z", "shell.execute_reply": "2026-07-04T20:00:50.118807Z" } }, "outputs": [], "source": [ "model.reference_values = None" ] }, { "cell_type": "markdown", "id": "ba0650a3", "metadata": {}, "source": [ "## Load preprocess and postprocess objects" ] }, { "cell_type": "code", "execution_count": 10, "id": "a5ee3961", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T20:00:50.120331Z", "iopub.status.busy": "2026-07-04T20:00:50.120262Z", "iopub.status.idle": "2026-07-04T20:00:50.121879Z", "shell.execute_reply": "2026-07-04T20:00:50.121512Z" } }, "outputs": [], "source": [ "model.preprocess_name = None\n", "model.preprocess_dependencies = None" ] }, { "cell_type": "code", "execution_count": 11, "id": "afd51b7b", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T20:00:50.123050Z", "iopub.status.busy": "2026-07-04T20:00:50.122968Z", "iopub.status.idle": "2026-07-04T20:00:50.124660Z", "shell.execute_reply": "2026-07-04T20:00:50.124250Z" } }, "outputs": [], "source": [ "model.postprocess_name = None\n", "model.postprocess_dependencies = None" ] }, { "cell_type": "markdown", "id": "d17ce2cd", "metadata": {}, "source": [ "## Check all clock parameters" ] }, { "cell_type": "code", "execution_count": 12, "id": "5757450e", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T20:00:50.125753Z", "iopub.status.busy": "2026-07-04T20:00:50.125691Z", "iopub.status.idle": "2026-07-04T20:00:50.145562Z", "shell.execute_reply": "2026-07-04T20:00:50.145102Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "%==================================== Model Details ====================================%\n", "Model Attributes:\n", "\n", "training: True\n", "metadata: {'approved_by_author': '⌛',\n", " 'citation': 'Barbu, Miruna C., et al. \"Epigenetic prediction of major '\n", " 'depressive disorder.\" Molecular Psychiatry 26.9 (2021): '\n", " '5112-5123.',\n", " 'clock_name': 'depressionbarbu',\n", " 'data_type': 'methylation',\n", " 'doi': 'https://doi.org/10.1038/s41380-020-0808-3',\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: None\n", "postprocess_dependencies: None\n", "features: ['cg02115394', 'cg15971980', 'cg03054303', 'cg09791621', 'cg12736206', 'cg22225420', 'cg22407822', 'cg02998018', 'cg07459409', 'cg20496693', 'cg15251779', 'cg03070922', 'cg04142555', 'cg18090197', 'cg14728380', 'cg09929180', 'cg13529291', 'cg19474047', 'cg04191989', 'cg03607825', 'cg05575921', 'cg03307749', 'cg12832498', 'cg10623600', 'cg15847996', 'cg25242471', 'cg10928544', 'cg02636222', 'cg08464831', 'cg15586193']... [Total elements: 196]\n", "base_model_features: None\n", "\n", "%==================================== Model Details ====================================%\n", "Model Structure:\n", "\n", "base_model: LinearModel(\n", " (linear): Linear(in_features=196, out_features=1, bias=True)\n", ")\n", "\n", "%==================================== Model Details ====================================%\n", "Model Parameters and Weights:\n", "\n", "base_model.linear.weight: [0.13174669444561005, 0.017368832603096962, 0.07059700042009354, -0.018956167623400688, 0.35600546002388, 0.2889443039894104, 0.0505455806851387, -0.4532535970211029, -0.2310783565044403, 0.006119664758443832, -0.07810869812965393, 0.0040334537625312805, 0.005831866059452295, 0.21422632038593292, 0.07561899721622467, 0.13462267816066742, 0.37971192598342896, -0.21138310432434082, 0.1820078045129776, 0.06565909832715988, -0.14514309167861938, -0.3468712866306305, 0.13083887100219727, 0.03079209290444851, -0.06906116753816605, 0.11012744158506393, 0.01911790855228901, 0.5892500281333923, -0.025107210502028465, -0.22120502591133118]... [Tensor of shape torch.Size([1, 196])]\n", "base_model.linear.bias: tensor([12.2170])\n", "\n", "%==================================== Model Details ====================================%\n", "\n" ] } ], "source": [ "pya.utils.print_model_details(model)" ] }, { "cell_type": "markdown", "id": "fca627cb", "metadata": {}, "source": [ "## Basic test" ] }, { "cell_type": "code", "execution_count": 13, "id": "739f2cc9", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T20:00:50.146806Z", "iopub.status.busy": "2026-07-04T20:00:50.146730Z", "iopub.status.idle": "2026-07-04T20:00:50.156509Z", "shell.execute_reply": "2026-07-04T20:00:50.156056Z" } }, "outputs": [ { "data": { "text/plain": [ "tensor([[11.7817],\n", " [12.6549],\n", " [11.4798],\n", " [12.3791],\n", " [11.4735],\n", " [ 7.2415],\n", " [18.4420],\n", " [14.8113],\n", " [14.2788],\n", " [13.7944]], 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": "2edd337e", "metadata": {}, "source": [ "## Save torch model" ] }, { "cell_type": "code", "execution_count": 14, "id": "6465d933", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T20:00:50.157819Z", "iopub.status.busy": "2026-07-04T20:00:50.157737Z", "iopub.status.idle": "2026-07-04T20:00:50.162507Z", "shell.execute_reply": "2026-07-04T20:00:50.162039Z" } }, "outputs": [], "source": [ "torch.save(model, f\"../weights/{model.metadata['clock_name']}.pt\")" ] }, { "cell_type": "markdown", "id": "c873651b", "metadata": {}, "source": [ "## Clear directory\n", "" ] }, { "cell_type": "code", "execution_count": 15, "id": "7d926633", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T20:00:50.163947Z", "iopub.status.busy": "2026-07-04T20:00:50.163863Z", "iopub.status.idle": "2026-07-04T20:00:50.167522Z", "shell.execute_reply": "2026-07-04T20:00:50.167065Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Deleted file: depressionbarbu.xlsx\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 }