Debian Codegraph @

doc/intro/tutorials/custom_func.ipynb

raw
1 {
2 "cells": [
3 {
4 "cell_type": "markdown",
5 "id": "76b9af91-1233-485b-a616-826a8ccfcbc8",
6 "metadata": {},
7 "source": [
8 "DataLab custom function example\n",
9 "===============================\n",
10 "\n",
11 "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",
12 "\n",
13 "The only requirement is to install the *Sigima* package (using `pip install sigima`, for example)."
14 ]
15 },
16 {
17 "cell_type": "code",
18 "execution_count": null,
19 "id": "47c889ba-ca07-46dd-bec0-b08d67bd1eb2",
20 "metadata": {},
21 "outputs": [],
22 "source": [
23 "import numpy as np\n",
24 "import scipy.ndimage as spi\n",
25 "from sigima.client import SimpleRemoteProxy\n",
26 "\n",
27 "# Define our custom processing function\n",
28 "def weighted_average_denoise(data: np.ndarray) -> np.ndarray:\n",
29 " \"\"\"Apply a custom denoising filter to an image.\n",
30 "\n",
31 " This filter averages the pixels in a 5x5 neighborhood, but gives less weight\n",
32 " to pixels that significantly differ from the central pixel.\n",
33 " \"\"\"\n",
34 "\n",
35 " def filter_func(values: np.ndarray) -> float:\n",
36 " \"\"\"Filter function\"\"\"\n",
37 " central_pixel = values[len(values) // 2]\n",
38 " differences = np.abs(values - central_pixel)\n",
39 " weights = np.exp(-differences / np.mean(differences))\n",
40 " return np.average(values, weights=weights)\n",
41 "\n",
42 " return spi.generic_filter(data, filter_func, size=5)"
43 ]
44 },
45 {
46 "cell_type": "markdown",
47 "id": "721d52f6-2df8-4559-89d2-f7088a5e2ee2",
48 "metadata": {},
49 "source": [
50 "Connecting to DataLab current session:"
51 ]
52 },
53 {
54 "cell_type": "code",
55 "execution_count": 3,
56 "id": "1de8616a-619c-4d5b-869f-fb41cf72b7cf",
57 "metadata": {},
58 "outputs": [
59 {
60 "name": "stdout",
61 "output_type": "stream",
62 "text": [
63 "Connecting to DataLab XML-RPC server...OK (port: 61330)\n"
64 ]
65 }
66 ],
67 "source": [
68 "proxy = SimpleRemoteProxy()\n",
69 "proxy.connect()"
70 ]
71 },
72 {
73 "cell_type": "markdown",
74 "id": "9246a105-d73d-4ba3-81e1-4fadd042a4c6",
75 "metadata": {},
76 "source": [
77 "Switch to the \"Image Panel\" and get the current image"
78 ]
79 },
80 {
81 "cell_type": "code",
82 "execution_count": 5,
83 "id": "ccdddb07-cc54-42dd-b744-751516ae73d9",
84 "metadata": {},
85 "outputs": [],
86 "source": [
87 "proxy.set_current_panel(\"image\")\n",
88 "image = proxy.get_object()\n",
89 "if image is None:\n",
90 " # We raise an explicit error if there is no image to process\n",
91 " raise RuntimeError(\"No image to process!\")"
92 ]
93 },
94 {
95 "cell_type": "markdown",
96 "id": "0b717a9d-f92f-4269-8c47-150beeed1075",
97 "metadata": {},
98 "source": [
99 "Get a copy of the image data, apply the function to it, and add new image to the panel"
100 ]
101 },
102 {
103 "cell_type": "code",
104 "execution_count": 6,
105 "id": "7216fc12-2675-48ff-a62a-64b5516c2e09",
106 "metadata": {},
107 "outputs": [
108 {
109 "data": {
110 "text/plain": [
111 "True"
112 ]
113 },
114 "execution_count": 6,
115 "metadata": {},
116 "output_type": "execute_result"
117 }
118 ],
119 "source": [
120 "data = np.array(image.data, copy=True)\n",
121 "data = weighted_average_denoise(data)\n",
122 "proxy.add_image(\"Filtered using a Jupyter notebook\", data)"
123 ]
124 },
125 {
126 "cell_type": "code",
127 "execution_count": null,
128 "id": "0ca0385b-bb43-4c14-b4ab-2768d44f04cc",
129 "metadata": {},
130 "outputs": [],
131 "source": []
132 }
133 ],
134 "metadata": {
135 "kernelspec": {
136 "display_name": "Python 3 (ipykernel)",
137 "language": "python",
138 "name": "python3"
139 },
140 "language_info": {
141 "codemirror_mode": {
142 "name": "ipython",
143 "version": 3
144 },
145 "file_extension": ".py",
146 "mimetype": "text/x-python",
147 "name": "python",
148 "nbconvert_exporter": "python",
149 "pygments_lexer": "ipython3",
150 "version": "3.11.6"
151 }
152 },
153 "nbformat": 4,
154 "nbformat_minor": 5
155 }