Skip to content

Instantly share code, notes, and snippets.

@keiono
Last active May 8, 2016 06:21
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save keiono/096816a468736a71a3bf915b2b156c81 to your computer and use it in GitHub Desktop.
Save keiono/096816a468736a71a3bf915b2b156c81 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Visualize \"Panama Papers\" Data Sets\n",
"# パナマ文書のデータを可視化する\n",
"\n",
"This is a simple python utility to convert this data sets into [Cytoscape.js](http://js.cytoscape.org/) JSON files. All results will be sent to [Cytoscape Desktop](http://www.cytoscape.org/) via [cyREST](http://apps.cytoscape.org/apps/cyrest).\n",
"\n",
"## How to Use This Notebook\n",
"\n",
"1. Clone the original data repository: ```git clone https://github.com/amaboura/panama-papers-dataset-2016.git```\n",
"1. ```cd panama-papers-dataset-2016/viz-data```\n",
"1. Copy thin notebook into the directory\n",
"1. Run it"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"# Make sure you have Python 3.x & py2cytoscape 0.5.0+\n",
"\n",
"from py2cytoscape.data.cynetwork import CyNetwork\n",
"from py2cytoscape.data.cyrest_client import CyRestClient\n",
"import json\n",
"import os"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Utility functions"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"def create_node(node):\n",
" cynode = {}\n",
" data = {}\n",
" \n",
" data['id'] = str(node['id'])\n",
" data['label'] = str(node['label'])\n",
" data['color'] = node['color']\n",
" data = convert_node_data(node['data'], data)\n",
" \n",
" cynode['data'] = data\n",
" return cynode\n",
"\n",
"\n",
"def convert_node_data(node_props, data):\n",
" node_type = node_props['categories']['0']\n",
" data['type'] = node_type\n",
" \n",
" for key, value in node_props['properties'].items():\n",
" data['prop.' + str(key)] = value\n",
"\n",
" return data\n",
"\n",
"\n",
"def create_edge(edge):\n",
" cyedge = {}\n",
" data = {}\n",
" \n",
" data['id'] = str(edge['id'])\n",
" data['label'] = str(edge['label'])\n",
" data['source'] = str(edge['source'])\n",
" data['target'] = str(edge['target'])\n",
" data['color'] = edge['color']\n",
" \n",
" cyedge['data'] = data\n",
" return cyedge\n",
"\n",
"\n",
"def create_network(network, title):\n",
" nodes = network['nodes']\n",
" edges = network['edges']\n",
"\n",
" cynet = {\n",
" 'data': {\n",
" 'name': title\n",
" },\n",
" 'elements': {\n",
" 'nodes': [],\n",
" 'edges': []\n",
" }\n",
" }\n",
" \n",
" for node in nodes:\n",
" cynet['elements']['nodes'].append(create_node(node))\n",
"\n",
" for edge in edges:\n",
" cynet['elements']['edges'].append(create_edge(edge))\n",
" \n",
" return cynet\n",
"\n",
"\n",
"def visualize_networks(network_files, rest_client):\n",
" for f in network_files:\n",
" with open(f) as data_file: \n",
" net = json.load(data_file)\n",
" cynet = create_network(net, f)\n",
" cyjs_network = rest_client.network.create(data=cynet)\n",
" rest_client.layout.apply(name='force-directed', network=cyjs_network)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"# Grab all network JSON files in this directory\n",
"all_files = os.listdir('.') \n",
"network_files = filter(lambda f: f.endswith('.json'), all_files)\n",
"\n",
"# Initialize cyREST client\n",
"cy = CyRestClient()\n",
"\n",
"# Visualize all\n",
"visualize_networks(network_files, cy)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Optional: Visual Style\n",
"\n",
"You can use the following Style file for basic visualization, but be creative!\n",
"\n",
"* https://gist.github.com/keiono/61c13178fdde1eebf5db439a62992058"
]
}
],
"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.5.1"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment