psychopy.org | Reference | Downloads | Github

Adding a new task coding help!

Hi,

I am performing a visual short term memory task where two images are displayed milliseconds apart and the subject has to identify if the two images are the same of different. Right now there are 3 conditions: the first is that the two images can differ in that one bar is a different color, the next where one bar is a different orientation and the last where it can be either. I am trying to ad a new condition where the second image can differ by the number of bars present in the image but cannot figure out the coding. can anyone help me?

Hi savalosavalos82,

Could you please show us the code of the existing conditions?

Best rbr

This is the code I have right now

#visual_estm
#J Rubinstein Oct 8, 2015
#based on.
#E Kowler. Aug 19, 2015

from psychopy import visual, core, gui, data, event, sound
from psychopy.tools.filetools import fromFile, toFile
import time, numpy, random, string, scipy
import xlwt

global mywin
global grating
global fixation
global blankletter
global centraltext
global xecc,yecc
global savetilt

set up the menu for choice of conditions

myDlg = gui.Dlg(title=“Visual Short Term Memory”,size=(1, 1))
myDlg.addField(‘Initials:’,‘xx’)
myDlg.addField(‘SessionNumber:’,1)
myDlg.addField(‘NumberofTrials’,10)
myDlg.addField(‘Time between Display 1 and 2’, 1000)
myDlg.addField(‘Bar length’, 100)
myDlg.addField(‘Bar width’, 15)
myDlg.addField(‘Task (C,O,or E)’)
myDlg.show()
if myDlg.OK:
sessioninfo=myDlg.data
else:
print ‘cancelled’

#window
mywin = visual.Window([800,600], monitor=“testMonitor”, units=“pix”)
#rgb=[-1,-1,-1] makes screen black
random.seed() #initializes by reading the time

#read values from menu and put in variables
idn=sessioninfo[0]
sessnumber=sessioninfo[1]
ntrials=sessioninfo[2]
pause=sessioninfo[3]
barlength=sessioninfo[4]
barwidth=sessioninfo[5]
task=sessioninfo[6]

actual = []
response = []
nbarstostore=[]
taskstore=[]
print task

#filename
filename=’’
filename=‘visstm_’
filename+=task[0]+’
filename+=idn
filename+=’
’+str(sessnumber)
filename+=’_’+data.getDateStr()
print filename
datetime=data.getDateStr()

#initialize things
displaytime = .25
#(seconds)
#possible x positions of bars…4 choices
barxpos = [-320, -160, 160, 320]
#possible y positions of bars…4 choices
barypos = [-240, -120, 120, 240]
barlocations = []

for x in barxpos:
for y in barypos:
barlocations.append([x,y])
colors = [‘Red’, ‘Green’, ‘Blue’, ‘Yellow’, ‘Black’, ‘White’]
orientation = [0, 45, 90, 135]
nbars = [2, 4, 6]

#write excel file with results
def write_file(filename):

#write stuff at beginning
book = xlwt.Workbook(encoding="utf-8")
sheet1 = book.add_sheet("Sheet 1")
sheet1.write(0, 0, "Visual Short Term Memory")
sheet1.write(1, 0, "Subject:%s"%idn)     
sheet1.write(2, 0, "SessionNumber:%d"%sessnumber)
sheet1.write(3, 0, "Task:%s" %task)
sheet1.write(4, 0, "Inter-display time:%d" %pause)
#column labels
sheet1.write(11, 0, "Trial")
sheet1.write(11, 1, "Task")
sheet1.write(11, 2, "N objects")
sheet1.write(11, 3, "Actual")
sheet1.write(11, 4, "Response")

#below is info for each trial

for i in range (0, ntrials): 
    sheet1.write(i+12, 0, i+1)          #trial
    sheet1.write(i+12, 1, taskstore[i])
    sheet1.write(i+12, 2, nbarstostore[i])
    sheet1.write(i+12, 3, actual[i])
    sheet1.write(i+12, 4, response[i])
    #sheet1.write(i+12, 3, d)    #same or different?
    
    
filename+='.xls'
book.save(filename)

def choosebars(nbars_trial): #randomly select bars for display
alreadyloc = []
bars=[0]*nbars_trial #initialize
print nbars_trial
for b in range(nbars_trial):

print b

