Welcome to mpi.is-brain-connectivity-plugin’s documentation!

_images/paraview_preview.png

Installation

The installation is done in 2 steps:

  1. first installing the package (preferably in a python virtual environment),
  2. then generating the plugin that points to the installed package, and register the plugin into Paraview. This is needed only if you want to use the Paraview plugin. Otherwise installing the package alone will let you use the utility functions.

Things to consider:

The installation procedure for the package and the plugin depend on the version of Paraview in use.

Identify your Paraview and Python installations

In case you want to use the visualization plugin and the package functions inside Paraview, you will need to identify the version of Paraview you are using. The most important aspect is to

  • identify if this is a standalone version of Paraview
  • if your version uses the same Python interpreter as the one of your package.

In case the installation of Paraview is standalone, Paraview may use its own Python interpreter (through pvpython or pvbatch), while your package may be using another Python interpreter that is eg. the one that defaults on your operating system.

Should this be the case, the interpreters in use in the plugin and the package would be different. This is usually not an issue, as the package code (as of May 2018) is pure Python and does not bring any compiled python extension. However, in case you start developing additional extensions and those contain python extensions, an ABI problem may occur and will potentially lead to a crash during the execution of the plugin.

To be safe, it is recommended to install a version of Paraview that uses the OS Python interpreter, and to use the same interpreter for installing our package.

See the Platform specific details for more information.

Package installation

The package preferably installs in a virtual environment. Also since Paraview is using Python2, we suggest sticking to Python2. For installing the package, you need at least virtualenv and pip. Once there, let’s first create a virtual environment like this (in a terminal):

mkdir ~/.venvs
virtualenv --system-site-packages ~/.venvs/venv_paraview_brain
. ~/.venvs/venv_paraview_brain/bin/activate

and then navigate to the path where you cloned the package, and type

cd <brain-connectivity-visualization-path>
pip install -r requirements.txt
pip install .

After the installation, you should see the package with pip list:

pip list
Package                          Version
-------------------------------- ---------
[...]
brain-connectivity-visualization 1.0

Plugin installation

For installing the plugin, we first generate a binding between Paraview and the package in the form of an .xml file, and we then install/register this file in Paraview as a plugin. This .xml will act as a binding between the relevant functions in our package and Paraview.

There is no streamlined procedure for installing a python plugin into Paraview, and/or to use an external python library. Paraview has different installation flavour that differ according to the platform or the way you installed the tool. We tried to make the procedure as easy as possible.

Generating the XML plugin file

Installing the package will bring you a script generate_brain_connectivity_plugin that will be used for generating a plugin file in the .xml format. This little tool converts the entry point of the plugin into an XML file that can be installed/registered into Paraview.

To call the tool, run the following from the command line terminal:

generate_brain_connectivity_plugin mpi_brain_plugin.xml

Registering the XML plugin file

This plugin should then be imported into Paraview

  1. Open the menu from Tools > Manage Plugins:

    Manage Plugins
  2. On the custom filter window, click on Load New:

    Plugins Window
  3. Select the .xml file that you generated earlier, make sure that the file filter of the selection dialog shows .xml:

    Plugins Selection
  4. You should then see the plugin on the list of plugins, make sure the plugin is loaded. You may check the autoload option for loading the plugin the next time you start Paraview.

    Plugins Selection

Warning

Paraview will need the .xml file you generated every time it needs to run your plugin. In case you move this file to some other physical location on disk, you will be needing to redo the installation steps above after having uninstalled the plugin.

Also the plugin will be using the location of the python virtual environment where the package is installed. This will let you update the package (and the plugin functionalities) without registering the plugin again, but will prevent you from removing the virtual environment.

Note

In case you save your session in Paraview for later reuse/load, the plugin needs to be loaded prior to loading the session. This is why the autoload option is convenient.

Tip

You may have noticed the message Local plugins are automatically searched for in <some-path> at the top of the plugin list dialog box. If you have a standalone installation of Paraview, you may generate the .xml file directly there and then skip the registration and loading of the plugin.

