{ "cells": [ { "cell_type": "markdown", "id": "85857f5c", "metadata": {}, "source": [ "# EpiCMITHypo" ] }, { "cell_type": "markdown", "id": "b5bc819a", "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": "6908f801", "metadata": {}, "source": [ "Let's first import some packages:" ] }, { "cell_type": "code", "execution_count": 1, "id": "38309ebf", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:39:18.861408Z", "iopub.status.busy": "2026-07-04T19:39:18.861231Z", "iopub.status.idle": "2026-07-04T19:39:20.907018Z", "shell.execute_reply": "2026-07-04T19:39:20.906530Z" } }, "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": "98778012", "metadata": {}, "source": [ "## Instantiate model class" ] }, { "cell_type": "code", "execution_count": 2, "id": "5aac85ce", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:39:20.908636Z", "iopub.status.busy": "2026-07-04T19:39:20.908465Z", "iopub.status.idle": "2026-07-04T19:39:20.911208Z", "shell.execute_reply": "2026-07-04T19:39:20.910849Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "class EpiCMITHypo(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.EpiCMITHypo)" ] }, { "cell_type": "code", "execution_count": 3, "id": "385d21d8", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:39:20.912617Z", "iopub.status.busy": "2026-07-04T19:39:20.912523Z", "iopub.status.idle": "2026-07-04T19:39:20.914218Z", "shell.execute_reply": "2026-07-04T19:39:20.913852Z" } }, "outputs": [], "source": [ "model = pya.models.EpiCMITHypo()" ] }, { "cell_type": "markdown", "id": "32bcc319", "metadata": {}, "source": [ "## Define clock metadata" ] }, { "cell_type": "code", "execution_count": 4, "id": "bb771c46", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:39:20.915449Z", "iopub.status.busy": "2026-07-04T19:39:20.915358Z", "iopub.status.idle": "2026-07-04T19:39:20.917567Z", "shell.execute_reply": "2026-07-04T19:39:20.917213Z" } }, "outputs": [], "source": [ "model.metadata[\"clock_name\"] = \"epicmithypo\"\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\"] = \"Hypomethylation component of epiCMIT: a 1,164-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\"] = \"complement of mean methylation\" # Paper: Complement of 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\"] = 1164\n", "model.metadata[\"citations\"] = 104\n", "model.metadata[\"citations_date\"] = \"2026-07-05\"\n" ] }, { "cell_type": "markdown", "id": "c4402281", "metadata": {}, "source": [ "## Download clock dependencies" ] }, { "cell_type": "code", "execution_count": 5, "id": "0070d992", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:39:20.918946Z", "iopub.status.busy": "2026-07-04T19:39:20.918870Z", "iopub.status.idle": "2026-07-04T19:39:23.319051Z", "shell.execute_reply": "2026-07-04T19:39:23.318537Z" } }, "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": "2dc0f591", "metadata": {}, "source": [ "## Load features" ] }, { "cell_type": "code", "execution_count": 6, "id": "7d8d285a", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:39:23.320498Z", "iopub.status.busy": "2026-07-04T19:39:23.320390Z", "iopub.status.idle": "2026-07-04T19:39:24.253177Z", "shell.execute_reply": "2026-07-04T19:39:24.252634Z" } }, "outputs": [], "source": [ "df = pd.read_excel('epicmit.xlsx', sheet_name='Table 23')\n", "df = df[df['epiCMIT.class'].astype(str).str.contains('hypo', case=False)]\n", "model.features = df['Name'].tolist()" ] }, { "cell_type": "markdown", "id": "97815cdc", "metadata": {}, "source": [ "## Load weights into base model" ] }, { "cell_type": "code", "execution_count": 7, "id": "41572803", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:39:24.254695Z", "iopub.status.busy": "2026-07-04T19:39:24.254602Z", "iopub.status.idle": "2026-07-04T19:39:24.256660Z", "shell.execute_reply": "2026-07-04T19:39:24.256276Z" } }, "outputs": [], "source": [ "weights = torch.tensor([-1.0]).unsqueeze(0)\n", "intercept = torch.tensor([1.0])" ] }, { "cell_type": "code", "execution_count": 8, "id": "4b3dec57", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:39:24.257835Z", "iopub.status.busy": "2026-07-04T19:39:24.257758Z", "iopub.status.idle": "2026-07-04T19:39:24.259861Z", "shell.execute_reply": "2026-07-04T19:39:24.259497Z" } }, "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": "8127702e", "metadata": {}, "source": [ "## Load reference values" ] }, { "cell_type": "code", "execution_count": 9, "id": "52cfab7a", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:39:24.261037Z", "iopub.status.busy": "2026-07-04T19:39:24.260974Z", "iopub.status.idle": "2026-07-04T19:39:24.262509Z", "shell.execute_reply": "2026-07-04T19:39:24.262171Z" } }, "outputs": [], "source": [ "model.reference_values = [-1]*len(model.features)" ] }, { "cell_type": "markdown", "id": "fb6b834b", "metadata": {}, "source": [ "## Load preprocess and postprocess objects" ] }, { "cell_type": "code", "execution_count": 10, "id": "7aa8d11a", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:39:24.263579Z", "iopub.status.busy": "2026-07-04T19:39:24.263510Z", "iopub.status.idle": "2026-07-04T19:39:24.265057Z", "shell.execute_reply": "2026-07-04T19:39:24.264672Z" } }, "outputs": [], "source": [ "model.preprocess_name = \"mean\"\n", "model.preprocess_dependencies = None" ] }, { "cell_type": "code", "execution_count": 11, "id": "57a46eea", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:39:24.266131Z", "iopub.status.busy": "2026-07-04T19:39:24.266055Z", "iopub.status.idle": "2026-07-04T19:39:24.267546Z", "shell.execute_reply": "2026-07-04T19:39:24.267197Z" } }, "outputs": [], "source": [ "model.postprocess_name = None\n", "model.postprocess_dependencies = None" ] }, { "cell_type": "markdown", "id": "86135a92", "metadata": {}, "source": [ "## Check all clock parameters" ] }, { "cell_type": "code", "execution_count": 12, "id": "8624db05", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:39:24.268509Z", "iopub.status.busy": "2026-07-04T19:39:24.268441Z", "iopub.status.idle": "2026-07-04T19:39:24.271620Z", "shell.execute_reply": "2026-07-04T19:39:24.271355Z" } }, "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': 'epicmithypo',\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: 1164]\n", "preprocess_name: 'mean'\n", "preprocess_dependencies: None\n", "postprocess_name: None\n", "postprocess_dependencies: None\n", "features: ['cg21870274', 'cg17810211', 'cg08263140', 'cg05663262', 'cg00055603', 'cg05042706', 'cg25469314', 'cg16515477', 'cg07910726', 'cg22037030', 'cg00954256', 'cg13047196', 'cg04910179', 'cg08123701', 'cg14777634', 'cg11795488', 'cg21679730', 'cg11228758', 'cg13784017', 'cg17987458', 'cg12302119', 'cg05928873', 'cg17041296', 'cg16926302', 'cg11338128', 'cg05948962', 'cg01952027', 'cg17175521', 'cg17011453', 'cg26032419']... [Total elements: 1164]\n", "base_model_features: None\n", "\n", "%==================================== Model Details ====================================%\n", "Model Structure:\n", "\n", "base_model: LinearModel(\n", " (linear): Linear(in_features=1164, 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([1.])\n", "\n", "%==================================== Model Details ====================================%\n", "\n" ] } ], "source": [ "pya.utils.print_model_details(model)" ] }, { "cell_type": "markdown", "id": "86be345b", "metadata": {}, "source": [ "## Basic test" ] }, { "cell_type": "code", "execution_count": 13, "id": "682e50ed", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:39:24.272825Z", "iopub.status.busy": "2026-07-04T19:39:24.272756Z", "iopub.status.idle": "2026-07-04T19:39:24.278148Z", "shell.execute_reply": "2026-07-04T19:39:24.277639Z" } }, "outputs": [ { "data": { "text/plain": [ "tensor([[0.9526],\n", " [1.0553],\n", " [1.0356],\n", " [0.9706],\n", " [1.0003],\n", " [0.9377],\n", " [1.0229],\n", " [1.0117],\n", " [1.0650],\n", " [0.9722]], 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": "3cdde583", "metadata": {}, "source": [ "## Save torch model" ] }, { "cell_type": "code", "execution_count": 14, "id": "1e3c2a62", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:39:24.279390Z", "iopub.status.busy": "2026-07-04T19:39:24.279300Z", "iopub.status.idle": "2026-07-04T19:39:24.284998Z", "shell.execute_reply": "2026-07-04T19:39:24.284482Z" } }, "outputs": [], "source": [ "torch.save(model, f\"../weights/{model.metadata['clock_name']}.pt\")" ] }, { "cell_type": "markdown", "id": "efc099e8", "metadata": {}, "source": [ "## Clear directory\n", "" ] }, { "cell_type": "code", "execution_count": 15, "id": "59376b02", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:39:24.286618Z", "iopub.status.busy": "2026-07-04T19:39:24.286529Z", "iopub.status.idle": "2026-07-04T19:39:24.290066Z", "shell.execute_reply": "2026-07-04T19:39:24.289760Z" } }, "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 }