Commit 1e1feb44 authored by csongor's avatar csongor

cythonify func by func

parent 47ed014e
import numpy as np
cimport numpy as np
#defining NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION
ctypedef fused FUSED_TYPES_t:
np.int32_t
np.int64_t
np.float32_t
np.float64_t
np.complex64_t
np.complex128_t
cpdef np.float32_t GaussianKernel_f(np.ndarray[np.float32_t, ndim=1] mpower, np.ndarray[np.float32_t, ndim=1] mk, np.float32_t mu, np.float32_t smooth_length):
cdef np.ndarray[np.float32_t, ndim=1] C = np.exp(-(mk - mu) ** 2 / (2. * smooth_length ** 2))
return np.sum(C * mpower) / np.sum(C)
cpdef np.ndarray[FUSED_TYPES_t, ndim=1] GaussianKernel(np.ndarray[FUSED_TYPES_t, ndim=1] mpower,np.ndarray[FUSED_TYPES_t, ndim=1] mk,np.ndarray[FUSED_TYPES_t, ndim=1] mu,np.float smooth_length):
cdef np.ndarray[FUSED_TYPES_t, ndim=1] C = np.exp(-(mk - mu) ** 2 / (2. * smooth_length ** 2))
cpdef np.float64_t GaussianKernel(np.ndarray[np.float64_t, ndim=1] mpower, np.ndarray[np.float64_t, ndim=1] mk, np.float64_t mu, np.float64_t smooth_length):
cdef np.ndarray[np.float64_t, ndim=1] C = np.exp(-(mk - mu) ** 2 / (2. * smooth_length ** 2))
return np.sum(C * mpower) / np.sum(C)
cpdef np.ndarray[FUSED_TYPES_t, ndim=1] apply_kernel_along_array(np.ndarray[FUSED_TYPES_t, ndim=1] power, np.int startindex,np.int endindex,np.ndarray[FUSED_TYPES_t, ndim=1] k,np.int exclude=1,np.float smooth_length=None):
cpdef np.ndarray[np.float64_t, ndim=1] apply_kernel_along_array(np.ndarray[np.float64_t, ndim=1] power, np.int_t startindex, np.int_t endindex, np.ndarray[np.float64_t, ndim=1] distances, np.float64_t smooth_length):
if smooth_length == 0:
if smooth_length == 0.0:
# No smoothing requested, just return the input array.
return power
return power[startindex:endindex]
cdef np.ndarray[FUSED_TYPES_t, ndim=1] excluded_power = np.array([])
if (exclude > 0):
k = k[exclude:]
excluded_power = np.copy(power[:exclude])
power = power[exclude:]
#excluded_power = np.array([])
#if (exclude > 0):
# distances = distances[exclude:]
# excluded_power = np.copy(power[:exclude])
# power = power[exclude:]
if (smooth_length is None) or (smooth_length < 0):
smooth_length = k[1]-k[0]
smooth_length = distances[1]-distances[0]
cdef np.ndarray[FUSED_TYPES_t, ndim=1] p_smooth = np.empty(endindex-startindex)
cdef np.ndarray[FUSED_TYPES_t, ndim=1] l,u
cdef np.ndarray[np.float64_t, ndim=1] p_smooth = np.empty(endindex-startindex, dtype=np.float64)
cdef np.int i
for i in xrange(startindex, endindex):
l = max(i-int(2*smooth_length)-1,0)
u = min(i+int(2*smooth_length)+2,len(p_smooth))
p_smooth[i-startindex] = GaussianKernel(power[l:u], k[l:u], k[i],
smooth_length)
if (exclude > 0):
p_smooth = np.r_[excluded_power,p_smooth]
p_smooth[i-startindex] = GaussianKernel(power[l:u], distances[l:u], distances[i], smooth_length)
#if (exclude > 0):
# p_smooth = np.r_[excluded_power,p_smooth]
return p_smooth
def getShape(a):
return tuple(a.shape)
cpdef np.ndarray[FUSED_TYPES_t] apply_along_axis(tuple axis,np.ndarray[FUSED_TYPES_t] arr, np.int startindex,np.int endindex,np.ndarray[FUSED_TYPES_t, ndim=1] distances,np.int exclude=1,np.float smooth_length=None):
cpdef np.ndarray[np.float64_t, ndim=1] apply_along_axis(tuple axis, np.ndarray[np.float64_t] arr, np.int_t startindex, np.int_t endindex, np.ndarray[np.float64_t, ndim=1] distances, np.float64_t smooth_length):
cdef tuple nd = arr.ndim
if axis < 0:
axis += nd
if (axis >= nd):
raise ValueError("axis must be less than arr.ndim; axis=%d, rank=%d."
% (axis, nd))
cdef np.ndarray ind = np.zeros(nd-1)
cdef np.ndarray i = np.zeros(nd, 'O')
indlist = list(range(nd))
cdef np.ndarray[np.int_t, ndim=1] ind = np.zeros(nd-1)
cdef np.ndarray[np.int_t, ndim=1] i = np.zeros(nd, 'O')
cdef tuple shape = getShape(arr)
cdef np.ndarray[np.int_t, ndim=1] indlist = list(range(nd))
indlist.remove(axis)
i[axis] = slice(None, None)
cdef np.ndarray outshape = np.asarray(arr.shape).take(indlist)
cdef np.ndarray[np.int_t, ndim=1] outshape = np.asarray(shape).take(indlist)
i.put(indlist, ind)
cdef np.ndarray res = apply_kernel_along_array(arr[tuple(i.tolist())], startindex=startindex,endindex=endindex,k=distances,exclude=exclude,smooth_length=smooth_length)
cdef np.ndarray[np.float64_t, ndim=1] slicedArr = arr[tuple(i.tolist())]
cdef np.ndarray[np.float64_t, ndim=1] res = apply_kernel_along_array(slicedArr, startindex, endindex, distances, smooth_length)
cdef np.int Ntot = np.product(outshape)
'''
Ntot = np.product(outshape)
holdshape = outshape
outshape = list(arr.shape)
outshape[axis] = len(res)
outarr = np.zeros(outshape, np.asarray(res).dtype)
outarr[tuple(i.tolist())] = res
cdef np.int k = 1
cdef np.int n
k = 1
while k < Ntot:
# increment the index
ind[-1] += 1
......@@ -76,7 +74,9 @@ cpdef np.ndarray[FUSED_TYPES_t] apply_along_axis(tuple axis,np.ndarray[FUSED_TYP
ind[n] = 0
n -= 1
i.put(indlist, ind)
res = apply_kernel_along_array(arr[tuple(i.tolist())], startindex=startindex,endindex=endindex,k=distances, exclude=exclude,smooth_length=smooth_length)
res = apply_kernel_along_array(arr[tuple(i.tolist())], startindex=startindex, endindex=endindex, distances=distances, smooth_length=smooth_length)
outarr[tuple(i.tolist())] = res
k += 1
return outarr
\ No newline at end of file
return outarr
'''
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment