Optional: Coding assistance#
Writing code for simple tasks such as showing images or iterating over files in a folder can be time-consuming. Generative artificial intelligence tools such as github copilot or bia-bob can help out. You can install bia-bob in your conda environment by opening another terminal, activating your environment and using pip:
Activate your environment:
mamba activate devbio-napari-env
Install bia-bob:
pip install bia-bob openai
Afterwards, restart the kernel of this notebook.
In this notebook we will demonstrate how to use bia-bob, a coding assistant specialized in bio-image analysis.
## The following cells might be necessary if you run this code on your computer.
## Ask a trainer for the key to enter at ...
import os
os.environ['OPENAI_API_KEY'] = 'sk-...'
You can initialize bia-bob like this.
import bia_bob
Afterwards, you ask for assistance by writing %bob
in front of an English prompt. Bob will generate code right below the cell.
%bob load blobs.tif and show it
I will load an image file named blobs.tif
from the disk and display it.
from skimage.io import imread
import stackview
# Load the image from disk
filename = 'blobs.tif'
image = imread(filename)
# Display the image
stackview.insight(image)
If your task is more complex, you can write multi-line prompts behind ´%%bob´. The more details you provide, e.g. which libraries and functions Bob should use, will make the code fit better to what you expect.
%%bob
For all image files in the folder ´data_banana´:
* load the image
* show the image using ´skimage.io.imshow´
I will load all image files located in the folder data_banana
and display each one using skimage.io.imshow
.
import os
from skimage.io import imread, imshow
import matplotlib.pyplot as plt
# Get a list of files in 'data_banana' directory
directory_path = 'data_banana'
file_list = os.listdir(directory_path)
# Filter out files that are image files (common extensions)
image_files = [f for f in file_list if f.endswith(('.tif', '.czi', '.png', '.jpg'))]
# Load and display each image file
for filename in image_files:
# Construct the full image path
file_path = os.path.join(directory_path, filename)
# Load the image
img = imread(file_path)
# Display the image
imshow(img)
plt.title(f"Displaying image: {filename}")
plt.show()