{ "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", "import numpy as np" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Plotting Simple Graphs using heatmap" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "A 2-block Stochastic 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", "In simple cases, the model is unweighted. Below, we plot an unweighted SBM." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from graspologic.simulations import sbm\n", "from graspologic.plot import heatmap\n", "\n", "n_communities = [50, 50]\n", "p = [[0.8, 0.2], \n", " [0.2, 0.8]]\n", "\n", "A, labels = sbm(n_communities, p, return_labels=True)\n", "heatmap(A, title=\"Basic Heatmap function\");" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Plotting with Hierarchy Labels" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "If we have labels, we can use them to show communities on a Heatmap." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "heatmap(A, inner_hier_labels=labels)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can plot outer hierarchy labels in addition to inner hierarchy labels." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "outer_labels = [\"Outer Labels\"] * 100\n", "heatmap(A, inner_hier_labels=labels,\n", " outer_hier_labels=outer_labels)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Weighted SBMs" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can also use heatmap when our graph is weighted. Here, we generate two weighted SBMs where the weights are distributed from a Poisson(3) and Normal(5, 1)." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Draw weights from a Poisson(3) distribution\n", "wt = np.random.poisson\n", "wtargs = dict(lam=3)\n", "A_poisson= sbm(n_communities, p, wt=wt, wtargs=wtargs)\n", "\n", "# Plot\n", "title = 'Weighted Stochastic Block Model with \\n weights drawn from a Poisson(3) distribution'\n", "fig= heatmap(A_poisson, title=title)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Draw weights from a Normal(5, 1) distribution\n", "wt = np.random.normal\n", "wtargs = dict(loc=5, scale=1)\n", "A_normal = sbm(n_communities, p, wt=wt, wtargs=wtargs)\n", "\n", "# Plot\n", "title = 'Weighted Stochastic Block Model with \\n weights drawn from a Normal(5, 1) distribution'\n", "fig = heatmap(A_normal, title=title)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Colormaps\n", "\n", "You can change colormaps. 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", "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.8.5" } }, "nbformat": 4, "nbformat_minor": 4 }