psychopy.org | Reference | Downloads | Github

Randomizing stimuli position with constraints

Hi everyone!

I’m trying to program a Go/Nogo experiment which is working so well in Psychopy 1.90.1. My problem is when I want to constraint the randomization of stimuli position. I have this little script in a code component object:

’ if random()>-0.5:
targetPos=[-0.5, 0.0]#on the left
corrAns=‘c’
else:
targetPos=[0.5, 0.0]#on the right
corrAns=‘m’

thisExp.addData(‘corrAns’, corrAns)
thisExp.addData(‘targetPos’, targetPos)’

But, what I really wanted to do is to program some constraint, which were able to change to the other position when 4 consecutive stimuli were presented in the same side! I’ll appreciate any suggestions to do this!

Thanks a lot in advance!

Hi @Marc_B, you can do something like this using the Counter class:

### Begin Experiment tab
from collections import Counter
prevFour = []    # List of previous 4 positions

### Begin Routine:
if random()>0.5:
    targetPos=.5#on the left
    corrAns='c'
else:
    targetPos=-.5#on the right
    corrAns='m'
prevFour.append(targetPos)

# Get last four targetPositions
if len(prevFour) >=4:
    prevFour = prevFour[-4:]  
    
# Check the prevFour list for most common values
# If most common == 4, swap position and reset list
allPos = Counter(prevFour)
nRepeats = allPos.most_common(1)[0][1]
mostCommonVal = allPos.most_common(1)[0][0] 
if nRepeats == 4:
    targetPos = mostCommonVal * -1
    prevFour = []

Hi @dvbridges !!

Thanks a lot for your advice and your quick answer. Finally I fixed the problem with this piece of code in my code component object!

I put that in a begin experiment section:

PosC = 1
PosM = 1

Then I write the next in the begin routine section:

if random()>0.5:
targetPos=[-0.5, 0.0]#on the left
corrAns=‘c’
PosC = PosC + 1
if PosC > 3:
targetPos=[0.5, 0.0]#on the right
PosC = 1
else:
targetPos=[0.5, 0.0]#on the right
corrAns=‘m’
PosM = PosM + 1
if PosM > 3:
targetPos=[-0.5, 0.0]#on the left
PosM = 1

thisExp.addData(‘corrAns’, corrAns)
thisExp.addData(‘targetPos’, targetPos)

Now my experiment is running and the position constrain is working perfectly!
Thank you again for your help @dvbridges . I’ll take it in consideration in future works!

take care!

1 Like