<jupyter_start><jupyter_text>Bootstrap2.5d Colab Before running the script, go to the toolbar above and select:    Runtime → Change Runtime type → Hardware accelerator → GPUAfter the runtime has been changed, the necessary dependencies need to be downloaded.<jupyter_code># Install dependencies
!pip install snakemake tqdm torch simpleitk albumentations -U -q
#reload modules whenever they are updated
%load_ext autoreload
%autoreload 2
%matplotlib inline
import os
import numpy as np
import SimpleITK as sitk
from glob import glob
from matplotlib import pyplot as plt
from matplotlib.colors import LinearSegmentedColormap<jupyter_output><empty_output><jupyter_text>Next, the bootstrap2.5d directory will be downloaded and the directories for the model's inputs and outputs will be created.<jupyter_code># Designate path of bootstrap2.5d directory
# Change to /content/drive/My Drive/bootstrap2.5d if downloading to Google Drive
boot_dir = "/content/bootstrap2.5d"

# Make the boot_dir and clone the GitHub repo
os.mkdir(boot_dir)
!git clone https://github.com/volume-em/bootstrap2.5d.git {boot_dir}
# Create data directories
os.mkdir(os.path.join(boot_dir, 'models'))
os.mkdir(os.path.join(boot_dir, 'data'))
os.mkdir(os.path.join(boot_dir, 'data/train'))
os.mkdir(os.path.join(boot_dir, 'data/train/images'))
os.mkdir(os.path.join(boot_dir, 'data/train/masks'))
os.mkdir(os.path.join(boot_dir, 'data/target'))
os.mkdir(os.path.join(boot_dir, 'data/target/images'))

#this is optional, only applies if there is a ground truth mask for the target images
os.mkdir(os.path.join(boot_dir, 'data/target/masks'))<jupyter_output><empty_output><jupyter_text>The repository has been cloned and the necessary directories have been created, now datasets can be added. To upload data, click the folder icon to the left. From there files can be added by:1. Dragging files from your local file browser into colab2. Right clicking on a folder in colab and pressing upload3. Pressing "Mount Drive" at the top of the files tab. This will allow you to access files from your Google Drive in Colab. Files uploaded to Colab runtimes are deleted when sessions end, for permanent storage the bootstrap2.5d repository should be downloaded directly to your Google Drive. Alternatively, if the repo is downloaded to the runtime's temporary storage one can click and drag files into Drive.  ExampleIn this example, we are downloading data from the paper [Automatic segmentation of mitochondria and endolysosomes in volumetric electron microscopy data](https://www.sciencedirect.com/science/article/abs/pii/S0010482520300792?via%3Dihub). This dataset contains training volumes with segmented mitochondria and lysosomes. Because the organelles are segmented in separate files, we have to combine them in the cell below.<jupyter_code># Run the setup script, save data in the data folder
data_dir = os.path.join(boot_dir, 'data')
setup_script = os.path.join(boot_dir, 'example_data/setup_data.py')
!python {setup_script} {data_dir}<jupyter_output><empty_output><jupyter_text>Now that the dataset has been downloaded, we can visualize what our training volumes and masks look like.<jupyter_code># Create a custom colormap so that background is transparent
# get colormap
ncolors = 256
color_array = plt.get_cmap('viridis')(range(ncolors))

# change alpha values
#color_array[:,-1] = np.linspace(0.0, 1.0, ncolors)
color_array[0, -1] = 0.0

# create a colormap object
map_object = LinearSegmentedColormap.from_list(name='viridis_alpha',colors=color_array)

# register this new colormap with matplotlib
plt.register_cmap(cmap=map_object)
# Take some 2D slices of a 3D volume
vol_index = 0 #index of file in the train/images directory

#get a list of the train image volumes
impaths = np.sort(glob(os.path.join(data_dir, 'train/images/*.nii.gz')))
impath = impaths[vol_index]

#load the image and the mask and convert them to arrays
train_image = sitk.GetArrayFromImage(sitk.ReadImage(impath))
train_mask = sitk.GetArrayFromImage(sitk.ReadImage(impath.replace('/images/', '/masks/')))

#sample 12 evenly spaced slices from the xy plane
slice_indices = np.linspace(0, train_image.shape[0] - 1, num=12, dtype='int')

#create subplots
f, ax = plt.subplots(2, 6, figsize=(16, 8))

#plot the images
c = 0
for y in range(2):
    for x in range(6):
        slice_index = slice_indices[c]
        ax[y, x].set_title(f'Slice {slice_index}')
        ax[y, x].imshow(train_image[slice_index], cmap='gray')
        ax[y, x].imshow(train_mask[slice_index], alpha=0.3, cmap='viridis_alpha')
        ax[y, x].set_yticks([])
        ax[y, x].set_xticks([])
        c += 1
        
plt.tight_layout()<jupyter_output><empty_output><jupyter_text>Now we're ready to run the snakemake. We need to set 3 configuration parameters: the data directory, model directory, and the number of segmentation classes. In this case we have 3: background, lysosomes, and mitochondria. For binary segmentation, we would set n_classes=1.Other hyperparameters or file paths can be changed by directly editing the Snakefile. Text files can be edited directly in Colab by double clicking them in the file browser.<jupyter_code>snakefile = os.path.join(boot_dir, 'Snakefile')
model_dir = os.path.join(boot_dir, 'models')
!snakemake -s {snakefile} --cores all --config data_dir={data_dir} model_dir={model_dir} n_classes=3<jupyter_output><empty_output><jupyter_text>Now that predictions have been made, we can visualize how the two algorithms performed.<jupyter_code># Take some 2D slices of a 3D volume
vol_index = 0 #index of file in the target/images directory

#get a list of the train image volumes
impaths = np.sort(glob(os.path.join(data_dir, 'target/images/*.nii.gz')))
impath = impaths[vol_index]

#load the image and mask and predictions and convert them to arrays
target_image = sitk.GetArrayFromImage(sitk.ReadImage(impath))
target_mask = sitk.GetArrayFromImage(sitk.ReadImage(impath.replace('/images/', '/masks/')))
target_super_preds = sitk.GetArrayFromImage(sitk.ReadImage(impath.replace('/images/', '/super_preds/')))
target_weaksuper_preds = sitk.GetArrayFromImage(sitk.ReadImage(impath.replace('/images/', '/weaksuper_preds/')))

#sample 6 evenly spaced slices from the xy plane
slice_indices = np.linspace(0, train_image.shape[0] - 1, num=6, dtype='int')
column_names = ['Ground Truth', 'Step 1. Super Prediction', 'Step 2. Weak Super Prediction']

#create subplots
f, ax = plt.subplots(6, 3, figsize=(16, 20))

#plot the images
c = 0
for y in range(6):
    for x, overlay in enumerate([target_mask, target_super_preds, target_weaksuper_preds]):
        slice_index = slice_indices[c]
        ax[y, x].set_title(column_names[x])
        ax[y, x].set_ylabel(f'Slice {slice_index}')
        ax[y, x].imshow(target_image[slice_index], cmap='gray')
        ax[y, x].imshow(overlay[slice_index], alpha=0.3, cmap='viridis_alpha')
        ax[y, x].set_yticks([])
        ax[y, x].set_xticks([])
    c += 1
        
plt.tight_layout()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.103, 'fraction_numerical': 0.013285714285714286, 'mean_word_length': 5.273297491039426, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 25, 'https://': 2, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 7, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '172841', 'license': 'BSD-3-Clause', 'n_tokens_mistral': 2338, 'n_tokens_neox': 2207, 'n_words': 726, 'path': 'colab_notebook.ipynb', 'repo_name': 'volume-em/bootstrap2.5d', 'size': '7000'}