{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "_cell_guid": "ed5f8ec6-9636-45b5-0128-cc0fe3ce4291"
      },
      "source": [
        "# Plotting the shared variables between each csv\n",
        "This kernel shows the relations between the csv files in a graph structure. Its my first kernel, I kept it very simple and will probably improve it over the next days.\n",
        "\n",
        "**//edit: added another more readable plot, changed colors for better contrast with text and other small improvements**"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "0fa3f10c-af3a-65f0-c46b-4f8f4e126762"
      },
      "outputs": [],
      "source": [
        "%matplotlib notebook\n",
        "import os\n",
        "import networkx as nx\n",
        "import seaborn as sns\n",
        "import matplotlib.pyplot as plt"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "684aed5f-1dbf-cb72-e70d-57042c58c9a4"
      },
      "outputs": [],
      "source": [
        "# Read headers in each csv.\n",
        "headers = {}\n",
        "for csv in os.listdir('../input'):\n",
        "    with open('../input/{}'.format(csv)) as f:\n",
        "        headers[csv] = f.readline().rstrip().split(',')"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "b9c4ccf1-1898-3d33-12a4-33e02a50f7b6"
      },
      "outputs": [],
      "source": [
        "# Make a graph out of it\n",
        "g = nx.Graph()\n",
        "\n",
        "for e, cols in headers.items():\n",
        "\tfor c in cols:\n",
        "\t\tg.add_edge(e, c)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "284ced90-a47c-ccdf-dbd3-538f769d2c9a"
      },
      "outputs": [],
      "source": [
        "def plot_graph(g): # we reuse it later\n",
        "    p = sns.color_palette(\"Paired\", 4)[::2] # use nicer colors\n",
        "    pos = nx.fruchterman_reingold_layout(g)\n",
        "    colors = [p[0] if '.csv' in n else p[1] for n in g.nodes()]\n",
        "    colors = [p[0] if '.csv' in n else p[1] for n in g.nodes()]\n",
        "    \n",
        "    plt.figure(figsize=(12,12), dpi=200)\n",
        "    nx.draw_networkx_nodes(\n",
        "        g,pos,\n",
        "        nodelist=g.nodes(),\n",
        "        node_color=colors,\n",
        "        node_size=1000,\n",
        "        alpha=0.8\n",
        "    )\n",
        "    nx.draw_networkx_edges(g, pos)\n",
        "    nx.draw_networkx_labels(g, pos, font_size=9)\n",
        "\n",
        "    plt.show()\n",
        "    \n",
        "plot_graph(g)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "_cell_guid": "cf990422-9383-f07c-9d18-8be3b55aa619"
      },
      "source": [
        "## Lets remove all nodes with a single degree to unclutter it"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "613a4b19-ef83-ff3f-cc91-c4030d228869"
      },
      "outputs": [],
      "source": [
        "g2 = g.copy()\n",
        "removable_nodes = [n for n, d in g2.degree_iter() if d == 1]\n",
        "g2.remove_nodes_from(removable_nodes)\n",
        "\n",
        "plot_graph(g2)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "1717d591-0234-ae91-0f0b-5ff17a0e8f27"
      },
      "outputs": [],
      "source": [
        ""
      ]
    }
  ],
  "metadata": {
    "_change_revision": 0,
    "_is_fork": false,
    "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.5.2"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}