-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhist.py
148 lines (132 loc) · 4.79 KB
/
hist.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
"""Quick histogram."""
import argparse
import numpy
import psycopg2.extras
import matplotlib.pyplot as plt
import cityism.config
# I should just get the colorbrewers library
COLORS = {
'divergent_10': [
'#b32448', # The default is a little too strong for my tastes. '#9e0142'
'#d53e4f',
'#f46d43',
'#fdae61',
'#fee08b',
'#e6f598',
'#abdda4',
'#66c2a5',
'#3288bd',
'#5e4fa2'
],
'sequential_9': [
'#ffffd9',
'#edf8b1',
'#c7e9b4',
'#7fcdbb',
'#41b6c4',
'#1d91c0',
'#225ea8',
'#253494',
'#081d58'
],
'sequential_5': [
'#f1eef6',
'#bdc9e1',
'#74a9cf',
'#2b8cbe',
'#045a8d'
]
}
def filterkey(items, key, minvalue=None):
"""Filter objects by key"""
if minvalue is not None:
return [i for i in items if i.get(key) > minvalue]
return [i for i in items if i.get(key)]
def histogram(values, bins=50, weights=None, density=False):
"""Display matpotlib histogram."""
values = filter(None, values)
hist, bins = numpy.histogram(values, bins=bins, weights=weights, density=density)
width = 0.7 * (bins[1] - bins[0])
center = (bins[:-1] + bins[1:]) / 2
plt.bar(center, hist, align='center', width=width)
plt.show()
def histocarto(bins, key, colors):
"""Generate CartoCSS useful for Tilemill. And a legend."""
fmt_cartocss = """ [%(key)s > %(bmin)s]{ polygon-fill: %(color)s; line-color: %(color)s; }"""
fmt_legend = """ <li><span style="background:%(color)s;"></span>%(min)s</li>"""
for b, color in zip(bins, colors):
print fmt_cartocss%{
'key': key,
'bmin': b[1],
'bmax': b[2],
'color': color
}
for b, color in zip(bins, colors):
print fmt_legend%{
'color': color,
'min': round(b[1], 2)
}
def binstats(bins, key, metric):
total_bin = sum(i[3] for i in bins)
total = sum([i.get(metric) for i in items])
print "Total bins: %s"%len(bins)
print "Bin metric total:", total_bin
print "Difference:", total - total_bin
for bin in bins:
print "min:", bin[1], "max:", bin[2], "pop:", sum(i.get('pop') for i in bin[5]), "aland:", sum(i.get('aland') for i in bin[5])/1e6
def breaks(items, key='hdi', metric='pop', count=10):
"""Break a list of dicts into ranges."""
# TODO: Use numpy.interpolate
# This function is somewhat buggy and needs work :(
# I should ask a real statistician how to do this.
# numpy has a great percentile method, but doesn't do weights.
items = filterkey(items, key, minvalue=0)
items = filterkey(items, metric, minvalue=0)
total = sum([i.get(metric) for i in items])
width = total / float(count)
# Don't ask...
# I can choose to have the highest or the lowest decile
# more closely match the desired width. In most cases,
# I'm more interested in the highest decile -- so, reverse.
i = 0
bins = []
items = sorted(items, key=lambda x:x.get(key), reverse=True)
for c in range(count-1, -1, -1):
refs = []
t = 0
start = items[i].get(key)
while t <= width:
t += items[i].get(metric)
refs.append(items[i])
if i+1 >= len(items):
break
i += 1
end = items[i].get(key)
bin = (c, end, start, t, t/width, refs)
bins.append(bin)
bins.reverse()
return bins
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("--bins", help="Bins", default=10, type=int)
parser.add_argument("--colors", help="Color scheme", default="divergent_10")
parser.add_argument("--mult", help="Key factor (e.g. convert km^2 density to mi^2 = 2.58999)", default=1.0, type=float)
parser.add_argument("--key", help="Histogram column", default='hdi')
parser.add_argument("--outkey", help="Output key", default=None)
parser.add_argument("--metric", help="Bin metric", default='pop')
args = parser.parse_args()
args.outkey = args.outkey or args.key
items = []
with cityism.config.connect() as conn:
with conn.cursor(cursor_factory=psycopg2.extras.DictCursor) as cursor:
query = """SELECT * FROM result_hdi;"""
cursor.execute(query)
for row in cursor:
row = dict(row)
row.pop('geom', None)
if row.get(args.key):
row[args.key] = row[args.key] * args.mult
items.append(row)
bins = breaks(items, count=args.bins, key=args.key, metric=args.metric)
binstats(bins=bins, key=args.key, metric=args.metric)
histocarto(bins=bins, key=args.outkey, colors=COLORS[args.colors])