Skip to content

Instantly share code, notes, and snippets.

@Carreau
Created April 25, 2014 13:03
Show Gist options
  • Select an option

  • Save Carreau/11288852 to your computer and use it in GitHub Desktop.

Select an option

Save Carreau/11288852 to your computer and use it in GitHub Desktop.
Some cython model for my research
import cython
import numpy
#cimport numpy
cimport cython
from libc.math cimport exp, sqrt
from cython.view cimport array as cvarray
@cython.cdivision(True)
cdef inline double dgauss(double radius, double x, double spread):
return exp(-1.0*(x-radius)**2/spread)
@cython.boundscheck(False)
@cython.wraparound(False)
cdef double dist(double[:] a , double[:] b):
cdef double r = sqrt(
(a[0]-b[0])**2 +
(a[1]-b[1])**2 +
(a[2]-b[2])**2
)
return r
@cython.boundscheck(False)
@cython.wraparound(False)
def makemask(double z0, double y0, double x0, double r0, double[:,:,:,:] posmap, double spread=50, outf=0,inf=0 ):
"""
makemask(double z0, double y0, double x0, double r0, double[:,:,:,:] posmap, double spread=50, outf=0, inf=0)
where posmap in an array of center of pixel in micro m
outf is outside fluorescence
"""
cdef :
double[:] center = numpy.array([x0,y0,z0])
double[:] cp
int zl,yl,xl
int ix,iy,iz
double d
double[:,:,:] iny
double[:,:] inx
zl,yl,xl,_ = numpy.shape(posmap)
cdef :
double [:,:,:] ze = numpy.zeros((zl,yl,xl))
int i,zz
int j
for iz in range(zl):
iny = posmap[iz]
for iy in range(yl):
inx = iny[iy]
for ix in range(xl):
cp = inx[ix]
d= dist(cp,center)
of = 0.0
if d>r0 :
of = inf
else :
of = outf
ze[iz,iy,ix] = 1-dgauss(r0,d,spread)+of
return numpy.array(ze)
@cython.boundscheck(False)
@cython.wraparound(False)
def makemask_doublet1(double z0, double y0, double x0, double r0, double[:,:,:,:] posmap, double thick=10, ):
cdef :
double[:] center = numpy.array([x0,y0,z0])
int zl,yl,xl
int ix,iy,iz
double Rp = r0+thick
double Rm = r0
zl,yl,xl,_ = numpy.shape(posmap)
cdef :
double [:,:,:] ze = numpy.zeros((zl,yl,xl))
int i,zz
int j
for iz in range(zl):
for iy in range(yl):
for ix in range(xl):
d= dist(posmap[iz,iy,ix],center)
if d < Rp :
ze[iz,iy,ix] = 1
if d < Rm :
ze[iz,iy,ix] = 0
return numpy.array(ze)
@cython.boundscheck(False)
@cython.wraparound(False)
def makemask_doublet2(double z0, double y0, double x0, double r0,double z1, double y1, double x1, double r1, double[:,:,:,:] posmap, double thick=10, ):
cdef :
double[:] center1 = numpy.array([x0,y0,z0])
double[:] center2 = numpy.array([x1,y1,z1])
int zl,yl,xl
int ix,iy,iz
double Rp1 = r0+thick
double Rm1 = r0
double Rp2 = r1+thick
double Rm2 = r1
zl,yl,xl,_ = numpy.shape(posmap)
cdef :
double [:,:,:] ze = numpy.zeros((zl,yl,xl))
int i,zz
int j
for iz in range(zl):
for iy in range(yl):
for ix in range(xl):
d1= dist(posmap[iz,iy,ix],center1)
d2= dist(posmap[iz,iy,ix],center2)
if d1 < Rp1 or d2< Rp2:
ze[iz,iy,ix] = 1
if d1 < Rm1 or d2 < Rm2 :
ze[iz,iy,ix] = 0
return numpy.array(ze)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment