Platform for trajectory tracking experiments & data analysis
TrajTracker Paradigms: Sample scripts
Using the events mechanism
This script demonstrates how to make your experiment do things in various times during the trial, without having to hack TrajTracker Paradigms functions.
​
This is achieved using TrajTracker's events mechanism. TrajTracker Paradigms defines the flow of a trial and dispatches several events during each trial (e.g. when the trial starts or ends, when the finger moves, etc.). The event manager lets you easily hook your functions to any of these events.
​
In the sample script, we defined four functions (e.g., on_finger_touched_screen) and plugged each of them into the trial flow by registering them to run when a specific event occurs (search for the calls to event_manager.register_operation()). You can also ask that your functions run in some delay after the event occurred. This is done by simply adding the delay (in seconds) to the event. For example, changing ttrk.events.TRIAL_STARTED to ttrk.events.TRIAL_STARTED+0.5 means that the corresponding function will run only 500 ms after the finger touched the screen.
​
See here a list of the events supported by TrajTracker Paradigms.
import expyriment as xpy
import trajtracker as ttrk
import trajtrackerp as ttrkp
from trajtrackerp import num2pos, common
if not xpy.misc.is_android_running():
xpy.control.defaults.window_mode = True
ttrk.log_to_console = True
ttrk.env.default_log_level = ttrk.log_info
config = num2pos.Config("Num2Pos(D+U)",
max_movement_time=2,
max_numberline_value=100,
data_source=range(101) * 2,
text_target_height=0.5)
#=====================================================================
msg = xpy.stimuli.TextBox(text="", size=(200, 40), text_font="Arial", text_size=15,
text_colour=xpy.misc.constants.C_WHITE)
def on_finger_touched_screen(time_in_trial, time_in_session):
print(">>>>>>> Finger touched screen")
msg.text = "State: Starting"
def on_finger_started_moving(time_in_trial, time_in_session):
print(">>>>>>> Finger started moving")
msg.text = "State: Moving"
def on_finger_stopped_moving(time_in_trial, time_in_session):
print(">>>>>>> Finger stopped moving")
msg.text = "State: Stopped"
def on_trial_ended(time_in_trial, time_in_session):
print(">>>>>>> Trial ended")
msg.text = "State: Ended"
#=====================================================================
#-- Initialize & start the Expyriment
exp = ttrk.initialize()
xpy.control.start(exp)
if not xpy.misc.is_android_running():
exp.mouse.show_cursor()
#-- Get subject info
(subj_id, subj_name) = ttrkp.common.get_subject_name_id()
#-- Initialize the experiment objects
exp_info = num2pos.ExperimentInfo(config, exp, subj_id, subj_name)
num2pos.create_experiment_objects(exp_info)
common.register_to_event_manager(exp_info)
#-- Set position of custom textboxes (this can only be done now, after the experiment was initialized)
msg.position = -exp_info.screen_size[0] / 2 + msg.size[0] / 2 + 10, -exp_info.screen_size[1] / 2 + msg.size[1] / 2 + 10
#-- Add custom objects
exp_info.stimuli.add(msg) # So present() is repeatedly invoked for msg1
#-- Invoke custom operations when certain events occur
exp_info.event_manager.register_operation(event=ttrk.events.TRIAL_STARTED,
operation=on_finger_touched_screen,
recurring=True,
description="Custom on-touch operation")
exp_info.event_manager.register_operation(event=ttrk.events.TRIAL_ENDED,
operation=on_trial_ended,
recurring=True,
description="Custom on-ended operation")
exp_info.event_manager.register_operation(event=common.FINGER_STARTED_MOVING,
operation=on_finger_started_moving,
recurring=True,
description="Custom on-move operation")
exp_info.event_manager.register_operation(event=common.FINGER_STOPPED_MOVING,
operation=on_finger_stopped_moving,
recurring=True,
description="Custom on-stop operation")
exp_info.event_manager.log_level = ttrk.log_debug
#-- Run the experiment
num2pos.run_trials(exp_info)
#-- Shutdown Expyriment
xpy.control.end()