Alternative to numpy.choice for pavlovia

URL of experiment: https://gitlab.pavlovia.org/PsychologyBath/tilda

Description of the problem: I can’t get the experiment to initialise, I’m pretty sure this is because I use numpy.choice I’m not sure how I can change this to JS? I’ve used numpy.choice based on reading other forum posts and trial and error (rather than any in depth knowledge of python!)
Is there an easy solution or do I need to find a different way to structure my experiment?

Thanks

Hi,

You could either try to shuffle the list (see crib sheet for JS implementation) then index the first element of the list.

Or you could try something like this Pavlovia: How to implement the range function? - #6 by Becca

Hope this helps,
Becca

Thanks Becca

I’ll have a go, I tried changing my code before but I got stuck on what i’d need to type into trials selected rows to replace this: $choice(32, size=1, replace=False) , any tips?

Ah yes, for easier transition online I would actually recommend adding a code component to a routine in advance of the loop and using something like

theseRows=choice...

Then use “theseRows” as the variable for your selected rows field.

Becca

I’m really sorry, i’m still struggling i’ve now put this into the code section

but now i’m getting the below error and i’m not sure where to start

I’m presuming I haven’t defined something properly? it’s still working offline, so presumably it’s a JS error again? I’ve tried changing choose for choice in the JS code but then Ihad this error " * ReferenceError: choice is not defined"

theseRows = choose(32, {“size”: 1, “replace”: false});

Hi There,

OK so this choose method takes a list rather than named arguments. Please can you check what happens if you use

theseRows = choose([1, 2, 3])
console.log('the selected item was', theseRows)

Then when you are online, try opening the developer tools (see crib sheet if unsure there PsychoPy Python to Javascript crib sheet) to see if you get the expected printout and check your choose function is working.

Becca

Hi Becky

feeling very much out of my depth, I can’t see it printing ‘the select item was’ anywhere, and I get a lot of errors with the same message as before:

Hi There,

OK you might need to chare your project URL with us. But before we do that, Can I share this project where I use the choose function and call it later on Pavlovia

you can click “View code<>” to download the files and take a look “under the hood” this might make it more concrete how to use these custom functions and implement them in your task!

Hope this helps,
Becca

Thanks for all your help with this, i’ll take a look at your code and see if I can decipher what to do.

I’ve set mine to public and this is the URL is that all you need? Pavlovia

okay so I solved the error message (as predicted I had empty columns in a conds file). But I can’t get the experiment to only choose one of my rows from the conds file to run. I’ve looked at your code but the only bit I can see to do with choices is the bit below, and I have that in mine, I just don’t know how to reference it in my trials selected rows bit

function choose(choices) {
var index = Math.floor(Math.random() * choices.length);
return choices[index];
}

So I think i’ve solved it! Thanks for your help and patience :slight_smile:

I want to do a bit more testing, but when i’m happy i’ll update on the changes I made incase it helps anybody else.

edit:
So I solved the first issue by cutting out numpy, but then I had to find an alternative. I’ve ended up using some code in the trial routine based on the help on this post Dynamically reference the current loop - #8 by RobM except I put mine into the start and end of the routine, rather than start of the experiment.
I also discovered I had errors in my conditions files which was causing issues.
It all seems to work currently, although i’ll keep doing a bit more testing to ensure it’s randomised how I expected.
Thanks for the tips and links, it really helped knowing someone else was on hand!

1 Like