PyEPL simple example

From MallWiki

Jump to: navigation, search

Below is pyEPL code for a simple recognition experiment requiring a six point confidence rating. It includes the coding for sending pulses to EEG equipment and creates a custom log file of when the pulse was sent, the type of trial, the response the participant made, and whether or not the response was correct. It requires the instruction text files mentioned in the code.


# acess pyEPL and random module
from pyepl.locals import *
from random import *


def keystr(key):
	if key == Key("1"):
		return "1"
	elif key == Key("2"):
		return "2"
	elif key == Key("3"):
		return "3"
	elif key == Key("8"):
		return "8"
	elif key == Key("9"):
		return "9"
	elif key == Key("0"):
		return "0"
	else:
		return "unknown"

def acc(key):
	if test[i] in study and key == Key("1"):
		return "correct"
	elif test[i] in study and key == Key("2"):
		return "correct"
	elif test[i] in study and key == Key("3"):
		return "correct"
	elif test[i] in study and key == Key("8"):
		return "error"
	elif test[i] in study and key == Key("9"):
		return "error"
	elif test[i] in study and key == Key("0"):
		return "error"
	elif key == Key("8") or key == Key("9") or key == Key("0"):
		return "correct"
	else:
		return "error"


#-----------Create Study & Test Lists-------------
# create experiment object
exp = Experiment()


# press esc + F1 to end experiment if needed
exp.setBreak()


# interface with monitor and keyboard
video = VideoTrack("video")
keyboard = KeyTrack("key")
eeg = EEGTrack("eeg") 
eeg.stopLogging() 
eeglog = LogTrack("customEEG")


# reset display to black
video.clear("black")


# set timing to clock
clk = PresentationClock()


#stimuli
words = ["ACRES","APPLY","BEGUN","BONDS","CHAIN","CRIED","DRAMA","ENDED","FEWER","FOODS","GRAND","GUILT","HOPED","LIKED",
         "MAYOR","NAVAL","OWNED","POETS","RAISE","ROYAL"]


#randomise words
random.shuffle(words)


#create study list
study = words[0:(len(words)/2)]
random.shuffle(study)


#create test list
test = words1
random.shuffle(test)


# open study instructions file
sinstructions = open("sinstructions.txt").read()


# display study instructions
instruct(sinstructions)


# delay study list first item for 1 second
clk.delay(1000)
flashStimulus(Text("Loading..."), clk=clk, duration = 5000)
flashStimulus(Text(""), clk=clk, duration = 1000)


#log start of study 
eeglog.logMessage("study stt1" ,clk)


#present study items & send & log pulse
i = 0

while i < len(study):
	flashStimulus(Text("+"), clk=clk, duration = 500)
	flashStimulus(Text(""), clk=clk, duration = 500)
	flashStimulus(Text(study[i]), clk=clk, duration = 1500)
	#eeg.timedPulse(10)
	flashStimulus(Text(""), clk=clk, duration = 500)
	eeglog.logMessage("Study " + study[i],clk)
	i = i + 1


#log end of study cycle
eeglog.logMessage("study end1" ,clk)


# get and display test instructions
tinstruct1 = open("tinstructions1.txt").read()
instruct(tinstruct1)
clk.delay(1000)
tinstruct2 = open("tinstructions2.txt").read()
instruct(tinstruct2)
clk.delay(1000)
tinstruct3 = open("tinstructions3.txt").read()
instruct(tinstruct3)
clk.delay(1000)
flashStimulus(Text("Loading..."), clk=clk, duration = 5000)
flashStimulus(Text(""), clk=clk, duration = 1000)


#log start of test cycle
eeglog.logMessage("test stt1" ,clk)


# display test items for 2000ms then get 6-point confidence response and record trial and response
i = 0

while i < len(test):
	flashStimulus(Text("+"), clk=clk, duration = 500)
	flashStimulus(Text(""), clk=clk, duration = 500)
	flashStimulus(Text(test[i]), clk=clk, duration = 2000)
	#eeg.timedPulse(10)
	flashStimulus(Text(""), clk=clk, duration = 500)
	bc = ButtonChooser(Key("1"), Key("2"), Key("3"), Key("8"), Key("9"), Key("0"))
	resp = Text("Sure Old = 1 / Sure New = 0")
	ts, b, rt = resp.present(clk=clk, duration = 5000, bc=bc)
	flashStimulus(Text(""), clk=clk, duration = 1000)
	if test[i] in study:
		eeglog.logMessage("Test " + test[i] +" target " + keystr(b) + " " + acc(b),clk)
	else:
		eeglog.logMessage("Test " + test[i] +" distracter " + keystr(b) + " " + acc(b),clk)	
	i = i + 1


#log end of test 
eeglog.logMessage("test end1" ,clk)


#let participant know it is the end of the experiment
end = open("end.txt").read()
instruct(end)