Psychophysics tasks tutorial

Goal: understand the trial sequence in the experiment, then learn what to change to test your own images.

CVR Summer School 2026

Psychophysics notebooks

Use these launch buttons to open the original notebooks in Google Colab.

← Back to tutorial hub
Manipulate stimulimanipulate_stimuli.ipynb
Open in Colab
Extract 2AFCpsychophysics/extract_2afc_results.ipynb
Open in Colab
Extract ratingspsychophysics/extract_ratings.ipynb
Open in Colab
Extract N-backpsychophysics/extract_n_back_results.ipynb
Open in Colab
Make 2AFC task linespsychophysics/make_2afc_html_lines.ipynb
Open in Colab
Make ratings task linespsychophysics/make_ratings_html_lines.ipynb
Open in Colab
Make N-back task linespsychophysics/make_n_back_html_lines.ipynb
Open in Colab

What is this tutorial?

This tutorial explains the experiment files in simple words.

You do not need to understand every line of code. Start by learning the screens that make one trial, then learn which small parts you should edit.

Main idea: each experiment is a list of screens. jsPsych shows the screens in order and saves the participant's responses.

Task paradigms

2AFC

2AFC means two-alternative forced choice.

The participant sees one image and must choose which one of two stimulus alternatives they saw.

Use this for object recognition or drawing recognition tasks.

Files: obj_recog.html, draw_recog.html

Rating

The participant sees one image and moves a slider to indicate their response.

Use this when there is no objectively correct answer.

File: social_ratings.html

N-Back

The participant sees images one at a time.

They answer whether each image is new or repeated.

File: memorability.html

What happens in one trial?

A trial is one small part of the experiment. In most tasks, one trial shows one image and asks for one response.

1. FixationLook at +
→
2. ImageShow the picture
→
3. BlankShort pause
→
4. ResponseClick or rate
→
5. Save dataResponse + reaction time

Most tasks repeat this same trial many times, once for each image.

In the code, these screens are added to a list called the timeline. jsPsych runs the timeline from top to bottom.

The four main screens

➕

1. Fixation screen

A plus sign appears in the middle of the screen. This tells the participant where to look before the image appears.

Common time: 500 ms
🖼️

2. Image screen

The image is shown. In many tasks, it is shown briefly so the participant cannot look at it for too long nor have time to make an eye movement and look away from the image.

Common time: 100–200 ms
⚪

3. Blank screen

Nothing appears for a short time. This separates the image from the response screen.

Common time: 100 ms
🧩

4. Response screen

The participant answers. They may click one of two choices, move a slider, or say whether an image is novel or repeated.

Common time: 3000–8000 ms

How to make the starting screens

Before the real trials start, the experiment usually adds setup screens to the timeline.

let timeline = [];

timeline.push(preload);       // load images before the task starts
timeline.push(fullscreen);    // ask the participant to enter fullscreen
timeline.push(consent_form);  // show consent information
timeline.push(intro_info);    // show basic information
timeline.push(survey);        // ask participant questions
timeline.push(welcome);       // show task instructions

What can I change?

How to make a fixation screen

timeline.push({
    type: 'html-keyboard-response',
    stimulus: '<div style="font-size: 60px;">+</div>',
    choices: jsPsych.NO_KEYS,
    trial_duration: 500
});

This screen shows a plus sign. The participant cannot answer during this screen.

Fixation screen

What can I change?

How to make an image screen

timeline.push({
    type: 'image-button-response',
    stimulus: images[i],
    stimulus_height: stimulusSize,
    choices: [],
    trial_duration: 100,
    data: { trial_index: i }
});

This screen shows one image from your image list. In 2AFC tasks, the participant sees the image first and answers later.

Stimulus screen

What can I change?

How to make a blank screen

timeline.push({
    type: 'html-keyboard-response',
    stimulus: '',
    choices: jsPsych.NO_KEYS,
    trial_duration: 100
});

This screen shows nothing. It creates a short pause after the image.

Blank screen

What can I change?

How to make response screens

Different task paradigms use different response screens.

2AFC response

Choose between two answers
2AFC response screen

The participant clicks the left or right answer.

Example code
timeline.push({
    type: 'html-button-response',
    stimulus: '',
    button_html: [lb1, lb2],
    choices: ['left', 'right'],
    trial_duration: 3000
});

