Rae
Rae

Reputation: 21

pseudo randomization in loop PsychoPy

I know other people have asked similar questions in past but I am still stuck on how to solve the problem and was hoping someone could offer some help. Using PsychoPy, I would like to present different images, specifically 16 emotional trials, 16 neutral trials and 16 face trials. I would like to pseudo randomize the loop such that there would not be more than 2 consecutive emotional trials. I created the experiment in Builder but compiled a script after reading through previous posts on pseudo randomization.

I have read the previous posts that suggest creating randomized excel files and using those, but considering how many trials I have, I think that would be too many and was hoping for some help with coding. I have tried to implement and tweak some of the code that has been posted for my experiment, but to no avail.

Does anyone have any advice for my situation?

Thank you, Rae

Upvotes: 2

Views: 1013

Answers (2)

jrgray
jrgray

Reputation: 415

Here's an approach that will always converge very quickly, given that you have 16 of each type and only reject runs of more than two emotion trials. @brittUWaterloo's suggestion to generate trials offline is very good--this what I do myself typically. (I like to have a small number of random orders, do them forward for some subjects and backwards for others, and prescreen them to make sure there are no weird or unintended juxtapositions.) But the algorithm below is certainly safe enough to do within an experiment if you prefer.

This first example assumes that you can represent a given trial using a string, such as 'e' for an emotion trial, 'n' neutral, 'f' face. This would work with 'emo', 'neut', 'face' as well, not just single letters, just change eee to emoemoemo in the code:

import random

trials = ['e'] * 16 + ['n'] * 16 + ['f'] * 16
while 'eee' in ''.join(trials):
    random.shuffle(trials)    
print trials

Here's a more general way of doing it, where the trial codes are not restricted to be strings (although they are strings here for illustration):

import random

def run_of_3(trials, obj):
    # detect if there's a run of at least 3 objects 'obj'
    for i in range(2, len(trials)):
        if trials[i-2: i+1] == [obj] * 3:
            return True
    return False

tr = ['e'] * 16 + ['n'] * 16 + ['f'] * 16
while run_of_3(tr, 'e'):
    random.shuffle(tr)
print tr

Edit: To create a PsychoPy-style conditions file from the trial list, just write the values into a file like this:

with open('emo_neu_face.csv', 'wb') as f:
    f.write('stim\n')  # this is a 'header' row
    f.write('\n'.join(tr))  # these are the values

Then you can use that as a conditions file in a Builder loop in the regular way. You could also open this in Excel, and so on.

Upvotes: 3

brittAnderson
brittAnderson

Reputation: 1473

This is not quite right, but hopefully will give you some ideas. I think you could occassionally get caught in an infinite cycle in the elif statement if the last three items ended up the same, but you could add some sort of a counter there. In any case this shows a strategy you could adapt. Rather than put this in the experimental code, I would generate the trial sequence separately at the command line, and then save a successful output as a list in the experimental code to show to all participants, and know things wouldn't crash during an actual run.

import random as r
#making some dummy data
abc = ['f']*10 + ['e']*10 + ['d']*10

def f (l1,l2):
    #just looking at the output to see how it works; can delete
    print "l1 = " + str(l1)
    print l2
    if not l2:
        #checks if second list is empty, if so, we are done
        out = list(l1)
    elif (l1[-1] == l1[-2] and l1[-1] == l2[0]):
        #shuffling changes list in place, have to copy it to use it
        r.shuffle(l2)
        t = list(l2)
        f (l1,t)
    else:
        print "i am here"
        l1.append(l2.pop(0))
        f(l1,l2)
    return l1

You would then run it with something like newlist = f(abc[0:2],abc[2:-1])

Upvotes: 0

Related Questions