-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.py
More file actions
79 lines (65 loc) · 2.4 KB
/
util.py
File metadata and controls
79 lines (65 loc) · 2.4 KB
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
import collections
def display_polygon(poly):
from matplotlib import pyplot
from matplotlib.patches import Circle
from shapely.geometry import Polygon
from descartes.patch import PolygonPatch
def plot_coords(ax, ob):
x, y = ob.xy
ax.plot(x, y, 'o', color='#999999', zorder=1)
fig = pyplot.figure(1, figsize=[5, 5], dpi=90)
ax = fig.add_subplot(111)
for poly in explode_poly(poly):
for inter in poly.interiors:
plot_coords(ax, inter)
plot_coords(ax, poly.exterior)
patch = PolygonPatch(poly, facecolor='#888888', edgecolor='#222222')
ax.add_patch(patch)
#ax.set_aspect(1)
pyplot.show()
def render_index(decomp, depth):
import process_data as pd
full, partial = decomp
dim = 2**depth
BITMAP = [[0 for i in xrange(dim)] for j in xrange(dim)]
def paint(ix, val):
z, x, y = 0, 0, 0
for k in ix:
k = int(k)
z += 1
x = 2*x + k % 2
y = 2*y + k // 2
c = {0: 0, 1: 128, 2: 255}[val]
celldim = 2**(depth - z)
for i in xrange(celldim):
for j in xrange(celldim):
BITMAP[y*celldim + j][x*celldim + i] = c
for ix in full:
paint(ix, pd.OVERLAP_FULL)
for ix in partial.keys():
paint(ix, pd.OVERLAP_PARTIAL)
import os
import tempfile
raw = tempfile.mktemp('.grey')
img = tempfile.mktemp('.png')
with open(raw, 'w') as f:
f.write(''.join(''.join(chr(col) for col in row) for row in BITMAP))
os.popen('convert -size %dx%d -depth 8 gray:%s %s' % (dim, dim, raw, img))
os.popen('gnome-open %s' % img)
def map_reduce(data, emitfunc=lambda rec: [(rec,)], reducefunc=lambda v: v):
"""perform a "map-reduce" on the data
emitfunc(datum): return an iterable of key-value pairings as (key, value). alternatively, may
simply emit (key,) (useful for reducefunc=len)
reducefunc(values): applied to each list of values with the same key; defaults to just
returning the list
data: iterable of data to operate on
"""
mapped = collections.defaultdict(list)
for rec in data:
for emission in emitfunc(rec):
try:
k, v = emission
except ValueError:
k, v = emission[0], None
mapped[k].append(v)
return dict((k, reducefunc(v)) for k, v in mapped.iteritems())