Bechmarking Cython code to find centroid of simulated Gaussian images

The name of the pictureThe name of the pictureThe name of the pictureClash 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






share|improve this question





















  • 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
















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






share|improve this question





















  • 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












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






share|improve this question













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








share|improve this question












share|improve this question




share|improve this question








edited May 24 at 5:56









Sam Onela

5,75961543




5,75961543









asked May 24 at 5:22









aishwarya selvaraj

1




1











  • 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
















  • 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















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















active

oldest

votes











Your Answer




StackExchange.ifUsing("editor", function ()
return StackExchange.using("mathjaxEditing", function ()
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
);
);
, "mathjax-editing");

StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "196"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);








 

draft saved


draft discarded


















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



































active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes










 

draft saved


draft discarded


























 


draft saved


draft discarded














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













































































Popular posts from this blog

Greedy Best First Search implementation in Rust

Function to Return a JSON Like Objects Using VBA Collections and Arrays

C++11 CLH Lock Implementation