Change: lb1 and lb2 to change the labels/images shown as answers. Change trial_duration to give more or less time to answer.

Saves: choice, accuracy, reaction time

Rating response

Move a slider
Rating response screen

The participant gives a rating using a slider.

Example code
timeline.push({
    type: 'html-slider-response',
    stimulus: '<p>Choose a position on the slider.</p>',
    labels: ['1', '5'],
    min: 1,
    max: 5,
    step: 1,
    require_movement: true,
    trial_duration: 8000
});

Change: stimulus for the question, min/max for the rating scale, labels for text under the slider, and trial_duration for response time.

Saves: rating, reaction time

N-Back response

Novel or repeated?
N-Back response screen

The participant says whether the image is new or has appeared before.

Example code
timeline.push({
    type: 'image-button-response',
    stimulus: images[i],
    stimulus_height: stimulusSize,
    choices: ['Novel', 'Repeated'],
    prompt: '<p>Has this image been shown before?</p>',
    trial_duration: 3000,
    stimulus_duration: 200
});

Change: choices to change button text, prompt to change the question, stimulus_duration to change image time, and trial_duration to change total answer time.

Saves: choice, accuracy, reaction time

Important timing words

Code wordMeaningExample
trial_durationHow long the whole screen lasts.trial_duration: 3000 means 3 seconds.
stimulus_durationHow long the image itself is visible during a response screen.stimulus_duration: 200 means the image disappears after 200 ms.
choices: jsPsych.NO_KEYSThe participant cannot press keys to answer.Used for fixation and blank screens.
choices: []No response buttons are shown.Used when the image is shown before the answer screen.
All times are in milliseconds. 1000 ms = 1 second.

The most important section to edit

Open a task file and look for this line:

// ===== STUDENTS: EDIT THIS SECTION =====

This section tells the task which images to show and, when needed, which answer is correct.

What can I change?

What you want to changeWhere to lookWhat to edit
Images shown in the task// STUDENTS: EDIT THIS SECTIONval
Image foldernear the image listimageFolder or image path text
Create altered image versionsmanipulate_stimuli.ipynbUse it to make blurred, noisy, brighter, or contrast-changed images
Correct answer in 2AFCobj_2afc.html or draw_2afc.htmlcorrect_choice
Left and right answers in 2AFCobj_2afc.html or draw_2afc.htmlch1 and ch2
Novel/repeated answer in N-Backn_back.htmlcorrect
Timing of a screeninside each screen objecttrial_duration or stimulus_duration
Rating scaleratings.htmlmin, max, step, and labels
Instruction textwelcome/instruction screenstext inside stimulus or prompt

How to change images in a 2AFC task

Example from obj_recog.html:

let val = [81, 63, 54, 31, 142];
let ch1 = [4, 5, 9, 2, 5];
let ch2 = [0, 3, 2, 1, 7];
let correct_choice = [4, 3, 2, 1, 7];

Read this as:

All four lists must have the same length. If you show 5 images, you need 5 left answers, 5 right answers, and 5 correct answers.

Example: test your own 2AFC images

Put your images in:

images/your_experiment_name/

Name them like this:

im0.png
im1.png
im2.png

Then change the image list:

let val = [0, 1, 2];

The task will now show im0.png, im1.png, and im2.png.

How to change images in a Rating task

Example from ratings.html:

let val = [7, 2, 15, 0, 18];

This means the task shows:

im7.jpg
im2.jpg
im15.jpg
im0.jpg
im18.jpg

To test your own images, put them in:

images/your_experiment_name/

Then change val to your image numbers.

How to change images in an N-Back task

Example from n_back.html:

var val = [161, 161, 115];
var correct = [0, 1, 0];

Read this as:

0 means Novel. 1 means Repeated.

How to use your own images

To test your own images, we suggest using around 20 images. This is enough to try the task without making the experiment too long.

There are three steps:

Important idea: the metadata file tells the experiment which images to show and what the correct answer is. After you create the metadata file, a small script can generate the lines you need to paste into the HTML file.

Step 1: save your images

Put your images in a folder inside the images folder. For example:

images/my_experiment/

Name your images with numbers:

im0.png
im1.png
im2.png
im3.png
...

