Hello,

I am trying to generate and present the image of a grating embedded in noise using a custom code element in Psychopy builder and keep getting the following error message, even though the code works fine in python. Importantly, the final goal is to run this online on Pavlovia, but I realize there are some limitations using numpy functions in JS - would it be possible in principle to use a version of the code below?

This is what I have so far:

```
from psychopy import core, visual, event
import numpy as np
import math
f_spat=1
imSize=10
m_distance=0.57
m_width=0.408
m_xResolution=1366
background=0
contrast_grating=0.1
contrast_noise=0.3
sd_smooth=0.05
phase=0.6412
theta=45
cycles = f_spat * imSize;
pi=math.pi;
def deg2pix(imSize, m_distance, m_width, m_xResolution):
sizePix = math.tan(imSize*(pi/180))*m_distance*(m_xResolution/m_width);
return sizePix
imSize_px = round(deg2pix(imSize,m_distance, m_width, m_xResolution));
X = np.arange(1,imSize_px+1);
X0 = (X / len(X)) - .5;
[Xm, Ym] = np.meshgrid(X0, X0);
phaseRad = (phase * 2* pi);
thetaRad = (theta / 360) * 2*pi;
Xt = Xm * np.cos(thetaRad);
Yt = Ym * np.sin(thetaRad);
XYt = Xt + Yt;
XYf = XYt * cycles * 2*pi;
grating = np.sin( XYf + phaseRad);
grating = grating*contrast_grating;
grating = grating * 127.5 + background;
sd_smooth_px = round(deg2pix(sd_smooth,m_distance, m_width, m_xResolution));
noisepatch = np.random.rand(imSize_px,imSize_px);
X = np.arange(1,imSize_px+1);
X0 = (X / imSize_px) - .5;
[x_kernel, y_kernel] = np.meshgrid(X0, X0);
s = sd_smooth_px /len(X);
kernel = np.exp((-(x_kernel**2 + y_kernel**2)/(2*s**2))/(2*pi*s**2));
ftkernel = np.fft.fft2(kernel);
ftnoise = np.fft.fft2(noisepatch);
ftfilterednoise = ftkernel*ftnoise;
filterednoise = np.real(np.fft.ifft2(ftfilterednoise));
filterednoise = (filterednoise - filterednoise.min()) / (filterednoise.max()-filterednoise.min()) * 2 - 1;
filterednoise = filterednoise * 255 * contrast_noise;
im = grating + filterednoise;
b=1;
a=-1;
im2=((b-a)/(im-im.min())/im.max() - im.min())+a
d = np.sqrt(Xm**2+Ym**2);
im2[d>0.5] = background;
im2=np.ascontiguousarray(im2,dtype='int16') #just in case!
nImages = 1
win = visual.Window(size=[720,480])
for thisIm in range(nImages):
win.flip()
visual.ImageStim(win,im2)
```

The error message is:

71.0484 ERROR numpy arrays used as textures should be in the range -1(black):1(white)

71.0771 ERROR numpy arrays used as textures should be in the range -1(black):1(white)

71.0858 ERROR numpy arrays used as textures should be in the range -1(black):1(white)

71.0891 ERROR numpy arrays used as textures should be in the range -1(black):1(white)

162: RuntimeWarning: divide by zero encountered in true_divide

im2=((b-a)/(im-im.min())/im.max() - im.min())+a

I added the code to rescale the array to -1:1 after getting this same error message without that step, however it doesn’t seem to resolve the issue. Prior to that, I set the RGB color map to rgb255, also to no avail.

The divide “by zero encountered in true_divide” in the rescaling line is also puzzling, as there is no error message when I run this in python.

I would be very grateful if anyone has any ideas or suggestions!