Recording Onset Time in MRI Scanner

PsychoPy version (1.83.04):

I have set up an n-back task that will be completed in an MRI scanner using the Builder view which has worked well. So far I have set it up so that a fixation cross appears until a ‘5’ is pressed as that is the signal from the MRI and after that, a new routine starts where I have a fixation cross appear for 10 seconds followed by the actual trials of the n-back.

The only problem is that the Excel data output file only measures the reaction time of when an answer is given within the 2s trial, however, I also need to record the time each response is given (by pressing the arrow key) relative to the first signal from the MRI (i.e. I need a column in the output file saying at what time a participant responded relative to the first 5 key press).

Is there any way to do this in Builder View? If not, do you know what code I could use to set this up?

Thanks!

I may be wrong so others can correct me, when you use builder you are stuck with the clock that is generated automatically at the beginning of the experiment which means if your actual data collection begins later there will be a delay.

So if you want to find out when you received your first TR, you can either add a clock variable right after you receive your trigger under a code component:

trigger_clock = core.Clock()

and use this code whenever you detect a key press:

thisExp.addData('trigger_time',exp_clock.getTime()) to save it as a column.

However, instead of doing that it may be a better idea to just record the very moment the trigger key is pressed as the lag between the experiment start and the start of actual data collection and subtract this value from all of your timing related data columns when you are creating your onset/event files because in the future you may need to use onset/offset times for your fMRI analysis as well.

Thank you for your help! I tried your first suggestion and it added a column measuring time, however, the values did not make sense based on the response time. Given that I have a 10-second fixation trial the first value should be 10 seconds plus the response time for that trial and that is not what I got. I tried to incorporate the code in the builder view using the code component but maybe I put it in the wrong tab.
I tried including the line “trigger_clock = core.Clock()” at the ‘end of the routine’ tab within the routine titled fixationCross as that ends when the 5 key press is detected. I included the second code component under the ‘each frame’ tab of my wordRoutine (where each trial is 2 seconds and participants can respond anytime within that 2s). The timestamps recorded were roughly 2s apart so I believe it measures when the trail ends as opposed to when a participant responds within that trail. Do you have any recommendations to fix this?

Also, I was hoping you could clarify how I could record the time of the trigger key (now it just shows me when the trigger key is pressed within that routine).

Also, I was hoping you could clarify how I could record the time of the trigger key (now it just shows me when the trigger key is pressed within that routine).

Well, your trigger_key.stopped will correspond to the very moment that trigger key has been pressed, so you can use this offset time to subtract from all of your onsets and offsets.

I tried including the line “trigger_clock = core.Clock()” at the ‘end of the routine’ tab within the routine titled fixationCross as that ends when the 5 key press is detected

If your fixationCross is at the same routine as your trigger key that makes sense. Otherwise, you should place this part under the same routine where your trigger key is defined, as a code component under the “end routine” tab.

I included the second code component under the ‘each frame’ tab of my wordRoutine (where each trial is 2 seconds and participants can respond anytime within that 2s).

No, you should place it under “End routine” tab of your n-back trial (where the participant presses a key to the words they view). For context, each frame happens while the routine is running (it is called on each screen refresh – which is not something you want)

Hope this helps.