From Lupyan_Lab_Wiki
Jump to: navigation, search

<sidebar>Psych711</sidebar> Here is some 'bad' code that shows a blue circle and a blue square. In this assignment you'll work on this code to expand it to a full blown perceptual grouping task.

  • Open perceptualGrouping.py
import time
import random
from psychopy import visual, core, event
win = visual.Window([800,600],color="black", units='pix')
circle = visual.Circle(win,size = 20)
square = visual.Rect(win,size = 40)
locations = [[-15,0], [15,0]]
colors = ['blue', 'blue']



  • For the next 15 minutes, see if you can figure out how to make it display a series of alternating circles and squares that look like the image below (shown on a black background). We'll be doing a lot with this code. Make it as extendable as possible. For example, the spacing and the number of items should be easily changeable. Your code to generate the series of alternating images needs to be *shorter* than the base code above.
Alternating shapes
  1. Now, for each trial, pick a random shape and "flip" it such that you have two squares or two circles in a row. The colors should always alternate. So, you might have red-square, blue-circle, red-circle, blue-square , etc.(a repetition of two circles in a row). Note, you will need to do more than just flip the one shape to make this work (flipping a single shape in an alternating sequence will create a sequence of three repeated items, which is not what you want).
    • hint: random.choice([0,1]) will output 0 or 1. The items in the list supplied as the argument to the random.choice function can be anything, e.g., random.choice(['within','between']) will output the words 'within' or 'between' randomly.
  2. Make it so that 50% of the trials have a repeat, and 50% don't (i.e., the latter just have the normal alternation, as in the picture above)
  3. The subject's task is to detect the repetition. If there's a repetition, they should press the 'up' arrow. If not, the 'down' arrow.
  4. Let's now add feedback. If there's an error, and only if there's an error, display the word 'ERROR' in red after the response.
  5. Let's make the subject report what shape was repeating. Make it so that if they press the 'up' arrow, they then see a prompt "Repeated square or circle?" which should stay up until they respond with the 's' key for square and the 'c' key for circle. If they press the 'down' arrow, just go on to the next trial.
  6. Now, let's actually keep track of what people are responding! For each trial, output the variables listed below to a file called results.txt. You can use
 '\t'.join([string1, string2, string3, stringN]) 

to create a line in which each variable is separated by a tab, and then write that line to the file. Each item in the list passed to join must be a string, so any items that are not (hint: the Reaction Time) must be first converted to a string using str(RT). See here for instructions on writing to a file one line at a time, making sure that there is no data loss if your program crashes. Here's what each line should contain:

  • The number of the trial (1, 2, 3 etc.)
  • The correct response (repeat, noRepeat)
  • Position of the repeated stimulus, e.g., if the 4th and 5th elements are the shame shape, output 4. If there's no repeat, output 'NA'
  • Whether the repeating shape is a circle or a square (i.e., output the words 'circle' or 'square' or NA if this is a noRepeat trial)
  • The subject's responses (note that there are two: the first is whether there's a repeat. The second is what the repeat is)
  • Correctness of the first response, correctness of the second response (1 for correct, 0 for incorrect)
  • The response times for both responses in milliseconds

7. This file will contain their responses and trial parameters, but we don't know who the subject is! Remember when I had you pop=up a box to enter in a person's name? Do the same thing here, but pop-up a box to ask for the subject code. You'll then want to do things with this subject code:

Name the output file with that code, e.g., repetition_101.txt, repetition_102.txt, etc. (there's a very good reason to start with 100 instead of 1. Any ideas?)
Write the subj code as the first entry in each column of your output file


Bonus: instead of having them respond with circle/square, let's make them tell us exactly where the repetition was. After they make a repeat/no-Repeat response, have the shapes change to X's and have the subject indicate with the mouse (ooo!) where the repeat was. Mark it as correct or not. If you want to try a 'bonus-lite' version, change the shapes to letters: a,b,c,d,e,f,g,h,i,j,k,l,m,n,o and have the subject press the letter corresponding to where the repeat was.