{ "cells": [ { "cell_type": "markdown", "id": "2f04eee0-5928-4e74-a754-6dc2e528810c", "metadata": {}, "source": [ "# PCPhenoAge" ] }, { "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:07:23.114508Z", "iopub.status.busy": "2024-03-05T21:07:23.114066Z", "iopub.status.idle": "2024-03-05T21:07:24.587411Z", "shell.execute_reply": "2024-03-05T21:07:24.587111Z" } }, "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:07:24.589382Z", "iopub.status.busy": "2024-03-05T21:07:24.589212Z", "iopub.status.idle": "2024-03-05T21:07:24.598456Z", "shell.execute_reply": "2024-03-05T21:07:24.598224Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "class PCPhenoAge(pyagingModel):\n", " def __init__(self):\n", " super().__init__()\n", "\n", " def preprocess(self, x):\n", " return x\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.PCPhenoAge)" ] }, { "cell_type": "code", "execution_count": 3, "id": "78536494-f1d9-44de-8583-c89a310d2307", "metadata": { "execution": { "iopub.execute_input": "2024-03-05T21:07:24.599881Z", "iopub.status.busy": "2024-03-05T21:07:24.599795Z", "iopub.status.idle": "2024-03-05T21:07:24.601491Z", "shell.execute_reply": "2024-03-05T21:07:24.601262Z" } }, "outputs": [], "source": [ "model = pya.models.PCPhenoAge()" ] }, { "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:07:24.602887Z", "iopub.status.busy": "2024-03-05T21:07:24.602817Z", "iopub.status.idle": "2024-03-05T21:07:24.604832Z", "shell.execute_reply": "2024-03-05T21:07:24.604593Z" } }, "outputs": [], "source": [ "model.metadata[\"clock_name\"] = \"pcphenoage\"\n", "model.metadata[\"data_type\"] = \"DNA methylation\" # Paper: The study constructs clocks from DNA methylation measurements.\n", "model.metadata[\"species\"] = \"Homo sapiens\" # Paper: The analyzed samples and clock are human.\n", "model.metadata[\"year\"] = 2022\n", "model.metadata[\"approved_by_author\"] = \"⌛\"\n", "model.metadata[\"citation\"] = \"Higgins-Chen, Albert T., et al. \\\"A computational solution for bolstering reliability of epigenetic clocks: implications for clinical trials and longitudinal tracking.\\\" Nature Aging 2 (2022): 644–661.\"\n", "model.metadata[\"doi\"] = \"https://doi.org/10.1038/s43587-022-00248-2\"\n", "model.metadata[\"notes\"] = \"Principal-component DNAm PhenoAge model trained directly on phenotypic-age scores rather than as a proxy of the original CpG clock.\"\n", "model.metadata[\"research_only\"] = None\n", "model.metadata[\"tissue\"] = [\"whole blood\"] # Paper: The training datasets represent whole blood.\n", "model.metadata[\"predicts\"] = [\"phenotypic age\"] # Paper: PCPhenoAge was trained directly on phenotypic-age scores calculated from clinical biomarkers.\n", "model.metadata[\"training_target\"] = [\"phenotypic age\"] # Paper: PCPhenoAge was trained directly on phenotypic-age scores calculated from clinical biomarkers.\n", "model.metadata[\"unit\"] = [\"years\"] # Paper: The reported output scale is years.\n", "model.metadata[\"model_type\"] = \"PCA + elastic net regression\" # Paper: Principal components were supplied to elastic-net regression.\n", "model.metadata[\"platform\"] = [\"Illumina 450K\", \"Illumina EPIC\"] # Paper: The listed arrays are those used for the model's training datasets.\n", "model.metadata[\"population\"] = \"adults\" # Paper: Training cohorts and sample types are specified in the study dataset table.\n", "model.metadata[\"journal\"] = \"Nature Aging\"\n", "model.metadata[\"last_author\"] = \"Morgan E. Levine\"\n", "model.metadata[\"n_features\"] = 78464\n", "model.metadata[\"citations\"] = 497\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": "code", "execution_count": 5, "id": "3de8fbd2-aa64-487f-a33a-6a054f1572a7", "metadata": { "execution": { "iopub.execute_input": "2024-03-05T21:07:24.606345Z", "iopub.status.busy": "2024-03-05T21:07:24.606271Z", "iopub.status.idle": "2024-03-05T21:12:56.500269Z", "shell.execute_reply": "2024-03-05T21:12:56.499203Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "|-----------> Downloading data to ./CalcAllPCClocks.RData\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "\r", "|-----------> in progress: 1.0003%" ] }, { "name": "stdout", "output_type": "stream", "text": [ "\r", "|-----------> in progress: 2.0006%" ] }, { "name": "stdout", "output_type": "stream", "text": [ "\r", "|-----------> in progress: 3.0009%" ] }, { "name": "stdout", "output_type": "stream", "text": [ "\r", "|-----------> in progress: 4.0012%" ] }, { "name": "stdout", "output_type": "stream", "text": [ "\r", "|-----------> in progress: 5.0015%" ] }, { "name": "stdout", "output_type": "stream", "text": [ "\r", "|-----------> in progress: 6.0018%" ] }, { "name": "stdout", "output_type": "stream", "text": [ "\r", "|-----------> in progress: 7.0021%" ] }, { "name": "stdout", "output_type": "stream", "text": [ "\r", "|-----------> in progress: 8.0024%" ] }, { "name": "stdout", "output_type": "stream", "text": [ "\r", "|-----------> in progress: 9.0027%" ] }, { "name": "stdout", "output_type": "stream", "text": [ "\r", "|-----------> in progress: 10.0030%" ] }, { "name": "stdout", "output_type": "stream", "text": [ "\r", "|-----------> in progress: 11.0033%" ] }, { "name": "stdout", "output_type": "stream", "text": [ "\r", "|-----------> in progress: 12.0036%" ] }, { "name": "stdout", "output_type": "stream", "text": [ "\r", "|-----------> in progress: 13.0039%" ] }, { "name": "stdout", "output_type": "stream", "text": [ "\r", "|-----------> in progress: 14.0042%" ] }, { "name": "stdout", "output_type": "stream", "text": [ "\r", "|-----------> in progress: 15.0045%" ] }, { "name": "stdout", "output_type": "stream", "text": [ "\r", "|-----------> in progress: 16.0048%" ] }, { "name": "stdout", "output_type": "stream", "text": [ "\r", "|-----------> in progress: 17.0051%" ] }, { "name": "stdout", "output_type": "stream", "text": [ "\r", "|-----------> in progress: 18.0054%" ] }, { "name": "stdout", "output_type": "stream", "text": [ "\r", "|-----------> in progress: 19.0057%" ] }, { "name": "stdout", "output_type": "stream", "text": [ "\r", "|-----------> in progress: 20.0060%" ] }, { "name": "stdout", "output_type": "stream", "text": [ "\r", "|-----------> in progress: 21.0063%" ] }, { "name": "stdout", "output_type": "stream", "text": [ "\r", "|-----------> in progress: 22.0066%" ] }, { "name": "stdout", "output_type": "stream", "text": [ "\r", "|-----------> in progress: 23.0069%" ] }, { "name": "stdout", "output_type": "stream", "text": [ "\r", "|-----------> in progress: 24.0072%" ] }, { "name": "stdout", "output_type": "stream", "text": [ "\r", "|-----------> in progress: 25.0075%" ] }, { "name": "stdout", "output_type": "stream", "text": [ "\r", "|-----------> in progress: 26.0078%" ] }, { "name": "stdout", "output_type": "stream", "text": [ "\r", "|-----------> in progress: 27.0081%" ] }, { "name": "stdout", "output_type": "stream", "text": [ "\r", "|-----------> in progress: 28.0084%" ] }, { "name": "stdout", "output_type": "stream", "text": [ "\r", "|-----------> in progress: 29.0087%" ] }, { "name": "stdout", "output_type": "stream", "text": [ "\r", "|-----------> in progress: 30.0090%" ] }, { "name": "stdout", "output_type": "stream", "text": [ "\r", "|-----------> in progress: 31.0093%" ] }, { "name": "stdout", "output_type": "stream", "text": [ "\r", "|-----------> in progress: 32.0096%" ] }, { "name": "stdout", "output_type": "stream", "text": [ "\r", "|-----------> in progress: 33.0099%" ] }, { "name": "stdout", "output_type": "stream", "text": [ "\r", "|-----------> in progress: 34.0102%" ] }, { "name": "stdout", "output_type": "stream", "text": [ "\r", "|-----------> in progress: 35.0105%" ] }, { "name": "stdout", "output_type": "stream", "text": [ "\r", "|-----------> in progress: 36.0108%" ] }, { "name": "stdout", "output_type": "stream", "text": [ "\r", "|-----------> in progress: 37.0111%" ] }, { "name": "stdout", "output_type": "stream", "text": [ "\r", "|-----------> in progress: 38.0114%" ] }, { "name": "stdout", "output_type": "stream", "text": [ "\r", "|-----------> in progress: 39.0117%" ] }, { "name": "stdout", "output_type": "stream", "text": [ "\r", "|-----------> in progress: 40.0120%" ] }, { "name": "stdout", "output_type": "stream", "text": [ "\r", "|-----------> in progress: 41.0123%" ] }, { "name": "stdout", "output_type": "stream", "text": [ "\r", "|-----------> in progress: 42.0126%" ] }, { "name": "stdout", "output_type": "stream", "text": [ "\r", "|-----------> in progress: 43.0129%" ] }, { "name": "stdout", "output_type": "stream", "text": [ "\r", "|-----------> in progress: 44.0132%" ] }, { "name": "stdout", "output_type": "stream", "text": [ "\r", "|-----------> in progress: 45.0135%" ] }, { "name": "stdout", "output_type": "stream", "text": [ "\r", "|-----------> in progress: 46.0138%" ] }, { "name": "stdout", "output_type": "stream", "text": [ "\r", "|-----------> in progress: 47.0141%" ] }, { "name": "stdout", "output_type": "stream", "text": [ "\r", "|-----------> in progress: 48.0144%" ] }, { "name": "stdout", "output_type": "stream", "text": [ "\r", "|-----------> in progress: 49.0147%" ] }, { "name": "stdout", "output_type": "stream", "text": [ "\r", "|-----------> in progress: 50.0150%" ] }, { "name": "stdout", "output_type": "stream", "text": [ "\r", "|-----------> in progress: 51.0153%" ] }, { "name": "stdout", "output_type": "stream", "text": [ "\r", "|-----------> in progress: 52.0156%" ] }, { "name": "stdout", "output_type": "stream", "text": [ "\r", "|-----------> in progress: 53.0159%" ] }, { "name": "stdout", "output_type": "stream", "text": [ "\r", "|-----------> in progress: 54.0162%" ] }, { "name": "stdout", "output_type": "stream", "text": [ "\r", "|-----------> in progress: 55.0165%" ] }, { "name": "stdout", "output_type": "stream", "text": [ "\r", "|-----------> in progress: 56.0168%" ] }, { "name": "stdout", "output_type": "stream", "text": [ "\r", "|-----------> in progress: 57.0171%" ] }, { "name": "stdout", "output_type": "stream", "text": [ "\r", "|-----------> in progress: 58.0174%" ] }, { "name": "stdout", "output_type": "stream", "text": [ "\r", "|-----------> in progress: 59.0177%" ] }, { "name": "stdout", "output_type": "stream", "text": [ "\r", "|-----------> in progress: 60.0180%" ] }, { "name": "stdout", "output_type": "stream", "text": [ "\r", "|-----------> in progress: 61.0183%" ] }, { "name": "stdout", "output_type": "stream", "text": [ "\r", "|-----------> in progress: 62.0186%" ] }, { "name": "stdout", "output_type": "stream", "text": [ "\r", "|-----------> in progress: 63.0189%" ] }, { "name": "stdout", "output_type": "stream", "text": [ "\r", "|-----------> in progress: 64.0192%" ] }, { "name": "stdout", "output_type": "stream", "text": [ "\r", "|-----------> in progress: 65.0195%" ] }, { "name": "stdout", "output_type": "stream", "text": [ "\r", "|-----------> in progress: 66.0198%" ] }, { "name": "stdout", "output_type": "stream", "text": [ "\r", "|-----------> in progress: 67.0201%" ] }, { "name": "stdout", "output_type": "stream", "text": [ "\r", "|-----------> in progress: 68.0204%" ] }, { "name": "stdout", "output_type": "stream", "text": [ "\r", "|-----------> in progress: 69.0207%" ] }, { "name": "stdout", "output_type": "stream", "text": [ "\r", "|-----------> in progress: 70.0210%" ] }, { "name": "stdout", "output_type": "stream", "text": [ "\r", "|-----------> in progress: 71.0213%" ] }, { "name": "stdout", "output_type": "stream", "text": [ "\r", "|-----------> in progress: 72.0216%" ] }, { "name": "stdout", "output_type": "stream", "text": [ "\r", "|-----------> in progress: 73.0219%" ] }, { "name": "stdout", "output_type": "stream", "text": [ "\r", "|-----------> in progress: 74.0222%" ] }, { "name": "stdout", "output_type": "stream", "text": [ "\r", "|-----------> in progress: 75.0225%" ] }, { "name": "stdout", "output_type": "stream", "text": [ "\r", "|-----------> in progress: 76.0228%" ] }, { "name": "stdout", "output_type": "stream", "text": [ "\r", "|-----------> in progress: 77.0231%" ] }, { "name": "stdout", "output_type": "stream", "text": [ "\r", "|-----------> in progress: 78.0234%" ] }, { "name": "stdout", "output_type": "stream", "text": [ "\r", "|-----------> in progress: 79.0237%" ] }, { "name": "stdout", "output_type": "stream", "text": [ "\r", "|-----------> in progress: 80.0240%" ] }, { "name": "stdout", "output_type": "stream", "text": [ "\r", "|-----------> in progress: 81.0243%" ] }, { "name": "stdout", "output_type": "stream", "text": [ "\r", "|-----------> in progress: 82.0246%" ] }, { "name": "stdout", "output_type": "stream", "text": [ "\r", "|-----------> in progress: 83.0249%" ] }, { "name": "stdout", "output_type": "stream", "text": [ "\r", "|-----------> in progress: 84.0252%" ] }, { "name": "stdout", "output_type": "stream", "text": [ "\r", "|-----------> in progress: 85.0255%" ] }, { "name": "stdout", "output_type": "stream", "text": [ "\r", "|-----------> in progress: 86.0258%" ] }, { "name": "stdout", "output_type": "stream", "text": [ "\r", "|-----------> in progress: 87.0261%" ] }, { "name": "stdout", "output_type": "stream", "text": [ "\r", "|-----------> in progress: 88.0264%" ] }, { "name": "stdout", "output_type": "stream", "text": [ "\r", "|-----------> in progress: 89.0267%" ] }, { "name": "stdout", "output_type": "stream", "text": [ "\r", "|-----------> in progress: 90.0270%" ] }, { "name": "stdout", "output_type": "stream", "text": [ "\r", "|-----------> in progress: 91.0273%" ] }, { "name": "stdout", "output_type": "stream", "text": [ "\r", "|-----------> in progress: 92.0276%" ] }, { "name": "stdout", "output_type": "stream", "text": [ "\r", "|-----------> in progress: 93.0279%" ] }, { "name": "stdout", "output_type": "stream", "text": [ "\r", "|-----------> in progress: 94.0282%" ] }, { "name": "stdout", "output_type": "stream", "text": [ "\r", "|-----------> in progress: 95.0285%" ] }, { "name": "stdout", "output_type": "stream", "text": [ "\r", "|-----------> in progress: 96.0288%" ] }, { "name": "stdout", "output_type": "stream", "text": [ "\r", "|-----------> in progress: 97.0291%" ] }, { "name": "stdout", "output_type": "stream", "text": [ "\r", "|-----------> in progress: 98.0294%" ] }, { "name": "stdout", "output_type": "stream", "text": [ "\r", "|-----------> in progress: 99.0297%" ] }, { "name": "stdout", "output_type": "stream", "text": [ "\r", "|-----------> in progress: 100.0000%\n" ] } ], "source": [ "#download PCClock Rdata file from https://yale.app.box.com/s/kq0b0a7lxckxjvaz7x5n4keaug7tewry\n", "logger = pya.logger.Logger()\n", "url = \"https://huggingface.co/lucascamillomd/pyaging-data/resolve/main/supporting_files/CalcAllPCClocks.RData\"\n", "dir = \".\"\n", "pya.utils.download(url, dir, logger, indent_level=1)" ] }, { "cell_type": "markdown", "id": "971ff75c-c5bf-4e8e-89d7-01861e9bc107", "metadata": {}, "source": [ "#### Download from R package" ] }, { "cell_type": "code", "execution_count": 6, "id": "c2cfbee5-bcaf-46f0-a8d8-3b6150e09bf0", "metadata": { "execution": { "iopub.execute_input": "2024-03-05T21:12:56.507651Z", "iopub.status.busy": "2024-03-05T21:12:56.507178Z", "iopub.status.idle": "2024-03-05T21:12:56.515393Z", "shell.execute_reply": "2024-03-05T21:12:56.514506Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Writing download.r\n" ] } ], "source": [ "%%writefile download.r\n", "\n", "library(dplyr)\n", "library(tibble)\n", "library(tidyr)\n", "library(jsonlite)\n", "\n", "load(file = \"CalcAllPCClocks.RData\")\n", "\n", "print(ls(all.names = TRUE))\n", "\n", "write_json(CalcPCPhenoAge, \"CalcPCPhenoAge.json\", digits = 10)\n", "write_json(CpGs, \"PCPhenoAgeCpGs.json\")\n", "write_json(imputeMissingCpGs, \"PCPhenoAgeReferenceCpGBetas.json\", digits = 10)" ] }, { "cell_type": "code", "execution_count": 7, "id": "b71a6f03-61ba-462f-a2a6-a5df95e105ff", "metadata": { "execution": { "iopub.execute_input": "2024-03-05T21:12:56.520095Z", "iopub.status.busy": "2024-03-05T21:12:56.519783Z", "iopub.status.idle": "2024-03-05T21:14:27.585829Z", "shell.execute_reply": "2024-03-05T21:14:27.585319Z" } }, "outputs": [ { "data": { "text/plain": [ "0" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "os.system(\"Rscript download.r\")" ] }, { "cell_type": "markdown", "id": "a14c7fc1-abe5-42a3-8bc9-0987521ddf33", "metadata": {}, "source": [ "## Load features" ] }, { "cell_type": "markdown", "id": "3e737582-3a28-4f55-8da9-3e34125362cc", "metadata": {}, "source": [ "#### From JSON file" ] }, { "cell_type": "code", "execution_count": 8, "id": "97e5b47b-0599-4ec3-aab4-dcfe9d3e4515", "metadata": { "execution": { "iopub.execute_input": "2024-03-05T21:14:27.596821Z", "iopub.status.busy": "2024-03-05T21:14:27.596691Z", "iopub.status.idle": "2024-03-05T21:14:27.603011Z", "shell.execute_reply": "2024-03-05T21:14:27.602759Z" } }, "outputs": [], "source": [ "with open('PCPhenoAgeCpGs.json', 'r') as f:\n", " model.features = json.load(f)" ] }, { "cell_type": "markdown", "id": "ee6d8fa0-4767-4c45-9717-eb1c95e2ddc0", "metadata": {}, "source": [ "## Load weights into base model" ] }, { "cell_type": "markdown", "id": "de92ee28-39b1-4356-a734-6b28a20e7bfe", "metadata": {}, "source": [ "#### From JSON file" ] }, { "cell_type": "code", "execution_count": 9, "id": "e09b3463-4fd4-41b1-ac21-e63ddd223fe0", "metadata": { "execution": { "iopub.execute_input": "2024-03-05T21:14:27.604702Z", "iopub.status.busy": "2024-03-05T21:14:27.604623Z", "iopub.status.idle": "2024-03-05T21:14:35.620726Z", "shell.execute_reply": "2024-03-05T21:14:35.620383Z" } }, "outputs": [], "source": [ "with open('CalcPCPhenoAge.json', 'r') as f:\n", " weights_dict = json.load(f)\n", "\n", "weights = torch.tensor(weights_dict['model']).unsqueeze(0).float()\n", "intercept = torch.tensor(weights_dict['intercept']).float()\n", "center = torch.tensor(weights_dict['center']).float()\n", "rotation = torch.tensor(weights_dict['rotation']).float()" ] }, { "cell_type": "markdown", "id": "a5fb949f-500a-49d0-a792-2a774b0de18f", "metadata": {}, "source": [ "#### PC linear model" ] }, { "cell_type": "code", "execution_count": 10, "id": "b6e1baed-fb83-4b97-a2e7-c2f17254ca47", "metadata": { "execution": { "iopub.execute_input": "2024-03-05T21:14:35.622683Z", "iopub.status.busy": "2024-03-05T21:14:35.622585Z", "iopub.status.idle": "2024-03-05T21:14:35.626845Z", "shell.execute_reply": "2024-03-05T21:14:35.626586Z" } }, "outputs": [], "source": [ "base_model = pya.models.PCLinearModel(input_dim=len(model.features), pc_dim=rotation.shape[1])\n", "\n", "base_model.center.data = center.float()\n", "base_model.rotation.data = rotation.float()\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": "93271aee-d045-45ba-b030-7ec8e57add42", "metadata": {}, "source": [ "#### From JSON file" ] }, { "cell_type": "code", "execution_count": 11, "id": "2089b66f-9cc4-4528-9bdc-5e45efc6d06b", "metadata": { "execution": { "iopub.execute_input": "2024-03-05T21:14:35.628566Z", "iopub.status.busy": "2024-03-05T21:14:35.628479Z", "iopub.status.idle": "2024-03-05T21:14:35.636912Z", "shell.execute_reply": "2024-03-05T21:14:35.636675Z" } }, "outputs": [], "source": [ "with open('PCPhenoAgeReferenceCpGBetas.json', 'r') as f:\n", " reference_feature_values = json.load(f)\n", "model.reference_values = reference_feature_values" ] }, { "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:14:35.638418Z", "iopub.status.busy": "2024-03-05T21:14:35.638337Z", "iopub.status.idle": "2024-03-05T21:14:35.640135Z", "shell.execute_reply": "2024-03-05T21:14:35.639905Z" } }, "outputs": [], "source": [ "model.preprocess_name = None\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:14:35.641495Z", "iopub.status.busy": "2024-03-05T21:14:35.641423Z", "iopub.status.idle": "2024-03-05T21:14:35.643050Z", "shell.execute_reply": "2024-03-05T21:14:35.642809Z" } }, "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:14:35.644518Z", "iopub.status.busy": "2024-03-05T21:14:35.644442Z", "iopub.status.idle": "2024-03-05T21:14:35.647537Z", "shell.execute_reply": "2024-03-05T21:14:35.647305Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "%==================================== Model Details ====================================%\n", "Model Attributes:\n", "\n", "training: True\n", "metadata: {'approved_by_author': '⌛',\n", " 'citation': 'Higgins-Chen, Albert T., et al. \"A computational solution for '\n", " 'bolstering reliability of epigenetic clocks: Implications for '\n", " 'clinical trials and longitudinal tracking.\" Nature aging 2.7 '\n", " '(2022): 644-661.',\n", " 'clock_name': 'pcphenoage',\n", " 'data_type': 'methylation',\n", " 'doi': 'https://doi.org/10.1038/s43587-022-00248-2',\n", " 'notes': None,\n", " 'research_only': None,\n", " 'species': 'Homo sapiens',\n", " 'version': None,\n", " 'year': 2022}\n", "reference_values: [0.82635363384, 0.18898814441, 0.72938889209, 0.8680421375, 0.090353927561, 0.0066895021761, 0.48924643338, 0.87262052546, 0.87955373232, 0.04847264273, 0.0093070979947, 0.16393676218, 0.058440936082, 0.18857484916, 0.58239394253, 0.86564960457, 0.58457176982, 0.82903550669, 0.065646928047, 0.8500055061, 0.79155429878, 0.83499889314, 0.7754384128, 0.0039641831799, 0.50570339787, 0.60547040884, 0.29093154314, 0.88154845595, 0.46844171936, 0.79205361021]... [Total elements: 78464]\n", "preprocess_name: None\n", "preprocess_dependencies: None\n", "postprocess_name: None\n", "postprocess_dependencies: None\n", "features: ['cg00000292', 'cg00000714', 'cg00001099', 'cg00001446', 'cg00001747', 'cg00002116', 'cg00002224', 'cg00002426', 'cg00002646', 'cg00002660', 'cg00002719', 'cg00002810', 'cg00003091', 'cg00003287', 'cg00003345', 'cg00003529', 'cg00003578', 'cg00003625', 'cg00003994', 'cg00004429', 'cg00004608', 'cg00004806', 'cg00005072', 'cg00005306', 'cg00005619', 'cg00005849', 'cg00006081', 'cg00006459', 'cg00007076', 'cg00007221']... [Total elements: 78464]\n", "base_model_features: None\n", "\n", "%==================================== Model Details ====================================%\n", "Model Structure:\n", "\n", "base_model: PCLinearModel(\n", " (linear): Linear(in_features=651, out_features=1, bias=True)\n", ")\n", "\n", "%==================================== Model Details ====================================%\n", "Model Parameters and Weights:\n", "\n", "base_model.center: [0.8411704301834106, 0.1685701608657837, 0.7557920217514038, 0.9095144271850586, 0.057498179376125336, 0.01194298267364502, 0.4872075021266937, 0.909848690032959, 0.8732143640518188, 0.03602154180407524, 0.061301641166210175, 0.12556762993335724, 0.03800154849886894, 0.16473431885242462, 0.5713522434234619, 0.8393409848213196, 0.5687119960784912, 0.859745442867279, 0.06355176866054535, 0.9047518372535706, 0.8930169343948364, 0.8788580298423767, 0.8006214499473572, 0.014365476556122303, 0.4905247688293457, 0.5528188347816467, 0.27434754371643066, 0.867400050163269, 0.4297264516353607, 0.8199979066848755]... [Tensor of shape torch.Size([78464])]\n", "base_model.rotation: [-0.00031331178615801036, 0.0009534272830933332, 0.00047261983854696155, -0.003930294886231422, 0.0005094414227642119, 0.0006895905244164169, -0.002100678626447916, -0.0012287608115002513, -0.00193792674690485, -0.006593621335923672, 0.00211642705835402, 0.0008730596164241433, 0.005285773426294327, 0.004182749427855015, -0.0009547793306410313, -0.002365315333008766, 0.0030447160825133324, -0.0016476826276630163, 0.0010397256119176745, 0.00157217460218817, -0.0012130382237955928, 4.213314241496846e-05, -0.001991128083318472, 0.0008436249918304384, 0.0022273382637649775, 4.255296153132804e-05, 0.002220186637714505, 0.0004465262754820287, -0.0006061694002710283, -0.0029561042319983244]... [Tensor of shape torch.Size([78464, 651])]\n", "base_model.linear.weight: [0.5824476480484009, -0.5351578593254089, -0.5155732035636902, 2.113274335861206, 2.877065658569336, 0.25946056842803955, -0.9349619150161743, -3.642695188522339, -0.8174840807914734, 0.7983757853507996, 4.807836055755615, -1.8483978509902954, 0.1970488429069519, -0.08622869849205017, -0.17043375968933105, -1.4405218362808228, -0.4617669880390167, 0.22850117087364197, 2.2766926288604736, 2.9017574787139893, 1.6008095741271973, -0.8295918107032776, -0.13391417264938354, -2.9964559078216553, -2.1537444591522217, -0.04686202108860016, 0.5735985040664673, -2.7465732097625732, -1.1616591215133667, 0.6878449320793152]... [Tensor of shape torch.Size([1, 651])]\n", "base_model.linear.bias: tensor([68.1700])\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:14:35.648966Z", "iopub.status.busy": "2024-03-05T21:14:35.648874Z", "iopub.status.idle": "2024-03-05T21:14:35.745409Z", "shell.execute_reply": "2024-03-05T21:14:35.745078Z" } }, "outputs": [ { "data": { "text/plain": [ "tensor([[ 18.8880],\n", " [ 55.6389],\n", " [ 29.9992],\n", " [-19.9164],\n", " [ 26.4750],\n", " [ 42.4600],\n", " [ 11.2694],\n", " [ 45.2477],\n", " [ 77.4414],\n", " [ 47.4068]], 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:14:35.747528Z", "iopub.status.busy": "2024-03-05T21:14:35.747415Z", "iopub.status.idle": "2024-03-05T21:14:36.340163Z", "shell.execute_reply": "2024-03-05T21:14:36.339846Z" } }, "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:14:36.343688Z", "iopub.status.busy": "2024-03-05T21:14:36.343586Z", "iopub.status.idle": "2024-03-05T21:14:36.441166Z", "shell.execute_reply": "2024-03-05T21:14:36.440857Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Deleted file: CalcPCPhenoAge.json\n", "Deleted file: CalcAllPCClocks.RData\n", "Deleted file: PCPhenoAgeReferenceCpGBetas.json\n", "Deleted file: download.r\n", "Deleted file: PCPhenoAgeCpGs.json\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 }