Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Supported by

Monkey-patching for more flexible randomisation in OpenSesame

edited June 2015 in OpenSesame

Hi,

I'm posting this for two contradictory purposes:

  1. In case anyone finds it useful in the future
  2. In case there is a better way to do this, so I don't do it again!

There is a table (Loop object) where the first 32 rows are experimental trials, and the next 96 are filler. We want to initially run 3 filler trials, then an experimental trial, then 2, 3 or 4 fillers before each remaining experimental trial. So if F represents filler and X experimental trial, the sequence will look something like this:

FFFXFFXFFXFFFFXFFFXFFFFX ....

My idea was to replace the run() method in the relevant Loop item, and use this to randomise the trials as we require. This method is an adapted version of the run() in the original code (module libopensesame.loop). My code to perform the replacement was:

https://gist.github.com/anonymous/66b01a1b3deb0db6dfcc
(I will also copy this code into a reply comment on this post, for posterity)

This was placed in the Prepare phase of an inline code item.

I'd be interested to know what other OpenSesame users (and the developers!) think of this.

Regards,

Alisdair

Comments

  • edited 10:22AM
    # Replace run() for loop object with our own methods
    import random
    import openexp
    import types
    
    # Modified copy of original run() method
    def new_MyLoop_run(self):
        self.set_item_onset()
    
        # generate cycle numbers (custom)
        # come up with an order of IDs from the table
        # First 32 are experiment, rest are filler
        # 2*11, 3*10, 4*10 plus 1*3 at start = 32 filler sequences
        n_filler_seq = [2]*11 + [3]*10 + [4]*10
        random.shuffle(n_filler_seq)
        n_filler_seq = [3] + n_filler_seq
        test_ids = range(0,32)
        filler_ids = range(32,128)
        l = []
        for n_filler in n_filler_seq:
            for _ in range(n_filler):
                l.append(filler_ids.pop(random.randrange(len(filler_ids))))
            l.append(test_ids.pop(random.randrange(len(test_ids))))
    
        # Create a keyboard to flush responses between cycles
        self._keyboard = openexp.keyboard.keyboard(self.experiment)
    
        # Make sure the item to run exists
        if self.item not in self.experiment.items:
            raise osexception( \
                u"Could not find item '%s', which is called by loop item '%s'" \
                % (self.item, self.name))
    
        # And run!
        _item = self.experiment.items[self.item]
        while len(l) > 0:
            cycle = l.pop(0)
            self.apply_cycle(cycle)
            self.experiment.set(u'repeat_cycle', 0)
            _item.prepare()
            _item.run()
    
    test_obj = exp.items["MyLoop"]
    test_obj.run = types.MethodType(new_MyLoop_run, test_obj)
    
Sign In or Register to comment.