Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Supported by

How Collect RF Pulse (Collecting Keyboard Responses Anytime)

I'm trying to build an experiment that collects responses in fMRI environment. However, I also do need to collect the pulses from the MRI machine itself. The machine will send a keyboard input every 2 seconds and I need to be able to collect it and write it to a log file. I am kind of clueless in this regard.

Comments

  • Hi @Xeonen ,

    In principle there's no difference between recording keyboard responses from the fMRI machine and those that come from the participant. They will (at least on most systems) simply be exposed as key presses to the software. How and whether to deal with this depends on the details of the experiment though.

    Are these 'pulse' key presses simply emitted every 2 s as a steady pulse? In that case, you could consider only registering the timestamp of the first pulse, because that's all the information you need. (In fact, this is exactly what's often done in fMRI experiments.) Or do the 'pulse' key presses actually convey important timing information, for example because they indicate the onset of something in a way that is not fully predictable?

    There's no general purpose solution here; the details matter! And so you'll have to provide a lot more information! I would also ask the lab technician (or experienced lab members) how they usually do this (even if they use other software). Labs tend to have their idiosyncratic ways of implementing these kinds of things, and it makes sense to stick to that, at least if you're just starting out.

    — Sebastiaan

  • Hello there @sebastiaan !

    Yes, the pulse key presses are emitted every 2 seconds as a steady pulse. However, if machine "hicks-up" the delay will change. These keypresses will be used to sync the actual timing with the images produced by the machine.

    In general, there is an experiment running on the background. Participant will be shown stimuli and they will grade the stimuli using a mouse. In this regard, there will be other keypresses and mouse clicks. What confused me is that OpenSesame records keypresses once for each keypress object, logger also logs them only when it runs. (timeline passes over the logger). I don't have a clue on how to add a logger and keypress object that will write to a separate csv file each time that pulse "key" is pressed.

    I tried to upload the experiment but I got 413 error. I will be sharing the Google Drive link.

    Lastly, thanks a lot for the reply. I was losing hope when I posted on the forum.

    Kind regards,

    Salih Fırat Canpolat

    https://drive.google.com/file/d/1C1n6hwk-2FBDqth4CWYfoDpE0ydMMcE5/view?usp=sharing

  • Hi @Xeonen ,

    It sounds like you want OpenSesame to behave normally, except that in the background the program is also continuously monitoring for, and responding to, presses of a specific key that is used to send pulses. That's a bit unusual, but it's possible.

    The easiest way to do this is by applying a so-called decorator to the function that PsychoPy uses to capture key presses (event._onPygletKey). Below you see a script that does this; this script should be in the prepare phase of an inline_script at the very start of the experiment. You can watch these videos if you want to know more about decorators, but the general logic is that a function (_onPygletKey in this case) is replaced with another function that adds some functionality, in this case printing out a message to the console whenever the k is pressed. Of course, in your case you want to change this so that a timestamp is written to a file whenever the key is pressed that the fMRI devices uses to signal pulses.

    Hope this helps!

    — Sebastiaan

    from psychopy import event
    
    
    def capture(fnc):
    
        def inner(symbol, modifiers, emulated=False):
            if symbol == 107:  # ASCII code for k
                print('The k was pressed')
            return fnc(symbol, modifiers, emulated=emulated)
    
        return inner
    
    
    event._onPygletKey = capture(event._onPygletKey)
    

    PS. Sorry for the slow response. I hope the issue is still relevant.

Sign In or Register to comment.