How to improve the speed of display implemented using matplotlib

Hi all,
I have 2 files created, one a .pyx file with a class named AndorCamersSDK
which has a function named LiveAcquisition(). The second file, a .py file
with a Class AndorCameraGUI which makes use of Tkinter to create a GUI.
This class has function LivePlot(), and RepeatPlot().Inside
LiveAcquisition() I am acquiring N number of frames and after acquiring
each frame I need to display it using LivePlot() and RepeatPlot(). Without
the display, just acquiring and storing for 300 frames takes 6.2 for
execution, which is fine by me. But When I start displaying even for 100
frames it takes 54sec. I need to acquire and display within 6 sec for 300
frames. How do I solve this?

?

File 1: .pyx code ; Present inside class AndorCameraSDK

def LiveAcquisition(self):
    for i in range(no_of_frames):
        data[i,:,:] = PyArray_NewFromDescr(<PyTypeObject *>
np.ndarray, np.dtype('<H'), 2,dims, strides,pBuf, np.NPY_C_CONTIGUOUS,
None)
        if (i==0):
            self.master.LivePlot(data[i,:,:])
        elif (i==2) or (i==15) or (i ==65) or (i ==96):
            self.master.RepeatPlot(data[i,:,:])
        else:
            pass

?

File 2 : .py code; Below functions are present inside a class named
AndorCameraGUI

def LivePlot(self,image):
    self.count = 0
    self.fig = Figure(figsize = (4, 5))
    self.fig.patch.set_facecolor('xkcd:light grey') # When this is
removed a white color is seen in the background of the figure
    self.a = self.fig.add_subplot(111)
    self.a.set_xlim([0, self.image_width/int(self.HBin)])
    self.a.set_ylim([0, self.image_height/int(self.VBin)])
    image = image.transpose()
    self.a.imshow(image,'gray')
    self.canvas = FigureCanvasTkAgg(self.fig,self.master)
    self.canvas.draw()
    self.canvas.get_tk_widget().pack(side =LEFT)
    self.toolbar = NavigationToolbar2TkAgg(self.canvas,self.master)
    self.toolbar.update()
    self.canvas._tkcanvas.pack(side = LEFT)# change this to TOP so
thee the navigation toolbar on the left down
def RepeatPlot(self,image):
    image = image.transpose()
    self.a.imshow(image,'gray')
    self.canvas.draw()

···

--
Regards,
Aishwarya Selvaraj
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/matplotlib-users/attachments/20180605/2abb60d9/attachment-0001.html>

