block by michalskop 8290471

CZ Presidential Elections '13: Voters' transfers (research)

Full Screen

index.html

<!DOCTYPE html>
<meta charset="utf-8">
<title>Voters transfers during Czech presidential elections 2013</title>
<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;
}

</style>
<body>
<link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.0.2/css/bootstrap.min.css">
<div class="navbar navbar-inverse navbar-fixed-top" role="navigation">
      <div class="container">
        <div class="navbar-header">
          <a class="navbar-brand" href="#">Voters transfers during Czech presidential elections 2013</a>
        </div>
      </div>
    </div>



<div style="position:fixed;top:50px;z-index:1000;">
      <div class="alert alert-info" >The data is from <a href="//volebnikalkulacka.cz/volba-prezidenta-cr-2013/kalkulacka-vyzkum.php">research</a> conducted among users of voting advice application <a href="//volebnikalkulacka.cz">Volební kalkulačka</a>. It is weighted so the 1st round of the real elections and the 2nd round of the real elections match the 1st and last round of the potentional <a href="//en.wikipedia.org/wiki/Instant-runoff_voting">instant-runoff voting (alternative vote) electoral system</a>.</div>
</div>
<div style="padding-bottom:130px"></div>
<p id="chart"></p>

<p>The analysis flow: 
<ol><li>get weights from <a href="//bl.ocks.org/michalskop/8155694/">this bl.ock</a> (compare)</li>
<li>extract_everything_weighted.py -> responses_weighted_info.csv</li>
<li>cal3_2.py -> sankey2.json</li>
</ol>
</p>

<script src="//d3js.org/d3.v2.min.js?2.9.1"></script>
<script src="sankey.js"></script>
<script>

var margin = {top: 1, right: 1, bottom: 6, left: 1},
    width = 600 - margin.left - margin.right,
    height = 350 - 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("sankey2.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/11525*100) + ' %'; });

  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/11525*100) + ' %'; });

  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) { return d.name; })
    .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);
  }
});

</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>

calc3_2.py

# -*- coding: utf-8 -*-

# calculates the transfers, 2 rounds
# using weights from R

import json
import csv

# read into data
i = 0
data = []
with open("responses_weighted_info.csv","r") as fin:
  finreader = csv.reader(fin)
  for row in finreader:
    #print i
    if i == 0:
      keys = row
    else:
      j = 0
      val = {}
      for item in row:
        val[keys[j]] = item
        j = j + 1
      data.append(val)
      
      #print data
      #raise Exception()
    i = i + 1
    

people = {
  "zeman": {"name":"Zeman","color":"#FF0000"},
  "schwarzenberg":{"name":"Schwarzenberg","color":"#880088"},
  "fischer":{"name":"Fischer","color":"#0000FF"},
  "dienstbier":{"name":"Dienstbier","color":"#FFA500"},
  "franz":{"name":"Franz","color":"#000000"},
  "roithova":{"name":"Roithová","color":"#FFFF00"},
  "fischerova":{"name":"Fischerová","color":"#00AA00"},
  "sobotka":{"name":"Sobotka","color":"#000088"},
  "bobosikova":{"name":"Bobošíková","color":"#FFFF44"}
}

# rounds
r1 = {}
r2 = {}
for jsonvar in data:
  for key in people:
    #print key
    if (int(jsonvar[key]) == 1):
      try:
        r1[key]
      except:
        r1[key] = float(jsonvar['weight'])
      else:
        r1[key] = r1[key] + float(jsonvar['weight'])
      
      if (int(jsonvar['zeman']) < int(jsonvar['schwarzenberg'])):
        name = 'zeman'
      else:
        name = 'schwarzenberg'
      
      try:
        r2[key]
      except:
        r2[key] = {}
      try:
        r2[key][name]
      except:
        r2[key][name] = float(jsonvar['weight'])
      else:
        r2[key][name] = r2[key][name] + float(jsonvar['weight']) 


#print r2

out = {}
out["nodes"] = []
out["links"] = []
j = 0
toj1 = {}
toj2 = {'zeman':9, 'schwarzenberg': 10}
for item in r2:
  toj1[item] = j
  j = j + 1
for item in r2:
  for item2 in r2[item]:
    out["links"].append({"source":toj1[item],"target":toj2[item2],"value":r2[item][item2]})
  node = people[item]
  node['j'] = toj1[item]  
  out["nodes"].append(node)
print out['nodes']
# the following did not work as I expected, so I had to correct the output file by hand:
for item2 in toj2:
  node = people[item2]
  node['j'] = toj2[item2]
  print node
  out["nodes"].append(node)
print out['nodes']
with open('sankey2.json', 'w') as outfile:
  json.dump(out, outfile)  

extract_everything_weighted.py

# -*- coding: utf-8 -*-

# extracts inputs and add weights
# only the correct ones

import json
import pickle
import collections
#fin = open('workfile.html', 'w')

# get weights
j = 0
weights = {}
with open("weights.csv","r") as fw:
  for wrow in fw:
    if (j>0):
      war = wrow.split("\t")
      weights[war[0].strip('"')] = {"zeman":war[1],"schwarzenberg":war[2].rstrip()}
    j = j + 1  
    

responses = []
i = 0

def one_input(jsonvar,w):
  try:
    jsonvar[w]
  except:
    return ""
  else:
    return jsonvar[w].encode("utf-8")

inputs = [
  "input-attend",
  "input-previously",
  "input-change_mind",
  "input-change_result",
  "input-looking_for_info",
  "input-info_from",
  "input-info_from-other",
  "input-negative_vote",
  "input-sex",
  "input-occupation",
  "input-age",
  "input-place",
]

with open("research.txt","r") as fin:
  for row in fin:
    ar = row.split("\t")
    #print(ar)
    jsonvar = json.loads(ar[3])
    newvar = {}
    newvar["order"] = {}
    try:
        if "input-sort-bobosikova" in jsonvar:
          newvar["order"]["bobosikova"] = int(jsonvar["input-sort-bobosikova"])
          if "input-sort-dienstbier" in jsonvar:
            newvar["order"]["dienstbier"] = int(jsonvar["input-sort-dienstbier"])
            if "input-sort-fischer" in jsonvar:
              newvar["order"]["fischer"] = int(jsonvar["input-sort-fischer"])
              if "input-sort-fischerova" in jsonvar:
                newvar["order"]["fischerova"] = int(jsonvar["input-sort-fischerova"])
                if "input-sort-franz" in jsonvar:
                  newvar["order"]["franz"] = int(jsonvar["input-sort-franz"])
                  if "input-sort-roithova" in jsonvar:
                    newvar["order"]["roithova"] = int(jsonvar["input-sort-roithova"])
                    if "input-sort-schwarzenberg" in jsonvar:
                      newvar["order"]["schwarzenberg"] = int(jsonvar["input-sort-schwarzenberg"])
                      if "input-sort-sobotka" in jsonvar:
                        newvar["order"]["sobotka"] = int(jsonvar["input-sort-sobotka"])
                        if "input-sort-zeman" in jsonvar:
                          newvar["order"]["zeman"] = int(jsonvar["input-sort-zeman"])
                          cc = 0
                          for n in range(1, 10):
                            if n in newvar["order"].values():
                              cc = cc + 1
                          if (cc == 9):
                            minikey = min(newvar["order"], key=newvar["order"].get)
                            if (newvar["order"]['zeman'] < newvar["order"]['schwarzenberg']):
                              newvar["weight"] = weights[minikey]['zeman']
                            else:
                              newvar["weight"] = weights[minikey]['schwarzenberg']
                            # other info:
                            info = {}
                            for iinput in inputs:
                              info[iinput] = one_input(jsonvar,iinput)
                            #print info
                            #raise Exception()
                            newvar["info"] = info
                            
                            newvar["session"] = ar[0]
                            newvar["date"] = ar[2]
                              
                            responses.append(newvar)
                            i = i + 1
    except:
      nothing = 1
      
print (i)      
with open("responsesw", 'wb') as f:
  pickle.dump(responses, f)

writer = csv.writer(open('responses_weighted_info.csv', 'wb'))
vals = []
vals.append("weight")
vals.append("session")
vals.append("date")

k = 0
for jsonvar in responses:
  if (k == 0):
    print sorted(jsonvar["order"])
    for key in sorted(jsonvar["order"]):
      vals.append(key)
    for key in sorted(jsonvar["info"]):
      vals.append(key)
    writer.writerow(vals)
  vals = []
  vals.append(jsonvar["weight"])
  vals.append(jsonvar["session"])
  vals.append(jsonvar["date"])
  for key in sorted(jsonvar["order"]):
    vals.append(jsonvar["order"][key])
  for key in sorted(jsonvar["info"]):
    vals.append(jsonvar["info"][key])
  writer.writerow(vals)
  k = k + 1
      #print(responses)
      #raise Exception('diePy')

sankey.js

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;
};

sankey2.json

{"nodes": [{"color": "#FF0000", "j": 0, "name": "Zeman"}, {"color": "#0000FF", "j": 1, "name": "Fischer"}, {"color": "#000088", "j": 2, "name": "Sobotka"}, {"color": "#00AA00", "j": 3, "name": "Fischerov\u00e1"}, {"color": "#880088", "j": 4, "name": "Schwarzenberg"}, {"color": "#FFA500", "j": 5, "name": "Dienstbier"}, {"color": "#FFFF00", "j": 6, "name": "Roithov\u00e1"}, {"color": "#FFFF44", "j": 7, "name": "Bobo\u0161\u00edkov\u00e1"}, {"color": "#000000", "j": 8, "name": "Franz"}, {"color": "#FF0000", "j": 9, "name": "Zeman"}, {"color": "#880088", "j": 10, "name": "Schwarzenberg"}], "links": [{"source": 0, "target": 9, "value": 2790.5571821537496}, {"source": 1, "target": 10, "value": 1047.5224641621242}, {"source": 1, "target": 9, "value": 821.216577535129}, {"source": 2, "target": 9, "value": 124.29282014560727}, {"source": 2, "target": 10, "value": 201.01950443805762}, {"source": 3, "target": 10, "value": 223.2901354058806}, {"source": 3, "target": 9, "value": 151.02618457187057}, {"source": 4, "target": 10, "value": 2683.7191693245986}, {"source": 5, "target": 10, "value": 317.3477186395925}, {"source": 5, "target": 9, "value": 1511.133203338977}, {"source": 6, "target": 9, "value": 264.6438013542945}, {"source": 6, "target": 10, "value": 293.5786808489737}, {"source": 7, "target": 9, "value": 181.63681352259297}, {"source": 7, "target": 10, "value": 81.02099977453234}, {"source": 8, "target": 9, "value": 443.06499016264905}, {"source": 8, "target": 10, "value": 337.3208891152333}]}