{ "cells": [ { "cell_type": "markdown", "id": "76b9af91-1233-485b-a616-826a8ccfcbc8", "metadata": {}, "source": [ "DataLab custom function example\n", "===============================\n", "\n", "This is part of the DataLab's custom function tutorial which aims at illustrating the extensibility of DataLab (macros, plugins, and control from an external IDE or a Jupyter notebook). \n", "\n", "The only requirement is to install the *Sigima* package (using `pip install sigima`, for example)." ] }, { "cell_type": "code", "execution_count": null, "id": "47c889ba-ca07-46dd-bec0-b08d67bd1eb2", "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "import scipy.ndimage as spi\n", "from sigima.client import SimpleRemoteProxy\n", "\n", "# Define our custom processing function\n", "def weighted_average_denoise(data: np.ndarray) -> np.ndarray:\n", " \"\"\"Apply a custom denoising filter to an image.\n", "\n", " This filter averages the pixels in a 5x5 neighborhood, but gives less weight\n", " to pixels that significantly differ from the central pixel.\n", " \"\"\"\n", "\n", " def filter_func(values: np.ndarray) -> float:\n", " \"\"\"Filter function\"\"\"\n", " central_pixel = values[len(values) // 2]\n", " differences = np.abs(values - central_pixel)\n", " weights = np.exp(-differences / np.mean(differences))\n", " return np.average(values, weights=weights)\n", "\n", " return spi.generic_filter(data, filter_func, size=5)" ] }, { "cell_type": "markdown", "id": "721d52f6-2df8-4559-89d2-f7088a5e2ee2", "metadata": {}, "source": [ "Connecting to DataLab current session:" ] }, { "cell_type": "code", "execution_count": 3, "id": "1de8616a-619c-4d5b-869f-fb41cf72b7cf", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Connecting to DataLab XML-RPC server...OK (port: 61330)\n" ] } ], "source": [ "proxy = SimpleRemoteProxy()\n", "proxy.connect()" ] }, { "cell_type": "markdown", "id": "9246a105-d73d-4ba3-81e1-4fadd042a4c6", "metadata": {}, "source": [ "Switch to the \"Image Panel\" and get the current image" ] }, { "cell_type": "code", "execution_count": 5, "id": "ccdddb07-cc54-42dd-b744-751516ae73d9", "metadata": {}, "outputs": [], "source": [ "proxy.set_current_panel(\"image\")\n", "image = proxy.get_object()\n", "if image is None:\n", " # We raise an explicit error if there is no image to process\n", " raise RuntimeError(\"No image to process!\")" ] }, { "cell_type": "markdown", "id": "0b717a9d-f92f-4269-8c47-150beeed1075", "metadata": {}, "source": [ "Get a copy of the image data, apply the function to it, and add new image to the panel" ] }, { "cell_type": "code", "execution_count": 6, "id": "7216fc12-2675-48ff-a62a-64b5516c2e09", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data = np.array(image.data, copy=True)\n", "data = weighted_average_denoise(data)\n", "proxy.add_image(\"Filtered using a Jupyter notebook\", data)" ] }, { "cell_type": "code", "execution_count": null, "id": "0ca0385b-bb43-4c14-b4ab-2768d44f04cc", "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.11.6" } }, "nbformat": 4, "nbformat_minor": 5 }