PDA

View Full Version : How to set up trials?


bobbin_93
08-20-2014, 08:53 AM
Hi,

I'm wondering if anyone can help me. I've designed an experiment but unfortunately since I'm not really familiar with vizard or coding I've coded for it very long-hand.

I've been told that before I can get the experiment up and running I need to re-organise my code to get it running in a loop of trials, however, I have absolutely no idea how to do this!!!!


I've copied the code below. If anyone could give me any pointers that would be fantastic :)

p.s. I've not copied the full code as it goes on forever but essentially there are 10 repeats of the same thing in the test phase!


import viz
import viztask
import vizact
import vizinfo
import vizmat
import vizshape

viz.setMultiSample
viz.fov(60)
viz.go()
viz.window.setSize(-1); #set to full screen


#Add score labels for trial 1
success = viz.addText('You ate the insect!', viz.SCREEN)
success_points = viz.addText('+5 points', viz.SCREEN)
fail = viz.addText('You got stung!', viz.SCREEN)
fail_points = viz.addText('-5 points', viz.SCREEN)
info_one = vizinfo.add('Press the space bar to try again')
info_one.translate(0.70,0.3)

#Make score labels invisible
success.visible(viz.OFF)
success_points.visible(viz.OFF)
fail.visible(viz.OFF)
fail_points.visible(viz.OFF)
info_one.visible(viz.OFF)

#Add score labels for trial 2
successTwo = viz.addText('You ate the insect!', viz.SCREEN)
success_pointsTwo = viz.addText('+5 points', viz.SCREEN)
failTwo = viz.addText('You got stung!', viz.SCREEN)
fail_pointsTwo = viz.addText('-5 points', viz.SCREEN)
info_two = vizinfo.add('Press the space bar to try again')
info_two.translate(0.70,0.3)

#Make score labels invisible
successTwo.visible(viz.OFF)
success_pointsTwo.visible(viz.OFF)
failTwo.visible(viz.OFF)
fail_pointsTwo.visible(viz.OFF)
info_two.visible(viz.OFF)

def participantInfo():

#add the vizinfo box
infoBox = vizinfo.add('')
infoBox.scale(2,2)
infoBox.translate(0.85,0.8)
infoBox.title('Participant Info')

#Add the GUI elements to the box
participantNumber = infoBox.add(viz.TEXTBOX,'Participant Number')
experimentNumber = infoBox.add(viz.TEXTBOX,'Experiment Number')
runButton = infoBox.add(viz.BUTTON,'Run')

#Wait for run button to be pressed before moving on
yield viztask.waitButtonUp(runButton)

#get data from text boxes and then remove the info box
participant = participantNumber.get()
number = experimentNumber.get()

infoBox.remove()

#open a text file
experiment_data = open('experiment_data.txt','a')
#write participant data to file
data = "Experiment Number: %s\tParticipant: %s\n\n" % (number,participant)
experiment_data.write(data)

def learnPhase():

#Add grass background
quadTwo = viz.addTexQuad(pos=[0, 1.8, 1.7], size=2.75)
textureTwo = viz.addTexture('Grass.png')
quadTwo.texture(textureTwo)

#Add insect example
quadOne = viz.addTexQuad(pos=[0,1.80,1.6])
texture = viz.addTexture('model_one.png')
quadOne.texture(texture)

#Add instructions of what to do
instruction = viz.addText('Eat insects to score points!',viz.SCREEN)
instruction.alignment(viz.ALIGN_CENTER_TOP)
instruction.setPosition(0.5,0.9)

info = vizinfo.add('Press the space bar to begin')
info.translate(0.65,0.2)

#Start test phase with space bar
yield viztask.waitKeyDown(' ')

info.remove()
quadOne.remove()
instruction.remove()

def testPhaseOne():
#Wait 3 seconds before loading the next image
yield viztask.waitTime(3)

#Add a critical question
critical_question = viz.addText('Would you eat this insect?',viz.SCREEN)

yes = viz.addText('Eat!',viz.SCREEN)
no = viz.addText('Do not eat!',viz.SCREEN)

critical_question.alignment(viz.ALIGN_CENTER_TOP)
critical_question.setPosition(0.5,0.9)

yes.alignment(viz.ALIGN_LEFT_TOP)
yes.setPosition(.15,.30)

no.alignment(viz.ALIGN_LEFT_TOP)
no.setPosition(.60,.30)

yes_button = viz.addButton()
yes_button.setPosition(.13,.26)

no_button = viz.addButton()
no_button.setPosition(.57,.26)

