Bottom Page

Thread Rating:
  • 2 Vote(s) - 2.5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help Understanding Portion of Code
#1
Hi everyone,

I'm very new to python and have been given some code to work with for a study I'll be conducting. There's just one section that I'm not quite understanding based on what I know about python so far. I've posted this section below.

What I'm not understanding is what's going on in the line below the note "initialize lists for hard coding...". Why would these lists be so long and written in this pattern if they're getting randomly shuffled?
The reason I want to understand this is that I want to figure out how to add a third soa time.

Thanks in advance for your help.

#define stimulus duration times 
fixscreen_time=.8
cue_time=.15
soa_short=.05
soa_long=.15

#initialise lists for hard coding side, soa and validity proportions 
#cueside - 0=left; 1=right; soa - 0=early; 1=late; validity - 0=valid; 1=invalid
cueside=[0,0,1,1,1,0,1,1,0,0,0,1,0,1,1,0,0,1,1,0,0,0,1,1,
0,0,1,1,1,0,1,1,0,0,0,1,0,1,1,0,0,1,1,0,0,0,1,1,
0,0,1,1,1,0,1,1,0,0,0,1,0,1,1,0,0,1,1,0,0,0,1,1,
0,0,1,1,1,0,1,1,0,0,0,1,0,1,1,0,0,1,1,0,0,0,1,1,
0,0,1,1,1,0,1,1,0,0,0,1,0,1,1,0,0,1,1,0,0,0,1,1,
0,0,1,1,1,0,1,1,0,0,0,1,0,1,1,0,0,1,1,0,0,0,1,1,
0,0,1,1,1,0,1,1,0,0,0,1,0,1,1,0,0,1,1,0,0,0,1,1]
soa=[1,0,0,1,1,0,1,0,1,1,0,1,0,0,1,0,1,0,0,1,1,0,1,0,
1,0,0,1,1,0,1,0,1,1,0,1,0,0,1,0,1,0,0,1,1,0,1,0,
1,0,0,1,1,0,1,0,1,1,0,1,0,0,1,0,1,0,0,1,1,0,1,0,
1,0,0,1,1,0,1,0,1,1,0,1,0,0,1,0,1,0,0,1,1,0,1,0,
1,0,0,1,1,0,1,0,1,1,0,1,0,0,1,0,1,0,0,1,1,0,1,0,
1,0,0,1,1,0,1,0,1,1,0,1,0,0,1,0,1,0,0,1,1,0,1,0,
1,0,0,1,1,0,1,0,1,1,0,1,0,0,1,0,1,0,0,1,1,0,1,0]
validity=[0,1,1,0,1,1,1,0,0,1,0,0,1,0,0,0,1,1,0,1,0,0,1,1,
0,1,1,0,1,1,1,0,0,1,0,0,1,0,0,0,1,1,0,1,0,0,1,1,
0,1,1,0,1,1,1,0,0,1,0,0,1,0,0,0,1,1,0,1,0,0,1,1,
0,1,1,0,1,1,1,0,0,1,0,0,1,0,0,0,1,1,0,1,0,0,1,1,
0,1,1,0,1,1,1,0,0,1,0,0,1,0,0,0,1,1,0,1,0,0,1,1,
0,1,1,0,1,1,1,0,0,1,0,0,1,0,0,0,1,1,0,1,0,0,1,1,
0,1,1,0,1,1,1,0,0,1,0,0,1,0,0,0,1,1,0,1,0,0,1,1]

#shuffle order of the trial factors
random.shuffle(cueside)
random.shuffle(soa)
random.shuffle(validity)

Quote
#2
Quote:Why would these lists be so long and written in this pattern if they're getting randomly shuffled (later in the code)?
Good point, can you provide more information about the code, where can it be found on the web, or a complete script?
Quote
#3
(Jan-12-2019, 07:52 PM)caroline_d_124 Wrote: Why would these lists be so long and written in this pattern if they're getting randomly shuffled?
you should ask whoever wrote the code. there is no apparent reason. If I have yo guess - values are based on some other info, not present here and they thought it easier to just put them as they come.
Quote
#4
Here is the full code for more context.

from psychopy import visual, gui, core, event, data
import random as random
from itertools import product
import os
import csv


#set directory path
path=os.getcwd()

#set number of trials
numtrials=3

