Exercise6-debugging

From Lupyan Lab
Jump to: navigation, search


Here's some broken code. When fixed, it should display wide and narrow rectangles, in random order, and play a bleep sound if you respond 'w' for wide or 'n' for narrow, and buzzing sound otherwise. It should also output the correctness of the response and the RT to standard output (i.e., the python screen). The .wav files it uses are in your Box folders (These can also be downloaded here

import time
import random
from psychopy import visual, core, event,sound
 
 
win = visual.Window([800,600],color="blak", units='pix')
 
rect = visual.Rect(win,fillColor="blue",size=size)
aspect = {'wide':[200,100], 'narrow':[100,200]}
validKeys = {'wide':'w', 'narrow':'n'}
 
bleep = sound.Sound('bleep.wav')
buzz = sound.Sound('buzz.wav')
 
for curIter in range(20)
	win.flip()
	core.wait(.5)
	curAspect  = random.choice(aspect.values())
	rect.setSize(aspect[curAspect])
	rect.draw()
	win.flip()
	timer = core.Timer()
	resp = event.waitKeys(keyList=validKeys.values())
	if resp==validKeys[aspect]:
		print 1, timer.getTime()
		bleep.play()
	else:
		print 0, timer.getTime()
		buzz.play()

Here's the fixed code

Here's some more broken code. This code is in your Box\Exercuse_6 directory (debugThis2.py) This program displays 4 squares (I know... very exciting!) The squares are shown in different positions: top, bottom, left, and right of the screen. Square are initially drawn in red (baseColor). One square will randomly flash blue (highlightColor).

Participant responds using the arrow keys. If the right square turns blue, the correct response is left. If the top square is highlighted, 'bottom' is the correct response. Once the user hits a max number of correct (maxCorrect) responses for a position, that position is no longer highlighted. For example if the top square is highlighted and correctly identified twice, the top square is no longer randomly selected. So, if the bottom square is highlighted maxCorrect times, and the user correctly responds both times, the bottom square will no longer be highlighted. When all square positions have been highlighted and correctly identified maxCorrect number of times, the highlighted square is again chosen from all possible positions.


Bonus: Run yourself for a while (whatever you think is sufficient data) and plot a line-graph showing your RTs (on correct trials only) as a function of the number of possible positions left on that trial (RT on the y-axis, number of possible positions on the x-axis).

import time
import random
from psychopy import visual, core, event,sound
 
win = visual.Window([800,600],color="black", units='pix')
maxCorrect = 2
baseColor = 'red'
highlightColor = 'blue'
flashWait = .2
 
square = visual.Rect(win,fillColor="red",size=size=100)
 
validRespnse = {'up': 'down', 'down': 'up', 'left': 'right', 'right':'left'}  
 
 
position  = {'up':[0,200], 'down':[0,-200],'right':[-300,0], 'left':[300,0]} 
 
bleep = sound.Sound('bleep.wav')
buzz = sound.Sound('bleep.wav')
numCorrectResponse  = {'up':0, 'down':0,'right':0, 'left':0}
 
maxCorrect = 5
def getHighlightList():
	highlightList = []
	for numCorrectKey in numCorrectResponse.keys():
		if numCorrectResponse(numCorrectKey) <= maxCorrect
			#print numCorrectResponse[numCorrectKey] 
		highlightList.append(numCorrectKey)
	#print numCorrectResponse
	#print highlightList
	if highlightList = []:
	return numCorrectResponse.keys()
	return highlightList
 
def highlightSquare():
	highlight = random.choice(getHighlightList)
	return highlight
 
def drawSquares(hSquare = None):
	#hSquare: the square that will be highlighted
	if hSquare:
		for curPosition in position.keys():
			if curPosition == hSquare:
				square.setColor(highlightColor)
			else
				square.setColor(baseColor)
			square.setPosition(position[curPosition])
			square.draw()
	else:
		for curPosition in position.keys():
			square.setPosition(position{curPosition})
			square.draw()
			square.setColor(baseColor)
 
 
for curIter in range(20):
	highlight = highlightSquare
	drawSquares()
	win.flip()
	core.wait(2)
	drawSquares(highlight)
	win.flip()
	core.wait(flashWait)
	drawSquares()
	win.flip()
 
	#curPosition  = random.choice(position.keys())
	#square.setPos(position[curPosition]) 
	#square.draw()
 
	win.flip() 
	timer = core.Clock()
	resp = event.waitKeys(keyList=validResponse.values())
	if validResponse[highlight]==resp[0]:
		print 1, timer.getTime()
		numCorrectResponse.update({highlight:numCorrectResponses[highlight]+1})
		print numCorrectResponse
		bleep.play()
	else:
		print 0, timer.getTime()
		buzz.play()

Here's the fixed code

Personal tools
Namespaces
Variants
Actions
Navigation
Download and Install
Notes for each class - will be updated ~week before each class.
Programming Exercises
Projects
Quick reference
Toolbox