To control the screen and visual stimuli for experiments
Used to set up a context in which to draw objects, using either PyGame (python’s SDL binding) or pyglet.
The pyglet backend allows multiple windows to be created, allows the user to specify which screen to use (if more than one is available, duh!) and allows movies to be rendered.
Pygame has fewer bells and whistles, but does seem a little faster in text rendering. Pygame is used for all sound production and for monitoring the joystick.
| Parameters: |
|
|---|
Flip the front and back buffers after drawing everything for your frame. (This replaces the win.update() method, better reflecting what is happening underneath).
win.flip(clearBuffer=True)#results in a clear screen after flipping win.flip(clearBuffer=False)#the screen is not cleared (so represent the previous screen)
Capture the current Window as an image. This can be done at any time (usually after a .update() command).
Frames are stored in memory until a .saveMovieFrames(filename) command is issued. You can issue getMovieFrame() as often as you like and then save them all in one go when finished.
Save recorded screen frame intervals to disk, as comma-separated values.
| Parameters: |
|---|
Writes any captured frames to disk. Will write any format that is understood by PIL (tif, jpg, bmp, png...)
| Parameters: |
mpgCodec: the code to be used by pymedia if the filename ends in .mpg fps: the frame rate to be used throughout the movie only for quicktime (.mov) movies |
|---|
Set the color of the window.
NB This command sets the color that the blank screen will have on the next clear operation. As a result it effectively takes TWO flip() operations to become visible (the first uses the color to create the new screen the second presents that screen to the viewer).
See Color spaces for further information about the ways to specify colors and their various implications.
| Parameters: |
|---|
Can be specified in one of many ways. If a string is given then it is interpreted as the name of the color. Any of the standard html/X11 color names <http://www.w3schools.com/html/html_colornames.asp> can be used. e.g.:
myStim.setColor('white')
myStim.setColor('RoyalBlue')#(the case is actually ignored)
A hex value can be provided, also formatted as with web colors. This can be provided as a string that begins with # (not using python’s usual 0x000000 format):
myStim.setColor('#DDA0DD')#DDA0DD is hexadecimal for plum
You can also provide a triplet of values, which refer to the coordinates in one of the Color spaces. If no color space is specified then the color space most recently used for this stimulus is used again.
myStim.setColor([1.0,-1.0,-1.0], ‘rgb’)#a red color in rgb space myStim.setColor([0.0,45.0,1.0], ‘dkl’) #DKL space with elev=0, azimuth=45 myStim.setColor([0,0,255], ‘rgb255’) #a blue stimulus using rgb255 space
Lastly, a single number can be provided, x, which is equivalent to providing [x,x,x].
myStim.setColor(255, ‘rgb255’) #all guns o max
colorSpace : string or None
defining which of the Color spaces to use. For strings and hex values this is not needed. If None the default colorSpace for the stimulus is used (defined during initialisation).
operation : one of ‘+’,’-‘,’*’,’/’, or ‘’ for no operation (simply replace value)
for colors specified as a triplet of values (or single intensity value) the new value will perform this operation on the previous color
thisStim.setColor([1,1,1],’rgb255’,’+’)#increment all guns by 1 value thisStim.setColor(-1, ‘rgb’, ‘*’) #multiply the color by -1 (which in this space inverts the contrast) thisStim.setColor([10,0,0], ‘dkl’, ‘+’)#raise the elevation from the isoluminant plane by 10 deg
Sets the visibility of the mouse cursor.
If Window was initilised with noGUI=True then the mouse is initially set to invisible, otherwise it will initially be visible.
To provide accurate measures of frame intervals, to determine whether frames are being dropped. Set this to False while the screen is not being updated e.g. during event.waitkeys() and set to True during critical parts of the script
This method is called from within the draw routine and sets the scale of the OpenGL context to map between units. Could potentially be called by the user in order to draw OpenGl objects manually in each frame.
The units can be ‘norm’(normalised),’pix’(pixels),’cm’ or ‘stroke_font’. The font parameter is only used if units=’stroke_font’
Stimulus object for drawing arbitrary bitmaps, textures and shapes. One of the main stimuli for PsychoPy.
Formally PatchStim is just a texture behind an optional transparency mask (an ‘alpha mask’). Both the texture and mask can be arbitrary bitmaps and their combination allows an enormous variety of stimuli to be drawn in realtime.
Examples:
myGrat = PatchStim(tex='sin',mask='circle') #gives a circular patch of grating
myGabor = PatchStim(tex='sin',mask='gauss') #gives a 'Gabor' patchgrating
myImage = PatchStim(tex='face.jpg',mask=None) #simply draws the image face.jpg
An PatchStim can be rotated scaled and shifted in position, its texture can be drifted in X and/or Y and it can have a spatial frequency in X and/or Y (for an image file that simply draws multiple copies in the patch).
Also since transparency can be controlled two PatchStims can combine e.g. to form a plaid.
Using Patchstim with images from disk (jpg, tif, pgn...)
Ideally images to be rendered should be square with ‘power-of-2’ dimensions e.g. 16x16, 128x128. Any image that is not will be upscaled (with linear interp) to the nearest such texture by PsychoPy. The size of the stimulus should be specified in the normal way using the appropriate units (deg, pix, cm...). Be sure to get the aspect ration the same as the image (if you don’t want it stretched!).
Why can’t I have a normal image, drawn pixel-by-pixel? PatchStims are rendered using OpenGL textures. This is more powerful than using simple screen blitting - it allows the rotation, masking, transparency to work. It is still necessary to have power-of-2 textures on most graphics cards.
| Parameters: |
|
|---|
Set the color of the stimulus. See Color spaces for further information about the various ways to specify colors and their various implications.
| Parameters: |
|---|
Can be specified in one of many ways. If a string is given then it is interpreted as the name of the color. Any of the standard html/X11 color names <http://www.w3schools.com/html/html_colornames.asp> can be used. e.g.:
myStim.setColor('white')
myStim.setColor('RoyalBlue')#(the case is actually ignored)
A hex value can be provided, also formatted as with web colors. This can be provided as a string that begins with # (not using python’s usual 0x000000 format):
myStim.setColor('#DDA0DD')#DDA0DD is hexadecimal for plum
You can also provide a triplet of values, which refer to the coordinates in one of the Color spaces. If no color space is specified then the color space most recently used for this stimulus is used again.
myStim.setColor([1.0,-1.0,-1.0], ‘rgb’)#a red color in rgb space myStim.setColor([0.0,45.0,1.0], ‘dkl’) #DKL space with elev=0, azimuth=45 myStim.setColor([0,0,255], ‘rgb255’) #a blue stimulus using rgb255 space
Lastly, a single number can be provided, x, which is equivalent to providing [x,x,x].
myStim.setColor(255, ‘rgb255’) #all guns o max
colorSpace : string or None
defining which of the Color spaces to use. For strings and hex values this is not needed. If None the default colorSpace for the stimulus is used (defined during initialisation).
operation : one of ‘+’,’-‘,’*’,’/’, or ‘’ for no operation (simply replace value)
for colors specified as a triplet of values (or single intensity value) the new value will perform this operation on the previous color
thisStim.setColor([1,1,1],’rgb255’,’+’)#increment all guns by 1 value thisStim.setColor(-1, ‘rgb’, ‘*’) #multiply the color by -1 (which in this space inverts the contrast) thisStim.setColor([10,0,0], ‘dkl’, ‘+’)#raise the elevation from the isoluminant plane by 10 deg
A simple stimulus for loading images from a file and presenting at exactly the resolution and color in the file (subject to gamma correction if set).
Unlike the PatchStim, this type of stimulus cannot be rescaled, rotated or masked (although flipping horizontally or vertically is possible). Drawing will also tend to be marginally slower, because the image isn’t preloaded to the gfx card. The advantage, however is that the stimulus will always be in its original aspect ratio, with no interplotation or other transformation. It is always
SimpleImageStim does not support a depth parameter (the OpenGL method that draws the pixels does not support it). Simple images will obscure any other stimulus type.
Also, unlike the PatchStim (whose textures should be square and power-of-two in size, there is no restriction on the size of images for the SimpleImageStim
| Parameters: |
|
|---|
Set the color of the stimulus. See Color spaces for further information about the various ways to specify colors and their various implications.
| Parameters: |
|---|
Can be specified in one of many ways. If a string is given then it is interpreted as the name of the color. Any of the standard html/X11 color names <http://www.w3schools.com/html/html_colornames.asp> can be used. e.g.:
myStim.setColor('white')
myStim.setColor('RoyalBlue')#(the case is actually ignored)
A hex value can be provided, also formatted as with web colors. This can be provided as a string that begins with # (not using python’s usual 0x000000 format):
myStim.setColor('#DDA0DD')#DDA0DD is hexadecimal for plum
You can also provide a triplet of values, which refer to the coordinates in one of the Color spaces. If no color space is specified then the color space most recently used for this stimulus is used again.
myStim.setColor([1.0,-1.0,-1.0], ‘rgb’)#a red color in rgb space myStim.setColor([0.0,45.0,1.0], ‘dkl’) #DKL space with elev=0, azimuth=45 myStim.setColor([0,0,255], ‘rgb255’) #a blue stimulus using rgb255 space
Lastly, a single number can be provided, x, which is equivalent to providing [x,x,x].
myStim.setColor(255, ‘rgb255’) #all guns o max
colorSpace : string or None
defining which of the Color spaces to use. For strings and hex values this is not needed. If None the default colorSpace for the stimulus is used (defined during initialisation).
operation : one of ‘+’,’-‘,’*’,’/’, or ‘’ for no operation (simply replace value)
for colors specified as a triplet of values (or single intensity value) the new value will perform this operation on the previous color
thisStim.setColor([1,1,1],’rgb255’,’+’)#increment all guns by 1 value thisStim.setColor(-1, ‘rgb’, ‘*’) #multiply the color by -1 (which in this space inverts the contrast) thisStim.setColor([10,0,0], ‘dkl’, ‘+’)#raise the elevation from the isoluminant plane by 10 deg
Class of text stimuli to be displayed in a Window
| Parameters: |
|
|---|
Draw the stimulus in its relevant window. You must call this method after every MyWin.flip() if you want the stimulus to appear on that frame and then update the screen again.
If win is specified then override the normal window of this stimulus.
Set the color of the stimulus. See Color spaces for further information about the various ways to specify colors and their various implications.
| Parameters: |
|---|
Can be specified in one of many ways. If a string is given then it is interpreted as the name of the color. Any of the standard html/X11 color names <http://www.w3schools.com/html/html_colornames.asp> can be used. e.g.:
myStim.setColor('white')
myStim.setColor('RoyalBlue')#(the case is actually ignored)
A hex value can be provided, also formatted as with web colors. This can be provided as a string that begins with # (not using python’s usual 0x000000 format):
myStim.setColor('#DDA0DD')#DDA0DD is hexadecimal for plum
You can also provide a triplet of values, which refer to the coordinates in one of the Color spaces. If no color space is specified then the color space most recently used for this stimulus is used again.
myStim.setColor([1.0,-1.0,-1.0], ‘rgb’)#a red color in rgb space myStim.setColor([0.0,45.0,1.0], ‘dkl’) #DKL space with elev=0, azimuth=45 myStim.setColor([0,0,255], ‘rgb255’) #a blue stimulus using rgb255 space
Lastly, a single number can be provided, x, which is equivalent to providing [x,x,x].
myStim.setColor(255, ‘rgb255’) #all guns o max
colorSpace : string or None
defining which of the Color spaces to use. For strings and hex values this is not needed. If None the default colorSpace for the stimulus is used (defined during initialisation).
operation : one of ‘+’,’-‘,’*’,’/’, or ‘’ for no operation (simply replace value)
for colors specified as a triplet of values (or single intensity value) the new value will perform this operation on the previous color
thisStim.setColor([1,1,1],’rgb255’,’+’)#increment all guns by 1 value thisStim.setColor(-1, ‘rgb’, ‘*’) #multiply the color by -1 (which in this space inverts the contrast) thisStim.setColor([10,0,0], ‘dkl’, ‘+’)#raise the elevation from the isoluminant plane by 10 deg
A stimulus class for playing movies (mpeg, avi, etc...) in PsychoPy.
examples:
mov = visual.MovieStim(myWin, 'testMovie.mp4', fliVert=False)
print mov.duration
print mov.format.width, mov.format.height #give the original size of the movie in pixels
mov.draw() #draw the current frame (automagically determined)
See MovieStim.py for demo.
| Parameters: |
|
|---|
Draw the current frame to a particular visual.Window (or to the default win for this object if not specified). The current position in the movie will be determined automatically.
This method should be called on every frame that the movie is meant to appear
Set the color of the stimulus. See Color spaces for further information about the various ways to specify colors and their various implications.
| Parameters: |
|---|
Can be specified in one of many ways. If a string is given then it is interpreted as the name of the color. Any of the standard html/X11 color names <http://www.w3schools.com/html/html_colornames.asp> can be used. e.g.:
myStim.setColor('white')
myStim.setColor('RoyalBlue')#(the case is actually ignored)
A hex value can be provided, also formatted as with web colors. This can be provided as a string that begins with # (not using python’s usual 0x000000 format):
myStim.setColor('#DDA0DD')#DDA0DD is hexadecimal for plum
You can also provide a triplet of values, which refer to the coordinates in one of the Color spaces. If no color space is specified then the color space most recently used for this stimulus is used again.
myStim.setColor([1.0,-1.0,-1.0], ‘rgb’)#a red color in rgb space myStim.setColor([0.0,45.0,1.0], ‘dkl’) #DKL space with elev=0, azimuth=45 myStim.setColor([0,0,255], ‘rgb255’) #a blue stimulus using rgb255 space
Lastly, a single number can be provided, x, which is equivalent to providing [x,x,x].
myStim.setColor(255, ‘rgb255’) #all guns o max
colorSpace : string or None
defining which of the Color spaces to use. For strings and hex values this is not needed. If None the default colorSpace for the stimulus is used (defined during initialisation).
operation : one of ‘+’,’-‘,’*’,’/’, or ‘’ for no operation (simply replace value)
for colors specified as a triplet of values (or single intensity value) the new value will perform this operation on the previous color
thisStim.setColor([1,1,1],’rgb255’,’+’)#increment all guns by 1 value thisStim.setColor(-1, ‘rgb’, ‘*’) #multiply the color by -1 (which in this space inverts the contrast) thisStim.setColor([10,0,0], ‘dkl’, ‘+’)#raise the elevation from the isoluminant plane by 10 deg
Create geometric (vector) shapes by defining vertex locations.
Shapes can be outlines or filled, by setting lineRGB and fillRGB to rgb triplets, or None. They can also be rotated (stim.setOri(__)) and translated (stim.setPos(__)) like any other stimulus.
NB for now the fill of objects is performed using glBegin(GL_POLYGON) and that is limited to convex shapes. With concavities you get unpredictable results (e.g. add a fill color to the arrow stim below). To create concavities, you can combine multiple shapes, or stick to just outlines. (If anyone wants to rewrite ShapeStim to use glu tesselators that would be great!)
| Parameters: |
lineRGB :
fillRGB :
|
|---|
Set the color of the stimulus. See Color spaces for further information about the various ways to specify colors and their various implications.
| Parameters: |
|---|
Can be specified in one of many ways. If a string is given then it is interpreted as the name of the color. Any of the standard html/X11 color names <http://www.w3schools.com/html/html_colornames.asp> can be used. e.g.:
myStim.setColor('white')
myStim.setColor('RoyalBlue')#(the case is actually ignored)
A hex value can be provided, also formatted as with web colors. This can be provided as a string that begins with # (not using python’s usual 0x000000 format):
myStim.setColor('#DDA0DD')#DDA0DD is hexadecimal for plum
You can also provide a triplet of values, which refer to the coordinates in one of the Color spaces. If no color space is specified then the color space most recently used for this stimulus is used again.
myStim.setColor([1.0,-1.0,-1.0], ‘rgb’)#a red color in rgb space myStim.setColor([0.0,45.0,1.0], ‘dkl’) #DKL space with elev=0, azimuth=45 myStim.setColor([0,0,255], ‘rgb255’) #a blue stimulus using rgb255 space
Lastly, a single number can be provided, x, which is equivalent to providing [x,x,x].
myStim.setColor(255, ‘rgb255’) #all guns o max
colorSpace : string or None
defining which of the Color spaces to use. For strings and hex values this is not needed. If None the default colorSpace for the stimulus is used (defined during initialisation).
operation : one of ‘+’,’-‘,’*’,’/’, or ‘’ for no operation (simply replace value)
for colors specified as a triplet of values (or single intensity value) the new value will perform this operation on the previous color
thisStim.setColor([1,1,1],’rgb255’,’+’)#increment all guns by 1 value thisStim.setColor(-1, ‘rgb’, ‘*’) #multiply the color by -1 (which in this space inverts the contrast) thisStim.setColor([10,0,0], ‘dkl’, ‘+’)#raise the elevation from the isoluminant plane by 10 deg
Set the xy values of the vertices (relative to the centre of the field). Values should be:
- an array/list of Nx2 coordinates.
Stimulus object for drawing radial stimuli, like an annulus, a rotating wedge, a checkerboard etc...
Ideal for fMRI retinotopy stimuli!
Many of the capabilities are built on top of the PatchStim.
This stimulus is still relatively new and I’m finding occasional gliches. it also takes longer to draw than a typical PatchStim, so not recommended for tasks where high frame rates are needed.
| Parameters: |
|
|---|
Draw the stimulus in its relevant window. You must call this method after every MyWin.flip() if you want the stimulus to appear on that frame and then update the screen again.
If win is specified then override the normal window of this stimulus.
Set the color of the stimulus. See Color spaces for further information about the various ways to specify colors and their various implications.
| Parameters: |
|---|
Can be specified in one of many ways. If a string is given then it is interpreted as the name of the color. Any of the standard html/X11 color names <http://www.w3schools.com/html/html_colornames.asp> can be used. e.g.:
myStim.setColor('white')
myStim.setColor('RoyalBlue')#(the case is actually ignored)
A hex value can be provided, also formatted as with web colors. This can be provided as a string that begins with # (not using python’s usual 0x000000 format):
myStim.setColor('#DDA0DD')#DDA0DD is hexadecimal for plum
You can also provide a triplet of values, which refer to the coordinates in one of the Color spaces. If no color space is specified then the color space most recently used for this stimulus is used again.
myStim.setColor([1.0,-1.0,-1.0], ‘rgb’)#a red color in rgb space myStim.setColor([0.0,45.0,1.0], ‘dkl’) #DKL space with elev=0, azimuth=45 myStim.setColor([0,0,255], ‘rgb255’) #a blue stimulus using rgb255 space
Lastly, a single number can be provided, x, which is equivalent to providing [x,x,x].
myStim.setColor(255, ‘rgb255’) #all guns o max
colorSpace : string or None
defining which of the Color spaces to use. For strings and hex values this is not needed. If None the default colorSpace for the stimulus is used (defined during initialisation).
operation : one of ‘+’,’-‘,’*’,’/’, or ‘’ for no operation (simply replace value)
for colors specified as a triplet of values (or single intensity value) the new value will perform this operation on the previous color
thisStim.setColor([1,1,1],’rgb255’,’+’)#increment all guns by 1 value thisStim.setColor(-1, ‘rgb’, ‘*’) #multiply the color by -1 (which in this space inverts the contrast) thisStim.setColor([10,0,0], ‘dkl’, ‘+’)#raise the elevation from the isoluminant plane by 10 deg
This stimulus class defines a field of elements whose behaviour can be independently controlled. Suitable for creating ‘global form’ stimuli or more detailed random dot stimuli. This stimulus can draw thousands of elements without dropping a frame, but in order to achieve this performance, uses several OpenGL extensions only available on modern graphics cards (supporting OpenGL2.0). See the ElementArray demo.
| Parameters: |
|
|---|
Set the contrast for each element. Should either be:
- a single value
- an Nx1 array/list
Set the phase for each element. Should either be:
- a single value
- an Nx1 array/list
- an Nx2 array/list (for separate X and Y phase)
Set the rgb for each element. Should either be:
- a single value
- an Nx1 array/list
- an Nx3 array/list
Set the spatial frequency for each element. Should either be:
- a single value
- an Nx1 array/list
- an Nx2 array/list (spatial frequency of the element in X and Y).
If the units for the stimulus are ‘pix’ or ‘norm’ then the units of sf are cycles per stimulus width. For units of ‘deg’ or ‘cm’ the units are c/cm or c/deg respectively.
Set the size for each element. Should either be:
- a single value
- an Nx1 array/list
- an Nx2 array/list
Set the xy values of the element centres (relative to the centre of the field). Values should be:
- None
- an array/list of Nx2 coordinates.
If value is None then the xy positions will be generated automatically, based on the fieldSize and fieldPos. In this case opacity will also be overridden by this function (it is used to make elements outside the field invisible.
This stimulus class defines a field of dots with an update rule that determines how they change on every call to the .draw() method.
This standard class can be used to generate a wide variety of dot motion types. For a review of possible types and their pros and cons see Scase, Braddick & Raymond (1996). All six possible motions they describe can be generated with appropriate choices of the signalDots (which determines whether signal dots are the ‘same’ or ‘different’ from frame to frame), noiseDots (which determines the locations of the noise dots on each frame) and the dotLife (which determines for how many frames the dot will continue before being regenerated).
‘Movshon’-type noise uses a random position, rather than random direction, for the noise dots and the signal dots are distinct (noiseDots=’different’). This has the disadvantage that the noise dots not only have a random direction but also a random speed (so differ in two ways from the signal dots). The default option for DotStim is that the dots follow a random walk, with the dot and noise elements being randomised each frame. This provides greater certainty that individual dots cannot be used to determine the motion direction.
When dots go out of bounds or reach the end of their life they are given a new random position. As a result, to prevent inhomogeneities arising in the dots distribution across the field, a limitted lifetime dot is strongly recommended.
If further customisation is required, then the DotStim should be subclassed and its _update_dotsXY and _newDotsXY methods overridden.
| Parameters: |
|
|---|
Set the color of the stimulus. See Color spaces for further information about the various ways to specify colors and their various implications.
| Parameters: |
|---|
Can be specified in one of many ways. If a string is given then it is interpreted as the name of the color. Any of the standard html/X11 color names <http://www.w3schools.com/html/html_colornames.asp> can be used. e.g.:
myStim.setColor('white')
myStim.setColor('RoyalBlue')#(the case is actually ignored)
A hex value can be provided, also formatted as with web colors. This can be provided as a string that begins with # (not using python’s usual 0x000000 format):
myStim.setColor('#DDA0DD')#DDA0DD is hexadecimal for plum
You can also provide a triplet of values, which refer to the coordinates in one of the Color spaces. If no color space is specified then the color space most recently used for this stimulus is used again.
myStim.setColor([1.0,-1.0,-1.0], ‘rgb’)#a red color in rgb space myStim.setColor([0.0,45.0,1.0], ‘dkl’) #DKL space with elev=0, azimuth=45 myStim.setColor([0,0,255], ‘rgb255’) #a blue stimulus using rgb255 space
Lastly, a single number can be provided, x, which is equivalent to providing [x,x,x].
myStim.setColor(255, ‘rgb255’) #all guns o max
colorSpace : string or None
defining which of the Color spaces to use. For strings and hex values this is not needed. If None the default colorSpace for the stimulus is used (defined during initialisation).
operation : one of ‘+’,’-‘,’*’,’/’, or ‘’ for no operation (simply replace value)
for colors specified as a triplet of values (or single intensity value) the new value will perform this operation on the previous color
thisStim.setColor([1,1,1],’rgb255’,’+’)#increment all guns by 1 value thisStim.setColor(-1, ‘rgb’, ‘*’) #multiply the color by -1 (which in this space inverts the contrast) thisStim.setColor([10,0,0], ‘dkl’, ‘+’)#raise the elevation from the isoluminant plane by 10 deg
A class for getting numeric subjective ratings, e.g., on a 1 to 7 scale.
Returns a rating-scale object, with display parameters defined at init().
The .draw() method displays the scale only (not the item to be rated), handles the subject’s response, and updates the display. When the subject responds, .noResponse goes False (i.e., there is a response). You can then call .getRating() to obtain the rating, or getRT() to get the decision time. The experimenter has to handle the item to be rated, and ensure its in the same visual window as the RatingScale. A RatingScale instance has no idea what else is on the screen.
The default settings should be good much of the time, but considerable customization is possible using the options. Auto-rescaling happens if the low-anchor is 0 and high-low is a multiple of 10.
Example:
myRatingScale = visual.RatingScale(myWin)
while myRatingScale.noResponse:
myItem.draw() # RatingScale knows nothing about the item(s) to be rated: text, image, movie, ...
myRatingScale.draw()
myWin.flip()
rating = myRatingScale.getRating()
decisionTime = myRatingScale.getRT()
See ‘ratingScale.py’ for a demo.
| Author: |
|
|---|---|
| Parameters: |
|