#make a GUI to collect participant number
COVATDlg=gui.Dlg(title="Covert Orienting Task")
COVATDlg.addText('Participant Number', color='black')
COVATDlg.addField('Participant ID:')
COVATDlg.addField('Age:')
COVATDlg.addField('Gender:',choices=['male','female','other'])
COVATDlg.addField('Handedness:',choices=['left','right','both'])
COVATDlg.show()
if gui.OK:
    startInfo=COVATDlg.data
    print(startInfo)
else: print(cancelled)

#set window
screen_size=(1366,768)
win=visual.Window(size=screen_size,color=(-1,-1,-1),units='pix',fullscr=True)

#initialise data file - refer to 'startInfo' above for participant number
filename = "boredom_{0}.csv".format(startInfo[0])
dataHeader=["Participant","Age","Gender","Handedness","Trial","Cue Side", "SOA","Target Type","RT", "Boredom"]
with open(filename, "w+") as dataFile:
    dataFile.write(",".join(dataHeader) + "\n")
    #Create function to write data 
def recordData(pt,age,gender,hand,trialnum,cue,soa,val,rt):
    dataFile.write(",".join([str(pt),str(age),str(gender),str(hand),str(trialnum),str(cue),str(soa),str(val),str(rt)])+"\n")
    dataFile.flush()


#define stimuli
#if using fullscreen the position values need to be changed and line width can be smaller
fixstim='+'
fixation=visual.TextStim(win,pos=(0,0),height=60,color=(1,1,1),text=fixstim)
rightlandmark=visual.Circle(win,units='pix',pos=(250,0),radius=30,edges=50,lineColor='LawnGreen',lineWidth=4)
leftlandmark=visual.Circle(win,units='pix',pos=(-250,0),radius=30,edges=50,lineColor='LawnGreen',lineWidth=4)
righttarg=visual.Circle(win,units='pix',pos=(250,0),radius=18,edges=50,lineColor='red',fillColor='red',lineWidth=4)
lefttarg=visual.Circle(win,units='pix',pos=(-250,0),radius=18,edges=50,lineColor='red',fillColor='red',lineWidth=4)
rightcue=visual.Circle(win,units='pix',pos=(250,0),radius=30,edges=50,lineColor='LawnGreen',lineWidth=12)
leftcue=visual.Circle(win,units='pix',pos=(-250,0),radius=30,edges=50,lineColor='LawnGreen',lineWidth=12)

#define stimulus duration times 
fixscreen_time=.8
cue_time=.15
soa_short=.05
soa_long=.15

#initialise lists for hard coding side, soa and validity proportions 
#cueside - 0=left; 1=right; soa - 0=early; 1=late; validity - 0=valid; 1=invalid

cueside=[0,0,1,1,1,0,1,1,0,0,0,1,0,1,1,0,0,1,1,0,0,0,1,1,
0,0,1,1,1,0,1,1,0,0,0,1,0,1,1,0,0,1,1,0,0,0,1,1,
0,0,1,1,1,0,1,1,0,0,0,1,0,1,1,0,0,1,1,0,0,0,1,1,
0,0,1,1,1,0,1,1,0,0,0,1,0,1,1,0,0,1,1,0,0,0,1,1,
0,0,1,1,1,0,1,1,0,0,0,1,0,1,1,0,0,1,1,0,0,0,1,1,
0,0,1,1,1,0,1,1,0,0,0,1,0,1,1,0,0,1,1,0,0,0,1,1,
0,0,1,1,1,0,1,1,0,0,0,1,0,1,1,0,0,1,1,0,0,0,1,1]
soa=[1,0,0,1,1,0,1,0,1,1,0,1,0,0,1,0,1,0,0,1,1,0,1,0,
1,0,0,1,1,0,1,0,1,1,0,1,0,0,1,0,1,0,0,1,1,0,1,0,
1,0,0,1,1,0,1,0,1,1,0,1,0,0,1,0,1,0,0,1,1,0,1,0,
1,0,0,1,1,0,1,0,1,1,0,1,0,0,1,0,1,0,0,1,1,0,1,0,
1,0,0,1,1,0,1,0,1,1,0,1,0,0,1,0,1,0,0,1,1,0,1,0,
1,0,0,1,1,0,1,0,1,1,0,1,0,0,1,0,1,0,0,1,1,0,1,0,
1,0,0,1,1,0,1,0,1,1,0,1,0,0,1,0,1,0,0,1,1,0,1,0]
validity=[0,1,1,0,1,1,1,0,0,1,0,0,1,0,0,0,1,1,0,1,0,0,1,1,
0,1,1,0,1,1,1,0,0,1,0,0,1,0,0,0,1,1,0,1,0,0,1,1,
0,1,1,0,1,1,1,0,0,1,0,0,1,0,0,0,1,1,0,1,0,0,1,1,
0,1,1,0,1,1,1,0,0,1,0,0,1,0,0,0,1,1,0,1,0,0,1,1,
0,1,1,0,1,1,1,0,0,1,0,0,1,0,0,0,1,1,0,1,0,0,1,1,
0,1,1,0,1,1,1,0,0,1,0,0,1,0,0,0,1,1,0,1,0,0,1,1,
0,1,1,0,1,1,1,0,0,1,0,0,1,0,0,0,1,1,0,1,0,0,1,1]

