| Reference | Downloads | Github

Creating an n-back task in builder view

OK, in that case, we need to use code to keep track of what the value of a variable was n trials ago.

Insert a code component in your trial routine and put something like this in the Begin routine tab (am assuming you loop is called trials, and you will replace your_variable with whatever is needed):

# keep track of the variable that was current two trials ago:
if trials.thisN == 0: 
    two_back = your_variable # initial value needed for trial 2
if trials.thisN == 1:
    one_back = your_variable # need this to swap with two-back on later trials
if trials.thisN > 2: # need to update the n-back values
    two_back = one_back
    one_back = your_variable

# set whatever keypress values are appropriate for this trial:
if your_variable == two_back:
    correct_response = 'y'
    correct_response = 'n'

Use the correct_response variable in your keyboard component to judge whether the right key as pressed.

You might need to skip the response collection on the first two trials (0 and 1). If so, put that stuff in a separate routine, and in a code component, put this in its Begin routine tab:

if trials.thisN <2:
    continueRoutine = False

Thank you very much Michael.

Hello Michael and everyone,

There has been a change in my task design. Please I need an assistance once again.

I have 80 sets of images to present to the participants in a 2 n back task. I will have 2 blocks. So in first part, I will present 80 pictures, followed by a break then I present another set of 80 pictures to the participants.

Their task is to compare whether the current picture matches with the picture presented 2 back in terms of emotional valance. That is, if the two pictures match as positive emotional expression or negative emotional expression.

The right key should be pressed when there is a match, and the left key pressed when there is no match.

The pictures will be presented sequentially. However, I want the order of the blocks to be presented randomly for each subject .

I need to record their accuracy and response time.
Thank you

Please ask an actual question.

Please Micheal I’m sorry I did not quite get you with what you meant by actual question. The narration above is the full description of what I intend doing in a visual 2 n-back task. I’ve been struggling for hours now trying to use the builder to organize it but I still don’t get it. If I can be assisted with the code to help me programme it I will be grateful. Thank you

I have finally been able to find my way out. Thank you

Hi, me too I’m trying to do a n-back task in builder view.

I was thinking about using letters for my variables (A,B,C,D,E). I will like that letters appear randomly and every time a letter appears, the participant would have to press (y) if 2 letters before it was the same letter that appear or (n) if not.

Do I have to create an excel file with the letters and all the possible combinaisons (AB, AC, AD, AA,…) and with an other column for the answers ? (y,n)

I wrote the code previously given in the code component.

Any help would be appreciated.

Thanks in advance.

And what happened? You need to describe your actual problem.

I get this error:

if trials.thisN == 0

SyntaxError: invalid syntax

That example code was missing some necessary colons. Now corrected above, to read for example:

if trials.thisN == 0:

I’m sorry for all those questions.
I modified the code. I still get an error.

NameError: name 'trials' is not define 

Where do I write my letters ? I tried with an excel file, but it’s not working. I don’t understand what I’m doing wrong

Thanks again

  • this is assuming you have a loop called trials. If it is called something else, use that name instead.
  • make sure the code isn’t in the “Begin experiment” tab: the loop won’t have been created at that stage. It needs to be in the “Begin routine” tab.
1 Like

Finally I changed a bit my code because the same letter two back was not appearing enough… so I need to ‘help’ the system.

I wrote this code :

import string
letters = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L']
my_list = [random.choice(letters) for _ in range(2)]  # initializing; first two elements have to be random
n = 50  # select the length of the list; note that 2 elements are already inside!
for i in range(n):
  if random.random() < 0.15:  # 15% of the time repeat a letter
    my_list.append(random.choice(letters))  # 85% of the time, get a new 

So when I am now able to generate a list of letters that 15% of the time the letter 2 back is the same…

I tried to put this code in the code component but after that I was not able to make run my experiment.

Because I want one letter at the time to appear.

Thanks for your help

1 Like

You need to explain to us exactly what you mean by this.

I dont understand how to link my code to the builder… I wrote the code in the code component in ‘each frame’

Thats’s my new code, I did some changes in it.

from random import choice, shuffle
import pandas as pd

num = 60

letters = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L']
my_list = [choice(letters),  choice(letters)]
probab = list(range(num - 2))

# We want 20% of the letters to repeat the letter 2 letters back
pourc = 20
repeatnum = num * pourc // 100
for i in probab:
    ch = prev = my_list[-2]
    if i >= repeatnum:
        while ch == prev:
            ch = choice(letters)

I added a text component and named it $my_list

I get this error :

NameError: name 'my_list' is not defined


As the name on the tab implies, that code will run on every frame (i.e. on every screen refresh), which is typically 60 times per second. That isn’t what you want.

Think about when you want the code to run, and put each part in the appropriate tab. e.g. imports should happen only once, so those lines belong in the “Begin Experiment” tab.

But in your case, these libraries are already imported by Builder, so you can (and should delete them). In particular, Builder imports choice and shuffle from numpy.random, so you importing it from Python’s standard random library will cause the intended ones to be overwritten.

I guess some other code should only run at the beginning of each trial, and so on.

It’s working, but I get the complet list at the same time.

It is possible to separate it ? I want each letter to be on a different screen.

I thought that if I put $mylist in the text component, it would be like a variable and show each letter one after the other.

Many thanks

Computers do what you tell them to do. If you ask it to display a list of items, that list will be displayed. If you want to step through the items of the list and display them one at a time, then that is quite a different thing and needs to be implemented explicitly: computers can’t guess what you want to achieve, they only take you literally.

I think you need to draw a flow chart of what you want to implement and when. The design of the flow panel (i.e. how many (possibly nested) loops) and what you need any code to do and when, will fall naturally out of that.

Thanks Michael for your help!
I’m sorry I don’t understand what you mean

I read many thing about the flow, but it all seems to be related to an csv files or an excel file with the conditions. I understand how to do that, but in this case, I want the loop to see in the list I created in the code component.

Is there a way to link both of them?