Howdy, Stranger!

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

Supported by

[open] Break from a loop item

edited April 2016 in OpenSesame

I have a block_loop item containing 800 cycles/trials.

I want the practice_loop to stop after 100 trials and then continue with the experimental phase.
Is it possible to break out of a loop item in an inline_script? I see that OS 0.27 has a "break if" field in the loop item, but the experiment should be backwards compatible with 0.26

Thanks in advance!

Comments

  • edited 10:05AM

    Hi Daniel,

    Yes, this is possible in 0.26, but it needs some hacking! And, to state the obvious, for anyone who is running 0.27, the Break if option of the loop item is to be preferred over the method described below.

    What you can do is replace the run() function of the trial_sequence by some dummy function. After you do this, the trial_sequence is effectively disabled and you will therefore break out of the loop.

    In the example below, we assume that you want to break the loop when a spacebar response has been collected, but you can adjust this criterion as you see fit, of course.

    # Create a dummy function which does nothing
    def dummy():
        pass
    
    # If space has been pressed, replace the run() function of trial_sequence by
    # the dummy function. Essentially, this prevents the sequence from being executed
    # again. The original run() function is stored as _run(), so that we can restore
    # it later on.
    if self.get('response') == 'space':
        item = exp.items['trial_sequence']
        item._run = item.run
        item.run = dummy

    Remember to restore the trial_sequence after the loop! Otherwise subsequent loops will not run either.

    # Restore the original run() function of trial_sequence
    item = exp.items['trial_sequence']
    item.run = item._run

    Another discussion that is related to your question is here:

    Hope this helps!

    Cheers,
    Sebastiaan

  • edited 10:05AM

    Thanks, I'm going to try that

  • edited 10:05AM

    Ok, I followed the instructions above, but after pressing space it takes the experiment about 10-15 seconds before it shows the slide "practice is finished". Until that moment, the feedback slide of the practice block is 'frozen' on the screen. Is this because it is running the 800 trials anyway, but just calls the dummy() function at each iteration?

  • edited 10:05AM

    Ah, right, probably there's lot's of stuff going on in the prepare phase as well... So let's apply the same trick there:

    # Create a dummy function which does nothing
    def dummy():
        pass
    
    # If space has been pressed, replace the run() function of trial_sequence by
    # the dummy function. Essentially, this prevents the sequence from being executed
    # again. The original run() function is stored as _run(), so that we can restore
    # it later on. The same for the prepare function
    if self.get('response') == 'space':
        item = exp.items['trial_sequence']
        item._run = item.run
        item.run = dummy
        item._prepare = item.prepare
        item.prepare = dummy

    and to restore:

    # Restore the original run() function of trial_sequence
    item = exp.items['trial_sequence']
    item.run = item._run
    item.prepare = item._prepare

    Does that fix it?

  • edited October 2012

    Almost, using this method I get the error

    Error: Runtime error
    Description: Failed to prepare item 'trial_sequence', which is called by loop item 'block_loop'
    

    Right after the feedback display at the end of the practice phase. I'll have a look into this later, but I understand what you're trying to do here.

    thanks again!

  • edited 10:05AM

    Ow, right. You need to tell OpenSesame that everything went ok, by returning True:

    # Create a dummy function which does nothing
    def dummy():
        return True

    Sorry! Should have checked first.

  • edited 10:05AM

    Hi,

    I'm having the same problem: can you tell me, please, what I should write in "break if" (OpenSesame 2.6.9) in the case, for instance, I would like to run just 5 trials instead of the whole cycle?

    Many thanks!
    Mario

    ps: OpenSesame is really great!

  • edited 10:05AM

    Hi Mario,

    This should do the trick (count starts at 0):

    [count_trial_sequence] = 4
    

    Also, you'll need to set count_trial_sequence to 0 at the start of the experiment. Otherwise, this break-if statement will fail the first time, when trial_sequence hasn't been executed yet, and count_trial_sequence thus doesn't exist. A simple inline_script will do the trick:

    exp.set('count_trial_sequence', 0)
    

    Cheers!
    Sebastiaan

  • edited April 2016

    Hi !

    I'm new at using open sesame, and i'd like to stop the practice-loop after 5 trials too.
    I followed all your instructions (july, 2015), but it still doesn't work, and they tell me that the variable 'practice' doesn't exist...
    please help !

    Chloé

  • edited April 2016

    Hi @choec

    In recent versions of Opensesme the syntax has slightly changed so instead of using the 'exp.set' command we now use 'var.' and then the name of your variable. so instead of

    exp.set('count_trial_sequence', 0)

    it now becomes

    would be var.count_trial_sequence = 0

    all the rest should be same though, I hope it helps

    Chris

  • edited 10:05AM

    Hi Chloec,

    Chris is right in that the modern syntax to access variables is through the var object, as described here:

    However, exp.set() still works (although it's deprecated), so I don't see how this explains your problem. You simply seem to be referring to a non-existent variable (practice). Could you provide full error message, and more details about what exactly you're trying to do?

    Cheers,
    Sebastiaan

  • edited April 2016

    In fact, when i try [count_trial_sequence] > 4 and [practice] = yes, in the brea-if field of the practice loop,they tell me that the variable practice doesn't exist.
    When i just write [count_trial_sequence] > 4, the practice loop keeps running.

    I'm just trying to have a few trials (4, or 20 it doesn't matter) for the practice block, and then continue with the full experimental loop.

  • edited 10:05AM

    Hi Chloe,

    It looks like you need to declare the variable practice first. The most logical place to do this would be in the practice_loop item. But, if you don't mind me asking, is there a specific reason you want to break the loop with scripting? Why not obtain this within the block_loop item by adjusting the amount of cycles and repetitions?

    Best,
    Laurent

  • edited 10:05AM

    I add the same block loop/ trial sequence for both practice and experiment. If i write a break if statement for the block loop, it would break out after 20 trials in the experiment too.
    Finally, i found a solution myself, i made practice and experiment independent, without using the same block sequence, and added the break if statement in the block loop of the practice loop, in sure that it wouldn't be applied to the experimental loop. And everything works as expected.

    Thank you :)

  • edited 10:05AM

    Ah, that makes sense why. Glad that you figured it out!

Sign In or Register to comment.