analitics

Pages

Thursday, April 18, 2019

About psychopy tool.

A good definition for this tool can be found at the Wikipedia website:
2002: PsychoPy was originally written by Peirce as a proof of concept - that a high-level scripting language could generate experimental stimuli in real time (existing solutions, such as Psychtoolbox, had to pre-generate movies or use CLUT animation techniques).
The install of this python module is very simple:
C:\Python373\Scripts>pip install psychopy
Using this command to start this tool:
C:\Python373>psychopy
The tool starts with two graphical interfaces:
  • one for Coder area;
  • one for the project with an untitled.psyexp;
Let's see one screenshot with this tool:

This open source tool, written in the Python programming language, help you to run a wide range of neuroscience, psychology and psychophysics experiments.
I searched the internet for the capabilities of this tool and the obvious conclusion is a utility for building experiments with a wide range of applicability.
This tool lets you use the pavlovia website, see more:
Pavlovia is a place for the wide community of researchers in the behavioural sciences to run, share, and explore experiments online.
This tool used commonly-used components for linguistic experiments:
  1. Text Component (display text on the screen);
  2. Sound Component (play sounds);
  3. Keyboard Component (receive input from the keyboard);
  4. RatingScale Component (collect a numeric rating or a choice from a few alternatives, via the mouse, the keyboard or both);
  5. Code Component (insert short pieces of python code into your experiments (e.g. time stamp for the production task);
The easy way is to use the Builder tool to generate a wide range of experiments easily from the Builder using its intuitive, graphical user interface (GUI).
Today I will use the Coder with programming a simple example.
To access demos, you need to create a python script in the Coder area (use File menu - New) then use the Demos from the application menu.
Everything in a PsychoPy experiment needs a unique name.
The name must contain only letters, numbers and underscores and not contain spaces, punctuation or mathematical symbols.
This tool saves several data files: a Microsoft Excel (spreadsheet) file, a psydat file, and a log file.
The example I used today for this tutorial is how to import from a CSV file named colors_001.csv and used with psychopy python module to create stimuli:
3.3;"red"
1.1;"green"
4.4;"red"
2.2;"green"
This python script named colors.py I used to import the CSV file and use it:
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("colors_001.csv", "r",encoding="utf8")
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()
The result of this script will show you four squares colored by the CSV file.
If you don't like to use the Builder or Coder areas, then you can simply use the python:
C:\Python373>python.exe
Python 3.7.3 (v3.7.3:ef4ec6ed12, Mar 25 2019, 22:22:05) [MSC v.1916 64 bit (AMD64)]
 on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from psychopy import visual, core
pygame 1.9.5
Hello from the pygame community. https://www.pygame.org/contribute.html
>>> win = visual.Window()
>>> msg = visual.TextStim(win, text=u"Hello python users!")
>>> msg.draw()
>>> win.flip()
47.541564770566765
The output will be a window with a text message.