How to change mean intensity of grating stim

Hi Everyone,

I’m using psychopy.visual.GratingStim and am struggling to figure out if there is a way to change the mean intensity of the grating. For example, I would like to be able to create a grating where the mean of the dark and light cycles has a color value of 0.25, for example. I understand that the color/contrast can be changed by using GratingStim.color, but the mean always seems to remain at 0 (middle grey) no matter what I try.

Thanks for the help,
Scott

For anyone curious about this in the future, I figured out that you can change the mean value by drawing a semi-opaque rectangle over the grating. For example, if you want to change the mean of the grating to 0.5, you can do something like:

grating = visual.GratingStim(
   win,
   color = [1.0, 1.0, 1.0]
)

coverRectangle = visual.Rect(
   win,
   color = [1, 1, 1],
   opacity = 0.5
)

grating.draw()
coverRectangle.draw()
win.flip()

Now you should have a grating with mean = 0.5. You can then further adjust the contrast of the grating and the mean should remain the same.

If you want to make the grating mean less than 0, use

coverRectangle.color = [-1, -1, -1]

and then assign opacity as before (e.g. if you want the mean to be -0.75 assign the rectangle opacity to 0.75)

Cheers,
Scott

Hi Scott,

I was wondering, does the contrast have to be set before the drawing ? Or should one draw the grating and the luminance mask, and then do the contrast change (and re-draw…) ?

I’m not 100% sure but it seems like either way should work. I typically set everything before drawing because I do not tend to dynamically update the mean intensity or contrast. See here for example: https://github.com/ScottHarris17/Bassoon/blob/main/src/protocols/MovingGratingDirection.py