{ "cells": [ { "cell_type": "markdown", "id": "2f04eee0-5928-4e74-a754-6dc2e528810c", "metadata": {}, "source": [ "# ZhangEN" ] }, { "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": "2024-03-05T21:24:38.968769Z", "iopub.status.busy": "2024-03-05T21:24:38.968325Z", "iopub.status.idle": "2024-03-05T21:24:40.288545Z", "shell.execute_reply": "2024-03-05T21:24:40.288251Z" } }, "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": "2024-03-05T21:24:40.290458Z", "iopub.status.busy": "2024-03-05T21:24:40.290301Z", "iopub.status.idle": "2024-03-05T21:24:40.299737Z", "shell.execute_reply": "2024-03-05T21:24:40.299463Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "class ZhangEN(pyagingModel):\n", " def __init__(self):\n", " super().__init__()\n", "\n", " def preprocess(self, x):\n", " \"\"\"\n", " Scales the input PyTorch tensor per row with mean 0 and std 1.\n", " \"\"\"\n", " row_means = torch.mean(x, dim=1, keepdim=True)\n", " row_stds = torch.std(x, dim=1, keepdim=True)\n", "\n", " # Avoid division by zero in case of a row with constant value\n", " row_stds = torch.where(row_stds == 0, torch.ones_like(row_stds), row_stds)\n", "\n", " x_scaled = (x - row_means) / row_stds\n", " return x_scaled\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.ZhangEN)" ] }, { "cell_type": "code", "execution_count": 3, "id": "78536494-f1d9-44de-8583-c89a310d2307", "metadata": { "execution": { "iopub.execute_input": "2024-03-05T21:24:40.301208Z", "iopub.status.busy": "2024-03-05T21:24:40.301125Z", "iopub.status.idle": "2024-03-05T21:24:40.302839Z", "shell.execute_reply": "2024-03-05T21:24:40.302612Z" } }, "outputs": [], "source": [ "model = pya.models.ZhangEN()" ] }, { "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": "2024-03-05T21:24:40.304285Z", "iopub.status.busy": "2024-03-05T21:24:40.304210Z", "iopub.status.idle": "2024-03-05T21:24:40.306066Z", "shell.execute_reply": "2024-03-05T21:24:40.305835Z" } }, "outputs": [], "source": [ "model.metadata[\"clock_name\"] = \"zhangen\"\n", "model.metadata[\"data_type\"] = \"DNA methylation\" # Paper: The predictors use DNA-methylation beta values.\n", "model.metadata[\"species\"] = \"Homo sapiens\" # Paper: The training collection comprises human blood and saliva cohorts.\n", "model.metadata[\"year\"] = 2019\n", "model.metadata[\"approved_by_author\"] = \"⌛\"\n", "model.metadata[\"citation\"] = \"Zhang, Q., Vallerga, C.L., Walker, R.M. et al. Improved precision of epigenetic clock estimates across tissues and its implication for biological ageing. Genome Medicine 11, 54 (2019).\"\n", "model.metadata[\"doi\"] = \"https://doi.org/10.1186/s13073-019-0667-1\"\n", "model.metadata[\"notes\"] = \"Elastic-net chronological-age predictor released from the largest multi-cohort training set, using 514 selected CpGs from predominantly blood plus saliva data.\"\n", "model.metadata[\"research_only\"] = None\n", "model.metadata[\"tissue\"] = [\"whole blood\", \"saliva\"] # Paper: The assembled training collection contained 13,402 blood and 259 saliva samples.\n", "model.metadata[\"predicts\"] = [\"chronological age\"] # Paper: Both released predictors estimate chronological age.\n", "model.metadata[\"training_target\"] = [\"chronological age\"] # Paper: Chronological age was the regression outcome.\n", "model.metadata[\"unit\"] = [\"years\"] # Paper: Predicted chronological age and prediction error are reported in years.\n", "model.metadata[\"model_type\"] = \"elastic net regression\" # Paper: Elastic net was used to select and shrink CpG effects for the released predictor.\n", "model.metadata[\"platform\"] = [\"Illumina 450K\", \"Illumina EPIC\"] # Paper: The 14 training cohorts were measured on HumanMethylation450 and EPIC arrays.\n", "model.metadata[\"population\"] = \"all ages\" # Paper: The assembled 14-cohort collection spanned ages 2–104 years.\n", "model.metadata[\"journal\"] = \"Genome Medicine\"\n", "model.metadata[\"last_author\"] = \"Peter M. Visscher\"\n", "model.metadata[\"n_features\"] = 514\n", "model.metadata[\"citations\"] = 519\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": "c2844c3a-8e78-48bd-95b5-9301c8b46012", "metadata": {}, "source": [ "#### Download GitHub repository" ] }, { "cell_type": "code", "execution_count": 5, "id": "0ee560a3-8ab6-4202-b8b2-cebf75089bfb", "metadata": { "execution": { "iopub.execute_input": "2024-03-05T21:24:40.307533Z", "iopub.status.busy": "2024-03-05T21:24:40.307459Z", "iopub.status.idle": "2024-03-05T21:24:46.698055Z", "shell.execute_reply": "2024-03-05T21:24:46.697761Z" } }, "outputs": [ { "data": { "text/plain": [ "0" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "github_url = \"https://github.com/qzhang314/DNAm-based-age-predictor.git\"\n", "github_folder_name = github_url.split('/')[-1].split('.')[0]\n", "os.system(f\"git clone {github_url}\")" ] }, { "cell_type": "markdown", "id": "eb5774d0-e6ea-4d18-949e-6ba1f0534d2a", "metadata": {}, "source": [ "#### Download from R package" ] }, { "cell_type": "code", "execution_count": 6, "id": "3460ce9a-719e-494c-9a94-21fc97dd0be4", "metadata": { "execution": { "iopub.execute_input": "2024-03-05T21:24:46.699717Z", "iopub.status.busy": "2024-03-05T21:24:46.699620Z", "iopub.status.idle": "2024-03-05T21:24:46.701947Z", "shell.execute_reply": "2024-03-05T21:24:46.701688Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Writing download.r\n" ] } ], "source": [ "%%writefile download.r\n", "\n", "data = readRDS(\"DNAm-based-age-predictor/data.rds\")\n", "\n", "write.csv(data, \"example_data.csv\")" ] }, { "cell_type": "code", "execution_count": 7, "id": "11ba180b-0ca9-40e0-8a89-72bc4e085451", "metadata": { "execution": { "iopub.execute_input": "2024-03-05T21:24:46.703323Z", "iopub.status.busy": "2024-03-05T21:24:46.703232Z", "iopub.status.idle": "2024-03-05T21:24:50.351464Z", "shell.execute_reply": "2024-03-05T21:24:50.351168Z" } }, "outputs": [ { "data": { "text/plain": [ "0" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "os.system(\"Rscript download.r\")" ] }, { "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": 8, "id": "8a3d5de6-6303-487a-8b4d-e6345792f7be", "metadata": { "execution": { "iopub.execute_input": "2024-03-05T21:24:50.353243Z", "iopub.status.busy": "2024-03-05T21:24:50.353144Z", "iopub.status.idle": "2024-03-05T21:24:50.356952Z", "shell.execute_reply": "2024-03-05T21:24:50.356660Z" } }, "outputs": [], "source": [ "df = pd.read_table('DNAm-based-age-predictor/en.coef', sep=' ')\n", "df['feature'] = df['probe']\n", "df['coefficient'] = df['coef']\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": 9, "id": "e09b3463-4fd4-41b1-ac21-e63ddd223fe0", "metadata": { "execution": { "iopub.execute_input": "2024-03-05T21:24:50.358454Z", "iopub.status.busy": "2024-03-05T21:24:50.358377Z", "iopub.status.idle": "2024-03-05T21:24:50.360355Z", "shell.execute_reply": "2024-03-05T21:24:50.360089Z" } }, "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": 10, "id": "d7f43b99-26f2-4622-9a76-316712058877", "metadata": { "execution": { "iopub.execute_input": "2024-03-05T21:24:50.361761Z", "iopub.status.busy": "2024-03-05T21:24:50.361687Z", "iopub.status.idle": "2024-03-05T21:24:50.363752Z", "shell.execute_reply": "2024-03-05T21:24:50.363516Z" } }, "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": "markdown", "id": "f7fdae64-096a-4640-ade7-6a17b78a01d5", "metadata": {}, "source": [ "#### From CSV file" ] }, { "cell_type": "code", "execution_count": 11, "id": "86de757f-fb38-4bcb-b91e-fc3372d22aad", "metadata": { "execution": { "iopub.execute_input": "2024-03-05T21:24:50.365313Z", "iopub.status.busy": "2024-03-05T21:24:50.365234Z", "iopub.status.idle": "2024-03-05T21:25:05.943375Z", "shell.execute_reply": "2024-03-05T21:25:05.943087Z" } }, "outputs": [], "source": [ "reference_feature_values_df = pd.read_csv('example_data.csv', index_col=0)\n", "reference_feature_values_df = reference_feature_values_df.loc[:, model.features]\n", "model.reference_values = reference_feature_values_df.mean().tolist()" ] }, { "cell_type": "markdown", "id": "af3bcf7b-74a8-4d21-9ccb-4de0c2b0516b", "metadata": {}, "source": [ "## Load preprocess and postprocess objects" ] }, { "cell_type": "code", "execution_count": 12, "id": "7a22fb20-c605-424d-8efb-7620c2c0755c", "metadata": { "execution": { "iopub.execute_input": "2024-03-05T21:25:05.945262Z", "iopub.status.busy": "2024-03-05T21:25:05.945156Z", "iopub.status.idle": "2024-03-05T21:25:05.946693Z", "shell.execute_reply": "2024-03-05T21:25:05.946454Z" } }, "outputs": [], "source": [ "model.preprocess_name = 'scale_row'\n", "model.preprocess_dependencies = None" ] }, { "cell_type": "code", "execution_count": 13, "id": "ff4a21cb-cf41-44dc-9ed1-95cf8aa15772", "metadata": { "execution": { "iopub.execute_input": "2024-03-05T21:25:05.948060Z", "iopub.status.busy": "2024-03-05T21:25:05.947981Z", "iopub.status.idle": "2024-03-05T21:25:05.949425Z", "shell.execute_reply": "2024-03-05T21:25:05.949210Z" } }, "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": 14, "id": "2168355c-47d9-475d-b816-49f65e74887c", "metadata": { "execution": { "iopub.execute_input": "2024-03-05T21:25:05.950807Z", "iopub.status.busy": "2024-03-05T21:25:05.950732Z", "iopub.status.idle": "2024-03-05T21:25:05.953434Z", "shell.execute_reply": "2024-03-05T21:25:05.953181Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "%==================================== Model Details ====================================%\n", "Model Attributes:\n", "\n", "training: True\n", "metadata: {'approved_by_author': '⌛',\n", " 'citation': 'Zhang, Qian, et al. \"Improved precision of epigenetic clock '\n", " 'estimates across tissues and its implication for biological '\n", " 'ageing.\" Genome medicine 11 (2019): 1-11.',\n", " 'clock_name': 'zhangen',\n", " 'data_type': 'methylation',\n", " 'doi': 'https://doi.org/10.1186/s13073-019-0667-1',\n", " 'notes': None,\n", " 'research_only': None,\n", " 'species': 'Homo sapiens',\n", " 'version': None,\n", " 'year': 2019}\n", "reference_values: [0.4203926578618443, 0.4908907575500855, 0.4552739071801655, 0.4913173878831697, 0.1630209603278157, 0.28031076416301215, 0.5630446021353376, 0.07173451377952844, 0.5286791351513329, 0.4914897138593993, 0.8776324935503583, 0.7783989797577173, 0.3679109172453385, 0.5469457656601266, 0.36321717183155133, 0.3929905988245433, 0.11900061695097393, 0.1448950534091979, 0.11166595534101968, 0.08958121797351054, 0.262072821834283, 0.4936214944065201, 0.07967343711600829, 0.4159523391121834, 0.6393676229693106, 0.28478196991507315, 0.2528507814707874, 0.3516112115460574, 0.4707586149929079, 0.8216726560029178]... [Total elements: 514]\n", "preprocess_name: 'scale_row'\n", "preprocess_dependencies: None\n", "postprocess_name: None\n", "postprocess_dependencies: None\n", "features: ['cg24611351', 'cg24173182', 'cg09604333', 'cg13617776', 'cg09432590', 'cg05516505', 'cg12757684', 'cg23606718', 'cg20050761', 'cg22452230', 'cg05898618', 'cg01620164', 'cg06758350', 'cg23615741', 'cg09692396', 'cg02046143', 'cg08540945', 'cg11714320', 'cg22708738', 'cg21567504', 'cg08313880', 'cg03527802', 'cg23995914', 'cg04027548', 'cg07077459', 'cg03025830', 'cg07978099', 'cg24349631', 'cg04218760', 'cg24788483']... [Total elements: 514]\n", "base_model_features: None\n", "\n", "%==================================== Model Details ====================================%\n", "Model Structure:\n", "\n", "base_model: LinearModel(\n", " (linear): Linear(in_features=514, out_features=1, bias=True)\n", ")\n", "\n", "%==================================== Model Details ====================================%\n", "Model Parameters and Weights:\n", "\n", "base_model.linear.weight: [-0.0018107433570548892, -0.2039545476436615, -0.703967809677124, -0.011524459347128868, 1.048977255821228, -0.14274194836616516, -0.7550708651542664, 3.435948610305786, -0.025350039824843407, -0.5448949933052063, -0.8968744874000549, -0.787463366985321, -0.06834892183542252, -0.7093232870101929, -1.467730164527893, -0.6339927315711975, 0.032782625406980515, -0.8660809397697449, 0.12924738228321075, 0.6532240509986877, -0.5267062187194824, -0.07851535081863403, 0.6190375089645386, -1.0144543647766113, -0.03378598392009735, 0.1000944972038269, -0.037325769662857056, -0.029759708791971207, -0.07072985917329788, -1.4537116289138794]... [Tensor of shape torch.Size([1, 514])]\n", "base_model.linear.bias: tensor([65.7930])\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": 15, "id": "936b9877-d076-4ced-99aa-e8d4c58c5caf", "metadata": { "execution": { "iopub.execute_input": "2024-03-05T21:25:05.955060Z", "iopub.status.busy": "2024-03-05T21:25:05.954940Z", "iopub.status.idle": "2024-03-05T21:25:05.958653Z", "shell.execute_reply": "2024-03-05T21:25:05.958416Z" } }, "outputs": [ { "data": { "text/plain": [ "tensor([[ 28.2548],\n", " [104.3112],\n", " [ 83.9525],\n", " [ 78.0909],\n", " [ 60.1387],\n", " [ 63.0642],\n", " [ 67.6295],\n", " [ 73.1206],\n", " [ 72.9059],\n", " [ 56.8473]], dtype=torch.float64, grad_fn=)" ] }, "execution_count": 15, "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": 16, "id": "5ef2fa8d-c80b-4fdd-8555-79c0d541788e", "metadata": { "execution": { "iopub.execute_input": "2024-03-05T21:25:05.960101Z", "iopub.status.busy": "2024-03-05T21:25:05.960021Z", "iopub.status.idle": "2024-03-05T21:25:05.962264Z", "shell.execute_reply": "2024-03-05T21:25:05.961999Z" } }, "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": 17, "id": "11aeaa70-44c0-42f9-86d7-740e3849a7a6", "metadata": { "execution": { "iopub.execute_input": "2024-03-05T21:25:05.963702Z", "iopub.status.busy": "2024-03-05T21:25:05.963623Z", "iopub.status.idle": "2024-03-05T21:25:05.972288Z", "shell.execute_reply": "2024-03-05T21:25:05.972044Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Deleted file: download.r\n", "Deleted folder: DNAm-based-age-predictor\n", "Deleted file: example_data.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", "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.17" } }, "nbformat": 4, "nbformat_minor": 5 }