{ "cells": [ { "cell_type": "markdown", "id": "2f04eee0-5928-4e74-a754-6dc2e528810c", "metadata": {}, "source": [ "# ENCen40" ] }, { "cell_type": "markdown", "id": "a3f514a3-772c-4a14-afdf-5a8376851ff4", "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": "d95fafdc-643a-40ea-a689-200bd132e90c", "metadata": {}, "source": [ "Let's first import some packages:" ] }, { "cell_type": "code", "execution_count": 1, "id": "4adfb4de-cd79-4913-a1af-9e23e9e236c9", "metadata": { "execution": { "iopub.execute_input": "2025-04-07T17:51:27.959555Z", "iopub.status.busy": "2025-04-07T17:51:27.959401Z", "iopub.status.idle": "2025-04-07T17:51:29.371268Z", "shell.execute_reply": "2025-04-07T17:51:29.370912Z" } }, "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": "145082e5-ced4-47ae-88c0-cb69773e3c5a", "metadata": {}, "source": [ "## Instantiate model class" ] }, { "cell_type": "code", "execution_count": 2, "id": "8aa77372-7ed3-4da7-abc9-d30372106139", "metadata": { "execution": { "iopub.execute_input": "2025-04-07T17:51:29.373042Z", "iopub.status.busy": "2025-04-07T17:51:29.372813Z", "iopub.status.idle": "2025-04-07T17:51:29.379919Z", "shell.execute_reply": "2025-04-07T17:51:29.379636Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "class ENCen40(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.ENCen40)" ] }, { "cell_type": "code", "execution_count": 3, "id": "78536494-f1d9-44de-8583-c89a310d2307", "metadata": { "execution": { "iopub.execute_input": "2025-04-07T17:51:29.381229Z", "iopub.status.busy": "2025-04-07T17:51:29.381147Z", "iopub.status.idle": "2025-04-07T17:51:29.382807Z", "shell.execute_reply": "2025-04-07T17:51:29.382555Z" } }, "outputs": [], "source": [ "model = pya.models.ENCen40()" ] }, { "cell_type": "markdown", "id": "51f8615e-01fa-4aa5-b196-3ee2b35d261c", "metadata": {}, "source": [ "## Define clock metadata" ] }, { "cell_type": "code", "execution_count": 4, "id": "6601da9e-8adc-44ee-9308-75e3cd31b816", "metadata": { "execution": { "iopub.execute_input": "2025-04-07T17:51:29.384092Z", "iopub.status.busy": "2025-04-07T17:51:29.384001Z", "iopub.status.idle": "2025-04-07T17:51:29.386007Z", "shell.execute_reply": "2025-04-07T17:51:29.385738Z" } }, "outputs": [], "source": [ "model.metadata[\"clock_name\"] = \"encen40\"\n", "model.metadata[\"data_type\"] = \"DNA methylation\" # Paper: DNA methylation clocks\n", "model.metadata[\"species\"] = \"Homo sapiens\" # Paper: study individuals\n", "model.metadata[\"year\"] = 2023\n", "model.metadata[\"approved_by_author\"] = \"⌛\"\n", "model.metadata[\"citation\"] = \"Dec, Eric, et al. \\\"Centenarian clocks: epigenetic clocks for validating claims of exceptional longevity.\\\" GeroScience 45 (2023): 1817–1835.\"\n", "model.metadata[\"doi\"] = \"https://doi.org/10.1007/s11357-023-00731-7\"\n", "model.metadata[\"notes\"] = \"Elastic-net DNAm-age clock trained in 7,039 people aged 40–115, including centenarians, to reduce extreme-old-age underestimation.\"\n", "model.metadata[\"research_only\"] = None\n", "model.metadata[\"tissue\"] = [\"whole blood\", \"saliva\", \"buccal epithelium\"] # Paper: blood, saliva, and buccals cells\n", "model.metadata[\"predicts\"] = [\"chronological age\"] # Paper: DNAm age was defined as predicted age\n", "model.metadata[\"training_target\"] = [\"chronological age\"] # Paper: regress chronological age on the CpG probes\n", "model.metadata[\"unit\"] = [\"years\"] # Paper: individuals aged 40 years or older; predicted age\n", "model.metadata[\"model_type\"] = \"elastic net regression\" # Paper: alpha parameter ... 0.5 (elastic net regression)\n", "model.metadata[\"platform\"] = [\"Illumina 450K\", \"Illumina EPIC\"] # Paper: Infinium 450 K array and the Infinium methylation EPIC beadchip\n", "model.metadata[\"population\"] = \"older adults\" # Paper: Age ≥40: N=7039, age range [40,115]\n", "model.metadata[\"journal\"] = \"GeroScience\"\n", "model.metadata[\"last_author\"] = \"Steve Horvath\"\n", "model.metadata[\"n_features\"] = 559\n", "model.metadata[\"citations\"] = 45\n", "model.metadata[\"citations_date\"] = \"2026-07-05\"\n" ] }, { "cell_type": "markdown", "id": "74492239-5aae-4026-9d90-6bc9c574c110", "metadata": {}, "source": [ "## Download clock dependencies" ] }, { "cell_type": "markdown", "id": "1472c242-cf7b-4c26-b28a-dae2dcb69106", "metadata": {}, "source": [ "#### Download GitHub repository" ] }, { "cell_type": "code", "execution_count": 5, "id": "51e8a008-833e-4499-a06f-32638c775821", "metadata": { "execution": { "iopub.execute_input": "2025-04-07T17:51:29.387406Z", "iopub.status.busy": "2025-04-07T17:51:29.387321Z", "iopub.status.idle": "2025-04-07T17:51:30.211443Z", "shell.execute_reply": "2025-04-07T17:51:30.210909Z" } }, "outputs": [ { "data": { "text/plain": [ "0" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "github_url = \"https://github.com/victorychain/Centenarian-Clock.git\"\n", "github_folder_name = github_url.split('/')[-1].split('.')[0]\n", "os.system(f\"git clone {github_url}\")" ] }, { "cell_type": "markdown", "id": "5035b180-3d1b-4432-8ebe-b9c92bd93a7f", "metadata": {}, "source": [ "## Load features" ] }, { "cell_type": "markdown", "id": "15f4af76-b93c-438c-b57f-f129d6e9ec99", "metadata": {}, "source": [ "#### From CSV file" ] }, { "cell_type": "code", "execution_count": 6, "id": "8a3d5de6-6303-487a-8b4d-e6345792f7be", "metadata": { "execution": { "iopub.execute_input": "2025-04-07T17:51:30.213455Z", "iopub.status.busy": "2025-04-07T17:51:30.213299Z", "iopub.status.idle": "2025-04-07T17:51:30.499029Z", "shell.execute_reply": "2025-04-07T17:51:30.498648Z" } }, "outputs": [], "source": [ "df = pd.read_csv('Centenarian-Clock/clocks/final_clocks.csv', index_col=0).T\n", "df = df[df['ENCen40+'] != 0]\n", "df = df.reset_index()\n", "\n", "model.features = df['index'][1:].tolist()" ] }, { "cell_type": "markdown", "id": "ee6d8fa0-4767-4c45-9717-eb1c95e2ddc0", "metadata": {}, "source": [ "## Load weights into base model" ] }, { "cell_type": "code", "execution_count": 7, "id": "e09b3463-4fd4-41b1-ac21-e63ddd223fe0", "metadata": { "execution": { "iopub.execute_input": "2025-04-07T17:51:30.500648Z", "iopub.status.busy": "2025-04-07T17:51:30.500558Z", "iopub.status.idle": "2025-04-07T17:51:30.502758Z", "shell.execute_reply": "2025-04-07T17:51:30.502486Z" } }, "outputs": [], "source": [ "weights = torch.tensor(df['ENCen40+'][1:].tolist()).unsqueeze(0).float()\n", "intercept = torch.tensor([df['ENCen40+'][0]]).float()" ] }, { "cell_type": "markdown", "id": "ad261636-5b00-4979-bb1d-67a851f7aa19", "metadata": {}, "source": [ "#### Linear model" ] }, { "cell_type": "code", "execution_count": 8, "id": "d7f43b99-26f2-4622-9a76-316712058877", "metadata": { "execution": { "iopub.execute_input": "2025-04-07T17:51:30.504113Z", "iopub.status.busy": "2025-04-07T17:51:30.504020Z", "iopub.status.idle": "2025-04-07T17:51:30.506065Z", "shell.execute_reply": "2025-04-07T17:51:30.505785Z" } }, "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": "ad8b4c1d-9d57-48b7-9a30-bcfea7b747b1", "metadata": {}, "source": [ "## Load reference values" ] }, { "cell_type": "code", "execution_count": 9, "id": "ade0f4c9-2298-4fc3-bb72-d200907dd731", "metadata": { "execution": { "iopub.execute_input": "2025-04-07T17:51:30.507416Z", "iopub.status.busy": "2025-04-07T17:51:30.507335Z", "iopub.status.idle": "2025-04-07T17:51:30.508816Z", "shell.execute_reply": "2025-04-07T17:51:30.508554Z" } }, "outputs": [], "source": [ "model.reference_values = None" ] }, { "cell_type": "markdown", "id": "af3bcf7b-74a8-4d21-9ccb-4de0c2b0516b", "metadata": {}, "source": [ "## Load preprocess and postprocess objects" ] }, { "cell_type": "code", "execution_count": 10, "id": "7a22fb20-c605-424d-8efb-7620c2c0755c", "metadata": { "execution": { "iopub.execute_input": "2025-04-07T17:51:30.510078Z", "iopub.status.busy": "2025-04-07T17:51:30.509989Z", "iopub.status.idle": "2025-04-07T17:51:30.511481Z", "shell.execute_reply": "2025-04-07T17:51:30.511235Z" } }, "outputs": [], "source": [ "model.preprocess_name = None\n", "model.preprocess_dependencies = None" ] }, { "cell_type": "code", "execution_count": 11, "id": "ff4a21cb-cf41-44dc-9ed1-95cf8aa15772", "metadata": { "execution": { "iopub.execute_input": "2025-04-07T17:51:30.512719Z", "iopub.status.busy": "2025-04-07T17:51:30.512626Z", "iopub.status.idle": "2025-04-07T17:51:30.514088Z", "shell.execute_reply": "2025-04-07T17:51:30.513850Z" } }, "outputs": [], "source": [ "model.postprocess_name = None\n", "model.postprocess_dependencies = None" ] }, { "cell_type": "markdown", "id": "86e3d6b1-e67e-4f3d-bd39-0ebec5726c3c", "metadata": {}, "source": [ "## Check all clock parameters" ] }, { "cell_type": "code", "execution_count": 12, "id": "2168355c-47d9-475d-b816-49f65e74887c", "metadata": { "execution": { "iopub.execute_input": "2025-04-07T17:51:30.515334Z", "iopub.status.busy": "2025-04-07T17:51:30.515251Z", "iopub.status.idle": "2025-04-07T17:51:30.518525Z", "shell.execute_reply": "2025-04-07T17:51:30.518301Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "%==================================== Model Details ====================================%\n", "Model Attributes:\n", "\n", "training: True\n", "metadata: {'approved_by_author': '⌛',\n", " 'citation': 'Dec, Eric, et al. \"Centenarian clocks: epigenetic clocks for '\n", " 'validating claims of exceptional longevity.\" GeroScience (2023): '\n", " '1-19.',\n", " 'clock_name': 'encen40',\n", " 'data_type': 'methylation',\n", " 'doi': 'https://doi.org/10.1007/s11357-023-00731-7',\n", " 'notes': None,\n", " 'research_only': None,\n", " 'species': 'Homo sapiens',\n", " 'version': None,\n", " 'year': 2023}\n", "reference_values: None\n", "preprocess_name: None\n", "preprocess_dependencies: None\n", "postprocess_name: None\n", "postprocess_dependencies: None\n", "features: ['cg06437747', 'cg12140144', 'cg17804348', 'cg18025409', 'cg17489312', 'cg15952725', 'cg20822990', 'cg09195271', 'cg18107827', 'cg14333454', 'cg08169949', 'cg25410668', 'cg15554401', 'cg04299080', 'cg12065799', 'cg23093580', 'cg07388493', 'cg13806070', 'cg03664992', 'cg12671744', 'cg26881761', 'cg06784991', 'cg14614643', 'cg06836772', 'cg13786801', 'cg10628205', 'cg12079303', 'cg21912203', 'cg24871743', 'cg09118625']... [Total elements: 559]\n", "base_model_features: None\n", "\n", "%==================================== Model Details ====================================%\n", "Model Structure:\n", "\n", "base_model: LinearModel(\n", " (linear): Linear(in_features=559, out_features=1, bias=True)\n", ")\n", "\n", "%==================================== Model Details ====================================%\n", "Model Parameters and Weights:\n", "\n", "base_model.linear.weight: [-2.098886489868164, 2.474569320678711, 0.13260692358016968, 1.0926762819290161, 2.006046772003174, -4.083102703094482, -5.88031530380249, 0.3561629354953766, -0.08976617455482483, 0.7154412865638733, 0.35456162691116333, 3.4960098266601562, 2.4149603843688965, -0.35807767510414124, -0.29285097122192383, -0.7337096929550171, -1.02739679813385, 3.951477289199829, 1.4296942949295044, -0.4479224681854248, -0.12719875574111938, 2.4558544158935547, -0.19818300008773804, -0.5594075322151184, -0.8976538181304932, -0.8460569381713867, -4.011434555053711, 2.74596905708313, 3.6343932151794434, 0.11721660196781158]... [Tensor of shape torch.Size([1, 559])]\n", "base_model.linear.bias: tensor([33.3240])\n", "\n", "%==================================== Model Details ====================================%\n", "\n" ] } ], "source": [ "pya.utils.print_model_details(model)" ] }, { "cell_type": "markdown", "id": "986d0262-e0c7-4036-b687-dee53ba392fb", "metadata": {}, "source": [ "## Basic test" ] }, { "cell_type": "code", "execution_count": 13, "id": "936b9877-d076-4ced-99aa-e8d4c58c5caf", "metadata": { "execution": { "iopub.execute_input": "2025-04-07T17:51:30.519903Z", "iopub.status.busy": "2025-04-07T17:51:30.519790Z", "iopub.status.idle": "2025-04-07T17:51:30.523577Z", "shell.execute_reply": "2025-04-07T17:51:30.523315Z" } }, "outputs": [ { "data": { "text/plain": [ "tensor([[ 53.0407],\n", " [109.0838],\n", " [ 9.5339],\n", " [ 91.3573],\n", " [ -9.1270],\n", " [100.4752],\n", " [ 43.2027],\n", " [ 84.4988],\n", " [-64.7365],\n", " [110.1437]], 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": "fe8299d7-9285-4e22-82fd-b664434b4369", "metadata": {}, "source": [ "## Save torch model" ] }, { "cell_type": "code", "execution_count": 14, "id": "5ef2fa8d-c80b-4fdd-8555-79c0d541788e", "metadata": { "execution": { "iopub.execute_input": "2025-04-07T17:51:30.524882Z", "iopub.status.busy": "2025-04-07T17:51:30.524798Z", "iopub.status.idle": "2025-04-07T17:51:30.527390Z", "shell.execute_reply": "2025-04-07T17:51:30.527097Z" } }, "outputs": [], "source": [ "torch.save(model, f\"../weights/{model.metadata['clock_name']}.pt\")" ] }, { "cell_type": "markdown", "id": "bac6257b-8d08-4a90-8d0b-7f745dc11ac1", "metadata": {}, "source": [ "## Clear directory\n", "" ] }, { "cell_type": "code", "execution_count": 15, "id": "11aeaa70-44c0-42f9-86d7-740e3849a7a6", "metadata": { "execution": { "iopub.execute_input": "2025-04-07T17:51:30.528690Z", "iopub.status.busy": "2025-04-07T17:51:30.528605Z", "iopub.status.idle": "2025-04-07T17:51:30.535583Z", "shell.execute_reply": "2025-04-07T17:51:30.535294Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Deleted folder: Centenarian-Clock\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", "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.9.7" } }, "nbformat": 4, "nbformat_minor": 5 }