#shuffle order of the trial factors
random.shuffle(cueside)
random.shuffle(soa)
random.shuffle(validity)

#define functions for components of trials
def fixscreen():
    fixation.draw()
    rightlandmark.draw()
    leftlandmark.draw()
    win.flip()
    core.wait(fixscreen_time)
    
def lcue():
    fixation.draw()
    rightlandmark.draw()
    leftcue.draw()
    win.flip()
    core.wait(cue_time)

def rcue():
    fixation.draw()
    leftlandmark.draw()
    rightcue.draw()
    win.flip()
    core.wait(cue_time)

def ltarg():
    fixation.draw()
    leftlandmark.draw()
    rightlandmark.draw()
    lefttarg.draw()
    win.flip()
        
def rtarg():
    fixation.draw()
    leftlandmark.draw()
    rightlandmark.draw()
    righttarg.draw()
    win.flip()
        
def short_soa():
    fixation.draw()
    leftlandmark.draw()
    rightlandmark.draw()
    win.flip()
    core.wait(soa_short)
    
def long_soa():
    fixation.draw()
    leftlandmark.draw()
    rightlandmark.draw()
    win.flip()
    core.wait(soa_long)
    
#instruction screen
instr1=visual.TextStim(win,height=35,color=(1,1,1),pos=(0,300),alignVert='center',alignHoriz='center',wrapWidth=1000,text="You will see a cross at the centre of the screen.")
instr2=visual.TextStim(win,height=35,color=(1,1,1),pos=(0,200),alignVert='center',alignHoriz='center',wrapWidth=1000,text="Try to keep looking at the cross.")
instr3=visual.TextStim(win,height=35,color=(1,1,1),pos=(0,100),alignVert='center',alignHoriz='center',wrapWidth=1000,text="One of the green circles to the left and right will get brighter.")
instr4=visual.TextStim(win,height=35,color=(1,1,1),pos=(0,0),alignVert='center',alignHoriz='center',wrapWidth=1000,text="A red circle will appear inside one of the green circles.")
instr5=visual.TextStim(win,height=35,color=(1,1,1),pos=(0,-100),alignVert='center',alignHoriz='center',wrapWidth=1000,text="Press the space bar when you see the red circle.")
instr6=visual.TextStim(win,height=35,color=(1,1,1),pos=(0,-200),alignVert='center',alignHoriz='center',wrapWidth=1000,text="Press the space bar to continue.")

instr1.draw()
instr2.draw()
instr3.draw()
instr4.draw()
instr5.draw()
instr6.draw()
win.flip()
keypress=event.waitKeys(keyList=["space","q"])
if keypress[0][0]=="q":
    core.quit()

#go through examples with the participant 
eg1=visual.TextStim(
    win, height=35, color=(1,1,1), pos=(0,-200), alignVert='center', 
    alignHoriz='center', wrapWidth=1000, 
    text="This is what the screen will look like."
)
eg1.draw()
fixation.draw()
leftlandmark.draw()
rightlandmark.draw()
win.flip()
event.waitKeys(keyList=['space'])

eg2=visual.TextStim(win,height=35,color=(1,1,1),pos=(0,-200),alignVert='center',alignHoriz='center',wrapWidth=1000,text="This is what a cue and target will look like.")
eg2.draw()
fixation.draw()
leftlandmark.draw()
rightcue.draw()
righttarg.draw()
win.flip()
event.waitKeys(keyList=['space'])

#first rating scale probe
ratingScale = visual.RatingScale(
    win, str('how bored do you feel right now?'),low=0, high=9, markerStart=4,
    acceptKeys='space')
while ratingScale.noResponse:
        ratingScale.draw()
        win.flip()

rating=ratingScale.getRating()
print(rating)

