psychopy.org | Reference | Downloads | Github

Limit Randomization To Two Repeats

I am creating a color discrimination task for common crows where pecks to the correct colored circle result in a food reward. The basic structure of the experiment is as follows. A flashing stimulus appears, if it is clicked (pecked at) the experiment moves onto the color test, if it is not clicked they have a penalty phase with no stimuli before being presented with the flashing stimulus again. If they make it to the color test, there are two circles presented side by side. A correct click to the rewarded color leads to food and starts the trial over again and an incorrect click also starts the trial over again but with no food reward.

On each trial, I would like the color’s of the circle to be randomized with the rule that the same color can’t be on the same side more than twice. I currently have the color of the two circles set up with this excel format:

If I click random for the trial looptype, the first order presentation is random then the next is the opposite then random then opposite which gets us close to our goal but prevents an order like this BBAABB from ever occurring. If I click on full random, I get nicer randomization but can’t prevent cases like AAAAB which I would like to. Is there a way to constrain the fullrandom order such that the same row can’t be selected more than twice in a row?

In essence, no. There are just too many possible constraints on randomisation to easily cater for in a simple GUI. To implement such constraints could require either of two approaches:

  • Use code to generate conditions files that satisfy your constraints. That conditions file would be used in a loop that is set to sequential rather than random, as the randomness is now built-in to the order of rows in the file. This would achieve what you want, but requires custom programming.
  • Alternatively, manually craft a conditions file that meets your constraints, and again present it sequentially. The disadvantage is that everybody would receive the same pseudo-random sequence.

Hi Benjamin,
maybe I misunderstand the problem, but:

You have two different conditions, condition a: violet-purple and condition b: purple-violet.
In the trials loop, you select loopType = Random and some value for nReps$ to repeat these two conditions n-times. Let’s say nReps$=2. This gives 4 possible sequences:
abab, abba, baab, baba. For me this looks as if it is exacly what you want.

For your main question

see Michaels answer.

-Sven-

Hi Sven,

Thanks for trying to help. Michael’s solution will work albeit it is a little more effortful than I would have hoped. You are correct in your describing of what we want but the problem is the current paradigm necessarily prevents AABB or BBAA combinations. As we are using animal models on repeated trials, we don’t want them to simply learn the pattern that if purple-violet is presented the following trial will always be violet-purple. Do you see a way around this other than Michael’s suggestion of manipulating the excel file prior to each session?

Hi @Benjamin_Seitz ,

Perhaps before the experiment begins, you could create a new conditions file from the example you gave here (in this example called “randomize.xlsx”). With just two trials, you could randomize trial order and append them to a new list, so that the final list will be random but never have more than 2 identical consecutive trials:

import pandas as pd
import numpy as np

def buildList(df, blocks, returnDF):
    """
    This function will create a new list using 2 * number of iterations. E.g., 2 * 10 blocks with give you 20 trials

    :param df: dataframe of trials
    :param blocks: number of blocks to create trials
    :returnDF: decision to return DF or save for later use in trialHandler
    """
    newDF = pd.DataFrame(df)
    for i in range(blocks):
        df = df.reindex(np.random.permutation(df.index))
        newDF = newDF.append(df)
    if returnDF:
        return newDF
    newDF.to_excel("newConditionFile.xlsx", index = False)

conditionFile = pd.read_excel("randomize.xlsx")
newTrialList = buildList(conditionFile, 10, returnDF = False)
1 Like