I think that at least part of the problem is that in RepeatPlot you are
piling one image plot on top of another. Instead, keep a reference to
the image object (let's call it "im") returned from your first call to
imshow, in LivePlot, and then, in RepeatPlot, instead of calling imshow
again, just call "self.im.set_data(image)".

Eric

···

On 2018/06/04 7:30 PM, aishwarya selvaraj wrote:

Hi all,
I have 2 files created, one a .pyx file with a class named
AndorCamersSDK which has a function named LiveAcquisition(). The second
file, a .py file with a Class AndorCameraGUI which makes use of Tkinter
to create a GUI. This class has function LivePlot(), and
RepeatPlot().Inside LiveAcquisition() I am acquiring N number of frames
and after acquiring each frame I need to display it using LivePlot() and
RepeatPlot(). Without the display, just acquiring and storing for 300
frames takes 6.2 for execution, which is fine by me. But When I start
displaying even for 100 frames it takes 54sec. I need to acquire and
display within 6 sec for 300 frames. How do I solve this?

?

File 1: .pyx code ; Present inside class AndorCameraSDK

>defLiveAcquisition(self):fori
inrange(no_of_frames):data[i,:,:]=PyArray_NewFromDescr(<PyTypeObject*>np.ndarray,np.dtype('<H'),2,dims,strides,pBuf,np.NPY_C_CONTIGUOUS,None)if(i==0):self.master.LivePlot(data[i,:,:])elif(i==2)or(i==15)or(i
==65)or(i ==96):self.master.RepeatPlot(data[i,:,:])else:pass|

?

File 2 : .py code; Below functions are present inside a class named
AndorCameraGUI

>defLivePlot(self,image):self.count =0self.fig =Figure(figsize
=(4,5))self.fig.patch.set_facecolor('xkcd:light grey')# When this is
removed a white color is seen in the background of the figureself.a
=self.fig.add_subplot(111)self.a.set_xlim([0,self.image_width/int(self.HBin)])self.a.set_ylim([0,self.image_height/int(self.VBin)])image
=image.transpose()self.a.imshow(image,'gray')self.canvas
=FigureCanvasTkAgg(self.fig,self.master)self.canvas.draw()self.canvas.get_tk_widget().pack(side
=LEFT)self.toolbar
=NavigationToolbar2TkAgg(self.canvas,self.master)self.toolbar.update()self.canvas._tkcanvas.pack(side
=LEFT)#change this to TOP so thee the navigation toolbar on the left
down defRepeatPlot(self,image):image
=image.transpose()self.a.imshow(image,'gray')self.canvas.draw()|

--
Regards,
Aishwarya Selvaraj

_______________________________________________
Matplotlib-users mailing list
Matplotlib-users at python.org
Matplotlib-users Info Page

I agree with Eric about only creating one image and updating the data for that one image. I've also worked with Andor cameras and data acquisition. I did a few things to speed up the visualization and data collection. 1) I saved every frame, but I didn't change the image displayed for every picture I took. 2) I parallelized my code using the multiprocessing module with shared arrays and set the display to only pull data from the shared array when it was ready to display the next frame. If you do that and find that it still is going slower than you would like for displaying the data, you can course grain your display. For example, instead of having a 100 x 100 image from your 100 x 100 array, you could display a 50 x 50 image taking every other pixel from your array (e.g. self.im.set_data(data[i,::2,::2]).

Kelly

···

-----Original Message-----
From: Matplotlib-users <matplotlib-users-bounces+kellyw=uoregon.edu@python.org> On Behalf Of Eric Firing
Sent: Monday, June 4, 2018 11:05 PM
To: matplotlib-users at python.org
Subject: Re: [Matplotlib-users] How to improve the speed of display implemented using matplotlib

I think that at least part of the problem is that in RepeatPlot you are piling one image plot on top of another. Instead, keep a reference to the image object (let's call it "im") returned from your first call to imshow, in LivePlot, and then, in RepeatPlot, instead of calling imshow again, just call "self.im.set_data(image)".

Eric

On 2018/06/04 7:30 PM, aishwarya selvaraj wrote:

Hi all,
I have 2 files created, one a .pyx file with a class named
AndorCamersSDK which has a function named LiveAcquisition(). The
second file, a .py file with a Class AndorCameraGUI which makes use of
Tkinter to create a GUI. This class has function LivePlot(), and
RepeatPlot().Inside LiveAcquisition() I am acquiring N number of
frames and after acquiring each frame I need to display it using
LivePlot() and RepeatPlot(). Without the display, just acquiring and
storing for 300 frames takes 6.2 for execution, which is fine by me.
But When I start displaying even for 100 frames it takes 54sec. I need
to acquire and display within 6 sec for 300 frames. How do I solve this?

?

File 1: .pyx code ; Present inside class AndorCameraSDK

>defLiveAcquisition(self):fori
inrange(no_of_frames):data[i,:,:]=PyArray_NewFromDescr(<PyTypeObject*>
np.ndarray,np.dtype('<H'),2,dims,strides,pBuf,np.NPY_C_CONTIGUOUS,None
)if(i==0):self.master.LivePlot(data[i,:,:])elif(i==2)or(i==15)or(i
==65)or(i ==96):self.master.RepeatPlot(data[i,:,:])else:pass|

?

File 2 : .py code; Below functions are present inside a class named
AndorCameraGUI

>defLivePlot(self,image):self.count =0self.fig =Figure(figsize
=(4,5))self.fig.patch.set_facecolor('xkcd:light grey')# When this is
removed a white color is seen in the background of the figureself.a
=self.fig.add_subplot(111)self.a.set_xlim([0,self.image_width/int(self
.HBin)])self.a.set_ylim([0,self.image_height/int(self.VBin)])image
=image.transpose()self.a.imshow(image,'gray')self.canvas
=FigureCanvasTkAgg(self.fig,self.master)self.canvas.draw()self.canvas.
get_tk_widget().pack(side
=LEFT)self.toolbar
=NavigationToolbar2TkAgg(self.canvas,self.master)self.toolbar.update()
self.canvas._tkcanvas.pack(side =LEFT)#change this to TOP so thee the
navigation toolbar on the left down defRepeatPlot(self,image):image
=image.transpose()self.a.imshow(image,'gray')self.canvas.draw()|

--
Regards,
Aishwarya Selvaraj

_______________________________________________
Matplotlib-users mailing list
Matplotlib-users at python.org
Matplotlib-users Info Page

_______________________________________________
Matplotlib-users mailing list
Matplotlib-users at python.org
https://mail.python.org/mailman/listinfo/matplotlib-users