Clustergrammer Jupyter Widget
Jupyter notebooks are ideal for generating reproducible workflows and analysis. They are also the best way to share Clustergrammer's interactive visualizations while providing context, analysis, and the underlying data to enable reproducibility (see :ref:`clustergrammer_widget_examples`). The Clustergrammer Widget enables users to easily produce interactive visualizations within a Jupyter notebook that can be shared with collaborators (using nbviewer). Clustergrammer-Widget can be used to visualize a matrix of data from a file or from a Pandas DataFrame (see :ref:`matrix_format_io` for more information). The library is free and open-source and can be found on GitHub.
Clustergrammer has been applied to a wide variety of biological and non-biological data. See the Jupyter notebook examples below and :ref:`case_studies` for more information:
- CCLE Jupyter Notebook
- Lung Cancer PTM and Gene Expression Regulation
- Single-Cell CyTOF Data
- MNIST Notebook
- USDA Nutrient Dataset
- Iris Dataset.ipynb
Clustergrammer can be used as an interactive widget within a Jupyter notebook and shared using nbviewer (see Running_clustergrammer_widget.ipynb example).
Jupyter Widget Dependencies
Clustergrammer-Widget works with Python 2 and 3.
To use the :ref:`clustergrammer_widget` users need to install: Python, Jupyter notebook, the widget dependencies (see :ref:`clustergrammer_widget_dependencies`), and ipywidgets version 6.0.0 (to save the notebook with widgets, version 6.0.0 is recommended). Users can install Anaconda, a Python distribution that includes the Jupyter notebook as well as other scientific computing libraries, to easily obtain the necessary dependencies (except ipywidgets version 6.0.0). The
clustergrammer_widget can the be installed (with pip) and enabled using the following commands:
pip install --upgrade clustergrammer_widget jupyter nbextension enable --py --sys-prefix widgetsnbextension jupyter nbextension enable --py --sys-prefix clustergrammer_widget
Clustergrammer-Widget Workflow Example
The Jupyter notebook Running_clustergrammer_widget.ipynb (which is rendered using nbviewer) shows how to visualize: a matrix from a file and a Pandas DataFrame. The following examples are taken from this notebook.
Here we are visualizing a matrix of data from a file (e.g.
rc_two_cats.txt). We start by instantiating the
net, and passing it the widget class, clustergrammer_widget as an argument. The net object is used to load data, filter, normalize, cluster, and render the widget. For more information about the
Network class, refer to the :ref:`clustergrammer_py_api`.
Load Data from File
# make imports and instantiate a Network instance with the widget class as an argument from clustergrammer_widget import * net = Network(clustergrammer_widget) # load matrix file net.load_file('rc_two_cats.txt') # cluster using default parameters net.cluster() # make interactive widget net.widget()
General Purpose DataFrame Viewer
Clustergrammer-Widget can also be used as a general purpose Pandas DataFrame viewer. Below is an example of how to visualize a Pandas DataFrame,
df, by loading it into the
# load DataFrame net.load_df(df) # cluster using default parameters net.cluster() # make interactive widget net.widget()
Loading new data into
net removes any old data, which allows the
net object to be easily reused within the same notebook.
Filtering, Downsampling, and Normalizing
net object can also be used to filter and normalize your data before visualizing (note that filtering and normalization are permanent and irreversible). The example below performs Z-score normalization on the columns, filters to keep the top 200 rows based on their absolute value sum, calculates clustering, and finally renders the widget:
# Z-score normalize columns net.normalize(axis='col', norm_type='zscore', keep_orig=True) # filter for the top 200 rows based on their absolute value sum net.filter_N_top('row', 200, 'sum') # cluster using default parameters net.cluster() # make interactive widget net.widget()
Two-way Widget Communication
df_widget method below for an example:
# After modifying the visualization (e.g. dendrogram cropping) we can export the # modified matrix to the back end using the df_widget method df_new = net.df_widget()
For more information about the
Network object and additional options refer to the :ref:`clustergrammer_py_api`.
Sharing with nbviewer
To enable rendering interactive widgets on nbviewer you must have ipywidgets version 6 or later installed and use the "Save Notebook with Widgets" action in the Widgets menu in the Jupyter notebook (see ipywidgets Rendering Interactive Widgets on nbviewer documentation and screenshot below):
Users can save notebooks with interactive HTML widgets using the "Save Notebook with Widgets" action in the Jupyter Notebook Widgets menu as shown here. ipywidgets version 6 or later must be installed in order to enable this feature.