From d52939bd8acef13fc4eb4ae5f26d73aa5cdf0792 Mon Sep 17 00:00:00 2001 From: Victoria A <52001888+adjeiv@users.noreply.github.com> Date: Tue, 28 Nov 2023 13:57:15 +0000 Subject: [PATCH] Transfer to copy, and remove delete API fns --- .ipynb_checkpoints/MD Bug-checkpoint.ipynb | 104 +++++++++++++++++++ .ipynb_checkpoints/Untitled-checkpoint.ipynb | 6 ++ MD Bug.ipynb | 87 ++++++++++++++++ Untitled.ipynb | 85 +++++++++++++++ optuna_dashboard/_app.py | 3 +- optuna_dashboard/_note.py | 49 +++------ python_tests/test_note.py | 52 +--------- 7 files changed, 297 insertions(+), 89 deletions(-) create mode 100644 .ipynb_checkpoints/MD Bug-checkpoint.ipynb create mode 100644 .ipynb_checkpoints/Untitled-checkpoint.ipynb create mode 100644 MD Bug.ipynb create mode 100644 Untitled.ipynb diff --git a/.ipynb_checkpoints/MD Bug-checkpoint.ipynb b/.ipynb_checkpoints/MD Bug-checkpoint.ipynb new file mode 100644 index 00000000..980a307a --- /dev/null +++ b/.ipynb_checkpoints/MD Bug-checkpoint.ipynb @@ -0,0 +1,104 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 5, + "id": "32593824-793e-486e-8494-7f18f73ae5ee", + "metadata": {}, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "[I 2023-11-18 12:06:31,357] A new study created in RDB with name: mdbug2\n", + "[W 2023-11-18 12:06:31,454] Trial 0 failed with parameters: {} because of the following error: TypeError(\"tpe_objective_fn() missing 1 required positional argument: 'observations'\").\n", + "Traceback (most recent call last):\n", + " File \"C:\\Users\\victo\\Documents\\OpenSource\\optuna_workspace\\optuna\\optuna\\study\\_optimize.py\", line 200, in _run_trial\n", + " value_or_values = func(trial)\n", + " ^^^^^^^^^^^\n", + "TypeError: tpe_objective_fn() missing 1 required positional argument: 'observations'\n", + "[W 2023-11-18 12:06:31,457] Trial 0 failed with value None.\n" + ] + }, + { + "ename": "TypeError", + "evalue": "tpe_objective_fn() missing 1 required positional argument: 'observations'", + "output_type": "error", + "traceback": [ + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)", + "Cell \u001b[1;32mIn[5], line 30\u001b[0m\n\u001b[0;32m 25\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;18m__name__\u001b[39m \u001b[38;5;241m==\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m__main__\u001b[39m\u001b[38;5;124m\"\u001b[39m:\n\u001b[0;32m 26\u001b[0m study \u001b[38;5;241m=\u001b[39m optuna\u001b[38;5;241m.\u001b[39mcreate_study(\n\u001b[0;32m 27\u001b[0m storage\u001b[38;5;241m=\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124msqlite:///db.sqlite3\u001b[39m\u001b[38;5;124m\"\u001b[39m, \u001b[38;5;66;03m# Specify the storage URL here.\u001b[39;00m\n\u001b[0;32m 28\u001b[0m study_name\u001b[38;5;241m=\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mmdbug2\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[0;32m 29\u001b[0m )\n\u001b[1;32m---> 30\u001b[0m \u001b[43mstudy\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43moptimize\u001b[49m\u001b[43m(\u001b[49m\u001b[43mtpe_objective_fn\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mn_trials\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;241;43m7\u001b[39;49m\u001b[43m)\u001b[49m\n\u001b[0;32m 31\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mBest value: \u001b[39m\u001b[38;5;132;01m{\u001b[39;00mstudy\u001b[38;5;241m.\u001b[39mbest_value\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m (params: \u001b[39m\u001b[38;5;132;01m{\u001b[39;00mstudy\u001b[38;5;241m.\u001b[39mbest_params\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m)\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n", + "File \u001b[1;32m~\\Documents\\OpenSource\\optuna_workspace\\optuna\\optuna\\study\\study.py:451\u001b[0m, in \u001b[0;36mStudy.optimize\u001b[1;34m(self, func, n_trials, timeout, n_jobs, catch, callbacks, gc_after_trial, show_progress_bar)\u001b[0m\n\u001b[0;32m 348\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21moptimize\u001b[39m(\n\u001b[0;32m 349\u001b[0m \u001b[38;5;28mself\u001b[39m,\n\u001b[0;32m 350\u001b[0m func: ObjectiveFuncType,\n\u001b[1;32m (...)\u001b[0m\n\u001b[0;32m 357\u001b[0m show_progress_bar: \u001b[38;5;28mbool\u001b[39m \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;01mFalse\u001b[39;00m,\n\u001b[0;32m 358\u001b[0m ) \u001b[38;5;241m-\u001b[39m\u001b[38;5;241m>\u001b[39m \u001b[38;5;28;01mNone\u001b[39;00m:\n\u001b[0;32m 359\u001b[0m \u001b[38;5;250m \u001b[39m\u001b[38;5;124;03m\"\"\"Optimize an objective function.\u001b[39;00m\n\u001b[0;32m 360\u001b[0m \n\u001b[0;32m 361\u001b[0m \u001b[38;5;124;03m Optimization is done by choosing a suitable set of hyperparameter values from a given\u001b[39;00m\n\u001b[1;32m (...)\u001b[0m\n\u001b[0;32m 449\u001b[0m \u001b[38;5;124;03m If nested invocation of this method occurs.\u001b[39;00m\n\u001b[0;32m 450\u001b[0m \u001b[38;5;124;03m \"\"\"\u001b[39;00m\n\u001b[1;32m--> 451\u001b[0m \u001b[43m_optimize\u001b[49m\u001b[43m(\u001b[49m\n\u001b[0;32m 452\u001b[0m \u001b[43m \u001b[49m\u001b[43mstudy\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[43m,\u001b[49m\n\u001b[0;32m 453\u001b[0m \u001b[43m \u001b[49m\u001b[43mfunc\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mfunc\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m 454\u001b[0m \u001b[43m \u001b[49m\u001b[43mn_trials\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mn_trials\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m 455\u001b[0m \u001b[43m \u001b[49m\u001b[43mtimeout\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mtimeout\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m 456\u001b[0m \u001b[43m \u001b[49m\u001b[43mn_jobs\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mn_jobs\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m 457\u001b[0m \u001b[43m \u001b[49m\u001b[43mcatch\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;28;43mtuple\u001b[39;49m\u001b[43m(\u001b[49m\u001b[43mcatch\u001b[49m\u001b[43m)\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;28;43;01mif\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[38;5;28;43misinstance\u001b[39;49m\u001b[43m(\u001b[49m\u001b[43mcatch\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mIterable\u001b[49m\u001b[43m)\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;28;43;01melse\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[43m(\u001b[49m\u001b[43mcatch\u001b[49m\u001b[43m,\u001b[49m\u001b[43m)\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m 458\u001b[0m \u001b[43m \u001b[49m\u001b[43mcallbacks\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mcallbacks\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m 459\u001b[0m \u001b[43m \u001b[49m\u001b[43mgc_after_trial\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mgc_after_trial\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m 460\u001b[0m \u001b[43m \u001b[49m\u001b[43mshow_progress_bar\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mshow_progress_bar\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m 461\u001b[0m \u001b[43m \u001b[49m\u001b[43m)\u001b[49m\n", + "File \u001b[1;32m~\\Documents\\OpenSource\\optuna_workspace\\optuna\\optuna\\study\\_optimize.py:66\u001b[0m, in \u001b[0;36m_optimize\u001b[1;34m(study, func, n_trials, timeout, n_jobs, catch, callbacks, gc_after_trial, show_progress_bar)\u001b[0m\n\u001b[0;32m 64\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[0;32m 65\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m n_jobs \u001b[38;5;241m==\u001b[39m \u001b[38;5;241m1\u001b[39m:\n\u001b[1;32m---> 66\u001b[0m \u001b[43m_optimize_sequential\u001b[49m\u001b[43m(\u001b[49m\n\u001b[0;32m 67\u001b[0m \u001b[43m \u001b[49m\u001b[43mstudy\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m 68\u001b[0m \u001b[43m \u001b[49m\u001b[43mfunc\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m 69\u001b[0m \u001b[43m \u001b[49m\u001b[43mn_trials\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m 70\u001b[0m \u001b[43m \u001b[49m\u001b[43mtimeout\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m 71\u001b[0m \u001b[43m \u001b[49m\u001b[43mcatch\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m 72\u001b[0m \u001b[43m \u001b[49m\u001b[43mcallbacks\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m 73\u001b[0m \u001b[43m \u001b[49m\u001b[43mgc_after_trial\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m 74\u001b[0m \u001b[43m \u001b[49m\u001b[43mreseed_sampler_rng\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;28;43;01mFalse\u001b[39;49;00m\u001b[43m,\u001b[49m\n\u001b[0;32m 75\u001b[0m \u001b[43m \u001b[49m\u001b[43mtime_start\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;28;43;01mNone\u001b[39;49;00m\u001b[43m,\u001b[49m\n\u001b[0;32m 76\u001b[0m \u001b[43m \u001b[49m\u001b[43mprogress_bar\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mprogress_bar\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m 77\u001b[0m \u001b[43m \u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m 78\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[0;32m 79\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m n_jobs \u001b[38;5;241m==\u001b[39m \u001b[38;5;241m-\u001b[39m\u001b[38;5;241m1\u001b[39m:\n", + "File \u001b[1;32m~\\Documents\\OpenSource\\optuna_workspace\\optuna\\optuna\\study\\_optimize.py:163\u001b[0m, in \u001b[0;36m_optimize_sequential\u001b[1;34m(study, func, n_trials, timeout, catch, callbacks, gc_after_trial, reseed_sampler_rng, time_start, progress_bar)\u001b[0m\n\u001b[0;32m 160\u001b[0m \u001b[38;5;28;01mbreak\u001b[39;00m\n\u001b[0;32m 162\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[1;32m--> 163\u001b[0m frozen_trial \u001b[38;5;241m=\u001b[39m \u001b[43m_run_trial\u001b[49m\u001b[43m(\u001b[49m\u001b[43mstudy\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mfunc\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mcatch\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m 164\u001b[0m \u001b[38;5;28;01mfinally\u001b[39;00m:\n\u001b[0;32m 165\u001b[0m \u001b[38;5;66;03m# The following line mitigates memory problems that can be occurred in some\u001b[39;00m\n\u001b[0;32m 166\u001b[0m \u001b[38;5;66;03m# environments (e.g., services that use computing containers such as GitHub Actions).\u001b[39;00m\n\u001b[0;32m 167\u001b[0m \u001b[38;5;66;03m# Please refer to the following PR for further details:\u001b[39;00m\n\u001b[0;32m 168\u001b[0m \u001b[38;5;66;03m# https://github.com/optuna/optuna/pull/325.\u001b[39;00m\n\u001b[0;32m 169\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m gc_after_trial:\n", + "File \u001b[1;32m~\\Documents\\OpenSource\\optuna_workspace\\optuna\\optuna\\study\\_optimize.py:251\u001b[0m, in \u001b[0;36m_run_trial\u001b[1;34m(study, func, catch)\u001b[0m\n\u001b[0;32m 244\u001b[0m \u001b[38;5;28;01massert\u001b[39;00m \u001b[38;5;28;01mFalse\u001b[39;00m, \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mShould not reach.\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[0;32m 246\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m (\n\u001b[0;32m 247\u001b[0m frozen_trial\u001b[38;5;241m.\u001b[39mstate \u001b[38;5;241m==\u001b[39m TrialState\u001b[38;5;241m.\u001b[39mFAIL\n\u001b[0;32m 248\u001b[0m \u001b[38;5;129;01mand\u001b[39;00m func_err \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m\n\u001b[0;32m 249\u001b[0m \u001b[38;5;129;01mand\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;28misinstance\u001b[39m(func_err, catch)\n\u001b[0;32m 250\u001b[0m ):\n\u001b[1;32m--> 251\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m func_err\n\u001b[0;32m 252\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m frozen_trial\n", + "File \u001b[1;32m~\\Documents\\OpenSource\\optuna_workspace\\optuna\\optuna\\study\\_optimize.py:200\u001b[0m, in \u001b[0;36m_run_trial\u001b[1;34m(study, func, catch)\u001b[0m\n\u001b[0;32m 198\u001b[0m \u001b[38;5;28;01mwith\u001b[39;00m get_heartbeat_thread(trial\u001b[38;5;241m.\u001b[39m_trial_id, study\u001b[38;5;241m.\u001b[39m_storage):\n\u001b[0;32m 199\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[1;32m--> 200\u001b[0m value_or_values \u001b[38;5;241m=\u001b[39m \u001b[43mfunc\u001b[49m\u001b[43m(\u001b[49m\u001b[43mtrial\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m 201\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m exceptions\u001b[38;5;241m.\u001b[39mTrialPruned \u001b[38;5;28;01mas\u001b[39;00m e:\n\u001b[0;32m 202\u001b[0m \u001b[38;5;66;03m# TODO(mamu): Handle multi-objective cases.\u001b[39;00m\n\u001b[0;32m 203\u001b[0m state \u001b[38;5;241m=\u001b[39m TrialState\u001b[38;5;241m.\u001b[39mPRUNED\n", + "\u001b[1;31mTypeError\u001b[0m: tpe_objective_fn() missing 1 required positional argument: 'observations'" + ] + } + ], + "source": [ + "import optuna\n", + "import numpy as np\n", + "\n", + "def cost_function(x, y):\n", + " return np.rand(0, 1)\n", + "\n", + "def tpe_objective_fn(trial):\n", + " num_steps = 5\n", + " num_heat_sources = 7\n", + " lb = [0, 1, 2, 3, 4, 5, 6]\n", + " ub = [4, 5, 6, 7, 8, 9, 10]\n", + " chosen_parameters = []\n", + " for step in range(num_steps):\n", + " for idx in range(num_heat_sources):\n", + " chosen_parameters.append(\n", + " trial.suggest_float(f\"tech_param_{step}_{idx}\", lb[idx], ub[idx])\n", + " )\n", + "\n", + " cost = cost_function(\n", + " chosen_parameters,\n", + " num_steps,\n", + " )\n", + " return cost\n", + "\n", + "if __name__ == \"__main__\":\n", + " study = optuna.create_study(\n", + " storage=\"sqlite:///db.sqlite3\", # Specify the storage URL here.\n", + " study_name=\"mdbug2\"\n", + " )\n", + " study.optimize(tpe_objective_fn, n_trials=7)\n", + " print(f\"Best value: {study.best_value} (params: {study.best_params})\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "2c66fec2-3e46-46c4-bc55-e784d237a3e5", + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "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.11.4" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/.ipynb_checkpoints/Untitled-checkpoint.ipynb b/.ipynb_checkpoints/Untitled-checkpoint.ipynb new file mode 100644 index 00000000..363fcab7 --- /dev/null +++ b/.ipynb_checkpoints/Untitled-checkpoint.ipynb @@ -0,0 +1,6 @@ +{ + "cells": [], + "metadata": {}, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/MD Bug.ipynb b/MD Bug.ipynb new file mode 100644 index 00000000..75b0ea27 --- /dev/null +++ b/MD Bug.ipynb @@ -0,0 +1,87 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 10, + "id": "32593824-793e-486e-8494-7f18f73ae5ee", + "metadata": {}, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "[I 2023-11-18 12:13:05,829] A new study created in RDB with name: mdbugb\n", + "[I 2023-11-18 12:13:06,187] Trial 0 finished with value: 0.9 and parameters: {'tech_param_0_0': 0.9863311667179313, 'tech_param_0_1': 2.9722067965404126, 'tech_param_0_2': 5.479907499743063, 'tech_param_0_3': 6.193147335867399, 'tech_param_0_4': 6.44329845141624, 'tech_param_0_5': 6.957704292533055, 'tech_param_0_6': 6.028116976338117, 'tech_param_1_0': 0.8528770906850203, 'tech_param_1_1': 2.8845780424110843, 'tech_param_1_2': 2.205086423835636, 'tech_param_1_3': 5.8965466201793735, 'tech_param_1_4': 6.765916728183967, 'tech_param_1_5': 5.216406754175934, 'tech_param_1_6': 9.921684209548362, 'tech_param_2_0': 3.9278254745655903, 'tech_param_2_1': 2.8605537473963465, 'tech_param_2_2': 4.466559550814772, 'tech_param_2_3': 4.838464946220083, 'tech_param_2_4': 4.026691756550308, 'tech_param_2_5': 5.3788556566702805, 'tech_param_2_6': 6.675770800145582, 'tech_param_3_0': 3.031562395355724, 'tech_param_3_1': 3.7349707362119777, 'tech_param_3_2': 5.473994937262473, 'tech_param_3_3': 6.881019240236522, 'tech_param_3_4': 6.120001351513482, 'tech_param_3_5': 6.036086717431755, 'tech_param_3_6': 9.160127979158023, 'tech_param_4_0': 2.758775673013467, 'tech_param_4_1': 1.0680707015794786, 'tech_param_4_2': 3.3292082013802666, 'tech_param_4_3': 6.687649735544305, 'tech_param_4_4': 7.443681016669299, 'tech_param_4_5': 7.162694801047951, 'tech_param_4_6': 9.999288376422493}. Best is trial 0 with value: 0.9.\n", + "[I 2023-11-18 12:13:06,501] Trial 1 finished with value: 0.9 and parameters: {'tech_param_0_0': 1.7140076646332538, 'tech_param_0_1': 3.2316252813476325, 'tech_param_0_2': 4.306343376567941, 'tech_param_0_3': 4.61192706012931, 'tech_param_0_4': 5.988119724240175, 'tech_param_0_5': 6.9327357154708285, 'tech_param_0_6': 6.53997726319049, 'tech_param_1_0': 0.3905783000188636, 'tech_param_1_1': 2.76165385255339, 'tech_param_1_2': 2.6085969410537855, 'tech_param_1_3': 4.749816941187031, 'tech_param_1_4': 4.846918571308436, 'tech_param_1_5': 6.254401319703383, 'tech_param_1_6': 7.8373063069546935, 'tech_param_2_0': 2.91103305557099, 'tech_param_2_1': 4.471679319725785, 'tech_param_2_2': 3.5984067288829946, 'tech_param_2_3': 3.470290937133842, 'tech_param_2_4': 7.799315424111551, 'tech_param_2_5': 7.826822170149293, 'tech_param_2_6': 8.849116163752687, 'tech_param_3_0': 3.5380619781168816, 'tech_param_3_1': 3.653451480021751, 'tech_param_3_2': 3.47434126199421, 'tech_param_3_3': 4.443348897470383, 'tech_param_3_4': 7.045847902681517, 'tech_param_3_5': 6.2594143301415475, 'tech_param_3_6': 7.979774488188764, 'tech_param_4_0': 1.0325150795599196, 'tech_param_4_1': 3.4448235367545186, 'tech_param_4_2': 3.3149959756969167, 'tech_param_4_3': 4.819419990796481, 'tech_param_4_4': 7.240391392837244, 'tech_param_4_5': 6.412867172919929, 'tech_param_4_6': 7.784828203240426}. Best is trial 0 with value: 0.9.\n", + "[I 2023-11-18 12:13:06,830] Trial 2 finished with value: 0.9 and parameters: {'tech_param_0_0': 3.7285153030023213, 'tech_param_0_1': 2.967995773245595, 'tech_param_0_2': 2.1806768067032025, 'tech_param_0_3': 4.166177958423523, 'tech_param_0_4': 7.340303214415031, 'tech_param_0_5': 8.466190260210265, 'tech_param_0_6': 8.838603377394787, 'tech_param_1_0': 1.7630336762562222, 'tech_param_1_1': 1.1619500439780515, 'tech_param_1_2': 3.4038384919507747, 'tech_param_1_3': 6.650347896582192, 'tech_param_1_4': 7.7764728822600055, 'tech_param_1_5': 8.639229218462436, 'tech_param_1_6': 7.113069116576301, 'tech_param_2_0': 3.9931082904645394, 'tech_param_2_1': 3.1473900771322443, 'tech_param_2_2': 4.8958785737518475, 'tech_param_2_3': 4.135490374890335, 'tech_param_2_4': 4.642792333607787, 'tech_param_2_5': 6.804997337959285, 'tech_param_2_6': 7.093905518212265, 'tech_param_3_0': 3.7794491588384793, 'tech_param_3_1': 2.9200030292104078, 'tech_param_3_2': 2.193269350123763, 'tech_param_3_3': 4.4228612589114915, 'tech_param_3_4': 6.768417418097517, 'tech_param_3_5': 7.833147977002255, 'tech_param_3_6': 6.043265945471216, 'tech_param_4_0': 1.239068042928749, 'tech_param_4_1': 3.0223635344959376, 'tech_param_4_2': 4.542741924432429, 'tech_param_4_3': 5.527626147325485, 'tech_param_4_4': 6.41989159985463, 'tech_param_4_5': 5.957849401921937, 'tech_param_4_6': 8.220767432559985}. Best is trial 0 with value: 0.9.\n", + "[I 2023-11-18 12:13:07,175] Trial 3 finished with value: 0.9 and parameters: {'tech_param_0_0': 2.3406080881578437, 'tech_param_0_1': 2.20431580814314, 'tech_param_0_2': 2.315142042718149, 'tech_param_0_3': 3.153313622905333, 'tech_param_0_4': 6.434868333320647, 'tech_param_0_5': 8.010675478290011, 'tech_param_0_6': 9.795451780231673, 'tech_param_1_0': 3.893698909905487, 'tech_param_1_1': 3.448571655312496, 'tech_param_1_2': 2.7450589731117088, 'tech_param_1_3': 5.2194903883501, 'tech_param_1_4': 7.236541468886768, 'tech_param_1_5': 6.823921860165192, 'tech_param_1_6': 9.616564141906174, 'tech_param_2_0': 0.39123634971167975, 'tech_param_2_1': 2.39626451267548, 'tech_param_2_2': 3.0034883416963494, 'tech_param_2_3': 6.376136926494553, 'tech_param_2_4': 7.218911507033649, 'tech_param_2_5': 6.750947297023547, 'tech_param_2_6': 8.736332931103936, 'tech_param_3_0': 0.6196213625340548, 'tech_param_3_1': 3.387324252791688, 'tech_param_3_2': 4.183149086442488, 'tech_param_3_3': 5.183411467900889, 'tech_param_3_4': 6.267654527841776, 'tech_param_3_5': 5.549621056761763, 'tech_param_3_6': 7.672308955901496, 'tech_param_4_0': 1.246623944346387, 'tech_param_4_1': 4.456831294515673, 'tech_param_4_2': 2.0724147884920074, 'tech_param_4_3': 5.379419267307556, 'tech_param_4_4': 7.112725631815934, 'tech_param_4_5': 8.87353147211932, 'tech_param_4_6': 7.693639964574908}. Best is trial 0 with value: 0.9.\n", + "[I 2023-11-18 12:13:07,487] Trial 4 finished with value: 0.9 and parameters: {'tech_param_0_0': 3.1068581046485, 'tech_param_0_1': 2.9287122006776443, 'tech_param_0_2': 5.4057937949623085, 'tech_param_0_3': 3.157484488408773, 'tech_param_0_4': 4.690948899704123, 'tech_param_0_5': 8.145312132270456, 'tech_param_0_6': 6.089344894219481, 'tech_param_1_0': 2.9926885191436527, 'tech_param_1_1': 3.176774691423508, 'tech_param_1_2': 2.7721710618333737, 'tech_param_1_3': 4.67762193566592, 'tech_param_1_4': 4.012363168044638, 'tech_param_1_5': 5.659401760473942, 'tech_param_1_6': 8.830934616907934, 'tech_param_2_0': 1.661839241732621, 'tech_param_2_1': 2.7405670060757923, 'tech_param_2_2': 2.100697848478035, 'tech_param_2_3': 6.253340195464311, 'tech_param_2_4': 6.976724037705612, 'tech_param_2_5': 5.510598362317128, 'tech_param_2_6': 7.522477241180887, 'tech_param_3_0': 2.4453135979613685, 'tech_param_3_1': 1.695095408763359, 'tech_param_3_2': 3.102101960127642, 'tech_param_3_3': 4.087680321929806, 'tech_param_3_4': 6.130088543099206, 'tech_param_3_5': 7.350026673357341, 'tech_param_3_6': 8.078588257404482, 'tech_param_4_0': 3.4813120281559478, 'tech_param_4_1': 3.6566237591430295, 'tech_param_4_2': 4.615726645672355, 'tech_param_4_3': 3.5688464780387226, 'tech_param_4_4': 4.212091370332146, 'tech_param_4_5': 6.790648339845854, 'tech_param_4_6': 9.793540661498884}. Best is trial 0 with value: 0.9.\n", + "[I 2023-11-18 12:13:07,804] Trial 5 finished with value: 0.9 and parameters: {'tech_param_0_0': 3.7955449152390734, 'tech_param_0_1': 3.5879051604064425, 'tech_param_0_2': 5.047936105941167, 'tech_param_0_3': 6.409387696471882, 'tech_param_0_4': 6.139796193716612, 'tech_param_0_5': 5.818035000269156, 'tech_param_0_6': 6.77671788266593, 'tech_param_1_0': 3.538002063564025, 'tech_param_1_1': 4.653916400756065, 'tech_param_1_2': 5.139067082232518, 'tech_param_1_3': 5.258612796259207, 'tech_param_1_4': 5.762995511794475, 'tech_param_1_5': 5.914412902115752, 'tech_param_1_6': 8.821608430328544, 'tech_param_2_0': 2.8391352600362043, 'tech_param_2_1': 1.3147834382265242, 'tech_param_2_2': 5.712530324883787, 'tech_param_2_3': 3.9982127746896365, 'tech_param_2_4': 7.59784603286235, 'tech_param_2_5': 8.249304067800995, 'tech_param_2_6': 9.13797124898645, 'tech_param_3_0': 3.4218288637879235, 'tech_param_3_1': 2.8432006830690506, 'tech_param_3_2': 3.705582741959596, 'tech_param_3_3': 4.932782214722957, 'tech_param_3_4': 5.397522930636624, 'tech_param_3_5': 7.665389302007817, 'tech_param_3_6': 8.161440460609116, 'tech_param_4_0': 0.9002808033064547, 'tech_param_4_1': 2.8214804227095938, 'tech_param_4_2': 4.209001287953269, 'tech_param_4_3': 3.4276241669947267, 'tech_param_4_4': 5.65920280643175, 'tech_param_4_5': 7.0296445059400385, 'tech_param_4_6': 6.58394027927913}. Best is trial 0 with value: 0.9.\n", + "[I 2023-11-18 12:13:08,436] Trial 6 finished with value: 0.9 and parameters: {'tech_param_0_0': 2.1661335557317467, 'tech_param_0_1': 2.10482781209877, 'tech_param_0_2': 3.5267055048813605, 'tech_param_0_3': 3.3403923781628193, 'tech_param_0_4': 7.251793183308415, 'tech_param_0_5': 5.419077835893116, 'tech_param_0_6': 6.92337803745288, 'tech_param_1_0': 2.248588320589992, 'tech_param_1_1': 1.5496877038644339, 'tech_param_1_2': 3.034290526391054, 'tech_param_1_3': 5.70430130433426, 'tech_param_1_4': 4.755971043039262, 'tech_param_1_5': 6.032107463009746, 'tech_param_1_6': 6.485937845252712, 'tech_param_2_0': 1.1198667631159256, 'tech_param_2_1': 4.443832638453198, 'tech_param_2_2': 4.029162583096888, 'tech_param_2_3': 4.421249989146505, 'tech_param_2_4': 7.266267320462586, 'tech_param_2_5': 7.272268040989632, 'tech_param_2_6': 9.34608451054512, 'tech_param_3_0': 3.5400627448638406, 'tech_param_3_1': 1.0291771457580832, 'tech_param_3_2': 3.001266350018537, 'tech_param_3_3': 3.83986701735892, 'tech_param_3_4': 4.21380354332598, 'tech_param_3_5': 5.7510339129863555, 'tech_param_3_6': 9.666174111722901, 'tech_param_4_0': 0.25936572770098243, 'tech_param_4_1': 1.0411384740076457, 'tech_param_4_2': 3.6068625395816722, 'tech_param_4_3': 4.778931228821068, 'tech_param_4_4': 5.259093795072825, 'tech_param_4_5': 8.171709511787906, 'tech_param_4_6': 9.580347111957641}. Best is trial 0 with value: 0.9.\n" + ] + } + ], + "source": [ + "import optuna\n", + "import numpy as np\n", + "\n", + "def cost_function(x, y):\n", + " return 0.9\n", + "\n", + "def tpe_objective_fn(trial):\n", + " num_steps = 5\n", + " num_heat_sources = 7\n", + " lb = [0, 1, 2, 3, 4, 5, 6]\n", + " ub = [4, 5, 6, 7, 8, 9, 10]\n", + " chosen_parameters = []\n", + " for step in range(num_steps):\n", + " for idx in range(num_heat_sources):\n", + " chosen_parameters.append(\n", + " trial.suggest_float(f\"tech_param_{step}_{idx}\", lb[idx], ub[idx])\n", + " )\n", + "\n", + " cost = cost_function(\n", + " chosen_parameters,\n", + " num_steps,\n", + " )\n", + " return cost\n", + "\n", + "if __name__ == \"__main__\":\n", + " study = optuna.create_study(\n", + " storage=\"sqlite:///db.sqlite3\", # Specify the storage URL here.\n", + " study_name=\"mdbugb\"\n", + " )\n", + " study.optimize(tpe_objective_fn, n_trials=7)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "2c66fec2-3e46-46c4-bc55-e784d237a3e5", + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "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.11.4" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/Untitled.ipynb b/Untitled.ipynb new file mode 100644 index 00000000..2307d508 --- /dev/null +++ b/Untitled.ipynb @@ -0,0 +1,85 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 1, + "id": "c45a38b9-08a7-4ad1-85eb-722b50240f2d", + "metadata": {}, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "C:\\Users\\victo\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\site-packages\\tqdm\\auto.py:21: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html\n", + " from .autonotebook import tqdm as notebook_tqdm\n", + "[I 2023-10-21 17:21:34,373] A new study created in RDB with name: quadratic-simple3\n", + "[I 2023-10-21 17:21:34,461] Trial 0 finished with value: 4357.102948257252 and parameters: {'x': 66.0007799064318, 'y': 1}. Best is trial 0 with value: 4357.102948257252.\n", + "[W 2023-10-21 17:21:34,508] Trial 1 failed with parameters: {'x': -21.941587885601493, 'y': -1} because of the following error: The value None could not be cast to float..\n", + "[W 2023-10-21 17:21:34,509] Trial 1 failed with value None.\n", + "[I 2023-10-21 17:21:34,566] Trial 2 finished with value: 2536.6789394289817 and parameters: {'x': 50.37538029066363, 'y': -1}. Best is trial 2 with value: 2536.6789394289817.\n", + "[W 2023-10-21 17:21:34,612] Trial 3 failed with parameters: {'x': -96.83015025022239, 'y': 0} because of the following error: The value None could not be cast to float..\n", + "[W 2023-10-21 17:21:34,613] Trial 3 failed with value None.\n", + "[I 2023-10-21 17:21:34,670] Trial 4 finished with value: 3591.471500742954 and parameters: {'x': -59.920543228036195, 'y': 1}. Best is trial 2 with value: 2536.6789394289817.\n", + "[W 2023-10-21 17:21:34,717] Trial 5 failed with parameters: {'x': -65.11319825521136, 'y': 1} because of the following error: The value None could not be cast to float..\n", + "[W 2023-10-21 17:21:34,717] Trial 5 failed with value None.\n", + "[I 2023-10-21 17:21:34,769] Trial 6 finished with value: 9478.234748213168 and parameters: {'x': -97.35622603723486, 'y': 0}. Best is trial 2 with value: 2536.6789394289817.\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Best value: 2536.6789394289817 (params: {'x': 50.37538029066363, 'y': -1})\n" + ] + } + ], + "source": [ + "import optuna\n", + "\n", + "def objective(trial):\n", + " x = trial.suggest_float(\"x\", -100, 100)\n", + " y = trial.suggest_categorical(\"y\", [-1, 0, 1])\n", + " if trial._trial_id % 2:\n", + " return None\n", + " return x**2 + y\n", + "\n", + "if __name__ == \"__main__\":\n", + " study = optuna.create_study(\n", + " storage=\"sqlite:///db.sqlite3\", # Specify the storage URL here.\n", + " study_name=\"quadratic-simple3\"\n", + " )\n", + " study.optimize(objective, n_trials=7)\n", + " print(f\"Best value: {study.best_value} (params: {study.best_params})\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "fedfab0b-c074-4d94-b63f-e6ee7df8e9e0", + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "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.11.4" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/optuna_dashboard/_app.py b/optuna_dashboard/_app.py index 32e2a431..f10d051f 100644 --- a/optuna_dashboard/_app.py +++ b/optuna_dashboard/_app.py @@ -165,7 +165,7 @@ def create_app( response.status = 500 return {"reason": "Failed to load the new study"} - note.transfer_notes(storage, src_study, dst_study) + note.copy_notes(storage, src_study, dst_study) storage.delete_study(src_study._study_id) response.status = 201 return serialize_study_summary(new_study_summary) @@ -177,7 +177,6 @@ def create_app( delete_all_artifacts(artifact_store, storage, study_id) try: - note.delete_study_notes(storage, study_id) storage.delete_study(study_id) except KeyError: response.status = 404 # Not found diff --git a/optuna_dashboard/_note.py b/optuna_dashboard/_note.py index 57135211..507e3d8f 100644 --- a/optuna_dashboard/_note.py +++ b/optuna_dashboard/_note.py @@ -110,21 +110,17 @@ def note_str_key_prefix(trial_id: Optional[int]) -> str: return f"dashboard:{trial_id}:note_str:" -def transfer_notes(storage: BaseStorage, src_study: optuna.Study, dst_study: optuna.Study) -> None: +def copy_notes(storage: BaseStorage, src_study: optuna.Study, dst_study: optuna.Study) -> None: system_attrs = storage.get_study_system_attrs(study_id=src_study._study_id) - def transfer(src_trial_id: Optional[int], dst_trial_id: Optional[int]) -> None: - note = get_note_from_system_attrs(system_attrs, src_trial_id)["body"] - save_note_with_version(storage, dst_study._study_id, dst_trial_id, 0, note) - delete_notes(storage, src_study._study_id, src_trial_id) - - # Transfer individual trial notes + # Copy individual trial notes for src_trial, dst_trial in zip(src_study.get_trials(), dst_study.get_trials()): - transfer(src_trial._trial_id, dst_trial._trial_id) + note = get_note_from_system_attrs(system_attrs, src_trial._trial_id)["body"] + save_note_with_version(storage, dst_study._study_id, dst_trial._trial_id, 0, note) - # Transfer study note - NO_SRC_TRIAL, NO_DST_TRIAL = None, None - transfer(NO_SRC_TRIAL, NO_DST_TRIAL) + # Copy study note + note = get_note_from_system_attrs(system_attrs, None)["body"] + save_note_with_version(storage, dst_study._study_id, None, 0, note) def get_note_from_system_attrs(system_attrs: dict[str, Any], trial_id: Optional[int]) -> NoteType: @@ -149,17 +145,6 @@ def version_is_incremented( return req_note_ver == db_note_ver + 1 -def all_trial_notes( - storage: BaseStorage, study_id: int, trial_id: Optional[int] -) -> dict[str, str]: - all_note_attrs: dict[str, str] = { - key: value - for key, value in storage.get_study_system_attrs(study_id).items() - if key.startswith(note_str_key_prefix(trial_id)) - } - return all_note_attrs - - def save_note_with_version( storage: BaseStorage, study_id: int, trial_id: Optional[int], ver: int, body: str ) -> None: @@ -170,26 +155,16 @@ def save_note_with_version( storage.set_study_system_attr(study_id, k, v) # Clear previous messages - all_note_attrs = all_trial_notes(storage, study_id, trial_id) + all_note_attrs: dict[str, str] = { + key: value + for key, value in storage.get_study_system_attrs(study_id).items() + if key.startswith(note_str_key_prefix(trial_id)) + } if len(all_note_attrs) > len(attrs): for i in range(len(attrs), len(all_note_attrs)): storage.set_study_system_attr(study_id, f"{note_str_key_prefix(trial_id)}{i}", "") -def delete_study_notes(storage: BaseStorage, study_id: int) -> None: - for trial in storage.get_all_trials(study_id): - delete_notes(storage, study_id, trial._trial_id) - - delete_notes(storage, study_id, None) - - -def delete_notes(storage: BaseStorage, study_id: int, trial_id: Optional[int]) -> None: - all_note_attrs = all_trial_notes(storage, study_id, trial_id) - - for i in range(len(all_note_attrs)): - storage.set_study_system_attr(study_id, f"{note_str_key_prefix(trial_id)}{i}", "") - - def split_body(note_str: str, trial_id: Optional[int]) -> dict[str, str]: note_len = len(note_str) attrs = {} diff --git a/python_tests/test_note.py b/python_tests/test_note.py index 55c8f610..592cf3e7 100644 --- a/python_tests/test_note.py +++ b/python_tests/test_note.py @@ -54,50 +54,7 @@ class NoteTestCase(TestCase): self.assertEqual(note_dict["body"], body) self.assertEqual(note_dict["version"], expected_ver) - def test_delete_notes_trial(self) -> None: - study = optuna.create_study() - trials = [ - study.ask({"x1": optuna.distributions.FloatDistribution(0, 10)}) for _ in range(2) - ] - storage = study._storage - - notes = ["trial 0", "trial 1"] - for trial, body in zip(trials, notes): - with self.subTest(body): - save_note(trial, body) - - self.assertEqual(get_note(trial), body) - - note.delete_notes(storage, study._study_id, trial._trial_id) - - self.assertEqual(get_note(trial), "") - - def test_delete_notes_study(self) -> None: - study = optuna.create_study() - trials = [ - study.ask({"x1": optuna.distributions.FloatDistribution(0, 10)}) for _ in range(2) - ] - storage = study._storage - - notes = ["trial 0", "trial 1"] - for trial, body in zip(trials, notes): - with self.subTest(body): - save_note(trial, body) - - actual = get_note(trial) - self.assertEqual(actual, body) - - save_note(study, "Study note") - actual = get_note(study) - self.assertEqual(actual, "Study note") - - note.delete_study_notes(storage, study._study_id) - - for trial in trials: - self.assertEqual(get_note(trial), "") - self.assertEqual(get_note(study), "") - - def test_transfer_notes(self) -> None: + def test_copy_notes(self) -> None: old_study = optuna.create_study() old_trials = [ old_study.ask({"x1": optuna.distributions.FloatDistribution(0, 10)}) for _ in range(2) @@ -112,12 +69,7 @@ class NoteTestCase(TestCase): new_study = optuna.create_study(storage=storage, directions=old_study.directions) new_study.add_trials(old_study.get_trials(deepcopy=False)) - note.transfer_notes(storage, old_study, new_study) - - for old_trial in old_trials: - self.assertEqual(get_note(old_trial), "") - self.assertEqual(get_note(old_study), "") - + note.copy_notes(storage, old_study, new_study) system_attrs = new_study._storage.get_study_system_attrs(new_study._study_id) for new_trial, body in zip(new_study.get_trials(), notes): actual = note.get_note_from_system_attrs(system_attrs, new_trial._trial_id)