{ "cells": [ { "cell_type": "markdown", "id": "f0798d7e", "metadata": {}, "source": [ "# Mayne" ] }, { "cell_type": "markdown", "id": "73b8d43e", "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": "c612c8d1", "metadata": {}, "source": [ "Let's first import some packages:" ] }, { "cell_type": "code", "execution_count": 1, "id": "239663df", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:51:53.328821Z", "iopub.status.busy": "2026-07-04T19:51:53.328743Z", "iopub.status.idle": "2026-07-04T19:51:55.563705Z", "shell.execute_reply": "2026-07-04T19:51:55.563193Z" } }, "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": "673886d3", "metadata": {}, "source": [ "## Instantiate model class" ] }, { "cell_type": "code", "execution_count": 2, "id": "bf1db003", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:51:55.566139Z", "iopub.status.busy": "2026-07-04T19:51:55.565931Z", "iopub.status.idle": "2026-07-04T19:51:55.569149Z", "shell.execute_reply": "2026-07-04T19:51:55.568335Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "class Mayne(LinearReferenceClock):\n", " def postprocess(self, x):\n", " \"\"\"Gestational age in weeks (as published).\"\"\"\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.Mayne)" ] }, { "cell_type": "code", "execution_count": 3, "id": "6182eb8e", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:51:55.570948Z", "iopub.status.busy": "2026-07-04T19:51:55.570836Z", "iopub.status.idle": "2026-07-04T19:51:55.572757Z", "shell.execute_reply": "2026-07-04T19:51:55.572356Z" } }, "outputs": [], "source": [ "model = pya.models.Mayne()" ] }, { "cell_type": "markdown", "id": "bea8d84c", "metadata": {}, "source": [ "## Define clock metadata" ] }, { "cell_type": "code", "execution_count": 4, "id": "39b9d1df", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:51:55.574131Z", "iopub.status.busy": "2026-07-04T19:51:55.574052Z", "iopub.status.idle": "2026-07-04T19:51:55.576155Z", "shell.execute_reply": "2026-07-04T19:51:55.575730Z" } }, "outputs": [], "source": [ "model.metadata[\"clock_name\"] = \"mayne\"\n", "model.metadata[\"data_type\"] = \"DNA methylation\" # Paper: The model is based on DNA methylation measurements.\n", "model.metadata[\"species\"] = \"Homo sapiens\" # Paper: The study samples are Homo sapiens.\n", "model.metadata[\"year\"] = 2017\n", "model.metadata[\"approved_by_author\"] = \"⌛\"\n", "model.metadata[\"citation\"] = \"Mayne, Benjamin T., et al. \\\"Accelerated placental aging in early onset preeclampsia pregnancies identified by DNA methylation.\\\" Epigenomics 9 (2017): 279–289.\"\n", "model.metadata[\"doi\"] = \"https://doi.org/10.2217/epi-2016-0103\"\n", "model.metadata[\"notes\"] = \"Placental elastic-net clock trained on pooled healthy human placenta methylation datasets; 62 selected CpGs predict gestational age and were used to test age acceleration in early-onset preeclampsia.\"\n", "model.metadata[\"research_only\"] = None\n", "model.metadata[\"tissue\"] = [\"placenta\"] # Paper: The listed tissue is the model-development sample material.\n", "model.metadata[\"predicts\"] = [\"gestational age\"] # Paper: The reported predictor output is gestational age.\n", "model.metadata[\"training_target\"] = [\"gestational age\"] # Paper: The fitting outcome is gestational age.\n", "model.metadata[\"unit\"] = [\"weeks\"] # Paper: The returned construct is expressed as weeks.\n", "model.metadata[\"model_type\"] = \"elastic net regression\" # Paper: The clock was fitted using Elastic net.\n", "model.metadata[\"platform\"] = [\"Illumina 27K\", \"Illumina 450K\"] # Paper: Training/selection used Illumina 27K/450K.\n", "model.metadata[\"population\"] = \"pregnancies\" # Paper: healthy human placentas sampled across gestation\n", "model.metadata[\"journal\"] = \"Epigenomics\"\n", "model.metadata[\"last_author\"] = \"Tina Bianco‐Miotto\"\n", "model.metadata[\"n_features\"] = 62\n", "model.metadata[\"citations\"] = 150\n", "model.metadata[\"citations_date\"] = \"2026-07-05\"\n" ] }, { "cell_type": "markdown", "id": "c96bed62", "metadata": {}, "source": [ "## Download clock dependencies" ] }, { "cell_type": "code", "execution_count": 5, "id": "b1ff73bd", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:51:55.577556Z", "iopub.status.busy": "2026-07-04T19:51:55.577452Z", "iopub.status.idle": "2026-07-04T19:51:55.877842Z", "shell.execute_reply": "2026-07-04T19:51:55.876779Z" } }, "outputs": [ { "data": { "text/plain": [ "0" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "os.system(f\"curl -sL -o coefficients.csv https://raw.githubusercontent.com/Duzhaozhen/OmniAge/c10fbe8cb92957520fbff1d55ae1def0691252e5/OmniAgePy/src/omniage/data/Mayne_GA.csv\")" ] }, { "cell_type": "markdown", "id": "6789a3b0", "metadata": {}, "source": [ "## Load features" ] }, { "cell_type": "code", "execution_count": 6, "id": "6ed15689", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:51:55.880682Z", "iopub.status.busy": "2026-07-04T19:51:55.880461Z", "iopub.status.idle": "2026-07-04T19:51:55.889165Z", "shell.execute_reply": "2026-07-04T19:51:55.887925Z" } }, "outputs": [], "source": [ "df = pd.read_csv('coefficients.csv')\n", "mask = df['probe'].astype(str).str.lower().isin(['intercept', '(intercept)'])\n", "intercept_value = float(df.loc[mask, 'coef'].iloc[0]) if mask.any() else 0.0\n", "coef_df = df.loc[~mask].reset_index(drop=True)\n", "model.features = coef_df['probe'].tolist()" ] }, { "cell_type": "markdown", "id": "16e86929", "metadata": {}, "source": [ "## Load weights into base model" ] }, { "cell_type": "code", "execution_count": 7, "id": "1fae9a73", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:51:55.892965Z", "iopub.status.busy": "2026-07-04T19:51:55.892763Z", "iopub.status.idle": "2026-07-04T19:51:55.896375Z", "shell.execute_reply": "2026-07-04T19:51:55.895608Z" } }, "outputs": [], "source": [ "weights = torch.tensor(coef_df['coef'].tolist()).unsqueeze(0).float()\n", "intercept = torch.tensor([intercept_value]).float()" ] }, { "cell_type": "code", "execution_count": 8, "id": "dd91a7d7", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:51:55.898330Z", "iopub.status.busy": "2026-07-04T19:51:55.898186Z", "iopub.status.idle": "2026-07-04T19:51:55.901668Z", "shell.execute_reply": "2026-07-04T19:51:55.900963Z" } }, "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": "1cef1afb", "metadata": {}, "source": [ "## Load reference values" ] }, { "cell_type": "code", "execution_count": 9, "id": "9301f2a3", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:51:55.903727Z", "iopub.status.busy": "2026-07-04T19:51:55.903550Z", "iopub.status.idle": "2026-07-04T19:51:55.906473Z", "shell.execute_reply": "2026-07-04T19:51:55.905367Z" } }, "outputs": [], "source": [ "model.reference_values = None" ] }, { "cell_type": "markdown", "id": "116cff22", "metadata": {}, "source": [ "## Load preprocess and postprocess objects" ] }, { "cell_type": "code", "execution_count": 10, "id": "d696a679", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:51:55.909186Z", "iopub.status.busy": "2026-07-04T19:51:55.909023Z", "iopub.status.idle": "2026-07-04T19:51:55.911714Z", "shell.execute_reply": "2026-07-04T19:51:55.911038Z" } }, "outputs": [], "source": [ "model.preprocess_name = None\n", "model.preprocess_dependencies = None" ] }, { "cell_type": "code", "execution_count": 11, "id": "8f20de15", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:51:55.913348Z", "iopub.status.busy": "2026-07-04T19:51:55.913202Z", "iopub.status.idle": "2026-07-04T19:51:55.915378Z", "shell.execute_reply": "2026-07-04T19:51:55.914781Z" } }, "outputs": [], "source": [ "model.postprocess_name = None\n", "model.postprocess_dependencies = None" ] }, { "cell_type": "markdown", "id": "4910951c", "metadata": {}, "source": [ "## Check all clock parameters" ] }, { "cell_type": "code", "execution_count": 12, "id": "d21da80d", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:51:55.916911Z", "iopub.status.busy": "2026-07-04T19:51:55.916785Z", "iopub.status.idle": "2026-07-04T19:51:55.921633Z", "shell.execute_reply": "2026-07-04T19:51:55.921081Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "%==================================== Model Details ====================================%\n", "Model Attributes:\n", "\n", "training: True\n", "metadata: {'approved_by_author': '⌛',\n", " 'citation': 'Mayne, Benjamin T., et al. \"Accelerated placental aging in early '\n", " 'onset preeclampsia pregnancies identified by DNA methylation.\" '\n", " 'Epigenomics 9.3 (2017): 279-289.',\n", " 'clock_name': 'mayne',\n", " 'data_type': 'methylation',\n", " 'doi': 'https://doi.org/10.2217/epi-2016-0103',\n", " 'notes': None,\n", " 'research_only': None,\n", " 'species': 'Homo sapiens',\n", " 'version': None,\n", " 'year': 2017}\n", "reference_values: None\n", "preprocess_name: None\n", "preprocess_dependencies: None\n", "postprocess_name: None\n", "postprocess_dependencies: None\n", "features: ['cg12146151', 'cg16127845', 'cg17133388', 'cg13997435', 'cg12360736', 'cg07300408', 'cg11739626', 'cg00828602', 'cg21624359', 'cg22639768', 'cg11388238', 'cg06958211', 'cg19317715', 'cg20291222', 'cg03490200', 'cg26353877', 'cg22957381', 'cg19742789', 'cg02976574', 'cg16356956', 'cg26143719', 'cg09990086', 'cg16746631', 'cg10245048', 'cg00047050', 'cg25623459', 'cg25374854', 'cg02589695', 'cg19289461', 'cg16816226']... [Total elements: 62]\n", "base_model_features: None\n", "\n", "%==================================== Model Details ====================================%\n", "Model Structure:\n", "\n", "base_model: LinearModel(\n", " (linear): Linear(in_features=62, out_features=1, bias=True)\n", ")\n", "\n", "%==================================== Model Details ====================================%\n", "Model Parameters and Weights:\n", "\n", "base_model.linear.weight: [-1.9282536506652832, -26.4451904296875, -17.35150718688965, -0.9527280330657959, -5.665579319000244, -2.0989339351654053, -0.7110825181007385, -0.03753948584198952, -2.35093355178833, -2.3877365589141846, -4.450428009033203, -0.32594966888427734, -2.3679003715515137, -3.580242156982422, -4.025350570678711, -0.034084122627973557, -5.023456573486328, -10.068197250366211, -6.389592170715332, -0.3280542492866516, -0.40900924801826477, -5.404386043548584, -6.043520927429199, -0.026322754099965096, -1.531197428703308, -2.4376535415649414, -0.4204040467739105, -7.178297996520996, -3.049933433532715, -1.4748220443725586]... [Tensor of shape torch.Size([1, 62])]\n", "base_model.linear.bias: tensor([24.9903])\n", "\n", "%==================================== Model Details ====================================%\n", "\n" ] } ], "source": [ "pya.utils.print_model_details(model)" ] }, { "cell_type": "markdown", "id": "49ef49e6", "metadata": {}, "source": [ "## Basic test" ] }, { "cell_type": "code", "execution_count": 13, "id": "c5210bac", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:51:55.924629Z", "iopub.status.busy": "2026-07-04T19:51:55.924455Z", "iopub.status.idle": "2026-07-04T19:51:55.929980Z", "shell.execute_reply": "2026-07-04T19:51:55.929425Z" } }, "outputs": [ { "data": { "text/plain": [ "tensor([[ -39.5419],\n", " [ 25.7659],\n", " [ 44.2797],\n", " [ -0.9077],\n", " [ 92.4899],\n", " [ 86.2686],\n", " [-106.0699],\n", " [ 76.7959],\n", " [ 20.1038],\n", " [ 21.3373]], 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": "e60e43d4", "metadata": {}, "source": [ "## Save torch model" ] }, { "cell_type": "code", "execution_count": 14, "id": "837d4957", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:51:55.931596Z", "iopub.status.busy": "2026-07-04T19:51:55.931488Z", "iopub.status.idle": "2026-07-04T19:51:55.935548Z", "shell.execute_reply": "2026-07-04T19:51:55.934698Z" } }, "outputs": [], "source": [ "torch.save(model, f\"../weights/{model.metadata['clock_name']}.pt\")" ] }, { "cell_type": "markdown", "id": "c73f1339", "metadata": {}, "source": [ "## Clear directory\n", "" ] }, { "cell_type": "code", "execution_count": 15, "id": "bc9ec094", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T19:51:55.937393Z", "iopub.status.busy": "2026-07-04T19:51:55.937256Z", "iopub.status.idle": "2026-07-04T19:51:55.943115Z", "shell.execute_reply": "2026-07-04T19:51:55.942694Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Deleted file: coefficients.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 (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 }