[visionegg] mouse and time logging o.k. please see through the code

Hi Andrew and folks

I expanded "our" code so that after the run I get a txt-file with the
time, the stimulus has been presented, the stimulus-name, the
reaction-time and the mousebutton, which has been pressed

please see through it, whether this is o.k. (it works, but I think it is
maybee spoiled time to check some of the if-then clauses every frame.


thanks for help!

next thing: please help me to define for how long a stimulus has to be
shown and for how long there should be a fixation cross after each
stimulus

and: please help me, to complement the code, so that each stimulus is
only shown after receiving a trigger at the LPT

christoph
------------------------------------------------


#!/usr/bin/env python
"""Load a picture from a file."""

############################
#  Import various modules  #
############################

from random import *
import os
from VisionEgg import *
from VisionEgg.Core import *
from VisionEgg.Textures import *
from math import *
import pygame

path =
"/home/christoph/work/programming/my_ve_projects/load_picture/data/"
#the path the images are in

photo = []
photoX = []

# present them in random order
for filename in os.listdir(path):
    photo.append(filename)
length = len(photo)
for i in range(0, length):
    ran = randint(0, len(photo)-1)
    file = photo[ran]
    photoX.append(file)
    photo.pop(ran)

os.chdir(path)

screen = get_default_screen()

duration_per_image = 1 # seconds
num_images = len(photoX)



# Create list of stimuli
preloaded_stimulus_list = [] # empty at first
for i in range(num_images):
    # Read the texture file
    texture = TextureFromFile(photoX[i])

    x = screen.size[0]/2 - texture.orig.size[0]/2
    y = screen.size[1]/2 - texture.orig.size[1]/2

    # Load the texture to OpenGL, prepare for display
    stimulus = TextureStimulus(texture = texture,
                               size = texture.orig.size,
                               lowerleft=(x,y))
  
    # Add to list of stimuli
    preloaded_stimulus_list.append( stimulus )


log_list = []
log = []
last_image = -1
first_stim_flag = 1

def image_selector( t ):
    global last_image, start_time, timestamp, last_timestamp,
first_stim_flag, log
    i = int(t/duration_per_image)
    if first_stim_flag == 1:
        start_time = VisionEgg.timing_func()
        first_stim_flag = 0
    if i <> last_image:
        if first_stim_flag == 0:
            log_list.append(log) #append last log
        timestamp = VisionEgg.timing_func()
        log = (timestamp - start_time, photoX[i], -1, -1) #create next
new log
    last_image = i
    return [ preloaded_stimulus_list[i] ]
    

viewport = Viewport(screen=screen)



#############################################
#  Create event handler callback functions  #
#############################################



def mousebuttondown(event):
    global log, timestamp
    if event.button == 1:
        log = (timestamp - start_time, photoX[last_image],
VisionEgg.timing_func() - timestamp, 'left') #overwrite actual log
    elif event.button == 3:
        log = (timestamp - start_time, photoX[last_image],
VisionEgg.timing_func() - timestamp, 'right') #overwrite actual log

        
handle_event_callbacks = [(pygame.locals.MOUSEBUTTONDOWN,
mousebuttondown)]

p = Presentation(go_duration=(duration_per_image *
num_images,'seconds'),
                 viewports=[viewport],
                 check_events=1,
                 handle_event_callbacks=handle_event_callbacks)

#############################################################
#  Connect the controllers with the variables they control  #
#############################################################

p.add_controller(viewport,'stimuli',
                 FunctionController(during_go_func=image_selector) )

#######################
#  Run the stimulus!  #
#######################

p.go()
log_list.append(log) #append last log

############################################
#  write the button press times to a file  #
############################################

f=open('/home/christoph/work/programming/my_ve_projects/load_picture/exp_log_file.txt',
 'w')
f.write('stim_onset    stim    RT    response' + '\n')
f.write('____________________________________' + '\n')
for x in log_list:
    for y in x:
#        number = round(y * 1000,2)
        f.write(string.ljust(str(y),20))
#        f.write(str(number),10)
    f.write('\n')
f.write('____________________________________' + '\n')
f.close()



======================================
The Vision Egg mailing list
Archives: http://www.freelists.org/archives/visionegg
Website: http://www.visionegg.org/mailinglist.html

Other related posts: