{ "cells": [ { "cell_type": "markdown", "id": "2f04eee0-5928-4e74-a754-6dc2e528810c", "metadata": {}, "source": [ "# PCSkinAndBlood" ] }, { "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:14:39.052447Z", "iopub.status.busy": "2024-03-05T21:14:39.052041Z", "iopub.status.idle": "2024-03-05T21:14:40.431161Z", "shell.execute_reply": "2024-03-05T21:14:40.430851Z" } }, "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:14:40.433096Z", "iopub.status.busy": "2024-03-05T21:14:40.432919Z", "iopub.status.idle": "2024-03-05T21:14:40.442206Z", "shell.execute_reply": "2024-03-05T21:14:40.441957Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "class PCSkinAndBlood(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.PCSkinAndBlood)" ] }, { "cell_type": "code", "execution_count": 3, "id": "78536494-f1d9-44de-8583-c89a310d2307", "metadata": { "execution": { "iopub.execute_input": "2024-03-05T21:14:40.443656Z", "iopub.status.busy": "2024-03-05T21:14:40.443570Z", "iopub.status.idle": "2024-03-05T21:14:40.445262Z", "shell.execute_reply": "2024-03-05T21:14:40.445042Z" } }, "outputs": [], "source": [ "model = pya.models.PCSkinAndBlood()" ] }, { "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:14:40.446749Z", "iopub.status.busy": "2024-03-05T21:14:40.446664Z", "iopub.status.idle": "2024-03-05T21:14:40.448670Z", "shell.execute_reply": "2024-03-05T21:14:40.448445Z" } }, "outputs": [], "source": [ "model.metadata[\"clock_name\"] = \"pcskinandblood\"\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 skin-and-blood age clock, trained against the original Horvath2 score using skin, blood, and fibroblast datasets.\"\n", "model.metadata[\"research_only\"] = None\n", "model.metadata[\"tissue\"] = [\"skin\", \"whole blood\", \"cultured fibroblasts\"] # Paper: The training datasets represent skin, whole blood, fibroblasts.\n", "model.metadata[\"predicts\"] = [\"chronological age\"] # Paper: The Horvath2 PC model predicts the original skin-and-blood clock value using substitute datasets.\n", "model.metadata[\"training_target\"] = [\"skin-and-blood clock output\"] # Paper: The Horvath2 PC model predicts the original skin-and-blood clock value using 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": "9d89c9e7-8e4c-4593-91b0-63a9aa57108c", "metadata": { "execution": { "iopub.execute_input": "2024-03-05T21:14:40.450140Z", "iopub.status.busy": "2024-03-05T21:14:40.450068Z", "iopub.status.idle": "2024-03-05T21:22:30.771042Z", "shell.execute_reply": "2024-03-05T21:22:30.770075Z" } }, "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:22:30.777526Z", "iopub.status.busy": "2024-03-05T21:22:30.777110Z", "iopub.status.idle": "2024-03-05T21:22:30.785776Z", "shell.execute_reply": "2024-03-05T21:22:30.784980Z" } }, "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(CalcPCHorvath2, \"CalcPCHorvath2.json\", digits = 10)\n", "write_json(CpGs, \"PCHorvath2CpGs.json\")\n", "write_json(imputeMissingCpGs, \"PCHorvath2ReferenceCpGBetas.json\", digits = 10)" ] }, { "cell_type": "code", "execution_count": 7, "id": "b71a6f03-61ba-462f-a2a6-a5df95e105ff", "metadata": { "execution": { "iopub.execute_input": "2024-03-05T21:22:30.789328Z", "iopub.status.busy": "2024-03-05T21:22:30.789064Z", "iopub.status.idle": "2024-03-05T21:22:54.286065Z", "shell.execute_reply": "2024-03-05T21:22:54.285759Z" } }, "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:22:54.287854Z", "iopub.status.busy": "2024-03-05T21:22:54.287751Z", "iopub.status.idle": "2024-03-05T21:22:54.292737Z", "shell.execute_reply": "2024-03-05T21:22:54.292482Z" } }, "outputs": [], "source": [ "with open('PCHorvath2CpGs.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:22:54.294442Z", "iopub.status.busy": "2024-03-05T21:22:54.294356Z", "iopub.status.idle": "2024-03-05T21:22:56.060620Z", "shell.execute_reply": "2024-03-05T21:22:56.060331Z" } }, "outputs": [], "source": [ "with open('CalcPCHorvath2.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:22:56.062463Z", "iopub.status.busy": "2024-03-05T21:22:56.062366Z", "iopub.status.idle": "2024-03-05T21:22:56.065322Z", "shell.execute_reply": "2024-03-05T21:22:56.065071Z" } }, "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:22:56.066914Z", "iopub.status.busy": "2024-03-05T21:22:56.066822Z", "iopub.status.idle": "2024-03-05T21:22:56.074254Z", "shell.execute_reply": "2024-03-05T21:22:56.074009Z" } }, "outputs": [], "source": [ "with open('PCHorvath2ReferenceCpGBetas.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:22:56.075754Z", "iopub.status.busy": "2024-03-05T21:22:56.075668Z", "iopub.status.idle": "2024-03-05T21:22:56.077261Z", "shell.execute_reply": "2024-03-05T21:22:56.077034Z" } }, "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:22:56.078614Z", "iopub.status.busy": "2024-03-05T21:22:56.078542Z", "iopub.status.idle": "2024-03-05T21:22:56.080107Z", "shell.execute_reply": "2024-03-05T21:22:56.079899Z" } }, "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:22:56.081514Z", "iopub.status.busy": "2024-03-05T21:22:56.081437Z", "iopub.status.idle": "2024-03-05T21:22:56.084454Z", "shell.execute_reply": "2024-03-05T21:22:56.084242Z" } }, "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': 'pcskinandblood',\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=139, out_features=1, bias=True)\n", ")\n", "\n", "%==================================== Model Details ====================================%\n", "Model Parameters and Weights:\n", "\n", "base_model.center: [0.6989603042602539, 0.15956708788871765, 0.6852545738220215, 0.8436605334281921, 0.06963495165109634, 0.022423643618822098, 0.3099723756313324, 0.5627376437187195, 0.8343909382820129, 0.08058885484933853, 0.039684172719717026, 0.1338687390089035, 0.07226327806711197, 0.17344607412815094, 0.4125054180622101, 0.6524730920791626, 0.6432204246520996, 0.7295728921890259, 0.08485441654920578, 0.8147767782211304, 0.7249654531478882, 0.8113170266151428, 0.8138924241065979, 0.02371947653591633, 0.6228847503662109, 0.5994234085083008, 0.27681541442871094, 0.6650522947311401, 0.3386619985103607, 0.47046491503715515]... [Tensor of shape torch.Size([78464])]\n", "base_model.rotation: [-0.004212469328194857, -0.0018427305622026324, -0.00342817441560328, -0.0019621967803686857, -0.0038084788247942924, -0.001714007230475545, 0.0009071855456568301, 0.0009312129113823175, 0.0005078349495306611, 0.0013207424199208617, 0.004016305319964886, -0.0008206570637412369, 0.002955526579171419, 0.0009135246509686112, -0.003197634592652321, 0.00868172850459814, -0.011338912881910801, 0.006802823860198259, -0.006287768483161926, -0.00478862039744854, 0.001487976755015552, -0.008474800735712051, 0.005175075959414244, -0.002744645345956087, -0.006363819353282452, 0.0027778574731200933, -0.007200028281658888, 0.003951014950871468, 0.0025292737409472466, -0.0006783857243135571]... [Tensor of shape torch.Size([78464, 139])]\n", "base_model.linear.weight: [-0.008271735161542892, 0.026615887880325317, 0.06532493233680725, 0.03855971246957779, 0.10452642291784286, -0.24639244377613068, 0.0814928412437439, -0.06538811326026917, 0.029210954904556274, -0.001272108987905085, 0.016053402796387672, 0.08029855042695999, -0.012919272296130657, -0.06098422035574913, 0.028758035972714424, -0.12004351615905762, -0.020917272195219994, -0.04205343499779701, 0.029078252613544464, -0.01387680321931839, 0.06542300432920456, -0.03968583419919014, 0.002720639808103442, -0.0522029809653759, 0.002719870302826166, 0.013684911653399467, 0.014434539712965488, -0.0017883781110867858, -0.07386524230241776, -0.02207360416650772]... [Tensor of shape torch.Size([1, 139])]\n", "base_model.linear.bias: tensor([0.4743])\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:22:56.086104Z", "iopub.status.busy": "2024-03-05T21:22:56.085997Z", "iopub.status.idle": "2024-03-05T21:22:56.125324Z", "shell.execute_reply": "2024-03-05T21:22:56.124964Z" } }, "outputs": [ { "data": { "text/plain": [ "tensor([[8.7096],\n", " [4.7385],\n", " [4.5074],\n", " [1.4422],\n", " [2.7803],\n", " [0.5132],\n", " [0.3571],\n", " [1.6538],\n", " [9.6653],\n", " [3.8396]], 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:22:56.127334Z", "iopub.status.busy": "2024-03-05T21:22:56.127230Z", "iopub.status.idle": "2024-03-05T21:22:56.262064Z", "shell.execute_reply": "2024-03-05T21:22:56.261645Z" } }, "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:22:56.264087Z", "iopub.status.busy": "2024-03-05T21:22:56.263973Z", "iopub.status.idle": "2024-03-05T21:22:56.352344Z", "shell.execute_reply": "2024-03-05T21:22:56.352037Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Deleted file: PCHorvath2ReferenceCpGBetas.json\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Deleted file: CalcAllPCClocks.RData\n", "Deleted file: PCHorvath2CpGs.json\n", "Deleted file: download.r\n", "Deleted file: CalcPCHorvath2.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 }