psychopy.org | Reference | Downloads | Github

NameError: global name 'expData' is not defined

eyetracking
#1

I am new to Python and PsychoPy. Our experiment had been freezing and we could not figure out the issue. It was suggested to us that there was a problem with the ‘while loop’. We were given code to fix that “‘while loop’ freezing issue”. Now, instead of fixing the original problem, this new code comes with a “NameError: global name ‘expData’ is not defined” problem. This had not happened before.

THE CODE:
Lines 62-72:

while not (pReady and oReady):
        # Go look for keyboard events
        if not pReady:
            for key in event.getKeys():
                if key in ['left','right']:
                    pReady = 1
                    expData[trial]['rt'] = timer.getTime()
                    if key in ['left']:
                        expData[trial]['choice'] = 1
                    else:
                        expData[trial]['choice'] = 0
        # Go look for opponent response
        if timer.getTime() > 2.5:
            oReady = 1
    #??
    return [oReady,pReady]

Lines 234-258:

while not (pReady and oReady):
        # Go look for keyboard events
        if not pReady:
            for key in event.getKeys():
                if key in ['left','right']:
                    rt = timer.getTime()
                    pReady = 1
                    needDraw = 1
                    expData[trial]['rt'] = timer.getTime()
                    if key in ['left']:
                        expData[trial]['choice'] = 1
                    else:
                        expData[trial]['choice'] = 0

THE ERROR:

Traceback (most recent call last):
  File "T:\Peipei's TBI data\Data\Pupil-ITI_MS\main.py", line 106, in <module>
    PD_trials2.run_trials(tracking, baseName, subNum)
  File "T:\Peipei's TBI data\Data\Pupil-ITI_MS\PD_trials2.py", line 602, in run_trials
    ret_value = do_trial(tracking, expData, trial, stim, logFile)
  File "T:\Peipei's TBI data\Data\Pupil-ITI_MS\PD_trials2.py", line 427, in do_trial
    expData[trial]['choice'], expData[trial]['rt'] = drawReady(surf, stim)
  File "T:\Peipei's TBI data\Data\Pupil-ITI_MS\PD_trials2.py", line 242, in drawReady
    expData[trial]['rt'] = timer.getTime()
NameError: global name 'expData' is not defined

There was no problem with this variable before, is it possible the “timer.getTime()” is creating the error? This is also used earlier on line 68. Is it just not clearing the values to get new ones? Or could it be something else entirely?

0 Likes

#2

The issue is simply that your code is referring to an object called expData before that object has actually been created.

expData seems to hold information about your trials, but we can’t tell you exactly what it is or how it should be created within this custom code. You need to work through this in discussion with whoever gave you that code. My worry here is that if you aren’t able to deal with this issue, which causes a show-stopping error, you might not notice or be able to deal with more subtle problems that could arise. Is there someone local who is familiar with Python (and ideally PsychoPy), who can help you develop and test this script?

0 Likes

#3

First, thank you for posting. I have no experience working with PsychoPy and I recently was brought onto the study using this program. The person who built the program is no longer here and no one in my foundation has experience with Python or PsychoPy. For now, I would like to see what I can do to try to solve this issue or learn as much about it as I can to help someone else solve it. All of your advice and guidance truly is appreciated.

As for the object, it is a variable that stores the behavioral data (choices participants made). Is it possible that the variable was defined within the while statement? The program was working without the error before we changed that statement. Also, would it help if I provided more code from the program?

0 Likes