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!

Buy Me A Coffee

Comments

  • edited 2:30AM

    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 2:30AM

    Thanks, I'm going to try that

    Buy Me A Coffee

  • edited 2:30AM

    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?

    Buy Me A Coffee

  • edited 2:30AM

    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!

    Buy Me A Coffee

  • edited 2:30AM

    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 2:30AM

    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 2:30AM

    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 2:30AM

    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 2:30AM

    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 2:30AM

    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 2:30AM

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

  • Hi, I am trying to break a training loop after a set number of trials as described in this thread. If I add the following to an inline script at the start of the experiment:

    var.count_trial_sequence = 0

    And the following to "Break if" in the loop item:

    Break if [count_trial_sequence] = 2

    The loop does not stop after 2 trials. Is count_trial_sequence something that OpenSesame just understands? Should this variable somehow be a column in my loop table? How does it know to correlate count_trial_sequence with the number of trials elapsed? I thought maybe count_ is a sort of tag that OpenSesame understands, and I could use an existing column title in my table instead of trial_sequence, likecount_sound(since I have a column named sound), but this doesn't appear to make a difference. Any advice?

  • I've solved this problem! Aside from adding the following to an inline script before the loop:

    var.count_trial_sequence = 0

    I also found I need to add the following to an inline script within the loop:

    count_trial_sequence = count_trial_sequence + 1

    Then it works as intended, breaking the loop after the designated number of trials in Break if !

Sign In or Register to comment.

agen judi bola , sportbook, casino, togel, number game, singapore, tangkas, basket, slot, poker, dominoqq, agen bola. Semua permainan bisa dimainkan hanya dengan 1 ID. minimal deposit 50.000 ,- bonus cashback hingga 10% , diskon togel hingga 66% bisa bermain di android dan IOS kapanpun dan dimana pun. poker , bandarq , aduq, domino qq , dominobet. Semua permainan bisa dimainkan hanya dengan 1 ID. minimal deposit 10.000 ,- bonus turnover 0.5% dan bonus referral 20%. Bonus - bonus yang dihadirkan bisa terbilang cukup tinggi dan memuaskan, anda hanya perlu memasang pada situs yang memberikan bursa pasaran terbaik yaitu http://45.77.173.118/ Bola168. Situs penyedia segala jenis permainan poker online kini semakin banyak ditemukan di Internet, salah satunya TahunQQ merupakan situs Agen Judi Domino66 Dan BandarQ Terpercaya yang mampu memberikan banyak provit bagi bettornya. Permainan Yang Di Sediakan Dewi365 Juga sangat banyak Dan menarik dan Peluang untuk memenangkan Taruhan Judi online ini juga sangat mudah . Mainkan Segera Taruhan Sportbook anda bersama Agen Judi Bola Bersama Dewi365 Kemenangan Anda Berapa pun akan Terbayarkan. Tersedia 9 macam permainan seru yang bisa kamu mainkan hanya di dalam 1 ID saja. Permainan seru yang tersedia seperti Poker, Domino QQ Dan juga BandarQ Online. Semuanya tersedia lengkap hanya di ABGQQ. Situs ABGQQ sangat mudah dimenangkan, kamu juga akan mendapatkan mega bonus dan setiap pemain berhak mendapatkan cashback mingguan. ABGQQ juga telah diakui sebagai Bandar Domino Online yang menjamin sistem FAIR PLAY disetiap permainan yang bisa dimainkan dengan deposit minimal hanya Rp.25.000. DEWI365 adalah Bandar Judi Bola Terpercaya & resmi dan terpercaya di indonesia. Situs judi bola ini menyediakan fasilitas bagi anda untuk dapat bermain memainkan permainan judi bola. Didalam situs ini memiliki berbagai permainan taruhan bola terlengkap seperti Sbobet, yang membuat DEWI365 menjadi situs judi bola terbaik dan terpercaya di Indonesia. Tentunya sebagai situs yang bertugas sebagai Bandar Poker Online pastinya akan berusaha untuk menjaga semua informasi dan keamanan yang terdapat di POKERQQ13. Kotakqq adalah situs Judi Poker Online Terpercayayang menyediakan 9 jenis permainan sakong online, dominoqq, domino99, bandarq, bandar ceme, aduq, poker online, bandar poker, balak66, perang baccarat, dan capsa susun. Dengan minimal deposit withdraw 15.000 Anda sudah bisa memainkan semua permaina pkv games di situs kami. Jackpot besar,Win rate tinggi, Fair play, PKV Games