psychopy.org | Reference | Downloads | Github

Select from different reward values across trials

Hello,

I have a ‘reward amount’ that varies every trial. Currently I have a parameter ‘rewards’ that is a vector of reward options:

rewards = ['$0.25','$0.50','$1','$2','$4','$8','$16','$32']

Currently every trial displays a different reward randomly:

current_reward = random.choice(rewards)
reward_amnt.setText(current_reward)

I would like the amounts offered in ‘rewards’ to be different depending on which section of the task the participant is in. So in the high reward section the rewards would be [’$8’,’$16’,’$32’]. For the low reward section the rewards would be [’$0.25’,’$0.50’,’$1’,’$2’]. And for the all rewards section the rewards would remain the same as above.

I have created an array called myvec that is one column and 16-1000 rows (depending on how many trials aka rows I would like). The column is build of a series of either 1,2 or 3. I would like the 1 to represent the all reward section. The 2 to represent high reward section and the 3 to represent low reward section.
For example:
array([[1],
[1],
[1],
[1],
[2],
[2],
[2],
[2],
[1],
[1],
[1],
[1],
[3],
[3],
[3],
[3]])

I chose an array because I could easily manipulate the amount of rows and section length. In the above array the section length is 4.

I have this sample draft code that will vary the rewards in the fashion I want:

for i in range(0,len(myvec)):
        if myvec[i,0] == 1:
            rewards = ['$0.25','$0.50','$1','$2','$4','$8','$16','$32']
        elif myvec [i,0]== 2:
            rewards = ['$8','$16','$32']
        else:
            rewards = ['$0.25','$0.50','$1','$2']

My current issue is that I don’t know how to make this code interact with my task code so far. I’m not sure if there is a specific field to place it on builder or coder so that the trial handler that creates the loops interacts with this.

Essentially every row of the array would be a trial. And therefore I know it should interact with the loop I created in builder, but I’m not sure how. Any help would be great, thank you!!

You don’t need a for loop in code: you just need to extract a value from your list once per trial, letting the Builder loop do the work for you. e.g. if the Builder loop runs 1000 times, and your list of values is 1000 entries long, then just pull out a value from that list once per trial.

Tips to note:

  • always give your variables meaningful names, e.g. reward_levels rather than generic terms like myvec. You’ll thank yourself for this when reading your code in 6 months time.
  • put meaningful values in that list like 'low', 'high', 'all' rather than 1, 2, 3. Again, you’ll thank yourself at the analysis stage.
  • convert the array to a list so that it is easier to work with, without all the messy indexing.

"Begin experiment" tab

# convert array of arrays to a flat list, using a list comprehension:
reward_levels = [entry[0] for entry in myvec]

low_rewards =  ['$0.25', '$0.50', '$1', '$2']
high_rewards = ['$8', '$16', '$32']
all_rewards = low_rewards + high_rewards + ['$4']

"Begin routine" tab

# get next reward level value on each trial:
reward_level = reward_levels.pop(0) # shrinks the list by one entry

# store it in the data:
thisExp.addData('reward_level', reward_level)

# get the specific value for this trial:
if reward_level == 'low':
    reward_value = np.random.choice(low_rewards)
elif reward_level == 'high':
    reward_value = np.random.choice(high_rewards)
else:
    reward_value = np.random.choice(all_rewards)

thisExp.addData('reward_value', reward_value) # record it