Skip to content

Instantly share code, notes, and snippets.

View andrewgiessel's full-sized avatar
🧬
hackhackhack

andrew giessel andrewgiessel

🧬
hackhackhack
View GitHub Profile
@andrewgiessel
andrewgiessel / gist:6122739
Created July 31, 2013 15:00
fit 2d gaussian with numpy and scipy, including rotation
def gaussian(self, height, center_x, center_y, width_x, width_y, rotation):
"""Returns a gaussian function with the given parameters"""
width_x = float(width_x)
width_y = float(width_y)
rotation = np.deg2rad(rotation)
center_x = center_x * np.cos(rotation) - center_y * np.sin(rotation)
center_y = center_x * np.sin(rotation) + center_y * np.cos(rotation)
def rotgauss(x,y):
@andrewgiessel
andrewgiessel / gist:6071508
Last active December 20, 2015 04:29
save and load wrappers for pickle
import cPickle as pickle
def save(obj, filename):
"""Simple wrapper to pickle an object on disk
:param: obj, any pickable object
:param: filename, string representation of the file to save to
"""
with open(filename, 'wb') as f:
@andrewgiessel
andrewgiessel / plot_array.py
Created June 27, 2013 19:44
Code to facet plot over a given dimension of a high dimensional numpy array
import numpy as np
import matplotlib.pyplot as plt
def plot_array(npArray, axis=1, xlim=None, ylim=None):
plt.figure()
num_plots = npArray.shape[axis]
side = np.ceil(np.sqrt(num_plots))
for current_plot in range(1, num_plots+1):
@andrewgiessel
andrewgiessel / gist:5816412
Created June 19, 2013 18:01
exponential decay fit
from scipy.optimize import curve_fit
def exponential(t, A, tau, C):
return A * np.exp(-tau * t) + C
start = None # redefine
ydata = data[start:]
xdata = np.arange(ydata.shape[0])
@andrewgiessel
andrewgiessel / gist:5709011
Created June 4, 2013 19:54
peakSort - like np.argsort, but sorts so that the middle values are the highest and alternate out to the edges
import numpy as np
def peakSort(values):
values = np.array(values)
low = np.floor(values.shape[0] / 2)
high = low+1
new = np.zeros_like(values)
for i, item in enumerate(np.sort(values)[::-1]):
if i % 2:
new[high] = np.argwhere(values==item)
high += 1
@andrewgiessel
andrewgiessel / gist:5708977
Created June 4, 2013 19:52
numpy lifetime sparseness
import numpy as np
def lifetimeSparseness(data):
N = data.shape[0]
top = (np.power(np.sum(data/N), 2))
bottom = np.sum(np.power(data, 2) / N)
return (1 - top / bottom)
@andrewgiessel
andrewgiessel / gist:5684769
Created May 31, 2013 12:52
fit a sigmoid curve, python, scipy
# good discussion here: http://stackoverflow.com/questions/4308168/sigmoidal-regression-with-scipy-numpy-python-etc
# curve_fit() example from here: http://permalink.gmane.org/gmane.comp.python.scientific.user/26238
# other sigmoid functions here: http://en.wikipedia.org/wiki/Sigmoid_function
import numpy as np
import pylab
from scipy.optimize import curve_fit
def sigmoid(x, x0, k):
y = 1 / (1 + np.exp(-k*(x-x0)))
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
on open dropped_dir
tell application "Terminal"
set dirname to do shell script "perl -e \"print quotemeta ('" & POSIX path of item 1 of dropped_dir & "');\""
do script "cd " & dirname & "; ipython notebook --pylab; exit"
end tell
end open
@andrewgiessel
andrewgiessel / iterdim.py
Last active December 16, 2015 07:39
iterdim routine a generator which yields slices of an array
# found here: http://stackoverflow.com/questions/1589706/iterating-over-arbitrary-dimension-of-numpy-array
import numpy as np
def iterdim(a, axis=0) :
a = np.asarray(a);
leading_indices = (slice(None),)*axis
for i in xrange(a.shape[axis]) :
yield a[leading_indices+(i,)]