{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Heatmap: Visualizing a Graph\n", "\n", "This example shows how to visualize graphs using a heatmap." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import graspologic\n", "\n", "import numpy as np\n", "%matplotlib inline" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Plotting graphs using heatmap\n", "\n", "### Simulate graphs using weighted stochastic block models\n", "The 2-block model is defined as below:\n", "\n", "\\begin{align*}\n", "P = \\begin{bmatrix}0.8 & 0.2 \\\\\n", "0.2 & 0.8 \n", "\\end{bmatrix}\n", "\\end{align*}\n", "\n", "We generate two weight SBMs where the weights are distributed from a Poisson(3) and Normal(5, 1)." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from graspologic.simulations import sbm\n", "\n", "n_communities = [50, 50]\n", "p = [[0.8, 0.2], \n", " [0.2, 0.8]]\n", "\n", "wt = np.random.poisson\n", "wtargs = dict(lam=3)\n", "A_poisson= sbm(n_communities, p, wt=wt, wtargs=wtargs)\n", "\n", "wt = np.random.normal\n", "wtargs = dict(loc=5, scale=1)\n", "A_normal = sbm(n_communities, p, wt=wt, wtargs=wtargs)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Plot the simulated weighted SBMs" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from graspologic.plot import heatmap\n", "\n", "title = 'Weighted Stochastic Block Model with Poisson(3)'\n", "\n", "fig= heatmap(A_poisson, title=title)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "title = 'Weighted Stochastic Block Model with Normal(5, 1)'\n", "\n", "fig= heatmap(A_normal, title=title)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### You can also change color maps\n", "\n", "See [here](https://matplotlib.org/tutorials/colors/colormaps.html) for a list of colormaps" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "title = 'Weighted Stochastic Block Model with Poisson(3)'\n", "\n", "fig= heatmap(A_poisson, title=title, transform=None, cmap=\"binary\", center=None)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Data transformations\n", "\n", "When your graphs have values that span a large range, it is often useful to transform the data in order to visualize it properly. Below, we use a real graph which is estimated from the a structural MRI scan. The data is provided by [HNU1](http://dx.doi.org/10.15387/fcp_indi.corr.hnu1). \n", "\n", "The data ranges from 0 to 44813, and visualizing without a transformation will emphasize the large weights. Both log and pass-to-ranks transforms provide better visualizations of the graph." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "tags": [] }, "outputs": [], "source": [ "G = np.load('./data/sub-0025427_ses-1_dwi_desikan.npy')\n", "\n", "print((np.min(G), np.max(G)))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Without transform" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "title = 'Without Transformation'\n", "fig= heatmap(G, title=title, transform=None)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### With log transform" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "title = 'Log Transform'\n", "fig= heatmap(G, title=title, transform='log')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### With pass to ranks" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "title = 'Pass-to-ranks (zero-boost) Transform'\n", "fig= heatmap(G, title=title, transform='zero-boost')" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.7.0" } }, "nbformat": 4, "nbformat_minor": 4 }