Welcome!

Sign in with your CogSci, Facebook, Google, or Twitter account.

Or register to create a new account.

We'll use your information only for signing in to this forum.

Supported by

all the trials of a block in a loop

JulietteJuliette Posts: 18

In my experiment, I have only one block which has 5 stimuli and I wanted to add those stimuli to the block using a loop as below:

    criteria=["ê", "ù", "û", "ü" ]

    exp = expyriment.design.Experiment(name="fontic task")
    expyriment.control.initialize(exp)

    for trials in range(0, np.shape(criteria)[0]):
        block_one = expyriment.design.Block(name="activation")
        var="trial"+str(trials)
        var = expyriment.design.Trial()
        stim = expyriment.stimuli.TextLine(text=criteria[trials])
        stim.preload()
        var.add_stimulus(stim)
        block_one.add_trial(var)
        exp.add_block(block_one)

    expyriment.control.start()

    for block in exp.blocks:
        for trial in block.trials:
            trial.stimuli[0].present()
            exp.clock.wait(1000)

    expyriment.control.end()

But I could see a strange error. I don't have any idea what this error means: I even started the code with "# -- coding: utf-8 --" but still the problem is there. here is the error

     log_txt = u"{0},{1}".format(log_txt, log_comment)
    UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 1: ordinal not in range(128)

Any comment is appreciated in advance .

Comments

  • fladdfladd Posts: 75

    Dear Juliette,

    does your editor with which you write the code also encode with UTF-8? If not, you need to set the encoding header to the encoding your editor uses.

    The error might, however, also be related to a bug in Expyriment (which will be fixed in version 0.9.0, probably released tomorrow).

    Thanked by 1Juliette

    Florian Krause (Developer)
    http://www.expyriment.org

  • JulietteJuliette Posts: 18

    Dear Florian,

    Thank you for your reply. At top of the editor I have these few lines:

        # -*- coding: utf-8 -*-
        """
        Created on Thu Feb  9 19:33:37 2017
    
        @author: Julliette
                  """
    

    up to my knowledge, these few lines is encoding the header. Am I right?

  • JulietteJuliette Posts: 18
    edited March 8

    .

  • fladdfladd Posts: 75

    Yes, they are, they tell any programme that opens the file that the method to put the characters into the file was via UTF-8, so those programmes know how to read the file. But that also means that if the method is different from what you write in the file, then this will of course not work. Hence I wanted to check whether you are sure that your editor really encodes in UTF-8, but I assume you looked that up before you put the line into the file.

    Thanked by 1Juliette

    Florian Krause (Developer)
    http://www.expyriment.org

  • JulietteJuliette Posts: 18
    edited March 8

    I even tried to put the data into an excel file and decode it:

        import pandas as np
        criteria = pd.read_excel(file, encoding='utf-16')
    

    but still the same error can be seen. I will wait for new version of expyriment tomorrow.

  • fladdfladd Posts: 75

    If you send me your script, then I can try to debug it here on my machine if you want to.

    Thanked by 1Juliette

    Florian Krause (Developer)
    http://www.expyriment.org

  • JulietteJuliette Posts: 18
    edited March 8

    Hi Florian,

    That's very nice of you. here is the full script:

        # -*- coding: utf-8 -*-
        """
        Created on Thu Feb  9 19:33:37 2017
    
        @author: Julliette
        """
    
    
        import pandas as pd
        import expyriment
        import numpy as np
        from expyriment import  misc, stimuli, design
        from expyriment.misc import constants
        from expyriment.misc._timer import get_time
    
    
        criteria=["ê", "ù", "û", "ü" ]
    
        exp = expyriment.design.Experiment(name="fontic task")
        expyriment.control.initialize(exp)
    
        for trials in range(0, np.shape(criteria)[0]):
                block_one = expyriment.design.Block(name="activation")
                var="trial"+str(trials)
                var = expyriment.design.Trial()
                stim = expyriment.stimuli.TextLine(text=criteria[trials])
                stim.preload()
                var.add_stimulus(stim)
                block_one.add_trial(var)
                exp.add_block(block_one)
    
        expyriment.control.start()
    
        for block in exp.blocks:
          for trial in block.trials:
           trial.stimuli[0].present()
           exp.clock.wait(3000)
    
        expyriment.control.end()
    

    Thanks a lot in advance

  • fladdfladd Posts: 75

    I just checked it, and it will work with the new version. I will release it tomorrow.

    Thanked by 1Julliette

    Florian Krause (Developer)
    http://www.expyriment.org

  • JulietteJuliette Posts: 18

    Hi Florian,

    Thank you for the new version of the Expyriment V. 9.0.
    I installed the new expyriment-0.9.0.tar.gz on my ubuntu but still the same error is there. Do the new version only fix the decoding on windows and not Linux?

  • fladdfladd Posts: 75
    edited March 9

    Hi Juliette,

    the new version fixes the problem on all OS.

    Important: Please do NOT install Expyriment from the source (expyriment-0.9.0.tar.gz)! Always install Expyriment as described in the installation instructions: http://docs.expyriment.org/Installation.html.

    If, after correct installation, the problem persists, then the characters have been put into the file with a different encoding than UTF-8. Which editor are you using to create the files?

    The code you pasted here in the forum worked for me, when copying and pasting into a new file. But I also know that my editor encodes in UTF-8 (actually, all editors on Linux should be doing this, except maybe IDLE or so).

    Thanked by 1Juliette

    Florian Krause (Developer)
    http://www.expyriment.org

  • fladdfladd Posts: 75

    Actually, if you see this exact error (from above):

    log_txt = u"{0},{1}".format(log_txt, log_comment)
        UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 1: ordinal not in range(128)
    

    Then it seems that you are not using the latest version of Expyriment (0.9.0), as the line in question has been changed to read:

    log_txt = u"{0},{1}".format(log_txt, byte2unicode(log_comment))
    

    (note the byte2unicode part)

    Hence, the new version of Expyriment cannot have created this error message.

    Florian Krause (Developer)
    http://www.expyriment.org

  • JulietteJuliette Posts: 18

    Hi Florian,

    Thank you for your message. You are very right! I reinstalled expyriment using the page that you mentioned and the problem is solved :)

    Thank you very much

Sign In or Register to comment.