{ "cells": [ { "cell_type": "markdown", "id": "2f04eee0-5928-4e74-a754-6dc2e528810c", "metadata": {}, "source": [ "# PCGrimAge" ] }, { "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-05T20:41:06.545614Z", "iopub.status.busy": "2024-03-05T20:41:06.545048Z", "iopub.status.idle": "2024-03-05T20:41:08.068446Z", "shell.execute_reply": "2024-03-05T20:41:08.068122Z" } }, "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-05T20:41:08.070463Z", "iopub.status.busy": "2024-03-05T20:41:08.070284Z", "iopub.status.idle": "2024-03-05T20:41:08.080131Z", "shell.execute_reply": "2024-03-05T20:41:08.079846Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "class PCGrimAge(pyagingModel):\n", " def __init__(self):\n", " super().__init__()\n", "\n", " self.center = nn.Parameter(torch.empty(78464), requires_grad=False)\n", " self.rotation = nn.Parameter(torch.empty((78464, 1933)), requires_grad=False)\n", "\n", " self.PCPACKYRS = None\n", " self.PCADM = None\n", " self.PCB2M = None\n", " self.PCCystatinC = None\n", " self.PCGDF15 = None\n", " self.PCLeptin = None\n", " self.PCPAI1 = None\n", " self.PCTIMP1 = None\n", "\n", " self.features_PCPACKYRS = None\n", " self.features_PCADM = None\n", " self.features_PCB2M = None\n", " self.features_PCCystatinC = None\n", " self.features_PCGDF15 = None\n", " self.features_PCLeptin = None\n", " self.features_PCPAI1 = None\n", " self.features_PCTIMP1 = None\n", "\n", " def forward(self, x):\n", " CpGs = x[:, :-2]\n", " Female = x[:, -2].unsqueeze(1)\n", " Age = x[:, -1].unsqueeze(1)\n", "\n", " CpGs = CpGs - self.center # Apply centering\n", " PCs = torch.mm(CpGs, self.rotation) # Apply PCA rotation\n", "\n", " x = torch.concat([PCs, Female, Age], dim=1)\n", "\n", " PCPACKYRS = self.PCPACKYRS(x[:, self.features_PCPACKYRS])\n", " PCADM = self.PCADM(x[:, self.features_PCADM])\n", " PCB2M = self.PCB2M(x[:, self.features_PCB2M])\n", " PCCystatinC = self.PCCystatinC(x[:, self.features_PCCystatinC])\n", " PCGDF15 = self.PCGDF15(x[:, self.features_PCGDF15])\n", " PCLeptin = self.PCLeptin(x[:, self.features_PCLeptin])\n", " PCPAI1 = self.PCPAI1(x[:, self.features_PCPAI1])\n", " PCTIMP1 = self.PCTIMP1(x[:, self.features_PCTIMP1])\n", "\n", " x = torch.concat(\n", " [\n", " PCPACKYRS,\n", " PCADM,\n", " PCB2M,\n", " PCCystatinC,\n", " PCGDF15,\n", " PCLeptin,\n", " PCPAI1,\n", " PCTIMP1,\n", " Age,\n", " Female,\n", " ],\n", " dim=1,\n", " )\n", "\n", " x = self.base_model(x)\n", "\n", " return x\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.PCGrimAge)" ] }, { "cell_type": "code", "execution_count": 3, "id": "78536494-f1d9-44de-8583-c89a310d2307", "metadata": { "execution": { "iopub.execute_input": "2024-03-05T20:41:08.081608Z", "iopub.status.busy": "2024-03-05T20:41:08.081525Z", "iopub.status.idle": "2024-03-05T20:41:08.083470Z", "shell.execute_reply": "2024-03-05T20:41:08.083224Z" } }, "outputs": [], "source": [ "model = pya.models.PCGrimAge()" ] }, { "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-05T20:41:08.084952Z", "iopub.status.busy": "2024-03-05T20:41:08.084854Z", "iopub.status.idle": "2024-03-05T20:41:08.086856Z", "shell.execute_reply": "2024-03-05T20:41:08.086632Z" } }, "outputs": [], "source": [ "model.metadata[\"clock_name\"] = \"pcgrimage\"\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 trained to reproduce the original DNAm GrimAge score; age and sex are additional model inputs.\"\n", "model.metadata[\"research_only\"] = None\n", "model.metadata[\"tissue\"] = [\"whole blood\"] # Paper: The training datasets represent whole blood.\n", "model.metadata[\"predicts\"] = [\"mortality risk\"] # Paper: PCGrimAge was trained to predict the original GrimAge clock value, a mortality-predictive clock.\n", "model.metadata[\"training_target\"] = [\"DNAm GrimAge output\"] # Paper: PCGrimAge was trained to predict the original GrimAge clock value, a mortality-predictive clock.\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\"] # 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\"] = 78466\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": "a846135e-44e5-4771-8368-8fc572e4bf6b", "metadata": { "execution": { "iopub.execute_input": "2024-03-05T20:41:08.088369Z", "iopub.status.busy": "2024-03-05T20:41:08.088277Z", "iopub.status.idle": "2024-03-05T20:48:26.284835Z", "shell.execute_reply": "2024-03-05T20:48:26.283893Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "|-----------> Downloading data to ./CalcAllPCClocks.RData\n", "|-----------> 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-05T20:48:26.291263Z", "iopub.status.busy": "2024-03-05T20:48:26.290856Z", "iopub.status.idle": "2024-03-05T20:48:26.298367Z", "shell.execute_reply": "2024-03-05T20:48:26.297814Z" } }, "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", "CalcPCGrimAge$rotation.names = colnames(CalcPCGrimAge$rotation)\n", "\n", "CalcPCGrimAge$PCPACKYRS.model.names = names(CalcPCGrimAge$PCPACKYRS.model)\n", "CalcPCGrimAge$PCADM.model.names = names(CalcPCGrimAge$PCADM.model)\n", "CalcPCGrimAge$PCB2M.model.names = names(CalcPCGrimAge$PCB2M.model)\n", "CalcPCGrimAge$PCCystatinC.model.names = names(CalcPCGrimAge$PCCystatinC.model)\n", "CalcPCGrimAge$PCGDF15.model.names = names(CalcPCGrimAge$PCGDF15.model)\n", "CalcPCGrimAge$PCLeptin.model.names = names(CalcPCGrimAge$PCLeptin.model)\n", "CalcPCGrimAge$PCPAI1.model.names = names(CalcPCGrimAge$PCPAI1.model)\n", "CalcPCGrimAge$PCTIMP1.model.names = names(CalcPCGrimAge$PCTIMP1.model)\n", "\n", "write_json(CalcPCGrimAge, \"CalcPCGrimAge.json\", digits = 9)\n", "write_json(CpGs, \"PCGrimAgeCpGs.json\")\n", "write_json(imputeMissingCpGs, \"PCGrimAgeReferenceCpGBetas.json\", digits = 10)" ] }, { "cell_type": "code", "execution_count": 7, "id": "b71a6f03-61ba-462f-a2a6-a5df95e105ff", "metadata": { "execution": { "iopub.execute_input": "2024-03-05T20:48:26.303326Z", "iopub.status.busy": "2024-03-05T20:48:26.303020Z", "iopub.status.idle": "2024-03-05T20:50:02.372698Z", "shell.execute_reply": "2024-03-05T20:50:02.370237Z" } }, "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": null, "id": "97e5b47b-0599-4ec3-aab4-dcfe9d3e4515", "metadata": { "execution": { "iopub.execute_input": "2024-03-05T20:50:02.406274Z", "iopub.status.busy": "2024-03-05T20:50:02.405951Z", "iopub.status.idle": "2024-03-05T20:50:02.425145Z", "shell.execute_reply": "2024-03-05T20:50:02.424853Z" } }, "outputs": [], "source": [ "with open('PCGrimAgeCpGs.json', 'r') as f:\n", " features = json.load(f)\n", "model.features = features + ['female'] + ['age']" ] }, { "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-05T20:50:02.427843Z", "iopub.status.busy": "2024-03-05T20:50:02.427740Z", "iopub.status.idle": "2024-03-05T20:50:18.998785Z", "shell.execute_reply": "2024-03-05T20:50:18.998269Z" } }, "outputs": [], "source": [ "with open('CalcPCGrimAge.json', 'r') as f:\n", " weights_dict = json.load(f)" ] }, { "cell_type": "markdown", "id": "76331eb3-39e9-4191-aa77-384dcd058b8d", "metadata": {}, "source": [ "#### PC component" ] }, { "cell_type": "code", "execution_count": 10, "id": "81b54257-0baa-4348-b779-8616800b81b0", "metadata": { "execution": { "iopub.execute_input": "2024-03-05T20:50:19.004115Z", "iopub.status.busy": "2024-03-05T20:50:19.003970Z", "iopub.status.idle": "2024-03-05T20:50:29.057742Z", "shell.execute_reply": "2024-03-05T20:50:29.057326Z" } }, "outputs": [], "source": [ "model.center.data = torch.tensor(weights_dict['center']).float()\n", "model.rotation.data = torch.tensor(weights_dict['rotation']).float()" ] }, { "cell_type": "markdown", "id": "3958ba73-42e8-40a5-94a1-4f4b8ae05dca", "metadata": {}, "source": [ "#### Linear model" ] }, { "cell_type": "code", "execution_count": 11, "id": "321a437c-8888-4e10-96e9-5ed2826a8f74", "metadata": { "execution": { "iopub.execute_input": "2024-03-05T20:50:29.061233Z", "iopub.status.busy": "2024-03-05T20:50:29.061127Z", "iopub.status.idle": "2024-03-05T20:50:29.100961Z", "shell.execute_reply": "2024-03-05T20:50:29.100678Z" } }, "outputs": [], "source": [ "all_features = weights_dict['rotation.names'] + ['Female'] + ['Age']\n", "\n", "model.PCPACKYRS = pya.models.LinearModel(input_dim=len(weights_dict['PCPACKYRS.model.names']))\n", "model.PCPACKYRS.linear.weight.data = torch.tensor(weights_dict['PCPACKYRS.model']).unsqueeze(0).float()\n", "model.PCPACKYRS.linear.bias.data = torch.tensor(weights_dict['PCPACKYRS.intercept']).float()\n", "model.features_PCPACKYRS = indices = torch.tensor([all_features.index(item) for item in weights_dict['PCPACKYRS.model.names'] if item in all_features]).long()\n", "\n", "model.PCADM = pya.models.LinearModel(input_dim=len(weights_dict['PCADM.model.names']))\n", "model.PCADM.linear.weight.data = torch.tensor(weights_dict['PCADM.model']).unsqueeze(0).float()\n", "model.PCADM.linear.bias.data = torch.tensor(weights_dict['PCADM.intercept']).float()\n", "model.features_PCADM = indices = torch.tensor([all_features.index(item) for item in weights_dict['PCADM.model.names'] if item in all_features]).long()\n", "\n", "model.PCB2M = pya.models.LinearModel(input_dim=len(weights_dict['PCB2M.model.names']))\n", "model.PCB2M.linear.weight.data = torch.tensor(weights_dict['PCB2M.model']).unsqueeze(0).float()\n", "model.PCB2M.linear.bias.data = torch.tensor(weights_dict['PCB2M.intercept']).float()\n", "model.features_PCB2M = indices = torch.tensor([all_features.index(item) for item in weights_dict['PCB2M.model.names'] if item in all_features]).long()\n", "\n", "model.PCCystatinC = pya.models.LinearModel(input_dim=len(weights_dict['PCCystatinC.model.names']))\n", "model.PCCystatinC.linear.weight.data = torch.tensor(weights_dict['PCCystatinC.model']).unsqueeze(0).float()\n", "model.PCCystatinC.linear.bias.data = torch.tensor(weights_dict['PCCystatinC.intercept']).float()\n", "model.features_PCCystatinC = indices = torch.tensor([all_features.index(item) for item in weights_dict['PCCystatinC.model.names'] if item in all_features]).long()\n", "\n", "model.PCGDF15 = pya.models.LinearModel(input_dim=len(weights_dict['PCGDF15.model.names']))\n", "model.PCGDF15.linear.weight.data = torch.tensor(weights_dict['PCGDF15.model']).unsqueeze(0).float()\n", "model.PCGDF15.linear.bias.data = torch.tensor(weights_dict['PCGDF15.intercept']).float()\n", "model.features_PCGDF15 = indices = torch.tensor([all_features.index(item) for item in weights_dict['PCGDF15.model.names'] if item in all_features]).long()\n", "\n", "model.PCLeptin = pya.models.LinearModel(input_dim=len(weights_dict['PCLeptin.model.names']))\n", "model.PCLeptin.linear.weight.data = torch.tensor(weights_dict['PCLeptin.model']).unsqueeze(0).float()\n", "model.PCLeptin.linear.bias.data = torch.tensor(weights_dict['PCLeptin.intercept']).float()\n", "model.features_PCLeptin = indices = torch.tensor([all_features.index(item) for item in weights_dict['PCLeptin.model.names'] if item in all_features]).long()\n", "\n", "model.PCPAI1 = pya.models.LinearModel(input_dim=len(weights_dict['PCPAI1.model.names']))\n", "model.PCPAI1.linear.weight.data = torch.tensor(weights_dict['PCPAI1.model']).unsqueeze(0).float()\n", "model.PCPAI1.linear.bias.data = torch.tensor(weights_dict['PCPAI1.intercept']).float()\n", "model.features_PCPAI1 = indices = torch.tensor([all_features.index(item) for item in weights_dict['PCPAI1.model.names'] if item in all_features]).long()\n", "\n", "model.PCTIMP1 = pya.models.LinearModel(input_dim=len(weights_dict['PCTIMP1.model.names']))\n", "model.PCTIMP1.linear.weight.data = torch.tensor(weights_dict['PCTIMP1.model']).unsqueeze(0).float()\n", "model.PCTIMP1.linear.bias.data = torch.tensor(weights_dict['PCTIMP1.intercept']).float()\n", "model.features_PCTIMP1 = indices = torch.tensor([all_features.index(item) for item in weights_dict['PCTIMP1.model.names'] if item in all_features]).long()" ] }, { "cell_type": "markdown", "id": "5742dc16-e063-414f-a38e-9721beb11351", "metadata": {}, "source": [ "#### Linear model" ] }, { "cell_type": "code", "execution_count": 12, "id": "c2e54115-c17b-48ce-88f1-de546c90d2b3", "metadata": { "execution": { "iopub.execute_input": "2024-03-05T20:50:29.102588Z", "iopub.status.busy": "2024-03-05T20:50:29.102501Z", "iopub.status.idle": "2024-03-05T20:50:29.104559Z", "shell.execute_reply": "2024-03-05T20:50:29.104329Z" } }, "outputs": [], "source": [ "base_model = pya.models.LinearModel(input_dim=len(weights_dict['components']))\n", "\n", "base_model.linear.weight.data = torch.tensor(weights_dict['PCGrimAge.model']).unsqueeze(0).float()\n", "base_model.linear.bias.data = torch.tensor(weights_dict['PCGrimAge.intercept']).float()\n", "\n", "model.base_model = base_model" ] }, { "cell_type": "code", "execution_count": 13, "id": "01a2d612-c6c2-4ab2-b09a-136ed8047794", "metadata": { "execution": { "iopub.execute_input": "2024-03-05T20:50:29.105961Z", "iopub.status.busy": "2024-03-05T20:50:29.105880Z", "iopub.status.idle": "2024-03-05T20:50:29.108327Z", "shell.execute_reply": "2024-03-05T20:50:29.108090Z" } }, "outputs": [ { "data": { "text/plain": [ "['PCPACKYRS',\n", " 'PCADM',\n", " 'PCB2M',\n", " 'PCCystatinC',\n", " 'PCGDF15',\n", " 'PCLeptin',\n", " 'PCPAI1',\n", " 'PCTIMP1',\n", " 'Age',\n", " 'Female']" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "weights_dict['components']" ] }, { "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": 14, "id": "2089b66f-9cc4-4528-9bdc-5e45efc6d06b", "metadata": { "execution": { "iopub.execute_input": "2024-03-05T20:50:29.110185Z", "iopub.status.busy": "2024-03-05T20:50:29.110081Z", "iopub.status.idle": "2024-03-05T20:50:29.119182Z", "shell.execute_reply": "2024-03-05T20:50:29.118907Z" } }, "outputs": [], "source": [ "with open('PCGrimAgeReferenceCpGBetas.json', 'r') as f:\n", " reference_feature_values = json.load(f)\n", "model.reference_values = reference_feature_values + [1, 65] # 65yo F" ] }, { "cell_type": "markdown", "id": "af3bcf7b-74a8-4d21-9ccb-4de0c2b0516b", "metadata": {}, "source": [ "## Load preprocess and postprocess objects" ] }, { "cell_type": "code", "execution_count": 15, "id": "7a22fb20-c605-424d-8efb-7620c2c0755c", "metadata": { "execution": { "iopub.execute_input": "2024-03-05T20:50:29.121015Z", "iopub.status.busy": "2024-03-05T20:50:29.120920Z", "iopub.status.idle": "2024-03-05T20:50:29.122381Z", "shell.execute_reply": "2024-03-05T20:50:29.122160Z" } }, "outputs": [], "source": [ "model.preprocess_name = None\n", "model.preprocess_dependencies = None" ] }, { "cell_type": "code", "execution_count": 16, "id": "ff4a21cb-cf41-44dc-9ed1-95cf8aa15772", "metadata": { "execution": { "iopub.execute_input": "2024-03-05T20:50:29.123916Z", "iopub.status.busy": "2024-03-05T20:50:29.123837Z", "iopub.status.idle": "2024-03-05T20:50:29.125251Z", "shell.execute_reply": "2024-03-05T20:50:29.125028Z" } }, "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": 17, "id": "2168355c-47d9-475d-b816-49f65e74887c", "metadata": { "execution": { "iopub.execute_input": "2024-03-05T20:50:29.126841Z", "iopub.status.busy": "2024-03-05T20:50:29.126766Z", "iopub.status.idle": "2024-03-05T20:50:29.136591Z", "shell.execute_reply": "2024-03-05T20:50:29.136338Z" } }, "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': 'pcgrimage',\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: 78466]\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: 78466]\n", "base_model_features: None\n", "features_PCPACKYRS: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29]... [Tensor of shape torch.Size([1234])]\n", "features_PCADM: [0, 1, 2, 3, 4, 6, 1232, 7, 8, 10, 11, 1233, 13, 14, 15, 16, 17, 18, 20, 21, 22, 24, 25, 26, 27, 29, 31, 1234, 36, 38]... [Tensor of shape torch.Size([331])]\n", "features_PCB2M: [0, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 1233, 13, 17, 18, 20, 21, 24, 25, 27, 29, 31, 32, 1234, 34, 35, 37, 38, 40]... [Tensor of shape torch.Size([286])]\n", "features_PCCystatinC: [0, 1, 2, 3, 4, 8, 9, 10, 11, 13, 15, 17, 19, 21, 25, 26, 27, 28, 29, 1234, 33, 39, 1235, 43, 45, 46, 47, 1236, 1530, 50]... [Tensor of shape torch.Size([174])]\n", "features_PCGDF15: [0, 2, 3, 4, 5, 1232, 7, 9, 10, 11, 1233, 13, 14, 15, 17, 18, 21, 22, 24, 27, 30, 42, 46, 47, 1236, 55, 72, 74, 82, 120]... [Tensor of shape torch.Size([96])]\n", "features_PCLeptin: [1, 2, 3, 4, 6, 7, 8, 11, 13, 15, 16, 17, 20, 22, 23, 24, 25, 26, 28, 29, 30, 31, 1234, 33, 34, 36, 37, 38, 40, 45]... [Tensor of shape torch.Size([192])]\n", "features_PCPAI1: [0, 1, 3, 4, 5, 6, 7, 9, 11, 12, 13, 14, 15, 16, 17, 19, 20, 21, 24, 25, 26, 27, 28, 32, 1234, 33, 34, 35, 36, 38]... [Tensor of shape torch.Size([631])]\n", "features_PCTIMP1: [0, 2, 4, 5, 6, 7, 9, 11, 12, 1233, 13, 14, 15, 17, 18, 19, 20, 21, 24, 25, 26, 27, 29, 31, 32, 1234, 33, 35, 40, 42]... [Tensor of shape torch.Size([102])]\n", "\n", "%==================================== Model Details ====================================%\n", "Model Structure:\n", "\n", "PCPACKYRS: LinearModel(\n", " (linear): Linear(in_features=1234, out_features=1, bias=True)\n", ")\n", "PCADM: LinearModel(\n", " (linear): Linear(in_features=331, out_features=1, bias=True)\n", ")\n", "PCB2M: LinearModel(\n", " (linear): Linear(in_features=286, out_features=1, bias=True)\n", ")\n", "PCCystatinC: LinearModel(\n", " (linear): Linear(in_features=174, out_features=1, bias=True)\n", ")\n", "PCGDF15: LinearModel(\n", " (linear): Linear(in_features=96, out_features=1, bias=True)\n", ")\n", "PCLeptin: LinearModel(\n", " (linear): Linear(in_features=192, out_features=1, bias=True)\n", ")\n", "PCPAI1: LinearModel(\n", " (linear): Linear(in_features=631, out_features=1, bias=True)\n", ")\n", "PCTIMP1: LinearModel(\n", " (linear): Linear(in_features=102, out_features=1, bias=True)\n", ")\n", "base_model: LinearModel(\n", " (linear): Linear(in_features=10, out_features=1, bias=True)\n", ")\n", "\n", "%==================================== Model Details ====================================%\n", "Model Parameters and Weights:\n", "\n", "center: [0.8050224781036377, 0.18032841384410858, 0.7313324213027954, 0.8423994779586792, 0.09859713166952133, 0.02947637252509594, 0.4717109501361847, 0.8521727323532104, 0.8586175441741943, 0.06356251239776611, 0.05615577474236488, 0.14219772815704346, 0.05609125643968582, 0.19808007776737213, 0.5805700421333313, 0.8329296708106995, 0.5806332230567932, 0.8273633718490601, 0.07795383781194687, 0.8332058191299438, 0.7649531364440918, 0.8077301979064941, 0.7726192474365234, 0.032133087515830994, 0.4985392689704895, 0.5387966632843018, 0.26533183455467224, 0.8369942307472229, 0.4400714635848999, 0.7762642502784729]... [Tensor of shape torch.Size([78464])]\n", "rotation: [0.002423123922199011, -0.0012329419841989875, -0.002256074920296669, -0.004647746216505766, 0.0026397579349577427, -0.0018584569916129112, 0.00032724899938330054, -0.003442202927544713, -0.0015503499889746308, -0.001219906029291451, 0.00100624596234411, 0.0004586990107782185, 0.002247574971988797, 0.0017181190196424723, -0.001358055043965578, 0.0006174049922265112, 0.0002751440042629838, 0.0005500320112332702, -0.0009896219708025455, -0.0011104169534519315, -0.0028521500062197447, 0.0005775609752163291, 0.00010585499694570899, -0.001865869970060885, -0.0008212269749492407, 0.0026659369468688965, -0.002107413951307535, -0.0004820720059797168, -0.001106615993194282, -0.0017073999624699354]... [Tensor of shape torch.Size([78464, 1933])]\n", "PCPACKYRS.linear.weight: [-0.14984282851219177, 0.3452908992767334, 1.1898494958877563, 0.2573627531528473, 0.14331825077533722, -0.6565908193588257, -0.2499941736459732, -0.5399421453475952, 0.2891318202018738, -0.8680660128593445, -0.12501518428325653, 0.1536022573709488, -0.6584336757659912, -2.950654983520508, 1.019046425819397, -3.8761327266693115, -0.8699817657470703, -4.393039703369141, -1.451832890510559, -0.655026912689209, 0.1668931096792221, -3.4419004917144775, 1.4055287837982178, -1.556980848312378, -2.518756151199341, 1.728127121925354, 1.1390047073364258, -0.04151243716478348, -0.6745365262031555, 2.9091978073120117]... [Tensor of shape torch.Size([1, 1234])]\n", "PCPACKYRS.linear.bias: tensor([3.6898])\n", "PCADM.linear.weight: [-1.580859899520874, 0.6895993947982788, 0.5884594321250916, 0.33028411865234375, 0.11279035359621048, -2.4742860794067383, 4.32066535949707, -1.7319400310516357, -0.6082714796066284, -0.18234848976135254, 1.7688186168670654, -0.4687173068523407, -3.820854902267456, 0.23701506853103638, -1.2404377460479736, 0.3446628451347351, -1.809430480003357, -0.35809019207954407, -1.8030858039855957, -0.9031863212585449, -0.24709266424179077, -1.57620370388031, 2.3358521461486816, 2.2262065410614014, -0.32931697368621826, -1.9858754873275757, 1.263405442237854, 2.155048131942749, 1.2939797639846802, 1.2711677551269531]... [Tensor of shape torch.Size([1, 331])]\n", "PCADM.linear.bias: tensor([293.5512])\n", "PCB2M.linear.weight: [-5527.6396484375, 618.7213134765625, 10538.052734375, 13935.08984375, -11641.298828125, -9476.55859375, -4081.583984375, -3303.738037109375, -2605.540283203125, -7426.14697265625, 26004.91015625, 5503.59716796875, 4332.962890625, -18251.943359375, -1677.3939208984375, -64.4184341430664, -4069.646728515625, -4367.86669921875, -11155.8095703125, 11732.5751953125, -8227.33203125, -8242.3095703125, -1011.329345703125, -3278.522216796875, 6034.12060546875, 15530.7216796875, 9036.619140625, -1259.7939453125, 5411.6298828125, -2013.9661865234375]... [Tensor of shape torch.Size([1, 286])]\n", "PCB2M.linear.bias: tensor([2137461.7500])\n", "PCCystatinC.linear.weight: [-758.8092651367188, 466.197021484375, 2975.08740234375, 4612.87939453125, 1127.079833984375, -943.728759765625, -1414.30126953125, -2423.542724609375, 4936.0390625, -1547.6116943359375, -582.6220703125, -1295.9365234375, -104.4135971069336, -2315.2763671875, 1610.7772216796875, -1364.6649169921875, -2691.782958984375, -2306.108154296875, -926.620849609375, 823.5294189453125, 293.9808349609375, -118.99710083007812, -247.8140869140625, -537.2789306640625, 1214.1744384765625, 1737.6654052734375, 252.37217712402344, -212.79415893554688, -33.61885070800781, 85.59772491455078]... [Tensor of shape torch.Size([1, 174])]\n", "PCCystatinC.linear.bias: tensor([540501.8125])\n", "PCGDF15.linear.weight: [-3.064344644546509, 6.427391052246094, 15.992425918579102, 2.824812173843384, -2.727965831756592, 1.533106803894043, -8.710307121276855, -3.053809642791748, -1.1963449716567993, 2.864203691482544, 2.7469873428344727, -13.630250930786133, 2.1277360916137695, -21.83341407775879, -26.710493087768555, -1.885144591331482, -3.6300323009490967, 2.7709856033325195, -5.206478118896484, -2.699326276779175, 0.024610411375761032, 0.23911644518375397, 10.243642807006836, 2.211014747619629, -3.9853546619415283, 0.5308716297149658, -1.5865904092788696, -0.5489709973335266, -1.4851919412612915, -3.113715410232544]... [Tensor of shape torch.Size([1, 96])]\n", "PCGDF15.linear.bias: tensor([94.8125])\n", "PCLeptin.linear.weight: [66.32584381103516, -29.714107513427734, -29.35553550720215, 6.036207675933838, -199.05287170410156, -188.17578125, -31.66545867919922, 53.26570129394531, -339.7561950683594, -185.63404846191406, 86.1479263305664, -73.50887298583984, -338.7237243652344, -57.23273468017578, 372.8580017089844, -84.76087951660156, 255.16909790039062, 34.87641906738281, 10.415830612182617, -65.53888702392578, 23.2608699798584, 225.397705078125, 73.31575012207031, -5.9991559982299805, 10.821805953979492, -3.767247438430786, -322.731689453125, 259.0422058105469, -270.3102722167969, 4.2313690185546875]... [Tensor of shape torch.Size([1, 192])]\n", "PCLeptin.linear.bias: tensor([595.9894])\n", "PCPAI1.linear.weight: [-103.13154602050781, -123.78325653076172, 115.10414123535156, 163.4703369140625, -205.56869506835938, 365.2059326171875, -141.0867919921875, -286.4826354980469, 608.5623168945312, -130.52430725097656, -770.8836059570312, 187.2977752685547, -367.0215759277344, 6.698156356811523, -153.1186981201172, -307.1435852050781, -281.5651550292969, -102.58252716064453, -2.1835734844207764, 245.5375518798828, 13.756028175354004, -65.49664306640625, 22.52094268798828, -207.51370239257812, 312.2713928222656, -12.289201736450195, 46.36296844482422, 26.23123550415039, 89.07231903076172, 133.31295776367188]... [Tensor of shape torch.Size([1, 631])]\n", "PCPAI1.linear.bias: tensor([27206.1406])\n", "PCTIMP1.linear.weight: [-46.880977630615234, 92.83746337890625, 40.29106903076172, -7.354409694671631, 38.623291015625, -68.65544128417969, -60.39823913574219, 196.2154998779297, -5.535160541534424, 5.4189066886901855, -127.39496612548828, -7.336097240447998, -38.622581481933594, -20.141542434692383, 6.978708267211914, -37.9903450012207, -30.112607955932617, -74.98777770996094, -12.825908660888672, 83.09314727783203, -27.617412567138672, -39.28822708129883, -51.4437255859375, 30.111513137817383, -0.7620040774345398, 71.8898696899414, 13.329928398132324, 47.78095626831055, -61.27421951293945, -33.29437255859375]... [Tensor of shape torch.Size([1, 102])]\n", "PCTIMP1.linear.bias: tensor([26016.6270])\n", "base_model.linear.weight: tensor([[ 2.6987e-01, 6.4222e-02, 8.8745e-06, 3.4165e-05, 7.7625e-03,\n", " -7.6811e-05, 4.1255e-04, 1.0161e-03, 1.3897e-01, -1.2596e+00]])\n", "base_model.linear.bias: tensor([-63.8778])\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": 18, "id": "936b9877-d076-4ced-99aa-e8d4c58c5caf", "metadata": { "execution": { "iopub.execute_input": "2024-03-05T20:50:29.138685Z", "iopub.status.busy": "2024-03-05T20:50:29.138598Z", "iopub.status.idle": "2024-03-05T20:50:29.507937Z", "shell.execute_reply": "2024-03-05T20:50:29.507593Z" } }, "outputs": [ { "data": { "text/plain": [ "tensor([[34.0448],\n", " [37.0862],\n", " [45.8467],\n", " [39.5590],\n", " [21.7459],\n", " [23.2970],\n", " [35.3788],\n", " [39.7534],\n", " [45.1109],\n", " [31.1977]], dtype=torch.float64, grad_fn=)" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "torch.manual_seed(42)\n", "input = torch.randn(10, len(model.features), dtype=float).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": 19, "id": "5ef2fa8d-c80b-4fdd-8555-79c0d541788e", "metadata": { "execution": { "iopub.execute_input": "2024-03-05T20:50:29.510255Z", "iopub.status.busy": "2024-03-05T20:50:29.510142Z", "iopub.status.idle": "2024-03-05T20:50:30.900121Z", "shell.execute_reply": "2024-03-05T20:50:30.899809Z" } }, "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": 20, "id": "11aeaa70-44c0-42f9-86d7-740e3849a7a6", "metadata": { "execution": { "iopub.execute_input": "2024-03-05T20:50:30.901738Z", "iopub.status.busy": "2024-03-05T20:50:30.901657Z", "iopub.status.idle": "2024-03-05T20:50:31.093807Z", "shell.execute_reply": "2024-03-05T20:50:31.093521Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Deleted file: PCGrimAgeReferenceCpGBetas.json\n", "Deleted file: PCGrimAgeCpGs.json\n", "Deleted file: CalcAllPCClocks.RData\n", "Deleted file: download.r\n", "Deleted file: CalcPCGrimAge.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": "research", "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.7" } }, "nbformat": 4, "nbformat_minor": 5 }