Skip to content

Instantly share code, notes, and snippets.

View clayadavis's full-sized avatar

Clayton A Davis clayadavis

View GitHub Profile
@clayadavis
clayadavis / multiscale_backbone.py
Last active October 26, 2018 21:51
Compute the multiscale backbone edge probabilities of a NetworkX graph
def compute_p(G, weight='weight', p='p'):
# Undirected
for node in G:
k_n = len(G[node])
if k_n > 1:
sum_w = sum( G[node][neighbor][weight] for neighbor in G[node] )
for neighbor in G[node]:
edge_weight = G[node][neighbor][weight]
p_ij = float(edge_weight)/sum_w
G[node][neighbor][p] = (1-p_ij)**(k_n-1)
@clayadavis
clayadavis / purr.py
Created November 11, 2016 17:41
Run multiple commands in parallel
'''
Run multiple commands in parallel.
Run from CLI like
$ python purr.py commands.txt
where `commands.txt` is a text file containing one command per line.
'''
import subprocess
import redis
# Number of records to move at once
N = 10000
# DB number and key
DB = 3
KEY = 'deletion_notices'
source = redis.StrictRedis('<source host>', db=DB)
@clayadavis
clayadavis / flask_mashape.py
Last active August 23, 2016 15:03
A route decorator for Flask that performs Mashape authentication
import functools
from flask import Flask, request
app = Flask(__name__)
def authenticate_mashape(func):
'''
Decorator to authenticate request with Mashape.
'''
@clayadavis
clayadavis / celery_test_tasks.py
Last active August 23, 2016 15:06
Test tasks
from .. import app
from celery.utils.log import get_task_logger
logger = get_task_logger(__name__)
# logger.setLevel(logging.DEBUG)
@app.task(name='test.noop', bind=True)
def noop(self):
logger.info("Completing NOOP task")
@clayadavis
clayadavis / gist:8a2b025b24fdd4b6016d
Last active January 22, 2016 17:15 — forked from yurydelendik/gist:f2b846dae7cb29c86d23
PDF.js get/show hightlight (for Chromium)
function getHightlightCoords() {
var pageIndex = PDFViewerApplication.pdfViewer.currentPageNumber - 1;
var page = PDFViewerApplication.pdfViewer.getPageView(pageIndex);
var pageRect = page.canvas.getClientRects()[0];
var selectionRects = Array.apply(this, window.getSelection().getRangeAt(0).getClientRects());
var viewport = page.viewport;
var selected = selectionRects.map(function (r) {
return viewport.convertToPdfPoint(r.left - pageRect.left, r.top - pageRect.top).concat(
viewport.convertToPdfPoint(r.right - pageRect.left, r.bottom - pageRect.top));
});
@clayadavis
clayadavis / botornot_example.py
Last active December 9, 2015 03:27
BotOrNot REST Example
import json
import requests
## `pip install tweepy`
import tweepy
auth = tweepy.OAuthHandler('consumer_key', 'consumer_secret')
auth.set_access_token('access_token_key', 'access_token_secret')
api = tweepy.API(auth, parser=tweepy.parsers.JSONParser())
@clayadavis
clayadavis / clip_magic.py
Last active September 16, 2015 19:50 — forked from nova77/clip_magic.py
copy to clipboard ipython magic
"""
Add copy to clipboard from IPython!
To install, just copy it to your profile/startup directory, typically:
~/.ipython/profile_default/startup/
Example usage:
%clip hello world
# will store "hello world"
@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)
@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'))