Issue with translating numpy function into javascript

Dear All –

I am having a problem translating some python code that works fine on my computer running psychopy Builder but does not seem to work on Pavlovia. What I am trying to do is use row numbers provided by an outer loop (from an excel file) to control the row numbers accessed in an excel file in an inner loop. In essence, with each iteration of the outer loop, its excel file provides a startRow and an endRow as variables (integers), and I want to use these rows to create a list of rows to loop through in the inner loop. For example, if the outer loop provides variables startRow = 3 and endRow = 7, I want to put into the “Selected Rows” text box of my inner loop a variable that has the numbers 3,4,5,6,7 (I realise that the first row in Python is always row number 0).

What I have done using a Python code component on my computer far is a code step that creates a variable:

listOfRows = np.arange(startRow, endRow + 1).tolist ()

This works on my machine, but not on Pavlovia. the problem seems to be that javascript does not have numpy library or the arange function.

Is there a workaround that will provide these row numbers for javascript?

Thanks in advance,

Harry

You can write your own JS function, something like:

Begin Experiment:

function range(start, end) {
    var ans = [];
    for (let i = start; i <= end; i++) {
        ans.push(i);
    }
    return ans;
}

And then just call it when you need it:

listOfRows = range(startRow, endRow + 1);

Hi, thank you for your reply.

After replacing the functions, I get this error (see below)

Here’s a link to the code:

Thanks for this really quick response. Just to double check (because I don’t know JS and I have no way to test it), you state:

listOfRows = range(startRow, endRow + 1);

I would have thought that this call would provide a list that was one number longer than I wanted, given the <= sign

function range(start, end) {
    var ans = [];
    for (let i = start; i <= end; i++) {
        ans.push(i);
    }
    return ans;
}

Any help greatly appreciated.

Kind regards,

Harry

Dear @harry_bsms looking at the repo you link to, moving the range function declaration in the global scope, outside of experimentInit() should work. You might run into issues with calling message.count(" ") after that though. Is that also a NumPy helper you need access to? Is it meant to count empty messages?

Thank you for your reply.

I’m using a function that creates a list of numbers that represent the rows of an excel spreadsheet that are arguments for a loop in psychopy.

function range(start, end) {
    var ans = [];
    for (let i = start; i <= end; i++) {
        ans.push(i);
    }
    return ans;
}

I’m working in builder, putting the function in a code routine in the tab labelled “beginning of experiment”, yet the function is still not recognised. I have also tried “end of experiment”. The only time where I have avoided the error message is when I manually edited the experiment.js file in the html folder (locally) and inserted the function around line 2069 (in between function importconditions and function quitpsychojs), see:

Why is the function not being recognised? If I place it manually in the local experiment.js file, it is overwritten when I synchronise with pavlovia so makes it very difficult to debug the experiment.

In 2020.2 we added a new option to the Code Component to add code “Before Experiment” and that was mostly to allow the loading/defining of JS functions earlier in script, outside any functions scopes. I think that should address the issue you have.