Platform specific installation details

OSX

Paraview on OSX relies on the OSX system Python (2.7.X at the current day). This means that it is possible to install the package and the plugins from a unique location using a python virtual environment, and this will be the preferred method on this platform.

Warning

If another version of Python is in use from the command line, eg. after an installation with Homebrew (either Python2 or Python3), it would mean that you may fall in the case mentioned earlier, and it would be safer to make the OSX system Python the default in that case.

The installation of Paraview is often located in /Application folder, and it is not possible to write into the Paraview application folder. Therefor the installation on OSX requires a virtual environment or a global installation.

Paraview comes with its own version of Numpy, that might not be ABI compatible with newer versions of the library. Numpy is extensively used in the visualization plugin, which indirectly means that the API being used in the plugin should be compatible with the version of Numpy installed along Paraview.

Some parts of the visualization plugin require VTK. It is unfortunately quite difficult to use VTK installation provided by Paraview. In particular it is not straightforward to use the libraries provided by Paraview with your own Python setup. One possibility would be

  1. create your virtual environment
  2. compile and install VTK with the python bindings
  3. load your python shell within your environment
  4. point to the freshly installed VTK

For compiling VTK:

cd <vtk-source-folder>
mkdir build
cd build
cmake -DCMAKE_INSTALL_PREFIX=~/usr/local -DVTK_WRAP_PYTHON=ON ..
make -j 4
make install

For using VTK if it is not installed in a system folder (recommanded), if for instance VTK is installed in ~/usr/local

virtualenv venv_paraview
. venv_paraview/bin/activate
pip install -r requirements.txt
pip install .
pip install ipython

# adding VTK to the list of paths of the virtualenv
USER_SITE=$(python -c "import os; print os.environ['VIRTUAL_ENV']")
echo "/Users/<myself>/usr/local/lib/python2.7/site-packages/" >> $USER_SITE/lib/python2.7/site-packages/vtk.pth

export DYLD_LIBRARY_PATH=/Users/<myself>/usr/local/lib/:$DYLD_LIBRARY_PATH
python -c "import vtk; print 'success!'"
ipython
> import vtk

For being able to run the scripts that require VTK, after installation do:

export DYLD_LIBRARY_PATH=/Users/<myself>/usr/local/lib/:$DYLD_LIBRARY_PATH
export PYTHONPATH=/Users/<myself>/usr/local/lib/python2.7/site-packages/:$PYTHONPATH

generate_brain_connectivity_volume_file id1090_timestep.nii nii-volume-2
Linux

The Linux installation is either:

  1. the one that comes with the operating system package manager, such as aptitude. In that case, it is likely that this installation uses the system Python and you will be using the same Python interpreter for Paraview and your package installation,
  2. or the one that can be downloaded from the Paraview website, that is often more recent, but that also may use an embedded/shipped Python interpreter that will differ from the one of your operating system.

In first case, the procedure with virtual environments can be safely followed. In the second case, and if the version of Python is different than the one of the operating system, an additional step outside of the virtual environment should be taken:

  1. Suppose the standalone version of Paraview is located $PARAVIEW_PATH, and the visualization package has been cloned to <brain-connectivity-visualization-path>

  2. Then install the package within the Paraview Python environment by typing the following in a terminal

    cd <brain-connectivity-visualization-path>
    $PARAVIEW_PATH/bin/pvpython setup.py install
    

The previous commands will install the package inside the Python installation shipped with Paraview. In this case the virtual environment installation is not necessary, as Paraview will be using only the package installed in its own environment. In all cases, the registration of the plugin remains the same.

you may need the virtual environment installation described above only if you need to perform some processing using the utility functions from a regular command line. The package will then have two installation location: the Paraview Python environement and your virtual environment.

Paraview visualization filter

