{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import networkx as nx\n",
    "import community\n",
    "import matplotlib.pyplot as plt\n",
    "from hiveplot import HivePlot\n",
    "from collections import defaultdict\n",
    "from dautil import plotting\n",
    "from dautil import data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "fb_file = data.SPANFB().load()\n",
    "G = nx.read_edgelist(fb_file, \n",
    "                     create_using = nx.Graph(), \n",
    "                     nodetype = int)\n",
    "print(nx.info(G))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "parts = community.best_partition(G)\n",
    "nodes = defaultdict(list)\n",
    "\n",
    "for n, d in parts.items():\n",
    "    nodes[d].append(n)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "edges = defaultdict(list)\n",
    "\n",
    "for u, v in nx.edges(G, nodes[0]):\n",
    "    edges[0].append((u, v, 0))\n",
    "\n",
    "for u, v in nx.edges(G, nodes[1]):\n",
    "    edges[1].append((u, v, 1))\n",
    "\n",
    "for u, v in nx.edges(G, nodes[2]):\n",
    "    edges[2].append((u, v, 2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "%matplotlib inline\n",
    "cmap = plotting.sample_hex_cmap(name='hot', ncolors=len(nodes.keys()))\n",
    "h = HivePlot(nodes, edges, cmap, cmap)\n",
    "h.draw()\n",
    "plt.title('Facebook Network Hive Plot')"
   ]
  }
 ],
 "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.4.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
