notebooks/plot_test_case.ipynb (160 lines of code) (raw):

{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import matplotlib.pyplot as plt\n", "from matplotlib import cm, colors, rcParams\n", "\n", "import numpy as np\n", "\n", "import bayesmark.constants as cc\n", "from bayesmark.path_util import abspath\n", "from bayesmark.serialize import XRSerializer\n", "from bayesmark.constants import ITER, METHOD, TEST_CASE, OBJECTIVE, VISIBLE_TO_OPT" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# User settings, must specify location of the data to make plots here for this to run\n", "DB_ROOT = abspath(\".\")\n", "DBID = \"bo_example_folder\"\n", "metric_for_scoring = VISIBLE_TO_OPT" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Matplotlib setup\n", "# Note this will put type-3 font BS in the pdfs, if it matters\n", "rcParams[\"mathtext.fontset\"] = \"stix\"\n", "rcParams[\"font.family\"] = \"STIXGeneral\"" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def build_color_dict(names):\n", " \"\"\"Make a color dictionary to give each name a mpl color.\n", " \"\"\"\n", " norm = colors.Normalize(vmin=0, vmax=1)\n", " m = cm.ScalarMappable(norm, cm.tab20)\n", " color_dict = m.to_rgba(np.linspace(0, 1, len(names)))\n", " color_dict = dict(zip(names, color_dict))\n", " return color_dict" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Load the data\n", "agg_results_ds, meta = XRSerializer.load_derived(DB_ROOT, db=DBID, key=cc.PERF_RESULTS)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Setup for plotting\n", "method_list = agg_results_ds.coords[METHOD].values\n", "method_to_rgba = build_color_dict(method_list.tolist())" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Make the plots for inidividual test functions\n", "for func_name in agg_results_ds.coords[TEST_CASE].values:\n", " plt.figure(figsize=(5, 5), dpi=300)\n", " for method_name in method_list:\n", " curr_ds = agg_results_ds.sel({TEST_CASE: func_name, METHOD: method_name, OBJECTIVE: metric_for_scoring})\n", "\n", " plt.fill_between(\n", " curr_ds.coords[ITER].values,\n", " curr_ds[cc.LB_MED].values,\n", " curr_ds[cc.UB_MED].values,\n", " color=method_to_rgba[method_name],\n", " alpha=0.5,\n", " )\n", " plt.plot(\n", " curr_ds.coords[ITER].values,\n", " curr_ds[cc.PERF_MED].values,\n", " color=method_to_rgba[method_name],\n", " label=method_name,\n", " marker=\".\",\n", " )\n", " plt.xlabel(\"evaluation\", fontsize=10)\n", " plt.ylabel(\"median score\", fontsize=10)\n", " plt.title(func_name)\n", " plt.legend(fontsize=8, bbox_to_anchor=(1.05, 1), loc=\"upper left\", borderaxespad=0.0)\n", " plt.grid()\n", "\n", " plt.figure(figsize=(5, 5), dpi=300)\n", " for method_name in method_list:\n", " curr_ds = agg_results_ds.sel({TEST_CASE: func_name, METHOD: method_name, OBJECTIVE: metric_for_scoring})\n", "\n", " plt.fill_between(\n", " curr_ds.coords[ITER].values,\n", " curr_ds[cc.LB_MEAN].values,\n", " curr_ds[cc.UB_MEAN].values,\n", " color=method_to_rgba[method_name],\n", " alpha=0.5,\n", " )\n", " plt.plot(\n", " curr_ds.coords[ITER].values,\n", " curr_ds[cc.PERF_MEAN].values,\n", " color=method_to_rgba[method_name],\n", " label=method_name,\n", " marker=\".\",\n", " )\n", " plt.xlabel(\"evaluation\", fontsize=10)\n", " plt.ylabel(\"mean score\", fontsize=10)\n", " plt.title(func_name)\n", " plt.legend(fontsize=8, bbox_to_anchor=(1.05, 1), loc=\"upper left\", borderaxespad=0.0)\n", " plt.grid()" ] } ], "metadata": { "kernelspec": { "display_name": "bobm_ipynb", "language": "python", "name": "bobm_ipynb" }, "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.6.5" } }, "nbformat": 4, "nbformat_minor": 2 }