PsychoPy coupling with fMRI not yielding responses


I am both a newbie to Psychopy / Programming in Python as well as running fMRI studies.
Nevertheless, I am currently about to set up a PsychoPy Coder based script for an fMRI scan.

The script that I have written works perfectly on my laptop: I have programmed it to show a grey screen until the keyevent ‘5’ is registered and to save any keyboard events (number ‘2’ or ‘4’).

My problem(s):

  1. I have coupled my stimulus laptop to the fMRI PC at our scanning site. The beamer-based presentation works (experiment is visible), but starting the experiment was not achieved by the scanner trigger. Instead, the exp only started when we pressed the button “5” on our stimulus laptop. Not a final solution, obviously; I would like to take the first trigger signal from the fMRI PC to start my experiment (see code below).
  2. The responses that our participants gave using a diamond response box were registered by the fMRI PC, but not by our attached stimulus laptop.

The only “key”-registering part in my code at the moment is

from psychopy.hardware import keyboard

# Record keypress
key_0,rt_intro0, time0 = trial_routine.wait_for_keys(components=list_components, valid_keys=['5'], label=routine_label, globaltime=globalClock)

# As soon as trigger signal is obtained from fMRI ("5"), start presentation and reset globalClock
if '5' in key_0:
    # Reset global core clock

The routine wait_for_keys is defined (colleague from a colleague) like this:

    def wait_for_keys(self, components, valid_keys, label, globaltime): # "globaltime" added by SK
        event.clearEvents() # clear event cache
        self.window.logOnFlip(level=logging.EXP, msg='%s onset' % label) # log onset stimuli
        key_list = np.append(valid_keys, self.escape_key)
        timeB = globaltime.getTime() # Line added by SK | Get global time at presentation

        while True:
            for comp in components:

            pressed_keys = event.getKeys(keyList=key_list, timeStamped=self.timer)
            if len(pressed_keys)>0:
                key, rt = pressed_keys[0] # get the first pressed key and response time 

                if key == self.escape_key:

        self.window.logOnFlip(level=logging.EXP, msg='%s offset' % label) # log offset stimuli
        if len(pressed_keys)>0: 
             return key, rt, timeB # Line added by SK
             return np.nan, self.timer.getTime()

Current solutions and questions:
Regarding problem 1, I found the example code fMRI_launchscan. Since I want to launch the scan only prior to the experiment: Am I right with implementing this code instead of my “if ‘5’ in key_0:” part and only use ‘Volumes’ = 1?

Regarding problem: I am not sure, if this is a hardware or software issue. The research team of the scanning site could not help either. If a software issue: Is it possible that the response box input is not registered by our laptop because the psychopy module “keyboard” is not appropriate?

Here are some technical details for the programming:

  • Scanner: 3 Tesla Siemens MAGNETOM Prisma Scanner
  • Coding Setting: Coder
  • PsychoPy version: Psychopy v2020.1.2
  • OS system: MacOS 10.14.6

I would be glad for any kind of support, particularly regarding the second problem!

If helpful, I can also append the full code of the experiment.


Hi There,

I have to admit I am not very knowledgeable in fMRI studies but let’s see if we can help suss this out. Can I please clarify a few things:

  1. It sounds as though you are wanting to skip the wait_for_keys function when in the scanner - is that correct? could you try adding a variable to say if you are in the scanner and only call the function if you are not in the scanner?
  2. to clarify - were the responses saved to an output file as you expected?



How did you couple your laptop to the fMRI-computer?

In any case, the keyboard component is most likely not the correct component. Usually, repsonse boxes are connected to the parallel port or an usb-port. You have to monitor there for an input. But given that the button box is connected to the fMRI-PC I don’t see how your laptop should register this response. Sort out the hardware setup before working on your program.

There is a fMRI-experiment on pavolvia which might help.

You can emulate an fMRI-experiment (see here).

What softare do other people use there? It might be better to use this piece of software instead of using PsychoPy? Then you only have to deal with your experiment but not how to wait for the scanner pulse or how to register key from the button box.

Cheers Jens

thank you very much for your answers and suggestions.

I had the chance to re-test my script at our scanning sites this morning, and everything worked out perfectly: The trigger from the scanner was received by our stimulus laptop, starting the psychopy experiment, and all responses were adequately registered and saved to an output file.

With regard to the coupling, I have no idea why it worked this time, but I was told by the scanning staff that this happens from time to time (?) and requires re-connecting your stimulus presentation device to the fMRI PC.

For the response issue, I realised that a part of my script where I used the posted wait_for_keys function was impairing this function from working properly. Having connected a usb-port to our stimulus laptop, it seems that the response box actually emulated the top row of the presentation laptop (as programmed), and sent it to the laptop via the fMRI PC, from what I can tell.

You are absolutely right, @Jens, looking back it seems logical to use the software predominantly used at the scanning site – I’ll definitely consider that for the next time.

Thanks a lot for your answers, though!


so pleased to hear you found a solution!


Hello Sarah,

cool that it worked out with PsychoPy. Setting up fMRI-experiments is tricky and having an unreliable connection doesn’t make it easier. Are the responses (and your conditions) also captured in the fMRI-file?

Cheers Jens

Hello Jens,

You are right, I am glad it worked out but also surprised that is seems to involve luck to a considerable degree.
If you refer to our behavioral output file, yes, it does capture the responses / conditions matching them to time stamps now.


Hello Sarah,

will you only analyse the behavioral output, I guess not? AFAIK, you need the button presses and condition codes in your fMRI-data file to analyse it, at least I needed this in my EEG- and MEG-datafiles. Here conditions and button presses are stored along with the EEG/MEG-signal.

Cheers Jens

Hello Jens,

you are right, we are of course interested in matching our presentation and behavioural output with the fMRI images. As far as I understand, this is mostly done by precisely time-stamping both file types and matching them post-hoc, which is why the (precise) coupling between the stimulus laptop and the fMRI PC is such a crucial point.
I hope that someone corrects me if I am wrong holding this assumption, though.


Run an analysis before you start testing. :wink: This way you see whether the coupling worked or not.

Success Jens