This page describes how to use the brain visualization filter inside Paraview:

  1. the plugin acts on a mesh of point representing the surface of the brain
  2. several visualization algorithm and parameters are shown in the user interface
  3. the plugin needs external files in order to render the connectivity edges

Input data

For the plugin to compute the visualization, the following elements are required:

  • the mesh on which computation will be performed. This is usually a mesh file that Paraview can understand/load without extra requirements,
  • the connectivity (or edges) file that contains the topology of connection network in the brain. This connectivity file is a mapping between pair of mesh or volume location and a weight. In its current form, it is a python numpy file that is generated with a command line tool form the brain connectivity package, and that is described in the section Connectivity file from Nifti.
  • Optionally it is also possible to provide a preprocessed clusters file, which would avoid the live computation of the clustering for the selected edges. See section Clustering for more details.

Parameters

The parameters exposed to the user by the plugin are shown in the picture below:

_images/brain_clusters1000_selected100_plugin.png

The meaning of the parameters are:

  • resolution: indicates the edges resolution, ie. the number of intermediate steps a path connecting two sites is divided into. The higher this number, the smoother the trajectory but also the heavier the on-screen drawing,
  • edge selection: a range of edge indices on which the processing will be performed. Any edge falling outside of this range is ignored in the visualization and the computation. This entry is ignored when a cluster file is provided.
  • edge file: the absolute path of the file containing the definition of the topology,
  • show edges: if checked, the edges will be shown
  • clusters file: the absolute path to the file containing the results of the clustering. This file contains a preprocessed version of the clustering. If not present, a clustering of the selected edges will be computed during the visualization, which may take some time. For offline computation of the clustering, see the corresponding section.
  • nb_clusters: indicates the number of desired clusters. This applies on the range of edges that has been selected or the full range if no selection has been made, and is ignored if the clustering file is given.
  • cluster index: if given, a specific cluster to show on screen. All other clusters are discarded. The edges that are in the specified cluster are also shown.
  • hierarchy index: the level of the hierarchy to which the cluster index applies.

Note

The hierarchical part is not functional.

Computation utilities

Along the Paraview plugin, several functions have been developed. Those utilities have different purposes:

  • make off-line computations in order to speed-up the visualization
  • support the writing of new visualization functions

Visualization preparation

The visualization requires a little preparation step that aims at creating necessary or optional files. Those files are either required for the visualization, or assist the visualization with additional elements.

The only necessary file is the edge file that indicates the plugin the end-points of the different connectivity edges in the 3d volume.

Additionally, it is possible to create:

  • a volume file from a nifti volumetric data. An example of use of such a file in Paraview is given in the image below

    _images/volume_view.png
  • a grid file of all edges end-points location.

    _images/end_points_location_view.png

Off-line computations

Clustering with kMeans

The clustering of edges is performed with the kmeans algorithm.

The version of this algorithm currently being in use within the plugin is implemented in a naive way in Python, using a distance function that is specific to the needs of the visualization. Accelerated algorithms exist such as the [Elkan] and that involves a lower number of computations and distance evaluations.

The hierarchical version of the kMeans runs the kMeans at each level of the hierarchy, and has also a complexity similar to the kMeans multiplied by the number of hierarchy levels.

Since the clustering may take some time to converge, a script accessible after the installation of the package in a virtual environment is provided.

Bibliographic references

[Elkan]Elkan: Using the triangle inequality to accelerate k-means, ICML‘03

References

mpi_is_sw.brain_connectivity.naive_k_means

This module defines the k-means clustering used for creating clusters of edges and summarized information for the set of edges. The implementation is pure python and is slow, hence the name naive.

convergent(old_means, new_means)

Returns True if the two sets of edges are close enough.

This would indicate that the convergence of the k-means clustering has been reached.

create_flip_map(distances)

Compares two arrays column-wise and returns an array of integer indices denoting whether the first or second array contains the smaller value.

Parameters:distances – a matrix of shape (2, k, n) where k is the number of means and n is the number of edges each mean is compared to
Returns:an array of integer values of 0, 1
distances(edges, means)

