Trying to shuffle and combine stimuli, can you let me know if I'm on the right track?

I have two groups of stimuli, lets say S1-S5 and L1-L5.

I’d like to randomly pair the five stimuli in each group with these five durations [2, 2.5, 3, 3.5, 4].

I think I’m making progress, but get Pavlovia freezing. I just want to see if I am completely offbase in my approach.


shuffle = util.shuffle;

zip = function zip() {
    var args = [].slice.call(arguments);
    var shortest = args.length==0 ? [] : args.reduce(function(a,b){
        return a.length<b.length ? a : b
    });

    return shortest.map(function(_,i){
        return args.map(function(array){return array[i]})
    });
}

#Create arrays for the two different sets of durations
SDurations = [2, 2.5, 3, 3.5, 4]
LDurations = [2, 2.5, 3, 3.5, 4]

#Create arrays for the two different sets of stimuli
SPrompts = ["S1", "S2", "S3", "S4", "S5"]
LPrompts = ["L1", "L2", "L3", "L4", "L5"]

#Shuffle Them
shuffle(SDurations)
shuffle(LDurations)
shuffle(SPrompts)
shuffle(LPrompts)

#Put the stim and durations together (now should be two columns of five rows each)
Small = list(zip(SPrompts, SDurations))
Large = list(zip(LPrompts, LDurations))

#Put the two types of stimuli together (now should be two columns of ten rows each)
TrialInfo = Small.concat(Large)

#Shuffle all of the trials
shuffle(TrialInfo)

Then to refer to stimuli or duration in the actual trial I believe I should be able to do this?

$TrialInfo[0][trials.thisN] #for stim
$TrialInfo[1][trials.thisN] #for duration

Hi @David_Sidhu,

  1. The “comment” character for JS is //, not #.
  2. You don’t need list(). In fact, it’s not even a JS function :slight_smile:
  3. You should use $TrialInfo[trials.thisN][0] for the stimulus and $TrialInfo[trials.thisN][1] for the duration.

With this, I think it should work. What I usually do to debug my JS code, is using jsfiddle.net. There you can run your script and print its results to the console by using console.log(). This way, you don’t have to recompile a PsychoJS script any time you change something.

Have you looked at my independent randomisation demo?

Thank you! However, it still is sticking on “Initialising Experiment”.

With what error when you look at the developer console?

https://psychopy.org/online/psychoJSCodingDebugging.html

Ah! Thank you for the link!

I am getting this error:

Uncaught ReferenceError: zip is not defined
    at Time Study Draft 2.js:23:5

However, I have code running before the experiment which should define it?

zip = function zip() {
    var args = [].slice.call(arguments);
    var shortest = args.length==0 ? [] : args.reduce(function(a,b){
        return a.length<b.length ? a : b
    });

    return shortest.map(function(_,i){
        return args.map(function(array){return array[i]})
    });
}

This is how that looks in the builder:

Copy-pasting this code to “Begin experiment” should solve this.

1 Like

The syntax in Before Experiment is:

function zip(x) {

In Begin Experiment the syntax is

zip = function (x) {

1 Like

Thank you both @ajus and @wakecarter, it is working nicely now!