Skip to content

Instantly share code, notes, and snippets.

View clayadavis's full-sized avatar

Clayton A Davis clayadavis

View GitHub Profile
@clayadavis
clayadavis / gist:5681622
Created May 30, 2013 22:02
How to get a Twitter API key
1. Go to https://dev.twitter.com/
2. Click on "Sign In" in the upper-right corner.
* There is a link to sign up under the username field if you do not already have a Twitter account.
* If you sign up for a new account, you'll have to confirm your email before you can get an API key.
3. Enter your credentials and sign in.
4. Back at https://dev.twitter.com/, click on your avatar in the upper-right corner, then My Applications.
5. Click on "Create a new application".
6. Fill out the information, agree to the Rules of the Road, do the captcha, and click on "Create your Twitter application".
7. In the application page that comes up next, copy down the "Consumer key" and "Consumer secret". This is half of the key info.
8. Click on "Create my access token" at the bottom of the application page, under "Your access token".
@clayadavis
clayadavis / gist:5686333
Created May 31, 2013 16:58
How to draw network layouts to files. Run this in a Python shell.
import matplotlib
#This has to go before importing pyplot
matplotlib.use('Agg')
import matplotlib.pyplot as plt
import networkx as nx
# Make your graph
G = nx.dodecahedral_graph()

Recap: SciPy 2013 Data Processing Tutorial

Ben and I wanted to take a minute today to thank everyone for coming to our tutorial. We hope you all learned something new and useful, and encourage everyone to continue the lively discussions from the sessions throughout this week and beyond. Towards that aim of facilitating further discussion of these topics, here is a quick rundown of the topics we went over and some additional resources for those interested in learning more.

  • The tutorial GitHub repo contains the slides and exercises, and should stay up for a while.
  • Your demo accounts on Wakari.io are not permanent, but it's super easy to sign up for a free account. Wakari is in active development, so if there's a feature you want or an annoyance you don't, feel free to give us a shout!

Pandas

{
"metadata": {
"name": ""
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
aptitude install build-essential libboost-all-dev libexpat-dev libcgal-dev
libsparsehash-dev libcairomm-1.0-dev graphviz-dev
## Optional: GraphViz
aptitude install graphviz-dev
# Make a conda recipe for pygraphviz?
pip install pygraphviz
conda install py2cairo
@clayadavis
clayadavis / rpy2-bcp.py
Last active December 31, 2015 04:09
R's bcp in Python with rpy2
x = [...]
from rpy2.robjects.packages import importr
importr('bcp')
import rpy2.robjects as robjects
bcp = robjects.r['bcp']
b = bcp(x)
@clayadavis
clayadavis / pandas_to_d3.py
Last active April 2, 2018 17:24
Convert a Pandas DataFrame to a D3 data array
def df_to_d3(df):
data = []
keys, labels = df.index, df.columns
for key in keys:
values = [{'label': l, 'value': df[l][key]} for l in labels]
data.append({'values': values, 'key': key})
return data
def series_to_d3(s):
return [{'label': k, 'value': v} for k,v in s.to_dict().items()]
@clayadavis
clayadavis / truncator.js
Last active August 23, 2016 15:08
Truncate Javascript string to fixed number of characters, then add ellipsis.
function truncator(len){
return function(txt){
if (txt.length > len){
txt = txt.substring(0, len).replace(/\W*\w*$/, '') + '\u2026';
}
return txt
}
}
@clayadavis
clayadavis / nx_to_d3.py
Last active May 29, 2019 20:47
Convert networkx graph to d3 graph
nodes = [{'name': n, 'group': G.node[n]['question_id'], 'size': G.node[n]['count']} for n in G]
l = G.edges()
edges = [{'source': l.index(s), 'target': l.index(t), 'value': G[s][t]['weight']} for s,t in itertools.product(l, l) if s in G and t in G[s]]
json.dump({'nodes': nodes, 'links': edges}, open('filename.json', 'w'))
@clayadavis
clayadavis / django_concat.py
Last active August 23, 2016 15:07
Group Concat aggregate for Django 1.4
from django.db.models import Aggregate, fields
from django.db.models.sql.aggregates import Aggregate as SQLAggregate
class SQLConcat(SQLAggregate):
sql_function = 'GROUP_CONCAT'
sql_template = ('%(function)s(%(distinct)s%(field)s'
' SEPARATOR "%(separator)s")')
def __init__(self, col, separator=', ', distinct=False, **extra):
super(SQLConcat, self).__init__(col, separator=separator,
distinct=distinct and 'DISTINCT ' or '', **extra)