Umlaut in PsychoPy

820 views
Skip to first unread message

Fran

unread,
Aug 26, 2012, 10:03:00 AM8/26/12
to psychop...@googlegroups.com
Hello,

I've just finished programming an experiment using the coder with the latest version of PsychoPy on a Mac. (It's supposed to run on a PC in the future.) In the experiment, the participant will be asked to enter text which is displayed on the screen as they type it; this input is also saved to an external file. However, the experiment is in German, so sooner or later the participant will enter an umlaut or the 'ß'. Is there a way to implement this in PsychoPy? At the moment I think the program assumes that I'm using a British/American keyboard layout.

Cheers,
Fran

Jeremy Gray

unread,
Aug 26, 2012, 12:48:50 PM8/26/12
to psychop...@googlegroups.com
Hi Fran,

PsychoPy has a lot of support for unicode built-in already, and aims
to do so completely. If something does not work someone can typically
provide a fix quickly (just send a detailed error report to the list
here). But I think there's a good chance that it will just work
without your needing to do anything extra. So the key thing is to just
try it out, in my opinion. You might try setting your locale to
non-English, and generate an umlaut or other char that way (not sure
how easy this is).

best,

--Jeremy
> --
> You received this message because you are subscribed to the Google Groups
> "psychopy-users" group.
> To post to this group, send email to psychop...@googlegroups.com.
> To unsubscribe from this group, send email to
> psychopy-user...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/psychopy-users/-/BylXLMPXq7sJ.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>

Fran

unread,
Aug 27, 2012, 3:40:38 PM8/27/12
to psychop...@googlegroups.com
Hi Jeremy,

thanks for the reply! I've set the locale to German; I also used the coding

#!/usr/bin/env python
#-- coding: UTF-8 --

However, when I put an umlaut into a message to the participant, e.g.

initmessage= visual.TextStim(win, pos=(0.0,0.0),color=[1,1,1],text='Fortfahren mit beliebiger Taste ü...')

I get the following error message:

SyntaxError: Non-ASCII character '\xc3' in file /Users/Fran/blur_basic.py on line 42, but no encoding declared; see http://www.python.org/peps/pep-0263.html for details

When the user tries to enter an umlaut, I don't get an error message, but instead of 'ü' being displayed it'll be 'bracketleft' and so on - y and z are also switched, so this is apparently an issue with the keyboard. This is only the case when I try to add an umlaut as a string to a TextStim; when I enter umlauts into a dialogue box, it'll work fine. I'm a bit puzzled why it would work fine with the dialogue box but not with everything else - any suggestions?

Take care,
Fran

Jeremy Gray

unread,
Aug 27, 2012, 3:48:02 PM8/27/12
to psychop...@googlegroups.com
Hi Fran,

I have no idea how to deal with the German keyboard y - z swap issues.
But fortunately the TextStim one is easy though. Instead of
text='... ü...'
you want a unicode string, that starts with a u outside the quotes like this:
text=u'... ü...'

so this works:
initmessage= visual.TextStim(win,
pos=(0.0,0.0),color=[1,1,1],text=u'Fortfahren mit beliebiger Taste
ü...')

I am not completely sure how much this will clean up other issues for
you (like the 'bracketleft' part). Let us know.

--Jeremy
> --
> You received this message because you are subscribed to the Google Groups
> "psychopy-users" group.
> To post to this group, send email to psychop...@googlegroups.com.
> To unsubscribe from this group, send email to
> psychopy-user...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/psychopy-users/-/ilMMhek9AOsJ.

Fran

unread,
Aug 28, 2012, 4:24:16 PM8/28/12
to psychop...@googlegroups.com
Hi,

thank you, that at least solved that particular issue... ;)

After some extensive Googling, I've come to the conclusion that my issue may have something to do with pyglet. I've read that pyglet accepts unicode input but I've no idea whether it does so automatically (it doesn't seem to in my case anyway) or whether I have to set it to read in unicode.




Jonathan Peirce

unread,
Aug 29, 2012, 4:55:33 AM8/29/12
to psychop...@googlegroups.com
OK, I think the attached script tells you how to handle this manually
(at least in code).

For info, this is the situation:
Pyglet has a pair of mechanisms for handling keyboard inputs:
on_key_press and on_text.
The former returns a code about what physical key on the keyboard was
pressed, which could be a letter, or something else (Ctrl, Shift, left,
right etc...) but that doesn't know how to handle unicode because they
aren't key names.
The on_text method (demonstrated in the script) doesn't tell you
anything about the physical keys that were pressed but reports what
unicode text it should represent on the current system. The problem with
that is it doesn't help you with cursor keys or modifiers because they
don't result in a text output.
Sebastiaan Mathot recently contributed a way to try and combine these
events into the key handling of psychopy so that it tried to use
on_key_press first and reverted to on_text if that failed (it was
released in 1.74.00). That way, possibly, we could have unicode (and
caps) text returned, but still be able to read the cursor keys (I
personally use the cursor keys a lot in subject responses). Possibly we
can adjust that to handle your situation, e.g. by swapping the priorities.