The number in the file name is the image index. For example, im7.png has image index 7.

Step 2: choose your task paradigm

Task paradigm What the participant does What the metadata needs
2AFC Choose between two answers. Image file, image index, correct answer.
Rating Move a slider to give a rating. Image file and image index.
N-Back Say whether the image is novel or repeated. Image file, image index, correct answer.

Step 3: create a metadata CSV file

A metadata CSV file is a small table that describes your images. You can make it in Excel, Google Sheets, or Python. Then save it as a .csv file.

Example metadata file for 2AFC

Use this format if the participant must choose between two answers. For 2AFC, you only need to provide the correct answer. The script will automatically create the left and right choices.

The answer numbers can come from this class list:

lb = ['bear','elephant','person','car','dog','apple','chair','plane','bird','zebra']

The first class has index 0, the second class has index 1, and so on:

image_file,image_idx,correct_choice
im0.png,0,4
im1.png,1,3
im2.png,2,6
im3.png,3,9

Read this as:

The script will use correct_choice to create the two response options. One option will be the correct class. The other option will be a wrong class chosen from the same class list.

Example metadata file for Rating

Use this format if the participant gives a rating. There is no correct answer.

image_file,image_idx
im0.png,0
im1.png,1
im2.png,2
im3.png,3

Example metadata file for N-Back

Use this format if the participant answers Novel or Repeated. For N-Back, you only need to say which images should be repeated. The script will create the final task order.

image_file,image_idx,repeated
im0.png,0,0
im1.png,1,0
im2.png,2,1
im3.png,3,0
im4.png,4,1

Read this as:

0 means the image is shown once only.
1 means the image is shown once, then shown again later as a repeated image.

By default, the script uses N = 5. This means that a repeated image appears again 5 trials after it first appeared.

Generate the HTML lines from the metadata file

After making the metadata CSV file, use the script for your task paradigm. The script reads the metadata file and prints the lines you need to copy into the HTML file.

Task paradigm Script Lines it creates
2AFC make_2afc_html_lines.py val, ch1, ch2, correct_choice
Rating make_ratings_html_lines.py val
N-Back make_nback_html_lines.py val, correct

Then paste the generated lines into the HTML file under:

// ===== STUDENTS: EDIT THIS SECTION =====

What jsPsych saves automatically

jsPsych saves the participant's data after every trial.

The task can also save extra information, such as whether the answer was correct.

Important: complete the task before closing the window

The CSV file is saved only at the end of the task, after the final thank-you screen appears.

If you close the browser window, refresh the page, or stop the task before the final screen, the CSV data file will not be saved.

Please continue until you see the message saying that the task is complete and the CSV file has been saved.

How data are scored

2AFC

The code checks whether the selected left/right option matches the correct label.

Saved column: correct.

Rating

There is no correct answer. The important value is the slider response.

Saved column: response.

N-Back

The code checks whether the participant clicked Novel or Repeated correctly.

Saved column: correct.

Example: raw CSV file

This is what the data look like immediately after running the experiment. The raw CSV contains all screens, including preload, fullscreen, instructions, survey questions, fixation screens, image screens, blank screens, and response screens. That is why the file has many rows that are not actual trials.

For analysis, we usually do not want every row. We mainly want the rows where the participant gave an answer.

Raw CSV

How do I extract the data?

After collecting CSV files from participants, open the parser notebook for your task paradigm.

Task paradigmUse this parser notebookWhat it does
2AFCextract_2afc_results.ipynbKeeps one row per image choice and computes whether the answer was correct.
Ratingextract_ratings.ipynbKeeps one row per rated image and keeps the slider rating.
N-Backextract_n_back_results.ipynbKeeps one row per image and computes whether Novel/Repeated was correct.

Example: extracted CSV file

This is what the data look like after using the parser notebook. The extracted CSV is much easier to read because it keeps only the useful information: one row per real trial, the image shown, the participant's response, the correct answer, and whether the participant was correct.

This cleaned file is the one you should use for plotting results and computing accuracy or average ratings.

Extracted CSV

What can I change?

input_folder = "data/raw_data_folder"
output_file = "data/extracted_results.csv"
Simple rule: raw CSV files are for saving everything. Extracted CSV files are for future analysis.

Student checklist

Hub