print bars

    barcolor = random.choice(colors)
    barori = random.choice(orientation)
    barloc = random.choice(barlocations)
    while barloc in alreadyloc:
        #check if there's already a bar at this location
        barloc = random.choice(barlocations)
    alreadyloc.append(barloc)
#    print barlocations
#    print barloc
    bars[b] = visual.Rect(mywin, width = barwidth, height = barlength)
    bars[b].pos = barloc
    bars[b].fillColor=barcolor
    bars[b].lineColor = barcolor
    bars[b].ori=barori
z = [b.pos for b in bars]
r = [b.fillColor for b in bars]

print z

print r

return bars


    #core.wait(.5)  #uncomment to slow down for debugging

#show the display with the bars
def showdisplay(display, bars, diff):
thistask=‘s’ #default. will change if display 2 and different
if display == 2:
#is second display
if diff:
#this display will be different
d = random.randint(0, len(bars)-1)
if task.lower() == ‘e’:
#either…randomly choose color or orientation
thistask = random.choice([‘c’,‘o’])
else:
thistask = task.lower()
if thistask == ‘c’:
prevcolor = bars[d].fillColor
while prevcolor == bars[d].fillColor: #won’t be the same color
newcolor = random.choice(colors)
bars[d].fillColor = newcolor
bars[d].lineColor = newcolor
elif thistask == ‘o’:
prevori = bars[d].ori
while prevori == bars[d].ori: #won’t be the same orientation
bars[d].ori = random.choice(orientation)

for b in range(len(bars)):
    bars[b].draw()

mywin.flip()

core.wait(displaytime)
return thistask

#return bars
#displays any text. Use for response

def blankscreen():

mywin.flip(clearBuffer=True)
mywin.flip()

print pause/1000.0

core.wait(pause/1000.0)

def displaysometext(texttodisplay):
prompt=visual.TextStim(win=mywin, text=texttodisplay,pos=[0,0],rgb=1,contrast=1)
prompt.draw()
mywin.flip()
#get response
def getresponse():
k = event.waitKeys()
event.clearEvents()
b=k[0]
if b==‘escape’: #option to get out
mywin.close()
core.quit()
return b

#run all trials
for itrial in range (0,ntrials):
print “trial”, itrial + 1
texttodisplay=‘Trial ’ + repr(itrial+1) + ’ Press key’
msg = visual.TextStim(win=mywin, text=texttodisplay)
msg.draw()
mywin.flip()

k = ['']
k = event.waitKeys()

core.wait(1)
nbarsnow=random.choice(nbars)

barstoshow=choosebars(random.choice(nbars))

barstoshow=choosebars(nbarsnow)
lettertimer=core.Clock()                        #define a clock to time events in th e trial

diff = bool(random.getrandbits(1))
#randomly choose if this display will be different or the same

#display displays
thistask=showdisplay(1, barstoshow, diff)
blankscreen()
thistask=showdisplay(2, barstoshow, diff)

#Trial over. Take responses and give feedback

sdresponse=0
utargetletterresponse='0'

displaysometext("same or different?")

while sdresponse not in ['s', 'd']:
    sdresponse=getresponse()
responsediff = 0
if sdresponse == 'd':
    sdtext = 'different'
else:
    sdtext = 'same'
response.append(sdresponse)
if diff:
    actualtxt = 'different'
    actual.append('d')
else:
    actualtxt = 'same'
    actual.append('s')
feedback='response: ' + sdtext + '   Answer was: ' + actualtxt

if responsediff == diff:

feedback = ‘Correct!’

else:

feedback = ‘Incorrect…’

displaysometext(feedback)
core.wait(2)



#store trial dataFile

nbarstostore.append(nbarsnow)

print ‘nbars’, nbarstostore

taskstore.append(str(thistask))

#letter.append(str(target))
#letter.append(letterstoshow[targetletter])

print letter

letterseq.append(targetletter+1)

print letterseq

letterresp.append(targetletterresponse)

print letterresp

gratingtilt.append(tilt)

print gratingtilt

gratingseq.append(letterwithgrating+1)

print gratingseq

gratinglocation.append(int(gloc))

print gratinglocation

gratingTresponse.append(ugratingtiltresponse)

print gratingTresponse

gratinglocresponse.append(int(gratinglocationresponse))

print gratinglocresponse

if len(event.getKeys())>0: break

event.clearEvents()

write_file(filename)
mywin.close()
core.quit()