Partial randomization of trial sequence

PennController for IBEX Forums Support Partial randomization of trial sequence

This topic contains 4 replies, has 2 voices, and was last updated by Avatar asdf 11 months, 2 weeks ago.

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #5132
    Avatar
    asdf
    Participant

    Hi Jeremy,

    I’m wondering if it’s possible to set up partial randomization of trial sequence. That is, I have two classes of trials A and B, and I want one trial of Class B to appear after every two trials of Class A. However, I still want to keep the sampling of trials randomized. Is there any possible way to implement this?

    Best,

    #5134
    Jeremy
    Jeremy
    Keymaster

    Hi,

    What you are asking for is a little advanced, so I wrote a snippet for it. Add a .js file to your Controllers (js_includes) folder containing this code:

    let subsequence;
    let repeat;
    
    (function (){
        function Times(n,predicate){
            this.predicate = predicate;
            this.n = n;
        }
        
        repeat = (predicate,n) => {
            if (n<=0) return predicate;
            else return new Times(n,predicate);
        };
        
        function Subsequence(...predicates) {
            this.args = [];
            this.times = [];
            for (let i = 0; i < predicates.length; i++){
                let predicate = predicates[i];
                if (predicate instanceof Times){
                    this.args.push(predicate.predicate);
                    this.times.push(predicate.n);
                }
                else{
                    this.args.push(predicate);
                    this.times.push(1);
                }
            }
        
            this.run = function(arrays) {
                let remainingPredicates = arrays.length;
                let newArray = [];
            
                while (remainingPredicates > 0){
                    for (let n = 0; n < arrays.length; n++){
                        let predicate = arrays[n];
                        for (let times = 0; times < this.times[n] && predicate.length > 0; times++)
                            newArray.push( predicate.pop() );
                        if (predicate.length===0) remainingPredicates--;
                    }
                }
                return newArray;
            };
        }
        
        subsequence = (...predicates) => new Subsequence(...predicates);
    }());

    Then you can use it like this in the Sequence command:

    Sequence( "intro" , subsequence( repeat(randomize("main"),2) ,"sep" ) , "end" )

    The command subsequence will return a series of interleaved trials with corresponding labels, exhausting them all, so if you simply had subsequence("main","sep") you would get a series of [main,sep,main,sep,main,sep,...] trials containing all the main and sep trials.

    You can use the repeat command inside the subsequence command to control how frequently your interleave things, as illustrated above. You could even do something like this: subsequence( repeat("main",2) , repeat("sep",2) ) and you would get series of 2 main trials followed by 2 sep trials (ordered as defined in your script, since there’s no randomize command here).

    Note that if you have more trials than necessary, they will be appended at the end of the subsequence: say you have 41 main trials and 22 sep trials, then subsequence( repeat(randomize("main"),2) ,"sep" ) will give you a series of 20 times [2 main trials followed by 1 sep trial] and then 1 remaining main trial followed by 2 remaining sep trials.

    Jeremy

    #5140
    Avatar
    asdf
    Participant

    Hi,

    I’ve added a .js file named as “PartialRandom.js” to controllers folder, and also modified the Sequence command to:
    Sequence("intro", subsequence( repeat(randomize("a_trials"),2) ,randomize("b_trials") ), "exit")
    The “a_trials” and “b_trials” are implemented using Templates separately.

    However, when I test the experiment, an error message will pop up: You must define the ‘items’ variable.

    Is it because I need to refer to the .js file that I’ve added to the controller fold somewhere in my code?

    #5141
    Jeremy
    Jeremy
    Keymaster

    Ugh, the forums mishandled the rendering of a < symbol, which I missed when replacing them with HTML entities, so you got < = on line 8 instead of <=

    I edited my message to fix the code, just update the content of your PartialRandom.js file with it and it should work

    Sorry about that

    Jeremy

    #5142
    Avatar
    asdf
    Participant

    It works now! Thank you so much Jeremy!

Viewing 5 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic.