Name Error: name 'win' is not defined

New to python and psychopy.

Trying to produce Gabor noise…

This is my code:

from psychopy.visual import GratingStim
from psychopy import visual

Noise_rc = 1

noise_rc = GratingStim(win, tex='raisedCos', mask='gauss', sf=0.9, size=1000, phase=0.3,opacity=1, contrast=Noise_rc)
noise_rc.draw()
win.flip()

Produces this error:

NameError: name 'win' is not defined

I do not understand why and there is nothing online to answer this question.

Can you help me?

Thanks

Firstly, this is just a general Python error: you are trying to refer to a variable (win) that has never been defined, so Python simply doesn’t know what to do with it. The same thing would have happened if you didn’t have a line defining noise_rc as having the value 1 before you attempted to use it when creating the grating stimulus.

In PsychoPy specific terms, win is just a common variable name for referring to the window you want to draw into. There is nothing magical about that variable name, you could call it anything you want (lcd_screen, participant_window, etc). In PsychoPy code, many of us just commonly use the name win because it is short and we all know what it means. But whatever you call it, it needs to be defined before you try to use it.

Just check any PsychoPy example script. Near the top, not long after the import lines, will be a line that looks like this:

win = visual.Window() # now you have a window to actually draw into.

In the Coder view, check out the Demos menu. There are many useful example scripts available there that show how to display stimuli, get keypresses, control timing, and so on. Good luck with learning the ropes.

2 Likes

Thanks so much!

I thought it was an argument of the GratingStim Class which had a default value e.g. true! even inthe documentation its specified as ‘win’ without any further info as far as I could see.

Anyway, thanks I am gonna look at it more deeply

One last question, please

Is their a way to export the stimulus in a .png from psychopy?

Ahh, OK, to clarify for others, when creating all visual stimuli, the first named parameter is called win, but we often leave out the name of the parameter and just pass it’s value, we could be more explicit and say GratingStim(win = win, …)

Yes, look up the getMovieFrame() and saveMovieFrames() method of the Window class.