Spatial Comparison and Visualization

Spatial image comparison means using a metric to derive a score that represents the similarity of two brain maps based on voxel values. Pybraincompare provides visualization methods (eg, Similarity Search, Atlas SVG) as well as computational ones (Spatial Computation) to perform and visualize this task.

Atlas SVG

You can generate an orthogonal view svg image, colored by region, for a brain atlas. As an example, we will use the MNI atlas provided by the package. These are also the atlases that can be integrated into a scatterplot comparison<http://vbmis.com/bmi/share/neurovault/scatter_atlas.html>_, with points colored by the atlas label. See here for the full example below.

from pybraincompare.compare import atlas as Atlas
from pybraincompare.mr.datasets import get_mni_atlas

# Color based on atlas labels
atlases = get_mni_atlas(voxdims=["2"])
atlas = atlases["2"]

# We return both paths and entire svg file
atlas.svg["coronal"]
atlas.paths["coronal"]

# Save svg views to file
output_directory = "/home/vanessa/Desktop"
atlas.save_svg(output_directory) # default will save all views generated
atlas.save_svg(output_directory,views=["coronal"]) # specify a custom set

Scatterplot Comparison

You can generate an interactive scatterplot (with an optional atlas to color regions by), and a demo is provided, along with this example as `script<https://github.com/vsoch/pybraincompare/blob/master/example/make_scatter.py>`_. First import functions to get images and standard brains, generate atlas SVGs, view a static html file from your local machine, and resample images:

# Create a scatterplot from two brain images
from pybraincompare.mr.datasets import get_pair_images, get_mni_atlas
from pybraincompare.compare.mrutils import resample_images_ref, get_standard_brain, get_standard_mask, do_mask
from pybraincompare.compare.maths import calculate_correlation
from pybraincompare.compare import scatterplot, atlas as Atlas
from pybraincompare.template.visual import view
from nilearn.image import resample_img
import numpy
import nibabel

The first example shows comparison with nifti image input

# SCATTERPLOT COMPARE ---- (with nifti input) ---------------------------------------------------

# Images that we want to compare - they must be in MNI space
image_names = ["image 1","image 2"]
images = get_pair_images(voxdims=["2","8"])

html_snippet,data_table = scatterplot.scatterplot_compare(images=images,
                                                 image_names=image_names,
                                                 corr_type="pearson")
view(html_snippet)

You may also need to use pybraincompare to resample images first, an example is provided below:

# RESAMPLING IMAGES -----------------------------------------------------------------------------

# If you use your own standard brain (arg reference) we recommend resampling to 8mm voxel
# Here you will get the resampled images and mask returned
reference = nibabel.load(get_standard_brain("FSL"))
images_resamp,ref_resamp = resample_images_ref(images,
                                           reference=reference,
                                           interpolation="continuous",
                                           resample_dim=[8,8,8])

Here is an example of an equivalent comparison, but using image vectors as input. This is an ideal solution in the case that you are saving reduced representations of your data. See pybraincompare.mr.transformation to generate such vectors.

# SCATTERPLOT COMPARE ---- (with vector input) ---------------------------------------------------

# We are also required to provide atlas labels, colors, and correlations, so we calculate those in advance
# pybraincompare comes with mni atlas at 2 resolutions, 2mm and 8mm
# 8mm is best resolution for rendering data in browser, 2mm is ideal for svg renderings
atlases = get_mni_atlas(voxdims=["8","2"])
atlas = atlases["8"]
atlas_rendering = atlases["2"]

This function will return a data frame with image vectors, colors, labels. The images must already be registered / in same space as reference

corrs_df = calculate_correlation(images=images_resamp,mask=ref_resamp,atlas=atlas,corr_type="pearson")

Option 1: Canvas based, no mouseover of points, will render 100,000’s of points. An example is provided. Note that we are specifying an output_directory, and so the result files will be saved there, and you can drop this into a web folder, or serve one locally to view (python -m SimpleHTTPServer 9999).

output_directory = "/home/vanessa/Desktop/test"
scatterplot.scatterplot_canvas(image_vector1=corrs_df.INPUT_DATA_ONE,
                          image_vector2=corrs_df.INPUT_DATA_TWO,
                          image_names=image_names,
                          atlas_vector = corrs_df.ATLAS_DATA,
                          atlas_labels=corrs_df.ATLAS_LABELS,
                          atlas_colors=corrs_df.ATLAS_COLORS,
                          output_directory=output_directory)

Option 2: D3 based, with mouseover of points, limited sampling of images.

html_snippet,data_table = scatterplot.scatterplot_compare_vector(image_vector1=corrs_df.INPUT_DATA_ONE,
                                                             image_vector2=corrs_df.INPUT_DATA_TWO,
                                                             image_names=image_names,
                                                             atlas=atlas_rendering,
                                                             atlas_vector = corrs_df.ATLAS_DATA,
                                                             atlas_labels=corrs_df.ATLAS_LABELS,
                                                             atlas_colors=corrs_df.ATLAS_COLORS,
                                                             corr_type="pearson")
view(html_snippet)

You can also use your own atlas, and here is how to generate it.

# CUSTOM ATLAS ---------------------------------------------------------------------------------

# You can specify a custom atlas, including a nifti file and xml file
atlas = Atlas.atlas(atlas_xml="MNI.xml",atlas_file="MNI.nii")
Default slice views are "coronal","axial","sagittal"

Plot Histogram

These functions have not been properly developed, however here is how to plot a histogram for an input image. This example is also provided as a script:

#!/usr/bin/python

from pybraincompare.report.histogram import plot_histogram
from pybraincompare.mr.datasets import get_pair_images

image = get_pair_images()[0]
plot_histogram(image)