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

Dispaly stimuli in blocks

Hi Florian,

I have an excel file consist of 10x3 elements (rows=10). In the code below, I read each line of the excel file and add 3 stimuli to the block. In each block, I would like to only display 5 stimuli. But with the code below I only display each stimuli 2 times. I know it's wrong. Any advice about how I can allocate first five stimuli to block one and second five stimuli to block number 2 and so on, is appreciated.

    for blocks in range(0,2):
       for trial_num in range(0, rows):

              data_list = [df.iloc[trial_num , 1], df.iloc[trial_num , 2],
                           df.iloc[trial_num , 3]]
              key = "Trial{}".format(trial_num )
              words[key] = data_list
              block_num="block"+str(blocks)
              block_num= expyriment.design.Block(name="activation")
              var="trial"+str(trial_num )
              var = expyriment.design.Trial()
              stim1 = expyriment.stimuli.TextLine(text=words[key][0], position=(0,200),text_size=32)
              stim2 = expyriment.stimuli.TextLine(text=words[key][1], position=(-230,-230),text_size=32)
              stim3 = expyriment.stimuli.TextLine(text=words[key][2], position=(230,-230),text_size=32)  
              canvas = expyriment.stimuli.Canvas(size=(600, 600))
              stim1.plot(canvas) 
              stim2.plot(canvas) 
              stim3.plot(canvas)
              canvas.preload() 
              var.add_stimulus(canvas)
              block_num.add_trial(var)
              exp.add_block(block_num)

Comments

  • fladdfladd Posts: 75

    Hi Juliette,

    how about this:

    # Save the excel file as .csv file (in Excel), then read it easily in Expyriment
    data = []
    with open("data.csv") as f:
        for line in f:
            data.append(line.strip().split(","))
    
    # Loop over sublists of 5 trials trials and create the design
    for trials in [data[x:x+5] for x in range(0, len(data), 5)]:
        b = expyriment.design.Block()
        for trial in trials:
            t = expyriment.design.Trial()
            stim1 = expyriment.stimuli.TextLine(text=trial[0], position=(0,200), text_size=32)
            stim2 = expyriment.stimuli.TextLine(text=trial[1], position=(-230,-230), text_size=32)
            stim3 = expyriment.stimuli.TextLine(text=trial[2], position=(230,-230), text_size=32)  
            canvas = expyriment.stimuli.Canvas(size=(600, 600))
            stim1.plot(canvas)    
            stim2.plot(canvas) 
            stim3.plot(canvas)
            canvas.preload() 
            t.add_stimulus(canvas)
            b.add_trial(t)
        exp.add_block(b)
    

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

  • JulietteJuliette Posts: 18
    edited March 13

    Dear Florian,

    Thank you for your comment. There is a problem about converting excel file to csv file. It seems Python can't encode French accents to csv file and therefore it cant display the word in canvas. Although, I might be wrong. I only increased one line to your comment

    df = pd.read_excel(file, encoding='utf-16')
       words=df.to_csv('se_de1.csv', encoding='utf-16')
       data=[]
       with open("se_de1.csv") as f:
               for line in f:
                      data.append(line.strip().split(","))
    
          Error: UnicodeEncodeError: 'latin-1' codec can't encode characters in position 0-1: ordinal not in    
           range(256)  
    
  • fladdfladd Posts: 75

    Hi Juliette,

    instead of open("se_de1.csv"), try to import codecs and then codecs.open("se_de_1.csv", encoding="utf-16"), and see whether that works.

    Thanked by 1Juliette

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

Sign In or Register to comment.