Skip to main content

A Jupyter-based assistant for data analysis tasks

Project description

bia-bob

PyPI Contributors PyPI - Downloads GitHub stars GitHub forks License

BIA bob is a Jupyter-based assistant for interacting with data using large language models which generate Python code for Bio-Image Analysis (BIA). It can make use of OpenAI's chatGPT, Google's Gemini, Anthropic's Claude, Helmholtz' blablador and Ollama. You need an OpenAI API account or a Google Cloud account or a Helmholtz ID account to use it. Using it with Ollama is free but requires running an Ollama server locally.

bob can write short Python code snippets and entire Jupyter notebooks for your image / data analysis workflow.

img.png

Disclaimer

bia-bob is a research project aiming at streamlining the design of image analysis workflows. Under the hood it uses artificial intelligence / large language models to generate text and code fulfilling the user's requests. Users are responsible to verify the generated code according to good scientific practice. Some general advice:

  • If you do not understand what a generated code snippet does, ask %%bob explain this code in detail to a Python beginner: before executing the code.
  • After Bob generated a data analysis workflow for you, ask %%bob How could I verify this analysis workflow ?. It is good scientific practice to measure the quality of segmentation results for example, or to measure the difference of automated quantitative measurements, in comparison to manual analysis.
  • If you are not sure if an image analysis workflow is valid, consider asking human experts. E.g. reach out via https://image.sc .

[!CAUTION] When using the OpenAI, Google Gemini, Anthropic or any other endpoint via BiA-Bob, you are bound to the terms of service of the respective companies or organizations. The prompts you enter are transferred to their servers and may be processed and stored there. Make sure to not submit any sensitive, confidential or personal data. Also using these services may cost money.

Usage

You can initialize bob like this:

from bia_bob import bob

Code generation

You can ask Bob to generate code like this:

%bob Load blobs.tif and show it

It will then respond with a Python code snippet that you can execute (see full example):

img.png

Use %bob if you want to write in the same line and %%bob if you want to write below.

If you want to continue using a variable in the next code cell, you need to specify the name of the variable in the following prompt.

Notebook generation

When asking Bob explicitly to generate a notebook, it will put a new notebook file in the current directory with the generated code (See full example). You can then open it in Jupyter lab.

img.png

Notebook modification

You can also ask Bob to modify an existing notebook, e.g. to introduce explanatory markdown cells (See full example):

img.png

Furthermore, one can translate Jupyter notebooks to other languages, e.g. by prompting %bob translate the filename.ipynb to <language>.

img.png

Writing prompts

You can also ask Bob to write a prompt for you. This can be useful to explore potential strategies for analyzing image data. Note: It might be necessary to modify those prompts, especially when suggested analysis workflows are long and complicated. Shorten suggested prompts to the minimal necessary steps to answer your scientific question. (See full example).

img.png

Prompt augmentation

You can add additional information from given Python variables into your prompt using the {variable_name} syntax. With this, the content of the variable will become part of the prompt (full example).

Explaining code

If you are not sure what generated code does, you can ask Bob to explain it to you:

Bug fixing

Bob can fix simple bugs in code you executed. Just add %%fix on top of the cell right after the error happened.

img.png

Code documentation

Using the %%doc magic, you can generate documentation for a given code cell.

img.png

GPU-acceleration

Using the %%acc magic, you can replace common image processing functions with GPU-accelerated functions. It is recommended to check if the image processing results remain the same. You can see an example in this notebook.

img.png

Example notebooks

Command line interface

You can use bia-bob from the terminal. This is recommended for creating notebooks for example like this:

bia-bob Please create a Jupyter Notebook that opens blobs.tif, segments the bright objects and shows the resulting label image on top of the original image with a curtain.

img_1.png

This can also be used to create other files, e.g. CSV files.

img.png

Known issues

If you want to ask bob a question, you need to put a space before the ?.

%bob What do you know about blobs.tif ?

Installation

You can install bia-bob using pip. It is recommended to install it into a conda/mamba environment. If you have never used conda before, please read this guide first.

It is recommended to install bia-bob in a conda-environment together with useful tools for bio-image analysis.

conda env create -f https://github.com/haesleinhuepf/bia-bob/raw/main/environment.yml

You can then activate this environment...

conda activate bob_env

Using OpenAI as backend

Create an OpenAI API Key and add it to your environment variables named OPENAI_API_KEY as explained on this page.

Using Anthropic / claude

Create an Anthropic API Key and add it to your environment variables named ANTHROPIC_API_KEY.

Using custom endpoints