with open(filename, "a", newline = '\n') as dataFile:
    writer = csv.writer(dataFile)
    writer.writerow([str(startInfo[0]),str(startInfo[1]),str(startInfo[2]),str(startInfo[3]),str(''),str(''),str(''),str(''),str(''), rating])

#loop for trial sequence
for i in range (numtrials):
    fixscreen()
    if cueside[i]==0:
        lcue()
    else:
        rcue()
    if soa[i]==0:
        short_soa()
    else:
        long_soa()
    if validity[i]==0:
        ltarg()
    else:
        rtarg()
    RTClock=core.Clock()
    keypress=event.waitKeys(keyList=["space","q"])
    RT=RTClock.getTime()
    if keypress[0][0]=="q":
        core.quit()
    else:
        with open(filename, "a", newline='\n') as dataFile:
            writer=csv.writer(dataFile)
            writer.writerow([str(startInfo[0]),str(startInfo[1]),str(startInfo[2]),str(startInfo[3]),i,cueside[i],soa[i],validity[i],RT])

#second boredom probe
ratingScale = visual.RatingScale(
    win, str('how bored do you feel right now?'),low=0, high=9, markerStart=4,
    acceptKeys='space')
while ratingScale.noResponse:
        ratingScale.draw()
        win.flip()

rating=ratingScale.getRating()
print(rating)

with open(filename, "a", newline = '\n') as dataFile:
    writer = csv.writer(dataFile)
    writer.writerow([str(startInfo[0]),str(startInfo[1]),str(startInfo[2]),str(startInfo[3]),str(''),str(''),str(''),str(''),str(''), rating])

#note, earlier we used the readData method (using the list in the original COVAT code, and this worked just the same)

for j in range (numtrials):
    fixscreen()
    if cueside[j]==0:
        lcue()
    else:
        rcue()
    if soa[j]==0:
        short_soa()
    else:
        long_soa()
    if validity[j]==0:
        ltarg()
    else:
        rtarg()
    RTClock=core.Clock()
    keypress=event.waitKeys(keyList=["space","q"])
    RT=RTClock.getTime()
    if keypress[0][0]=="q":
        core.quit()
    else:
        with open(filename, "a", newline='\n') as dataFile:
            writer=csv.writer(dataFile)
            writer.writerow([str(startInfo[0]),str(startInfo[1]),str(startInfo[2]),str(startInfo[3]),j,cueside[j],soa[j],validity[j],RT])

#third boredom probe
ratingScale = visual.RatingScale(
    win, str('how bored do you feel right now?'),low=0, high=9, markerStart=4,
    acceptKeys='space')
while ratingScale.noResponse:
        ratingScale.draw()
        win.flip()

rating=ratingScale.getRating()
print(rating)

with open(filename, "a", newline = '\n') as dataFile:
    writer = csv.writer(dataFile)
    writer.writerow([str(startInfo[0]),str(startInfo[1]),str(startInfo[2]),str(startInfo[3]),str(''),str(''),str(''),str(''),str(''), rating])

for k in range (numtrials):
    fixscreen()
    if cueside[k]==0:
        lcue()
    else:
        rcue()
    if soa[k]==0:
        short_soa()
    else:
        long_soa()
    if validity[k]==0:
        ltarg()
    else:
        rtarg()
    RTClock=core.Clock()
    keypress=event.waitKeys(keyList=["space","q"])
    RT=RTClock.getTime()
    if keypress[0][0]=="q":
        core.quit()
    else:
        with open(filename, "a", newline='\n') as dataFile:
            writer=csv.writer(dataFile)
            writer.writerow([str(startInfo[0]),str(startInfo[1]),str(startInfo[2]),str(startInfo[3]),k,cueside[k],soa[k],validity[k],RT])

#fourth boredom probe
ratingScale = visual.RatingScale(
    win, str('how bored do you feel right now?'),low=0, high=9, markerStart=4,
    acceptKeys='space')
while ratingScale.noResponse:
        ratingScale.draw()
        win.flip()

rating=ratingScale.getRating()
print(rating)

with open(filename, "a", newline = '\n') as dataFile:
    writer = csv.writer(dataFile)
    writer.writerow([str(startInfo[0]),str(startInfo[1]),str(startInfo[2]),str(startInfo[3]),str(''),str(''),str(''),str(''),str(''), rating])