Could you just confirm that you're using version 1.74.xx?

cheers,
jon
> --
> You received this message because you are subscribed to the Google
> Groups "psychopy-users" group.
> To post to this group, send email to psychop...@googlegroups.com.
> To unsubscribe from this group, send email to
> psychopy-user...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/psychopy-users/-/9SRME-dn9UsJ.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>

--
Jonathan Peirce
Nottingham Visual Neuroscience

http://www.peirce.org.uk/

unicodeTextInput.py

Fran

unread,
Aug 30, 2012, 2:23:01 PM8/30/12
to psychop...@googlegroups.com
Hi Jon,

thank you very much for your reply! I'm indeed using the latest version of PsychoPy, which is 1.74.03 I think.

I've seen the posts by Sebastiaan Mathot and I suspected that I might be having the same problem, but I couldn't figure out how to incorporate this into my own code. However with the code you've attached I think I'll get it to work. Thank you very much indeed!

Take care,
Fran

Fran

unread,
Aug 30, 2012, 3:40:46 PM8/30/12
to psychop...@googlegroups.com
I think I've got it to work now... One more question. I first tried to adapt my original code, which contained event.getKeys(). For the attached code that would look like this:

while True:
    for lastKey in event.getKeys():
        #update key if needed
        if previousKey != lastKey:
            textStim.setText('last key: %s' %lastKey)
            previousKey = lastKey
        #draw the stimulus
        textStim.draw()
        win.flip()

(Ignoring the fact that I've just created an infinite loop.) However, in this case it won't work and the output reverts to the original on_key_press mechanism. Do you know why?


Take care,
Fran

On Wednesday, August 29, 2012 10:55:36 AM UTC+2, Jon wrote:

Horea Christian

unread,
Aug 19, 2013, 3:16:28 PM8/19/13
to psychop...@googlegroups.com
Any news on this? I would also like to use umlauts and u'my ümläut text here' won't work. Using version 1.78.00 with this code here  (text input at line 39ff).

Jonathan Peirce

unread,
Aug 20, 2013, 5:00:04 AM8/20/13
to psychop...@googlegroups.com
How about if you add the line
# -*- coding: utf-8 -*-
Or could you (as always) provide the actual error rather than saying "it won't work". PsychoPy can definitely present unicode characters as text stimuli.

cheers,
Jon
To unsubscribe from this group and stop receiving emails from it, send an email to psychopy-user...@googlegroups.com.

To post to this group, send email to psychop...@googlegroups.com.

For more options, visit https://groups.google.com/groups/opt_out.

-- 
Jonathan Peirce
Nottingham Visual Neuroscience

http://www.peirce.org.uk

This message and any attachment are intended solely for the addressee and may contain confidential information. If you have received this message in error, please send it back to me, and immediately delete it.   Please do not use, copy or disclose the information contained in this message or in any attachment.  Any views or opinions expressed by the author of this email do not necessarily reflect the views of the University of Nottingham.

This message has been checked for viruses but the contents of an attachment may still contain software viruses which could damage your computer system, you are advised to perform your own checks. Email communications with the University of Nottingham may be monitored as permitted by UK legislation.


Horea Christian

unread,
Aug 20, 2013, 11:17:18 AM8/20/13
to psychop...@googlegroups.com
Sure, I added the UTF line to both start.py and experiments.py now (new code as seen here). The exact error output is: 

chymera@desktophost ~/src/faceRT $ python "start.py"
/usr/lib64/python2.7/site-packages/psychopy/gui.py:45: wxPyDeprecationWarning: Using deprecated class PySimpleApp.
  app
= wx.PySimpleApp()
moved pre
-existing data file /home/chymera/src/faceRT/results/chr.csv to backup location (/home/chymera/src/faceRT/results/.backup/chr_2013-08-20_171316.csv)
Traceback (most recent call last):
 
File "start.py", line 52, in <module>
    em_faces
(win, expInfo, fixation, fixationtime, trialClock, local_dir)
 
File "/home/chymera/src/faceRT/experiments.py", line 43, in em_faces
   
\n\nFortfahren mit beliebiger Taste.',wrapWidth=20.0)
  File "/usr/lib64/python2.7/site-packages/psychopy/visual.py", line 4317, in __init__
    self.setText(text, log=False) #self.width and self.height get set with text and calcSizeRednered is called
  File "/usr/lib64/python2.7/site-packages/psychopy/visual.py", line 4399, in setText
    self.text = unicode(text)
UnicodeDecodeError: '
ascii' codec can't decode byte 0xc3 in position 1: ordinal not in range(128)
6.0520 WARNING User requested fullscreen with size [1920 1080], but screen is actually [2048, 1152]. Using actual size
AL lib
: (EE) alc_cleanup: 1 device not closed


Previously (with the code from my last post) it was:

chymera@desktophost ~/src/faceRT $ python "start.py"
Traceback (most recent call last):
 
File "start.py", line 7, in <module>
   
from experiments import em_faces
 
File "/home/chymera/src/faceRT/experiments.py", line 39
SyntaxError: Non-ASCII character '\xc3' in file /home/chymera/src/faceRT/experiments.py on line 39, but no encoding declared; see http://www.python.org/peps/pep-0263.html for details

Many thanks,

Jonathan Peirce

unread,
Aug 20, 2013, 11:30:11 AM8/20/13
to psychop...@googlegroups.com
OK, that helps. You need both the encoding thing at the top of the file (or you get the lower error) and you have to specify each string in your script as being u (or you get the upper error). I suspect the string you've got that ends with "Fortfahren mit beliebiger Taste." doesn't start with a u. Sorry, I know this is a pain, but it's a historical part of Python itself and I can't make it go away! ;-)

(Note for Builder users - don't worry about any of this crazy stuff! Builder should do it all for you I think :-) )

cheers,
Jon

For more options, visit https://groups.google.com/groups/opt_out.

Radka Jersakova

unread,
Nov 4, 2013, 11:45:09 AM11/4/13
to psychop...@googlegroups.com
Hello,

I have a similar problem but unfortunately do not quite follow the solution. I am programming a memory experiment in coder using french words. I have a simple text file with all the words that I then open in psychopy and create a list with all the stimuli. However it does not want to display the words with special letters in it and instead I get the classic error message

UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 1: ordinal not in range(128)

Is there an easy way to fix this problem?

Thank you,

Radka

global...@gmail.com

unread,
Nov 23, 2013, 4:23:39 PM11/23/13
to psychop...@googlegroups.com
In my experience, it's best to avoid importing from .txt or .csv files if stimuli contain umlauts or other non-ASCII characters. Instead, importing from an Excel (xlsx) file works fine. The file has to be created with Microsoft Excel though; PsychoPy often crashes/freezes while trying to open .xlsx files created with OpenOffice/LibreOffice.

Follow-up question: Is it possible to define non-ASCII key inputs in the builder view? It would be really helpful if I could define u'ä', u'ö', u'ü' as a valid key, but PsychoPy throws UnicodeDecodeErrors or doesn't compile the script at all.

Regards,
Jan

Jonathan Peirce

unread,
Nov 24, 2013, 7:19:56 AM11/24/13
to psychop...@googlegroups.com
Specifying them in Builder shouldn't be a problem (if you give us the complete error message we can probably fix - actually we've handled this already a fair bit so it might be fixed in the repository, awaiting release).

The bigger problem is that the current default key-handling system from pyglet doesn't look at the character resulting from a key press but looks at the name of the key(s) that are pressed. 'ä' is not the name of a key but the result of a combination of keys being pressed. Sol's new iohub system is able to return either the key identities or the unicode chars they represent so in the longer term we will be able to handle this case, but currently accessing that needs code (and is a little developmental still).

cheers,
Jon

For more options, visit https://groups.google.com/groups/opt_out.

global...@gmail.com

unread,
Nov 26, 2013, 11:32:19 AM11/26/13
to psychop...@googlegroups.com
Hi Jon, thanks for your explanation. I'm using the key response, a TextStim and some scripting to generate a little text input stimulus in which participants are supposed to enter one word during a trial. It works fine, but not being able to type umlauts is a bit distracting for German participants, especially with a time limit involved.

The minimalist example would be creating an empty Builder experiment and adding only a key response with 'ä' (or u'ä') as the allowed key. Running the experiment (in 1.78.01/Windows) fails with this message:

Error when generating experiment script:
'ascii' codec can't encode character u'\xe4' in position 1: ordinal not in range(128)

Regards,
Jan

benjami...@uni-graz.at

unread,
Dec 22, 2015, 10:50:02 AM12/22/15
to psychopy-users
A comment on what you stated concerning umlauts and defining every single piece of text in the script with  u'...' .

if you add the following code at the very beginning of your experiment, then you can specify strings without the preceding "u"

import sys  
reload
(sys)  
sys
.setdefaultencoding('utf8')

A text stimulus defined like textstim1 = 'Hallöle' should work now.



Am Dienstag, 20. August 2013 17:30:11 UTC+2 schrieb Jon:
...You need both the encoding thing at the top of the file (or you get the lower error) and you have to specify each string in your script as being u (or you get the upper error).



On 20/08/2013 16:17, Horea Christian wrote:
Sure, I added the UTF line to both start.py and experiments.py now (new code as seen here). The exact error output is: 

Reply all
Reply to author
Forward
0 new messages