Howdy, Stranger!

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

Supported by

[open] Conditional randomization

edited October 2014 in OpenSesame

Hi,

I saw that there were a couple of forums about randomization, but I either did not understand it well, or the particular question I had wasn't answered. My apologies if it is the former and this is redundant.

I have a short experiment with 16 critical items and 4 filler items. I want near-total randomization, with the condition that a participant see 1 filler item after 4 critical items. In other words, no more than 4 reps of the critical item. Is there a reasonably straightforward way to accommodate this requirement?

Thanks!
-A

Comments

  • edited October 2014

    Hi,

    If you want to have the filler item at every 5th position, you can do following.

    What you need is an inline_scriptbefore the trial loop and one inline_scriptwithin the loop (and of course all other stuff you might need for your experiment).

    First, you define three lists, one for critical, one for filler items and an empty one, which will contain all items, in an inline_script and shuffle them. Then you append all the items to the empty list by adding items from the critical list and every 5th element items form the filler list. The Modulo function will do the trick. The script could like this:

    from random import shuffle
    
    # 1) defining the lists
    critical = list(xrange(16))
    shuffle(critical)
    filler =  list(xrange(100,105))
    shuffle(filler)
    no_items = 20
    all_items = []
    
    # 2) Adding elements to the list of all items
    for i in xrange(no_items):
        if i%5 == 4:  # "%" means modulo
            all_items.append(filler.pop())
        else:
            all_items.append(critical.pop())
    
    

    Within the loop element you set a helper variable that works basically as a counter and add as many repetitions as you have items.

    Finally, you pick the item from the item list by setting the variable in the prepare phase of the inline_script, to the current value of the list:

    cur_item = all_items[exp.Var] 
    # exp.Var is the helper variable I defined in the loop. It runs from 0-19
    exp.set('cur_item',cur_item)
    

    From then on, you should be able to use the item, e.g. by [cur_item].

    This is maybe not the most efficient way, but it works. If you need more help, please let us know.

    Eduard

    Buy Me A Coffee

  • edited 10:25PM

    Hi Eduard,

    Thanks so much for this! I haven't gotten this to work though, but I suspect it is because my experiment turns out to be a bit more complicated than I first made it out to be. So each item (critical and filler) consists of a pair of pictures that must appear sequentially in a set order.

    Right now, in my list of variables, I have columns for item# (1-20, critical = 1-16, filler = 17-20), image1, and image2. For each trial, Image1 for each item gets called first, followed after about 2 secs by Image2.

    I modified your code to specify the rows that belong to the critical and filler items, and it looks like I've already messed up!


    define inline_script inline_script1
    _run
    from random import shuffle

    # 1) defining the lists
    critical = Item(xrange(0,15))
    shuffle(critical)
    filler =  Item(xrange(16,19))
    shuffle(filler)
    no_items = 20
    all_items = [] 
    

    I'm quite new to OpenSesame, so my apologies if this is too basic!

    Relatedly, I wonder about your helper variable. I won't just be able to call the item using [cur_item], since I would need to call, e.g., image1 for cur_item, followed by image2 for the same. How can I modify the code to do this?

    Thanks again!

  • edited 10:25PM

    Hi,

    Alright, if your items consist of two images each, you don't have to change much. It requires though, that your pictures are labelled according to the same scheme. For example: Picture_1a, Picture_1b, ...

    For each picture that belongs to one item, you add one sketchpad to the sequence and put one picture into it. Afterwards, you open the script view and change the name of the picture file from, let's say, "Picture_1a" to "Picture_[cur_item]a" and likewise picture 1b. By doing so, you create a placeholder that is dynamically filled with the picture of respective trial.

    The general idea is described quite detailed in the step-by-step tutorial, step 6.

    Oh, by the way, when I said you don't have to change much, I meant in comparison to the code I posted earlier. I didn't entirely understand in which way you modified your code. Sorry for that.

    Eduard

    Buy Me A Coffee

  • edited October 2014

    Hi,

    Thanks again. The latter part you said makes sense to me, but the first portion about defining the lists is still giving me trouble.

    For some reason, I am failing to execute the (pretty much exact) code you gave in your first response. The error I am getting is "IndexError: pop from empty list"

    I then try to define the lists manually (in the prepare phase of the inline script), e.g., as follows, and I continue to get the same error...

    from random import shuffle
    
    # defining the lists
    # each item is a tuple consisting of item#, image1, and image2
    
    critical =  [
        ('1', '1_pic1.png', '1_pic2.png'),
       ('2', '2_pic1.png', '2_pic2.png'),
       ('3', '3_pic1.png', '3_pic2.png'),
       ('4', '4_pic1.png', '4_pic2.png')
    
        ] 
    
    shuffle(critical)
    filler = [ 
        ('17', '17_pic1.png', '17F_pic2.png'),
       ('18', '18_pic1.png', '18F_pic2.png'),
    ]
    shuffle(filler)
    no_items = 6
    all_items = [ ]    # empty list
    
    
  • edited 10:25PM

    Did you also add the part, where you fill the list all_items? Can you also post the part where you use the pop function? If I try the code in my first response, I don't get an error, so I guess this part has to be changed a bit.

    Buy Me A Coffee

  • edited October 2014

    For this, I haven't changed your code at all. I have tried entering this in both the run phase and the prepare phase, but neither has worked.

    # 2) Adding elements to the list of all items
    for i in xrange(no_items):
        if i%5 == 4:  # "%" means modulo
            all_items.append(filler.pop())
        else:
            all_items.append(critical.pop())
    
  • edited 10:25PM

    This is weird, because it works just fine for me. Can you maybe give some more detail on the error that is shown to you? You can inspect it yourself in the Debug window.
    Mostly, I am curious in which inline script and which line the error occurs.
    What you also can do, is printing the lists and to see whether they look like you think they do (e.g. print filler, and check again the Debug Window for the results).

    Buy Me A Coffee

  • edited 10:25PM

    I got this to work (thank you!), but I have a follow up question. For each item, the correct response is either 't' or 'f', and I want to be able to log not just which key was pressed by the participant, but also compute accuracy. However, defining correct response in the variable list won't work anymore. Is there a way I can define in, e.g., the critical list, what the correct response is for each item? If yes, how do I refer to this when logging accuracy?

    Sorry if this is too basic a question. I fairly new to OS and very new to inline scripts!

  • edited 10:25PM

    Well, you can simply add another field to your list, which contains the correct response.

    E.g.

    critical =  [
        ('1', '1_pic1.png', '1_pic2.png', correct_response),
       ('2', '2_pic1.png', '2_pic2.png', correct_response),
       ('3', '3_pic1.png', '3_pic2.png', correct_response),
       ('4', '4_pic1.png', '4_pic2.png', correct_response)
        ] 
    

    From then on you have to check whether the actual response equals the correct response and keep on counting:

    # on every trial you can do 
    if [response] == critical[trial][-1]:
        exp.cor += 1
    exp.trials += 1
    exp.acc = exp.cor / exp.trials
    
    

    Please make sure that [response] is really the proper variable that records the keypress of the participants. The other variables (exp.cor, exp.trials, exp.acc) you have to define yourself. In the beginning of your script you have to intialize them first (set them to 0).

    Good luck,

    Eduard

    Buy Me A Coffee

  • edited 10:25PM

    Thank you, and sorry for the late response.

    My concern is the following. Currently, my critical and filler lists have singleton items ( ['1', '2', '3'...] ) and I call sounds and images using this (e.g., [cur_item]_pic1.png). If I change the items to tuples (e.g., ('1', 'correct_resp')), is there a way I can make reference to just the first field in the item when calling images, etc.?

    What I have tried instead of your code is defining a dictionary with the correct response, in addition to the list. Currently, my code looks as follows:

    inline_script1 prior to the experiment loop


    from random import shuffle exp.set('total_responses', 0) exp.set('total_correct', 0) # 1) defining the lists #list of experimental items critical = [ ('1'), ('2'), ('3'), ('4'), ('5'), ('6'), ('7'), ('8'), ('9'), ('10'), ('11'), ('12'), ('13'), ('14'), ('15'), ('16') ] shuffle(critical) #list, filler items filler = [ ('17F'), ('18F'), ('19F'), ('20F') ] shuffle(filler) # dictionary with critical items and the correct response (RIGHT SHIFT just used as a dummy) critical2 = { '1': 'RIGHT SHIFT', '2': 'RIGHT SHIFT', '3': 'RIGHT SHIFT', '4': 'RIGHT SHIFT', '5': 'RIGHT SHIFT', '6': 'RIGHT SHIFT', '7': 'RIGHT SHIFT', '8': 'RIGHT SHIFT', '9': 'RIGHT SHIFT', '10': 'RIGHT SHIFT', '11': 'RIGHT SHIFT', '12': 'RIGHT SHIFT', '13': 'RIGHT SHIFT', '14': 'RIGHT SHIFT', '15': 'RIGHT SHIFT', '16': 'RIGHT SHIFT' } filler2 = { dictionary w/ correct_resp for fillers } no_items = 20 all_items = [ ] # empty list # 2) Adding elements to the list of all items for i in range(no_items): if i % 5 == 4: # "%" means modulo all_items.append(filler.pop()) else: all_items.append(critical.pop()) # 4 critical, 1 filler

    inline script2 inside the experiment loop
    cur_correct is what I was hoping to be the variable noting accuracy for a given trial.

    cur_item = all_items[exp.Var] 
    # exp.Var is the helper variable I defined in the loop. It runs from 0-19
    exp.set('cur_item',cur_item)
    cur_correct = 0
    exp.set('cur_correct',cur_correct)
    

    inline script3 inside loop, after keyboard response

    if self.get('response_Answer') == critical2[cur_item]:
        cur_correct = 1
        exp.set('total_correct', self.get('total_correct')+1)
    
    exp.set('total_responses', self.get('total_responses')+1)
    

    This doesn't seem to work. The experiment runs, but in the csv file, it is clear that cur_correct is not calculated accurately. Could you tell me where I might be going wrong?

  • edited 10:25PM

    Hi

    Currently, my critical and filler lists have singleton items ( ['1', '2', '3'...] ) and I call sounds and images using this (e.g., [cur_item]_pic1.png). If I change the items to tuples (e.g., ('1', 'correct_resp')), is there a way I can make reference to just the first field in the item when calling images, etc.?

    Yes, you have to use indices. So to access an image called "1_pic1.png" and and a item list with the shape (('1',correct_resp),'2',(correct_resp),...), you simply write [cur_item][0]_pic1.png to access the first value in the list and connect it with the rest of the filename.

    I guess the reason why your script isn't working, is that there is no variable called "response_Answer" unless you have defined it yourself somewhere. The auto-logged variable that stores the key, which was pressed is called "response". Can you check whether this is already enough to fix it?

    Eduard

    Buy Me A Coffee

  • edited October 2016

    Hi,

    I have an almost identical question. I have been trying to get the code posted above to work, but I receive the error when I try to run it.

    Traceback:
      File "dist\libopensesame\inline_script.py", line 81, in prepare
      File "dist\libopensesame\python_workspace.py", line 159, in _exec
      File "<string>", line 2, in <module>
      File "dist\libopensesame\item.py", line 280, in __getattr__
    AttributeError: Var not found
    

    I have a setup where I want to run two critical (out of four critical items), then one (out of two filler items) randomly and then repeat the process with the leftover two critical and one filler item.

    As I have to access different pictures, numbers etc. during my sequence which I have specified in my loop, it is important that these can still be accessed by the sequence.

    Currently my code is as follows:
    Run part of Inline script 1 (before loop)

    from random import shuffle
    
    rom random import shuffle
    
            # 1) defining the lists
            critical = [ 
               ('3'),
               ('4'),
               ('5'),
               ('6')
             ] 
    
            shuffle(critical)
    
            #list, filler items
            filler = [ 
               ('1'),
               ('2')
            ]
            shuffle(filler)
    
            no_items = 6
            all_items = [ ]    # empty list
    
            # 2) Adding elements to the list of all items
            for i in xrange(no_items):
                if i%3 == 2:  # "%" means modulo
                    all_items.append(filler.pop())
                else:
                    all_items.append(critical.pop())
    

    Prepare part of inline script 2 (within loop)

                cur_item = all_items[exp.Var] 
                # exp.Var is the helper variable I defined in the loop. It runs from 0-19
                exp.set('cur_item',cur_item)
    

    I have also included the helper variable [cur_item] in my loop along with the numbers 1-6 as called.

    I am still new to OpenSesame, and especially to coding, so I am probably missing some small aspect somewhere.

    Thank you!

    Aurelia

  • Hi Aurelia,

    The tips in this discussion are a bit dated. As of v3.1, OpenSesame natively supports some forms of constrained randomization; that is, in the loop item, you can specify a maximum number of repetitions, or a minimum distance between repetitions.

    See also:

    If this fits your needs, it's probably much easier than doing the same thing with a script!

    Cheers,
    Sebastiaan

  • Hi Sebastian,

    Thank you for your quick response. I am currently running my experiments on version 3.0.7, as this is the version installed on the experimental computers. If I update my computer to the newest version of Opensesame, will the program be compatible to be played with the earlier version of it?
    I have a question regarding the possible constraints. If I understand the page correctly, I cannot specify that I want two of the critical items and then one fixed item but only that there should be maximally two critical item behind each other. Is there maybe some way to code giving the filler items a fixed spot?

    Thank you,
    Aurelia

  • I am currently running my experiments on version 3.0.7, as this is the version installed on the experimental computers. If I update my computer to the newest version of Opensesame, will the program be compatible to be played with the earlier version of it?

    No, I'm afraid not! You can use 'No installation required' package though.

    If I understand the page correctly, I cannot specify that I want two of the critical items and then one fixed item but only that there should be maximally two critical item behind each other. Is there maybe some way to code giving the filler items a fixed spot?

    Probably, but you'll first have to give a concrete and complete description of your criteria. For example, do you mean:

    1. One or two critical items followed by exactly one fixed item;
    2. Two critical items followed by exactly one fixed item;
    3. One or two critical items followed by at least one fixed item;
    4. Two critical items followed by at least one fixed item; or
    5. Something else?

    Once you have that clear for yourself (you may already have that, of course, even though the description wasn't), then you can think how you can use the maxrep and mindist constraints to implement it. I think it should be possible!

    Cheers,
    Sebastiaan

  • Hi Sebastiaan,

    Thank you for your answer. I am using a Plugin for the VU-AMS measuring device which does not work on the newest version. Therefore, I cannot update to the newer version.

    I mean your second criteria: Two critical items followed by exactly one fixed item.

    Is there any way I can code this in the earlier version?

    Thank you!
    Aurelia

  • Hi Aurelia,

    Thank you for your answer. I am using a Plugin for the VU-AMS measuring device which does not work on the newest version. Therefore, I cannot update to the newer version.

    I didn't put this on the www.vu-ams.nl website yet as I'm still working on it, however ( ;) ) the VU-AMS plugin (version => 1.5) is OpenSesame 3 compatible:

    Hope this helps,
    Jarik

  • Hi Jarik,

    Thank you for your help! This is of course the ideal situation. Will version 1.5 run without problems or is there something I should look out for?

    Aurelia

  • Hi Aurelia,

    There's nothing specific to look out for, but it's always important to thoroughly test your experiment and check the VU-AMS data for the sync markers.

    One more handy note; you can make VU-AMS recordings without putting on electrode stickers or even without connecting the ECG/ICG electrode cable. You will only record noise but will be able to open the 5FS file in VU-DAMS to check if all event markers are stored.
    You could even disable the triple beeps you get because of the out of range physiological signals via the Warning button in the configuration window. But don't forget to turn them back on!

    If you run into any problems with the VU-AMS plugin please start a new discussion here or in the VU-AMS forum

    Best,
    Jarik

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