Computes the distance between two set of edges.

The distance is summing the distance between the end points of two edges, irrespective of the orientation of those edges.

The fixed set is called means. For each edge returns the index of the closest mean edge, as well as a boolean value marking whether or not edge_i has been closer to the flipped or original version of the mean in question.

fetch_edges(mean_index, edges, means)

Returns a matrix containing all edges assigned to a mean_index.

init_centroids(k, edges)

Returns k centroids by randomly sampling data points out of a given set of edges, as a matrix of shape (k, n).

Parameters:
  • k – an integer value denoting the number of means/clusters.
  • edges – a matrix of edges to sample the initial means from
Returns:

a matrix of k data points from the original edges matrix

Raises:

AssertError if k > edges.shape[0]

kmeans(k, edges, max_iterations=100, verbose=False, callback=None)

Returns k-means clustering of a set of edges.

This algorithm clusters the set of edges in k clusters, using the metric given by the distance() function.

Parameters:
  • k – an integer value denoting the number of means/clusters
  • edges – a matrix of n-dimensional datapoints
  • max_iterations – an integer value that defines the maximum number of iterations should convergence not be reached
  • callback – a callable object taking two arguments: the centroids and the labels. The callback is called every 100 iterations and may be used for checkpointing.
Returns:

a 2-uples where the first element is a matrix of shape (k, n) containing the centroids/means of the clustered dataset, and the second element of the tuple being the assignments/labels of the edges given as argument to the indexed centroids.

label_elements(distances)
reorder_edges(edges, flip_map)

Combines an array edges of datapoints and its flipped copy into a single array with length equal to the length of the original edges. Datapoints are chosen based on a boolean array flip_map.

Parameters:
  • edges – a matrix of shape (n, 6)
  • flip_map – an integer/boolean array of length n
Returns:

a matrix of shape (n, 6)

update_centroids(edges, labels, means, flip_map)

Returns an updated matrix of means. For each mean, sets it to the mean of all points assigned to it.

Parameters:
  • edges – a matrix of n-dimensional edges to compute clusters of
  • labels – a matrix of shape edges.shape with integer values in the half-open interval [0, k)
  • means – a matrix of shape (k, 1, n) where k is the number of datapoints and n is the length of a single datapoint
Returns:

a matrix of shape (k, 1, n), containing the updated means

File conversion utilities

Connectivity file from Nifti

The set of connection between two different sites of the brain is contained in a edge file file, that makes it convenient to read in the visualization filter in Paraview.

Generating the edge file is done through the command generate_brain_connectivity_edge_file and requires:

  • the nifti file that can be read with nibabel,
  • the csv file encoding the connections between sites in the brain,
  • the output file.

Warning

This functionality requires the installation of the nibabel python package. Please make sure you followed the Installation procedures.

End-points volume image

The 3D volume containing all the end-points of the connexels can be generated from the command line in the same manner as for the connectivity file. The generated file is in the VTK format.

Warning

Requires nibabel (see above) and VTK (see installation instructions).

Volume image

The 3D volumetric image may be generated from the command line with the command generate_brain_connectivity_volume_file. This call is a wrapper around the function generate_volume_file().

Clustering

A script for performing the clustering of all the edges is provided. This scripts generates a file that can be given to the visualization filter, which makes the exploration of the different edge clusters much faster.

The command to generate the cluster file is generate_brain_connectivity_cluster_file. The command requires:

  • the mesh file,
  • the edge file,
  • the number of desired clusters,
  • the output file

The output file can be given to the visualization filter in Paraview to avoid expensive computations during the visualization.

Warning

Requires nibabel (see above) and VTK (see installation instructions).

Tip

scipy may be used to speed-up the computation of the k-means if installed.

References

utils.generate_from_nifty

Provides tools for constructing intermediate files needed for the visualization.

Note

This file is meant to be installed during the installation of the package as a callable script with several commands.

