Square GratingStim with two colors

565 views
Skip to first unread message

Gaelen Hadlett

unread,
Jul 12, 2014, 6:07:52 PM7/12/14
to psychop...@googlegroups.com


I need to create a square GratingStim that alternates between blue & green and red & green using the Coder.  I couldn't figure out how to create it without using a 2x2 PNG texture.  Here is a snippet of that:

std_stim = visual.GratingStim(
    window
,
    tex
='blue-green.png',
    texRes
=256,
    units
='deg',
    sf
=5.25,
    size
=2
)

dev_stim = visual.GratingStim(
    window
,
    tex
='red-green.png',
    texRes
=256,
    units
='deg',
    sf
=5.25,
    size
=2
)

That seems to work (give or take—the sf and size are so small that the bars of the stim are not always equal pixels).  My problem is that I need them to be isoluminant, so I need to use the DKL space.  I took the measurements with the photometer and have everything else involved with using DKL in place, but I do not know how to create a square GratingStim using any color  space (DKL or RGB) to alternate between two specific colors.

I haven't come across anything in the docs that explains how to do this.  Is there a way to create a square GratingStim where the bars alternate between blue & green and red & green?

Jonathan Peirce

unread,
Jul 15, 2014, 7:53:45 AM7/15/14
to psychop...@googlegroups.com
No, I'm afraid that gratings alternating between arbitrary colours does require you to create them yourself using an array or an image. PsychoPy's grating colours are designed to balance around the mean grey.

For an arbitrary DKL-coloured stimulus I would;
    - create an array that is NxNx3 (e.g. 128x128x3)
        - the last dimension with 3 levels allows you to specify the elevation, azimuth and intensity of your colours. For isoluminant stimuli the eleveation will presumably be  for every pixel and the intensity (contrast) may as well be 1.0 for every pixel, so you might be changing just the azimuth for each point in space
        - use a single cycle of the grating and you can use the spatial frequency parameters of PsychoPy to control the number of cycles on the actual stimulus
        - beware that
    - convert that into an rgb array using psychopy.misc.dkl2rgb (you need to provide the dkl2rgb conversion matrix from your monitor calibration to this function)
    - provide the rgb texture to the grating stimulus
    - stimulus contrast will still work to scale the colours

Note that some colours aren't possible. For the cardinal axes (azimuths 0,90,180,270) you should be fine, but at oblique azimuth/elevations you won't get to a contrast/intensity of 1.0 without overmodulating. When PsychoPy thinks this is going to happen it generates a blue/yellow stimulus in standard rendering mode, or a red/blue speckled pattern for new windows with useFBO=True

best wishes
Jon
--
You received this message because you are subscribed to the Google Groups "psychopy-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to psychopy-user...@googlegroups.com.
To post to this group, send email to psychop...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/psychopy-users/66c5f2a1-7874-4d05-9590-234351f3118a%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Gaelen Hadlett

unread,
Jul 16, 2014, 10:17:31 AM7/16/14
to psychop...@googlegroups.com
Just to make sure I'm understanding this correctly:

 - With an NxNx3 array, the grating stim will map one cycle across the NxN array, and use the colors values at each cell for the pixel.
 - For the last pixel column in a horizontal grating stim, it will use the values from N[-1]xNx3
 - For the next cycle, it will loop back to N[0]xNx3 to get the values for the first pixel column in a horizontal grating stim.

Does that sound correct?

Since it's a square grating, could I get away with just using a 2x2x3 array to represent the mapping?

Do I need to manually convert the DKL colors to RGB?  I was under the assumption that if I set the colorspace to DKL and provided a conversion matrix (using data created in the monitor center from a photometer), that PsychoPy would handle the conversion.

Thanks,
Gaelen

Jonathan Peirce

unread,
Jul 18, 2014, 5:30:27 AM7/18/14
to psychop...@googlegroups.com
2x2x3 will be OK if you turn off interpolation. If you don't drift your grating and keep it horizontal or vertical that will be fine. For oriented gratings and drifting sqr gratings I would go with some a bit higher resolution and allow interpolation to create a smooth transition around the boundary.

For the color spaces, no the value of textures is always in RGB (-1:1) and the stimulus color is applied to that RGB value at each point. If you want colors that aren't centred on mean grey as you suggest, then you can't use psychopy's built-in color system you need to convert your colors to rgb and supply them as the texture instead. But you can use psychopy functions to do that, as I pointed to before

Jon

For more options, visit https://groups.google.com/d/optout.

-- 
Jonathan Peirce
Nottingham Visual Neuroscience

http://www.peirce.org.uk

Gaelen Hadlett

unread,
Jul 21, 2014, 12:20:10 PM7/21/14
to psychop...@googlegroups.com
Ah, your first response makes complete sense now.  Thanks for the additional clarification.  PsychoPy has an awesome been tool for our visual ERP project (much easier to program and refine than Presentation and E-Prime).
Reply all
Reply to author
Forward
0 new messages