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
  Help Understanding Code Variables 1 88 May-02-2019, 05:53 PM
Last Post: micseydel
  Need help understanding simple Array code. Please. stluwa 1 103 Apr-13-2019, 07:16 PM
Last Post: loomski
  Better Understanding of Security and injection attacks SQLite KevinBrown 1 170 Apr-09-2019, 09:45 PM
Last Post: Legomancer
  Trouble Understanding Why This Code Works crocolicious 2 132 Apr-09-2019, 05:24 PM
Last Post: crocolicious
  Help with understanding a python package pyhill00 4 170 Mar-21-2019, 12:42 AM
Last Post: Larz60+
  Understanding if Statements in Python Kathleen 1 120 Mar-05-2019, 07:55 PM
Last Post: Yoriz
  help with understanding a program prompt drasil 5 184 Feb-14-2019, 05:54 PM
Last Post: ichabod801
  Extracting a portion of a text document alarcon032002 8 323 Jan-17-2019, 10:35 PM
Last Post: Larz60+
  Lack of understanding "graphX, = ax.plot" easayed 1 209 Dec-21-2018, 11:08 PM
Last Post: micseydel
  i have problems understanding 2 functions,look for help. Tony 2 252 Dec-05-2018, 12:35 PM
Last Post: Tony

Forum Jump:


Users browsing this thread: 1 Guest(s)