psychopy.org | Reference | Downloads | Github

Set Every Repeat Error in Code

# Initialize components for Routine "MainComponent"
    MainComponentClock = core.Clock()
    WordStim = visual.TextStim(win=win, name='WordStim',
        text='default text',
        font='Arial',
        pos=(0, 0), height=0.1, wrapWidth=None, ori=0, 
        color='white', colorSpace='rgb', opacity=1, 
        languageStyle='LTR',
        depth=0.0);

    # Create some handy timers
    globalClock = core.Clock()  # to track the time since experiment started
    routineTimer = core.CountdownTimer()  # to track time remaining of each (non-slip) routine 

    # set up handler to look after randomisation of conditions etc
    randLoop = data.TrialHandler(nReps=5, method='random', 
        extraInfo=expInfo, originPath=-1,
        trialList=data.importConditions('word_stimuli.xlsx'),
        seed=None, name='randLoop')
    thisExp.addLoop(randLoop)  # add the loop to the experiment
    thisRandLoop = randLoop.trialList[0]  # so we can initialise stimuli with some values
    # abbreviate parameter names if possible (e.g. rgb = thisRandLoop.rgb)
    if thisRandLoop != None:
        for paramName in thisRandLoop:
            exec('{} = thisRandLoop[paramName]'.format(paramName))

    for thisRandLoop in randLoop:
        currentLoop = randLoop
        # abbreviate parameter names if possible (e.g. rgb = thisRandLoop.rgb)
        if thisRandLoop != None:
            for paramName in thisRandLoop:
                exec('{} = thisRandLoop[paramName]'.format(paramName))
        # ------Prepare to start Routine "MainComponent"-------
        t = 0
        MainComponentClock.reset()  # clock
        frameN = -1
        continueRoutine = True
        routineTimer.add(1.500000)
        # update component parameters for each repeat
        #Stimuli = data.importConditions('word_stimuli.xlsx')
        #Stimuli = inSheet.row_values(0)
        #Stimuli = Stimuli[]
        WordStim.setText(Stimuli)
        # keep track of which components have finished
        MainComponentComponents = [WordStim]
        for thisComponent in MainComponentComponents:
            if hasattr(thisComponent, 'status'):
                thisComponent.status = NOT_STARTED```

In the code above: a list of words is loaded from an excel file. The header of the column in that excel is ‘Stimuli’,
The code above was created using builder and then edited in code.
It is set to be random, and “Set Every Repeat”. Using the builder, the text box component I wrote $Stimuli
Which prompts it to use the header of that column for all its values and present one word every repeat.

It works perfectly when I run the code using python shell.
However, when I package the code into a function so that it becomes:


#imports go here
import....

def present():
    # Exp Info
...
    # Setup Window
...
# ------Prepare to start Routine "MainComponent"-------
        t = 0
        MainComponentClock.reset()  # clock
        frameN = -1
        continueRoutine = True
        routineTimer.add(1.500000)
        # update component parameters for each repeat
        #Stimuli = data.importConditions('word_stimuli.xlsx')
        #Stimuli = inSheet.row_values(0)
        #Stimuli = Stimuli[]
        WordStim.setText(Stimuli)
        # keep track of which components have finished
        MainComponentComponents = [WordStim]
        for thisComponent in MainComponentComponents:
            if hasattr(thisComponent, 'status'):
                thisComponent.status = NOT_STARTED

I get an error when packaging it into a function, the error is that:

Word.Stim.setText(Stimuli)

Error: Stimuli is not defined.

Stimuli refers to the list of words that a random one is chosen from every repeat.

When i remove def present(): … It works again…


The reason why I am facing this problem is because I want to import a function that runs this experiment into my main code. If I don’t fit this code into a function, I cannot import it into my other main python file.

Any help is appreciated!

The Stimulus variable should change for every iteration of a for loop over a trial handler iterable. Could you include this loop in the excerpt, which you show?