Skip to content

Instantly share code, notes, and snippets.

View alexbw's full-sized avatar

Alex Wiltschko alexbw

  • Google
  • Boston, MA
View GitHub Profile
@alexbw
alexbw / gist:3006780
Created June 27, 2012 20:53
multiple gaussian fitting
from numpy import *
from scipy import optimize
def n_gaussians(parameters):
"""
Returns a summed gaussian function with the given parameters,
so parameters is a list with repeated motif [height, center_x, center_y, a, b, c]
for each gaussian. So, two gaussians would be defined by a length-12 list.
Guided by: http://code.google.com/p/agpy/source/browse/trunk/agpy/gaussfitter.py
@alexbw
alexbw / repeatingtimer.py
Created September 1, 2011 20:11
A repeating timer in Python
class RepeatingTimer(object):
"""
USAGE:
from time import sleep
def myFunction(inputArgument):
print(inputArgument)
r = RepeatingTimer(0.5, myFunction, "hello")
r.start(); sleep(2); r.interval = 0.05; sleep(2); r.stop()
"""
@alexbw
alexbw / kalman.py
Created February 20, 2012 03:32
Kalman Filter in Python
class Kalman:
"""
USAGE:
# e.g., tracking an (x,y) point over time
k = Kalman(state_dim = 6, obs_dim = 2)
# when you get a new observation —
someNewPoint = np.r_[1,2]
k.update(someNewPoint)
@alexbw
alexbw / skeletonize.py
Created February 24, 2012 17:47
Skeletonizing an image
import SimpleCV as scv
import scipy.ndimage as ndimage
radius = 5
# Grab a little blobby image from the interwebs (it's a picture of a mouse, taken with the Microsoft Kinect)
scv_img = scv.Image("http://i.imgur.com/mGg0V.png")
# Grab a grayscale layer of the image
img = scv_img.getNumpy()[:,:,0]
import numpy as np
import cPickle as pickle
import joblib
from moseq.train import ARHMM, train_model
from moseq.train.util import whiten_all
from collections import OrderedDict
from syllables import analysis
# Load the data
with open("/data/efs/drugs/alldoses/dataset.pkl","r") as f:
@alexbw
alexbw / gist:4705170
Created February 4, 2013 05:30
Some of the scikit.cuda failures
======================================================================
ERROR: test_eye_complex64 (test_linalg.test_linalg)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/dattalab/Downloads/scikits.cuda/tests/test_linalg.py", line 21, in setUp
linalg.init()
File "/home/dattalab/Downloads/scikits.cuda/scikits/cuda/misc.py", line 136, in init
cublas.cublasInit()
File "/home/dattalab/Downloads/scikits.cuda/scikits/cuda/cublas.py", line 235, in cublasInit
_libcublas_ctx = cublasCreate()
@alexbw
alexbw / assert.py
Created July 17, 2018 16:37
Asserts
def f(x):
assert x != 0, 'Do not pass zero!'
return x * x
@alexbw
alexbw / lists_with_autograph.py
Created July 17, 2018 16:37
Lists with AutoGraph
def f(n):
z = []
# We ask you to tell us the element dtype of the list
autograph.set_element_type(z, tf.int32)
for i in range(n):
z.append(i)
# when you're done with the list, stack it
# (this is just like np.stack)
return autograph.stack(z)
@alexbw
alexbw / nested_control_flow.py
Created July 17, 2018 16:36
Nested control flow in AutoGraph
def f(n):
if n >= 0:
while n < 5:
n += 1
print(n)
return n
@alexbw
alexbw / collatz.py
Created July 17, 2018 16:36
Collatz with AutoGraph
def collatz(a):
counter = 0
while a != 1:
if a % 2 == 0:
a = a // 2
else:
a = 3 * a + 1
counter = counter + 1
return counter