for l in range (numtrials):
    fixscreen()
    if cueside[l]==0:
        lcue()
    else:
        rcue()
    if soa[l]==0:
        short_soa()
    else:
        long_soa()
    if validity[l]==0:
        ltarg()
    else:
        rtarg()
    RTClock=core.Clock()
    keypress=event.waitKeys(keyList=["space","q"])
    RT=RTClock.getTime()
    if keypress[0][0]=="q":
        core.quit()
    else:
        with open(filename, "a", newline='\n') as dataFile:
            writer=csv.writer(dataFile)
            writer.writerow([str(startInfo[0]),str(startInfo[1]),str(startInfo[2]),str(startInfo[3]),l,cueside[l],soa[l],validity[l],RT])

#fifth boredom probe
ratingScale = visual.RatingScale(
    win, str('how bored do you feel right now?'),low=0, high=9, markerStart=4,
    acceptKeys='space')
while ratingScale.noResponse:
        ratingScale.draw()
        win.flip()

rating=ratingScale.getRating()
print(rating)

with open(filename, "a", newline = '\n') as dataFile:
    writer = csv.writer(dataFile)
    writer.writerow([str(startInfo[0]),str(startInfo[1]),str(startInfo[2]),str(startInfo[3]),str(''),str(''),str(''),str(''),str(''), rating])

for m in range (numtrials):
    fixscreen()
    if cueside[m]==0:
        lcue()
    else:
        rcue()
    if soa[m]==0:
        short_soa()
    else:
        long_soa()
    if validity[m]==0:
        ltarg()
    else:
        rtarg()
    RTClock=core.Clock()
    keypress=event.waitKeys(keyList=["space","q"])
    RT=RTClock.getTime()
    if keypress[0][0]=="q":
        core.quit()
    else:
        with open(filename, "a", newline='\n') as dataFile:
            writer=csv.writer(dataFile)
            writer.writerow([str(startInfo[0]),str(startInfo[1]),str(startInfo[2]),str(startInfo[3]),m,cueside[m],soa[m],validity[m],RT])

#sixth and final boredom probe
ratingScale = visual.RatingScale(
    win, str('how bored do you feel right now?'),low=0, high=9, markerStart=4,
    acceptKeys='space')
while ratingScale.noResponse:
        ratingScale.draw()
        win.flip()

rating=ratingScale.getRating()
print(rating)

with open(filename, "a", newline = '\n') as dataFile:
    writer = csv.writer(dataFile)
    writer.writerow([str(startInfo[0]),str(startInfo[1]),str(startInfo[2]),str(startInfo[3]),str(''),str(''), str(''), str(''),str(''), rating])



#close out data file and end experiment on a key press
dataFile.close()
thankyou=visual.TextStim(win,pos=(0,0),height=35,color=(1,1,1),text='Thank you for participating.')
thankyou.draw()
win.flip()
event.waitKeys()
core.quit()



(Jan-12-2019, 07:54 PM)Larz60+ Wrote:
Quote:Why would these lists be so long and written in this pattern if they're getting randomly shuffled (later in the code)?
Good point, can you provide more information about the code, where can it be found on the web, or a complete script?

I just attached it (accidentally) as a reply to my post here.
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Extracting a portion of a text document alarcon032002 8 139 Jan-17-2019, 10:35 PM
Last Post: Larz60+
  Lack of understanding "graphX, = ax.plot" easayed 1 88 Dec-21-2018, 11:08 PM
Last Post: micseydel
  i have problems understanding 2 functions,look for help. Tony 2 140 Dec-05-2018, 12:35 PM
Last Post: Tony
  Understanding GerbMerge code djsb 1 134 Oct-27-2018, 02:04 PM
Last Post: Larz60+
  Understanding the code Eugine 1 176 Oct-22-2018, 05:56 PM
Last Post: j.crater
  Fundamental understanding-problem regards to while-loops Placebo 7 313 Oct-11-2018, 01:18 PM
Last Post: Placebo
  Need help with understanding for/while loops! WombatHat42 2 204 Sep-26-2018, 11:46 AM
Last Post: ThiefOfTime
  Help in understanding scope of dictionaries and lists passed to recursive functions barles 2 276 Aug-11-2018, 06:45 PM
Last Post: barles
  Understanding Scoping in Python yksingh1097 5 331 Aug-06-2018, 07:42 PM
Last Post: nilamo
  understanding sorted key parameter amirt 2 254 Jul-30-2018, 06:27 PM
Last Post: amirt

Forum Jump:


Users browsing this thread: 1 Guest(s)