| 1 |
{ |
| 2 |
"cells": [ |
| 3 |
{ |
| 4 |
"cell_type": "code", |
| 5 |
"execution_count": null, |
| 6 |
"metadata": {}, |
| 7 |
"outputs": [], |
| 8 |
"source": [ |
| 9 |
"from itertools import chain\n", |
| 10 |
"\n", |
| 11 |
"from bqplot import OrdinalColorScale, Figure, Graph\n", |
| 12 |
"from ipywidgets import Layout" |
| 13 |
] |
| 14 |
}, |
| 15 |
{ |
| 16 |
"cell_type": "code", |
| 17 |
"execution_count": null, |
| 18 |
"metadata": {}, |
| 19 |
"outputs": [], |
| 20 |
"source": [ |
| 21 |
"# mobile patent suits - http://bl.ocks.org/mbostock/1153292\n", |
| 22 |
"suits = [\n", |
| 23 |
" {'source': 'Microsoft', 'target': 'Amazon', 'type': 'licensing'},\n", |
| 24 |
" {'source': 'Microsoft', 'target': 'HTC', 'type': 'licensing'},\n", |
| 25 |
" {'source': 'Samsung', 'target': 'Apple', 'type': 'suit'},\n", |
| 26 |
" {'source': 'Motorola', 'target': 'Apple', 'type': 'suit'},\n", |
| 27 |
" {'source': 'Nokia', 'target': 'Apple', 'type': 'resolved'},\n", |
| 28 |
" {'source': 'HTC', 'target': 'Apple', 'type': 'suit'},\n", |
| 29 |
" {'source': 'Kodak', 'target': 'Apple', 'type': 'suit'},\n", |
| 30 |
" {'source': 'Microsoft', 'target': 'Barnes & Noble', 'type': 'suit'},\n", |
| 31 |
" {'source': 'Microsoft', 'target': 'Foxconn', 'type': 'suit'},\n", |
| 32 |
" {'source': 'Oracle', 'target': 'Google', 'type': 'suit'},\n", |
| 33 |
" {'source': 'Apple', 'target': 'HTC', 'type': 'suit'},\n", |
| 34 |
" {'source': 'Microsoft', 'target': 'Inventec', 'type': 'suit'},\n", |
| 35 |
" {'source': 'Samsung', 'target': 'Kodak', 'type': 'resolved'},\n", |
| 36 |
" {'source': 'LG', 'target': 'Kodak', 'type': 'resolved'},\n", |
| 37 |
" {'source': 'RIM', 'target': 'Kodak', 'type': 'suit'},\n", |
| 38 |
" {'source': 'Sony', 'target': 'LG', 'type': 'suit'},\n", |
| 39 |
" {'source': 'Kodak', 'target': 'LG', 'type': 'resolved'},\n", |
| 40 |
" {'source': 'Apple', 'target': 'Nokia', 'type': 'resolved'},\n", |
| 41 |
" {'source': 'Qualcomm', 'target': 'Nokia', 'type': 'resolved'},\n", |
| 42 |
" {'source': 'Apple', 'target': 'Motorola', 'type': 'suit'},\n", |
| 43 |
" {'source': 'Microsoft', 'target': 'Motorola', 'type': 'suit'},\n", |
| 44 |
" {'source': 'Motorola', 'target': 'Microsoft', 'type': 'suit'},\n", |
| 45 |
" {'source': 'Huawei', 'target': 'ZTE', 'type': 'suit'},\n", |
| 46 |
" {'source': 'Ericsson', 'target': 'ZTE', 'type': 'suit'},\n", |
| 47 |
" {'source': 'Kodak', 'target': 'Samsung', 'type': 'resolved'},\n", |
| 48 |
" {'source': 'Apple', 'target': 'Samsung', 'type': 'suit'},\n", |
| 49 |
" {'source': 'Kodak', 'target': 'RIM', 'type': 'suit'},\n", |
| 50 |
" {'source': 'Nokia', 'target': 'Qualcomm', 'type': 'suit'},\n", |
| 51 |
"]" |
| 52 |
] |
| 53 |
}, |
| 54 |
{ |
| 55 |
"cell_type": "code", |
| 56 |
"execution_count": null, |
| 57 |
"metadata": {}, |
| 58 |
"outputs": [], |
| 59 |
"source": [ |
| 60 |
"# transform data into nodes and links\n", |
| 61 |
"nodes = list(set(chain(*((suit['source'], suit['target']) for suit in suits))))\n", |
| 62 |
"\n", |
| 63 |
"# set custom node attrs\n", |
| 64 |
"node_data = [{'label': node, 'shape_attrs': {'r': 6}, 'label_display': 'outside'} for node in nodes]\n", |
| 65 |
"# for links, source and target should be indices into the nodes list\n", |
| 66 |
"nodes_index_map = {node: i for i, node in enumerate(nodes)}\n", |
| 67 |
"link_data = [{'source': nodes_index_map[s['source']], \n", |
| 68 |
" 'target': nodes_index_map[s['target']], \n", |
| 69 |
" 'value': s['type']} for s in suits]\n", |
| 70 |
"\n", |
| 71 |
"# encode suit type with link color\n", |
| 72 |
"link_color_scale = OrdinalColorScale(domain=['licensing', 'suit', 'resolved'], \n", |
| 73 |
" colors=['limegreen', 'dodgerblue', 'orangered'])\n", |
| 74 |
"graph = Graph(node_data=node_data, link_data=link_data, link_type='arc', \n", |
| 75 |
" scales={'link_color': link_color_scale}, colors=['gray'], \n", |
| 76 |
" directed=True, link_distance=100, charge=-600)\n", |
| 77 |
"Figure(marks=[graph], layout=Layout(height='900px', width='1000px'), title='Mobile Patent Suits')" |
| 78 |
] |
| 79 |
} |
| 80 |
], |
| 81 |
"metadata": { |
| 82 |
"kernelspec": { |
| 83 |
"display_name": "Python 3", |
| 84 |
"language": "python", |
| 85 |
"name": "python3" |
| 86 |
}, |
| 87 |
"language_info": { |
| 88 |
"codemirror_mode": { |
| 89 |
"name": "ipython", |
| 90 |
"version": 3 |
| 91 |
}, |
| 92 |
"file_extension": ".py", |
| 93 |
"mimetype": "text/x-python", |
| 94 |
"name": "python", |
| 95 |
"nbconvert_exporter": "python", |
| 96 |
"pygments_lexer": "ipython3", |
| 97 |
"version": "3.7.6" |
| 98 |
} |
| 99 |
}, |
| 100 |
"nbformat": 4, |
| 101 |
"nbformat_minor": 2 |
| 102 |
} |