{ "cells": [ { "cell_type": "markdown", "id": "fce1f113", "metadata": {}, "source": [ "# ReedBMI" ] }, { "cell_type": "markdown", "id": "39972ff2", "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": "eee019dd", "metadata": {}, "source": [ "Let's first import some packages:" ] }, { "cell_type": "code", "execution_count": 1, "id": "b5882df9", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T20:00:52.354226Z", "iopub.status.busy": "2026-07-04T20:00:52.354087Z", "iopub.status.idle": "2026-07-04T20:00:54.669364Z", "shell.execute_reply": "2026-07-04T20:00:54.668870Z" } }, "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": "7d8f1eb5", "metadata": {}, "source": [ "## Instantiate model class" ] }, { "cell_type": "code", "execution_count": 2, "id": "c5f2907a", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T20:00:54.671055Z", "iopub.status.busy": "2026-07-04T20:00:54.670865Z", "iopub.status.idle": "2026-07-04T20:00:54.673901Z", "shell.execute_reply": "2026-07-04T20:00:54.673498Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "class ReedBMI(LinearReferenceClock):\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.ReedBMI)" ] }, { "cell_type": "code", "execution_count": 3, "id": "2347b342", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T20:00:54.675282Z", "iopub.status.busy": "2026-07-04T20:00:54.675188Z", "iopub.status.idle": "2026-07-04T20:00:54.678287Z", "shell.execute_reply": "2026-07-04T20:00:54.677573Z" } }, "outputs": [], "source": [ "model = pya.models.ReedBMI()" ] }, { "cell_type": "markdown", "id": "522df83a", "metadata": {}, "source": [ "## Define clock metadata" ] }, { "cell_type": "code", "execution_count": 4, "id": "f27f7cb2", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T20:00:54.680262Z", "iopub.status.busy": "2026-07-04T20:00:54.680168Z", "iopub.status.idle": "2026-07-04T20:00:54.682449Z", "shell.execute_reply": "2026-07-04T20:00:54.682102Z" } }, "outputs": [], "source": [ "model.metadata[\"clock_name\"] = \"reedbmi\"\n", "model.metadata[\"data_type\"] = \"DNA methylation\" # Paper: DNA methylation scores for BMI\n", "model.metadata[\"species\"] = \"Homo sapiens\" # Paper: ARIES cohort of mothers and children\n", "model.metadata[\"year\"] = 2020\n", "model.metadata[\"approved_by_author\"] = \"⌛\"\n", "model.metadata[\"citation\"] = \"Reed, Zoe E., et al. \\\"The association of DNA methylation with body mass index: distinguishing between predictors and biomarkers.\\\" Clinical Epigenetics 12 (2020): 50.\"\n", "model.metadata[\"doi\"] = \"https://doi.org/10.1186/s13148-020-00841-5\"\n", "model.metadata[\"notes\"] = \"Weighted blood-DNA-methylation score built from published BMI EWAS effect estimates and evaluated across the ARIES life course; it is a biomarker associated with concurrent BMI, not a calibrated prediction in kilograms.\"\n", "model.metadata[\"research_only\"] = None\n", "model.metadata[\"tissue\"] = [\"whole blood\", \"cord blood\"] # Paper: DNA methylation measured in peripheral blood; at birth, childhood, adolescence, pregnancy and middle age\n", "model.metadata[\"predicts\"] = [\"BMI methylation score\"] # Paper: methylation score for BMI\n", "model.metadata[\"training_target\"] = [\"body mass index\"] # Paper: 135 CpG sites from ... meta-analysis of DNA methylation and BMI\n", "model.metadata[\"unit\"] = [\"unitless\"] # Paper: CpG methylation levels were multiplied by published effect estimates and summed; analyses report effects per SD of the score.\n", "model.metadata[\"model_type\"] = \"weighted methylation aggregation\" # Paper: multiplying ... methylation levels ... with corresponding published effect estimates and then summing\n", "model.metadata[\"platform\"] = [\"Illumina 450K\"] # Paper: ARIES methylation profiles\n", "model.metadata[\"population\"] = \"all ages\" # Paper: mothers and children ... at multiple time points\n", "model.metadata[\"journal\"] = \"Clinical Epigenetics\"\n", "model.metadata[\"last_author\"] = \"Gibran Hemani\"\n", "model.metadata[\"n_features\"] = 135\n", "model.metadata[\"citations\"] = 86\n", "model.metadata[\"citations_date\"] = \"2026-07-05\"\n" ] }, { "cell_type": "markdown", "id": "28e61558", "metadata": {}, "source": [ "## Download clock dependencies" ] }, { "cell_type": "code", "execution_count": 5, "id": "2c728268", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T20:00:54.683745Z", "iopub.status.busy": "2026-07-04T20:00:54.683666Z", "iopub.status.idle": "2026-07-04T20:00:55.024150Z", "shell.execute_reply": "2026-07-04T20:00:55.023732Z" } }, "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/bio-learn/biolearn/180852e2bab473303cb85da627178b1695ee9d86/biolearn/data/BMI_Reed.csv\")" ] }, { "cell_type": "markdown", "id": "e2511b63", "metadata": {}, "source": [ "## Load features" ] }, { "cell_type": "code", "execution_count": 6, "id": "97d9e08e", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T20:00:55.025624Z", "iopub.status.busy": "2026-07-04T20:00:55.025470Z", "iopub.status.idle": "2026-07-04T20:00:55.033059Z", "shell.execute_reply": "2026-07-04T20:00:55.032624Z" } }, "outputs": [], "source": [ "df = pd.read_csv('coefficients.csv')\n", "mask = df['CpGmarker'].astype(str).str.lower().isin(['intercept', '(intercept)'])\n", "intercept_value = float(df.loc[mask, 'CoefficientTraining'].iloc[0]) if mask.any() else 0.0\n", "coef_df = df.loc[~mask].reset_index(drop=True)\n", "model.features = coef_df['CpGmarker'].tolist()" ] }, { "cell_type": "markdown", "id": "94b130f5", "metadata": {}, "source": [ "## Load weights into base model" ] }, { "cell_type": "code", "execution_count": 7, "id": "a8fcf42c", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T20:00:55.034746Z", "iopub.status.busy": "2026-07-04T20:00:55.034650Z", "iopub.status.idle": "2026-07-04T20:00:55.036795Z", "shell.execute_reply": "2026-07-04T20:00:55.036440Z" } }, "outputs": [], "source": [ "weights = torch.tensor(coef_df['CoefficientTraining'].tolist()).unsqueeze(0).float()\n", "intercept = torch.tensor([intercept_value]).float()" ] }, { "cell_type": "code", "execution_count": 8, "id": "405b96e1", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T20:00:55.038151Z", "iopub.status.busy": "2026-07-04T20:00:55.038020Z", "iopub.status.idle": "2026-07-04T20:00:55.040284Z", "shell.execute_reply": "2026-07-04T20:00:55.039918Z" } }, "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": "650ab22a", "metadata": {}, "source": [ "## Load reference values" ] }, { "cell_type": "code", "execution_count": 9, "id": "fd5a060a", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T20:00:55.041505Z", "iopub.status.busy": "2026-07-04T20:00:55.041416Z", "iopub.status.idle": "2026-07-04T20:00:55.043025Z", "shell.execute_reply": "2026-07-04T20:00:55.042667Z" } }, "outputs": [], "source": [ "model.reference_values = None" ] }, { "cell_type": "markdown", "id": "2963260d", "metadata": {}, "source": [ "## Load preprocess and postprocess objects" ] }, { "cell_type": "code", "execution_count": 10, "id": "5ba1bb4c", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T20:00:55.044562Z", "iopub.status.busy": "2026-07-04T20:00:55.044435Z", "iopub.status.idle": "2026-07-04T20:00:55.046192Z", "shell.execute_reply": "2026-07-04T20:00:55.045804Z" } }, "outputs": [], "source": [ "model.preprocess_name = None\n", "model.preprocess_dependencies = None" ] }, { "cell_type": "code", "execution_count": 11, "id": "3f76fe43", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T20:00:55.047432Z", "iopub.status.busy": "2026-07-04T20:00:55.047359Z", "iopub.status.idle": "2026-07-04T20:00:55.050006Z", "shell.execute_reply": "2026-07-04T20:00:55.049102Z" } }, "outputs": [], "source": [ "model.postprocess_name = None\n", "model.postprocess_dependencies = None" ] }, { "cell_type": "markdown", "id": "69cfcda4", "metadata": {}, "source": [ "## Check all clock parameters" ] }, { "cell_type": "code", "execution_count": 12, "id": "5ebcc8f1", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T20:00:55.052939Z", "iopub.status.busy": "2026-07-04T20:00:55.052708Z", "iopub.status.idle": "2026-07-04T20:00:55.058185Z", "shell.execute_reply": "2026-07-04T20:00:55.057696Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "%==================================== Model Details ====================================%\n", "Model Attributes:\n", "\n", "training: True\n", "metadata: {'approved_by_author': '⌛',\n", " 'citation': 'Reed, Zoe E., et al. \"Perinatal and childhood epigenetic '\n", " 'biomarkers of body composition.\" Clinical Epigenetics 12.1 '\n", " '(2020): 156.',\n", " 'clock_name': 'reedbmi',\n", " 'data_type': 'methylation',\n", " 'doi': 'https://doi.org/10.1186/s13148-020-00841-5',\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: None\n", "postprocess_dependencies: None\n", "features: ['cg00108715', 'cg00222799', 'cg00574958', 'cg01130991', 'cg01243823', 'cg01368219', 'cg01455178', 'cg01526748', 'cg01597398', 'cg01671681', 'cg01751802', 'cg01798813', 'cg01881899', 'cg02286155', 'cg02571142', 'cg02711608', 'cg02716826', 'cg03078551', 'cg03218374', 'cg03500056', 'cg03682690', 'cg03725309', 'cg04011474', 'cg04286697', 'cg04483863', 'cg04557677', 'cg04816311', 'cg04927537', 'cg05119988', 'cg06192883']... [Total elements: 135]\n", "base_model_features: None\n", "\n", "%==================================== Model Details ====================================%\n", "Model Structure:\n", "\n", "base_model: LinearModel(\n", " (linear): Linear(in_features=135, out_features=1, bias=True)\n", ")\n", "\n", "%==================================== Model Details ====================================%\n", "Model Parameters and Weights:\n", "\n", "base_model.linear.weight: [0.007000000216066837, 0.007000000216066837, -0.019999999552965164, 0.009999999776482582, -0.00800000037997961, 0.00800000037997961, 0.008999999612569809, 0.008999999612569809, -0.006000000052154064, -0.00800000037997961, 0.00800000037997961, 0.009999999776482582, 0.009999999776482582, 0.006000000052154064, 0.00800000037997961, -0.00800000037997961, -0.00800000037997961, -0.009999999776482582, 0.008999999612569809, 0.006000000052154064, 0.009999999776482582, -0.009999999776482582, -0.00800000037997961, 0.007000000216066837, 0.008999999612569809, -0.00800000037997961, 0.009999999776482582, 0.009999999776482582, -0.008999999612569809, 0.00800000037997961]... [Tensor of shape torch.Size([1, 135])]\n", "base_model.linear.bias: tensor([0.])\n", "\n", "%==================================== Model Details ====================================%\n", "\n" ] } ], "source": [ "pya.utils.print_model_details(model)" ] }, { "cell_type": "markdown", "id": "607e396a", "metadata": {}, "source": [ "## Basic test" ] }, { "cell_type": "code", "execution_count": 13, "id": "778690b4", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T20:00:55.059649Z", "iopub.status.busy": "2026-07-04T20:00:55.059559Z", "iopub.status.idle": "2026-07-04T20:00:55.063874Z", "shell.execute_reply": "2026-07-04T20:00:55.063467Z" } }, "outputs": [ { "data": { "text/plain": [ "tensor([[ 0.1953],\n", " [-0.0139],\n", " [ 0.0094],\n", " [-0.0168],\n", " [ 0.0056],\n", " [ 0.0452],\n", " [ 0.0059],\n", " [ 0.0187],\n", " [ 0.1456],\n", " [-0.0608]], 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": "6868ca12", "metadata": {}, "source": [ "## Save torch model" ] }, { "cell_type": "code", "execution_count": 14, "id": "e2c058ee", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T20:00:55.065428Z", "iopub.status.busy": "2026-07-04T20:00:55.065333Z", "iopub.status.idle": "2026-07-04T20:00:55.068815Z", "shell.execute_reply": "2026-07-04T20:00:55.068406Z" } }, "outputs": [], "source": [ "torch.save(model, f\"../weights/{model.metadata['clock_name']}.pt\")" ] }, { "cell_type": "markdown", "id": "423bb494", "metadata": {}, "source": [ "## Clear directory\n", "" ] }, { "cell_type": "code", "execution_count": 15, "id": "b1703823", "metadata": { "execution": { "iopub.execute_input": "2026-07-04T20:00:55.070186Z", "iopub.status.busy": "2026-07-04T20:00:55.070113Z", "iopub.status.idle": "2026-07-04T20:00:55.073720Z", "shell.execute_reply": "2026-07-04T20:00:55.073268Z" } }, "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 }