psychopy.org | Reference | Downloads | Github

Varying number of repetitions of conditions

Hello,

I am having some trouble varying the number of repetitions different conditions. For example, say I have numbers (1-6) appearing on screen and participants have to give a keyboard (Spacebar) response when it is an odd number, but do nothing when it is an even number. Therefore, I am just looping around a routine, which links to an excel file with such a structure:

Number CorrAns
1 space
2 none
3 space
4 none
5 space
6 none

I want a large majority (say 80%) of trials to require the participant to press the spacebar, i.e. be odd and a minority (say 20% to be even). Is there a way to vary the number of repetitions of each condition (i.e. row), without just copying and pasting lots more odd numbers in the excel file? This is because when my conditions get more complex later, I don’t want the same condition to appear twice in a row. I just want numbers 1-6 to be shown randomly, with a 80% predominance for numbers 1,3 and 5.

I would appreciate any advice on this.

Thanks,
Lucy

Hi Lucy, it’s very simple to create lists of conditions in code. e.g.

# make lists of your distinct conditions:
odd = ['1', '3', '5']
even = ['2', '4', '6']

# grow the lists in a weighted way, and combine 
# them into one long list of 30 entries: 
numbers = (odd * 8) + (even * 2)

# randomise the overall list:
shuffle(numbers)

would give you 30 randomly-ordered numbers (i.e. (3 × 8) + (3 × 2)) which are 80% even and 20% odd. You can use these within a loop to set stimuli and so on, but since they didn’t come from a conditions file, you’d need to explicitly save each trial’s value in the data.

But the real issue is adding pseudo-randomisation constraints (such as you not wanting two successive numbers to be the same). Randomising is easy, but constraining that randomisation is hard. I’ve always done it in a brute force way, with code that cycles through many times until a solution is found. Maybe someone out here has a cleverer way to do that aspect of things…

1 Like

Hi Michael,

Thanks very much for the code. Where in my code component is the best place to put it? I’ve tried begin experiment, begin routine and each frame, but I’m still getting around a 50/50 split for evens and odds. Do you know what I might be doing wrong? Sorry if I’m missing something, still quite new to Psychopy!

Thanks,
Lucy

Hi

  1. That code won’t do anything unless you actually use the values it generates. I’m guessing that at the moment you’re still actually driving the experiment from your conditions file?

  2. That code generates a list of conditions to be used for a loop of trials. Hence it should be only run once (e.g. at the beginning of the experiment). You don’t want to regenerate your conditions at the beginning of every trial, and certainly not every frame (typically 60 times per second). In the same way, you wouldn’t want your loop to use a different conditions file on every iteration.

To actually use these calculated conditions, you’d need to do something like this in the Begin routine tab to extract the number for this trial:

# get the next number in the list:
current_number = numbers.pop() 

# manually store it in the data:
thisExp.addData('number', current_number)

Then you can use the variable name $current_number in the text field of your text component (instead of getting it from a conditions file).

Make sense?

2 Likes

Hi Michael,

Sorry it has been a while to reply - I had to put the experiment aside for the last month.

You code makes perfect sense but I am still having trouble. I have the following in the ‘begin routine’ part of a code component and $current_number in the text field of my text component. I have now unlinked the conditions excel file. but I am getting a message saying current_number is not defined:

#make lists of your distinct conditions:
odd = [1,3,5]
even = [2,4,6]

#grow the lists in a weighted way, and combine
#them into one long list of 30 entries
numbers = odd*8 + even*2

#randomise the overall list:
shuffle(numbers)

#get the number in the list:
current_number = numbers.pop()

#manually store it in the data:
thisExp.addData('number', current_number)

Do you have any suggestions? I am not familiar with running an exp except through use of the linked conditions file.

Thanks,
Lucy

Hi Lucy, when pasting in code, please surround with three back ticks before and after so that it gets properly formatted: `

I haven’t read through all of this thread again, but this code looks like it should be split into different tabs so that parts happen at different times. Very likely you just want to create and shuffle the list of conditions just once (i.e in the Begin experiment tab). Putting it in the Begin routine tab means the list is being re-created on every routine, which probably doesn’t make sense.

The stuff that (likely) should be happening on every routine are the last two lines, where you select the current number and store it in the data file.

Getting the error means that current_number hasn’t yet been defined when you try to use it. This can be for several reasons:

  1. You have set the text stimulus to have constant constant rather than to be updated on every routine (in which case, it expects current_number to exist at the start of the experiment.

  2. You need to have the code component above the text stimulus so that the value gets created before the text stimulus uses it.