On repetitions trials, one of the presented stimuli replaced randomly, but how to achieve?

Hi psychopy community,
Right now I have an experiment that has two conditions:1. Repeat the prime word 3 times on the screen before the judgment task; 2.Repeat the prime word 30 times before the judgment task.
My experiment consisted 144 trials, in 72 trails, the word was repeated 3 times , and in the other 72 trails, the word was repeated 30 times . Trial order was randomised for each participant.
Now, I want to test whether the participant is focused during the experiment, so I need to present a colour word in 14 of my trials(7 in each of the two conditions). On 3 repetition trials, the colour word replaced randomly either the second or third prime word, and on 30 repetition trials, the colour word replaced randomly one of the prime words in positions 10 through 25. Participants had to press the space bar when they detected this colour word.
It’d be so glad if someone can help me out!
Thanks!

Can the 14 trials occur on the same conditions for each subject? If so, it could be done relatively simply using the conditions file (ie add another column for the colour words which is empty except for 14 rows). If it must be randomised across subjects, it would need to be handled entirely in code.

Thanks so much Michael. I think completely random across sujects is the best. But if it’s too complicated, then occur on the same conditions for each subject is ok. This is my condition file, would you tell me how to add the colour words? I’m very new to psychopy, thank you very much for your help!

So the current words are actually images of words, correct? And so the colour words would also be images?

Lastly, would there be a single set of seven colour words, each of which is used in both of the two conditions, or would you have fourteen unique colour words that are randomly spread across the two conditions?

But to get started, I’ll make some guesses about the above anyway. First we can select the trial numbers on which the colour words will appear. Insert a code component in one of your routines, and in the “begin experiment” tab of that component, put something like:

# trial numbers to show colours on:
colour_trial_numbers = np.random.choice(list(range(144)), size = 14, replace = False)

# the word numbers to be shown on:
A_word_numbers = np.random.choice([2, 3], size = 7, replace = True)
B_word_numbers = np.random.choice(list(range(10, 26)), size = 7, replace = True) # NB 26 is deliberate

# the colours to be shown:
A_colours = ['red.jpg', 'orange.jpg', 'yellow.jpg', 'green.jpg', 'blue.jpg', 'indigo.jpg', 'violet.jpg')]
B_colours = ['red.jpg', 'orange.jpg', 'yellow.jpg', 'green.jpg', 'blue.jpg', 'indigo.jpg', 'violet.jpg')]
shuffle(A_colours)
shuffle(B_colours)

Then in the “begin routine” tab of the code component, you need to decide if the trial and repetition number is the one to make a change on. You will need to put this in the “begin routine” tab of a code component of each of the two trial routines, amending for A and B conditions as required. This is what the A would look like.

if trials_3.thisN in colour_trial_numbers:
    if loopA.thisN == 0: # only do this once
        colour_name = A_colours.pop()
        word_number = A_word_numbers.pop()
    elif loopA.thisN == word_number:
        normal_word_variable_name = colour_name # not sure what variable you use here
        thisExp.addData('colour_word', colour_name) # record it in the data for this trial

In that last code block, you are overwriting whatever the normal variable containing the word should be, so the code component should be above the stimulus component, so that it gets executed first. You can control the order of components by right-clicking their icons and shifting them up and down.

Michael, I’m deeply grateful for your help. But I still have some problems, and I hope you can help me to look at them.
My experiment has 144 trials in total, includes 3 blocks, and each block has 48 trails. Before the experiment, the subjects had to practice 5 trials.
Now, I insert a code component in a routine and put it at the beginning of the block. I put codes as same as you told me:

# trial numbers to show colours on:
colour_trial_numbers = np.random.choice(list(range(48)), size = 30, replace = False)

# the word numbers to be shown on:
A_word_numbers = np.random.choice([2, 3], size = 15, replace = True)
B_word_numbers = np.random.choice(list(range(10, 26)), size = 15, replace = True) # NB 26 is deliberate

# the colours to be shown:
A_colours = ['red.jpg', 'orange.jpg', 'yellow.jpg', 'green.jpg', 'blue.jpg', 'gray.jpg', 'violet.jpg']
B_colours = ['red.jpg', 'orange.jpg', 'yellow.jpg', 'green.jpg', 'blue.jpg', 'gray.jpg', 'violet.jpg']
shuffle(A_colours)
shuffle(B_colours)

( I have some questions about this. The 144 trials were divided into three blocks, so I inserted the routine at the beginning of the block, and codes are in the “begin experiment” tab. I didn’t put this routine at the beginning of the experiment, because the three blocks has three different condition files. But I don’t think I’m doing it right, because the experiment can run normally, but the words will not be replaced by colour words)

Then, I insert two code components in each of the two trial routines and codes are in the “begin routine” tab. I put the codes like this:

if trials_3.thisN in colour_trial_numbers:
    if loopA.thisN == 0: # only do this once
        colour_name = A_colours.pop()
        word_number = A_word_numbers.tolist().pop
    elif loopA.thisN == word_number:
        repeat3_w = colour_name # not sure what variable you use here
        thisExp.addData('colour_word', colour_name) # record it in the data for this trial
if trial_type != 'B':
    continueRoutine = False
if trials_3.thisN in colour_trial_numbers:
    if loopB.thisN == 0: # only do this once
        colour_name = B_colours.pop()
        word_number = word_number = B_word_numbers.tolist().pop
    elif loopB.thisN == word_number:
        repeat30_w = colour_name # not sure what variable you use here
        thisExp.addData('colour_word', colour_name) # record it in the data for this trial
if trial_type != 'A':
    continueRoutine = False

The above is my current situation. The experiment can run, but the words can be replaced by colour words.


You’re going to need to explain the design to me, using the actual names of loops and routines. e.g. from your screen shot (which has been edited in some way), I can only see the outer trials_3 loop, which I assumed had 144 repetitions. So I need to see how the experiment is actually being broken up into 3 blocks of 48 trials.

Michael I’m sorry I didn’t explain clearly before. My experiment has 144 trials in total, and was devided into three blocks of 48 trials each. For each block there are 24 trials with 3 repetitions, and for the other 24 trials with 30 repetitions. Between blocks, subjects were allowed a brief pause.

Three blocks have three condition files, I put the three blocks condition files in a Excel file, then I invoke this file in “trial_4”. And “trial_3” can invoke a block condition file from “trials_4” every time.

Since the conditions of 3 repetitions and 30 repetitions are randomly interspersed between the trials, it is necessary to determine whether the trial is 3 or 30 repetitions before each cycle. So I put two code components in LoopA and LoopB to determine which routine(“repeat3” or “repeat 30”) to proceed.

Last, I need to replace 8 repetitions by colour words, 4 for 3 repetitions, and 4 for 30 repetitions. In every trial, the subjects needed to read the repetition words first, and then completed the task of face discrimination. But in order to test whether the subjects were focused in reading repetition words, repetition words were sometimes replaced by colour words. And subjects had to press the space bar
when they detected this colour word. In addition, when the colour word appears, I hope that a keyboard component will appear to record the key response.

Finally, thank you very sincerely for your help, Michael!