{ "cells": [ { "cell_type": "markdown", "id": "2f04eee0-5928-4e74-a754-6dc2e528810c", "metadata": {}, "source": [ "# MammalianBlood3" ] }, { "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-07T18:00:23.114536Z", "iopub.status.busy": "2025-04-07T18:00:23.114433Z", "iopub.status.idle": "2025-04-07T18:00:24.758404Z", "shell.execute_reply": "2025-04-07T18:00:24.758083Z" } }, "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\n", "import numpy as np" ] }, { "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-07T18:00:24.760273Z", "iopub.status.busy": "2025-04-07T18:00:24.759990Z", "iopub.status.idle": "2025-04-07T18:00:24.768541Z", "shell.execute_reply": "2025-04-07T18:00:24.768263Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "class MammalianBlood3(pyagingModel):\n", " def __init__(self):\n", " super().__init__()\n", "\n", " def forward(self, x):\n", " x_cpg = x[:, :-1707] # number of species in lookup table\n", " x_species = x[:, -1707:] # number of species in lookup table\n", " x = self.base_model(x_cpg)\n", " x = self.postprocess(x, x_species)\n", " return x\n", "\n", " def preprocess(self, x):\n", " return x\n", "\n", " def postprocess(self, x, x_species):\n", " \"\"\"\n", " Converts output of to units of years.\n", " \"\"\"\n", " indices = torch.argmax(x_species, dim=1)\n", " anage_array = self.postprocess_dependencies[0]\n", " anage_tensor = torch.tensor(anage_array, dtype=x.dtype, device=x.device)\n", "\n", " gestation_time = anage_tensor[indices, 0].unsqueeze(1)\n", " average_maturity_age = anage_tensor[indices, 1].unsqueeze(1)\n", " m_hat = 5 * (gestation_time / average_maturity_age) ** (0.38)\n", "\n", " # Create a mask for negative and non-negative values\n", " mask_negative = x < 0\n", " mask_non_negative = ~mask_negative\n", "\n", " x_pos = x[mask_non_negative]\n", " x_neg = x[mask_negative]\n", "\n", " gestation_time_pos = gestation_time[mask_non_negative]\n", " gestation_time_neg = gestation_time[mask_negative]\n", "\n", " average_maturity_age_pos = average_maturity_age[mask_non_negative]\n", " average_maturity_age_neg = average_maturity_age[mask_negative]\n", "\n", " m_hat_pos = m_hat[mask_non_negative]\n", " m_hat_neg = m_hat[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_non_negative] = (\n", " m_hat_pos * (average_maturity_age_pos + gestation_time_pos) * (x_pos + 1) - gestation_time_pos\n", " )\n", "\n", " # Linear transformation for non-negative values\n", " age_tensor[mask_negative] = (\n", " m_hat_neg * (average_maturity_age_neg + gestation_time_neg) * torch.exp(x_neg) - gestation_time_neg\n", " )\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.MammalianBlood3)" ] }, { "cell_type": "code", "execution_count": 3, "id": "78536494-f1d9-44de-8583-c89a310d2307", "metadata": { "execution": { "iopub.execute_input": "2025-04-07T18:00:24.769870Z", "iopub.status.busy": "2025-04-07T18:00:24.769774Z", "iopub.status.idle": "2025-04-07T18:00:24.771465Z", "shell.execute_reply": "2025-04-07T18:00:24.771211Z" } }, "outputs": [], "source": [ "model = pya.models.MammalianBlood3()" ] }, { "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-07T18:00:24.772684Z", "iopub.status.busy": "2025-04-07T18:00:24.772597Z", "iopub.status.idle": "2025-04-07T18:00:24.774590Z", "shell.execute_reply": "2025-04-07T18:00:24.774351Z" } }, "outputs": [], "source": [ "model.metadata[\"clock_name\"] = \"mammalianblood3\"\n", "model.metadata[\"data_type\"] = \"DNA methylation\" # Paper: methylation\n", "model.metadata[\"species\"] = \"multiple species\" # Paper: multi\n", "model.metadata[\"year\"] = 2023\n", "model.metadata[\"approved_by_author\"] = \"⌛\"\n", "model.metadata[\"citation\"] = \"Lu, A. T., et al. \\\"Universal DNA methylation age across mammalian tissues.\\\" Nature Aging 3.9 (2023): 1144-1166.\"\n", "model.metadata[\"doi\"] = \"https://doi.org/10.1038/s43587-023-00462-6\"\n", "model.metadata[\"notes\"] = \"Blood-specific universal clock 3 fits the gestation/maturity-based log-linear age transformation and returns chronological age in years.\"\n", "model.metadata[\"research_only\"] = None\n", "model.metadata[\"tissue\"] = [\"blood\"] # Paper: blood\n", "model.metadata[\"predicts\"] = [\"chronological age\"] # Paper: The official inverse transformation returns DNAm chronological age in years.\n", "model.metadata[\"training_target\"] = [\"chronological age\"] # Paper: log-linear transformed chronological age\n", "model.metadata[\"unit\"] = [\"years\"] # Paper: The official inverse transformation returns DNAm chronological age in years.\n", "model.metadata[\"model_type\"] = \"elastic net regression\" # Paper: Elastic net\n", "model.metadata[\"platform\"] = [\"Horvath MammalMethylChip40\"] # Paper: HorvathMammalMethylChip40\n", "model.metadata[\"population\"] = \"multiple mammalian species\" # Paper: multi-species mammalian blood samples\n", "model.metadata[\"journal\"] = \"Nature Aging\"\n", "model.metadata[\"last_author\"] = \"Steve Horvath\"\n", "model.metadata[\"n_features\"] = 2097\n", "model.metadata[\"citations\"] = 390\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": "5881f33e-0486-45d1-bd87-9c995f47ca62", "metadata": {}, "source": [ "#### Download GitHub repository" ] }, { "cell_type": "code", "execution_count": 5, "id": "e85525f7-6da4-4962-a7d5-0607e76eea33", "metadata": { "execution": { "iopub.execute_input": "2025-04-07T18:00:24.775887Z", "iopub.status.busy": "2025-04-07T18:00:24.775803Z", "iopub.status.idle": "2025-04-07T18:02:37.490394Z", "shell.execute_reply": "2025-04-07T18:02:37.490043Z" } }, "outputs": [ { "data": { "text/plain": [ "0" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "github_url = \"https://github.com/shorvath/MammalianMethylationConsortium.git\"\n", "github_folder_name = github_url.split('/')[-1].split('.')[0]\n", "os.system(f\"git clone {github_url}\")" ] }, { "cell_type": "markdown", "id": "2c3e8ec5-a59b-48c5-ae7c-8d2a344ce11a", "metadata": {}, "source": [ "#### Download from R package" ] }, { "cell_type": "code", "execution_count": 6, "id": "3fbce39a-5eae-4bff-ac42-d5da0b357bc0", "metadata": { "execution": { "iopub.execute_input": "2025-04-07T18:02:37.492066Z", "iopub.status.busy": "2025-04-07T18:02:37.491936Z", "iopub.status.idle": "2025-04-07T18:02:37.494677Z", "shell.execute_reply": "2025-04-07T18:02:37.494384Z" } }, "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", "\n", "myinput.list=readRDS('MammalianMethylationConsortium/UniversalPanMammalianClock/ClockParameters/mydata_GitHub.Rds')\n", "anage=myinput.list[[3]]\n", "anage=subset(anage,select=c(SpeciesLatinName,GestationTimeInYears, averagedMaturity.yrs,maxAge))\n", "anage$HighmaxAge=1.3*anage$maxAge\n", "anage$HighmaxAge[anage$SpeciesLatinName=='Homo sapiens']=anage$maxAge[anage$SpeciesLatinName=='Homo sapiens']\n", "anage$HighmaxAge[anage$SpeciesLatinName=='Mus musculus']=anage$maxAge[anage$SpeciesLatinName=='Mus musculus']\n", "write.csv(anage, \"species_annotation.csv\")" ] }, { "cell_type": "code", "execution_count": 7, "id": "3733db47-ba6b-4df7-9b8f-0341b6796586", "metadata": { "execution": { "iopub.execute_input": "2025-04-07T18:02:37.496037Z", "iopub.status.busy": "2025-04-07T18:02:37.495926Z", "iopub.status.idle": "2025-04-07T18:02:38.119693Z", "shell.execute_reply": "2025-04-07T18:02:38.118541Z" } }, "outputs": [ { "data": { "text/plain": [ "0" ] }, "execution_count": 7, "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": "15f4af76-b93c-438c-b57f-f129d6e9ec99", "metadata": {}, "source": [ "#### From CSV file" ] }, { "cell_type": "code", "execution_count": 8, "id": "8a3d5de6-6303-487a-8b4d-e6345792f7be", "metadata": { "execution": { "iopub.execute_input": "2025-04-07T18:02:38.121976Z", "iopub.status.busy": "2025-04-07T18:02:38.121827Z", "iopub.status.idle": "2025-04-07T18:02:38.135968Z", "shell.execute_reply": "2025-04-07T18:02:38.134738Z" } }, "outputs": [], "source": [ "df = pd.read_csv('MammalianMethylationConsortium/UniversalPanMammalianClock/ClockParameters/tissue_specific_clock/UniversalBloodClock3_final.csv')\n", "df['feature'] = df['var']\n", "df['coefficient'] = df['beta']\n", "cpg_features = df['feature'][1:].tolist()\n", "\n", "anage_df = pd.read_csv('species_annotation.csv', index_col=0)\n", "anage_df = anage_df[~anage_df['averagedMaturity.yrs'].isna()]\n", "anage_df = anage_df[~anage_df['GestationTimeInYears'].isna()]\n", "anage_df = anage_df.reset_index().drop('index', axis=1)\n", "anage_df = anage_df.fillna(0)\n", "species_features = anage_df['SpeciesLatinName'].tolist()\n", "\n", "model.features = cpg_features + species_features" ] }, { "cell_type": "markdown", "id": "ee6d8fa0-4767-4c45-9717-eb1c95e2ddc0", "metadata": {}, "source": [ "## Load weights into base model" ] }, { "cell_type": "code", "execution_count": 9, "id": "e09b3463-4fd4-41b1-ac21-e63ddd223fe0", "metadata": { "execution": { "iopub.execute_input": "2025-04-07T18:02:38.139994Z", "iopub.status.busy": "2025-04-07T18:02:38.139639Z", "iopub.status.idle": "2025-04-07T18:02:38.144962Z", "shell.execute_reply": "2025-04-07T18:02:38.144144Z" } }, "outputs": [], "source": [ "weights = torch.tensor(df['coefficient'][1:].tolist()).unsqueeze(0)\n", "intercept = torch.tensor([df['coefficient'][0]])" ] }, { "cell_type": "markdown", "id": "ad261636-5b00-4979-bb1d-67a851f7aa19", "metadata": {}, "source": [ "#### Linear model" ] }, { "cell_type": "code", "execution_count": 10, "id": "d7f43b99-26f2-4622-9a76-316712058877", "metadata": { "execution": { "iopub.execute_input": "2025-04-07T18:02:38.148088Z", "iopub.status.busy": "2025-04-07T18:02:38.147909Z", "iopub.status.idle": "2025-04-07T18:02:38.150411Z", "shell.execute_reply": "2025-04-07T18:02:38.150128Z" } }, "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": 11, "id": "ade0f4c9-2298-4fc3-bb72-d200907dd731", "metadata": { "execution": { "iopub.execute_input": "2025-04-07T18:02:38.151879Z", "iopub.status.busy": "2025-04-07T18:02:38.151768Z", "iopub.status.idle": "2025-04-07T18:02:38.153945Z", "shell.execute_reply": "2025-04-07T18:02:38.153687Z" } }, "outputs": [], "source": [ "reference_list = np.array([0] * len(model.features))\n", "reference_list[len(cpg_features) + np.where(anage_df.SpeciesLatinName == 'Homo sapiens')[0][0]] = 0.5\n", "model.reference_values = reference_list" ] }, { "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": "2025-04-07T18:02:38.155370Z", "iopub.status.busy": "2025-04-07T18:02:38.155260Z", "iopub.status.idle": "2025-04-07T18:02:38.156837Z", "shell.execute_reply": "2025-04-07T18:02:38.156589Z" } }, "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": "2025-04-07T18:02:38.158224Z", "iopub.status.busy": "2025-04-07T18:02:38.158078Z", "iopub.status.idle": "2025-04-07T18:02:38.160974Z", "shell.execute_reply": "2025-04-07T18:02:38.160657Z" } }, "outputs": [], "source": [ "model.postprocess_name = 'mammalian3'\n", "anage_df = anage_df.loc[:, ['GestationTimeInYears','averagedMaturity.yrs', 'maxAge',\t'HighmaxAge']]\n", "anage_array = np.array(anage_df)\n", "model.postprocess_dependencies = [anage_array]" ] }, { "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": "2025-04-07T18:02:38.162457Z", "iopub.status.busy": "2025-04-07T18:02:38.162350Z", "iopub.status.idle": "2025-04-07T18:02:38.167318Z", "shell.execute_reply": "2025-04-07T18:02:38.167042Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "%==================================== Model Details ====================================%\n", "Model Attributes:\n", "\n", "training: True\n", "metadata: {'approved_by_author': '⌛',\n", " 'citation': 'Lu, A. T., et al. \"Universal DNA methylation age across '\n", " 'mammalian tissues.\" Nature aging 3.9 (2023): 1144-1166.',\n", " 'clock_name': 'mammalianblood3',\n", " 'data_type': 'methylation',\n", " 'doi': 'https://doi.org/10.1038/s43587-023-00462-6',\n", " 'notes': None,\n", " 'research_only': None,\n", " 'species': 'multi',\n", " 'version': None,\n", " 'year': 2023}\n", "reference_values: array([0, 0, 0, ..., 0, 0, 0])\n", "preprocess_name: None\n", "preprocess_dependencies: None\n", "postprocess_name: 'mammalian3'\n", "postprocess_dependencies: [array([[2.19178082e-02, 2.49315068e+00, 3.60000000e+01, 4.68000000e+01],\n", " [1.64383562e-02, 3.00000000e+00, 1.15000000e+01, 1.49500000e+01],\n", " [3.01369863e-02, 4.50000000e+00, 1.50000000e+01, 1.95000000e+01],\n", " ...,\n", " [1.48575342e-01, 1.15317808e+00, 0.00000000e+00, 0.00000000e+00],\n", " [6.95616438e-02, 1.16958904e-01, 0.00000000e+00, 0.00000000e+00],\n", " [3.15068493e-01, 4.58334000e-01, 2.70000000e+01, 3.51000000e+01]])]\n", "features: ['cg00114412', 'cg00295657', 'cg00296110', 'cg00310215', 'cg00439117', 'cg00471897', 'cg00559067', 'cg00578937', 'cg00587168', 'cg00728976', 'cg00742557', 'cg00780852', 'cg00833227', 'cg00910419', 'cg00915004', 'cg00918089', 'cg00935831', 'cg00953859', 'cg01053290', 'cg01079397.1', 'cg01079397.2', 'cg01137681', 'cg01190601', 'cg01235968', 'cg01393939', 'cg01486146', 'cg01528792', 'cg01566077', 'cg01701526', 'cg01932632']... [Total elements: 2097]\n", "base_model_features: None\n", "\n", "%==================================== Model Details ====================================%\n", "Model Structure:\n", "\n", "base_model: LinearModel(\n", " (linear): Linear(in_features=2097, out_features=1, bias=True)\n", ")\n", "\n", "%==================================== Model Details ====================================%\n", "Model Parameters and Weights:\n", "\n", "base_model.linear.weight: [-0.014848549850285053, -0.015909673646092415, -0.03707726672291756, 0.9694833755493164, -0.9203034043312073, -0.026087677106261253, 0.33000633120536804, -0.7896574139595032, 0.04147997871041298, -0.49385330080986023, 0.09399742633104324, -0.002576522994786501, 0.25313621759414673, -0.9668075442314148, 0.591387927532196, -0.04230678081512451, 0.014051662757992744, 0.5414583086967468, -0.41466763615608215, 0.13480553030967712, -0.48314857482910156, 0.3226204514503479, 0.036402441561222076, -0.20592643320560455, 0.476875901222229, 0.17880657315254211, -0.5554146766662598, -1.3004117012023926, -0.031236404553055763, -0.15437926352024078]... [Tensor of shape torch.Size([1, 390])]\n", "base_model.linear.bias: tensor([3.0563])\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": "2025-04-07T18:02:38.168749Z", "iopub.status.busy": "2025-04-07T18:02:38.168651Z", "iopub.status.idle": "2025-04-07T18:02:38.175860Z", "shell.execute_reply": "2025-04-07T18:02:38.175562Z" } }, "outputs": [ { "data": { "text/plain": [ "tensor([[-8.2191e-02],\n", " [-4.9315e-02],\n", " [ 5.2099e+00],\n", " [ 4.4614e+01],\n", " [ 2.0724e+01],\n", " [-2.6487e-02],\n", " [-4.5110e-02],\n", " [-4.7053e-01],\n", " [ 4.8666e+01],\n", " [ 3.5803e+02]], 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": "2025-04-07T18:02:38.177240Z", "iopub.status.busy": "2025-04-07T18:02:38.177149Z", "iopub.status.idle": "2025-04-07T18:02:38.190224Z", "shell.execute_reply": "2025-04-07T18:02:38.189918Z" } }, "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": "2025-04-07T18:02:38.191579Z", "iopub.status.busy": "2025-04-07T18:02:38.191484Z", "iopub.status.idle": "2025-04-07T18:02:38.334779Z", "shell.execute_reply": "2025-04-07T18:02:38.334451Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Deleted file: coefficients.xlsx\n", "Deleted file: species_annotation.csv\n", "Deleted folder: MammalianMethylationConsortium\n", "Deleted file: download.r\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 }