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:7fc86d805f694e33b779
Created August 1, 2014 17:30
linear de-trend using curve_fit and sigma
from scipy.optimize import curve_fit
def linear(x, m, b):
return m*x + b
x = np.arange(0,400,1)
y = linear(x, -0.2, 3)
yn = y + np.random.normal(size=len(x))*2
yn[50:350] *= np.random.normal(size=300)
@andrewgiessel
andrewgiessel / ezp1_proxy_bookmarklet.js
Last active August 29, 2015 14:00
bookmarklet to automatically proxify a page for harvard's library system
// most recent update: 04/22/2014
// one liner
javascript:function go(){var asdf = window.location.host.replace(/\./g, "-");window.location = window.location.protocol+'//'+asdf+'.ezp1.harvard.edu'+window.location.pathname+window.location.search;}go();void(0);
// expanded
function go() {
var asdf = window.location.host.replace(/\./g, "-"); // replace all the periods with dashes (uses regexp)
window.location = window.location.protocol+'//' +
@andrewgiessel
andrewgiessel / gist:7894557
Last active December 30, 2015 22:29
launch a ipython notebook server via bsub, then open automatically chain a tunnel to the host
#!/usr/bin/env python
import subprocess
import time
import re
import os
import sys
# launch the server and get the jobid
@andrewgiessel
andrewgiessel / gist:7875659
Created December 9, 2013 16:50
continuous regions in an boolean array
import numpy as np
def contiguous_regions(condition):
"""Finds contiguous True regions of the boolean array "condition". Returns
three 1d arrays: start indicies, stop indicies and lengths of contigous regions
"""
d = np.diff(condition)
idx, = d.nonzero()
idx += 1 # need to shift indices because of diff
@andrewgiessel
andrewgiessel / gist:7589513
Created November 21, 2013 20:58
fft based filtering with FIR windows!
import numpy as np
import scipy.signal
import matplotlib.pyplot as plt
n=256
data = np.random.random(n)
data_fft = np.fft.fft(data)
# see also : http://mpastell.com/2010/01/18/fir-with-scipy/
@andrewgiessel
andrewgiessel / gist:7567879
Last active December 28, 2015 21:59
use of scipy.spatial.kdtree to get all points w/i specified distance from point
# imports
import numpy as np
import scipy as scipy
# construct static 2d distance tree
size = 256
x, y = np.mgrid[0:size, 0:size]
distance_tree = spatial.KDTree(zip(x.ravel(), y.ravel()))
# simple use case
@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 / gist:7340672
Last active December 27, 2015 14:29
Matplotlib Alpha colormaps
# setup colormaps with alpha backgrounds
imshow(np.random.random((10,10)), cmap='Reds')
imshow(np.random.random((10,10)), cmap='Blues')
close('all')
import copy
red_alpha = copy.copy(mpl.cm.Reds)
blue_alpha = copy.copy(mpl.cm.Blues)
@andrewgiessel
andrewgiessel / gist:6368061
Created August 28, 2013 16:30
IPython notebook for replacing text in a file.
{
"metadata": {
"name": "replace text in files"
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
@andrewgiessel
andrewgiessel / gist:6134640
Created August 1, 2013 19:54
python code to exclude pixel islands of a given size, requires mahotas for building a label matrix (could use pymorph instead)
import mahotas
def excludePixels(image, size_cutoff=1):
labeled_image = mahotas.label(image)[0]
for label_id in range(labeled_image.max()+1):
label_id_index = labeled_image == label_id
if label_id_index.sum() <= size_cutoff:
labeled_image[label_id_index] = 0