From 38f4075c97eda2960c23e99d1ec6e907901537c5 Mon Sep 17 00:00:00 2001 From: Bas Nijholt <basnijholt@gmail.com> Date: Mon, 23 Sep 2019 18:11:49 +0200 Subject: [PATCH] improve error fig --- figures.ipynb | 49 ++++++++++++++++++++++++++++++------------------- 1 file changed, 30 insertions(+), 19 deletions(-) diff --git a/figures.ipynb b/figures.ipynb index d954248..063bea0 100644 --- a/figures.ipynb +++ b/figures.ipynb @@ -20,15 +20,22 @@ "metadata": {}, "outputs": [], "source": [ + "import functools\n", + "import itertools\n", + "import pickle\n", + "\n", + "import holoviews.plotting.mpl\n", + "import matplotlib; matplotlib.use(\"agg\")\n", + "import matplotlib.pyplot as plt\n", + "import matplotlib.tri as mtri\n", "import numpy as np\n", - "import matplotlib\n", + "from scipy import interpolate\n", "\n", - "matplotlib.use(\"agg\")\n", + "import adaptive\n", "\n", - "import matplotlib.pyplot as plt\n", "\n", "%matplotlib inline\n", - "# %config InlineBackend.figure_format = 'svg'\n", + "%config InlineBackend.figure_format = 'svg'\n", "\n", "golden_mean = (np.sqrt(5) - 1) / 2 # Aesthetic ratio\n", "fig_width_pt = 246.0 # Columnwidth\n", @@ -53,16 +60,7 @@ "}\n", "\n", "plt.rcParams.update(params)\n", - "plt.rc(\"text.latex\", preamble=[r\"\\usepackage{xfrac}\", r\"\\usepackage{siunitx}\"])\n", - "\n", - "import adaptive\n", - "from scipy import interpolate\n", - "import functools\n", - "import itertools\n", - "import adaptive\n", - "import holoviews.plotting.mpl\n", - "import time\n", - "import matplotlib.tri as mtri" + "plt.rc(\"text.latex\", preamble=[r\"\\usepackage{xfrac}\", r\"\\usepackage{siunitx}\"])" ] }, { @@ -335,6 +333,8 @@ "metadata": {}, "outputs": [], "source": [ + "import time\n", + "\n", "learner = adaptive.Learner1D(funcs_1D[0][\"function\"], bounds=funcs_1D[0][\"bounds\"])\n", "times = []\n", "for i in range(10000):\n", @@ -665,8 +665,8 @@ " return err_lin, err_learner\n", "\n", "\n", - "N_max = 100\n", - "Ns = np.geomspace(4, N_max, 20).astype(int)\n", + "N_max = 10_000\n", + "Ns = np.geomspace(4, N_max, 50).astype(int)\n", "\n", "loss_1D = adaptive.learner.learner1D.curvature_loss_function()\n", "loss_2D = adaptive.learner.learnerND.curvature_loss_function()\n", @@ -688,7 +688,10 @@ " err[i][d[\"title\"]] = (err_hom, err_adaptive)\n", "\n", " d[\"err_hom\"] = err_hom\n", - " d[\"err_adaptive\"] = err_adaptive" + " d[\"err_adaptive\"] = err_adaptive\n", + "\n", + "with open(\"error_line_loss.pickle\", \"wb\") as f:\n", + " pickle.dump(err, f)" ] }, { @@ -697,10 +700,16 @@ "metadata": {}, "outputs": [], "source": [ + "import pickle, os\n", + "with open('error_line_loss.pickle', 'rb') as f:\n", + " err = pickle.load(f)\n", + " \n", "fig, axs = plt.subplots(2, 1, figsize=(fig_width, 1.6 * fig_height))\n", + "plt.subplots_adjust(hspace=0.3)\n", + "\n", + "axs[1].set_xlabel(\"$N$\")\n", "\n", "for i, ax in enumerate(axs):\n", - " ax.set_xlabel(\"$N$\")\n", " ax.set_ylabel(r\"$\\text{Err}_{1}(\\tilde{f})$\")\n", "\n", " for j, (title, (err_hom, err_adaptive)) in enumerate(err[i].items()):\n", @@ -708,8 +717,8 @@ " label = \"abc\"[j]\n", " ax.loglog(Ns, err_hom, ls=\"--\", c=color)\n", " ax.loglog(Ns, err_adaptive, label=f\"$\\mathrm{{({label})}}$ {title}\", c=color)\n", + " ax.legend()\n", "\n", - "plt.legend()\n", "plt.savefig(\"figures/line_loss_error.pdf\", bbox_inches=\"tight\", transparent=True)\n", "plt.show()" ] @@ -721,9 +730,11 @@ "outputs": [], "source": [ "# Error reduction\n", + "print(\"1D\")\n", "for title, (err_hom, err_adaptive) in err[0].items():\n", " print(title, err_hom[-1] / err_adaptive[-1])\n", "\n", + "print(\"2D\")\n", "for title, (err_hom, err_adaptive) in err[1].items():\n", " print(title, err_hom[-1] / err_adaptive[-1])" ] -- GitLab