Skip to content

Instantly share code, notes, and snippets.

View MarcCote's full-sized avatar

Marc-Alexandre Côté MarcCote

View GitHub Profile
@MarcCote
MarcCote / cumsum_leak.py
Last active August 29, 2015 13:56
Gist showing a potential memory leak when using the 'out' parameter of PyArray_CumSum of the numpy's C-api.
import resource
import numpy as np
from scipy import weave
x = np.ones(1e6, dtype=np.float32)
# There is no memory leak, when we let PyArray_CumSum creates the output array, i.e. when out=NULL.
code_good = r"""
npy_intp shape[1] = { PyArray_SIZE(x_array) };
int dtype = PyArray_DTYPE(x_array)->type_num;
@MarcCote
MarcCote / tensor_dot.py
Created February 19, 2014 19:35
Gist showing an erroneous result from tensor.dot when one of the operand is zero.
import numpy as np
import theano
import theano.tensor as T
print "Version:", theano.version.version
print "=Theano="
A = T.zeros((2,10))
B = T.zeros((30, 10))
print "Shape of A:", A.eval().shape
@MarcCote
MarcCote / save_trk_with_color.py
Created March 14, 2014 16:06
Example of saving a trk file with color using nibabel.
import numpy as np
from nibabel import trackvis as tv
from dipy.segment.quickbundles import QuickBundles
from dipy.data import get_data
fname = get_data('fornix')
streams, hdr = tv.read(fname)
streamlines = [i[0] for i in streams]
qb = QuickBundles(streamlines, dist_thr=10., pts=12)
@MarcCote
MarcCote / length_per_voxel.py
Last active August 29, 2015 14:04
Measure length of streamline passing trough each voxel. Plus some utility functions to test, bench and vizualize.
from dipy.viz import fvtk
import numpy as np
from numpy.testing import measure
from nose.tools import assert_equal, assert_almost_equal
from numpy.testing import assert_array_equal, assert_array_almost_equal
norm = lambda x: np.sqrt(np.sum(x**2))
@MarcCote
MarcCote / metric.pyx
Created August 13, 2014 13:34
Problem when using fused type with class inheritance in Cython
ctypedef float[:,:] float2d
ctypedef double[:,:] double2d
ctypedef fused Line:
float2d
double2d
cdef class Metric:
cdef float dist(Metric self, Line line) nogil:
pass
@MarcCote
MarcCote / main.py
Created August 27, 2014 17:51
Memory leak with vector of memoryviews
import numpy as np
import resource
import metric
def test_memory_leak():
NB_LOOPS = 20
NB_LINES = 10000
NB_POINTS = 100
@MarcCote
MarcCote / nstreams_bug.py
Created September 3, 2014 18:45
Shows nstreams limitation on GPU
import theano
import numpy as np
from theano.sandbox.rng_mrg import MRG_RandomStreams as RandomStreams
theano_rng = RandomStreams(42)
# This one works fine
print np.sum(theano_rng.uniform(size=(10000, 784), dtype=theano.config.floatX, nstreams=10000).eval(), 1)
# This one produces NaN at the end
print np.sum(theano_rng.uniform(size=(10000, 784), dtype=theano.config.floatX, nstreams=10000*400).eval(), 1)
@MarcCote
MarcCote / sum_of_stack_bug.py
Created September 15, 2014 20:03
Unexpected behaviour when summing stacked tensors
import theano
import theano.tensor as T
import numpy as np
A = theano.shared(np.array([1, 2, 3, 4, 5]))
print "Theano"
print T.sum(T.stack(A, A), axis=0).eval()
print T.sum(T.stack(A, A), axis=1).eval()
import os
import argparse
import nibabel as nib
from nibabel.streamlines import Field
from nibabel.orientations import aff2axcodes
def build_argparser():
DESCRIPTION = "Convert tractograms (TCK -> TRK)."
import os
import argparse
import nibabel as nib
def build_argparser():
DESCRIPTION = "Convert tractograms (TRK -> TCK)."
p = argparse.ArgumentParser(description=DESCRIPTION)
p.add_argument('tractograms', metavar='bundle', nargs="+", help='list of tractograms.')
p.add_argument('-f', '--force', action="store_true", help='overwrite existing output files.')