XBRAIN Demo Notebook

Last Update: 10/12/2017


In [12]:
# Imports 
import numpy as np
import scipy as sio
from PIL import Image
import xbrain
import time
import os
import intern
from intern.remote.dvid import DVIDRemote

In [13]:
#This is the only function in ndparse we use throughout the code, so I have decided to include the raw function's code here instead. 

def ndplot(im1, im2=None, cmap1='gray', cmap2='jet', slice=0,
         alpha=1, show_plot=True, save_plot=False):
    """
    Convenience function to handle plotting of neurodata arrays.
    Mostly tested with 8-bit image and 32-bit annos, but lots of
    things should work.  Mimics (and uses) matplotlib, but transparently
    deals with RAMON objects, transposes, and overlays.  Slices 3D arrays
    and allows for different blending when using overlays.  We require
    (but verify) that dimensions are the same when overlaying.
    Arguments:
        im1 (array): RAMONObject or numpy array
        im2 (array) [None]:  RAMONObject or numpy array
        cmap1 (string ['gray']): Colormap for base image
        cmap2 (string ['jet']): Colormap for overlay image
        slice (int) [0]: Used to choose slice from 3D array
        alpha (float) [1]: Used to set blending option between 0-1
    Returns:
        None.
    """

    import numpy as np
    import matplotlib.pyplot as plt

    # get im1_proc as 2D array
    fig = plt.figure()
    # fig.set_size_inches(2, 2)
    ax = plt.Axes(fig, [0., 0., 1., 1.])
    ax.set_axis_off()
    fig.add_axes(ax)

    base_image = False
    im1_proc = None

    if hasattr(im1, 'cutout') and im1.cutout is not None:
        im1_proc = im1.cutout
    elif im1 is not None:
        im1_proc = im1

    if im1_proc is not None and len(np.shape(im1_proc)) == 3:
        im1_proc = im1_proc[:, :, slice]

    if im1_proc is not None:
        base_image = True

    # get im2_proc as 2D array if exists
    overlay_image = False
    im2_proc = None

    if im2 is not None:

        if hasattr(im2, 'cutout') and im2.cutout is not None:
            im2_proc = im2.cutout
        elif im2 is not None:
            im2_proc = im2

        if im2_proc is not None and len(np.shape(im2_proc)) == 3:
            im2_proc = im2_proc[:, :, slice]

    if im2_proc is not None and np.shape(im1_proc) == np.shape(im2_proc):
        overlay_image = True

    if base_image:

        plt.imshow(im1_proc.T, cmap=cmap1, interpolation='bilinear')

    if base_image and overlay_image and alpha == 1:
        # This option is often recommended but seems less good in general.
        # Produces nice solid overlays for things like ground truth
        im2_proc = np.ma.masked_where(im2_proc == 0, im2_proc)
        plt.imshow(im2_proc.T, cmap=cmap2, interpolation='nearest')

    elif base_image and overlay_image and alpha < 1:

        plt.hold(True)
        im2_proc = np.asarray(im2_proc, dtype='float')  # TODO better way
        im2_proc[im2_proc == 0] = np.nan  # zero out bg
        plt.imshow(im2_proc.T, cmap=cmap2,
                   alpha=alpha, interpolation='nearest')

    if save_plot is not False:
        # TODO: White-space
        plt.savefig(save_plot, dpi=300, pad_inches=0)

    if show_plot is True:
        plt.show()

    pass

Initialize

  • Path of files
  • Parameters for ilastik
  • Parameters for cell detection
  • Parameters for blood vessel segmentation

In [14]:
# Set folder where data is stored
current_dir = os.path.abspath(os.getcwd())
folder = next(os.walk('.'))[1][0]
#print(os.path.exists(folder)) # testing

# ilastik parameters
classifier_file = folder + '/xbrain_vessel_seg_v7.ilp'
#print(classifier_file)                 # testing
#print(os.path.exists(classifier_file)) # testing

# testing ground truth
image_file = folder + '/V3_imgdata_gt.npy'
#print(image_file)                 # testing
#print(os.path.exists(image_file)) # testing


# Dictate the processing power
ram_size = 4000 # 4000 MB                                                                            
no_of_threads = 8 # 8 threads

# Cell detection parameters                                                                         
cell_probability_threshold  = 0.2
stopping_criterion = 0.47
initial_template_size = 18
dilation_size = 8
max_no_cells = 500

# Vessel segmentation parameters                                                                    
vessel_probability_threshold = .68
dilation_size = 3
minimum_size = 4000

Load in the data


In [15]:
#DVID Data fetch:
dvid = DVIDRemote({
    "protocol": "http",
    "host": "10.194.126.16:8000",
    })

chan = "d108503322e644b7a6853b20285bd811/MaskedImg1"
input_data = dvid.get_cutout(
    dvid.get_channel(chan),0,
    [1400,1600],[1800,2000],[390,590]
    )

