I am doing an experiment and I want to have 3 conditions that look like this: C1: Right ones: grib and tryk. Wrong ones: hop, hvisk C2: Right ones: slik and tryk. Wrong ones: kast, hop C3: Right ones: vink and tryk. Wrong ones: kast, hop
My script currently looks like this: I am not very experienced with PsychoPy yet and I really need this experiment to be programmed soon so I can get participants, so I would really appreciate very specific changes, written lines of code or likewise, instead of more complex answers. I hope you understand, and thanks in advance! :-)
# -*- coding: utf-8 -*-
# Grounding of Words Experiment
#Import libraries
import re
import glob
from psychopy import sound, visual, event, data, core, gui # imports a module for visual presentation and one for controlling events like key presses
import random #you need to import the random module to sample/randomize things
from psychopy import data
import pandas as pd
import numpy as np
# ID, age, gender box display
myDlg = gui.Dlg(title="Experiment") #, pos=(400,400)
myDlg.addField('ID:')
myDlg.addField('Age:')
myDlg.addField('Gender:', choices = ['Female', 'Male'])
myDlg.addField('Condition order:', choices = ['1', '2', '3'])
myDlg.show()#you have to call show() for a Dlg
if myDlg.OK:
ID = myDlg.data[0]
Age = myDlg.data[1]
Gender = myDlg.data[2]
Cond_order = myDlg.data[3]
else:
core.quit()
# define window
win = visual.Window(fullscr=True)
### STIMULI ###
# number of repetition of stimulus items
REP = 3
# sound stimuli
stimuli = ["tryk.wav", "kast.wav", "loeb.wav", "slik.wav", "traed.wav", "bold.wav", "hvisk.wav", "grib.wav", "roeb.wav", "vink.wav"]
# multiply stimuli
stimuli = stimuli*REP
# create pandas data frame for logfile
columns = ['ID', 'Age', 'Gender', 'Condition_order', 'Trial', 'Condition', 'Stimulus', 'Target', 'Correct', 'Reaction_time']
index = np.arange(0) # array of numbers for the number of samples
logfile = pd.DataFrame(columns=columns, index = index)
# function to draw instructions (probably overkill unless run more than once)
def instruct(txt):
instructions = visual.TextStim(win, text=txt, height = 0.05) # create an instruction text
instructions.draw() # draw the text stimulus in a "hidden screen" so that it is ready to be presented
win.flip() # flip the screen to reveal the stimulus
event.waitKeys() # wait for any key press
# Condition order
if Cond_order == '1':
conditions = ['klik','kast','slik']
elif Cond_order == '2':
conditions = ['kast', 'sov', 'klik']
elif Cond_order == '3':
conditions = ['sov', 'klik', 'kast']
# this is a function that checks if a list with other nested lists is empty
def isEmpty(alist):
try:
for a in alist:
if not isEmpty(a):
return False
except:
return False
return True
# display instructions
instruct('''Welcome to the experiment!\n\nYou will be going through three sessions where you will hear different words.\n
Each session has a particular target word. The target word will be anounced in the beginning of the session. \n
Whenever you hear the word please press the 'space' key.\n
Whenever you hear any other word - do nothing.\n
Try to be as fast and accurate as possible.\n
Please put on the headphones.\n
The experiment will take 5 minutes.
Press any key to start the experiment''')
# loop through the three conditions (sessions)
for condition in conditions:
# shuffle stimuli order
random.shuffle(stimuli)
# show trial instruction
instr_txt = "Your target word in this session is '" + condition + "'" + "\n\n\nPress space to continue..."
instruct(instr_txt)
# loop through trials
for trial in range(len(stimuli)):
# select stimulus
stimulus = stimuli[trial]
# prepare stimulus
recording = sound.Sound(stimulus)
# play stimulus
recording.play()
# reset keys
keys = []
# record start time
time_start = win.flip()
# Number of frames before next sound comes on
for frame in range(120):
win.flip()
# if keys is empty append new key stroke
if isEmpty(keys):
keys.append(event.getKeys(keyList=['space', 'escape'], timeStamped = True))
# if keys is not empty - i.e. there has been a key stroke
if not isEmpty(keys):
key, time_key = keys[-1][0]
rt = time_key - time_start
recording.stop() # ready to start next trial immediately
if key == 'escape':
core.quit()
if stimulus[:-4] == condition:
target = 1
accuracy = 1
else:
target = 0
accuracy = 0
# if we reach the max out time and keys is still empty
elif frame == 119 and isEmpty(keys):
if stimulus[:-4] == condition:
target = 1
accuracy = 0
rt = 'NA'
else:
target = 0
accuracy = 1
rt = 'NA'
# write all values to logfile
logfile = logfile.append({
'ID': ID,
'Age': Age,
'Gender': Gender,
'Condition_order': Cond_order,
'Trial': trial,
'Condition': condition,
'Stimulus': stimulus,
'Target': target,
'Correct': accuracy,
'Reaction_time': rt
}, ignore_index=True)
# logfile name
logfilename = 'logfile_' + ID + '.csv'
# save logfile to working directory
logfile.to_csv(logfilename, index=False)
# display goodbye
instruct('''
The experiment is done.\n
Thank you very much for your participation!!!
''')
# close experiment
win.close()
core.quit()
Aucun commentaire:
Enregistrer un commentaire