{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "#

     Force-directed graphs in Lightning" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "##
Setup" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import os\n", "from lightning import Lightning\n", "\n", "from numpy import random, asarray, linspace, corrcoef\n", "from colorsys import hsv_to_rgb\n", "from sklearn import datasets\n", "import networkx as nx" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Connect to server" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "
Lightning initialized
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "Connected to server at http://public.lightning-viz.org\n" ] }, { "data": { "application/javascript": [ "(function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require==\"function\"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);throw new Error(\"Cannot find module '\"+o+\"'\")}var f=n[o]={exports:{}};t[o][0].call(f.exports,function(e){var n=t[o][1][e];return s(n?n:e)},f,f.exports,e,t,n,r)}return n[o].exports}var i=typeof require==\"function\"&&require;for(var o=0;o" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "lgn = Lightning(ipython=True, host='http://public.lightning-viz.org')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "##
Random binary network" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "A random graph where every node has the same degree, with default styling.\n", "
\n", "This and other graph plots take matrices (e.g. adjacency matrices) as inputs.\n", "
\n", "We use the `networkx` library to generate the graph, then get its adjancency matrix.\n", "
\n", "Double click on a point to see its neighbors (all should have 3).\n", "
\n", "Drag points to try and move the graph, it should remain tight because of the degree structure." ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "n = 100\n", "G = nx.random_regular_graph(3,n)\n", "mat = nx.adjacency_matrix(G).todense()\n", "lgn.force(mat)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "##
Random weighted network" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The previous network was binary (all links either 0 or 1).\n", "Here the links are weighted, which is reflected in the line widths." ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "G = nx.random_regular_graph(3,100)\n", "mat = asarray(nx.adjacency_matrix(G).todense()) * (((random.rand(100,100))*5) ** 2)\n", "lgn.force(mat)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "##
Lobster network" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The lobster graph, a backbone with some leaves, is colored here by node ordering.\n", "
\n", "We also set all nodes with degree less than 2 to gray." ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "G = nx.random_lobster(60, 0.5, 0.0, seed=44)\n", "mat = nx.adjacency_matrix(G).todense()\n", "n = len(G.nodes())\n", "c = [list(asarray(hsv_to_rgb(float(x) / n , 0.8, 1.0))*255) if y > 1 else [150,150,150] for (x,y) in G.degree_iter()]\n", "lgn.force(mat, color=c)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "##
Coloring by degree" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Here we color points (and also change their size) to indicate their degree.\n", "
\n", "Click to confirm bigger points have more connections." ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "G = nx.random_geometric_graph(50, 0.1)\n", "mat = nx.adjacency_matrix(G).todense()\n", "g = G.degree().values()\n", "lgn.force(mat, group=g, size=(asarray(g) + 1.5)*3)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "##
Graph of clustering" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Graphs can be a useful way to look at data that doesn't neccessarily come from a graph.\n", "
\n", "Here we create a graph from a thresholded correlation matrix on data drawn from a set of clusters.\n", "
\n", "The cluster identities are clear in the resulting graph." ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d, g = datasets.make_blobs(n_features=5, n_samples=50, centers=5, cluster_std=2.0, random_state=100)\n", "c = corrcoef(d)\n", "c[c<0.9] = 0\n", "lgn.force(c, group=g)" ] } ], "metadata": { "kernelspec": { "display_name": "Python 2", "language": "python", "name": "python2" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 2 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", "version": "2.7.9" } }, "nbformat": 4, "nbformat_minor": 0 }