{ "cells": [ { "cell_type": "markdown", "id": "2f04eee0-5928-4e74-a754-6dc2e528810c", "metadata": {}, "source": [ "# PCHorvath2013" ] }, { "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:01:00.984260Z", "iopub.status.busy": "2024-03-05T21:01:00.983856Z", "iopub.status.idle": "2024-03-05T21:01:02.504320Z", "shell.execute_reply": "2024-03-05T21:01:02.504017Z" } }, "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:01:02.506190Z", "iopub.status.busy": "2024-03-05T21:01:02.506029Z", "iopub.status.idle": "2024-03-05T21:01:02.515402Z", "shell.execute_reply": "2024-03-05T21:01:02.515149Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "class PCHorvath2013(pyagingModel):\n", " def __init__(self):\n", " super().__init__()\n", "\n", " def preprocess(self, x):\n", " return x\n", "\n", " def postprocess(self, x):\n", " \"\"\"\n", " Applies an anti-logarithmic linear transformation to a PyTorch tensor.\n", " \"\"\"\n", " adult_age = 20\n", "\n", " # Create a mask for negative and non-negative values\n", " mask_negative = x < 0\n", " mask_non_negative = ~mask_negative\n", "\n", " # Initialize the result tensor\n", " age_tensor = torch.empty_like(x)\n", "\n", " # Exponential transformation for negative values\n", " age_tensor[mask_negative] = (1 + adult_age) * torch.exp(x[mask_negative]) - 1\n", "\n", " # Linear transformation for non-negative values\n", " age_tensor[mask_non_negative] = (1 + adult_age) * x[\n", " mask_non_negative\n", " ] + adult_age\n", "\n", " return age_tensor\n", "\n" ] } ], "source": [ "def print_entire_class(cls):\n", " source = inspect.getsource(cls)\n", " print(source)\n", "\n", "print_entire_class(pya.models.PCHorvath2013)" ] }, { "cell_type": "code", "execution_count": 3, "id": "78536494-f1d9-44de-8583-c89a310d2307", "metadata": { "execution": { "iopub.execute_input": "2024-03-05T21:01:02.516852Z", "iopub.status.busy": "2024-03-05T21:01:02.516771Z", "iopub.status.idle": "2024-03-05T21:01:02.518516Z", "shell.execute_reply": "2024-03-05T21:01:02.518259Z" } }, "outputs": [], "source": [ "model = pya.models.PCHorvath2013()" ] }, { "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:01:02.520071Z", "iopub.status.busy": "2024-03-05T21:01:02.519980Z", "iopub.status.idle": "2024-03-05T21:01:02.522034Z", "shell.execute_reply": "2024-03-05T21:01:02.521798Z" } }, "outputs": [], "source": [ "model.metadata[\"clock_name\"] = \"pchorvath2013\"\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 proxy of the 2013 Horvath pan-tissue clock, trained against the original clock score using substituted multi-tissue datasets.\"\n", "model.metadata[\"research_only\"] = None\n", "model.metadata[\"tissue\"] = [\"multi-tissue\"] # Paper: The training datasets represent multi-tissue.\n", "model.metadata[\"predicts\"] = [\"chronological age\"] # Paper: The Horvath1 PC model predicts the original CpG clock value using multi-tissue substitute datasets.\n", "model.metadata[\"training_target\"] = [\"Horvath clock output\"] # Paper: The Horvath1 PC model predicts the original CpG clock value using multi-tissue substitute datasets.\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\"] = \"all ages\" # 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": "c8f14b7d-2d57-4516-97ed-2c3a47c0732e", "metadata": { "execution": { "iopub.execute_input": "2024-03-05T21:01:02.523426Z", "iopub.status.busy": "2024-03-05T21:01:02.523352Z", "iopub.status.idle": "2024-03-05T21:06:57.815477Z", "shell.execute_reply": "2024-03-05T21:06:57.814153Z" } }, "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:06:57.822263Z", "iopub.status.busy": "2024-03-05T21:06:57.821863Z", "iopub.status.idle": "2024-03-05T21:06:57.830369Z", "shell.execute_reply": "2024-03-05T21:06:57.829140Z" } }, "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(CalcPCHorvath1, \"CalcPCHorvath1.json\", digits = 10)\n", "write_json(CpGs, \"PCHorvath1CpGs.json\")\n", "write_json(imputeMissingCpGs, \"PCHorvath1ReferenceCpGBetas.json\", digits = 10)" ] }, { "cell_type": "code", "execution_count": 7, "id": "b71a6f03-61ba-462f-a2a6-a5df95e105ff", "metadata": { "execution": { "iopub.execute_input": "2024-03-05T21:06:57.834701Z", "iopub.status.busy": "2024-03-05T21:06:57.834437Z", "iopub.status.idle": "2024-03-05T21:07:18.968803Z", "shell.execute_reply": "2024-03-05T21:07:18.968475Z" } }, "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:07:18.970732Z", "iopub.status.busy": "2024-03-05T21:07:18.970631Z", "iopub.status.idle": "2024-03-05T21:07:18.975473Z", "shell.execute_reply": "2024-03-05T21:07:18.975187Z" } }, "outputs": [], "source": [ "with open('PCHorvath1CpGs.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:07:18.977073Z", "iopub.status.busy": "2024-03-05T21:07:18.976993Z", "iopub.status.idle": "2024-03-05T21:07:20.454325Z", "shell.execute_reply": "2024-03-05T21:07:20.454033Z" } }, "outputs": [], "source": [ "with open('CalcPCHorvath1.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:07:20.456104Z", "iopub.status.busy": "2024-03-05T21:07:20.456011Z", "iopub.status.idle": "2024-03-05T21:07:20.458661Z", "shell.execute_reply": "2024-03-05T21:07:20.458429Z" } }, "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:07:20.460308Z", "iopub.status.busy": "2024-03-05T21:07:20.460214Z", "iopub.status.idle": "2024-03-05T21:07:20.467639Z", "shell.execute_reply": "2024-03-05T21:07:20.467424Z" } }, "outputs": [], "source": [ "with open('PCHorvath1ReferenceCpGBetas.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:07:20.469160Z", "iopub.status.busy": "2024-03-05T21:07:20.469087Z", "iopub.status.idle": "2024-03-05T21:07:20.470669Z", "shell.execute_reply": "2024-03-05T21:07:20.470435Z" } }, "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:07:20.472006Z", "iopub.status.busy": "2024-03-05T21:07:20.471932Z", "iopub.status.idle": "2024-03-05T21:07:20.473523Z", "shell.execute_reply": "2024-03-05T21:07:20.473253Z" } }, "outputs": [], "source": [ "model.postprocess_name = 'anti_log_linear'\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:07:20.474959Z", "iopub.status.busy": "2024-03-05T21:07:20.474880Z", "iopub.status.idle": "2024-03-05T21:07:20.477725Z", "shell.execute_reply": "2024-03-05T21:07:20.477458Z" } }, "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': 'pchorvath2013',\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: 'anti_log_linear'\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=120, out_features=1, bias=True)\n", ")\n", "\n", "%==================================== Model Details ====================================%\n", "Model Parameters and Weights:\n", "\n", "base_model.center: [0.7290785312652588, 0.16658970713615417, 0.7259728312492371, 0.8467999696731567, 0.07713332772254944, 0.020072568207979202, 0.35924986004829407, 0.6348602175712585, 0.8571182489395142, 0.07178357243537903, 0.046470556408166885, 0.13662011921405792, 0.07353769242763519, 0.21539008617401123, 0.5204617977142334, 0.8018048405647278, 0.5394126176834106, 0.629653811454773, 0.08743196725845337, 0.8038381934165955, 0.638761579990387, 0.823479950428009, 0.7942575216293335, 0.02160545252263546, 0.6240652799606323, 0.6269707083702087, 0.2557828724384308, 0.810107409954071, 0.42385315895080566, 0.6233302354812622]... [Tensor of shape torch.Size([78464])]\n", "base_model.rotation: [-0.0048322658985853195, 0.0016888284590095282, 0.0043016355484724045, 0.0006329840398393571, -0.0018616552697494626, 0.00015872654330451041, 0.0047627720050513744, -0.0007954642060212791, 0.001977873034775257, -0.0038264873437583447, 0.002861293265596032, -0.0020775371231138706, 0.0016555585898458958, 0.006291448138654232, 0.002375122159719467, 0.013304566964507103, -0.00033986676135100424, -0.0005006726132705808, 0.002878241939470172, -0.004465107340365648, -0.0033801733516156673, 0.00140372384339571, 0.0010448938701301813, -0.006161581724882126, -0.0026484185364097357, -0.0017679710872471333, -0.0001990400196518749, 0.0012213967274874449, 0.0035699992440640926, -0.0028738761320710182]... [Tensor of shape torch.Size([78464, 120])]\n", "base_model.linear.weight: [0.0033615094143897295, 0.005445790942758322, -0.0690847635269165, 0.04485338553786278, -0.03673980012536049, 0.02644198387861252, -0.21757060289382935, 0.12505319714546204, -0.007363998331129551, -0.0007567511056549847, 0.0277174673974514, 0.01490762084722519, 0.05097680911421776, 0.02121218666434288, 0.030744116753339767, -0.000863946508616209, 0.0261214692145586, 0.008246997371315956, -0.01691156066954136, 0.12098846584558487, -0.16675393283367157, -0.032332953065633774, 0.04423817619681358, -0.10392844676971436, 0.024131860584020615, 0.04625745117664337, -0.019460106268525124, 0.02763879857957363, 0.027718640863895416, -0.08220915496349335]... [Tensor of shape torch.Size([1, 120])]\n", "base_model.linear.bias: tensor([1.1583])\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:07:20.479197Z", "iopub.status.busy": "2024-03-05T21:07:20.479121Z", "iopub.status.idle": "2024-03-05T21:07:20.515391Z", "shell.execute_reply": "2024-03-05T21:07:20.515059Z" } }, "outputs": [ { "data": { "text/plain": [ "tensor([[59.4351],\n", " [58.9732],\n", " [36.1313],\n", " [11.8124],\n", " [37.2571],\n", " [42.6807],\n", " [35.9014],\n", " [45.7016],\n", " [61.6516],\n", " [59.3143]], 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:07:20.517358Z", "iopub.status.busy": "2024-03-05T21:07:20.517244Z", "iopub.status.idle": "2024-03-05T21:07:20.668047Z", "shell.execute_reply": "2024-03-05T21:07:20.667536Z" } }, "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:07:20.670161Z", "iopub.status.busy": "2024-03-05T21:07:20.670044Z", "iopub.status.idle": "2024-03-05T21:07:20.775559Z", "shell.execute_reply": "2024-03-05T21:07:20.775255Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Deleted file: PCHorvath1CpGs.json\n", "Deleted file: CalcPCHorvath1.json\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Deleted file: CalcAllPCClocks.RData\n", "Deleted file: download.r\n", "Deleted file: PCHorvath1ReferenceCpGBetas.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 }