elementArrays.py :  » Game-2D-3D » PsychoPy » PsychoPy-0.96.02 » psychopy » demos » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Game 2D 3D » PsychoPy 
PsychoPy » PsychoPy 0.96.02 » psychopy » demos » elementArrays.py
"""This script demonstrates the use of the ElementArrayStim, a highly optimised stimulus for generating 
arrays of similar (but not identical) elements, such as in global form arrays or random dot stimuli.

Elements must have the same basic texture and mask, but can differ in any other way (ori, sf, rgb...).

This is a more complex demo, using numpy arrays to manipulate stimulus characteristics. Don't use
for loops for a large array of stimuli like this.
"""

from psychopy import visual,core,misc,event
import numpy #for maths on arrays
from numpy.random import random,shuffle#we only need these two commands from this lib
win=visual.Window([1024,768],units='pix',monitor='testMonitor')

N=500
fieldSize = 500
elemSize = 40
coherence=0.5

#build a standard (but dynamic!) global form stimulus
globForm = visual.ElementArrayStim(win, nElements=N,sizes=elemSize,sfs=3,
                                                    xys = random([N,2])*fieldSize-fieldSize/2.0) 
    
#calculate the orientations for global form stimulus
def makeCoherentOris(XYs, coherence, formAngle):
    nNew = XYs.shape[0]#length along the first dimension
    newOris = random(nNew)*180 #create a random array 0:180
    #select some elements to be coherent
    possibleIndices = range(nNew)#create an array of indices...
    shuffle(possibleIndices) #...shuffle it'in-place' (without creating a new array)...
    coherentIndices = possibleIndices[0:int(nNew*coherence)]#...and take first nnn elements
    #set the ori of the coherent elements
    theta, radius = misc.cart2pol(XYs[:,0], XYs[:,1]) #get polar coordinates for elements
    newOris[coherentIndices] = theta[coherentIndices]+formAngle
    return newOris
    
globForm.setOris( makeCoherentOris(globForm.xys, coherence, 45) )

#let's give each element a life of 10 frames and give it a new position after that
lives = random(N)*10 #this will be the current life of each element
while True:
    #take a copy of the current xy and ori values
    newXYs = globForm.xys
    newOris = globForm.oris
    #find the dead elemnts and reset their life
    deadElements = (lives>10)
    lives[deadElements]=0
    #for the dead elements update the xy and ori
    newXYs[deadElements,:] = random(newXYs[deadElements,:].shape)*fieldSize-fieldSize/2.0#random array same shape as dead elements
    new = makeCoherentOris(newXYs[deadElements,:], coherence, 45)#for new elements we still want same % coherent
    newOris[deadElements] = new
    #update the oris and xys of the new elements
    globForm.setXYs( newXYs )
    globForm.setOris( newOris )
    
    globForm.draw()
    
    win.flip() 
    lives = lives+1

    #handle key presses each frame
    for key in event.getKeys():
        if key in ['escape','q']:
            print win.fps()
            win.close()
            core.quit()
    event.clearEvents()#keep the event buffer from overflowing
    
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.