psychopy.org | Reference | Downloads | Github

Randomization not working online (problem with "map"?)

URL of experiment: testMT [PsychoPy]

Hello (again),
I’m trying to move a mouse-tracking experiment online, which works locally. I’ve been having multiple error messages (thanks already to those who solved my previous issues) but now the latest error message is pretty obscure and I think it’s more convenient if I show you the whole code.
At the moment when I pilot the experiment, I get an error message after the dialog box:
Uncaught SyntaxError: Unexpected identifier

First, a capture of the flow:

The first code sets up the cursor at the center of the screen for each trial (I found that on the forum, not sure if it works for me).

#Begin routine
const position = mouse.getPos();
  mouse.x.push(position[0]);
  mouse.y.push(position[0]);
  mouse.time.push(mouse.mouseClock.getTime());

Then the next bit randomizes the position of the two text components (for 10 trials) so that the sides are 50/50 across trials.

#Begin experiment
target_positions = [0.4, -0.4].map((val) => val * 5);
util.shuffle(target_positions);
#Begin routine
target_x = target_positions.pop();
competitor_x = target_x.map(function(x) x * -1);
psychoJS.experiment.addData("target_x", target_x);
psychoJS.experiment.addData("competitor_x", competitor_x);

And in the text components I’ve set the positions in Builder to [target_x, 0] for targets and [competitor_x, 0] for competitors.

There was a time in a previous version where the experiment started, the first audio file was playing, but no text component. That’s when I changed the way I multiply competitor positions using “map”, and now I have the “unexpected identifier” error. So the problem could be there. But it’s also very possible that there are other problems elsewhere, so any suggestion is very welcome.

Many thanks!

Are you trying to move the mouse cursor? If so that’s not possible in JS.

https://github.com/psychopy/psychojs/issues/35

However, there could be a workaround of hiding the mouse cursor and replacing it with an image of the cursor – for which you can customise the position. However, you’d also need invisible objects for anything you want this mouse to interact with. I’ve done this once offline because I wanted to rotate how the mouse works (e.g. moving the mouse to the right moved the cursor down and right) but I’ve not tried it online.

Best wishes,

Wakefield

Hi Wakefield, thanks for your answer!
Good to know that moving cursor is not possible, I’ll find another way.

What I really need is to randomize the position of the two text components, and that’s where there seems to be issues with the code, probably with these lines:
target_positions = [0.4, -0.4].map((val) => val * 5);
util.shuffle(target_positions);
target_x = target_positions.pop();
competitor_x = target_x.map(function(x) x * -1);

Now I’ve also tried a version of the experiment without any custom code, just fixed position coordinates, and what I got was the two words showed up on the screen, the audio was playing, I could see the cursor but it didn’t end the trial when I clicked on a word. Is “end routine on valid click” not possible online for text stimuli?

Scroll to the bottom of my crib sheet to confirm that text objects can’t have the contains method. I’ve used two different workarounds.

  1. Add a clickable rectangle behind the text.

  2. If the text doesn’t change, use images instead.

My dot probe task randomises the position of two stimuli using a shuffle function and has clickable images of 1 and 2 (which highlight for a few hundred milliseconds when clicked).

Thanks! I’ve downloaded your dot probe experiment and I’ll try to see how I can use the same method. You don’t happen to have like a “read me” document by any chance? I’m not sure I understand how you used ‘conditions’ and ‘scale’ to randomize the positions.

Scale is a constant in case I want to change the sizes.

Conditions are 1 and 0 (or possibly 1 and -1) and are used as a multiplier in the positions of the stimuli.

BTW it’s coded for a touch screen

Hi Wakefield,
I’ve had a hard look at your experiment and I think I understand it a little better now, but when I tried to use your method, I now get the sound playing online but no text showing at all. This is what I’ve done:

#Begin experiment
target_positions = [1, -1].map((val) => val * 5); #that creates 10 sets for 10 stimuli where position is either multiplied by 1 or -1
util.shuffle(target_positions);
scale = 0.4; #I want my positions (x) to be either 0.4 or -0.4

Then to set the position of my text components I use this, to “set every repeat” :

$(target_positions[trials.thisN][0]*scale, 0) #for targets
$(-target_positions[trials.thisN][0]*scale, 0) #for competitors, so the opposite

Then I set the position of the polygons to follow that of the text components:

[target_text.x, 0]
[compet_text.x, 0]

It runs, but nothing appears on the screen. Can you spot what I did wrong? Thanks a lot!

I would create variable target_x in the code and use this variable for both the text and polygon positions (-target_x for the competitor)

Thanks, I’ve got it working now! For posterity:

#Begin experiment
target_positions = [[1, (-1)],[1, (-1)],[1, (-1)],[1, (-1)],[1, (-1)],[(-1), 1],[(-1), 1],[(-1), 1],[(-1), 1],[(-1), 1]];
util.shuffle(target_positions);
scale = 0.4;
#Begin routine
psychoJS.experiment.addData("target_x", target_positions[trials.thisN][0]);
psychoJS.experiment.addData("competitor_x", -target_positions[trials.thisN][0]);
#Each frame
const position = mouse.getPos(); #saves mouse coordinates and time stamps on each frame
  mouse.x.push(position[0]);
  mouse.y.push(position[1]);
  mouse.time.push(mouse.mouseClock.getTime());

polygon_target position in the component (set every repeat): $(target_positions[trials.thisN][0]*scale, 0)
polygon_compet position in the component (set every repeat): $(-target_positions[trials.thisN][0]*scale, 0)

target_text position in the component (set every repeat): $(target_positions[trials.thisN][0]*scale, 0)
compet_text position in the component (set every repeat): $(-target_positions[trials.thisN][0]*scale, 0)

Clickable stimuli for the mouse component: polygon_target, polygon_compet

I hope it can help others :slight_smile: