Error message: asarray not defined

OS (e.g. Win10): Win10
PsychoPy version (e.g. 1.84.x): 2021.1
Standard Standalone? (y/n) If not then what?: yes
**What are you trying to achieve?:
I built an experiment in the builder, which works fine. As soon as I switch to pavlovia, I get an error message: asarray not defined. I checked the compiled python code and found that two variable settings are executed with an asarray function:


**What did you try to make it work?:
So far, I checked whether my condition file provides values for these variables in all trials, but that looks fine so far.

Did anyone run into the same problem and could help me here?

Thanks a lot in advance!



Hi Kathrin,

I’ve had the same problem. I think it’s to do with the fact that asarray is defined in the Python version by importing numpy, but this does not happen when it converts to JS, so in JS asarray becomes ‘undefined’. Indeed, the cribsheet advises there will be problems converting from Psychopy to JS if numpy is imported.

I’m trying to find a way to vary the duration of a stimulus without relying on ‘asarray’, or changing the JS version of the script so it doesn’t rely on ‘asarray’ but unfortunately I don’t have a solution yet at the moment so would appreciate any help :slight_smile: thanks


numpy.asarrary is a bit tricky, since it can perform a lot of different kinds of conversions. I would recommend writing a special solution for your cases.

setOri in PsychoJS takes a single number: JSDoc: Source: visual/VisualStim.js

So what you could try is, first write the value you’re trying to convert to the log to see what it looks like (see, then pluck out the right value.

Best, Thomas

Thank you so much! I don’t quite understand how this can be used to add a variable duration for a stimulus in Pavlovia?

this isn’t numpy.asarray. As the question said, the js code just spontaneously started using asarray during translation. I had the same problem and I solved it by attaching a function called asarray that does absolutely nothing and returns the exact same input as the output at the beginning of the experiment.

it looks like.
asarray = function (something) {
return something;

This will be useful until the the psychopy team fixes this problem.


Thank you, good idea! I’m just trying to implement it now, putting a code at the beginning of the experiment. However, it won’t translate to Pavlovia (specifically, when I try to sync, I get this error:

** Traceback (most recent call last):
File “C:\Program Files\PsychoPy3\lib\site-packages\psychopy\app\builder\”, line 1339, in onPavloviaRun
File “C:\Program Files\PsychoPy3\lib\site-packages\psychopy\app\builder\”, line 759, in fileExport
File “C:\Program Files\PsychoPy3\lib\site-packages\psychopy\scripts\”, line 73, in generateScript
compileScript(infile=exp, version=None, outfile=filename)
File “C:\Program Files\PsychoPy3\lib\site-packages\psychopy\scripts\”, line 242, in compileScript
_makeTarget(thisExp, outfile, targetOutput)
File “C:\Program Files\PsychoPy3\lib\site-packages\psychopy\scripts\”, line 214, in makeTarget
script = thisExp.writeScript(outfile, target=targetOutput, modular=True)
File “C:\Program Files\PsychoPy3\lib\site-packages\psychopy\”, line 242, in writeScript
File “C:\Program Files\PsychoPy3\lib\site-packages\psychopy\experiment\”, line 140, in writeInitCodeJS
File "C:\Program Files\PsychoPy3\lib\site-packages\psychopy\experiment\components\code_init
.py", line 221, in writeInitCodeJS
if len(str(self.params[‘Begin JS Experiment’])) and not self.params[‘disabled’]:
TypeError: str returned non-string (type NoneType)

Do you know why this might be?

Thank you so much :slight_smile:

Hey @verdi,

Could you share your gitlab with me (tpronk), then I’ll have a look.

Best, Thomas


In code_JS this should be written as

asarray=function(something) {
return something;

Please let me know if this solves the issue.

Unfortunately it doesn’t solve the issue. Please see the screenshot below for my code piece. The unused ‘rcccc’ variable was to check whether there was a general problem with Code_JS, but there wasn’t, as it worked when I included this alone without adding the ‘asarray’ function.

Thanks for taking the time to look at this :slight_smile:

Verdi, I’d like to pick up your problem as a little exercise into understanding parts of PsychoPy that are outside of my specialism. If that’s OK with you, then could you share the repo? Here is a guide on how to do that: Searching for experiments on Pavlovia — PsychoPy v2021.1

Sure @thomas_pronk - I’ve just added you. Please let me know how you get on or if you’d like any more explanation. Currently, ‘itiz’ is the name of the variable from a conditions file added into the ‘duration’ section of the stimulus ‘blank_between’ (representing the ITI). This works fine locally. Alternatively, if I put a fixed value, e.g. 0.5, it works fine on Pavlovia.
The experiment is SequenceTask1 [PsychoPy]
Thanks a lot for offering to help :slight_smile:

My pleasure! I’ll wait a little while to collect some more experiments that need assistance, and then I’ll dive into it

I’ll need developer access (you made me guest)

Hi everyone,

First of all, thanks for sharing your problems and solutions. I’ll defintely try to find a solution, but right now I don’t have access to my account. I guess it’s probably due to the disruptions.(>>> authentication error: Unauthorised access to a GitLab API resource)



1 Like

I think I’ve just given you developer access. Let me know if any problems :slight_smile:

I managed to get the experiment to get to the introduction slide (followed by a tone). This is what I did: in the code-component code_JS, tab “Before Experiment”, I added the code below:

let asarray=function(something) {
  return something;
1 Like

Thanks so much! Did you manage to set one of the stimuli to a variable duration?

Haven’t tried. Can you give me some more pointers on how you’d like to have that?

No worries. I just want to implement a stimulus with variable duration. This can be done in Psychopy builder by putting a variable in the ‘duration’ box, and then changing this variable in a conditions file or in code at the start of the routine. However it won’t transfer to Pavlovia because in the code of Psychopy it uses numpy, which doesn’t transfer to JS :frowning:

Aha, is that the asarray issue? If so, tried the fix above? If not, your experiment was named “sequence [something]” right?