Bechmarking Cython code to find centroid of simulated Gaussian images
Clash Royale CLAN TAG#URR8PPP
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;
up vote
0
down vote
favorite
I need to benchmark a code in cython for which I have written a code to find the centroid of 1000 simulated Gaussian images. When executed, it takes 1.58 seconds to run. But when I wrote a pure python code even it took around 1.5 seconds for execution.
Yes, I have made use of numpy modules in both. Could someone please take a look at the cython code? how can I improve the speed of the cython code?
from scipy.interpolate import UnivariateSpline
import matplotlib.pyplot as plt
import pylab as pl
import random
import math
import time
cimport cython
import numpy as np
@cython.boundscheck(False)
@cython.wraparound(False)
def gaussian_2D_distribution(x,y,int mean_x,int mean_y,double sigma):
#cdef double[:,:]f
f = np.exp((-0.5*(np.square(x-mean_x) + np.square(y-mean_y))/sigma))/(sigma*np.sqrt(2*math.pi));
return f
def Centroid2(x,y,f):
temp_x = np.multiply(x,f)
temp_y = np.multiply(y,f)
X_c = (temp_x.sum()/f.sum());
Y_c = -((temp_y.sum()/f.sum())+1);
return(X_c,Y_c);
@cython.boundscheck(False)
@cython.wraparound(False)
def benchmark_func(int no_of_frames):
cdef:
int i
double start
double end
#int shift[10000][2]
double Centroid[10000][2]
#---------------------------------------------Creating Matrices--------------------------------------------------------#
start = time.time()
a = np.arange(-50,50,1);
x = np.matlib.repmat(a,100 ,1);
y = x.transpose();
f = np.zeros([no_of_frames,100,100])
print "Done stage 1 ..."
#---------------------------------------Creating Gaussian Images------------------------------------------------------#
shift = np.zeros([no_of_frames,2])
shift[:,0] = 15
shift[:,1] = 25
for i in range(int (no_of_frames)):
#shift[i][0] = random.randint(-50,50)
#shift[i][1] = random.randint(-50,50)
f[i,:,:] = gaussian_2D_distribution(x,y,shift[i][0],shift[i][1],0.1)
f[i,:,:]= np.flip(f[i,:,:],0)
print "Done stage 2 ..."
#---------------------------------------To find the centroid----------------------------------------------------------#
#start = time.time()
for i in range(int (no_of_frames)):
Centroid[i][:] = Centroid2(x,y,f[i])
end = time.time()
#print Centroid[2]
print(end - start),
print "seconds"
print "Done stage 3 ..."
return Centroid
python numpy cython
add a comment |Â
up vote
0
down vote
favorite
I need to benchmark a code in cython for which I have written a code to find the centroid of 1000 simulated Gaussian images. When executed, it takes 1.58 seconds to run. But when I wrote a pure python code even it took around 1.5 seconds for execution.
Yes, I have made use of numpy modules in both. Could someone please take a look at the cython code? how can I improve the speed of the cython code?
from scipy.interpolate import UnivariateSpline
import matplotlib.pyplot as plt
import pylab as pl
import random
import math
import time
cimport cython
import numpy as np
@cython.boundscheck(False)
@cython.wraparound(False)
def gaussian_2D_distribution(x,y,int mean_x,int mean_y,double sigma):
#cdef double[:,:]f
f = np.exp((-0.5*(np.square(x-mean_x) + np.square(y-mean_y))/sigma))/(sigma*np.sqrt(2*math.pi));
return f
def Centroid2(x,y,f):
temp_x = np.multiply(x,f)
temp_y = np.multiply(y,f)
X_c = (temp_x.sum()/f.sum());
Y_c = -((temp_y.sum()/f.sum())+1);
return(X_c,Y_c);
@cython.boundscheck(False)
@cython.wraparound(False)
def benchmark_func(int no_of_frames):
cdef:
int i
double start
double end
#int shift[10000][2]
double Centroid[10000][2]
#---------------------------------------------Creating Matrices--------------------------------------------------------#
start = time.time()
a = np.arange(-50,50,1);
x = np.matlib.repmat(a,100 ,1);
y = x.transpose();
f = np.zeros([no_of_frames,100,100])
print "Done stage 1 ..."
#---------------------------------------Creating Gaussian Images------------------------------------------------------#
shift = np.zeros([no_of_frames,2])
shift[:,0] = 15
shift[:,1] = 25
for i in range(int (no_of_frames)):
#shift[i][0] = random.randint(-50,50)
#shift[i][1] = random.randint(-50,50)
f[i,:,:] = gaussian_2D_distribution(x,y,shift[i][0],shift[i][1],0.1)
f[i,:,:]= np.flip(f[i,:,:],0)
print "Done stage 2 ..."
#---------------------------------------To find the centroid----------------------------------------------------------#
#start = time.time()
for i in range(int (no_of_frames)):
Centroid[i][:] = Centroid2(x,y,f[i])
end = time.time()
#print Centroid[2]
print(end - start),
print "seconds"
print "Done stage 3 ..."
return Centroid
python numpy cython
Arex
andy
numpy arrays? (I at least see anp.multiply(f, x)
) You can define the type to be a numpy array: see here
â Dair
May 24 at 6:39
Actually that link is outdated, here is the more up to date one.
â Dair
May 24 at 6:42
add a comment |Â
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I need to benchmark a code in cython for which I have written a code to find the centroid of 1000 simulated Gaussian images. When executed, it takes 1.58 seconds to run. But when I wrote a pure python code even it took around 1.5 seconds for execution.
Yes, I have made use of numpy modules in both. Could someone please take a look at the cython code? how can I improve the speed of the cython code?
from scipy.interpolate import UnivariateSpline
import matplotlib.pyplot as plt
import pylab as pl
import random
import math
import time
cimport cython
import numpy as np
@cython.boundscheck(False)
@cython.wraparound(False)
def gaussian_2D_distribution(x,y,int mean_x,int mean_y,double sigma):
#cdef double[:,:]f
f = np.exp((-0.5*(np.square(x-mean_x) + np.square(y-mean_y))/sigma))/(sigma*np.sqrt(2*math.pi));
return f
def Centroid2(x,y,f):
temp_x = np.multiply(x,f)
temp_y = np.multiply(y,f)
X_c = (temp_x.sum()/f.sum());
Y_c = -((temp_y.sum()/f.sum())+1);
return(X_c,Y_c);
@cython.boundscheck(False)
@cython.wraparound(False)
def benchmark_func(int no_of_frames):
cdef:
int i
double start
double end
#int shift[10000][2]
double Centroid[10000][2]
#---------------------------------------------Creating Matrices--------------------------------------------------------#
start = time.time()
a = np.arange(-50,50,1);
x = np.matlib.repmat(a,100 ,1);
y = x.transpose();
f = np.zeros([no_of_frames,100,100])
print "Done stage 1 ..."
#---------------------------------------Creating Gaussian Images------------------------------------------------------#
shift = np.zeros([no_of_frames,2])
shift[:,0] = 15
shift[:,1] = 25
for i in range(int (no_of_frames)):
#shift[i][0] = random.randint(-50,50)
#shift[i][1] = random.randint(-50,50)
f[i,:,:] = gaussian_2D_distribution(x,y,shift[i][0],shift[i][1],0.1)
f[i,:,:]= np.flip(f[i,:,:],0)
print "Done stage 2 ..."
#---------------------------------------To find the centroid----------------------------------------------------------#
#start = time.time()
for i in range(int (no_of_frames)):
Centroid[i][:] = Centroid2(x,y,f[i])
end = time.time()
#print Centroid[2]
print(end - start),
print "seconds"
print "Done stage 3 ..."
return Centroid
python numpy cython
I need to benchmark a code in cython for which I have written a code to find the centroid of 1000 simulated Gaussian images. When executed, it takes 1.58 seconds to run. But when I wrote a pure python code even it took around 1.5 seconds for execution.
Yes, I have made use of numpy modules in both. Could someone please take a look at the cython code? how can I improve the speed of the cython code?
from scipy.interpolate import UnivariateSpline
import matplotlib.pyplot as plt
import pylab as pl
import random
import math
import time
cimport cython
import numpy as np
@cython.boundscheck(False)
@cython.wraparound(False)
def gaussian_2D_distribution(x,y,int mean_x,int mean_y,double sigma):
#cdef double[:,:]f
f = np.exp((-0.5*(np.square(x-mean_x) + np.square(y-mean_y))/sigma))/(sigma*np.sqrt(2*math.pi));
return f
def Centroid2(x,y,f):
temp_x = np.multiply(x,f)
temp_y = np.multiply(y,f)
X_c = (temp_x.sum()/f.sum());
Y_c = -((temp_y.sum()/f.sum())+1);
return(X_c,Y_c);
@cython.boundscheck(False)
@cython.wraparound(False)
def benchmark_func(int no_of_frames):
cdef:
int i
double start
double end
#int shift[10000][2]
double Centroid[10000][2]
#---------------------------------------------Creating Matrices--------------------------------------------------------#
start = time.time()
a = np.arange(-50,50,1);
x = np.matlib.repmat(a,100 ,1);
y = x.transpose();
f = np.zeros([no_of_frames,100,100])
print "Done stage 1 ..."
#---------------------------------------Creating Gaussian Images------------------------------------------------------#
shift = np.zeros([no_of_frames,2])
shift[:,0] = 15
shift[:,1] = 25
for i in range(int (no_of_frames)):
#shift[i][0] = random.randint(-50,50)
#shift[i][1] = random.randint(-50,50)
f[i,:,:] = gaussian_2D_distribution(x,y,shift[i][0],shift[i][1],0.1)
f[i,:,:]= np.flip(f[i,:,:],0)
print "Done stage 2 ..."
#---------------------------------------To find the centroid----------------------------------------------------------#
#start = time.time()
for i in range(int (no_of_frames)):
Centroid[i][:] = Centroid2(x,y,f[i])
end = time.time()
#print Centroid[2]
print(end - start),
print "seconds"
print "Done stage 3 ..."
return Centroid
python numpy cython
edited May 24 at 5:56
Sam Onela
5,75961543
5,75961543
asked May 24 at 5:22
aishwarya selvaraj
1
1
Arex
andy
numpy arrays? (I at least see anp.multiply(f, x)
) You can define the type to be a numpy array: see here
â Dair
May 24 at 6:39
Actually that link is outdated, here is the more up to date one.
â Dair
May 24 at 6:42
add a comment |Â
Arex
andy
numpy arrays? (I at least see anp.multiply(f, x)
) You can define the type to be a numpy array: see here
â Dair
May 24 at 6:39
Actually that link is outdated, here is the more up to date one.
â Dair
May 24 at 6:42
Are
x
and y
numpy arrays? (I at least see a np.multiply(f, x)
) You can define the type to be a numpy array: see hereâ Dair
May 24 at 6:39
Are
x
and y
numpy arrays? (I at least see a np.multiply(f, x)
) You can define the type to be a numpy array: see hereâ Dair
May 24 at 6:39
Actually that link is outdated, here is the more up to date one.
â Dair
May 24 at 6:42
Actually that link is outdated, here is the more up to date one.
â Dair
May 24 at 6:42
add a comment |Â
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f195061%2fbechmarking-cython-code-to-find-centroid-of-simulated-gaussian-images%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Are
x
andy
numpy arrays? (I at least see anp.multiply(f, x)
) You can define the type to be a numpy array: see hereâ Dair
May 24 at 6:39
Actually that link is outdated, here is the more up to date one.
â Dair
May 24 at 6:42