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 / lfp_notebok.ipynb
Created December 4, 2012 23:10
LFP filtering and spectrogram IPython Notebook
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@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)))
@andrewgiessel
andrewgiessel / gist:4635563
Last active June 30, 2023 20:31
simple numpy based 2d gaussian function
import numpy as np
def makeGaussian(size, fwhm = 3, center=None):
""" Make a square gaussian kernel.
size is the length of a side of the square
fwhm is full-width-half-maximum, which
can be thought of as an effective radius.
"""
PREFIX=$HOME
VERSION=1.2.3
# Install Protocol Buffers
wget http://protobuf.googlecode.com/files/protobuf-2.4.1.tar.bz2
tar -xf protobuf-2.4.1.tar.bz2
cd protobuf-2.4.1
./configure --prefix=$PREFIX
make
make install
@andrewgiessel
andrewgiessel / gist:5263554
Created March 28, 2013 14:28
plot mean +/- SEM in matplotlib
import scipy
def plot_mean_and_sem(array, axis=1):
mean = array.mean(axis=axis)
sem_plus = mean + scipy.stats.sem(array, axis=axis)
sem_minus = mean - scipy.stats.sem(array, axis=axis)
fill_between(np.arange(mean.shape[0]), sem_plus, sem_minus, alpha=0.5)
plot(mean)
@andrewgiessel
andrewgiessel / gist:4589258
Created January 21, 2013 20:52
histogram normalization with numpy
import numpy as np
def histeq(im,nbr_bins=256):
#get image histogram
imhist,bins = np.histogram(im.flatten(),nbr_bins,normed=True)
cdf = imhist.cumsum() #cumulative distribution function
cdf = 255 * cdf / cdf[-1] #normalize
#use linear interpolation of cdf to find new pixel values
im2 = np.interp(im.flatten(),bins[:-1],cdf)
@andrewgiessel
andrewgiessel / gist:4514186
Last active October 17, 2020 01:42
butterworth bandpass filter in python
from scipy.signal import butter, lfilter
def butter_bandpass_filter(data, lowcut, highcut, fs, order=5):
nyq = 0.5 * fs
low = lowcut / nyq
high = highcut / nyq
b, a = butter(order, [low, high], btype='band')
y = lfilter(b, a, data)
return y
@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 / pytables_numpy.py
Last active January 7, 2020 18:17
simple example to use pytables to save a numpy array
import tables
import numpy as np
# Store "all_data" in a chunked array...
# from: http://stackoverflow.com/questions/8843062/python-how-to-store-a-numpy-multidimensional-array-in-pytables
f = tables.openFile('all_data.hdf', 'w')
atom = tables.Atom.from_dtype(all_data.dtype)
filters = tables.Filters(complib='blosc', complevel=5)
ds = f.createCArray(f.root, 'all_data', atom, all_data.shape, filters=filters)
@andrewgiessel
andrewgiessel / pyside_matplotlib_example.py
Created August 28, 2012 00:02
Pyside GUI w/embedded matplotlib class in IPython Notebook
from PySide import QtCore, QtGui
import sys
matplotlib.rcParams['backend.qt4']='PySide'
from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.figure import Figure
class MatplotlibWidget(FigureCanvas):
def __init__(self, parent=None):