Skip to content

Instantly share code, notes, and snippets.

View dwf's full-sized avatar

David Warde-Farley dwf

View GitHub Profile
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@dwf
dwf / gist:3790284
Created September 26, 2012 20:12
A key-aware default dictionary implementation.
class KeyAwareDefaultDict(dict):
"""
Like a standard library defaultdict, but pass the key
to the default factory.
"""
def __init__(self, default_factory=None):
self.default_factory = default_factory
def __getitem__(self, key):
if key not in self and self.default_factory is not None:
"""
A matplotlib-based image viewer utility function.
"""
import matplotlib.pyplot as plt
def image_view(arr, fig=None, frame_on=True, *args, **kwargs):
"""
Sensibly display an image in a plot window.
Parameters
@dwf
dwf / gist:2219652
Created March 27, 2012 19:47
Serialization-friendly cached Theano-functions-as-instance-attributes.
import theano
import functools
def cached_theano_function(fn):
@functools.wraps(fn)
def wrapped(self):
if not hasattr(self, '_function_cache'):
self._function_cache = {}
if fn.func_name not in self._function_cache:
@dwf
dwf / kmeans.pyx
Created March 25, 2012 22:18
Parallelized k-means in Cython.
"""
Parallelized k-means module.
By David Warde-Farley, February 2012. Licensed under the 3-clause BSD.
"""
cimport cython
from cython.parallel import prange
import numpy as np
cimport numpy as np
from numpy.random import normal
@dwf
dwf / gist:1766222
Created February 8, 2012 06:55
Example of how to read a partial NumPy array stored in NPY format off of disk.
"""
Example of how to read a partial NumPy array stored in NPY
format off of disk.
"""
__author__ = "David Warde-Farley"
__copyright__ = "Copyright (c) 2012 by " + __author__
__license__ = "3-clause BSD"
__email__ = "dwf@dwf.name"
import struct
@dwf
dwf / resize_videos.py
Created November 29, 2011 19:41
A batch video-resizing script.
#!/usr/bin/env python
# Copyright (c) 2009, David Warde-Farley
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
@dwf
dwf / gist:1335246
Created November 2, 2011 23:10
My implementation of feature sign search for L1 minimization.
"""
L1-penalized minimization using the feature sign search algorithm.
"""
import logging
import numpy as np
log = logging.getLogger("feature_sign")
log.setLevel(logging.INFO)
@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 = []