psychopy.org | Reference | Downloads | Github

Accept non-ascii characters from MRI trigger


#1

Hi everyone,

I am currently running PsychoPy 1.80.06 on a Macbook Pro (10.10.5) for an fMRI experiment. Our trigger sends out ‘§’ as its character when connected to Macs (but for some reason sends out ‘quoteleft’ for Windows…). Of course, when I use this as a response character, I get this error:

‘ascii’ codec can’t encode character u’\xa7’ in position 1: ordinal not in range(128)

I’m not sure how to get around this. Is there a different encoding I should be using? Can I convert just this character to another allowed ascii character?

Thanks for your help!


#2

Try adding this line # -*- coding: utf-8 -*- at the beginning of your script
It might work.
Cheers
Emanuele


#3

Hi Emanuele,

I did have # -- coding utf-8 -- at the beginning of my script. It doesn’t seem work, unfortunately…


#4

I haven’t done fMRI studies before but that error is a common issue in python2. Could you include a code snippet or at least the full error traceback?


#5

Hi Joshua,
In my case it works perfectly, the line that you wrote in the comment (# -- coding utf-8 --) would not work, have you used the correct one?
Cheers


#6

Yeah, sorry, when I pasted it in, the * didn’t appear. I have tried the correct one (the one you supplied).


#7

I should note that I have updated (for other reasons) to 1.84.0 PsychoPy2. Also this the error traceback that I get when I try to paste the character from a text file into the theseKeys = event.getKeys(keyList=[’’]) code.

Traceback (most recent call last):
File “/Applications/PsychoPy2.app/Contents/Resources/lib/python2.7/psychopy/app/coder/coder.py”, line 2654, in paste
foc.Paste()
File “/Applications/PsychoPy2.app/Contents/Resources/lib/python2.7/psychopy/app/coder/coder.py”, line 1103, in Paste
self.ReplaceSelection(txt)
File “/Applications/PsychoPy2.app/Contents/Resources/lib/python2.7/wx/stc.py”, line 3639, in ReplaceSelection
return _stc.StyledTextCtrl_ReplaceSelection(*args, **kwargs)
UnicodeDecodeError: ‘ascii’ codec can’t decode byte 0xc2 in position 0: ordinal not in range(128)


#8

Also here is the top of my code:

#!/usr/bin/env python2
# -*- coding: utf-8 -*-
from __future__ import absolute_import, division
import psychopy
psychopy.useVersion('1.84.0')

from psychopy import locale_setup, visual, core, data, event, logging, sound, gui
from psychopy.constants import (NOT_STARTED, STARTED, PLAYING, PAUSED,
                                STOPPED, FINISHED, PRESSED, RELEASED, FOREVER)
import numpy as np  # whole numpy lib is available, prepend 'np.'
from numpy import (sin, cos, tan, log, log10, pi, average,
                   sqrt, std, deg2rad, rad2deg, linspace, asarray)
from numpy.random import random, randint, normal, shuffle
import os  # handy system and path functions
import sys  # to get file system encoding

And the code that I want to be able to read the ‘§’ character:

    # *syncResp* updates
if t >= 0.0 and syncResp.status == NOT_STARTED:
    # keep track of start time/frame for later
    syncResp.tStart = t
    syncResp.frameNStart = frameN  # exact frame index
    syncResp.status = STARTED
    # keyboard checking is just starting
    win.callOnFlip(syncResp.clock.reset)  # t=0 on next screen flip
    event.clearEvents(eventType='keyboard')
if syncResp.status == STARTED:
    theseKeys = event.getKeys(keyList=['x'])

Where ‘x’ in keyList should be ‘§’…


#9

Does if work if you write u'§ instead of just '§'?


#10

Unfortunately, no. It won’t let me paste that character into the script at all, even if preceded by u’.


#11

Have you tried using chr(167) instead of §?


#12

It seems the “StyledTextCtrl” widget Builder is using does not support that character and crashes.


#13

So taking a step back, if you don’t supply an argument to get keys, and you print the keys, does it print the keys no problem, and are we sure it’s printing that symbol? (Also, how do you know your machine is sending that character, if I might ask?)

if syncResp.status == STARTED:
    theseKeys = event.getKeys()
    print(theseKeys)

I ask because I’m not sure if the problem is with entering the character in the script on your end, or if the software psychopy relies on isn’t processing the keypress correctly? I suspect it’s the former since that symbol is common on some keyboards in Europe. (And appears in the same place on the keyboard as the left quote here in the US. You can see a photo here: http://i.stack.imgur.com/aOQYU.png)

If the problem is that Psychopy’s interface isn’t letting you enter the character in your script, open the script in a different text editor, and try entering it there (as unicode u’§’). The wikipedia page has a section indicating how to enter it on the Mac https://en.wikipedia.org/wiki/Section_sign , and you could always enter it with that character application Mac has https://support.apple.com/en-us/HT201586 , search for its unicode hex: 00A7 .

Hopefully we can shake something loose.


#14

Actually probably better to try it with:

if syncResp.status == STARTED:
    theseKeys = event.getKeys()
    if theseKeys:
        print(theseKeys)

to avoid printing thousands of empty lists…


#15

Joshua, the error you’ve pasted in the original post looks like only the last line of the error. Could you post the entire thing so we can see where it’s coming from?

My guess is that this is trying to print something to the terminal, and we know that the wx output window falls over with that. But without the full stack trace (error message) we don’t know if you’re manually making any print statements or if these are being printed by the logging module


#16

Sorry for my late reply. Here is the full code when I try to paste in the character to the script. Once I can get to the scanner, I will also try daniel.riggs1 suggestion from above.

Traceback (most recent call last):
File “/Applications/PsychoPy2.app/Contents/Resources/lib/python2.7/psychopy/app/coder/coder.py”, line 2654, in paste
foc.Paste()
File “/Applications/PsychoPy2.app/Contents/Resources/lib/python2.7/psychopy/app/coder/coder.py”, line 1103, in Paste
self.ReplaceSelection(txt)
File “/Applications/PsychoPy2.app/Contents/Resources/lib/python2.7/wx/stc.py”, line 3639, in ReplaceSelection
return stc.StyledTextCtrlReplaceSelection(*args, **kwargs)
UnicodeDecodeError: ‘ascii’ codec can’t decode byte 0xc2 in position 0: ordinal not in range(128)


#17

So @jon , does your last message mean Joshua should NOT try my suggestion of printing to the console (one post before yours)?

Regardless, my suggestion about properly entering the character in a text editor still stands. Copy/pasting can sometimes come with some surprises. Depending on where you’re copying from, sometimes more than what meets the eye is copied.


#18

I know that the machine sends that specific character because I hooked up our trigger box and had it send some dummy triggers to TextEdit. When I tried to type u’§’ in TextEdit and then copy&paste to PsychoPy code, that also did not work. However, when I tried your code:

if syncResp.status == STARTED:
    theseKeys = event.getKeys()
    if theseKeys:
        print(theseKeys)

PsychoPy did accept the trigger and the program progressed as normal (and subsequent triggers did not interfere). However, it saved the character as [‘world 0’] for some strange reason.


#19

So you should get the correct result if you use ‘world 0’ instead, right?

if syncResp.status == STARTED:
    theseKeys = event.getKeys(keyList=['world 0'])

It’s important to realize that just because a key on a keyboard generates a certain character, that doesn’t mean that that’s the key’s ‘name’ as far as a computer program is concerned. For example, on my number pad, if I hit the number 7, the key that is sent according to the program psychopy uses is ‘num_7’, which is different from the key for ‘7’. Printing the keys as in the example has hopefully showed you what the actual name of the keypress is, as far as the computer program is concerned.


#20

Yep, that works! Thanks so much.