Skip to content

Instantly share code, notes, and snippets.

View alexrutherford's full-sized avatar

Alex Rutherford alexrutherford

View GitHub Profile
@alexrutherford
alexrutherford / index.html
Last active September 22, 2015 00:29
Visualisation of hierarchical constitution ontology
<!DOCTYPE html>
<meta charset="utf-8">
<style>
.node {
cursor: pointer;
}
.node circle {
fill: #fff;
@alexrutherford
alexrutherford / index.html
Last active September 22, 2015 03:10
Gowalla Checkin Visualisation
<!DOCTYPE html>
<meta charset="utf-8">
<style>
.node {
stroke-width: 0.5px;
}
.node:not(:hover) .nodetext {
<!DOCTYPE html>
<meta charset="utf-8">
<style>
.node {
stroke-width: 0.5px;
}
.node:not(:hover) .nodetext {
@alexrutherford
alexrutherford / dREADME.md
Last active September 25, 2015 23:57 — forked from mbostock/.block
Clustered Force Layout I

This clustered force layout is implemented using two custom forces. The first, cluster, pushes nodes towards the largest node of the same color. A second collide force prevents circles from overlapping by detecting collisions.

This example uses standard gravity; compare to custom gravity applied only to the largest node of each color. To minimize distracting jitter during initialization, try an entry transition.

@alexrutherford
alexrutherford / clear_output.py
Last active October 19, 2015 19:32
Clears output from Jupyter notebooks
#! /usr/bin/python
import json,os,sys
############
def main():
############
'''
Usage: python clear_output.py name.ipynb
Output name_clean.ipynb [--inplace]
Script to clean output from IPython notebooks
@alexrutherford
alexrutherford / fig_hack.py
Created October 5, 2015 21:34
Figure settings for Jupyter notebooks
def setFigSize(dim=(16.5,5.5)):
fig = matplotlib.pyplot.gcf()
fig.set_size_inches(dim[0],dim[1])
setFigSize(dim=(10,10))
# Just call it before you plot (dimension measured in inches)
%config InlineBackend.figure_format = 'svg'
# Also, you get crisp vector images if you set this
@alexrutherford
alexrutherford / mongo_timestamp.py
Created October 8, 2015 14:32
Create MongoDB compatible timestamp in Python
import time
ts = time.time()
isodate = datetime.datetime.fromtimestamp(ts, None)
# Now import directly into MongoDB
# This timestamp will be read as ISODate()
@alexrutherford
alexrutherford / aggregate.sh
Created October 8, 2015 17:31
Count most common page categories in MongoDB
db.pages.aggregate([
{$match:{id:{$exists:true}}},
{$group:{_id:'$category',total:{$sum:1}}},
{$sort:{total:-1}}
])
@alexrutherford
alexrutherford / log.py
Created October 19, 2015 16:24
Snippet to add logging with timestamps to Flask app
import logging
hdlr = logging.FileHandler('log.log')
formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s')
hdlr.setFormatter(formatter)
app = Flask(__name__)
app.debug=True
app.logger.addHandler(hdlr)
import shapefile
sf = shapefile.Reader("data/test.shp")
fig=plt.figure()
ax=fig.add_subplot(111)
for nshp in xrange(len(shapes)):
ptchs = []
pts = np.array(shapes[nshp].points)