# get the shape of the data
print(input_data.shape) # (200, 200, 200)


Grabbing your cutout...
(200, 200, 200)

Ingest data and classifer into ilastik


In [16]:
# Compute time required for processing
start = time.time()

# Process the data to probability maps
probability_maps = xbrain.classify_pixel(input_data, classifier_file, threads=no_of_threads, ram=ram_size)

end = time.time()
print("\nElapsed time: %f minutes" % ((end - start)/60))


WARNING default_config.py(243): ResourceWarning: unclosed file <_io.TextIOWrapper name='/opt/conda/ilastik-meta/ilastik/ilastik/ilastik_logging/logging_config.json' mode='r' encoding='UTF-8'>
---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
<ipython-input-16-67521d4a89cf> in <module>()
      3 
      4 # Process the data to probability maps
----> 5 probability_maps = xbrain.classify_pixel(input_data, classifier_file, threads=no_of_threads, ram=ram_size)
      6 
      7 end = time.time()

~/work/xbrain.py in classify_pixel(input_data, classifier, threads, ram)
     41     # Instantiate the 'shell', (an instance of ilastik.shell.HeadlessShell)
     42     # This also loads the project file into shell.projectManager
---> 43     shell = ilastik_main.main(args)
     44     assert isinstance(shell.workflow, PixelClassificationWorkflow)
     45 

/opt/conda/ilastik-meta/ilastik/ilastik_main.py in main(parsed_args, workflow_cmdline_args, init_logging)
    103     # The shell is provided as a parameter to the function.
    104     postinit_funcs = []
--> 105     load_fn = _prepare_auto_open_project(parsed_args)
    106     if load_fn:
    107         postinit_funcs.append(load_fn)

/opt/conda/ilastik-meta/ilastik/ilastik_main.py in _prepare_auto_open_project(parsed_args)
    342        not os.path.exists(parsed_args.project):
    343         raise RuntimeError("Project file '" +
--> 344                            parsed_args.project + "' does not exist.")
    345 
    346     parsed_args.project = os.path.expanduser(parsed_args.project)

RuntimeError: Project file '.ipynb_checkpoints/xbrain_vessel_seg_v7.ilp' does not exist.

Display the results of ilastik


In [8]:
# pull down the coorisponding matricies
cell_prob_map = probability_maps[:, :, :, 2]
vessel_prob_map = probability_maps[:, :, :, 1]

print("cell_prob_map shape", cell_prob_map.shape)
ndplot(cell_prob_map, slice=50, cmap1='jet', alpha=0.5)


print("vessel_prob_map shape", vessel_prob_map.shape)
ndplot(vessel_prob_map, slice=50, cmap1='jet')


---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-8-974900263821> in <module>()
      1 # pull down the coorisponding matricies
----> 2 cell_prob_map = probability_maps[:, :, :, 2]
      3 vessel_prob_map = probability_maps[:, :, :, 1]
      4 
      5 print("cell_prob_map shape", cell_prob_map.shape)

NameError: name 'probability_maps' is not defined

Running different package for testing new algorithm


In [17]:
# reload packages for testing new algorithms
# import importlib
# importlib.reload(xbrain)

# Compute time required for processing
start = time.time()

# cell detection
centroids, cell_map = xbrain.detect_cells(cell_prob_map, cell_probability_threshold, stopping_criterion, initial_template_size, dilation_size, max_no_cells)
print(centroids)

end = time.time()
print("\nElapsed time: %f minutes" % ((end - start)/60))


---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-17-8c913349d0c6> in <module>()
      7 
      8 # cell detection
----> 9 centroids, cell_map = xbrain.detect_cells(cell_prob_map, cell_probability_threshold, stopping_criterion, initial_template_size, dilation_size, max_no_cells)
     10 print(centroids)
     11 

NameError: name 'cell_prob_map' is not defined

In [91]:
# find vessels

vessel_map = xbrain.segment_vessels(vessel_prob_map, vessel_probability_threshold, dilation_size, minimum_size)

Display results of new algorithm


In [95]:
# show results

print("Vessel Segmentation")
ndplot(input_data, vessel_map, slice = 50, alpha = 0.5)

print("Cell Segmentation")
ndplot(input_data, cell_map, slice = 50, alpha = 0.5)


WARNING assess.py(299): MatplotlibDeprecationWarning: pyplot.hold is deprecated.
    Future behavior will be consistent with the long-time default:
    plot commands add elements without first clearing the
    Axes and/or Figure.
WARNING __init__.py(917): UserWarning: axes.hold is deprecated. Please remove it from your matplotlibrc and/or style files.
WARNING rcsetup.py(152): UserWarning: axes.hold is deprecated, will be removed in 3.0
Vessel Segmentation
Cell Segmentation

Thank you for going throught the XBRAIN tutorial! For more information about the lab, please visit: dyerlab.gatech.edu


In [ ]: