{ "cells": [ { "cell_type": "markdown", "id": "2f04eee0-5928-4e74-a754-6dc2e528810c", "metadata": {}, "source": [ "# Template" ] }, { "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": null, "id": "4adfb4de-cd79-4913-a1af-9e23e9e236c9", "metadata": {}, "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": null, "id": "8aa77372-7ed3-4da7-abc9-d30372106139", "metadata": {}, "outputs": [], "source": [ "def print_entire_class(cls):\n", " source = inspect.getsource(cls)\n", " print(source)\n", "\n", "print_entire_class(pya.models.Template)" ] }, { "cell_type": "code", "execution_count": null, "id": "914a94cf-bf6c-4b9d-862a-a2787842e07e", "metadata": {}, "outputs": [], "source": [ "model = pya.models.Template()" ] }, { "cell_type": "markdown", "id": "51f8615e-01fa-4aa5-b196-3ee2b35d261c", "metadata": {}, "source": [ "## Define clock metadata" ] }, { "cell_type": "code", "execution_count": null, "id": "9a662703-0ea4-4b9b-98cc-310e4e604d65", "metadata": {}, "outputs": [], "source": [ "model.metadata[\"clock_name\"] = 'example'\n", "model.metadata[\"data_type\"] = 'methylation'\n", "model.metadata[\"species\"] = 'Homo sapiens'\n", "model.metadata[\"year\"] = 2023\n", "model.metadata[\"approved_by_author\"] = '⌛'\n", "model.metadata[\"citation\"] = \"citation\"\n", "model.metadata[\"doi\"] = 'doi'\n", "model.metadata[\"research_only\"] = None\n", "model.metadata[\"notes\"] = None\n", "model.metadata[\"tissue\"] = None\n", "model.metadata[\"predicts\"] = None\n", "model.metadata[\"unit\"] = None\n", "model.metadata[\"model_type\"] = None\n", "model.metadata[\"platform\"] = None\n", "model.metadata[\"population\"] = None\n", "model.metadata[\"journal\"] = None\n", "model.metadata[\"last_author\"] = None\n", "model.metadata[\"n_features\"] = None\n", "model.metadata[\"citations\"] = None\n", "model.metadata[\"citations_date\"] = None" ] }, { "cell_type": "markdown", "id": "74492239-5aae-4026-9d90-6bc9c574c110", "metadata": {}, "source": [ "## Download clock dependencies" ] }, { "cell_type": "markdown", "id": "1b0906c9-3c7e-4877-b2f9-3a107b8dee41", "metadata": {}, "source": [ "#### Download directly with curl" ] }, { "cell_type": "code", "execution_count": null, "id": "f406c537-c784-43bb-8c36-9624af63ebf4", "metadata": {}, "outputs": [], "source": [ "supplementary_url = \"example_url.com\"\n", "supplementary_file_name = \"example.xlsx\"\n", "os.system(f\"curl -o {supplementary_file_name} {supplementary_url}\")" ] }, { "cell_type": "markdown", "id": "7bec474f-80ce-4884-9472-30c193327117", "metadata": {}, "source": [ "#### Download GitHub repository" ] }, { "cell_type": "code", "execution_count": null, "id": "aa4a1b59-dda3-4ea8-8f34-b3c53ecbc310", "metadata": {}, "outputs": [], "source": [ "github_url = \"https://github.com/example/example_github.git\"\n", "github_folder_name = github_url.split('/')[-1].split('.')[0]\n", "os.system(f\"git clone {github_url}\")" ] }, { "cell_type": "markdown", "id": "6bd15521-363f-4029-99ff-9f0b2ae0ed2e", "metadata": {}, "source": [ "#### Download from R package" ] }, { "cell_type": "code", "execution_count": null, "id": "f1f9bbe4-cfc8-494c-b910-c96da88afb2b", "metadata": {}, "outputs": [], "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(\"example/example\", build_vignettes = FALSE)\n", "library(example)\n", "\n", "write_json(example_object, \"example.json\", digits = 9)" ] }, { "cell_type": "code", "execution_count": null, "id": "f1965587-a6ac-47ce-bd7a-bb98ca1d91b5", "metadata": {}, "outputs": [], "source": [ "os.system(\"Rscript download.r\")" ] }, { "cell_type": "markdown", "id": "5035b180-3d1b-4432-8ebe-b9c92bd93a7f", "metadata": {}, "source": [ "## Load features" ] }, { "cell_type": "markdown", "id": "793d7b24-b1ef-4dd2-ac26-079f7b67fba7", "metadata": {}, "source": [ "#### From Excel file" ] }, { "cell_type": "code", "execution_count": null, "id": "110a5ded-d25f-4cef-8e84-4f51210dfc26", "metadata": {}, "outputs": [], "source": [ "model.features = pd.read_excel('example.xlsx', sheet_name='example_sheet', nrows=100)" ] }, { "cell_type": "markdown", "id": "15f4af76-b93c-438c-b57f-f129d6e9ec99", "metadata": {}, "source": [ "#### From CSV file" ] }, { "cell_type": "code", "execution_count": null, "id": "8a3d5de6-6303-487a-8b4d-e6345792f7be", "metadata": {}, "outputs": [], "source": [ "model.features = pd.read_csv('example.csv', skiprows=0)" ] }, { "cell_type": "markdown", "id": "d8025ed7-0013-419b-8cb5-1a2db98f9eba", "metadata": {}, "source": [ "#### From JSON file" ] }, { "cell_type": "code", "execution_count": null, "id": "77face1a-b58f-4f8f-9fe8-1f12037be99a", "metadata": {}, "outputs": [], "source": [ "with open('features.json', 'r') as f:\n", " features_dict = json.load(f)[0]\n", "model.features = features_dict['template']" ] }, { "cell_type": "markdown", "id": "ee6d8fa0-4767-4c45-9717-eb1c95e2ddc0", "metadata": {}, "source": [ "## Load weights into base model" ] }, { "cell_type": "markdown", "id": "e57cf136-cf72-434c-843d-d3f4815b9135", "metadata": {}, "source": [ "#### From Excel file" ] }, { "cell_type": "code", "execution_count": null, "id": "a5ffad66-d948-4c78-aaf2-5809a4b42fa7", "metadata": {}, "outputs": [], "source": [ "weights = pd.read_excel('example.xlsx', sheet_name='example_sheet', nrows=100)" ] }, { "cell_type": "markdown", "id": "d79e5690-e284-4de6-8460-d3545a8192af", "metadata": {}, "source": [ "#### From CSV file" ] }, { "cell_type": "code", "execution_count": null, "id": "7f6187ed-fcff-4ff2-bcb1-b5bcef8190e8", "metadata": {}, "outputs": [], "source": [ "weights = pd.read_csv('example.csv', skiprows=0)" ] }, { "cell_type": "markdown", "id": "de92ee28-39b1-4356-a734-6b28a20e7bfe", "metadata": {}, "source": [ "#### From JSON file" ] }, { "cell_type": "code", "execution_count": null, "id": "e09b3463-4fd4-41b1-ac21-e63ddd223fe0", "metadata": {}, "outputs": [], "source": [ "with open('weights.json', 'r') as f:\n", " weights_dict = json.load(f)[0]" ] }, { "cell_type": "markdown", "id": "ad261636-5b00-4979-bb1d-67a851f7aa19", "metadata": {}, "source": [ "#### Linear model" ] }, { "cell_type": "code", "execution_count": null, "id": "d7f43b99-26f2-4622-9a76-316712058877", "metadata": {}, "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": "a5fb949f-500a-49d0-a792-2a774b0de18f", "metadata": {}, "source": [ "#### PC linear model" ] }, { "cell_type": "code", "execution_count": null, "id": "b6e1baed-fb83-4b97-a2e7-c2f17254ca47", "metadata": {}, "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": "code", "execution_count": null, "id": "ade0f4c9-2298-4fc3-bb72-d200907dd731", "metadata": {}, "outputs": [], "source": [ "model.reference_values = None" ] }, { "cell_type": "markdown", "id": "acf125f2-0e55-46c2-a915-57257d06f3f6", "metadata": {}, "source": [ "#### From Excel file" ] }, { "cell_type": "code", "execution_count": null, "id": "90d45266-962d-41b6-927c-6a147ed41305", "metadata": {}, "outputs": [], "source": [ "model.reference_values = pd.read_excel('example.xlsx', sheet_name='example_sheet', nrows=100)" ] }, { "cell_type": "markdown", "id": "f7fdae64-096a-4640-ade7-6a17b78a01d5", "metadata": {}, "source": [ "#### From CSV file" ] }, { "cell_type": "code", "execution_count": null, "id": "86de757f-fb38-4bcb-b91e-fc3372d22aad", "metadata": {}, "outputs": [], "source": [ "model.reference_values = pd.read_csv('example.csv', skiprows=0)" ] }, { "cell_type": "markdown", "id": "93271aee-d045-45ba-b030-7ec8e57add42", "metadata": {}, "source": [ "#### From JSON file" ] }, { "cell_type": "code", "execution_count": null, "id": "2089b66f-9cc4-4528-9bdc-5e45efc6d06b", "metadata": {}, "outputs": [], "source": [ "with open('reference_values.json', 'r') as f:\n", " reference_values_dict = json.load(f)[0]\n", "model.reference_values = reference_values_dict['template']" ] }, { "cell_type": "markdown", "id": "af3bcf7b-74a8-4d21-9ccb-4de0c2b0516b", "metadata": {}, "source": [ "## Load preprocess and postprocess objects" ] }, { "cell_type": "code", "execution_count": null, "id": "79a1b3a2-00f1-42b1-9fcd-f919343391d7", "metadata": {}, "outputs": [], "source": [ "model.preprocess_name = None\n", "model.preprocess_dependencies = None" ] }, { "cell_type": "code", "execution_count": null, "id": "ff4a21cb-cf41-44dc-9ed1-95cf8aa15772", "metadata": {}, "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": null, "id": "2168355c-47d9-475d-b816-49f65e74887c", "metadata": {}, "outputs": [], "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": null, "id": "352cffb0-c5a8-4c82-8f61-fce35baf5a22", "metadata": {}, "outputs": [], "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": null, "id": "0c3a2d80-1b5f-458a-926c-cbc0aa9416e1", "metadata": {}, "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": null, "id": "11aeaa70-44c0-42f9-86d7-740e3849a7a6", "metadata": {}, "outputs": [], "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 }