|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "id": "906a254e-5fc2-41de-894e-91ace6a2756b", |
| 6 | + "metadata": {}, |
| 7 | + "source": [ |
| 8 | + "# Bia-bob\n", |
| 9 | + "This notebook is used to generate a python-file that serves as interoperability connector to [bia-bob](https://github.com/haesleinhuepf/bia-bob). You need an OpenAI account to run this notebook. Note: It uses chatGPT in a loop and executing this notebook costs some cents." |
| 10 | + ] |
| 11 | + }, |
| 12 | + { |
| 13 | + "cell_type": "code", |
| 14 | + "execution_count": 1, |
| 15 | + "id": "f77d1470-7f46-47e8-8ac4-5db660a52586", |
| 16 | + "metadata": {}, |
| 17 | + "outputs": [], |
| 18 | + "source": [ |
| 19 | + "import stackview\n", |
| 20 | + "import openai" |
| 21 | + ] |
| 22 | + }, |
| 23 | + { |
| 24 | + "cell_type": "code", |
| 25 | + "execution_count": 2, |
| 26 | + "id": "45bea0e2-3e26-4723-a529-1bf3340679b9", |
| 27 | + "metadata": {}, |
| 28 | + "outputs": [], |
| 29 | + "source": [ |
| 30 | + "def prompt(message:str, model=\"gpt-3.5-turbo\"):\n", |
| 31 | + " \"\"\"A prompt helper function that sends a message to openAI\n", |
| 32 | + " and returns only the text response.\n", |
| 33 | + " \"\"\"\n", |
| 34 | + " client = openai.OpenAI()\n", |
| 35 | + " response = client.chat.completions.create(\n", |
| 36 | + " model=model,\n", |
| 37 | + " messages=[{\"role\": \"user\", \"content\": message}]\n", |
| 38 | + " )\n", |
| 39 | + " return response.choices[0].message.content" |
| 40 | + ] |
| 41 | + }, |
| 42 | + { |
| 43 | + "cell_type": "code", |
| 44 | + "execution_count": 3, |
| 45 | + "id": "e24d6116-d19b-42d0-8387-b71202a364e7", |
| 46 | + "metadata": {}, |
| 47 | + "outputs": [ |
| 48 | + { |
| 49 | + "data": { |
| 50 | + "text/plain": [ |
| 51 | + "dict_keys(['annotate', 'assist', 'create_colormap', 'crop', 'curtain', 'insight', 'interact', 'jupyter_displayable_output', 'merge_rgb', 'nop', 'orthogonal', 'picker', 'side_by_side', 'slice', 'switch'])" |
| 52 | + ] |
| 53 | + }, |
| 54 | + "execution_count": 3, |
| 55 | + "metadata": {}, |
| 56 | + "output_type": "execute_result" |
| 57 | + } |
| 58 | + ], |
| 59 | + "source": [ |
| 60 | + "all = {}\n", |
| 61 | + "for n in dir(stackview):\n", |
| 62 | + " if not n.startswith('_'):\n", |
| 63 | + " all[n] = getattr(stackview, n)\n", |
| 64 | + "\n", |
| 65 | + "all.keys()" |
| 66 | + ] |
| 67 | + }, |
| 68 | + { |
| 69 | + "cell_type": "code", |
| 70 | + "execution_count": 4, |
| 71 | + "id": "6fda77e3-718f-4317-9afc-3e22958abb79", |
| 72 | + "metadata": {}, |
| 73 | + "outputs": [ |
| 74 | + { |
| 75 | + "data": { |
| 76 | + "text/plain": [ |
| 77 | + "{'annotate': <function stackview._annotate.annotate(image, labels, slice_number: int = None, alpha: float = 0.5, axis: int = 0, continuous_update: bool = True, slider_text: str = 'Slice', zoom_factor: float = 1.0, zoom_spline_order: int = 0, colormap: str = None, display_min: float = None, display_max: float = None)>,\n", |
| 78 | + " 'crop': <function stackview._crop.crop(image, slice_number: int = None, axis: int = 0, continuous_update: bool = True, slider_text: str = 'Slice', axis_names=None, zoom_factor: float = 1.0, zoom_spline_order: int = 0, colormap: str = None, display_min: float = None, display_max: float = None)>,\n", |
| 79 | + " 'curtain': <function stackview._curtain.curtain(image, image_curtain, slice_number: int = None, axis: int = 0, display_width: int = None, display_height: int = None, continuous_update: bool = True, alpha: float = 1, zoom_factor: float = 1.0, zoom_spline_order: int = 0, colormap: str = None, display_min: float = None, display_max: float = None, curtain_colormap: str = None, curtain_display_min: float = None, curtain_display_max: float = None)>,\n", |
| 80 | + " 'insight': <function stackview._static_view.insight(image, library_name=None, help_url=None)>,\n", |
| 81 | + " 'interact': <function stackview._interact.interact(func, image=None, *args, continuous_update: bool = True, context: dict = None, zoom_factor: float = 1.0, zoom_spline_order: int = 0, colormap: str = None, display_min: float = None, display_max: float = None, viewer: stackview._slice_viewer._SliceViewer = None, **kwargs)>,\n", |
| 82 | + " 'orthogonal': <function stackview._orthogonal.orthogonal(image, display_width: int = None, display_height: int = None, continuous_update: bool = True, zoom_factor: float = 1.0, zoom_spline_order: int = 0, colormap: str = None, display_min: float = None, display_max: float = None)>,\n", |
| 83 | + " 'picker': <function stackview._picker.picker(image, slice_number: int = None, display_width: int = None, display_height: int = None, continuous_update: bool = True, slider_text: str = 'Slice', zoom_factor: float = 1.0, zoom_spline_order: int = 0, colormap: str = None, display_min: float = None, display_max: float = None)>,\n", |
| 84 | + " 'side_by_side': <function stackview._side_by_side.side_by_side(image1, image2, slice_number: int = None, axis: int = 0, display_width: int = None, display_height: int = None, continuous_update: bool = True, slider_text: str = 'Slice', zoom_factor: float = 1.0, zoom_spline_order: int = 0, colormap: str = None, display_min: float = None, display_max: float = None)>,\n", |
| 85 | + " 'slice': <function stackview._slice.slice(image, slice_number: int = None, axis: int = 0, display_width: int = None, display_height: int = None, continuous_update: bool = True, slider_text: str = 'Slice', zoom_factor: float = 1.0, zoom_spline_order: int = 0, colormap: str = None, display_min: float = None, display_max: float = None)>,\n", |
| 86 | + " 'switch': <function stackview._switch.switch(images, slice_number: int = None, axis: int = 0, display_width: int = None, display_height: int = None, continuous_update: bool = True, slider_text: str = 'Slice', zoom_factor: float = 1.0, zoom_spline_order: int = 0, colormap: str = None, display_min: float = None, display_max: float = None, toggleable: bool = False)>}" |
| 87 | + ] |
| 88 | + }, |
| 89 | + "execution_count": 4, |
| 90 | + "metadata": {}, |
| 91 | + "output_type": "execute_result" |
| 92 | + } |
| 93 | + ], |
| 94 | + "source": [ |
| 95 | + "all.pop('jupyter_displayable_output')\n", |
| 96 | + "all.pop('create_colormap')\n", |
| 97 | + "all.pop('merge_rgb')\n", |
| 98 | + "all.pop('nop')\n", |
| 99 | + "all.pop('assist')\n", |
| 100 | + "\n", |
| 101 | + "all" |
| 102 | + ] |
| 103 | + }, |
| 104 | + { |
| 105 | + "cell_type": "code", |
| 106 | + "execution_count": 5, |
| 107 | + "id": "dc8af4de-826b-4f2c-8630-f6ad96665ffc", |
| 108 | + "metadata": {}, |
| 109 | + "outputs": [ |
| 110 | + { |
| 111 | + "name": "stdout", |
| 112 | + "output_type": "stream", |
| 113 | + "text": [ |
| 114 | + "Displays an image with a slider and label showing mouse position and intensity.\n", |
| 115 | + "stackview.annotate(image, labels, slice_number: int = None, alpha: float = 0.5, axis: int = 0, continuous_update: bool = True, slider_text: str = 'Slice', zoom_factor: float = 1.0, zoom_spline_order: int = 0, colormap: str = None, display_min: float = None, display_max: float = None)\n", |
| 116 | + "\n", |
| 117 | + "\n", |
| 118 | + "Allows cropping an image along all axes.\n", |
| 119 | + "stackview.crop(image, slice_number: int = None, axis: int = 0, continuous_update: bool = True, slider_text: str = 'Slice', axis_names=None, zoom_factor: float = 1.0, zoom_spline_order: int = 0, colormap: str = None, display_min: float = None, display_max: float = None)\n", |
| 120 | + "\n", |
| 121 | + "\n", |
| 122 | + "- Display two images and use a slider to switch between them.\n", |
| 123 | + "stackview.curtain(image, image_curtain, slice_number: int = None, axis: int = 0, display_width: int = None, display_height: int = None, continuous_update: bool = True, alpha: float = 1, zoom_factor: float = 1.0, zoom_spline_order: int = 0, colormap: str = None, display_min: float = None, display_max: float = None, curtain_colormap: str = None, curtain_display_min: float = None, curtain_display_max: float = None)\n", |
| 124 | + "\n", |
| 125 | + "\n", |
| 126 | + "- Convert numpy-array-like image to numpy-compatible array with convenient display in Jupyter notebooks\n", |
| 127 | + "stackview.insight(image, library_name=None, help_url=None)\n", |
| 128 | + "\n", |
| 129 | + "\n", |
| 130 | + "Build a user interface with sliders for numeric and \"footprint\" or \"selem\" parameters\n", |
| 131 | + "stackview.interact(func, image=None, *args, continuous_update: bool = True, context: dict = None, zoom_factor: float = 1.0, zoom_spline_order: int = 0, colormap: str = None, display_min: float = None, display_max: float = None, viewer: stackview._slice_viewer._SliceViewer = None, **kwargs)\n", |
| 132 | + "\n", |
| 133 | + "\n", |
| 134 | + "- Display an image and allows slicing in three dimensions (Z, Y, and X) with optional zooming and colormap settings.\n", |
| 135 | + "stackview.orthogonal(image, display_width: int = None, display_height: int = None, continuous_update: bool = True, zoom_factor: float = 1.0, zoom_spline_order: int = 0, colormap: str = None, display_min: float = None, display_max: float = None)\n", |
| 136 | + "\n", |
| 137 | + "\n", |
| 138 | + "* Display an image with a slider and label showing the mouse position and intensity.\n", |
| 139 | + "stackview.picker(image, slice_number: int = None, display_width: int = None, display_height: int = None, continuous_update: bool = True, slider_text: str = 'Slice', zoom_factor: float = 1.0, zoom_spline_order: int = 0, colormap: str = None, display_min: float = None, display_max: float = None)\n", |
| 140 | + "\n", |
| 141 | + "\n", |
| 142 | + "* Display two images side by side, with an additional overlay view of their colocalization, and a slider to navigate through a stack.\n", |
| 143 | + "stackview.side_by_side(image1, image2, slice_number: int = None, axis: int = 0, display_width: int = None, display_height: int = None, continuous_update: bool = True, slider_text: str = 'Slice', zoom_factor: float = 1.0, zoom_spline_order: int = 0, colormap: str = None, display_min: float = None, display_max: float = None)\n", |
| 144 | + "\n", |
| 145 | + "\n", |
| 146 | + "* Display an image with a slider to navigate through a stack.\n", |
| 147 | + "stackview.slice(image, slice_number: int = None, axis: int = 0, display_width: int = None, display_height: int = None, continuous_update: bool = True, slider_text: str = 'Slice', zoom_factor: float = 1.0, zoom_spline_order: int = 0, colormap: str = None, display_min: float = None, display_max: float = None)\n", |
| 148 | + "\n", |
| 149 | + "\n", |
| 150 | + "- Allows switching between multiple images and displaying them with a slider.\n", |
| 151 | + "stackview.switch(images, slice_number: int = None, axis: int = 0, display_width: int = None, display_height: int = None, continuous_update: bool = True, slider_text: str = 'Slice', zoom_factor: float = 1.0, zoom_spline_order: int = 0, colormap: str = None, display_min: float = None, display_max: float = None, toggleable: bool = False)\n", |
| 152 | + "\n", |
| 153 | + "\n", |
| 154 | + "[\"Displays an image with a slider and label showing mouse position and intensity.\\nstackview.annotate(image, labels, slice_number: int = None, alpha: float = 0.5, axis: int = 0, continuous_update: bool = True, slider_text: str = 'Slice', zoom_factor: float = 1.0, zoom_spline_order: int = 0, colormap: str = None, display_min: float = None, display_max: float = None)\\n\\n\", \"Allows cropping an image along all axes.\\nstackview.crop(image, slice_number: int = None, axis: int = 0, continuous_update: bool = True, slider_text: str = 'Slice', axis_names=None, zoom_factor: float = 1.0, zoom_spline_order: int = 0, colormap: str = None, display_min: float = None, display_max: float = None)\\n\\n\", '- Display two images and use a slider to switch between them.\\nstackview.curtain(image, image_curtain, slice_number: int = None, axis: int = 0, display_width: int = None, display_height: int = None, continuous_update: bool = True, alpha: float = 1, zoom_factor: float = 1.0, zoom_spline_order: int = 0, colormap: str = None, display_min: float = None, display_max: float = None, curtain_colormap: str = None, curtain_display_min: float = None, curtain_display_max: float = None)\\n\\n', '- Convert numpy-array-like image to numpy-compatible array with convenient display in Jupyter notebooks\\nstackview.insight(image, library_name=None, help_url=None)\\n\\n', 'Build a user interface with sliders for numeric and \"footprint\" or \"selem\" parameters\\nstackview.interact(func, image=None, *args, continuous_update: bool = True, context: dict = None, zoom_factor: float = 1.0, zoom_spline_order: int = 0, colormap: str = None, display_min: float = None, display_max: float = None, viewer: stackview._slice_viewer._SliceViewer = None, **kwargs)\\n\\n', '- Display an image and allows slicing in three dimensions (Z, Y, and X) with optional zooming and colormap settings.\\nstackview.orthogonal(image, display_width: int = None, display_height: int = None, continuous_update: bool = True, zoom_factor: float = 1.0, zoom_spline_order: int = 0, colormap: str = None, display_min: float = None, display_max: float = None)\\n\\n', \"* Display an image with a slider and label showing the mouse position and intensity.\\nstackview.picker(image, slice_number: int = None, display_width: int = None, display_height: int = None, continuous_update: bool = True, slider_text: str = 'Slice', zoom_factor: float = 1.0, zoom_spline_order: int = 0, colormap: str = None, display_min: float = None, display_max: float = None)\\n\\n\", \"* Display two images side by side, with an additional overlay view of their colocalization, and a slider to navigate through a stack.\\nstackview.side_by_side(image1, image2, slice_number: int = None, axis: int = 0, display_width: int = None, display_height: int = None, continuous_update: bool = True, slider_text: str = 'Slice', zoom_factor: float = 1.0, zoom_spline_order: int = 0, colormap: str = None, display_min: float = None, display_max: float = None)\\n\\n\", \"* Display an image with a slider to navigate through a stack.\\nstackview.slice(image, slice_number: int = None, axis: int = 0, display_width: int = None, display_height: int = None, continuous_update: bool = True, slider_text: str = 'Slice', zoom_factor: float = 1.0, zoom_spline_order: int = 0, colormap: str = None, display_min: float = None, display_max: float = None)\\n\\n\", \"- Allows switching between multiple images and displaying them with a slider.\\nstackview.switch(images, slice_number: int = None, axis: int = 0, display_width: int = None, display_height: int = None, continuous_update: bool = True, slider_text: str = 'Slice', zoom_factor: float = 1.0, zoom_spline_order: int = 0, colormap: str = None, display_min: float = None, display_max: float = None, toggleable: bool = False)\\n\\n\"]\n" |
| 155 | + ] |
| 156 | + } |
| 157 | + ], |
| 158 | + "source": [ |
| 159 | + "def get_function_signature(func):\n", |
| 160 | + " import inspect\n", |
| 161 | + " \n", |
| 162 | + " function_object = func\n", |
| 163 | + " signature_object = inspect.signature(function_object)\n", |
| 164 | + " signature_string = \"stackview.\" + func.__name__ + str(signature_object)\n", |
| 165 | + " return signature_string\n", |
| 166 | + "\n", |
| 167 | + "all_descriptions = []\n", |
| 168 | + "for name, func in all.items():\n", |
| 169 | + " \n", |
| 170 | + " description = prompt(f\"\"\"Shorten the following docstring into a single bullet point:\n", |
| 171 | + "\n", |
| 172 | + " ```\n", |
| 173 | + " {func.__doc__}\n", |
| 174 | + " ```\n", |
| 175 | + " \n", |
| 176 | + " Shorten it to a single bullet point explaining what the function does without mentioning the function name and without phrases such as `use this function`. Do not mention and explain all parameters. Keep it short.\n", |
| 177 | + "\n", |
| 178 | + " Examples:\n", |
| 179 | + "\n", |
| 180 | + " * compute the absolute value of every individual pixel in an image\n", |
| 181 | + " cle.absolute(source: ndarray, destination: ndarray = None) -> ndarray\n", |
| 182 | + "\n", |
| 183 | + " * determine the absolute difference pixel by pixel between two images\n", |
| 184 | + " cle.absolute_difference(source1: ndarray, source2: ndarray, destination: ndarray = None) -> ndarray\n", |
| 185 | + " \n", |
| 186 | + " * add a scalar value to all pixels of an image\n", |
| 187 | + " cle.add_image_and_scalar(source: ndarray, destination: ndarray = None, scalar: float = 1) -> ndarray\n", |
| 188 | + " \n", |
| 189 | + " \"\"\").replace(\"If you want to \", \"* \")\n", |
| 190 | + " description = description + \"\\n\" + get_function_signature(func) + \"\\n\\n\"\n", |
| 191 | + " print(description)\n", |
| 192 | + " \n", |
| 193 | + " all_descriptions.append(description)\n", |
| 194 | + "\n", |
| 195 | + "print(all_descriptions[:1000])" |
| 196 | + ] |
| 197 | + }, |
| 198 | + { |
| 199 | + "cell_type": "code", |
| 200 | + "execution_count": 6, |
| 201 | + "id": "754b2914-3556-4f2b-8361-5bcd8f3286bf", |
| 202 | + "metadata": {}, |
| 203 | + "outputs": [], |
| 204 | + "source": [ |
| 205 | + "descriptions = []\n", |
| 206 | + "for bullet_point in all_descriptions:\n", |
| 207 | + " if bullet_point[0:2] in ['- ', '* ']:\n", |
| 208 | + " bullet_point = bullet_point[2:]\n", |
| 209 | + "\n", |
| 210 | + " descriptions.append(\"* \" + bullet_point)\n" |
| 211 | + ] |
| 212 | + }, |
| 213 | + { |
| 214 | + "cell_type": "code", |
| 215 | + "execution_count": 7, |
| 216 | + "id": "99adee01-0847-43e8-9ef3-df511805f7cd", |
| 217 | + "metadata": {}, |
| 218 | + "outputs": [], |
| 219 | + "source": [ |
| 220 | + "text = \"\".join(descriptions).replace('\"', \"'\")\n", |
| 221 | + "text = \" \" + text.replace(\"\\n\", \"\\n \")\n", |
| 222 | + "text = f'''\n", |
| 223 | + "def list_bia_bob_plugins():\n", |
| 224 | + " \"\"\"List of function hints for bia_bob\"\"\"\n", |
| 225 | + " return \"\"\"{text}\"\"\"\n", |
| 226 | + "'''\n", |
| 227 | + "\n", |
| 228 | + "with open(\"../stackview/_bia_bob_plugins.py\", 'w') as file:\n", |
| 229 | + " file.write(text)" |
| 230 | + ] |
| 231 | + }, |
| 232 | + { |
| 233 | + "cell_type": "code", |
| 234 | + "execution_count": null, |
| 235 | + "id": "003174bb-9c47-48f7-8e2b-361f7d7a4359", |
| 236 | + "metadata": {}, |
| 237 | + "outputs": [], |
| 238 | + "source": [] |
| 239 | + } |
| 240 | + ], |
| 241 | + "metadata": { |
| 242 | + "kernelspec": { |
| 243 | + "display_name": "Python 3 (ipykernel)", |
| 244 | + "language": "python", |
| 245 | + "name": "python3" |
| 246 | + }, |
| 247 | + "language_info": { |
| 248 | + "codemirror_mode": { |
| 249 | + "name": "ipython", |
| 250 | + "version": 3 |
| 251 | + }, |
| 252 | + "file_extension": ".py", |
| 253 | + "mimetype": "text/x-python", |
| 254 | + "name": "python", |
| 255 | + "nbconvert_exporter": "python", |
| 256 | + "pygments_lexer": "ipython3", |
| 257 | + "version": "3.10.13" |
| 258 | + } |
| 259 | + }, |
| 260 | + "nbformat": 4, |
| 261 | + "nbformat_minor": 5 |
| 262 | +} |
0 commit comments