psychopy.org | Reference | Downloads | Github

Itertools alternative - python to JS

Hi All,

I’m trying to pare back an existing psychopy program to remove code so it will run online. The code is from the original IAT program that was written many moons ago for version 1.8x

The program has the following JS code that needs fixing - I have two questions to help try and point me in the right direction.

  1. The issue I think is with the itertools.chain function. It’s trying to flatten a list of lists. Is there an off the shelf way to do this? Or am I approaching this in the wrong way?
  2. Where is the code getting the ‘multiplier’ variable from? When I view the full code I don’t see it initialised anywhere.

Any pointers would be great. Happy to elaborate more if someone thinks they can help.

Justin.

var block_order, exemplars, exemplars_filename, list_multiplier, n_exemplars, participantNumber, trial_rows;
exemplars_filename = "stimuli.xlsx";
exemplars = data.importConditions(exemplars_filename);
n_exemplars = 5;
list_multiplier = 2;
function generate_trials(trial_type_column, multiplier) {

    var a;
    a = dict();
    for (var i = 0, _pj_a = exemplars.length; (i < _pj_a); i += 1) {
        a[i] = ([exemplars[i][trial_type_column]] * multiplier);
    }
    a = a.values();
    a = list(itertools.chain(...a));
    random.shuffle(a);
    return a;
}
trial_rows = "";
participantNumber = 1;
block_order = 1;22

(I’ve added ``` around your code so it’ readable)

I don’t know what the itertools.chain is doing so not sure what to suggest. There is a working version of IAT already online if you search on pavlovia. Maybe it’s easier to tweak that than to fix this version?

1 Like

Hi Justin, not sure about the ‘multiplier’ arg, but there may be some hint in the original python code. For flattening your nested array, you can use:

a = a.flat(Infinity)

See Array docs, with also the list of compatible browsers. Although, as Jon says, you can find a working version of the IAT on Pavlovia

1 Like