Skip to content

Instantly share code, notes, and snippets.

@michalskop
Last active August 29, 2015 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save michalskop/9865376 to your computer and use it in GitHub Desktop.
Save michalskop/9865376 to your computer and use it in GitHub Desktop.
UA: Changes in Parliamentary Groups with connection to Maidan and Crimea
# -*- coding: utf-8 -*-
import csv
import time
from datetime import datetime
from dateutil.relativedelta import relativedelta
import json
groups = {
"Батьківщина (Fatherland-Tymoshenko)":{
'name': "Батьківщина (Fatherland-Tymoshenko)",
'color': '#DC143C'
},
"Економічний розвиток (Economic Development-Kinakh)":{
'name': "Економічний розвиток (Economic Development-Kinakh)",
'color': '#666'
},
"Компартія України (Communist Party of Ukraine-Symonenko)":{
'name': "Компартія України (Communist Party of Ukraine-Symonenko)",
'color': '#BF0C0C'
},
"Партія регіонів (Party of Regions-Yanukovych-Azarov)":{
'name': "Партія регіонів (Party of Regions-Yanukovych-Azarov)",
'color': '#0047AB'
},
"Позафракційні (Independents)":{
'name' : "Позафракційні (Independents)",
'color': '#888'
},
"Свобода (Freedom-Tyahnybok)":{
'name': "Свобода (Freedom-Tyahnybok)",
'color': '#002072'
},
"Суверенна європейська Україна (Sovereign European Ukraine-Yeremeyev)":{
'name': "Суверенна європейська Україна (Sovereign European Ukraine-Yeremeyev)",
'color': '#777'
},
"УДАР Віталія Кличка (UDAR-Klitschko)":{
'name': "УДАР Віталія Кличка (UDAR-Klitschko)",
'color': '#f00'
}
}
mpnodes = {}
mp = {}
k = 0
with open("ua2012-2014.csv","r") as fin:
finreader = csv.reader(fin,delimiter=",")
for row in finreader:
try:
mp[row[0]]['row'] = row
except:
mp[row[0]] = {'row':row,'last_time':'2000'}
if int(row[1]) == 4212:
yearmonth = '2015'
else:
yearmonth = time.strftime("%Y",time.strptime(row[2],"%Y-%m-%d %H:%M"))
if yearmonth > mp[row[0]]['last_time']:
try:
mpnodes[row[0]][yearmonth] = row[5]
except:
mpnodes[row[0]] = {}
mpnodes[row[0]][yearmonth] = row[5]
mp[row[0]]['last_time'] = yearmonth
k = k + 1
# if k > 30000:
# break
# print mpnodes[row[0]]
# raise(Exception,'dipy')
print mpnodes[row[0]]
linksd = {}
nodesd = {}
j = 0
for key0 in mpnodes:
for key in mpnodes[key0]:
try:
nodesd[mpnodes[key0][key] + '-' + key]
except:
nodesd[mpnodes[key0][key] + '-' + key] = groups[mpnodes[key0][key]].copy()
nodesd[mpnodes[key0][key] + '-' + key]['j'] = j
j = j + 1
tmp = datetime.strptime(key,"%Y") + relativedelta(years=-1)
previous_month = tmp.strftime("%Y")
# print previous_month
# print key0
# print mpnodes[key0][key]
# if int(previous_month) > 2011:
# print str(previous_month)
# print key
# print mpnodes[key0][key]
# print mpnodes[key0][previous_month]
# sourcek = mpnodes[key0][previous_month]
# print "haha:" + sourcek
try:
mpnodes[key0][previous_month]
except:
nothing = 0
else:
sourcek = mpnodes[key0][previous_month] + '-' + previous_month
#print "hihi:" + sourcek
targetk = mpnodes[key0][key] + '-' + key
try:
nodesd[sourcek]
except:
nothing = 0
#print sourcek
else:
try:
linksd[sourcek]
except:
linksd[sourcek] = {}
try:
linksd[sourcek][targetk]
except:
linksd[sourcek][targetk] = {
'source': nodesd[sourcek]['j'],
'target': nodesd[targetk]['j'],
'value': 0
}
print "sourcek:"+sourcek
print "sourcej:"+str(nodesd[sourcek]['j'])
print "targetk:"+targetk
print "targetj:"+str(nodesd[targetk]['j'])
linksd[sourcek][targetk]['value'] = linksd[sourcek][targetk]['value'] + 1
#print nodesd
#print linksd
nodes = []
links = []
for key in nodesd:
nodes.append(nodesd[key])
for key0 in linksd:
for key in linksd[key0]:
links.append(linksd[key0][key])
print nodes
print links
sortednodes = sorted(nodes, key=lambda k: k['j'])
out = {
'nodes': sortednodes,
'links': links
}
with open('sankey.json', 'w') as outfile:
json.dump(out, outfile)
<!DOCTYPE html>
<meta charset="utf-8">
<title>Ukrainian Parliament 2012-2014/03</title>
<link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.0.2/css/bootstrap.min.css">
<style>
#chart {
height: 500px;
}
.node rect {
cursor: move;
fill-opacity: .9;
shape-rendering: crispEdges;
}
.node text {
pointer-events: none;
text-shadow: 0 1px 0 #fff;
}
.link {
fill: none;
stroke: #000;
stroke-opacity: .2;
}
.link:hover {
stroke-opacity: .5;
}
.axis path,
.axis line {
fill: none;
stroke: #000;
shape-rendering: crispEdges;
}
.tick {
display: none;
}
.axis text {
font-size: 30px;
color: #bbb;
font-family: sans-serif;
font-weight: bold;
}
h1 {
margin-bottom: 5px;
margin-top: 5px;
font-size: 1em;
}
</style>
<body>
<div class="navbar navbar-inverse navbar-fixed-top" role="navigation">
<div class="container">
<div class="navbar-header">
<a class="navbar-brand" href="#"><h1>Changes in Parliamentary groups in Ukrainian Parliament (Verkhovna Rada/Верховна Рада) (through 03/2014)</h1></a>
</div>
</div>
</div>
<h1>Changes in Parliamentary groups in Ukrainian Parliament (through 03/2014)</h1>
<div style="position:relative;top:35px;z-index:1000;">
<div class="alert alert-info" >
How Ukraninian MPs were changing political groups, particularly during the beginning of 2014.<br/>
The former majority (through 2013) had Party of Regions (Партія регіонів) and Communist Party (Компартія України)
</div>
</div>
<p id="chart"></p>
<script src="http://d3js.org/d3.v2.min.js?2.9.1"></script>
<script src="sankey.js"></script>
<script>
var margin = {top: 20, right: 1, bottom: 60, left: 1},
width = 960 - margin.left - margin.right,
height = 450 - margin.top - margin.bottom;
var formatNumber = d3.format(",.0f"),
format = function(d) { return formatNumber(d); },
color = function(d){ return d.color; };//d3.scale.category20();
var svg = d3.select("#chart").append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
var sankey = d3.sankey()
.nodeWidth(15)
.nodePadding(10)
.size([width, height]);
var path = sankey.link();
d3.json("sankey.json", function(energy) {
sankey
.nodes(energy.nodes)
.links(energy.links)
.layout(32);
var link = svg.append("g").selectAll(".link")
.data(energy.links)
.enter().append("path")
.attr("class", "link")
.attr("d", path)
.style("stroke-width", function(d) { return Math.max(1, d.dy); })
.sort(function(a, b) { return b.dy - a.dy; });
link.append("title")
.text(function(d) { return d.source.name + " → " + d.target.name + "\n" + format(d.value); });
var node = svg.append("g").selectAll(".node")
.data(energy.nodes)
.enter().append("g")
.attr("class", "node")
.attr("transform", function(d) { return "translate(" + d.x + "," + d.y + ")"; })
.call(d3.behavior.drag()
.origin(function(d) { return d; })
.on("dragstart", function() { this.parentNode.appendChild(this); })
.on("drag", dragmove));
node.append("rect")
.attr("height", function(d) { return d.dy; })
.attr("width", sankey.nodeWidth())
.style("fill", function(d) { return d.color; })
.style("stroke", function(d) { return d3.rgb(d.color).darker(2); })
.append("title")
.text(function(d) { return d.name + "\n" + format(d.value); });
node.append("text")
.attr("x", -6)
.attr("y", function(d) { return d.dy / 2; })
.attr("dy", ".35em")
.attr("text-anchor", "end")
.attr("transform", null)
.text(function(d) {
if (d.x > 900) return d.name; else return '';
})
.filter(function(d) { return d.x < width / 2; })
.attr("x", 6 + sankey.nodeWidth())
.attr("text-anchor", "start");
function dragmove(d) {
d3.select(this).attr("transform", "translate(" + d.x + "," + (d.y = Math.max(0, Math.min(height - d.dy, d3.event.y))) + ")");
sankey.relayout();
link.attr("d", path);
}
names = ['12/2012','01/2013','01/2014','04/2014'];
var x3 = d3.scale.ordinal()
.rangeRoundBands([-width/4/2+50, width+width/4/2-50])
.domain(names);
var xAxis3 = d3.svg.axis()
.scale(x3)
.orient("top");
bot = height + 2*margin.bottom/3;
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0,"+bot+")")
.call(xAxis3);
});
</script>
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-8592359-13', 'ocks.org');
ga('send', 'pageview');
</script>
d3.sankey = function() {
var sankey = {},
nodeWidth = 24,
nodePadding = 8,
size = [1, 1],
nodes = [],
links = [];
sankey.nodeWidth = function(_) {
if (!arguments.length) return nodeWidth;
nodeWidth = +_;
return sankey;
};
sankey.nodePadding = function(_) {
if (!arguments.length) return nodePadding;
nodePadding = +_;
return sankey;
};
sankey.nodes = function(_) {
if (!arguments.length) return nodes;
nodes = _;
return sankey;
};
sankey.links = function(_) {
if (!arguments.length) return links;
links = _;
return sankey;
};
sankey.size = function(_) {
if (!arguments.length) return size;
size = _;
return sankey;
};
sankey.layout = function(iterations) {
computeNodeLinks();
computeNodeValues();
computeNodeBreadths();
computeNodeDepths(iterations);
computeLinkDepths();
return sankey;
};
sankey.relayout = function() {
computeLinkDepths();
return sankey;
};
sankey.link = function() {
var curvature = .5;
function link(d) {
var x0 = d.source.x + d.source.dx,
x1 = d.target.x,
xi = d3.interpolateNumber(x0, x1),
x2 = xi(curvature),
x3 = xi(1 - curvature),
y0 = d.source.y + d.sy + d.dy / 2,
y1 = d.target.y + d.ty + d.dy / 2;
return "M" + x0 + "," + y0
+ "C" + x2 + "," + y0
+ " " + x3 + "," + y1
+ " " + x1 + "," + y1;
}
link.curvature = function(_) {
if (!arguments.length) return curvature;
curvature = +_;
return link;
};
return link;
};
// Populate the sourceLinks and targetLinks for each node.
// Also, if the source and target are not objects, assume they are indices.
function computeNodeLinks() {
nodes.forEach(function(node) {
node.sourceLinks = [];
node.targetLinks = [];
});
links.forEach(function(link) {
var source = link.source,
target = link.target;
if (typeof source === "number") source = link.source = nodes[link.source];
if (typeof target === "number") target = link.target = nodes[link.target];
source.sourceLinks.push(link);
target.targetLinks.push(link);
});
}
// Compute the value (size) of each node by summing the associated links.
function computeNodeValues() {
nodes.forEach(function(node) {
node.value = Math.max(
d3.sum(node.sourceLinks, value),
d3.sum(node.targetLinks, value)
);
});
}
// Iteratively assign the breadth (x-position) for each node.
// Nodes are assigned the maximum breadth of incoming neighbors plus one;
// nodes with no incoming links are assigned breadth zero, while
// nodes with no outgoing links are assigned the maximum breadth.
function computeNodeBreadths() {
var remainingNodes = nodes,
nextNodes,
x = 0;
while (remainingNodes.length) {
nextNodes = [];
remainingNodes.forEach(function(node) {
node.x = x;
node.dx = nodeWidth;
node.sourceLinks.forEach(function(link) {
nextNodes.push(link.target);
});
});
remainingNodes = nextNodes;
++x;
}
//
moveSinksRight(x);
scaleNodeBreadths((width - nodeWidth) / (x - 1));
}
function moveSourcesRight() {
nodes.forEach(function(node) {
if (!node.targetLinks.length) {
node.x = d3.min(node.sourceLinks, function(d) { return d.target.x; }) - 1;
}
});
}
function moveSinksRight(x) {
nodes.forEach(function(node) {
if (!node.sourceLinks.length) {
node.x = x - 1;
}
});
}
function scaleNodeBreadths(kx) {
nodes.forEach(function(node) {
node.x *= kx;
});
}
function computeNodeDepths(iterations) {
var nodesByBreadth = d3.nest()
.key(function(d) { return d.x; })
.sortKeys(d3.ascending)
.entries(nodes)
.map(function(d) { return d.values; });
//
initializeNodeDepth();
resolveCollisions();
for (var alpha = 1; iterations > 0; --iterations) {
relaxRightToLeft(alpha *= .99);
resolveCollisions();
relaxLeftToRight(alpha);
resolveCollisions();
}
function initializeNodeDepth() {
var ky = d3.min(nodesByBreadth, function(nodes) {
return (size[1] - (nodes.length - 1) * nodePadding) / d3.sum(nodes, value);
});
nodesByBreadth.forEach(function(nodes) {
nodes.forEach(function(node, i) {
node.y = i;
node.dy = node.value * ky;
});
});
links.forEach(function(link) {
link.dy = link.value * ky;
});
}
function relaxLeftToRight(alpha) {
nodesByBreadth.forEach(function(nodes, breadth) {
nodes.forEach(function(node) {
if (node.targetLinks.length) {
var y = d3.sum(node.targetLinks, weightedSource) / d3.sum(node.targetLinks, value);
node.y += (y - center(node)) * alpha;
}
});
});
function weightedSource(link) {
return center(link.source) * link.value;
}
}
function relaxRightToLeft(alpha) {
nodesByBreadth.slice().reverse().forEach(function(nodes) {
nodes.forEach(function(node) {
if (node.sourceLinks.length) {
var y = d3.sum(node.sourceLinks, weightedTarget) / d3.sum(node.sourceLinks, value);
node.y += (y - center(node)) * alpha;
}
});
});
function weightedTarget(link) {
return center(link.target) * link.value;
}
}
function resolveCollisions() {
nodesByBreadth.forEach(function(nodes) {
var node,
dy,
y0 = 0,
n = nodes.length,
i;
// Push any overlapping nodes down.
nodes.sort(ascendingDepth);
for (i = 0; i < n; ++i) {
node = nodes[i];
dy = y0 - node.y;
if (dy > 0) node.y += dy;
y0 = node.y + node.dy + nodePadding;
}
// If the bottommost node goes outside the bounds, push it back up.
dy = y0 - nodePadding - size[1];
if (dy > 0) {
y0 = node.y -= dy;
// Push any overlapping nodes back up.
for (i = n - 2; i >= 0; --i) {
node = nodes[i];
dy = node.y + node.dy + nodePadding - y0;
if (dy > 0) node.y -= dy;
y0 = node.y;
}
}
});
}
function ascendingDepth(a, b) {
return a.y - b.y;
}
}
function computeLinkDepths() {
nodes.forEach(function(node) {
node.sourceLinks.sort(ascendingTargetDepth);
node.targetLinks.sort(ascendingSourceDepth);
});
nodes.forEach(function(node) {
var sy = 0, ty = 0;
node.sourceLinks.forEach(function(link) {
link.sy = sy;
sy += link.dy;
});
node.targetLinks.forEach(function(link) {
link.ty = ty;
ty += link.dy;
});
});
function ascendingSourceDepth(a, b) {
return a.source.y - b.source.y;
}
function ascendingTargetDepth(a, b) {
return a.target.y - b.target.y;
}
}
function center(node) {
return node.y + node.dy / 2;
}
function value(link) {
return link.value;
}
return sankey;
};
{"nodes": [{"color": "#666", "j": 0, "name": "\u0415\u043a\u043e\u043d\u043e\u043c\u0456\u0447\u043d\u0438\u0439 \u0440\u043e\u0437\u0432\u0438\u0442\u043e\u043a (Economic Development-Kinakh)"}, {"color": "#0047AB", "j": 1, "name": "\u041f\u0430\u0440\u0442\u0456\u044f \u0440\u0435\u0433\u0456\u043e\u043d\u0456\u0432 (Party of Regions-Yanukovych-Azarov)"}, {"color": "#0047AB", "j": 2, "name": "\u041f\u0430\u0440\u0442\u0456\u044f \u0440\u0435\u0433\u0456\u043e\u043d\u0456\u0432 (Party of Regions-Yanukovych-Azarov)"}, {"color": "#0047AB", "j": 3, "name": "\u041f\u0430\u0440\u0442\u0456\u044f \u0440\u0435\u0433\u0456\u043e\u043d\u0456\u0432 (Party of Regions-Yanukovych-Azarov)"}, {"color": "#888", "j": 4, "name": "\u041f\u043e\u0437\u0430\u0444\u0440\u0430\u043a\u0446\u0456\u0439\u043d\u0456 (Independents)"}, {"color": "#BF0C0C", "j": 5, "name": "\u041a\u043e\u043c\u043f\u0430\u0440\u0442\u0456\u044f \u0423\u043a\u0440\u0430\u0457\u043d\u0438 (Communist Party of Ukraine-Symonenko)"}, {"color": "#BF0C0C", "j": 6, "name": "\u041a\u043e\u043c\u043f\u0430\u0440\u0442\u0456\u044f \u0423\u043a\u0440\u0430\u0457\u043d\u0438 (Communist Party of Ukraine-Symonenko)"}, {"color": "#BF0C0C", "j": 7, "name": "\u041a\u043e\u043c\u043f\u0430\u0440\u0442\u0456\u044f \u0423\u043a\u0440\u0430\u0457\u043d\u0438 (Communist Party of Ukraine-Symonenko)"}, {"color": "#BF0C0C", "j": 8, "name": "\u041a\u043e\u043c\u043f\u0430\u0440\u0442\u0456\u044f \u0423\u043a\u0440\u0430\u0457\u043d\u0438 (Communist Party of Ukraine-Symonenko)"}, {"color": "#0047AB", "j": 9, "name": "\u041f\u0430\u0440\u0442\u0456\u044f \u0440\u0435\u0433\u0456\u043e\u043d\u0456\u0432 (Party of Regions-Yanukovych-Azarov)"}, {"color": "#f00", "j": 10, "name": "\u0423\u0414\u0410\u0420 \u0412\u0456\u0442\u0430\u043b\u0456\u044f \u041a\u043b\u0438\u0447\u043a\u0430 (UDAR-Klitschko)"}, {"color": "#f00", "j": 11, "name": "\u0423\u0414\u0410\u0420 \u0412\u0456\u0442\u0430\u043b\u0456\u044f \u041a\u043b\u0438\u0447\u043a\u0430 (UDAR-Klitschko)"}, {"color": "#f00", "j": 12, "name": "\u0423\u0414\u0410\u0420 \u0412\u0456\u0442\u0430\u043b\u0456\u044f \u041a\u043b\u0438\u0447\u043a\u0430 (UDAR-Klitschko)"}, {"color": "#f00", "j": 13, "name": "\u0423\u0414\u0410\u0420 \u0412\u0456\u0442\u0430\u043b\u0456\u044f \u041a\u043b\u0438\u0447\u043a\u0430 (UDAR-Klitschko)"}, {"color": "#777", "j": 14, "name": "\u0421\u0443\u0432\u0435\u0440\u0435\u043d\u043d\u0430 \u0454\u0432\u0440\u043e\u043f\u0435\u0439\u0441\u044c\u043a\u0430 \u0423\u043a\u0440\u0430\u0457\u043d\u0430 (Sovereign European Ukraine-Yeremeyev)"}, {"color": "#DC143C", "j": 15, "name": "\u0411\u0430\u0442\u044c\u043a\u0456\u0432\u0449\u0438\u043d\u0430 (Fatherland-Tymoshenko)"}, {"color": "#DC143C", "j": 16, "name": "\u0411\u0430\u0442\u044c\u043a\u0456\u0432\u0449\u0438\u043d\u0430 (Fatherland-Tymoshenko)"}, {"color": "#DC143C", "j": 17, "name": "\u0411\u0430\u0442\u044c\u043a\u0456\u0432\u0449\u0438\u043d\u0430 (Fatherland-Tymoshenko)"}, {"color": "#DC143C", "j": 18, "name": "\u0411\u0430\u0442\u044c\u043a\u0456\u0432\u0449\u0438\u043d\u0430 (Fatherland-Tymoshenko)"}, {"color": "#888", "j": 19, "name": "\u041f\u043e\u0437\u0430\u0444\u0440\u0430\u043a\u0446\u0456\u0439\u043d\u0456 (Independents)"}, {"color": "#002072", "j": 20, "name": "\u0421\u0432\u043e\u0431\u043e\u0434\u0430 (Freedom-Tyahnybok)"}, {"color": "#002072", "j": 21, "name": "\u0421\u0432\u043e\u0431\u043e\u0434\u0430 (Freedom-Tyahnybok)"}, {"color": "#002072", "j": 22, "name": "\u0421\u0432\u043e\u0431\u043e\u0434\u0430 (Freedom-Tyahnybok)"}, {"color": "#002072", "j": 23, "name": "\u0421\u0432\u043e\u0431\u043e\u0434\u0430 (Freedom-Tyahnybok)"}, {"color": "#888", "j": 24, "name": "\u041f\u043e\u0437\u0430\u0444\u0440\u0430\u043a\u0446\u0456\u0439\u043d\u0456 (Independents)"}, {"color": "#888", "j": 25, "name": "\u041f\u043e\u0437\u0430\u0444\u0440\u0430\u043a\u0446\u0456\u0439\u043d\u0456 (Independents)"}], "links": [{"source": 13, "target": 12, "value": 41}, {"source": 12, "target": 11, "value": 41}, {"source": 1, "target": 9, "value": 119}, {"source": 1, "target": 4, "value": 29}, {"source": 1, "target": 0, "value": 31}, {"source": 1, "target": 14, "value": 24}, {"source": 3, "target": 25, "value": 1}, {"source": 3, "target": 2, "value": 205}, {"source": 2, "target": 1, "value": 201}, {"source": 2, "target": 24, "value": 4}, {"source": 11, "target": 10, "value": 41}, {"source": 17, "target": 24, "value": 10}, {"source": 17, "target": 16, "value": 89}, {"source": 21, "target": 20, "value": 32}, {"source": 18, "target": 17, "value": 98}, {"source": 7, "target": 6, "value": 30}, {"source": 25, "target": 1, "value": 2}, {"source": 25, "target": 24, "value": 26}, {"source": 25, "target": 6, "value": 1}, {"source": 19, "target": 25, "value": 24}, {"source": 24, "target": 15, "value": 2}, {"source": 24, "target": 4, "value": 27}, {"source": 24, "target": 9, "value": 1}, {"source": 24, "target": 0, "value": 3}, {"source": 24, "target": 14, "value": 12}, {"source": 6, "target": 5, "value": 31}, {"source": 22, "target": 21, "value": 35}, {"source": 16, "target": 4, "value": 3}, {"source": 16, "target": 15, "value": 84}, {"source": 23, "target": 25, "value": 1}, {"source": 23, "target": 22, "value": 35}, {"source": 8, "target": 7, "value": 30}, {"source": 8, "target": 25, "value": 1}]}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment