Skip to content

Instantly share code, notes, and snippets.

@jampekka
Created April 2, 2017 20:56
Show Gist options
  • Save jampekka/9c5e0697bc3114bcc01c6f1d19c8539f to your computer and use it in GitHub Desktop.
Save jampekka/9c5e0697bc3114bcc01c6f1d19c8539f to your computer and use it in GitHub Desktop.
/r/place activity animated heatmap
import numpy as np
import matplotlib.pyplot as plt
import scipy.stats
import pyximport; pyximport.install()
from pixcount import pixcount
import scipy.ndimage
data = np.load('diffs.npy')
ts = data[:,0]
frames = 1000
bins = np.linspace(ts[0], ts[-1], frames)
framing = np.digitize(data[:,0], bins=bins)
def transform(counts):
return np.log10(counts + 1.0)
import arrow
image = np.zeros((1000, 1000), dtype=np.float)
tmpimg = np.zeros((1000, 1000), dtype=np.int)
for idx in np.unique(framing):
print idx
frame = data[framing == idx]
duration = np.max(frame[:,1]) - np.min(frame[:,0])
time = np.mean(frame[:,1])
tmpimg[:] = 0
pixcount(frame[:,2:], tmpimg)
if duration > 0:
tmpimg *= 60*60/duration
tmp = transform(tmpimg.astype(np.float))
w = 0.05
image *= (1 - w)
image += w*tmp
plt.clf()
plt.title(arrow.get(time))
tile = np.rot90(image)
tile = tile[430:630,0:200]
#plt.imshow(tile, origin='lower', cmap='hot',
# vmin=0.02, vmax=0.3
# )
plt.imsave("frames/%05i.png"%idx, np.rot90(image), origin='lower', cmap='hot',
vmin=0.02, vmax=0.3
)
#plt.colorbar()
#plt.draw()
#plt.pause(0.1)
#plt.show()
# For data and diff_pb2 see: https://www.reddit.com/r/place/comments/62z2uu/rplace_archive_update_and_boardbitmap_description/
import os
import numpy as np
from diff_pb2 import Diff
diff_filenames = os.listdir('diffs')
diff_filenames = sorted([os.path.join('diffs', i) for i in diff_filenames if i.endswith('bin')])
# function to read a diff file
def read_diff(filename):
with open(filename, 'rb') as file:
b = file.read()
return Diff.FromString(b)
diffs = []
for filename in diff_filenames:
diff = read_diff(filename)
start = diff.fromTimestamp
end = diff.fromTimestamp
for pixel in diff.pixel:
diffs.append([start, end, pixel.x, pixel.y])
diffs = np.array(diffs)
np.save('diffs.npy', diffs)
import numpy as np
cimport numpy as np
DTYPE = np.int
ctypedef np.int_t DTYPE_t
cimport cython
@cython.boundscheck(False)
@cython.wraparound(False)
def pixcount(
np.ndarray[DTYPE_t, ndim=2] pixels,
np.ndarray[DTYPE_t, ndim=2] image
):
cdef int length = pixels.shape[0]
cdef int x, y
for i in range(length):
x = pixels[i, 0]
y = pixels[i, 1]
image[x, y] += 1
return image
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment