{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "Dask and Scikit-Image on a Cluster\n", "==================================\n", "\n", "" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from dask_gateway import Gateway\n", "\n", "gateway = Gateway()\n", "cluster = gateway.new_cluster()\n", "cluster.scale(10)\n", "cluster" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from dask.distributed import Client, progress\n", "client = Client(cluster)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%matplotlib inline\n", "from dask import delayed\n", "import skimage.io\n", "import matplotlib.pyplot as plt" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Read a single image from Google Cloud Storage" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "sample = skimage.io.imread('https://storage.googleapis.com/pangeo-data/FIB-25/iso.00000.png')\n", "\n", "fig = plt.figure(figsize=(10, 10))\n", "plt.imshow(sample, cmap='gray')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Construct a Lazy Dask Array from all of the images" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from dask import delayed\n", "import dask.array as da\n", "\n", "# filenames = ['https://storage.googleapis.com/pangeo-data/FIB-25/iso.%05d.png' % i for i in range(0, 8090)]\n", "filenames = ['https://storage.googleapis.com/pangeo-data/FIB-25/iso.%05d.png' % i for i in range(0, 1000)]\n", "images = [delayed(skimage.io.imread)(fn) for fn in filenames]\n", "arrays = [da.from_delayed(im, shape=sample.shape, dtype=sample.dtype) for im in images]\n", "x = da.stack(arrays, axis=0)\n", "x" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Visualize Lazily with Holoviews" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import holoviews as hv\n", "from holoviews.operation.datashader import regrid\n", "hv.extension('bokeh', width=100)\n", "\n", "dims = tuple([range(d) for d in x.shape][::-1])\n", "hv_ds = hv.Dataset(dims + (x,), ['x', 'y', 'sample'], 'z', datatype=['xarray'])\n", "im = hv_ds.to(hv.Image, ['x', 'y'], dynamic=True)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%%opts Image [width=800 height=800] (cmap='magma') \n", "regrid(im, precompute=True)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Rearrange data by Pixel\n", "\n", "Currently our data is organized in chunks of size `1x6000x6000`, one chunk for each image. If we want to do analysis per-pixel then we need to rechunk our data, which we do below into chunks of size `1000x100x100`" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "x = x.rechunk((1000, 100, 100))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Ask Dask to persist that data in memory\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "x = x.persist()\n", "progress(x)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Do per-pixel computations, like FFTs" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "y = da.fft.fft(x, axis=0)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%matplotlib inline" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import matplotlib.pyplot as plt\n", "\n", "plt.figure(figsize=(20, 5))\n", "plt.plot(y[:, 3000, 3000])\n", "plt.show()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "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.7.6" } }, "nbformat": 4, "nbformat_minor": 2 }