# ElementArray of gratings with non-zero pedestal value

**URL:** <https://discourse.psychopy.org/t/elementarray-of-gratings-with-non-zero-pedestal-value/904>\
**Category:** Coding\
**Created:** [August 18, 2016, 2:21pm UTC](https://discourse.psychopy.org/t/elementarray-of-gratings-with-non-zero-pedestal-value/904 "2016-08-18T14:21:23Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![mwaskom](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/mwaskom/32/1598_2.png) [@mwaskom](https://discourse.psychopy.org/u/mwaskom)\
**Post date:** [August 18, 2016, 2:21pm UTC](https://discourse.psychopy.org/t/elementarray-of-gratings-with-non-zero-pedestal-value/904/1 "2016-08-18T14:21:24Z")

</div>

Hi all,

I would like to draw a number of gratings on a background that is darker than 0 (in Psychopy’s [-1, 1] convention) luminance. I need to be able to manipulate the contrast of these gratings from trial to trial. I want the mean luminance to be constant across the window, so the contrast needs to change the span of the grating luminance around the background value (pedestal) My understanding is that this is not currently possible out of the box in Psychopy.

I’ve found some discussion of a similar problem using `GratingStim` directly on the [old mailing list](https://groups.google.com/forum/#!topic/psychopy-users/e2MM3EyKiGM). This led me to [this](https://github.com/nwilming/PedestalGrating/blob/master/pedestal_grating.py) very nice piece of code that adds a pedestal value to the Psychopy `GratingStim`.

I’m willing to do some hacking to get the same functionality in `ElementArrayStim`, but I’m not exactly sure where to start. The `PedestalGratingStim` seems to work mainly by overriding the `_updateListShaders` method, but this method does not exist for `ElementArrayStim`.

Can you please give me some hints for how to apply the solution in `PedestalGratingStim` to make a `PedestalElementArrayStim`?

Thanks!

---

<div class="post-metadata">

**Author:** ![mwaskom](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/mwaskom/32/1598_2.png) [@mwaskom](https://discourse.psychopy.org/u/mwaskom)\
**Post date:** [August 18, 2016, 3:17pm UTC](https://discourse.psychopy.org/t/elementarray-of-gratings-with-non-zero-pedestal-value/904/2 "2016-08-18T15:17:03Z")

</div>

Replying to myself:

I determined that the relevant code that was in `GratingStim._updateListShaders` appears in the `ElementArrayStim.draw` method. Therefore, I applied the solution from `PedestalGratingStim` (see link above) rather directly. I now have an object that, in minimal testing, behaves the way I want it to. I have put my code into a [gist](https://gist.github.com/mwaskom/495a7f60a1c0e4343e0376d7f6df8314).

It would still be helpful to know if I am missing anything obvious about the difference between `GratingStim` and `ElementArrayStim` that is going to cause me problems.

---

<div class="post-metadata">

**Author:** ![mwaskom](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/mwaskom/32/1598_2.png) [@mwaskom](https://discourse.psychopy.org/u/mwaskom)\
**Post date:** [August 18, 2016, 3:27pm UTC](https://discourse.psychopy.org/t/elementarray-of-gratings-with-non-zero-pedestal-value/904/3 "2016-08-18T15:27:19Z")

</div>

Actually I suppose this is not fully fixed. Things work fine if I import my module _after_ opening a PsychoPy window. However, if I import my module before doing that, it fails with

```auto
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-2-111c4cce99b8> in <module>()
----> 1 import pedestalelementarray

/Users/mwaskom/Google Drive/for__Michael/Projects/scdp_python/pedestalelementarray.py in <module>()
     36 '''
     37
---> 38 class PedestalElementArrayStim(ElementArrayStim, MinimalStim, TextureMixin):
     39 """
     40 This stimulus class defines a field of elements whose behaviour can be independently

TypeError: Error when calling the metaclass bases
    metaclass conflict: the metaclass of a derived class must be a (non-strict) subclass of the metaclasses of all its bases

```

I’m not fully sure I understand this error or why it seems to be dependent on whether a `Window` has been opened. Any thoughts?

---

<div class="post-metadata">

**Author:** ![mwaskom](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/mwaskom/32/1598_2.png) [@mwaskom](https://discourse.psychopy.org/u/mwaskom)\
**Post date:** [August 18, 2016, 6:22pm UTC](https://discourse.psychopy.org/t/elementarray-of-gratings-with-non-zero-pedestal-value/904/4 "2016-08-18T18:22:32Z")

</div>

OK, I suppose that’s [this issue](https://github.com/psychopy/psychopy/issues/1159).

Easily solved by importing `ElementArrayStim` directly from `psychopy.visual.elementarray`.

---

<div class="post-metadata">

**Author:** ![djmannion](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/djmannion/32/37535_2.png) [@djmannion](https://discourse.psychopy.org/u/djmannion)\
**Post date:** [August 18, 2016, 7:01pm UTC](https://discourse.psychopy.org/t/elementarray-of-gratings-with-non-zero-pedestal-value/904/5 "2016-08-18T19:01:46Z")

</div>

I might not be completely understanding, but I think this should be able to be done with a regular `GratingStim` or `ElementArrayStim` by setting (before drawing) `win.blendMode = "add"` and adjusting the contrast?

---

<div class="post-metadata">

**Author:** ![jon](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/jon/32/22_2.png) [@jon](https://discourse.psychopy.org/u/jon)\
**Post date:** [August 19, 2016, 9:50am UTC](https://discourse.psychopy.org/t/elementarray-of-gratings-with-non-zero-pedestal-value/904/8 "2016-08-19T09:50:21Z")

</div>

> [@djmannion](#):
>
> I might not be completely understanding, but I think this should be able to be done with a regular GratingStim or ElementArrayStim by setting (before drawing) win.blendMode = “add” and adjusting the contrast?

To create the pedestal that @mwaskom needs he’d need to draw once with sf=0 and color set to be his desired pedestal, then with blendMode = ‘add’ draw again (now with his actual desired element SFs) but that all feels like a bit of a hack.

Jonas’ solution definitely won’t work because, as pointed out, it changes the contrast not the luminance.

The gist code that is linked by @mwaskom is the right thing to do (which we should add to these stimuli again). It doesn’t need to be done in the update list section though, it can be done in the regular `draw()` code instead for stimuli that don’t use lists. The key pieces are the creation of the fragment shader code at the top (to replace the standard shader for the stimulus) and then the step whereby the value of the pedestal is passed to the fragment shader which is this in the gist linked. That’s the part that could happily go in the list creation or in the draw function:

```python
        GL.glUseProgram(self._progSignedTexMask)
        GL.glUniform1i(GL.glGetUniformLocation(self._progSignedTexMask, "texture"), 0) #set the texture to be texture unit 0
        GL.glUniform1i(GL.glGetUniformLocation(self._progSignedTexMask, "mask"), 1) # mask is texture unit 1
        GL.glUniform1f(GL.glGetUniformLocation(self._progSignedTexMask, "pedestal"), self.pedestal) # mask is texture unit 1

```

I can’t think of anything else you need to worry about 🙂

---

<div class="post-metadata">

**Author:** ![mwaskom](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/mwaskom/32/1598_2.png) [@mwaskom](https://discourse.psychopy.org/u/mwaskom)\
**Post date:** [August 22, 2016, 12:50pm UTC](https://discourse.psychopy.org/t/elementarray-of-gratings-with-non-zero-pedestal-value/904/9 "2016-08-22T12:50:32Z")

</div>

Thanks Jon. I’ll mark this as solved as everything seems to be working smoothly for now, but I am hopeful that this feature will make its way (back) into PsychoPy core at some.point.