Custom endpoints can be used as well if they support the OpenAI API. Examples are blablador and ollama. An example is shown in this notebook:

For this, just install the openai backend as explained above (tested version: 1.5.0).

  • If you want to use ollama and e.g. the codellama model, you must run ollama serve from a separate terminal and then initialize bob like this:
bob.initialize(endpoint='ollama', model='codellama')
  • If you want to use blablador, which is free for German academics, just get an API key as explained on this page and store it in your environment as BLABLADOR_API_KEY variable.
bob.initialize(
    endpoint='blablador', 
    model='Mistral-7B-Instruct-v0.2')
  • Custom end points can be used as well, for example like this:
bob.initialize(
    endpoint='http://localhost:11434/v1', 
    api_key='my_key',
    model='codellama')

Using Google gemini 1.5 flash / pro

Create a Google API key and store it in the environment variable GOOGLE_API_KEY.

pip install google-generativeai>=0.7.2

Using Google's Cloud AI API as backend

Note: This method is deprecated. Use gemini 1.5 as shown above.

pip install google-cloud-aiplatform

(Recommended google-cloud-aiplatform version >= 1.38.1)

To make use of the Google Cloud API, you need to create a Google Cloud account here and a project within the Google cloud (for billing) here. You need to store authentication details locally as explained here. This requires installing Google Cloud CLI. In very short: run the installer and when asked, activate the "Run gcloud init" checkbox. Or run 'gcloud init' from the terminal yourself. Restart the terminal window. After installing Google Cloud CLI, start a terminal and authenticate using:

gcloud auth application-default login

Follow the instructions in the browser. Enter your Project ID (not the name). If it worked the terminal should approximately look like this:

img.png

Development

If you want to contribute to bia-bob, you can install it in development mode like this:

git clone https://github.com/haesleinhuepf/bia-bob.git
cd bia-bob
pip install -e .

Extensibility

If you are maintainer of a Python library and want to make BiA-bob aware of functions in your library, you can extend Bob's knowledge using entry-points. Add this to your library setup.cfg:


[options.entry_points]
bia_bob_plugins =
    plugin1 = your_library._bia_bob_plugins:list_bia_bob_plugins

In the above mentioned _bia_bob_plugins.py define this function (and feel free to rename the function and the Python file):

def list_bia_bob_plugins():
    """List of function hints for bia_bob"""
    return """
    * Computes the sum of a and b
    your_library.compute_sum(a:int,b:int) -> int
    
    * Determines the difference between a and b
    your_library.compute_difference(a:int, b:int) -> int
    
    """

Note that the syntax should be pretty much as shown above: A bullet point with a short description and a code-snippet just below. You can also generate the list_bia_bob_plugins function as demonstrated in this notebook. Please only list the most important functions. If the list of all plugins extending BiA-Bob becomes too long, the prompt will exceed the maximum prompt length.

List of known Python libraries that provide extensions to Bob:

(Feel free to extend this list by sending a pull-request)

Similar projects

There are similar projects:

Issues

If you encounter any problems or want to provide feedback or suggestions, please create a thread on image.sc along with a detailed description and tag @haesleinhuepf .

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

bia_bob-0.21.2.tar.gz (29.4 kB view details)

Uploaded Source

Built Distribution

bia_bob-0.21.2-py3-none-any.whl (29.0 kB view details)

Uploaded Python 3

File details

Details for the file bia_bob-0.21.2.tar.gz.

File metadata

  • Download URL: bia_bob-0.21.2.tar.gz
  • Upload date:
  • Size: 29.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.11.9

File hashes

Hashes for bia_bob-0.21.2.tar.gz
Algorithm Hash digest
SHA256 b4f0ac316bf07f197a390d04a2f084866863e7235c4a81618f6882e0434db8cf
MD5 8ca91861caf9c40360347e1e4542972b
BLAKE2b-256 d59599611a3ba0769335c4ba076688228aa7b183c2fe2fcd648ad711bd0d99de

See more details on using hashes here.

File details

Details for the file bia_bob-0.21.2-py3-none-any.whl.

File metadata

  • Download URL: bia_bob-0.21.2-py3-none-any.whl
  • Upload date:
  • Size: 29.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.11.9

File hashes

Hashes for bia_bob-0.21.2-py3-none-any.whl
Algorithm Hash digest
SHA256 4d9d9527087937a1e35ac0d5833100380d124dcca88e7a721a80357597d1505a
MD5 b326b9f131291c08cc5824d123cbff1e
BLAKE2b-256 40adf1510dc86460963dd44e717631faecbbe05c3ab2f857036b3d238061b3f0

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page