{ "cells": [ { "cell_type": "markdown", "id": "a472b026", "metadata": {}, "source": [ "# EpiCMITHyper" ] }, { "cell_type": "markdown", "id": "17dcbe69", "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": "5ab6f61e", "metadata": {}, "source": [ "Let's first import some packages:" ] }, { "cell_type": "code", "execution_count": 1, "id": "ec3511d3", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:39:11.098617Z", "iopub.status.busy": "2026-07-04T19:39:11.098453Z", "iopub.status.idle": "2026-07-04T19:39:13.414710Z", "shell.execute_reply": "2026-07-04T19:39:13.413768Z" } }, "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": "4bd750d1", "metadata": {}, "source": [ "## Instantiate model class" ] }, { "cell_type": "code", "execution_count": 2, "id": "e2ecfb86", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:39:13.416927Z", "iopub.status.busy": "2026-07-04T19:39:13.416645Z", "iopub.status.idle": "2026-07-04T19:39:13.421587Z", "shell.execute_reply": "2026-07-04T19:39:13.421051Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "class EpiCMITHyper(epiTOC1):\n", " pass\n", "\n" ] } ], "source": [ "def print_entire_class(cls):\n", " source = inspect.getsource(cls)\n", " print(source)\n", "\n", "print_entire_class(pya.models.EpiCMITHyper)" ] }, { "cell_type": "code", "execution_count": 3, "id": "43dca5a3", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:39:13.422887Z", "iopub.status.busy": "2026-07-04T19:39:13.422808Z", "iopub.status.idle": "2026-07-04T19:39:13.424729Z", "shell.execute_reply": "2026-07-04T19:39:13.424290Z" } }, "outputs": [], "source": [ "model = pya.models.EpiCMITHyper()" ] }, { "cell_type": "markdown", "id": "47e4582b", "metadata": {}, "source": [ "## Define clock metadata" ] }, { "cell_type": "code", "execution_count": 4, "id": "c2b5bd52", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:39:13.426027Z", "iopub.status.busy": "2026-07-04T19:39:13.425925Z", "iopub.status.idle": "2026-07-04T19:39:13.428495Z", "shell.execute_reply": "2026-07-04T19:39:13.427930Z" } }, "outputs": [], "source": [ "model.metadata[\"clock_name\"] = \"epicmithyper\"\n", "model.metadata[\"data_type\"] = \"DNA methylation\" # Paper: methylation\n", "model.metadata[\"species\"] = \"Homo sapiens\" # Paper: Homo sapiens\n", "model.metadata[\"year\"] = 2020\n", "model.metadata[\"approved_by_author\"] = \"⌛\"\n", "model.metadata[\"citation\"] = \"Duran-Ferrer, M., et al. \\\"The proliferative history shapes the DNA methylome of B-cell tumors and predicts clinical outcome.\\\" Nature Cancer 1 (2020): 1066-1081.\"\n", "model.metadata[\"doi\"] = \"https://doi.org/10.1038/s43018-020-00131-2\"\n", "model.metadata[\"notes\"] = \"Hypermethylation component of epiCMIT: a 184-CpG score ranging from 0 to 1 that tracks low-to-high relative proliferative history in normal and neoplastic B cells.\"\n", "model.metadata[\"research_only\"] = None\n", "model.metadata[\"tissue\"] = [\"B cells\"] # Paper: normal and neoplastic B cells\n", "model.metadata[\"predicts\"] = [\"mitotic age\"] # Paper: The author tutorial states both component clocks range from 0 to 1 and report relative proliferative history.\n", "model.metadata[\"training_target\"] = [\"replicative history\"] # Paper: relative proliferative history\n", "model.metadata[\"unit\"] = [\"proportion\"] # Paper: The author tutorial states both component clocks range from 0 to 1 and report relative proliferative history.\n", "model.metadata[\"model_type\"] = \"mean methylation aggregation\" # Paper: Mean methylation score\n", "model.metadata[\"platform\"] = [\"Illumina 450K\", \"Illumina EPIC\"] # Paper: Illumina 450K; Illumina EPIC\n", "model.metadata[\"population\"] = \"human, age unspecified\" # Paper: 1,595 human samples spanning normal B-cell subpopulations and 14 B-cell tumor subtypes\n", "model.metadata[\"journal\"] = \"Nature Cancer\"\n", "model.metadata[\"last_author\"] = \"José I. Martín-Subero\"\n", "model.metadata[\"n_features\"] = 184\n", "model.metadata[\"citations\"] = 104\n", "model.metadata[\"citations_date\"] = \"2026-07-05\"\n" ] }, { "cell_type": "markdown", "id": "569da7a8", "metadata": {}, "source": [ "## Download clock dependencies" ] }, { "cell_type": "code", "execution_count": 5, "id": "8be3b2e0", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:39:13.429977Z", "iopub.status.busy": "2026-07-04T19:39:13.429896Z", "iopub.status.idle": "2026-07-04T19:39:15.872124Z", "shell.execute_reply": "2026-07-04T19:39:15.870891Z" } }, "outputs": [ { "data": { "text/plain": [ "0" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "supplementary_url = \"https://static-content.springer.com/esm/art%3A10.1038%2Fs43018-020-00131-2/MediaObjects/43018_2020_131_MOESM3_ESM.xlsx\"\n", "supplementary_file_name = \"epicmit.xlsx\"\n", "os.system(f\"curl -sL -o {supplementary_file_name} {supplementary_url}\")" ] }, { "cell_type": "markdown", "id": "a7e8e03c", "metadata": {}, "source": [ "## Load features" ] }, { "cell_type": "code", "execution_count": 6, "id": "3d17c8fc", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:39:15.875161Z", "iopub.status.busy": "2026-07-04T19:39:15.874889Z", "iopub.status.idle": "2026-07-04T19:39:16.856710Z", "shell.execute_reply": "2026-07-04T19:39:16.856186Z" } }, "outputs": [], "source": [ "df = pd.read_excel('epicmit.xlsx', sheet_name='Table 23')\n", "df = df[df['epiCMIT.class'].astype(str).str.contains('hyper', case=False)]\n", "model.features = df['Name'].tolist()" ] }, { "cell_type": "markdown", "id": "78d5555d", "metadata": {}, "source": [ "## Load weights into base model" ] }, { "cell_type": "code", "execution_count": 7, "id": "e8ed83f6", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:39:16.858171Z", "iopub.status.busy": "2026-07-04T19:39:16.858093Z", "iopub.status.idle": "2026-07-04T19:39:16.860299Z", "shell.execute_reply": "2026-07-04T19:39:16.859655Z" } }, "outputs": [], "source": [ "weights = torch.tensor([1.0]).unsqueeze(0)\n", "intercept = torch.tensor([0.0])" ] }, { "cell_type": "code", "execution_count": 8, "id": "6c3e88f2", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:39:16.862131Z", "iopub.status.busy": "2026-07-04T19:39:16.861989Z", "iopub.status.idle": "2026-07-04T19:39:16.864681Z", "shell.execute_reply": "2026-07-04T19:39:16.864142Z" } }, "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": "8381f629", "metadata": {}, "source": [ "## Load reference values" ] }, { "cell_type": "code", "execution_count": 9, "id": "bfdf0b41", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:39:16.866249Z", "iopub.status.busy": "2026-07-04T19:39:16.866136Z", "iopub.status.idle": "2026-07-04T19:39:16.868018Z", "shell.execute_reply": "2026-07-04T19:39:16.867560Z" } }, "outputs": [], "source": [ "model.reference_values = [-1]*len(model.features)" ] }, { "cell_type": "markdown", "id": "6b2ac10e", "metadata": {}, "source": [ "## Load preprocess and postprocess objects" ] }, { "cell_type": "code", "execution_count": 10, "id": "db5944d5", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:39:16.869473Z", "iopub.status.busy": "2026-07-04T19:39:16.869280Z", "iopub.status.idle": "2026-07-04T19:39:16.871514Z", "shell.execute_reply": "2026-07-04T19:39:16.870907Z" } }, "outputs": [], "source": [ "model.preprocess_name = \"mean\"\n", "model.preprocess_dependencies = None" ] }, { "cell_type": "code", "execution_count": 11, "id": "062e7d42", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:39:16.873060Z", "iopub.status.busy": "2026-07-04T19:39:16.872948Z", "iopub.status.idle": "2026-07-04T19:39:16.874740Z", "shell.execute_reply": "2026-07-04T19:39:16.874347Z" } }, "outputs": [], "source": [ "model.postprocess_name = None\n", "model.postprocess_dependencies = None" ] }, { "cell_type": "markdown", "id": "774e7915", "metadata": {}, "source": [ "## Check all clock parameters" ] }, { "cell_type": "code", "execution_count": 12, "id": "7d20e68f", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:39:16.875947Z", "iopub.status.busy": "2026-07-04T19:39:16.875872Z", "iopub.status.idle": "2026-07-04T19:39:16.879530Z", "shell.execute_reply": "2026-07-04T19:39:16.879157Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "%==================================== Model Details ====================================%\n", "Model Attributes:\n", "\n", "training: True\n", "metadata: {'approved_by_author': '⌛',\n", " 'citation': 'Duran-Ferrer, Marti, et al. \"The proliferative history shapes '\n", " 'the DNA methylome of B-cell tumors and predicts clinical '\n", " 'outcome.\" Nature Cancer 1.11 (2020): 1066-1081.',\n", " 'clock_name': 'epicmithyper',\n", " 'data_type': 'methylation',\n", " 'doi': 'https://doi.org/10.1038/s43018-020-00131-2',\n", " 'notes': None,\n", " 'research_only': None,\n", " 'species': 'Homo sapiens',\n", " 'version': None,\n", " 'year': 2020}\n", "reference_values: [-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1]... [Total elements: 184]\n", "preprocess_name: 'mean'\n", "preprocess_dependencies: None\n", "postprocess_name: None\n", "postprocess_dependencies: None\n", "features: ['cg19715410', 'cg18279094', 'cg11823511', 'cg24453699', 'cg03989260', 'cg14565725', 'cg00466334', 'cg24035245', 'cg05378938', 'cg09671258', 'cg09826050', 'cg07279070', 'cg18093372', 'cg18173235', 'cg07813142', 'cg24772753', 'cg17737681', 'cg17078253', 'cg05467160', 'cg04415176', 'cg01405040', 'cg02694427', 'cg03964958', 'cg09578028', 'cg04739647', 'cg05167251', 'cg22674699', 'cg22541735', 'cg19384289', 'cg14473102']... [Total elements: 184]\n", "base_model_features: None\n", "\n", "%==================================== Model Details ====================================%\n", "Model Structure:\n", "\n", "base_model: LinearModel(\n", " (linear): Linear(in_features=184, out_features=1, bias=True)\n", ")\n", "\n", "%==================================== Model Details ====================================%\n", "Model Parameters and Weights:\n", "\n", "base_model.linear.weight: tensor([[1.]])\n", "base_model.linear.bias: tensor([0.])\n", "\n", "%==================================== Model Details ====================================%\n", "\n" ] } ], "source": [ "pya.utils.print_model_details(model)" ] }, { "cell_type": "markdown", "id": "04d85a27", "metadata": {}, "source": [ "## Basic test" ] }, { "cell_type": "code", "execution_count": 13, "id": "fff8eeb7", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:39:16.880736Z", "iopub.status.busy": "2026-07-04T19:39:16.880664Z", "iopub.status.idle": "2026-07-04T19:39:16.890617Z", "shell.execute_reply": "2026-07-04T19:39:16.890143Z" } }, "outputs": [ { "data": { "text/plain": [ "tensor([[ 0.0423],\n", " [ 0.0471],\n", " [ 0.0455],\n", " [ 0.0341],\n", " [ 0.0093],\n", " [ 0.0674],\n", " [ 0.0421],\n", " [ 0.0545],\n", " [ 0.0112],\n", " [-0.1275]], 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": "6fd22db0", "metadata": {}, "source": [ "## Save torch model" ] }, { "cell_type": "code", "execution_count": 14, "id": "7f18d318", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:39:16.891834Z", "iopub.status.busy": "2026-07-04T19:39:16.891757Z", "iopub.status.idle": "2026-07-04T19:39:16.896145Z", "shell.execute_reply": "2026-07-04T19:39:16.895688Z" } }, "outputs": [], "source": [ "torch.save(model, f\"../weights/{model.metadata['clock_name']}.pt\")" ] }, { "cell_type": "markdown", "id": "642baee1", "metadata": {}, "source": [ "## Clear directory\n", "" ] }, { "cell_type": "code", "execution_count": 15, "id": "b2e4fe26", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:39:16.897525Z", "iopub.status.busy": "2026-07-04T19:39:16.897449Z", "iopub.status.idle": "2026-07-04T19:39:16.901192Z", "shell.execute_reply": "2026-07-04T19:39:16.900815Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Deleted file: epicmit.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 }