{ "cells": [ { "cell_type": "markdown", "id": "2f04eee0-5928-4e74-a754-6dc2e528810c", "metadata": {}, "source": [ "# DunedinPACE" ] }, { "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": "2025-04-07T17:50:37.811306Z", "iopub.status.busy": "2025-04-07T17:50:37.810909Z", "iopub.status.idle": "2025-04-07T17:50:39.184200Z", "shell.execute_reply": "2025-04-07T17:50:39.183818Z" } }, "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": "2025-04-07T17:50:39.185944Z", "iopub.status.busy": "2025-04-07T17:50:39.185725Z", "iopub.status.idle": "2025-04-07T17:50:39.192877Z", "shell.execute_reply": "2025-04-07T17:50:39.192607Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "class DunedinPACE(pyagingModel):\n", " def __init__(self):\n", " super().__init__()\n", "\n", " def preprocess(self, x):\n", " \"\"\"\n", " Apply quantile normalization on x using gold standard means.\n", " \"\"\"\n", " # Ensure gold_standard_means is a 1D tensor and sorted\n", " sorted_gold_standard = torch.sort(torch.tensor(self.reference_values, device=x.device, dtype=x.dtype))[0]\n", "\n", " # Pre-compute the quantile indices\n", " quantile_indices = torch.linspace(0, len(sorted_gold_standard) - 1, steps=x.size(1)).long()\n", "\n", " # Prepare a tensor to hold normalized data\n", " normalized_data = torch.empty_like(x, device=x.device, dtype=x.dtype)\n", "\n", " for i in range(x.size(0)):\n", " sorted_indices = torch.argsort(x[i, :])\n", " normalized_data[i, sorted_indices] = sorted_gold_standard[quantile_indices]\n", "\n", " # Return only the subset from x that is used in the base model\n", " return normalized_data[:, self.preprocess_dependencies[0]]\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.DunedinPACE)" ] }, { "cell_type": "code", "execution_count": 3, "id": "78536494-f1d9-44de-8583-c89a310d2307", "metadata": { "execution": { "iopub.execute_input": "2025-04-07T17:50:39.194269Z", "iopub.status.busy": "2025-04-07T17:50:39.194173Z", "iopub.status.idle": "2025-04-07T17:50:39.195804Z", "shell.execute_reply": "2025-04-07T17:50:39.195566Z" } }, "outputs": [], "source": [ "model = pya.models.DunedinPACE()" ] }, { "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": "2025-04-07T17:50:39.197117Z", "iopub.status.busy": "2025-04-07T17:50:39.197033Z", "iopub.status.idle": "2025-04-07T17:50:39.199038Z", "shell.execute_reply": "2025-04-07T17:50:39.198798Z" } }, "outputs": [], "source": [ "model.metadata[\"clock_name\"] = \"dunedinpace\"\n", "model.metadata[\"data_type\"] = \"DNA methylation\" # Paper: DNA-methylation blood-test\n", "model.metadata[\"species\"] = \"Homo sapiens\" # Paper: Dunedin Study 1972–1973 birth cohort\n", "model.metadata[\"year\"] = 2022\n", "model.metadata[\"approved_by_author\"] = \"✅\"\n", "model.metadata[\"citation\"] = \"Belsky, D. W., Caspi, A., Corcoran, D. L., et al. (2022). DunedinPACE, a DNA methylation biomarker of the pace of aging. eLife, 11, e73420.\"\n", "model.metadata[\"doi\"] = \"https://doi.org/10.7554/elife.73420\"\n", "model.metadata[\"notes\"] = \"Whole-blood elastic-net pace-of-aging biomarker trained at age 45 against a 20-year longitudinal slope composite of 19 organ-system biomarkers. PyAging follows the official 20,000-probe quantile-normalization panel: 173 scoring CpGs plus 19,827 background probes.\"\n", "model.metadata[\"research_only\"] = True\n", "model.metadata[\"tissue\"] = [\"whole blood\"] # Paper: blood collected at age 45\n", "model.metadata[\"predicts\"] = [\"pace of aging\"] # Paper: DunedinPACE values quantify how much faster or slower an individual is aging\n", "model.metadata[\"training_target\"] = [\"pace of aging\"] # Paper: The criterion variable was 20-year Pace of Aging.\n", "model.metadata[\"unit\"] = [\"biological years per chronological year\"] # Paper: reference value of 1 year of biological aging per calendar year\n", "model.metadata[\"model_type\"] = \"elastic net regression\" # Paper: using elastic-net regression\n", "model.metadata[\"platform\"] = [\"Illumina EPIC\"] # Paper: DunedinPACE was developed from age-45 Dunedin Study whole-blood methylation measured on Illumina EPIC arrays.\n", "model.metadata[\"population\"] = \"adults\" # Paper: N=817 with methylation and measured Pace of Aging at age 45\n", "model.metadata[\"journal\"] = \"eLife\"\n", "model.metadata[\"last_author\"] = \"Terrie E. Moffitt\"\n", "model.metadata[\"n_features\"] = 20000\n", "model.metadata[\"citations\"] = 967\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": "markdown", "id": "aeee6c5a-4e0a-4f2a-acef-d657596f453a", "metadata": {}, "source": [ "#### Download from R package" ] }, { "cell_type": "code", "execution_count": 5, "id": "d81f2c9d-362f-43cb-ad52-012e28217164", "metadata": { "execution": { "iopub.execute_input": "2025-04-07T17:50:39.200480Z", "iopub.status.busy": "2025-04-07T17:50:39.200386Z", "iopub.status.idle": "2025-04-07T17:50:39.202718Z", "shell.execute_reply": "2025-04-07T17:50:39.202462Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Writing download.r\n" ] } ], "source": [ "%%writefile download.r\n", "\n", "options(repos = c(CRAN = \"https://cloud.r-project.org/\"))\n", "library(jsonlite)\n", "\n", "install.packages(\"devtools\")\n", "devtools::install_github(\"danbelsky/DunedinPACE\", build_vignettes = FALSE)\n", "library(DunedinPACE)\n", "\n", "PACE_list = list(c(\n", " mPACE_Models$model_names, \n", " mPACE_Models$gold_standard_probes, \n", " mPACE_Models$model_weights, \n", " mPACE_Models$model_intercept,\n", " mPACE_Models$model_means,\n", " mPACE_Models$model_probes,\n", " mPACE_Models$gold_standard_means \n", "))\n", "\n", "write_json(PACE_list, \"DunedinPACE.json\", digits = 12)" ] }, { "cell_type": "code", "execution_count": 6, "id": "1ce2bac8-dd33-46cb-a7b6-14a1d0976f05", "metadata": { "execution": { "iopub.execute_input": "2025-04-07T17:50:39.203879Z", "iopub.status.busy": "2025-04-07T17:50:39.203793Z", "iopub.status.idle": "2025-04-07T17:51:17.345313Z", "shell.execute_reply": "2025-04-07T17:51:17.345040Z" } }, "outputs": [ { "data": { "text/plain": [ "0" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "os.system(\"Rscript download.r\")" ] }, { "cell_type": "markdown", "id": "5035b180-3d1b-4432-8ebe-b9c92bd93a7f", "metadata": {}, "source": [ "## Load features" ] }, { "cell_type": "markdown", "id": "d8025ed7-0013-419b-8cb5-1a2db98f9eba", "metadata": {}, "source": [ "#### From JSON file" ] }, { "cell_type": "code", "execution_count": 7, "id": "77face1a-b58f-4f8f-9fe8-1f12037be99a", "metadata": { "execution": { "iopub.execute_input": "2025-04-07T17:51:17.346883Z", "iopub.status.busy": "2025-04-07T17:51:17.346780Z", "iopub.status.idle": "2025-04-07T17:51:17.351220Z", "shell.execute_reply": "2025-04-07T17:51:17.350910Z" } }, "outputs": [], "source": [ "with open('DunedinPACE.json', 'r') as f:\n", " PACE_list = json.load(f)[0]\n", "model.features = PACE_list['DunedinPACE']\n", "model.base_model_features = PACE_list['DunedinPACE.4']" ] }, { "cell_type": "markdown", "id": "ee6d8fa0-4767-4c45-9717-eb1c95e2ddc0", "metadata": {}, "source": [ "## Load weights into base model" ] }, { "cell_type": "code", "execution_count": 8, "id": "e09b3463-4fd4-41b1-ac21-e63ddd223fe0", "metadata": { "execution": { "iopub.execute_input": "2025-04-07T17:51:17.352641Z", "iopub.status.busy": "2025-04-07T17:51:17.352546Z", "iopub.status.idle": "2025-04-07T17:51:17.354505Z", "shell.execute_reply": "2025-04-07T17:51:17.354270Z" } }, "outputs": [], "source": [ "weights = torch.tensor(PACE_list['DunedinPACE.1']).unsqueeze(0).float()\n", "intercept = torch.tensor([PACE_list['DunedinPACE.2'][0]]).float()" ] }, { "cell_type": "markdown", "id": "ad261636-5b00-4979-bb1d-67a851f7aa19", "metadata": {}, "source": [ "#### Linear model" ] }, { "cell_type": "code", "execution_count": 9, "id": "d7f43b99-26f2-4622-9a76-316712058877", "metadata": { "execution": { "iopub.execute_input": "2025-04-07T17:51:17.355857Z", "iopub.status.busy": "2025-04-07T17:51:17.355777Z", "iopub.status.idle": "2025-04-07T17:51:17.357878Z", "shell.execute_reply": "2025-04-07T17:51:17.357647Z" } }, "outputs": [], "source": [ "base_model = pya.models.LinearModel(input_dim=len(model.features))\n", "\n", "base_model.linear.weight.data = weights.float()\n", "base_model.linear.bias.data = intercept.float()\n", "\n", "model.base_model = base_model" ] }, { "cell_type": "markdown", "id": "ad8b4c1d-9d57-48b7-9a30-bcfea7b747b1", "metadata": {}, "source": [ "## Load reference values" ] }, { "cell_type": "code", "execution_count": 10, "id": "ade0f4c9-2298-4fc3-bb72-d200907dd731", "metadata": { "execution": { "iopub.execute_input": "2025-04-07T17:51:17.359170Z", "iopub.status.busy": "2025-04-07T17:51:17.359082Z", "iopub.status.idle": "2025-04-07T17:51:17.360595Z", "shell.execute_reply": "2025-04-07T17:51:17.360341Z" } }, "outputs": [], "source": [ "model.reference_values = PACE_list['DunedinPACE.5']" ] }, { "cell_type": "markdown", "id": "af3bcf7b-74a8-4d21-9ccb-4de0c2b0516b", "metadata": {}, "source": [ "## Load preprocess and postprocess objects" ] }, { "cell_type": "code", "execution_count": 11, "id": "7a22fb20-c605-424d-8efb-7620c2c0755c", "metadata": { "execution": { "iopub.execute_input": "2025-04-07T17:51:17.361910Z", "iopub.status.busy": "2025-04-07T17:51:17.361819Z", "iopub.status.idle": "2025-04-07T17:51:17.363629Z", "shell.execute_reply": "2025-04-07T17:51:17.363379Z" } }, "outputs": [], "source": [ "model.preprocess_name = 'quantile_normalization_with_gold_standard'\n", "\n", "indices = [model.features.index(item) for item in model.base_model_features]\n", "model.preprocess_dependencies = [indices]" ] }, { "cell_type": "code", "execution_count": 12, "id": "ff4a21cb-cf41-44dc-9ed1-95cf8aa15772", "metadata": { "execution": { "iopub.execute_input": "2025-04-07T17:51:17.364879Z", "iopub.status.busy": "2025-04-07T17:51:17.364799Z", "iopub.status.idle": "2025-04-07T17:51:17.366253Z", "shell.execute_reply": "2025-04-07T17:51:17.366008Z" } }, "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": 13, "id": "2168355c-47d9-475d-b816-49f65e74887c", "metadata": { "execution": { "iopub.execute_input": "2025-04-07T17:51:17.367703Z", "iopub.status.busy": "2025-04-07T17:51:17.367609Z", "iopub.status.idle": "2025-04-07T17:51:17.371142Z", "shell.execute_reply": "2025-04-07T17:51:17.370862Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "%==================================== Model Details ====================================%\n", "Model Attributes:\n", "\n", "training: True\n", "metadata: {'approved_by_author': '✅',\n", " 'citation': 'Belsky, Daniel W., et al. \"DunedinPACE, a DNA methylation '\n", " 'biomarker of the pace of aging.\" Elife 11 (2022): e73420.',\n", " 'clock_name': 'dunedinpace',\n", " 'data_type': 'methylation',\n", " 'doi': 'https://doi.org/10.7554/eLife.73420',\n", " 'notes': 'This model is for research purposes only. Commercial users should '\n", " 'contact exclusive DunedinPACE licensee TruDiagnosticTM. The '\n", " 'automatic failure if fewer than 80% of the CpG probes are available '\n", " \"is not implemented and left to the user's discretion.\",\n", " 'research_only': True,\n", " 'species': 'Homo sapiens',\n", " 'version': None,\n", " 'year': 2022}\n", "reference_values: [0.8499678448731, 0.7897610893879, 0.9717597609504, 0.8480138104804, 0.2475593905826, 0.1097207575569, 0.189885225724, 0.3909313843346, 0.9168688056148, 0.2708397202163, 0.8736696325841, 0.5147248840362, 0.9619479966112, 0.2575877641187, 0.9432588276732, 0.8989191043041, 0.580889869872, 0.1242693996089, 0.8932207211244, 0.6638228904263, 0.09830894494804, 0.8640316497494, 0.08535902315207, 0.07942500721274, 0.7473024928443, 0.8737263989611, 0.7534216033511, 0.09034449086512, 0.109486844368, 0.6611683137784]... [Total elements: 20000]\n", "preprocess_name: 'quantile_normalization_with_gold_standard'\n", "preprocess_dependencies: [[0,\n", " 1,\n", " 2,\n", " 3,\n", " 4,\n", " 5,\n", " 6,\n", " 7,\n", " 8,\n", " 9,\n", " 10,\n", " 11,\n", " 12,\n", " 13,\n", " 14,\n", " 15,\n", " 16,\n", " 17,\n", " 18,\n", " 19,\n", " 20,\n", " 21,\n", " 22,\n", " 23,\n", " 24,\n", " 25,\n", " 26,\n", " 27,\n", " 28,\n", " 29,\n", " 30,\n", " 31,\n", " 32,\n", " 33,\n", " 34,\n", " 35,\n", " 36,\n", " 37,\n", " 38,\n", " 39,\n", " 40,\n", " 41,\n", " 42,\n", " 43,\n", " 44,\n", " 45,\n", " 46,\n", " 47,\n", " 48,\n", " 49,\n", " 50,\n", " 51,\n", " 52,\n", " 53,\n", " 54,\n", " 55,\n", " 56,\n", " 57,\n", " 58,\n", " 59,\n", " 60,\n", " 61,\n", " 62,\n", " 63,\n", " 64,\n", " 65,\n", " 66,\n", " 67,\n", " 68,\n", " 69,\n", " 70,\n", " 71,\n", " 72,\n", " 73,\n", " 74,\n", " 75,\n", " 76,\n", " 77,\n", " 78,\n", " 79,\n", " 80,\n", " 81,\n", " 82,\n", " 83,\n", " 84,\n", " 85,\n", " 86,\n", " 87,\n", " 88,\n", " 89,\n", " 90,\n", " 91,\n", " 92,\n", " 93,\n", " 94,\n", " 95,\n", " 96,\n", " 97,\n", " 98,\n", " 99,\n", " 100,\n", " 101,\n", " 102,\n", " 103,\n", " 104,\n", " 105,\n", " 106,\n", " 107,\n", " 108,\n", " 109,\n", " 110,\n", " 111,\n", " 112,\n", " 113,\n", " 114,\n", " 115,\n", " 116,\n", " 117,\n", " 118,\n", " 119,\n", " 120,\n", " 121,\n", " 122,\n", " 123,\n", " 124,\n", " 125,\n", " 126,\n", " 127,\n", " 128,\n", " 129,\n", " 130,\n", " 131,\n", " 132,\n", " 133,\n", " 134,\n", " 135,\n", " 136,\n", " 137,\n", " 138,\n", " 139,\n", " 140,\n", " 141,\n", " 142,\n", " 143,\n", " 144,\n", " 145,\n", " 146,\n", " 147,\n", " 148,\n", " 149,\n", " 150,\n", " 151,\n", " 152,\n", " 153,\n", " 154,\n", " 155,\n", " 156,\n", " 157,\n", " 158,\n", " 159,\n", " 160,\n", " 161,\n", " 162,\n", " 163,\n", " 164,\n", " 165,\n", " 166,\n", " 167,\n", " 168,\n", " 169,\n", " 170,\n", " 171,\n", " 172]]\n", "postprocess_name: None\n", "postprocess_dependencies: None\n", "features: ['cg00112187', 'cg00151250', 'cg00359421', 'cg00513564', 'cg00532802', 'cg00574958', 'cg00668559', 'cg00782811', 'cg00835193', 'cg01055871', 'cg01101459', 'cg01360413', 'cg01554316', 'cg01936220', 'cg02004723', 'cg02079413', 'cg02229095', 'cg02300147', 'cg02307277', 'cg02571857', 'cg02650017', 'cg02949067', 'cg02997983', 'cg03604011', 'cg03776935', 'cg03810769', 'cg03868770', 'cg04051458', 'cg04105250', 'cg04305539']... [Total elements: 20000]\n", "base_model_features: ['cg00112187', 'cg00151250', 'cg00359421', 'cg00513564', 'cg00532802', 'cg00574958', 'cg00668559', 'cg00782811', 'cg00835193', 'cg01055871', 'cg01101459', 'cg01360413', 'cg01554316', 'cg01936220', 'cg02004723', 'cg02079413', 'cg02229095', 'cg02300147', 'cg02307277', 'cg02571857', 'cg02650017', 'cg02949067', 'cg02997983', 'cg03604011', 'cg03776935', 'cg03810769', 'cg03868770', 'cg04051458', 'cg04105250', 'cg04305539']... [Total elements: 173]\n", "\n", "%==================================== Model Details ====================================%\n", "Model Structure:\n", "\n", "base_model: LinearModel(\n", " (linear): Linear(in_features=20000, out_features=1, bias=True)\n", ")\n", "\n", "%==================================== Model Details ====================================%\n", "Model Parameters and Weights:\n", "\n", "base_model.linear.weight: [-0.13975931704044342, 0.1523076742887497, 0.1058167889714241, -0.12104399502277374, 0.04729907587170601, -0.2645362913608551, -0.09107177704572678, 0.07626617699861526, -0.05811680853366852, -0.23175522685050964, 0.06270736455917358, 0.015027794055640697, 0.5146545171737671, -0.03559967875480652, 0.009041309356689453, 0.20905275642871857, 0.02438066340982914, 0.14796297252178192, 0.03653242066502571, 0.09093873202800751, -0.5942692160606384, -0.14602923393249512, 0.028370223939418793, 0.12909314036369324, 0.15818408131599426, 0.026989631354808807, -0.038831036537885666, 0.16809432208538055, 0.11093547195196152, -0.012085522525012493]... [Tensor of shape torch.Size([1, 173])]\n", "base_model.linear.bias: tensor([-1.9499])\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": 14, "id": "936b9877-d076-4ced-99aa-e8d4c58c5caf", "metadata": { "execution": { "iopub.execute_input": "2025-04-07T17:51:17.372574Z", "iopub.status.busy": "2025-04-07T17:51:17.372487Z", "iopub.status.idle": "2025-04-07T17:51:17.398592Z", "shell.execute_reply": "2025-04-07T17:51:17.398259Z" } }, "outputs": [ { "data": { "text/plain": [ "tensor([[ 0.2938],\n", " [ 0.8014],\n", " [-0.0384],\n", " [-0.2034],\n", " [-0.1848],\n", " [ 0.5177],\n", " [-0.1668],\n", " [ 0.3322],\n", " [-0.8489],\n", " [ 0.7232]], dtype=torch.float64, grad_fn=)" ] }, "execution_count": 14, "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": 15, "id": "5ef2fa8d-c80b-4fdd-8555-79c0d541788e", "metadata": { "execution": { "iopub.execute_input": "2025-04-07T17:51:17.400130Z", "iopub.status.busy": "2025-04-07T17:51:17.400026Z", "iopub.status.idle": "2025-04-07T17:51:17.412150Z", "shell.execute_reply": "2025-04-07T17:51:17.411807Z" } }, "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": 16, "id": "11aeaa70-44c0-42f9-86d7-740e3849a7a6", "metadata": { "execution": { "iopub.execute_input": "2025-04-07T17:51:17.413650Z", "iopub.status.busy": "2025-04-07T17:51:17.413558Z", "iopub.status.idle": "2025-04-07T17:51:17.417532Z", "shell.execute_reply": "2025-04-07T17:51:17.417258Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Deleted file: coefficients.csv\n", "Deleted file: coefficients.xlsx\n", "Deleted file: download.r\n", "Deleted file: DunedinPACE.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.7" } }, "nbformat": 4, "nbformat_minor": 5 }