From 4d1a52ebf59b4221d77f9cec4be09eedde24ed5f Mon Sep 17 00:00:00 2001 From: Dimitri Yatsenko Date: Tue, 17 Oct 2023 09:40:04 -0400 Subject: [PATCH 1/2] add JuliaSets --- short_tutorials/JuliaSets.ipynb | 238 ++++++++++++++++++++++++++++++++ 1 file changed, 238 insertions(+) create mode 100644 short_tutorials/JuliaSets.ipynb diff --git a/short_tutorials/JuliaSets.ipynb b/short_tutorials/JuliaSets.ipynb new file mode 100644 index 0000000..53199e4 --- /dev/null +++ b/short_tutorials/JuliaSets.ipynb @@ -0,0 +1,238 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "8607eb0e", + "metadata": {}, + "source": [ + "# Julia Sets" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "87bd1a0f", + "metadata": {}, + "outputs": [], + "source": [ + "%matplotlib inline\n", + "from matplotlib import pyplot as plt\n", + "import numpy as np" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "3f5a75af", + "metadata": {}, + "outputs": [], + "source": [ + "def julia(c, size=256, center=(0.0, 0.0), zoom=1.0, iters=256):\n", + " x, y = np.meshgrid(\n", + " np.linspace(-1, 1, size)/zoom + center[0], \n", + " np.linspace(-1, 1, size)/zoom + center[1], \n", + " )\n", + " z = x + 1j * y\n", + " im = np.zeros(z.shape)\n", + " ix = np.ones(z.shape, dtype=bool)\n", + " for i in range(iters):\n", + " z[ix] = z[ix] ** 2 + c\n", + " ix = np.abs(z) < 2\n", + " im += ix\n", + " return im" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "79556895", + "metadata": {}, + "outputs": [], + "source": [ + "plt.imshow(julia(-0.4+0.6j), cmap='magma')\n", + "plt.axis(False);" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "a43baa42", + "metadata": {}, + "outputs": [], + "source": [ + "plt.imshow(julia(-0.4+0.6j, center=(0.34, -0.30), zoom=10000.0), cmap='magma')\n", + "plt.axis(False);" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "3c63ce73", + "metadata": {}, + "outputs": [], + "source": [ + "c = (\n", + " -0.4 + 0.6j, \n", + " -0.74543 + 0.11301j, \n", + " -0.75 + 0.11j, \n", + " -0.1 + 0.651j,\n", + " -0.835 - 0.2321j,\n", + " -0.70176 - 0.3842j,\n", + ")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "cfc85940", + "metadata": {}, + "outputs": [], + "source": [ + "noise_level = 5.0\n", + "\n", + "fig, ax = plt.subplots(3, 2, figsize=(10, 16))\n", + "for c_, a in zip(c, ax.flatten()):\n", + " img = julia(c_, zoom=0.5) \n", + " img += np.random.randn(*img.shape) * noise_level\n", + " a.imshow(img, cmap='magma')\n", + " a.axis(False)" + ] + }, + { + "cell_type": "markdown", + "id": "b01e70d9", + "metadata": {}, + "source": [ + "# Image processing" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "b4069a34", + "metadata": {}, + "outputs": [], + "source": [ + "from skimage import data\n", + "from skimage import filters" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "39bc25c7", + "metadata": {}, + "outputs": [], + "source": [ + "from skimage.morphology import disk\n", + "from skimage import restoration" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "aa225a15", + "metadata": {}, + "outputs": [], + "source": [ + "noise_level = 50.0\n", + "img = julia(-0.4+0.6j, size=200)\n", + "noise_img = img + np.random.randn(*img.shape) * noise_level\n", + "median_img = filters.median(noise_img, disk(3))\n", + "tv_img = restoration.denoise_tv_chambolle(noise_img, weight=20.0)\n", + "wavelet_img = restoration.denoise_wavelet(noise_img)\n", + "gaussian_img = filters.gaussian(noise_img, sigma=1.8)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "d60ecafd", + "metadata": { + "scrolled": false + }, + "outputs": [], + "source": [ + "fig, ax = plt.subplots(3, 2, figsize=(12, 18))\n", + "for a, (im, title) in zip(\n", + " ax.flatten(),\n", + " ((img, 'original'), \n", + " (noise_img, 'original+noise'),\n", + " (gaussian_img, 'gaussian'),\n", + " (median_img, 'median'), \n", + " (wavelet_img, 'wavelet'),\n", + " (tv_img, 'tv'), )):\n", + " a.imshow(im, cmap='magma', vmin=0, vmax=255)\n", + " a.axis(False)\n", + " a.set_title(title)" + ] + }, + { + "cell_type": "markdown", + "id": "b495345a", + "metadata": {}, + "source": [ + "# DataJoint Pipeline" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "9015c43e", + "metadata": {}, + "outputs": [], + "source": [ + "import datajoint as dj" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "62067735", + "metadata": {}, + "outputs": [], + "source": [ + "schema = dj.Schema('julia')" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "4c4ef4a9", + "metadata": {}, + "outputs": [], + "source": [ + "img.max()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "5630641b", + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "benv", + "language": "python", + "name": "benv" + }, + "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.10.4" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} From 02b70e5e0fd1363b6162e81be7ae929af68279c5 Mon Sep 17 00:00:00 2001 From: Dimitri Yatsenko Date: Tue, 17 Oct 2023 14:48:12 +0000 Subject: [PATCH 2/2] add JuliaSet notebook --- short_tutorials/JuliaSets.ipynb | 214 ++++++++++++++++++++++++++++++-- 1 file changed, 202 insertions(+), 12 deletions(-) diff --git a/short_tutorials/JuliaSets.ipynb b/short_tutorials/JuliaSets.ipynb index 53199e4..5ee5e5f 100644 --- a/short_tutorials/JuliaSets.ipynb +++ b/short_tutorials/JuliaSets.ipynb @@ -5,7 +5,9 @@ "id": "8607eb0e", "metadata": {}, "source": [ - "# Julia Sets" + "# Julia Sets\n", + "\n", + "The following is a quick introduction to Julia Sets" ] }, { @@ -17,7 +19,8 @@ "source": [ "%matplotlib inline\n", "from matplotlib import pyplot as plt\n", - "import numpy as np" + "import numpy as np\n", + "import datajoint as dj" ] }, { @@ -90,7 +93,7 @@ "source": [ "noise_level = 5.0\n", "\n", - "fig, ax = plt.subplots(3, 2, figsize=(10, 16))\n", + "fig, ax = plt.subplots(3, 2, figsize=(7.5, 12))\n", "for c_, a in zip(c, ax.flatten()):\n", " img = julia(c_, zoom=0.5) \n", " img += np.random.randn(*img.shape) * noise_level\n", @@ -153,7 +156,7 @@ }, "outputs": [], "source": [ - "fig, ax = plt.subplots(3, 2, figsize=(12, 18))\n", + "fig, ax = plt.subplots(3, 2, figsize=(6, 9))\n", "for a, (im, title) in zip(\n", " ax.flatten(),\n", " ((img, 'original'), \n", @@ -172,7 +175,9 @@ "id": "b495345a", "metadata": {}, "source": [ - "# DataJoint Pipeline" + "# DataJoint Pipeline\n", + "\n", + "Now let's build a data pipeline managing Julia sets and their analysis" ] }, { @@ -182,9 +187,19 @@ "metadata": {}, "outputs": [], "source": [ - "import datajoint as dj" + "import datajoint as dj\n", + "\n", + "schema = dj.Schema('julia')" ] }, + { + "cell_type": "code", + "execution_count": null, + "id": "edbcdf64", + "metadata": {}, + "outputs": [], + "source": [] + }, { "cell_type": "code", "execution_count": null, @@ -192,17 +207,45 @@ "metadata": {}, "outputs": [], "source": [ - "schema = dj.Schema('julia')" + "@schema \n", + "class JuliaSpec(dj.Lookup):\n", + " definition = \"\"\"\n", + " julia_spec : smallint \n", + " ---\n", + " creal : float\n", + " cimag : float\n", + " size=256 : smallint\n", + " center_real=0.0 : float \n", + " center_imag=0.0 : float\n", + " zoom=1.0 : float\n", + " noise_level=50 : float\n", + " \"\"\" \n", + "\n", + " contents = (\n", + " dict(julia_spec=0, creal=-0.4, cimag=0.6, noise_level=50),\n", + " dict(julia_spec=1, creal=-0.7453, cimag=0.11301, noise_level=50),\n", + " dict(julia_spec=2, creal=-0.75, cimag=0.11, noise_level=50),\n", + " dict(julia_spec=3, creal=-0.1, cimag=0.651, noise_level=50),\n", + " dict(julia_spec=4, creal=-0.835, cimag=-0.2321, noise_level=50),\n", + " dict(julia_spec=5, creal=-0.70176, cimag=-0.3842, noise_level=50),\n", + " )\n", + "\n" ] }, { "cell_type": "code", "execution_count": null, - "id": "4c4ef4a9", + "id": "0bc39f02", "metadata": {}, "outputs": [], "source": [ - "img.max()" + "JuliaSpec.insert1(\n", + " dict(julia_spec=10, \n", + " creal=-0.4, cimag=0.6, \n", + " center_real=0.34, center_imag=-0.30, \n", + " zoom=10000.0, \n", + " noise_level=50.0)\n", + ") " ] }, { @@ -211,14 +254,161 @@ "id": "5630641b", "metadata": {}, "outputs": [], + "source": [ + "@schema\n", + "class JuliaImage(dj.Computed):\n", + " definition = \"\"\"\n", + " -> JuliaSpec \n", + " ---\n", + " image : longblob\n", + " \"\"\"\n", + "\n", + " def make(self, key):\n", + " spec = (JuliaSpec & key).fetch1()\n", + " img = julia(spec['creal'] + 1j*spec['cimag'], \n", + " size=spec['size'],\n", + " center=(spec['center_real'], spec['center_imag']),\n", + " zoom=spec['zoom'],\n", + " )\n", + " img += np.random.randn(*img.shape) * spec['noise_level']\n", + " self.insert1(dict(key, image=img.astype(np.float32)))" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "1c8e3481", + "metadata": {}, + "outputs": [], + "source": [ + "JuliaImage.populate(display_progress=True)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "560613ef", + "metadata": {}, + "outputs": [], + "source": [ + "JuliaImage()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "76a38851", + "metadata": {}, + "outputs": [], + "source": [ + "plt.imshow((JuliaImage & 'julia_spec=2').fetch1('image'))\n", + "plt.axis(False);" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "b108650f", + "metadata": {}, + "outputs": [], + "source": [ + "@schema\n", + "class DenoiseMethod(dj.Lookup):\n", + " definition = \"\"\"\n", + " denoise_method : smallint\n", + " ---\n", + " method : varchar(16)\n", + " params=null : blob\n", + " \"\"\"\n", + " contents = (\n", + " (0, 'gaussian', dict(sigma=1.8)),\n", + " (1, 'median', dict(radius=3)),\n", + " (2, 'wavelet', {}),\n", + " (3, 'tv', dict(weight=20.0))\n", + " )\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "9314c7f5", + "metadata": {}, + "outputs": [], + "source": [ + "@schema \n", + "class JuliaDenoised(dj.Computed):\n", + " definition = \"\"\"\n", + " -> JuliaImage\n", + " -> DenoiseMethod\n", + " ---\n", + " denoised_image : longblob\n", + " \"\"\"\n", + "\n", + " def make(self, key):\n", + " img = (JuliaImage & key).fetch1('image')\n", + " method, params = (DenoiseMethod & key).fetch1('method', 'params')\n", + "\n", + " if method == \"gaussian\":\n", + " result = filters.gaussian(img, **params)\n", + " elif method == \"median\":\n", + " result = filters.median(img, disk(params['radius']))\n", + " elif method == 'tv':\n", + " result = restoration.denoise_tv_chambolle(img, **params)\n", + " elif method == \"wavelet\":\n", + " result = restoration.denoise_wavelet(noise_img, **params)\n", + " else:\n", + " raise NotImplementedError\n", + " self.insert1(dict(key, denoised_image=result))" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "834782d9", + "metadata": {}, + "outputs": [], + "source": [ + "JuliaDenoised.populate(display_progress=True)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "f3ad1348", + "metadata": {}, + "outputs": [], + "source": [ + "JuliaDenoised()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "b4b76369", + "metadata": {}, + "outputs": [], + "source": [ + "keys = JuliaDenoised.fetch('KEY')\n", + "img = ((JuliaDenoised & keys[0])).fetch1('denoised_image')\n", + "plt.imshow(img)\n", + "plt.axis(False);" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "97601fad", + "metadata": {}, + "outputs": [], "source": [] } ], "metadata": { "kernelspec": { - "display_name": "benv", + "display_name": "Python 3", "language": "python", - "name": "benv" + "name": "python3" }, "language_info": { "codemirror_mode": { @@ -230,7 +420,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.10.4" + "version": "3.9.17" } }, "nbformat": 4,