#Add model 1
quadOne = viz.addTexQuad(pos=[0.1,1.80,1.5])
texture = viz.addTexture('model.png')
quadOne.texture(texture)

#Mark the time the programme started
start_time = viz.tick()

question_data = open('experiment_data.txt','a')

def onbutton(obj,state):
#Use our starting time variable to find out how much
#time has elapsed.
elapsed_time = viz.tick() - start_time

#Create string lines to put in the data file, depending on which
#button was pushed.
if obj == yes_button:
data = 'Participant' + ' ate the insect.\t'
success.setPosition(0.5,0.7)
success_points.alignment(viz.ALIGN_CENTER_CENTER)
fail_points.alignment(viz.ALIGN_CENTER_CENTER)
fail_points.setPosition(0.5,0.45)
fail.visible(viz.ON)
fail_points.visible(viz.ON)
info_one.visible(viz.ON)

if obj == no_button:
data = 'Participant' + ' did not eat the insect.\t'
fail.alignment(viz.ALIGN_CENTER_TOP)
fail.setPosition(0.5,0.7)
success_points.alignment(viz.ALIGN_CENTER_CENTER)
success_points.setPosition(0.5,0.45)
fail.visible(viz.ON)
success_points.visible(viz.ON)
info_one.visible(viz.ON)

#add elapsed time to data
data = data + 'Elapsed time was: ' + str(round(elapsed_time,2)) + ' seconds\n'

#Write the data to our file.
question_data.write(data)

#Flush the internal buffer.
question_data.flush()

#Remove the illustrations
quadOne.remove()
critical_question.remove()
yes_button.remove()
no_button.remove()
yes.remove()
no.remove()

viz.callback(viz.BUTTON_EVENT,onbutton)

def testPhaseTwo():
#Start test phase with space bar
yield viztask.waitKeyDown(' ')

#Remove score from previous trial
success.remove()
success_points.remove()
fail.remove()
fail_points.remove()
info_one.remove()

yield viztask.waitTime( 3 )

#Add a critical question
critical_question = viz.addText('Would you eat this insect?',viz.SCREEN)

yes = viz.addText('Eat!',viz.SCREEN)
no = viz.addText('Do not eat!',viz.SCREEN)

critical_question.alignment(viz.ALIGN_CENTER_TOP)
critical_question.setPosition(0.5,0.9)

yes.alignment(viz.ALIGN_LEFT_TOP)
yes.setPosition(.15,.30)

no.alignment(viz.ALIGN_LEFT_TOP)
no.setPosition(.60,.30)

yes_button = viz.addButton()
yes_button.setPosition(.13,.26)

no_button = viz.addButton()
no_button.setPosition(.57,.26)

#Add model 2
quadOne = viz.addTexQuad(pos=[0.10,1.80,1.5])
texture = viz.addTexture('model.png')
quadOne.texture(texture)

#Mark the time the programme started
start_time = viz.tick()

question_data = open('experiment_data.txt','a')

def onbutton(obj,state):
#Use our starting time variable to find out how much
#time has elapsed.
elapsed_time = viz.tick() - start_time

#Create string lines to put in the data file, depending on which
#button was pushed.
if obj == yes_button:
data = 'Participant' + ' ate the insect.\t'
failTwo.alignment(viz.ALIGN_CENTER_TOP)
failTwo.setPosition(0.5,0.7)
fail_pointsTwo.alignment(viz.ALIGN_CENTER_CENTER)
fail_pointsTwo.setPosition(0.5,0.45)
failTwo.visible(viz.ON)
fail_pointsTwo.visible(viz.ON)
info_two.visible(viz.ON)

if obj == no_button:
data = 'Participant' + ' did not eat the insect.\t'
successTwo.alignment(viz.ALIGN_CENTER_TOP)
successTwo.setPosition(0.5,0.7)
success_pointsTwo.alignment(viz.ALIGN_CENTER_CENTE R)
success_pointsTwo.setPosition(0.5,0.45)
successTwo.visible(viz.ON)
success_pointsTwo.visible(viz.ON)
info_two.visible(viz.ON)

#add elapsed time to data
data = data + 'Elapsed time was: ' + str(round(elapsed_time,2)) + ' seconds\n'

#Write the data to our file.
question_data.write(data)

#Flush the internal buffer.
question_data.flush()

#Remove the illustrations
quadOne.remove()
critical_question.remove()
yes_button.remove()
no_button.remove()
yes.remove()
no.remove()

viz.callback(viz.BUTTON_EVENT,onbutton)