The functions provided here are:

  • the transformation of a Nifti indicator file to a set of edge coordinates. Those edges will be used by the visualization algorithm
  • the transformation of the Nifti volumetric data to a VTK image that can be rendered inside Paraview.

The edge file requires:

  • a Nifti volume that indicates the end points on a grid, and
  • a CSV file that contains the topology that connects the previous end points. This CSV file should have two columns and a number of rows identical to the number of edges. Each column entry is an index to a coordinate that is extracted from the previous Nifti file. The other columns are ignored.
generate_cluster_file(mesh_file, edge_file, number_of_clusters, output_file, max_iterations=100)

Convenience function for running the clustering on a set of edges

Parameters:
  • mesh_file – the mesh that will be used for centering the data and making it isotropic
  • edge_file – the file containing the edges as computed from generate_edge_file()
  • number_of_cluster – the number of desired clusters
  • output_file – the file in which the results of the clutering will be done
  • max_steps – maximum number of iterations for the kmeans

Note

The plugin will not know if the files and parameters are consistent. If you provide a file name that contains the computation of the centroids for a specific edge file, and the edge file is incorrectly given to the plugin, the plugin will be unable to check.

generate_edge_file(csv_file_path, nifti_edge_file_path, output_file)

Convenience script for creating an edge file

Parameters:
  • nifti_edge_file_path – the path containing the nifti volume indicating the locations of the edge end points
  • csv_file_path – the file containing the topology
  • output_file – the output file in a numpy array format

See also

file_ops.assemble_edges()

generate_edge_volume_file(nifti_edge_file_path, output_file)

Convenience script for creating a volume locating all edges end-points

Parameters:
  • nifti_edge_file_path – the path containing the nifti volume indicating the locations of the edge end points
  • csv_file_path – the file containing the topology
  • output_file – the output file in a VTK file format

See also

file_ops.assemble_edges()

See also

file_ops.save_points_to_vtk_array()

generate_volume_file(nifti_volumetric_file_path, output_file)

Convenience script for creating a VTK compatible volume image file.

utils.generate_plugin_xml

Provides tools for generating the XML Paraview plugins.

generatePythonFilterFromFiles(script_file, output_file, function_name=None)

Generates an XML plugin file for Paraview from Python source file

Parameters:function_name – the name of the function containing the plugin entry point. Defaults to RequestData
generate_plugin_after_install(output_file)

Utility for generating the plugin XML file after the package has been successfully installed

This utility uses the plugin.py file as an entry point. This is suitable for running in Paraview in an environment that is not a virtual environment. It requires a Paraview installation of the plugin using pvpython. See the documentation for more details.

generate_plugin_in_virtualenv_after_install(output_file)

Utility for generating the plugin XML file after the package has been successfully installed

This utility uses the plugin.py file as an entry point. This is suitable for running in Paraview in an environment that is not a virtual environment. It requires a Paraview installation of the plugin using pvpython. See the documentation for more details.

This package contains a set of python tools for assisting in the visualization of brain connectivity information. The package contains:

  • a Paraview filter, that is a parametrizable plugin running in a Paraview visualization pipeline. This plugin shows the connectivity of the brain sites, in a raw or summarized fashion,
  • utility functions for converting files and performing off-line computation on the brain connectivity

References

This package was developed by Lennart Bramlage and Raffi Enficiaud at the Software Workshop Central Scientific Facility of the Max Planck Institute for Intelligent Systems, in Tübingen, Germany. The details of the computations are given in the following report:

Installation

The installation procedures are described on the page Installation.

How to use the plugin

The usage of the plugin is detailed in the page Paraview visualization filter.

Utility function

The additional tools are described in the pages Computation utilities and File conversion utilities.

License

The code contained in this repository is released under the MIT license and copyright is owned by

  • Max Planck Society
  • Lennart Bramlage
  • and Raffi Enficiaud.

See the LICENSE.txt file at the root folder of the repository for more information.

Indices and tables