{ "cells": [ { "cell_type": "markdown", "id": "ad36b0ca", "metadata": {}, "source": [ "# Relabeling a label image\n", "See also [discussion on image.sc](https://forum.image.sc/t/making-np-where-faster/58641/2)." ] }, { "cell_type": "code", "execution_count": 1, "id": "e3a91561", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import numpy as np\n", "import numpy\n", "import cupy as cp\n", "import cupy\n", "import pyclesperanto_prototype as cle\n", "import timeit\n", "cle.select_device(\"RTX\")" ] }, { "cell_type": "code", "execution_count": 2, "id": "1060e301", "metadata": {}, "outputs": [], "source": [ "sliceimage = (np.random.random((17, 2439, 2439)) * 4).astype(int)\n", "\n", "def relabel_numpy(sliceimage):\n", " return numpy.take(numpy.array([0, 3, 3, 6, 4], numpy.uint8), sliceimage)\n", "\n", "def relabel_cupy(sliceimage):\n", " return cupy.asnumpy(\n", " cupy.take(\n", " cupy.asarray([0, 3, 3, 6, 4]), \n", " cupy.asarray(sliceimage)\n", " )\n", " )\n", "\n", "def relabel_cle(sliceimage):\n", " return cle.replace_intensities(sliceimage, np.asarray([0, 3, 3, 6, 4]))" ] }, { "cell_type": "code", "execution_count": 3, "id": "03a7d70d", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "292 ms ± 24.8 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)\n" ] } ], "source": [ "%timeit relabel_cupy(sliceimage)" ] }, { "cell_type": "code", "execution_count": 4, "id": "eaaad577", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "793 ms ± 12.7 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)\n" ] } ], "source": [ "%timeit relabel_numpy(sliceimage)" ] }, { "cell_type": "code", "execution_count": 5, "id": "b9b3fc04", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "264 ms ± 5.14 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)\n" ] } ], "source": [ "%timeit relabel_cle(sliceimage)" ] }, { "cell_type": "code", "execution_count": null, "id": "a1388823", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "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.8.5" } }, "nbformat": 4, "nbformat_minor": 5 }