Skip to content

Instantly share code, notes, and snippets.

View gka's full-sized avatar
🐢

Gregor Aisch gka

🐢
View GitHub Profile
@gka
gka / contr-all.r
Created February 13, 2012 11:23
plots all contributions over time, colored by party
# load csv
contr <- read.csv("fec-contr.csv")
# convert date column to R date type
contr$date <- strptime(as.character(contr$contb_receipt_dt), "%Y-%m-%dT%H:%M:%SZ")
plot(date,contb_receipt_amt,axes=F,col=color,cex=.7,ylab="",xlab="",main="Contributions in 2011")
# add time axis
axis(2, las=2, cex.axis=0.65)
axis.POSIXct(1, las=2, at=seq(as.Date("2011/1/1"), as.Date("2012/01/01"), by="month"), cex=0.65, cex.axis=0.65, format="%b")
@gka
gka / contr-comm.r
Created February 13, 2012 11:25
plots all contributions by authorized committees over time, colored by party
# you need to run https://gist.github.com/1816161 first
comm = subset(contr, form_tp=="SA18")
plot(comm$date, comm$contb_receipt_amt, log='y', col=comm$color, axes=F, xlab="", ylab="", main="Contributions by Authorized Committees (log scale)", cex=.65)
axis.POSIXct(1, las=2, at=seq(min(comm$date), as.POSIXct("2012/01/02"), by="month"), cex=0.65, cex.axis=0.65, format="%b")
options(scipen=10)
@gka
gka / ucsv.py
Created March 3, 2012 10:57
unicode pimped csv
import csv, codecs, cStringIO
class UTF8Recoder:
"""
Iterator that reads an encoded stream and reencodes the input to UTF-8
"""
def __init__(self, f, encoding):
self.reader = codecs.getreader(encoding)(f)
def __iter__(self):
@gka
gka / readme.md
Created March 19, 2012 20:23
API proposal for a simple force-directed bubble chart library

Force-directed Bubble Charts

Proposal for a simple force-directed bubble chart library. The visualization would show a set of bubbles which try to move towards a (definable) center of gravity. The library would try to render the bubbles using whatever technology is supported by the client (canvas, svg, flash).

var items = [
   { amount: 12345.67, label: "Foo", id: "foo", color: "#345" }, 
   /* ... */
];
@gka
gka / a.py
Created March 20, 2012 11:51
BeautifulSoup4 test run
from bs4 import BeautifulSoup
import requests
url = "https://www.cia.gov/library/publications/the-world-factbook/rankorder/2119rank.html"
s = BeautifulSoup(requests.get(url).text)
for o in s.find(id='countryCode').find_all('option'):
code = o.get('value')
name = o.get_text()
print code, name
@gka
gka / map2pdf.js
Created March 22, 2012 18:44
Rendering a Kartograph map to PDF using PhantomJS
var page = new WebPage(),
url = "http://kartograph.org/showcase/italia/",
output = "italia.pdf";
page.viewportSize = { width: 1100, height: 750 };
page.open(url, function (status) {
if (status !== 'success') {
console.log('Unable to load the address!');
} else {
@gka
gka / readme.md
Created March 30, 2012 22:01
API for dead simple line charts (based on raphael and d3)
var data = { 
   a: [1,2,3,2,2,3,4,5,6],
   b: [5,3,2,3,5,null,2,1,1,2]
};

linechart.create({
   container: '#chart',
   size: [500,300],
 data: data, // lots of numbers
@gka
gka / alfred-script.sh
Created April 10, 2012 08:49
Local full-text search on tweets and favorites
cd /your/local/path/to/tweet-search/
rm last-results.html
search.py {query}
open last-results.html
@gka
gka / kartograph-api.py
Created May 15, 2012 12:26
Kartograph web.py API
#!/usr/bin/env python
import web
import json
from kartograph.kartograph import Kartograph
urls = (
'/1.0/svg/', 'GenerateSVG',
)
app = web.application(urls, globals())
@gka
gka / birth-transposed-2.csv
Created May 19, 2012 14:34
Daily Births Per Month in Germany, normalized by birth per day over the full year.
We can make this file beautiful and searchable if this error is corrected: It looks like row 7 should actually have 14 columns, instead of 11. in line 6.
Year,Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec,Total
1950,3091.48387097,3266.96428571,3325.90322581,3187.86666667,3160.58064516,3078.7,2958.09677419,2885.80645161,3056.83333333,2943.70967742,2912.8,2864.74193548,3059.45479452
1951,3029.5483871,3181.71428571,3210.74193548,3142.96666667,3196.41935484,3087.1,3017.0,2929.35483871,3022.56666667,2884.61290323,2806.4,2878.16129032,3031.17808219
1952,3072.0,3223.24137931,3268.4516129,3181.03333333,3115.93548387,2994.03333333,2973.5483871,2865.29032258,3070.0,2807.74193548,2860.5,2814.5483871,3027.62739726
1953,3052.90322581,3175.96428571,3248.19354839,3163.06666667,3142.83870968,3039.6,2975.5483871,2856.61290323,3061.5,2774.12903226,2679.63333333,2846.16129032,3000.07945205
1954,3074.06451613,3238.64285714,3172.93548387,3180.3,3243.35483871,3096.2,3021.51612903,2930.4516129,3054.9,2781.06451613,2848.86666667,2862.19354839,3040.39178082
1955,3058.03225806,3235.96428571,3286.93548387,3178.4,3151.77419355,3104.63333333,2988.51612903,2933.41935484,3064.06666667,287