{ "cells": [ { "cell_type": "markdown", "id": "2f04eee0-5928-4e74-a754-6dc2e528810c", "metadata": {}, "source": [ "# Han" ] }, { "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:46.692625Z", "iopub.status.busy": "2025-04-07T17:51:46.692299Z", "iopub.status.idle": "2025-04-07T17:51:48.095262Z", "shell.execute_reply": "2025-04-07T17:51:48.094929Z" } }, "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:48.097001Z", "iopub.status.busy": "2025-04-07T17:51:48.096775Z", "iopub.status.idle": "2025-04-07T17:51:48.104151Z", "shell.execute_reply": "2025-04-07T17:51:48.103862Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "class Han(pyagingModel):\n", " def __init__(self):\n", " super().__init__()\n", "\n", " def preprocess(self, x):\n", " return x\n", "\n", " def postprocess(self, x):\n", " \"\"\"\n", " Applies an anti-logarithmic linear transformation to a PyTorch tensor.\n", " \"\"\"\n", " adult_age = 20\n", "\n", " # Create a mask for negative and non-negative values\n", " mask_negative = x < 0\n", " mask_non_negative = ~mask_negative\n", "\n", " # Initialize the result tensor\n", " age_tensor = torch.empty_like(x)\n", "\n", " # Exponential transformation for negative values\n", " age_tensor[mask_negative] = (1 + adult_age) * torch.exp(x[mask_negative]) - 1\n", "\n", " # Linear transformation for non-negative values\n", " age_tensor[mask_non_negative] = (1 + adult_age) * x[mask_non_negative] + adult_age\n", "\n", " return age_tensor\n", "\n" ] } ], "source": [ "def print_entire_class(cls):\n", " source = inspect.getsource(cls)\n", " print(source)\n", "\n", "print_entire_class(pya.models.Han)" ] }, { "cell_type": "code", "execution_count": 3, "id": "78536494-f1d9-44de-8583-c89a310d2307", "metadata": { "execution": { "iopub.execute_input": "2025-04-07T17:51:48.105530Z", "iopub.status.busy": "2025-04-07T17:51:48.105441Z", "iopub.status.idle": "2025-04-07T17:51:48.107139Z", "shell.execute_reply": "2025-04-07T17:51:48.106894Z" } }, "outputs": [], "source": [ "model = pya.models.Han()" ] }, { "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:48.108426Z", "iopub.status.busy": "2025-04-07T17:51:48.108344Z", "iopub.status.idle": "2025-04-07T17:51:48.110326Z", "shell.execute_reply": "2025-04-07T17:51:48.110083Z" } }, "outputs": [], "source": [ "model.metadata[\"clock_name\"] = \"han\"\n", "model.metadata[\"data_type\"] = \"DNA methylation\" # Paper: The predictor uses CpG DNA methylation beta values.\n", "model.metadata[\"species\"] = \"Homo sapiens\" # Paper: The training data consisted of healthy human blood samples.\n", "model.metadata[\"year\"] = 2020\n", "model.metadata[\"approved_by_author\"] = \"βœ…\"\n", "model.metadata[\"citation\"] = \"Han, Y., et al. β€œNew targeted approaches for epigenetic age predictions.” BMC Biology 18, 71 (2020).\"\n", "model.metadata[\"doi\"] = \"https://doi.org/10.1186/s12915-020-00807-2\"\n", "model.metadata[\"notes\"] = \"Whole-blood 65-CpG multivariable linear age predictor selected for robust targeted measurement; it fits Horvath-transformed chronological age and inverse-transforms the return to years.\"\n", "model.metadata[\"research_only\"] = None\n", "model.metadata[\"tissue\"] = [\"whole blood\"] # Paper: The 973-sample training set consisted of healthy human blood profiles.\n", "model.metadata[\"predicts\"] = [\"chronological age\"] # Paper: The model provides epigenetic predictions of chronological age.\n", "model.metadata[\"training_target\"] = [\"chronological age\"] # Paper: Before fitting the multivariable model, chronological age was transformed with adult.age set to 20 years.\n", "model.metadata[\"unit\"] = [\"years\"] # Paper: pyaging applies the inverse Horvath age transformation and returns age in years.\n", "model.metadata[\"model_type\"] = \"linear regression\" # Paper: A multivariable regression model was established from the 65 CpGs; no penalized selection was used for this 65-CpG microarray model.\n", "model.metadata[\"platform\"] = [\"Illumina 450K\"] # Paper: The 973 training profiles came from seven studies measured on the 450K Illumina BeadChip.\n", "model.metadata[\"population\"] = \"all ages\" # Paper: The training set comprised 973 healthy blood profiles from donors aged 1 to 101 years.\n", "model.metadata[\"journal\"] = \"BMC Biology\"\n", "model.metadata[\"last_author\"] = \"Wolfgang Wagner\"\n", "model.metadata[\"n_features\"] = 65\n", "model.metadata[\"citations\"] = 102\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": "code", "execution_count": 5, "id": "f1965587-a6ac-47ce-bd7a-bb98ca1d91b5", "metadata": { "execution": { "iopub.execute_input": "2025-04-07T17:51:48.111621Z", "iopub.status.busy": "2025-04-07T17:51:48.111529Z", "iopub.status.idle": "2025-04-07T17:51:48.115249Z", "shell.execute_reply": "2025-04-07T17:51:48.114982Z" } }, "outputs": [], "source": [ "# from authors\n", "cpg_sites = [\n", " \"(Intercept)\",\n", " 'cg19283806',\n", " 'cg11807280',\n", " 'cg00329615',\n", " 'cg22454769',\n", " 'cg16867657',\n", " 'cg22796704',\n", " 'cg09809672',\n", " 'cg18618815',\n", " 'cg25533247',\n", " 'cg02286081',\n", " 'cg20222376',\n", " 'cg19344626',\n", " 'cg07082267',\n", " 'cg15845821',\n", " 'cg11741201',\n", " 'cg16054275',\n", " 'cg18933331',\n", " 'cg20249566',\n", " 'cg16604658',\n", " 'cg07583137',\n", " 'cg16008966',\n", " 'cg14556683',\n", " 'cg03746976',\n", " 'cg14314729',\n", " 'cg03431918',\n", " 'cg22156456',\n", " 'cg23078123',\n", " 'cg09748749',\n", " 'cg17457912',\n", " 'cg06492796',\n", " 'cg17593342',\n", " 'cg05308819',\n", " 'cg22512670',\n", " 'cg01820962',\n", " 'cg06639320',\n", " 'cg03224418',\n", " 'cg17436656',\n", " 'cg19500607',\n", " 'cg03735592',\n", " 'cg20669012',\n", " 'cg19761273',\n", " 'cg07080372',\n", " 'cg03638795',\n", " 'cg19722847',\n", " 'cg24711336',\n", " 'cg26935102',\n", " 'cg10221746',\n", " 'cg02085953',\n", " 'cg04604946',\n", " 'cg08558886',\n", " 'cg22361181',\n", " 'cg04208403',\n", " 'cg12623930',\n", " 'cg21572722',\n", " 'cg17885226',\n", " 'cg00748589',\n", " 'cg13033938',\n", " 'cg19784428',\n", " 'cg22016779',\n", " 'cg01974375',\n", " 'cg25256723',\n", " 'cg24724428',\n", " 'cg07547549',\n", " 'cg25410668',\n", " 'cg21296230'\n", "]\n", "\n", "coefficients = [\n", " 0.711184864,\n", " -0.588354066,\n", " -0.212038592,\n", " 0.014351188,\n", " 0.051285529,\n", " 2.152191741,\n", " -0.689940565,\n", " -0.643729974,\n", " -0.772516118,\n", " 0.116662569,\n", " -0.233409678,\n", " 0.002802259,\n", " -0.062172432,\n", " -0.224027294,\n", " 1.535209377,\n", " 0.344367661,\n", " 0.188826525,\n", " -0.409150014,\n", " -0.776065004,\n", " 0.500336643,\n", " 0.06125005,\n", " -0.391624093,\n", " 0.100449175,\n", " 0.02000403,\n", " 0.266044453,\n", " -0.259829677,\n", " 0.254063071,\n", " -0.726178338,\n", " -1.141947121,\n", " -0.06322441,\n", " -0.196926134,\n", " 0.85613244,\n", " -0.887977059,\n", " -0.334654336,\n", " -0.854110638,\n", " 1.916122401,\n", " 0.92208575,\n", " -0.070665617,\n", " 0.524707402,\n", " 0.319375235,\n", " 0.376055859,\n", " 0.033361038,\n", " -1.458360975,\n", " -0.267930475,\n", " -0.590085273,\n", " 0.642506165,\n", " 0.470352872,\n", " 0.273581649,\n", " -0.637989789,\n", " -1.109388991,\n", " -0.16886654,\n", " 0.662451226,\n", " -0.091891613,\n", " 0.086290028,\n", " -0.426089316,\n", " 0.32615363,\n", " 2.535639458,\n", " -3.626802894,\n", " 0.097619541,\n", " -0.427604263,\n", " -0.41418774,\n", " -0.27412342,\n", " 0.703772384,\n", " -0.110027226,\n", " 0.283649813,\n", " 0.928585964\n", "]" ] }, { "cell_type": "markdown", "id": "5035b180-3d1b-4432-8ebe-b9c92bd93a7f", "metadata": {}, "source": [ "## Load features" ] }, { "cell_type": "code", "execution_count": 6, "id": "77face1a-b58f-4f8f-9fe8-1f12037be99a", "metadata": { "execution": { "iopub.execute_input": "2025-04-07T17:51:48.116554Z", "iopub.status.busy": "2025-04-07T17:51:48.116472Z", "iopub.status.idle": "2025-04-07T17:51:48.118568Z", "shell.execute_reply": "2025-04-07T17:51:48.118323Z" } }, "outputs": [], "source": [ "df = pd.DataFrame({\n", " 'feature': cpg_sites,\n", " 'coefficient': coefficients\n", "})\n", "model.features = df['feature'][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:48.119823Z", "iopub.status.busy": "2025-04-07T17:51:48.119742Z", "iopub.status.idle": "2025-04-07T17:51:48.121663Z", "shell.execute_reply": "2025-04-07T17:51:48.121431Z" } }, "outputs": [], "source": [ "weights = torch.tensor(df['coefficient'][1:].tolist()).unsqueeze(0)\n", "intercept = torch.tensor([df['coefficient'][0]])" ] }, { "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:48.123025Z", "iopub.status.busy": "2025-04-07T17:51:48.122941Z", "iopub.status.idle": "2025-04-07T17:51:48.125057Z", "shell.execute_reply": "2025-04-07T17:51:48.124808Z" } }, "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:48.126363Z", "iopub.status.busy": "2025-04-07T17:51:48.126279Z", "iopub.status.idle": "2025-04-07T17:51:48.127744Z", "shell.execute_reply": "2025-04-07T17:51:48.127481Z" } }, "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:48.129005Z", "iopub.status.busy": "2025-04-07T17:51:48.128920Z", "iopub.status.idle": "2025-04-07T17:51:48.130258Z", "shell.execute_reply": "2025-04-07T17:51:48.130035Z" } }, "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:48.131493Z", "iopub.status.busy": "2025-04-07T17:51:48.131394Z", "iopub.status.idle": "2025-04-07T17:51:48.132907Z", "shell.execute_reply": "2025-04-07T17:51:48.132639Z" } }, "outputs": [], "source": [ "model.postprocess_name = 'anti_log_linear'\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:48.134236Z", "iopub.status.busy": "2025-04-07T17:51:48.134147Z", "iopub.status.idle": "2025-04-07T17:51:48.137401Z", "shell.execute_reply": "2025-04-07T17:51:48.137169Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "%==================================== Model Details ====================================%\n", "Model Attributes:\n", "\n", "training: True\n", "metadata: {'approved_by_author': 'βœ…',\n", " 'citation': 'Han, Yang, et al. \"New targeted approaches for epigenetic age '\n", " 'predictions.\" BMC biology 18 (2020): 1-15.',\n", " 'clock_name': 'han',\n", " 'data_type': 'methylation',\n", " 'doi': 'https://doi.org/10.1186/s12915-020-00807-2',\n", " 'notes': None,\n", " 'research_only': None,\n", " 'species': 'Homo sapiens',\n", " 'version': None,\n", " 'year': 2020}\n", "reference_values: None\n", "preprocess_name: None\n", "preprocess_dependencies: None\n", "postprocess_name: 'anti_log_linear'\n", "postprocess_dependencies: None\n", "features: ['cg19283806', 'cg11807280', 'cg00329615', 'cg22454769', 'cg16867657', 'cg22796704', 'cg09809672', 'cg18618815', 'cg25533247', 'cg02286081', 'cg20222376', 'cg19344626', 'cg07082267', 'cg15845821', 'cg11741201', 'cg16054275', 'cg18933331', 'cg20249566', 'cg16604658', 'cg07583137', 'cg16008966', 'cg14556683', 'cg03746976', 'cg14314729', 'cg03431918', 'cg22156456', 'cg23078123', 'cg09748749', 'cg17457912', 'cg06492796']... [Total elements: 65]\n", "base_model_features: None\n", "\n", "%==================================== Model Details ====================================%\n", "Model Structure:\n", "\n", "base_model: LinearModel(\n", " (linear): Linear(in_features=65, out_features=1, bias=True)\n", ")\n", "\n", "%==================================== Model Details ====================================%\n", "Model Parameters and Weights:\n", "\n", "base_model.linear.weight: [-0.5883540511131287, -0.21203859150409698, 0.014351188205182552, 0.051285527646541595, 2.152191638946533, -0.6899405717849731, -0.6437299847602844, -0.772516131401062, 0.11666256934404373, -0.23340967297554016, 0.0028022590558975935, -0.062172431498765945, -0.22402729094028473, 1.5352094173431396, 0.344367653131485, 0.1888265311717987, -0.40915000438690186, -0.7760649919509888, 0.5003366470336914, 0.061250049620866776, -0.3916240930557251, 0.10044917464256287, 0.0200040303170681, 0.2660444676876068, -0.25982967019081116, 0.25406306982040405, -0.7261783480644226, -1.1419471502304077, -0.0632244125008583, -0.19692613184452057]... [Tensor of shape torch.Size([1, 65])]\n", "base_model.linear.bias: tensor([0.7112])\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:48.138789Z", "iopub.status.busy": "2025-04-07T17:51:48.138674Z", "iopub.status.idle": "2025-04-07T17:51:48.144871Z", "shell.execute_reply": "2025-04-07T17:51:48.144611Z" } }, "outputs": [ { "data": { "text/plain": [ "tensor([[ -1.0000],\n", " [ 2.2557],\n", " [310.9586],\n", " [ 4.2231],\n", " [ -0.7209],\n", " [161.4707],\n", " [ -0.7927],\n", " [557.5405],\n", " [ 0.8421],\n", " [ -0.9992]], 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:48.146102Z", "iopub.status.busy": "2025-04-07T17:51:48.146014Z", "iopub.status.idle": "2025-04-07T17:51:48.149610Z", "shell.execute_reply": "2025-04-07T17:51:48.149356Z" } }, "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:48.150930Z", "iopub.status.busy": "2025-04-07T17:51:48.150845Z", "iopub.status.idle": "2025-04-07T17:51:48.153522Z", "shell.execute_reply": "2025-04-07T17:51:48.153219Z" } }, "outputs": [], "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 }