{ "cells": [ { "cell_type": "markdown", "id": "906a254e-5fc2-41de-894e-91ace6a2756b", "metadata": {}, "source": [ "# Bia-bob\n", "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." ] }, { "cell_type": "code", "execution_count": 1, "id": "f77d1470-7f46-47e8-8ac4-5db660a52586", "metadata": {}, "outputs": [], "source": [ "import stackview\n", "import openai" ] }, { "cell_type": "code", "execution_count": 2, "id": "45bea0e2-3e26-4723-a529-1bf3340679b9", "metadata": {}, "outputs": [], "source": [ "def prompt(message:str, model=\"gpt-3.5-turbo\"):\n", " \"\"\"A prompt helper function that sends a message to openAI\n", " and returns only the text response.\n", " \"\"\"\n", " client = openai.OpenAI()\n", " response = client.chat.completions.create(\n", " model=model,\n", " messages=[{\"role\": \"user\", \"content\": message}]\n", " )\n", " return response.choices[0].message.content" ] }, { "cell_type": "code", "execution_count": 3, "id": "e24d6116-d19b-42d0-8387-b71202a364e7", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "dict_keys(['annotate', 'assist', 'create_colormap', 'crop', 'curtain', 'insight', 'interact', 'jupyter_displayable_output', 'merge_rgb', 'nop', 'orthogonal', 'picker', 'side_by_side', 'slice', 'switch'])" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "all = {}\n", "for n in dir(stackview):\n", " if not n.startswith('_'):\n", " all[n] = getattr(stackview, n)\n", "\n", "all.keys()" ] }, { "cell_type": "code", "execution_count": 4, "id": "6fda77e3-718f-4317-9afc-3e22958abb79", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'annotate': ,\n", " 'crop': ,\n", " 'curtain': ,\n", " 'insight': ,\n", " 'interact': ,\n", " 'orthogonal': ,\n", " 'picker': ,\n", " 'side_by_side': ,\n", " 'slice': ,\n", " 'switch': }" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "all.pop('jupyter_displayable_output')\n", "all.pop('create_colormap')\n", "all.pop('merge_rgb')\n", "all.pop('nop')\n", "all.pop('assist')\n", "\n", "all" ] }, { "cell_type": "code", "execution_count": 5, "id": "dc8af4de-826b-4f2c-8630-f6ad96665ffc", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Displays an image with a slider and label showing mouse position and intensity.\n", "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", "\n", "\n", "Allows cropping an image along all axes.\n", "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", "\n", "\n", "- Display two images and use a slider to switch between them.\n", "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", "\n", "\n", "- Convert numpy-array-like image to numpy-compatible array with convenient display in Jupyter notebooks\n", "stackview.insight(image, library_name=None, help_url=None)\n", "\n", "\n", "Build a user interface with sliders for numeric and \"footprint\" or \"selem\" parameters\n", "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", "\n", "\n", "- Display an image and allows slicing in three dimensions (Z, Y, and X) with optional zooming and colormap settings.\n", "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", "\n", "\n", "* Display an image with a slider and label showing the mouse position and intensity.\n", "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", "\n", "\n", "* Display two images side by side, with an additional overlay view of their colocalization, and a slider to navigate through a stack.\n", "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", "\n", "\n", "* Display an image with a slider to navigate through a stack.\n", "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", "\n", "\n", "- Allows switching between multiple images and displaying them with a slider.\n", "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", "\n", "\n", "[\"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" ] } ], "source": [ "def get_function_signature(func):\n", " import inspect\n", " \n", " function_object = func\n", " signature_object = inspect.signature(function_object)\n", " signature_string = \"stackview.\" + func.__name__ + str(signature_object)\n", " return signature_string\n", "\n", "all_descriptions = []\n", "for name, func in all.items():\n", " \n", " description = prompt(f\"\"\"Shorten the following docstring into a single bullet point:\n", "\n", " ```\n", " {func.__doc__}\n", " ```\n", " \n", " 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", "\n", " Examples:\n", "\n", " * compute the absolute value of every individual pixel in an image\n", " cle.absolute(source: ndarray, destination: ndarray = None) -> ndarray\n", "\n", " * determine the absolute difference pixel by pixel between two images\n", " cle.absolute_difference(source1: ndarray, source2: ndarray, destination: ndarray = None) -> ndarray\n", " \n", " * add a scalar value to all pixels of an image\n", " cle.add_image_and_scalar(source: ndarray, destination: ndarray = None, scalar: float = 1) -> ndarray\n", " \n", " \"\"\").replace(\"If you want to \", \"* \")\n", " description = description + \"\\n\" + get_function_signature(func) + \"\\n\\n\"\n", " print(description)\n", " \n", " all_descriptions.append(description)\n", "\n", "print(all_descriptions[:1000])" ] }, { "cell_type": "code", "execution_count": 6, "id": "754b2914-3556-4f2b-8361-5bcd8f3286bf", "metadata": {}, "outputs": [], "source": [ "descriptions = []\n", "for bullet_point in all_descriptions:\n", " if bullet_point[0:2] in ['- ', '* ']:\n", " bullet_point = bullet_point[2:]\n", "\n", " descriptions.append(\"* \" + bullet_point)\n" ] }, { "cell_type": "code", "execution_count": 7, "id": "99adee01-0847-43e8-9ef3-df511805f7cd", "metadata": {}, "outputs": [], "source": [ "text = \"\".join(descriptions).replace('\"', \"'\")\n", "text = \" \" + text.replace(\"\\n\", \"\\n \")\n", "text = f'''\n", "def list_bia_bob_plugins():\n", " \"\"\"List of function hints for bia_bob\"\"\"\n", " return \"\"\"{text}\"\"\"\n", "'''\n", "\n", "with open(\"../stackview/_bia_bob_plugins.py\", 'w') as file:\n", " file.write(text)" ] }, { "cell_type": "code", "execution_count": null, "id": "003174bb-9c47-48f7-8e2b-361f7d7a4359", "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.10.13" } }, "nbformat": 4, "nbformat_minor": 5 }