Skip to content

Instantly share code, notes, and snippets.

View dwf's full-sized avatar

David Warde-Farley dwf

View GitHub Profile
@dwf
dwf / arccos_demo.py
Created October 19, 2011 03:42
Demonstrates an issue with the numerical stability of the gradient of arccos.
import theano.tensor as tensor
from theano import function
import numpy as np
value = np.array([ 0.992827313923019039165751564724,
0.118152792712208978831434080803,
-0.008531793400639134036800292904,
0.016157066691618496290239193058])
aa = tensor.vector('aa')
bb = tensor.vector('bb')
ee = tensor.scalar()
@dwf
dwf / gist:1222883
Created September 16, 2011 19:19
Using matplotlib + multiprocessing for asynchronous, interactive monitoring plots.
"""Demo of how to pop up plots asynchronously using separate processes."""
from multiprocessing import Process
import time
import sys
import matplotlib.pyplot as plt
import numpy as np
def demo():
i = 0
processes = []
@dwf
dwf / gist:828099
Created February 15, 2011 19:50
Pull out a submatrix from a COO SciPy sparse matrix.
import scipy as S
def coo_submatrix_pull(matr, rows, cols):
"""
Pulls out an arbitrary i.e. non-contiguous submatrix out of
a sparse.coo_matrix.
"""
if type(matr) != S.sparse.coo_matrix:
raise TypeError('Matrix must be sparse COOrdinate format')
@dwf
dwf / barcharts.py
Created December 3, 2010 05:35
Make some plots for our 2010 NAR Web Server paper.
"""Make some plots for our 2010 NAR Web Server paper."""
import sys
import numpy as np
import matplotlib as mpl
mpl.use('Agg') # Don't pop up figures, just render in memory
import matplotlib.pyplot as plt
# Empty dictionaries
bp = {}
@dwf
dwf / rocarea.py
Created August 30, 2010 19:01
ROC curve plotting code.
"""
Simple implementation of ROC curve plotting with NumPy and matplotlib.
No bells and whistles, no fancy data structures, just one function and
a (hopefully) very gentle learning curve.
"""
__author__ = "David Warde-Farley <dwf AT cs.toronto.edu>"
__copyright__ = "(c) 2010 David Warde-Farley"
__license__ = "3-clause BSD license"
@dwf
dwf / gist:544590
Created August 23, 2010 01:39
Messing around with Kaprekar's procedure.
def kaprekar(s, t=4):
"""
Demonstrate Kaprekar's procedure and its convergence to Kaprekar's constant.
"""
s = str(s)
# t = 3 or 4 will converge; otherwise all bets are off.
assert len(s) <= t
if len(s) < t:
s = ('0' * (t - len(s))) + s
print s
@dwf
dwf / gist:468955
Created July 9, 2010 02:34
Quick hack to get pudb to respond to the %debug magic in IPython 0.10.
--- iplib.bak 2010-07-08 22:30:10.000000000 -0400
+++ iplib.py 2010-07-08 22:31:09.000000000 -0400
@@ -1684,6 +1684,14 @@
if Debugger.has_pydb:
from pydb import pm
else:
+ try:
+ import pudb
+ pudb.post_mortem((sys.last_type,
+ sys.last_value,
@dwf
dwf / gist:377466
Created April 24, 2010 04:35
And *that* is how to reverse a linked list. I'm an idiot for messing this up.
class Node(object):
def __init__(self, value):
self.value = value
self.next = None
def __str__(self):
"""Return a string representation of the list starting here."""
values = []
curr = self
while curr is not None:
@dwf
dwf / patches.py
Created April 14, 2010 19:49
Some patch extraction code I'm using to process images.
import os
import numpy as np
import scipy.ndimage as ndimage
import matplotlib
import matplotlib.pyplot as plt
def frac_eq_to(image, value=0):
return (image == value).sum() / float(np.prod(image.shape))
def extract_patches(image, patchshape, overlap_allowed=0.5, cropvalue=None,
#!/bin/bash
# screencap2dropbox.sh -- Takes a screenshot with Mac OS X's
# 'screencapture' utility, places the output in a publicly accessible
# Dropbox folder ( http://www.dropbox.com/ for more information )
# and then copies the URL to the clipboard.
#
# By David Warde-Farley, March 18, 2010. Inspired by an earlier Automator
# version by Andrew Louis.
#