Howdy, Stranger!

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

Supported by

shuffle related rows

Hi,
I'm very new on Opensesame and therefore I might have a problem that is very easy to solve. But somehow I could not find a solution yet, so maybe someone could help me with this.
I need to randomize some items in a column, so every subject gets another sequence of items. The problem I have is, that this item should always be presented with another item.
I have a screenshot of the loop table:

I want to randomize the CS_closed, so that CS_closed for every subject is presented with another US_rating (US_rating consists of different images). CS_closed 1 (here: CS_Kanji1.bmp) should always be presented with the same US_rating for the respective subject. Plus, US_rating cannot be randomized because it is depending on the column valence and correct_response. I hope you can understand what I mean. Thanks!

Caro

Comments

  • Edit: You can see that I tried to do it blockwise, but I need a full randomization, so this is not what I was looking for.

  • Edit: Also tried it with coding:


    But unfortunately Python always crashes.

  • Hello,

    So if I understood you want to random one column while keeping the other constant?
    If so a stategy could be:

    • first you enter your initial matrix in a loop at the very beginning of your scrip (just to load it);
    • then you extract these values, just random the first column in a new datamatrix, then copy this new randomized datamatrix in the subsequant loop.

    Like this:

    With the inline for randomizing and preparing the subsequant loop as:

    from datamatrix import DataMatrix
    import random
    
    # create a list to stock the element from the column of the datamatrix (ie Load_Item_Loop)
    # to randomize
    
    list_column1_to_randomize = list()
    
    # fill this list
    for row in items['Load_Item_Loop'].dm.column1_name:
        list_column1_to_randomize.append(row)
    
    # randomize the list
    random.shuffle(list_column1_to_randomize)
    
    # then create a new datamatrix that will be used for the subsequent loop where
    # column1_name is randomized but the other column not randomized
    # create the new datamatrix
    
    new_dm = DataMatrix(length=len(list_column1_to_randomize))
    # fill the first column with the randomized list
    new_dm.column1_name = list_column1_to_randomize
    # initialize and fill the two other columns
    new_dm.column2_name = ''
    new_dm.column3_name = ''
    
    for i, row in enumerate(new_dm.column2_name):
        new_dm.column2_name[i] = items['Load_Item_Loop'].dm.column2_name[i]
        new_dm.column3_name[i] = items['Load_Item_Loop'].dm.column3_name[i]
    
    # now copy the new_dm in new_loop (it will become the new values of this loop)
    items['new_loop'].dm = new_dm
    

    Find the script too maybe it will be more explicit.
    Just be sure you change the two loop as sequential (I do not think it matters a lot but up to you)

    Best,

    Sylvain

  • Hello Sylvain,

    thank you for replying. I started the experiment you added and the first column is randomized, that's exactly what I want! But column 2 and 3 are presented sequentially (even if I change 'order' to 'random'). This should not be the case. Plus, the biggest problem I have is that the items in column 1 are presented two times, so they should be randomized but like this:
    column 1, row 1 to 8 is randomized (e.g. 6, 4, 7, 3, 2, 1, 8, 5) and this order is attached to the items in column 2. Row 9 to 16 in column 1 and 2 should contain the same pairing as row 1 to 8. Does this make any sense? ':D

    Caro

  • And maybe you could tell me what is wrong with the code mentioned above? Because I do not understand why Python always crashes.

  • I do not really know why it crashes otherwise if I understood what you want you could try that:

    from datamatrix import DataMatrix, operations
    import random
    
    # create a list to stock the element from the column of the datamatrix (ie Load_Item_Loop)
    # to randomize
    
    list_column1_to_randomize = list()
    
    # fill this list
    for row in items['Load_Item_Loop'].dm.column1_name:
        list_column1_to_randomize.append(row)
    
    # randomize the list
    random.shuffle(list_column1_to_randomize)
    
    # then create a new datamatrix that will be used for the subsequent loop where
    # column1_name is randomized but the other column not randomized
    # create the new datamatrix
    
    new_dm = DataMatrix(length=len(list_column1_to_randomize))
    # fill the first column with the randomized list
    new_dm.column1_name = list_column1_to_randomize
    # initialize and fill the two other columns
    new_dm.column2_name = ''
    new_dm.column3_name = ''
    
    for i, row in enumerate(new_dm.column2_name):
        new_dm.column2_name[i] = items['Load_Item_Loop'].dm.column2_name[i]
        new_dm.column3_name[i] = items['Load_Item_Loop'].dm.column3_name[i]
    
    print(new_dm)
    
    # now shuffle everything but keep row associated
    
    final_dm = operations.shuffle(new_dm)
    
    # repeat it n times and concatenate
    
    repeat = 1
    for r in range(repeat):
        dm_repeat = operations.shuffle(new_dm)
        final_dm = final_dm << dm_repeat
    
    
    
    print(final_dm)
    
    
    # now copy the new_dm in new_loop (it will become the new values of this loop)
    items['new_loop'].dm = final_dm
    
    

    Datamatrix function are on this page:
    http://datamatrix.cogsci.nl/

    And find the modified script.

    Hope it will work this time.

  • Hello Sylvain,

    wow, thanks alot! This looks like it could be a very nice solution for my problem. So far I got it going by this code:

    But I think your code looks much nicer and as if it could be used in a more general way. I will try it this way as well Thank you so much for your time and this nice piece of code!

    Caro

Sign In or Register to comment.