Jeff
08-20-2014, 10:41 AM
You can control program flow using task functions in combination with loops. Take a look at the designing an experimental study (http://docs.worldviz.com/vizard/#tutorial__experiment_createframework.htm) tutorial.

bobbin_93
08-21-2014, 01:47 AM
Hi, I have looked at that tutorial but can't make much sense out of how it would apply.

Basically I have 15 different pictures of insect which will be displayed one by one on the screen in a random order and the participant must make a button press ('y' for yes and 'n' for no) to decide whether or not they think the insect would sting them or not. When they have made the button press the insect is removed, the result of whether they are correct or not will be displayed and then the next insect will be displayed. This process will then be repeated 3 times!!

If you have any indication of how I might go about setting this up I would be extremely grateful!

Jeff
08-21-2014, 12:14 PM
The following is a short example script that loops through 3 trials in a task function. Each trial loops for the number of images in the images list. For each image loop the program waits for either the 'y' or 'n' key to be pressed:

import viz
import viztask
import random

viz.go()

#Store images in list
images = []

def experiment():

#Repeat trial 3 times
for i in range(3):

#Use random.shuffle to put images in random order
random.shuffle(images)

#Cycle through images
for image in images:

#display image

#wait for 'y' or 'n' key to be pressed
data = yield viztask.waitKeyDown(('y','n'))
print data.key

#remove image and let user know if response is correct

viztask.schedule( experiment() )

bobbin_93
08-22-2014, 08:55 AM
Hi,

That's absolutely brilliant, thank you so much for your help it's really helped me to put the experiment together!

I have one more question however.

Within my 15 insects there are 5 'models' and 10 mimics'. Models sting, whereas mimics don't and therefore I need to display different answers for each - which I am having trouble with.

Here's the code that I've put. In the images the models are labelled 1 to 5 and the mimics are labelled 6 to fifteen. I've also tried seperating them into 2 seperate lists 'stinging' and 'non-stinging' however, the problem with this is I then can't get them to shuffle randomly between the two. Any ideas?




viz.go()

#Store images in list
images = [one,two,three,four,five,six,seven,eight,nine,ten,e leven,twelve,thirteen,fourteen,fifteen]


def testPhase():

#Repeat trial 3 times
for i in range (3):
#Make background visible
background.visible(viz.ON)
#Add focus point
focus.visible(viz.ON)
#Use random.shuffle to put images in a random order
random.shuffle(images)

#Cycle through images
for image in images:
#Wait 3 seconds for participant to look at focus point
yield viztask.waitTime(3)
#Remove focus point
focus.visible(viz.OFF)
#display image
image.visible(viz.ON)
#Mark the time the programme started
start_time = viz.tick()
question_data = open('experiment_data.txt','a')

#Wait for 'y' or 'n' key to be pressed
d = yield viztask.waitKeyDown(['y','n'])
elapsed_time = viz.tick() - start_time

#remove image and let user know if response is correct
#write data to file
if image == one or two or three or four or five:
if d.key == 'y':
image.visible(viz.OFF)
data = 'Participant' + ' Thinks the insect would sting.\t'
correct.visible(viz.ON)
correctPoints.visible(viz.ON)
info.visible(viz.ON)
if d.key == 'n':
image.visible(viz.OFF)
data = 'Participant' + ' Does not think the insect would sting.\t'
wrong.visible(viz.ON)
wrongPoints.visible(viz.ON)
info.visible(viz.ON)

if image == six or seven or eight or nine or ten or eleven or twelve or thirteen or fourteen or fifteen:
if data == 'n':
image.visible(viz.OFF)
data = 'Participant' + ' Does not think the insect would sting.\t'
correct.visible(viz.ON)
correctPoints.visible(viz.ON)
info.visible(viz.ON)
if data == 'y':
image.visible(viz.OFF)
data = 'Participant' + ' Thinks the insect would sting.\t'
wrong.visible(viz.ON)
wrongPoints.visible(viz.ON)
info.visible(viz.ON)
#add elapsed time to data
data = str(image) + data + ' Elapsed time was: ' + str(round(elapsed_time,2)) + ' seconds\n'
#Write the data to our file.
question_data.write(data)

#Press space bar to start the game
yield viztask.waitKeyDown(' ')
#Remove score
correct.visible(viz.OFF)
correctPoints.visible(viz.OFF)
wrong.visible(viz.OFF)
wrongPoints.visible(viz.OFF)
info.visible(viz.OFF)
focus.visible(viz.ON)

viz.quit()