2010-10-15 5 views
8

के साथ निरंतर अद्यतन छवि के रूप में NumPy सरणी प्रदर्शित करें मुझे पाइथन में न्यूमपी और साइपी का उपयोग करके एक सिमुलेशन मॉडल चल रहा है और यह प्रत्येक पुनरावृत्ति के आउटपुट के रूप में 2 डी न्यूमपी सरणी उत्पन्न करता है। मैं matplotlib और imshow फ़ंक्शन का उपयोग कर इस आउटपुट को एक छवि के रूप में प्रदर्शित कर रहा हूं। हालांकि, मैं Glumpy बारे में पता चला है, और इसके प्रलेखन पृष्ठ पर यह कहते हैं:ग्लूपी

IPython खोल के लिए धन्यवाद, glumpy जब उनकी सामग्री बदल गया है इंटरैक्टिव मोड में दौड़ा जा सकता है जहाँ आप प्रदर्शित सरणियों में लाइव अपडेट अनुभव कर सकते हैं ।

हालांकि, मैं यह नहीं समझ सकता कि उनके द्वारा दिए गए उदाहरणों के साथ ऐसा कैसे किया जाए। असल में मेरा मॉडल एक एकल फ़ंक्शन के रूप में चलता है जिसमें इसके चलते पुनरावृत्तियों की संख्या के लिए लूप के लिए एक बड़ा लूप होता है। लूप के प्रत्येक पुनरावृत्ति के अंत में मैं सरणी प्रदर्शित करना चाहता हूं। फिलहाल मैं छवि को पीएनजी फ़ाइल में सहेजने के लिए matplotlib का उपयोग कर रहा हूं, क्योंकि मैटलप्लिब के माध्यम से स्क्रीन पर इसे प्रदर्शित करने से पाइथन प्रक्रिया को फ्रीज किया जाता है।

मुझे यकीन है कि ग्लेम्पी के साथ ऐसा करने का एक तरीका है, मुझे यकीन नहीं है कि कैसे, और मुझे कोई उपयोगी ट्यूटोरियल नहीं मिल रहा है।

+0

स्क्रिप्ट को फ्रीज करने वाले मैटलप्लिब के साथ आपके पास समस्या काफी आम है और आमतौर पर केवल एक साधारण फिक्स की आवश्यकता होती है, जैसे कि * ड्रा * के बजाय * ड्रा * का उपयोग करना, या * -pylab * मोड आदि में ipython चलाना, – tom10

+0

कर सकते हैं आप उस कोड को पोस्ट करते हैं जिसका उपयोग आप सरणी को प्रदर्शित करने के लिए कर रहे हैं? भी, आपका 'matplotlib .__ version__' क्या है, क्योंकि हाल ही में कुछ महत्वपूर्ण तरीकों से जीयूआई लूप को संभालने का तरीका बदल गया है। – wim

उत्तर

10

गड़बड़ दस्तावेज काफी असहनीय है! इधर, एक सरल अनुकरण का एक उदाहरण है glumpymatplotlib के खिलाफ के साथ सरणी दृश्य की तुलना:

import numpy as np 
import glumpy 
from OpenGL import GLUT as glut 
from time import time 
from matplotlib.pyplot import subplots,close 
from matplotlib import cm 

def randomwalk(dims=(256,256),n=3,sigma=10,alpha=0.95,seed=1): 
    """ A simple random walk with memory """ 
    M = np.zeros(dims,dtype=np.float32) 
    r,c = dims 
    gen = np.random.RandomState(seed) 
    pos = gen.rand(2,n)*((r,),(c,)) 
    old_delta = gen.randn(2,n)*sigma 
    while 1: 
     delta = (1.-alpha)*gen.randn(2,n)*sigma + alpha*old_delta 
     pos += delta 
     for ri,ci in pos.T: 
      if not (0. <= ri < r) : ri = abs(ri % r) 
      if not (0. <= ci < c) : ci = abs(ci % c) 
      M[ri,ci] += 1 
     old_delta = delta 
     yield M 

def mplrun(niter=1000): 
    """ Visualise the simulation using matplotlib, using blit for 
    improved speed""" 
    fig,ax = subplots(1,1) 
    rw = randomwalk() 
    im = ax.imshow(rw.next(),interpolation='nearest',cmap=cm.hot,animated=True) 
    fig.canvas.draw() 
    background = fig.canvas.copy_from_bbox(ax.bbox) # cache the background 

    tic = time() 
    for ii in xrange(niter): 
     im.set_data(rw.next())   # update the image data 
     fig.canvas.restore_region(background) # restore background 
     ax.draw_artist(im)   # redraw the image 
     fig.canvas.blit(ax.bbox)  # redraw the axes rectangle 

    close(fig) 
    print "Matplotlib average FPS: %.2f" %(niter/(time()-tic)) 

def gprun(niter=1000): 
    """ Visualise the same simulation using Glumpy """ 
    rw = randomwalk() 
    M = rw.next() 

    # create a glumpy figure 
    fig = glumpy.figure((512,512)) 

    # the Image.data attribute is a referenced copy of M - when M 
    # changes, the image data also gets updated 
    im = glumpy.image.Image(M,colormap=glumpy.colormap.Hot) 

    @fig.event 
    def on_draw(): 
     """ called in the simulation loop, and also when the 
     figure is resized """ 
     fig.clear() 
     im.update() 
     im.draw(x=0, y=0, z=0, width=fig.width, height=fig.height) 

    tic = time() 
    for ii in xrange(niter): 
     M = rw.next()   # update the array   
     glut.glutMainLoopEvent() # dispatch queued window events 
     on_draw()   # update the image in the back buffer 
     glut.glutSwapBuffers()  # swap the buffers so image is displayed 

    fig.window.hide() 
    print "Glumpy average FPS: %.2f" %(niter/(time()-tic)) 

if __name__ == "__main__": 
    mplrun() 
    gprun() 

GTKAgg मेरी बैकएंड के रूप में साथ matplotlib का उपयोग करते हुए और पृष्ठभूमि हर बार ड्राइंग से बचने के लिए blit का उपयोग कर, मैं लगभग 95 एफपीएस हिट कर सकते हैं। Glumpy के साथ मुझे लगभग 250-300 एफपीएस मिलते हैं, भले ही मैं वर्तमान में अपने लैपटॉप पर काफी गड़बड़ ग्राफिक्स सेटअप करता हूं। ऐसा कहने के बाद, Glumpy काम करने के लिए थोड़ा और अधिक स्पष्ट रूप से है, और जब तक आप विशाल मैट्रिस से निपट नहीं रहे हैं, या आपको किसी भी कारण से बहुत अधिक फ़्रेमेट की आवश्यकता है, तो मैं के साथ matplotlib का उपयोग कर चिपकूँगा।