Skip to content

Instantly share code, notes, and snippets.

View npinto's full-sized avatar

Nicolas Pinto npinto

View GitHub Profile
@npinto
npinto / labels_to_integers.py
Created September 4, 2012 21:36
String labels to sorted 0-based integers.
In [12]: l = ['a', 'a', 'c', 'c', 'c', 'b']
In [13]: u = np.unique(l)
In [14]: u
Out[14]:
array(['a', 'b', 'c'],
dtype='|S1')
In [15]: np.searchsorted(u, l)
@npinto
npinto / .gitignore
Created August 1, 2012 23:40
Cython 0.16 C++ Demo
*.so
_demo.cpp
build
@npinto
npinto / openmpsum.py
Created July 31, 2012 19:03
openmp with scipy weave inline
import numpy as np
from scipy import weave
def openmpSum(in_array):
"""
Performs fast sum of an array using openmm
"""
a = np.asarray(in_array)
b = np.array([1.])
N = int(np.prod(a.shape))
@npinto
npinto / connectomics_swirls.py
Created July 21, 2012 08:58
Connectomics swirls
import numpy as np
from scipy import misc
from skimage.transform import swirl
import connectomics_data as cd
def random_swirls(X, Y, n_swirls=1, order=0, rseed=None):
@npinto
npinto / connectomics_noise.py
Created July 21, 2012 08:05
Connectomics EM Noise Simulation
from scipy import ndimage
from scipy import misc
import numpy as np
import connectomics_data as cd
def get_mask(shape, p=0.999, sigma=10, th=0.2):
r = np.random.binomial(1, p, size=shape).astype('f')
r2 = ndimage.gaussian_filter(r, sigma)
@npinto
npinto / gd.py
Created July 21, 2012 05:41
steepest descent w/ line search (brent)
import numpy as np
from scipy.optimize import brent
def steepest_descent(func, X0, grad, maxiter= 100, verbose = False,
project=None, alpha_0=1.0, gtol = 1e-5, **args):
def func_one(X,dir_):
def inner(alpha):
if project is None:
return func(X + alpha*dir_)
@npinto
npinto / img2_00_X.png
Created July 20, 2012 23:21
Infinite Connectomics Data
img2_00_X.png
@npinto
npinto / perl-5.12.3.ebuild
Created July 3, 2012 06:18
Ubuntu 11+ compatible perl gentoo prefix ebuild
# Copyright 1999-2011 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2
# $Header: /var/cvsroot/gentoo-x86/dev-lang/perl/perl-5.12.3.ebuild,v 1.1 2011/01/22 09:41:54 tove Exp $
EAPI=3
inherit eutils alternatives flag-o-matic toolchain-funcs multilib
PATCH_VER=1
@npinto
npinto / deform.py
Created July 2, 2012 20:16
"Water" Image Deformation
import numpy as np
from scipy import ndimage
#from pylab import quiver
from scipy import misc
l = misc.lena()
yy, xx = np.indices(l.shape)
@npinto
npinto / rv_grad.py
Created June 28, 2012 21:20
Gradients of Rolling View
import numpy as np
from skimage.util.shape import view_as_windows, view_as_blocks
from sthor.util import filter_pad2d
def f_g(x):
xr = view_as_windows(x, (2, 2)).copy()
#print xr
loss = 0.5 * (xr**2.).sum()