Search code examples
pythonloopsfor-looprectanglesrect

Simple rectangle experiment in psychopy


enter image description hereI am trying to adapt an existing script to create a very simple experiment in psychopy in which the aim is that a rectangle with different colours and dimensions loops through the screen.

<**from psychopy import core, visual, event
import csv
  
## Setup section, read experiment variables from file
win = visual.Window([400,300], monitor="testMonitor", units="cm", fullscr=False)
stimuli = []
datafile = open("stimuli.csv", "rt")
reader = csv.reader(datafile, delimiter=";")
for row in reader:
    if len(row)==2:         # ignore empty and incomplete lines
        size = float(row[0])  # the first element in the row converted to a floating point number
        color = row[1]        # the second element in the row
        stimulus = visual.Rect(win, width=size, height=size)
        stimulus.fillColor = color
        stimuli.append(stimulus)
datafile.close()
  
## Experiment Section, use experiment variables here
for stimulus in stimuli:
    stimulus.draw()
    win.flip()
    core.wait(1.000)
 
## Closing Section
win.close()
core.quit(**)>

I also have an excel file in csv formt which contains the dimensions (in first column) and color (second color) of the rectangel

However, I get the following error:

“File “C:\Users\USER\OneDrive\Área de Trabalho\psychopy\new_coding\untitled.py”, line 11, in

size = float(row[0]) # the first element in the row converted to a floating point number

ValueError: could not convert string to float: ‘3’

Experiment ended.

I attach the script and the excel files (which contains the rectangle dimensions and colours). I have tried several options including changing the dimensions in the excel files without decimals but the error message held.


Solution

  • I see no problem in your code.

    Most likely the problem is in your CSV file. I think it was saved with encoding UTF-8 with BOM-signature. Try to change one line in your code:

    datafile = open("stimuli.csv", "rt")
    

    to

    datafile = open("stimuli.csv", "r", encoding="utf-8-sig")