| Reference | Downloads | Github

Using TrialHandler to save data as a .csv file


I am trying to save some Stroop data from one participant into a .csv file. The experiment runs fine, its just when the script is told to save the data, it only manages to save the headers I have created, not the data. Also, it saves some headers that I don’t really understand. If someone could help me save the data and explain the unknown headers to me that would be great! :slight_smile:

my code:

from datetime import datetime

#create csv file to store stimuli in order to call them using trial handler

#create a dictionary variable to hold the correct answer keys for each colour of the words 
answer_keys = {'f': 'red', 'g': 'blue', 'h': 'green', 'j': 'yellow'}

dat = {'Expdate':'%Y%m%d_%H%M'), 'ID': '', 'Age': '', 'Gender': ''}
dlg = gui.DlgFromDict(dat, title='Input participant info', fixed='Expdate', order=['ID', 'Age', 'Gender','Expdate'])

if not dlg.OK:
    print("User exited")
filename = 'part_{}_{}_{}_{}.csv'.format(data['ID'], data['Age'], data['Gender'], data['Expdate'])

#experimentalHandler - to help with data saving
thisExp = data.ExperimentHandler(name = 'Strooptask', version='1.0', extraInfo = {'participantNo': '', 'gender':''}, dataFileName = filename) # in this saved data file

# set up a loop handler to look after randomisation of conditions etc:
trials = data.TrialHandler(nReps = 1, method = 'random', 
    trialList = data.importConditions('stroop_stimuli.xlsx'),
    name = 'trials')

thisExp.addLoop(trials)  # add the loop to the experiment

#set up experimental window, instruction text for participant and ready screen
win = visual.Window([1024, 768], fullscr = False, \
                    allowGUI=True, units="pix", color = (-1, -1, -1))

stim = visual.TextStim(win)

instructionText = visual.TextStim(win, "Thank you for choosing to participate in this experiment. You will be presented with different names of colours. In some trials the colour of the word will match the name of the colour, in some the colour of the word \
                                  will be different to the name of the colour. Your task is to identify the colour of the word, ignoring the word itself. Coloured stickers on the keyboard in front of you correspond to the key you should press to indicate your response.\
                                  PRESS 'ENTER' TO CONTINUE", color=(1, 1, 1))


#create and prepare ready text so participant can prepare before experiment - repeat any instructions
readyText = visual.TextStim(win, "Ready?", color=(1, 1, 1))


#present ready text on the screen 

#wait for user to press return key 

for thisTrial in trials:
    # display stimulus:
    stim.text = thisTrial['colour_text']
    stim.color = thisTrial['colour_name']
    displaytime = win.flip()

    # get response:
    key, rt = event.waitKeys(keyList = answer_keys.keys(), timeStamped = core.Clock())[0]

    # save response in data file:
    thisExp.addData('rt', rt)
    thisExp.addData('response', key)
    # decide if response was correct:
    thisExp.addData('correct', answer_keys[key] == thisTrial['colour_name'])

    # now cycle to next trial automatically
#want a message to thank the participant for their time
thankYou = visual.TextStim(win, "Thank you for participating.", color=(1, 1, 1))


#present it on the screen

#wait for user to press return key

# all done, so terminate the session.
# I think this will tell the expt handler to save the data as it shuts down:

my .xlsx stimuli file:

the .csv data file at the moment:



You have to call thisExp.nextEntry() at the end of every iteration of your trial loop.



The columns of your file are automatically generated by your ExperimentHandler when you add your TrialHandler and the extraInfo to it.



thank you very much for your reply! it was very helpful :slight_smile:

1 Like