Vizard 8 » Tutorials & Examples » Creating data files » Tutorial: Creating data files
8.0

Tutorial: Creating data files

If you are using Vizard for research you will most probably want to record data while you are running a world. This tutorial will go over a simple data collection script.

Start the script

Let's imagine that we are conducting groundbreaking research on the visibility of pigeons. For our experiment, we will let the participant navigate through a world until he or she can identify our targeted bird. To get started, let's create a world with the pigeon and the necessary text:

 

If you have trouble getting the code in this tutorial to work, you can find the complete example script data.py in the \tutorials\data directory.

Creating data files

First add the following lines including a prompt for the participant's identification number.

import viz
import vizinput

viz.setMultiSample(4)
viz.fov(60)
viz.go()

#Prompt for the participant's identification number.
subject = vizinput.input('What is the participant number?')

Set up the scene.

viz.MainView.move([0,0,2])

#Add the environment
piazza = viz.addChild('piazza.osgb')

#Add the pigeon.
pigeon = viz.addAvatar('pigeon.cfg',pos=[-0.9, 2.88, 11.3],euler=[100,0,0])
pigeon.state(1)

Write the critical question on the screen, along with the possible answers and their corresponding check boxes.

critical_question = viz.addText('Do you see a pigeon?',viz.SCREEN)

yes = viz.addText('Yes!',viz.SCREEN)
no = viz.addText('No!',viz.SCREEN)

critical_question.alignment(viz.ALIGN_CENTER_TOP)
critical_question.setPosition(.5,.9)

yes.alignment(viz.ALIGN_LEFT_TOP)
yes.setPosition(.25,.75)

no.alignment(viz.ALIGN_LEFT_TOP)
no.setPosition(.75,.75)

yes_button = viz.addButton()
yes_button.setPosition(.22,.71)

no_button = viz.addButton()
no_button.setPosition(.72,.71)

Run the script.

Now we'll add a variable that records the time when the program was started. We'll also open a data file and create a function to detect when one of the buttons has been pushed.

 

Mark the time that the program was started.

start_time = viz.tick()

Open a file to collect the data on this subject. The 'a' here means that we will be appending our new data to the existing data in this file.

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

Finally, get the data from the button press.

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 = 'Subject ' + str(subject) + ' saw a pigeon.\t'
    if obj == no_button:
        data = 'Subject ' + str(subject) + ' did not see a pigeon.\t'
   
    #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()
    #Close the world.
    viz.quit()

viz.callback(viz.BUTTON_EVENT,onbutton)

Now run your script. Notice that when you click on one of the buttons, the world automatically closes. That's because we put a viz.quit() command in the button function.

 

Because we know the orientation and position of the participant at any given moment, we can record that data throughout the session. So, add the following line to open another data file and to fill it with tracking data.  Notice that each individual subject will have his or her own file.

tracking_data = open('tracking_'+str(subject)+'.txt', 'a')

Create the head tracking timer and function.

#Get the tracking data.
def getData():
    orientation = viz.MainView.getEuler()
    position = viz.MainView.getPosition()
    #Make a string out of the data.
    data = str(subject) + '\t' + str(orientation) + '\t' + str(position) + '\n'
    #Write it to the tracking file.
    tracking_data.write(data)

vizact.ontimer(1, getData)

The timer function will be called once every second to collect tracking data as long as the world is open.

 

Run the script again with a new subject number to gather some tracking data.

 

Now go the script's directory and open your data files. The file "pigeon_data.txt" should contain a response to the critical question as well as the elapsed time for each subject number you used. You should also have a "tracking_[subject number].txt" file for each subject containing that subject's tracking data.