From 49e4821a8c97e66cca8db72ed01454bad0948748 Mon Sep 17 00:00:00 2001 From: Bas Nijholt <basnijholt@gmail.com> Date: Mon, 23 Sep 2019 13:27:09 +0200 Subject: [PATCH] style --- figures.ipynb | 63 ++++++++++++++++++++++++++------------------------- 1 file changed, 32 insertions(+), 31 deletions(-) diff --git a/figures.ipynb b/figures.ipynb index 37a3a6b..c497a6d 100644 --- a/figures.ipynb +++ b/figures.ipynb @@ -117,7 +117,7 @@ " ip, _ = phase_diagram_setup(fname)\n", " zs = ip(xy)\n", " return np.round(zs)\n", - " \n", + "\n", "\n", "def density(x, eps=0):\n", " e = [0.8, 0.2]\n", @@ -225,7 +225,6 @@ "metadata": {}, "outputs": [], "source": [ - "\n", "fig, axs = plt.subplots(2, len(funcs_1D), figsize=(fig_width, 1.5 * fig_height))\n", "n_points = 50\n", "for i, ax in enumerate(axs.T.flatten()):\n", @@ -243,9 +242,7 @@ " ax.set_title(rf\"\\textrm{{{d['title']}}}\")\n", " elif kind == \"adaptive\":\n", " loss = adaptive.learner.learner1D.curvature_loss_function()\n", - " learner = adaptive.Learner1D(\n", - " f, bounds=bounds, loss_per_interval=loss\n", - " )\n", + " learner = adaptive.Learner1D(f, bounds=bounds, loss_per_interval=loss)\n", " adaptive.runner.simple(learner, goal=lambda l: l.npoints >= n_points)\n", " xs, ys = zip(*sorted(learner.data.items()))\n", " xs_dense = np.linspace(*bounds, 1000)\n", @@ -338,7 +335,7 @@ "metadata": {}, "outputs": [], "source": [ - "learner = adaptive.Learner1D(funcs_1D[0]['function'], bounds=funcs_1D[0]['bounds'])\n", + "learner = adaptive.Learner1D(funcs_1D[0][\"function\"], bounds=funcs_1D[0][\"bounds\"])\n", "times = []\n", "for i in range(10000):\n", " t_start = time.time()\n", @@ -500,7 +497,7 @@ "# ys_hom = f(xs_hom) - (i + 3) * step\n", "# axs.plot(xs_hom, ys_hom, zorder=1, c=\"k\")\n", "# axs.scatter(xs_hom, ys_hom, alpha=1, zorder=2, c=\"k\")\n", - " \n", + "\n", "axs.axis(\"off\")\n", "plt.savefig(\"figures/algo.pdf\", bbox_inches=\"tight\", transparent=True)\n", "plt.show()" @@ -522,10 +519,11 @@ "from matplotlib.patches import Polygon\n", "\n", "fig, axs = plt.subplots(5, 1, figsize=(fig_width, 1.5 * fig_height))\n", - "f = lambda x: np.sin(x)**2\n", - "xs = np.array([0, 1.3, 3, 5, 7, 8])\n", + "f = lambda x: np.sin(x) ** 2\n", + "xs = np.array([0, 1.3, 3, 5, 7, 8])\n", "ys = f(xs)\n", "\n", + "\n", "def plot(xs, ax):\n", " ys = f(xs)\n", " xs_dense = np.linspace(xs[0], xs[-1], 300)\n", @@ -534,9 +532,10 @@ " ax.plot(xs, ys, c=\"k\")\n", " ax.scatter(xs, ys, zorder=10, s=14, c=\"k\")\n", "\n", + "\n", "plot(xs, axs[0])\n", "plot(xs, axs[1])\n", - " \n", + "\n", "for i, ax in enumerate(axs):\n", " ax.axis(\"off\")\n", " ax.set_ylim(-1.5, 1.5)\n", @@ -565,6 +564,7 @@ " [xs[i - 1], xs[i + 1]], [ys[i - 1], ys[i + 1]], c=\"k\", ls=\"--\", alpha=0.3\n", " )\n", "\n", + "\n", "plot_tri(xs, axs[1])\n", "\n", "for i in [2, 3, 4]:\n", @@ -621,25 +621,28 @@ " err_learner = err_1D(xs, ys, xs_rand, learner.function)\n", " return err_lin, err_learner\n", "\n", + "\n", "def err_2D(zs, zs_rand):\n", " abserr = np.abs(zs - zs_rand)\n", " return np.average(abserr ** 2) ** 0.5\n", "\n", + "\n", "def get_err_2D(learner, N):\n", " xys_rand = np.vstack(\n", " [\n", - " np.random.uniform(*learner.bounds[0], size=int(100_000**0.5)),\n", - " np.random.uniform(*learner.bounds[1], size=int(100_000**0.5)),\n", + " np.random.uniform(*learner.bounds[0], size=int(100_000 ** 0.5)),\n", + " np.random.uniform(*learner.bounds[1], size=int(100_000 ** 0.5)),\n", " ]\n", " )\n", "\n", " xys_hom = np.array(\n", " [\n", " (x, y)\n", - " for x in np.linspace(*learner.bounds[0], int(N**0.5))\n", - " for y in np.linspace(*learner.bounds[1], int(N**0.5))\n", + " for x in np.linspace(*learner.bounds[0], int(N ** 0.5))\n", + " for y in np.linspace(*learner.bounds[1], int(N ** 0.5))\n", " ]\n", " )\n", + " N = len(xys_hom)\n", "\n", " try:\n", " # Vectorized\n", @@ -654,12 +657,13 @@ " zs = ip(xys_rand.T)\n", " err_lin = err_2D(zs, zs_rand)\n", "\n", - " ip = interpolate.LinearNDInterpolator(learner.points[:len(xys_hom)], learner.values[:len(xys_hom)])\n", + " ip = interpolate.LinearNDInterpolator(learner.points[:N], learner.values[:N])\n", " zs = ip(xys_rand.T)\n", " err_learner = err_2D(zs, zs_rand)\n", "\n", " return err_lin, err_learner\n", "\n", + "\n", "N_max = 10000\n", "Ns = np.geomspace(4, N_max, 20).astype(int)\n", "\n", @@ -831,6 +835,7 @@ " adaptive.runner.simple(l, goal=lambda l: l.npoints > 1000)\n", " return l.plot_isosurface(level=level)\n", "\n", + "\n", "interact_manual(iso, level=(-6, 9, 0.1), unit_cell=lattices.keys())" ] }, @@ -844,8 +849,10 @@ " x, y = xy\n", " return x ** 2 + y ** 3\n", "\n", + "\n", "bounds = [(-1, 1), (-1, 1)]\n", - "npoints = 17**2\n", + "npoints = 17 ** 2\n", + "\n", "\n", "def isoline_loss_function(y_iso, sigma, priority=1):\n", " from adaptive.learner.learnerND import default_loss\n", @@ -855,7 +862,9 @@ "\n", " def loss(simplex, values, value_scale):\n", " distance = np.mean([abs(y_iso * value_scale - y) for y in values])\n", - " return priority * gaussian(distance, 0, sigma) + default_loss(simplex, values, value_scale)\n", + " return priority * gaussian(distance, 0, sigma) + default_loss(\n", + " simplex, values, value_scale\n", + " )\n", "\n", " return loss\n", "\n", @@ -883,7 +892,7 @@ " kind = \"homogeneous\" if i == 0 else \"adaptive\"\n", "\n", " if kind == \"homogeneous\":\n", - " xs, ys = [np.linspace(*bound, int(npoints**0.5)) for bound in bounds]\n", + " xs, ys = [np.linspace(*bound, int(npoints ** 0.5)) for bound in bounds]\n", " data = {xy: f(xy) for xy in itertools.product(xs, ys)}\n", " learner.data = data\n", " elif kind == \"adaptive\":\n", @@ -893,17 +902,16 @@ " adaptive.runner.simple(learner, goal=lambda l: l.npoints >= npoints)\n", "\n", " if with_tri:\n", - " xy = np.array([learner.tri.get_vertices(s)\n", - " for s in learner.tri.simplices])\n", - " print(f'{len(xy)} triangles')\n", + " xy = np.array([learner.tri.get_vertices(s) for s in learner.tri.simplices])\n", + " print(f\"{len(xy)} triangles\")\n", " triang = mtri.Triangulation(*xy.reshape(-1, 2).T)\n", " ax.triplot(triang, c=\"w\", lw=0.2, alpha=0.8)\n", "\n", " # Isolines\n", - " vertices, lines = learner._get_iso(level, which='line')\n", + " vertices, lines = learner._get_iso(level, which=\"line\")\n", " paths = np.array([[vertices[i], vertices[j]] for i, j in lines]).T\n", - " print('{} line segments'.format(len(paths.T)))\n", - " ax.plot(*paths, c='k')\n", + " print(\"{} line segments\".format(len(paths.T)))\n", + " ax.plot(*paths, c=\"k\")\n", "\n", " values = np.array(list(learner.data.values()))\n", " ax.imshow(\n", @@ -918,13 +926,6 @@ "axs[1].set_ylabel(r\"$\\textrm{adaptive}$\")\n", "plt.savefig(\"figures/isoline.pdf\", bbox_inches=\"tight\", transparent=True)" ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] } ], "metadata": { -- GitLab