Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save jeinarsson/473177761de159d5b2cbab4321a3afc0 to your computer and use it in GitHub Desktop.
Save jeinarsson/473177761de159d5b2cbab4321a3afc0 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"data_from_eg_excel=[\n",
"['A', 'Z', 'i1', 30],\n",
"['A', 'Z', 'i2', 24],\n",
"['A', 'Z', 'i0', 37],\n",
"['A', 'Y', 'i1', 52],\n",
"['A', 'Y', 'i2', 19],\n",
"['A', 'Y', 'i0', 22],\n",
"['B', 'Z', 'i1', 22],\n",
"['B', 'Z', 'i2', 22],\n",
"['B', 'Z', 'i0', 28],\n",
"['B', 'Y', 'i1', 15],\n",
"['B', 'Y', 'i2', 37],\n",
"['B', 'Y', 'i0', 33]\n",
"]"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"# list(set(x)) gives list of unique elements in x\n",
"unique_source_nodes = list(set(map(lambda row: row[0], data_from_eg_excel)))\n",
"unique_dest_nodes = list(set(map(lambda row: row[1], data_from_eg_excel)))\n",
"unique_intermediate_nodes = list(set(map(lambda row: row[2], data_from_eg_excel)))"
]
},
{
"cell_type": "code",
"execution_count": 39,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"import random # just for randomizing colors\n",
"def make_items(nodes):\n",
" return [{\n",
" 'items': [\n",
" {\n",
" 'title': x,\n",
" 'color': \"#%03x\" % random.randint(0, 0xFFF)\n",
" } for x in nodes\n",
" ],\n",
" 'title': '',\n",
" 'label': 0\n",
" }]\n",
"\n",
"source_layer = {\n",
" 'items': make_items(unique_source_nodes),\n",
" 'title': 'Source layer',\n",
" 'x': 0.0\n",
"}\n",
"intermediate_layer = {\n",
" 'items': make_items(unique_intermediate_nodes),\n",
" 'title': 'Intermediate layer',\n",
" 'x': 0.5\n",
"}\n",
"dest_layer = {\n",
" 'items': make_items(unique_dest_nodes),\n",
" 'title': 'Destination layer',\n",
" 'x': 1.0\n",
"}\n",
"nodes = [source_layer, intermediate_layer, dest_layer]"
]
},
{
"cell_type": "code",
"execution_count": 40,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"flows = [\n",
" {\n",
" 'path': [\n",
" [0,0,unique_source_nodes.index(row[0])],\n",
" [1,0,unique_intermediate_nodes.index(row[2])],\n",
" [2,0,unique_dest_nodes.index(row[1])]\n",
" ],\n",
" 'magnitude': row[3]\n",
" }\n",
" for row in data_from_eg_excel\n",
"]"
]
},
{
"cell_type": "code",
"execution_count": 41,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{\n",
" \"nodes\": [\n",
" {\n",
" \"items\": [\n",
" {\n",
" \"items\": [\n",
" {\n",
" \"color\": \"#2a5\",\n",
" \"title\": \"A\"\n",
" },\n",
" {\n",
" \"color\": \"#8c0\",\n",
" \"title\": \"B\"\n",
" }\n",
" ],\n",
" \"label\": 0,\n",
" \"title\": \"\"\n",
" }\n",
" ],\n",
" \"x\": 0.0,\n",
" \"title\": \"Source layer\"\n",
" },\n",
" {\n",
" \"items\": [\n",
" {\n",
" \"items\": [\n",
" {\n",
" \"color\": \"#39f\",\n",
" \"title\": \"i1\"\n",
" },\n",
" {\n",
" \"color\": \"#4f3\",\n",
" \"title\": \"i2\"\n",
" },\n",
" {\n",
" \"color\": \"#618\",\n",
" \"title\": \"i0\"\n",
" }\n",
" ],\n",
" \"label\": 0,\n",
" \"title\": \"\"\n",
" }\n",
" ],\n",
" \"x\": 0.5,\n",
" \"title\": \"Intermediate layer\"\n",
" },\n",
" {\n",
" \"items\": [\n",
" {\n",
" \"items\": [\n",
" {\n",
" \"color\": \"#80f\",\n",
" \"title\": \"Z\"\n",
" },\n",
" {\n",
" \"color\": \"#033\",\n",
" \"title\": \"Y\"\n",
" }\n",
" ],\n",
" \"label\": 0,\n",
" \"title\": \"\"\n",
" }\n",
" ],\n",
" \"x\": 1.0,\n",
" \"title\": \"Destination layer\"\n",
" }\n",
" ],\n",
" \"flows\": [\n",
" {\n",
" \"path\": [\n",
" [\n",
" 0,\n",
" 0,\n",
" 0\n",
" ],\n",
" [\n",
" 1,\n",
" 0,\n",
" 0\n",
" ],\n",
" [\n",
" 2,\n",
" 0,\n",
" 0\n",
" ]\n",
" ],\n",
" \"magnitude\": 30\n",
" },\n",
" {\n",
" \"path\": [\n",
" [\n",
" 0,\n",
" 0,\n",
" 0\n",
" ],\n",
" [\n",
" 1,\n",
" 0,\n",
" 1\n",
" ],\n",
" [\n",
" 2,\n",
" 0,\n",
" 0\n",
" ]\n",
" ],\n",
" \"magnitude\": 24\n",
" },\n",
" {\n",
" \"path\": [\n",
" [\n",
" 0,\n",
" 0,\n",
" 0\n",
" ],\n",
" [\n",
" 1,\n",
" 0,\n",
" 2\n",
" ],\n",
" [\n",
" 2,\n",
" 0,\n",
" 0\n",
" ]\n",
" ],\n",
" \"magnitude\": 37\n",
" },\n",
" {\n",
" \"path\": [\n",
" [\n",
" 0,\n",
" 0,\n",
" 0\n",
" ],\n",
" [\n",
" 1,\n",
" 0,\n",
" 0\n",
" ],\n",
" [\n",
" 2,\n",
" 0,\n",
" 1\n",
" ]\n",
" ],\n",
" \"magnitude\": 52\n",
" },\n",
" {\n",
" \"path\": [\n",
" [\n",
" 0,\n",
" 0,\n",
" 0\n",
" ],\n",
" [\n",
" 1,\n",
" 0,\n",
" 1\n",
" ],\n",
" [\n",
" 2,\n",
" 0,\n",
" 1\n",
" ]\n",
" ],\n",
" \"magnitude\": 19\n",
" },\n",
" {\n",
" \"path\": [\n",
" [\n",
" 0,\n",
" 0,\n",
" 0\n",
" ],\n",
" [\n",
" 1,\n",
" 0,\n",
" 2\n",
" ],\n",
" [\n",
" 2,\n",
" 0,\n",
" 1\n",
" ]\n",
" ],\n",
" \"magnitude\": 22\n",
" },\n",
" {\n",
" \"path\": [\n",
" [\n",
" 0,\n",
" 0,\n",
" 1\n",
" ],\n",
" [\n",
" 1,\n",
" 0,\n",
" 0\n",
" ],\n",
" [\n",
" 2,\n",
" 0,\n",
" 0\n",
" ]\n",
" ],\n",
" \"magnitude\": 22\n",
" },\n",
" {\n",
" \"path\": [\n",
" [\n",
" 0,\n",
" 0,\n",
" 1\n",
" ],\n",
" [\n",
" 1,\n",
" 0,\n",
" 1\n",
" ],\n",
" [\n",
" 2,\n",
" 0,\n",
" 0\n",
" ]\n",
" ],\n",
" \"magnitude\": 22\n",
" },\n",
" {\n",
" \"path\": [\n",
" [\n",
" 0,\n",
" 0,\n",
" 1\n",
" ],\n",
" [\n",
" 1,\n",
" 0,\n",
" 2\n",
" ],\n",
" [\n",
" 2,\n",
" 0,\n",
" 0\n",
" ]\n",
" ],\n",
" \"magnitude\": 28\n",
" },\n",
" {\n",
" \"path\": [\n",
" [\n",
" 0,\n",
" 0,\n",
" 1\n",
" ],\n",
" [\n",
" 1,\n",
" 0,\n",
" 0\n",
" ],\n",
" [\n",
" 2,\n",
" 0,\n",
" 1\n",
" ]\n",
" ],\n",
" \"magnitude\": 15\n",
" },\n",
" {\n",
" \"path\": [\n",
" [\n",
" 0,\n",
" 0,\n",
" 1\n",
" ],\n",
" [\n",
" 1,\n",
" 0,\n",
" 1\n",
" ],\n",
" [\n",
" 2,\n",
" 0,\n",
" 1\n",
" ]\n",
" ],\n",
" \"magnitude\": 37\n",
" },\n",
" {\n",
" \"path\": [\n",
" [\n",
" 0,\n",
" 0,\n",
" 1\n",
" ],\n",
" [\n",
" 1,\n",
" 0,\n",
" 2\n",
" ],\n",
" [\n",
" 2,\n",
" 0,\n",
" 1\n",
" ]\n",
" ],\n",
" \"magnitude\": 33\n",
" }\n",
" ]\n",
"}\n"
]
}
],
"source": [
"import json\n",
"data = {\n",
" 'nodes': nodes,\n",
" 'flows': flows\n",
"}\n",
"data_json = json.dumps(data, indent=4)\n",
"print(data_json)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"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
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment