Skip to content

Instantly share code, notes, and snippets.

@michalskop
michalskop / csv2json_sankey.py
Last active January 4, 2016 01:49
Senate Elections 2014, Zlín; Ecological Inference using Regression
# -*- coding: utf-8 -*-
# creates json for sankey from matrix row->column
# 10 -> 3
import json
import csv
# read into data
out = {}
@michalskop
michalskop / csv2json.php
Last active January 3, 2016 23:19
Senate Elections 2014, Zlín, 2nd Round
<?php
//join election results with list of all stations and output lat,lng,results into json
// we want to get such structure (similar):
// {"name": "\u00dast\u00ed nad Labem-0", "winner": "Zeman", "coordinates": ["14.036975", "50.660316"], "id": "554804-0", "population": {"p9": "16025", "p6": "21011", etc. }, "party" : {"p1": "Úsvit", etc. }, "names": ["p1": "Talaš ..", etc.}},
$handle_results = fopen('zlin_stations.csv','r');
$handle_geo = fopen('okrsky_geo_adj.csv','r');
$handle_out = fopen('zlin.json','w');
// read results
@michalskop
michalskop / index.html
Last active January 3, 2016 20:29
Weighted PCA Model in R
We couldn’t find that file to show.
@michalskop
michalskop / csv2json.php
Last active January 2, 2016 23:59
CZ Senate Elections; 2014 (Winners in Polling Station)
<?php
//join election results with list of all stations and output lat,lng,results into json
// we want to get such structure (similar):
// {"name": "\u00dast\u00ed nad Labem-0", "winner": "Zeman", "coordinates": ["14.036975", "50.660316"], "id": "554804-0", "population": {"p9": "16025", "p6": "21011", etc. }, "party" : {"p1": "Úsvit", etc. }, "names": ["p1": "Talaš ..", etc.}},
$handle_results = fopen('zlin_stations.csv','r');
$handle_geo = fopen('okrsky_geo_adj.csv','r');
$handle_out = fopen('zlin.json','w');
// read results
@michalskop
michalskop / calc4.py
Last active January 2, 2016 13:19
CZ Presidential Elections: 4+ and 2- votes (stacked chart)
# -*- coding: utf-8 -*-
#
#
import pickle
import csv
import json
with open("responsesw", 'rb') as f:
@michalskop
michalskop / calc3_2.py
Last active January 2, 2016 10:29
CZ Presidential Elections '13: Voters' transfers (research)
# -*- coding: utf-8 -*-
# calculates the transfers, 2 rounds
# using weights from R
import json
import csv
# read into data
i = 0
@michalskop
michalskop / cz_president_2013_okrsky_2_ring.json
Last active January 2, 2016 08:39
CZ president. elections '13: Each Polling Station (color saturation)
This file has been truncated, but you can view the full file.
[{"name": "\u00dast\u00ed nad Labem-0", "winner": "Zeman", "coordinates": ["14.036975", "50.660316"], "classname": "zeman", "id": "554804-0", "population": {"p9": "16025", "p6": "21011"}}, {"name": "Praha 3-0", "winner": "Schwarzenberg", "coordinates": ["14.454167", "50.084444"], "classname": "schwarzenberg", "id": "500097-0", "population": {"p9": "21749", "p6": "10244"}}, {"name": "Opava-0", "winner": "Zeman", "coordinates": ["17.90251", "49.938873"], "classname": "zeman", "id": "505927-0", "population": {"p9": "11415", "p6": "15536"}}, {"name": "Plze\u0148 1-0", "winner": "Schwarzenberg", "coordinates": ["13.3714008", "49.7669356"], "classname": "schwarzenberg", "id": "545970-0", "population": {"p9": "11984", "p6": "11443"}}, {"name": "Brno-sever-0", "winner": "Schwarzenberg", "coordinates": ["16.627121", "49.214233"], "classname": "schwarzenberg", "id": "551031-0", "population": {"p9": "12400", "p6": "9926"}}, {"name": "Praha 1-0", "winner": "Schwarzenberg", "coordinates": ["14.411111", "50.086389"], "clas
@michalskop
michalskop / csv2json.py
Last active January 2, 2016 08:28
CZ Presidential Elections '13: Each Polling Station
# -*- coding: utf-8 -*-
import json
import csv
import string
data = []
i = 0
with open("okrsky_geo_adj.csv","r") as fin:
finreader = csv.reader(fin)
@michalskop
michalskop / calc2.py
Last active January 1, 2016 14:09
CZ presidential election 2013: instant runoff voting
# -*- coding: utf-8 -*-
# calculates 18 groups
import pickle
import csv
with open("responses0", 'rb') as f:
responses = pickle.load(f)
@michalskop
michalskop / cl_2013_2_ring_bachelet_matthei.json
Last active January 1, 2016 00:19
CL Presidential Elections 2013: 2nd round, Metropolitan area
{"features":[{"id":"1207","winner":"Bachelet","classname":"bachelet","population":{"p6":"69751","p9":"37854"},"coordinates":["-70.5666667","-33.6166667"],"name":"PUENTE ALTO"},{"id":"1160","winner":"Bachelet","classname":"bachelet","population":{"p6":"55859","p9":"34120"},"coordinates":["-70.7666667","-33.5166667"],"name":"MAIPU"},{"id":"1276","winner":"Bachelet","classname":"bachelet","population":{"p6":"50000","p9":"24209"},"coordinates":["-71.65729","-35.430647"],"name":"TALCA"},{"id":"1211","winner":"Bachelet","classname":"bachelet","population":{"p6":"47733","p9":"27729"},"coordinates":["-70.7","-33.5833333"],"name":"SAN BERNARDO"},{"id":"1226","winner":"Bachelet","classname":"bachelet","population":{"p6":"38966","p9":"23653"},"coordinates":["-70.738965","-34.168052"],"name":"RANCAGUA"},{"id":"1307","winner":"Bachelet","classname":"bachelet","population":{"p6":"38291","p9":"22906"},"coordinates":["-72.102423","-36.609354"],"name":"CHILLAN"},{"id":"1425","winner":"Bachelet","classname":"bachelet","populat