Counterbalancing across different factors

Say I want to counterbalance across 2 factors, e.g. SOA (1, 2) and color (red, blue). How would I structure this in Python code so that a) all participants experience all possible conditions (ie, 1/red, 1/blue, 2/red, 2/blue) and b) the order of all possible conditions is randomized? I’m hoping to see something basic that I can build off of. Thanks!

Create lists which cycle at the required rates to get balancing across their respective entries:

SOAs = [1.0, 2.0] * 2                 # entries cycle quickly
[1, 2, 1, 2]

colours = ['red'] * 2 + ['blue'] * 2  # entries cycle slower
['red', 'red', 'blue', 'blue']

Zip them together in a single list:

condition_pairs = list(zip(colours, SOAs))

[('red', 1), ('red', 2), ('blue', 1), ('blue', 2)]

More usefully you might want the trial conditions in a list of dictionaries so you can refer to attributes by name, so do something like this:

trials = [{'colour':c[0], 'SOA':c[1]} for c in zip(colours, SOAs)]

[{'colour': 'red', 'SOA': 1},
 {'colour': 'red', 'SOA': 2},
 {'colour': 'blue', 'SOA': 1},
 {'colour': 'blue', 'SOA': 2}]

To randomise:

from numpy.random import shuffle

shuffle(trials)

e.g.

for trial in trials:
    some_stimulus.colour = trial['colour']
    some_stimulus.draw()
    win.flip()
    core.wait(trial['SOA'])

Hi,
I’m designing a primed picture naming experiment in which I have some conditions.
There are 4 blocks of different trials that I need to randomize them, both within block and between blocks. The first two blocks are related to one condition and the second two are related to another condition. So I need to make three randomization types. With responses to questions and videos, I discovered that I need to make a loop for each of the blocks and set them random. Then I should make a loop for each of my two blocks and then make a loop around all the four blocks. But doing so, my experiment doesn’t run.

Another point is that there are two SOA durations in the experiment which is going to be random too for each block; i.e. a group of participants receive the first block with a short SOA and the second group with a long SOA, then for the second block it would be the reverse, and so forth. How should I define it in the experiment so that it doesn’t need to design two or maybe 4 different experiments for each SOA?
Thanks for your helps in advance.