psychopy.org | Reference | Downloads | Github

Save as wide text doesn't save all stimuli

Hello, in my flanker task experiment, the first trial has four stimuli, but when I try to save the results to a csv file using saveAsWideText, I only get the first three and these seems to be the same stimulus (in my case the sequence ‘CCCSCCC’) repeated three times.
Here’s the code:

from psychopy import visual, core, data, event, gui
from psychopy.hardware import keyboard

expName = "Eriksen's Flanker Task"

exp = data.ExperimentHandler(name=expName, savePickle=True, saveWideText=True)

kb = keyboard.Keyboard()

trial_timer = core.Clock()

flank_stimuli = ['KKKHKKK','SSSCSSS','HHHKHHH','CCCSCCC']  # experiment stimuli, congruent condition

for stim in flank_stimuli:
    
    if stim[3] == 'H':
        correct_response = 'x'
        
    elif stim[3] == 'K':
        correct_response = 'x'
        
    else:
        correct_response = 'm'

trials = data.TrialHandler(flank_stimuli, 1, method='random')

trials.data.addDataType('RT')
trials.data.addDataType('Response')
trials.data.addDataType('Accuracy')

trials.setExp(exp)

experiment_window = visual.Window(size=(800,600),winType='pyglet',fullscr=True, screen=0,color="black", colorSpace='rgb') # creating the experiment window

experiment_window.mouseVisible = False

screen_text = visual.TextStim(experiment_window,text=None, alignText="center", anchorHoriz='center', color = 'white') # drawing the stimuli


kb.clock.reset()


for trial in trials:
    current_time = 0
    trial_run = True
    trial_timer.reset()


    while trial_run:
        current_time = trial_timer.getTime()
        
        keys = kb.getKeys(['x', 'm', 'space', 'escape'], waitRelease=True)
        
        if 'escape' in keys:
            core.quit()
            
        
        kb.start()
        
               
        screen_text.setText('+')
        screen_text.draw()
        experiment_window.update()
        core.wait(1.0)
        
        kb.clock.reset()

        screen_text.setText(trial)
        screen_text.draw()
        experiment_window.update()
        
        
        core.wait(2.0)
        
        
        experiment_window.flip()        
        kb.stop()
        
  
        for key in keys:

            if key == correct_response:
                accuracy = 1
            else:
                accuracy = 0
                
        
            if not key:
                accuracy = 0
        
        
            RT = key.rt
            resp = key.name
        
            exp.addData('Stimulus', stim)
            exp.addData('Response', resp)
            exp.addData('Accuracy', accuracy)
            exp.addData('RT', RT)
            
        exp.nextEntry()
     
        break
    
    trial_timer.reset()

exp.saveAsWideText(fileName='flank_raw_new.csv',appendFile=True)
exp.saveAsPickle(fileName='flank_raw')

experiment_window.close()

core.quit()

Sorry if I made some banal mistake but I’m just a beginner in python

Just to be clear, this is the output of my .csv file

Stimulus,Response,Accuracy,RT,
,
CCCSCCC,x,0,0.49144560005515814,
CCCSCCC,x,0,0.47231839993037283,
CCCSCCC,m,1,0.2719296000432223,

When I should get all the four conditions that were presented

So, I went a step further, but still not solved.

I set my stim=trial , and now I manage to get different entries in my csv file.

As an example, having these stimuli presented:
CCCSCCC
HHHKHHH
SSSCSSS
KKKHKKK

I get this file:

Stimulus,Response,Accuracy,RT,
,,,,
HHHKHHH,m,0,0.49020720000044093,
SSSCSSS,x,0,0.38491699999940465,
KKKHKKK,m,0,0.5070997000002535,

So the first simulus is missing, while the last kb entry is not recorded, and my data are completely wrong :frowning:

I can’t really make out the intention of your code, but you should only be calling exp.addData() once per trial (at the end of that trial). Otherwise, every time you call it, you are just overwriting the previous value. At the moment it looks like you are calling it deeply nested within a loop within a loop within your trial.

So check your indenting and only call these functions to store data once, at the highest level of indenting within your for trial in trials:

1 Like