Difference between revisions of "PsychoPy"

From BCI2000 Wiki
Jump to: navigation, search
Line 1: Line 1:
 
PsychoPy is an open-source application using which a large number of neuroscience, psychology and psychophysics experiments can be performed [1].
 
PsychoPy is an open-source application using which a large number of neuroscience, psychology and psychophysics experiments can be performed [1].
This section first describes the two components that are developed to remotely start BCI2000 from the PsychoPy and the second section lists the steps to follow to add the new components in the PsychoPy application.
+
The documentation first describes the two components that are developed to remotely start BCI2000 from the PsychoPy, then lists the steps to follow to add the new components in the PsychoPy application and finally goes over two tutorial examples.
  
 
== Components ==
 
== Components ==
  
=== BCIStart ===
+
=== BCI2000Start ===
 
The component starts up the BCI2000 modules remotely. The component takes two parameters:
 
The component starts up the BCI2000 modules remotely. The component takes two parameters:
 
; Path : Path to the BCI2000 ''prog'' directory which contains the ''BCI2000Remote.py'' file.
 
; Path : Path to the BCI2000 ''prog'' directory which contains the ''BCI2000Remote.py'' file.
Line 11: Line 11:
 
[[File:BCI2000Start.png|alt="BCI2000 start component image showing its parameters"|BCIStart Component]]
 
[[File:BCI2000Start.png|alt="BCI2000 start component image showing its parameters"|BCIStart Component]]
  
=== BCIUpdateState ===
+
=== BCI2000UpdateState ===
 
The component defines which state to update and when. It takes the following parameters:
 
The component defines which state to update and when. It takes the following parameters:
 
; State Name : The name of the state in BCI2000 to update.
 
; State Name : The name of the state in BCI2000 to update.
Line 42: Line 42:
  
 
'''Note:''' Make sure you don't run the experiments on full screen if you are using a single screen.  
 
'''Note:''' Make sure you don't run the experiments on full screen if you are using a single screen.  
 +
 +
== Tutorial ==
 +
In this section, 2 examples are presented to help the user understand the working of the BCI2000 within PsychoPy. The examples modifies the demo stroopExtended (already provided by PsychoPy) and integrates BCI2000 within it. The stroopExtended demo asks user to press certain keys depending on the color shown on the screen and records the response time.
 +
 +
=== Using Response Component Variable Option ===
 +
In this tutorial, we will add a new state called '''correctKey''' in BCI2000 using PsychoPy. This state will be changed to 1 if the key pressed by user is correct, otherwise 0.
 +
<ol>
 +
<li>
 +
Open the stroopExtended demo in PsychoPy. This is it should looks.
 +
[[File:PsychoPy_Start.png|alt="PsychoPy Homepage with BCI2000 Components"|PsychoPy Homepage with BCI2000 Components]]
 +
</li>
 +
 +
<li>
 +
Start BCI2000 - With '''instruct''' routine on the screen, click on '''BCI2000Start''' and enter the path to the '''prog''' directory of BCI2000 (within which BCI2000Remote.py exists) in ''Path'' field and '''correctkey 0 1''' in the ''States'' field as shown in the image. '''correctKey''' is the name we want the new state to have in BCI2000, '''0''' is the default value the state should have and '''1''' is the maximum value the state can take in any case.
 +
[[File:PsychoPy_Start.png|alt="PsychoPy Homepage with BCI2000 Components"|PsychoPy Homepage with BCI2000 Components]]
 +
</li>
 +
 +
<li>
 +
Update BCI2000 State - With '''trial''' routine on the screen, click on '''BCI2000UpdateState'''. Fill in the field ''State Name'' with '''$correctKey''' (BCI2000 state we want to update; '$' is used to indicate PsychoPy to treat it as a variable) and ''Response Component Variable'' with '''$resp.corr''' ('resp' is the name of the keyboard which takes the input and 'corr' is its property that stores the whether the key pressed is correct or not; you can see this variable being used in the program when you compile it to script) as shown in the image. This will essentially update the state 'correctKey' with the value of 'resp.corr'. 
 +
[[File:PsychoPy_Start.png|alt="PsychoPy Homepage with BCI2000 Components"|PsychoPy Homepage with BCI2000 Components]]
 +
</li>
 +
 +
<li>
 +
Run the experiment. BCI2000 will start. Add the newly created state to watch (on BCI2000 operator, click on View -> States and Events.., right click on correctKey and add to watch, click Ok on States and Events screen) and proceed as with your PsychoPy experiment. The output of this experiment would be similar to as shown below. '''Note:''' Make sure you don't run the experiments on full screen if you are using a single screen.
 +
[[File:PsychoPy_Start.png|alt="PsychoPy Homepage with BCI2000 Components"|PsychoPy Homepage with BCI2000 Components]]
 +
</li>
 +
</ol>
 +
 +
=== Using Use Expression Option ===
 +
In this tutorial, we will add a new state called '''responseTime''' in BCI2000 using PsychoPy. This state will represent the time the user takes to press a key in seconds(with no decimals).
 +
<ol>
 +
<li>
 +
Open the stroopExtended demo in PsychoPy. This is it should looks.
 +
[[File:PsychoPy_Start.png|alt="PsychoPy Homepage with BCI2000 Components"|PsychoPy Homepage with BCI2000 Components]]
 +
</li>
 +
 +
<li>
 +
Start BCI2000 - With '''instruct''' routine on the screen, click on '''BCI2000Start''' and enter the path to the '''prog''' directory of BCI2000 (within which BCI2000Remote.py exists) in ''Path'' field and '''responseTime 0 600''' in the ''States'' field as shown in the image. '''responseTime''' is the name we want the new state to have in BCI2000, '''0''' is the default value the state should have and '''600''' is the maximum value the state can take in any case, i.e, the maximum response time could be 10 minutes.
 +
[[File:PsychoPy_Start.png|alt="PsychoPy Homepage with BCI2000 Components"|PsychoPy Homepage with BCI2000 Components]]
 +
</li>
 +
 +
<li>
 +
Update BCI2000 State - With '''trial''' routine on the screen, click on '''BCI2000UpdateState'''. Check on the '''Use Expression''' option to run the expression that is written in '''Expression''' field. Write the lines of code in python language in '''Expression''' field as shown in the image. These lines set the state 'responseTime' to the integer value of 'resp.rt' ('resp' is the name of the keyboard which takes the input and 'rt' is its property that stores the time it took user to press the key; you can see this variable being used in the program when you compile it to script) using the bci.SetState command. 
 +
[[File:PsychoPy_Start.png|alt="PsychoPy Homepage with BCI2000 Components"|PsychoPy Homepage with BCI2000 Components]]
 +
</li>
 +
 +
<li>
 +
Run the experiment. BCI2000 will start. Add the newly created state to watch (on BCI2000 operator, click on View -> States and Events.., right click on correctKey and add to watch, click Ok on States and Events screen) and proceed as with your PsychoPy experiment. The output of this experiment would be similar to as shown below. '''Note:''' Make sure you don't run the experiments on full screen if you are using a single screen.
 +
[[File:PsychoPy_Start.png|alt="PsychoPy Homepage with BCI2000 Components"|PsychoPy Homepage with BCI2000 Components]]
 +
</li>
 +
</ol>
 +
 +
 
== References ==
 
== References ==
* 1. [https://www.psychopy.org/ PsychoPy site]
+
1. [https://www.psychopy.org/ PsychoPy site]
  
 
== See Also ==
 
== See Also ==
 
* [[Programming Reference:BCI2000Remote Class]]
 
* [[Programming Reference:BCI2000Remote Class]]
 
* [[User Reference:Operator Module Scripting]]
 
* [[User Reference:Operator Module Scripting]]

Revision as of 20:37, 30 July 2019

PsychoPy is an open-source application using which a large number of neuroscience, psychology and psychophysics experiments can be performed [1]. The documentation first describes the two components that are developed to remotely start BCI2000 from the PsychoPy, then lists the steps to follow to add the new components in the PsychoPy application and finally goes over two tutorial examples.

Components

BCI2000Start

The component starts up the BCI2000 modules remotely. The component takes two parameters:

Path 
Path to the BCI2000 prog directory which contains the BCI2000Remote.py file.
States 
List of new states to add to the BCI2000. The states in the list are defined as follows: 'StateName defaultValue maxValue'. Eg: lastKey 0 255, correctKey 0 1.

"BCI2000 start component image showing its parameters"

BCI2000UpdateState

The component defines which state to update and when. It takes the following parameters:

State Name 
The name of the state in BCI2000 to update.
Response Component Variable 
The variable to which the state is be set OR the variable which contains the new value of the state. Note- the variable is written as $VariableName.
Use Expression 
If it is checked, the code written in 'Expression' is executed for each Frame and the variable specified is not used to update the state.
Expression 
The code to execute for each Frame. It is given for the cases, where the variable is not available and the users want more control over how the state is updated. Note- the user would have to use commands like bci.Execute and SET STATE to update the state in such cases. See Programming Reference:BCI2000Remote Class and User Reference:Operator Module Scripting for more details.

"BCI2000 update state component image showing its parameters"

Integrating BCI2000 into PyschoPy

Download the PsychoPy from the PyschoPy website.
Here is a picture of how PsychoPy looks before integrating BCI2000 in it. Notice that in the rightmost column, under Custom Components, there is no BCI2000 component.

"PsychoPy Homepage without BCI2000 Components"


Standalone version

For Windows, Copy the folders BCIStart and BCIUpdateState from src/contrib/PsychoPy Components directory of your BCI2000 repository into the folder {$PsychoPyHome}\Lib\site-packages\psychopy\experiment\components folder.


Developer version

Copy the folders BCIStart and BCIUpdateState from src/contrib/PsychoPy Components directory of your BCI2000 repository into the folder {$PsychoPyHome}\psychopy\experiment\components. Open terminal and navigate to the PsychoPy home directory where setup.py resides. Run the following command in the terminal:
pip install -e .



Start Psychopy application and in the builder view, under the Custom Components (in rightmost column), you should see the two new components - BCI2000Start and BCI2000UpdateState as shown in the figure below.

"PsychoPy Homepage with BCI2000 Components"

Note: Make sure you don't run the experiments on full screen if you are using a single screen.

Tutorial

In this section, 2 examples are presented to help the user understand the working of the BCI2000 within PsychoPy. The examples modifies the demo stroopExtended (already provided by PsychoPy) and integrates BCI2000 within it. The stroopExtended demo asks user to press certain keys depending on the color shown on the screen and records the response time.

Using Response Component Variable Option

In this tutorial, we will add a new state called correctKey in BCI2000 using PsychoPy. This state will be changed to 1 if the key pressed by user is correct, otherwise 0.

  1. Open the stroopExtended demo in PsychoPy. This is it should looks. "PsychoPy Homepage with BCI2000 Components"
  2. Start BCI2000 - With instruct routine on the screen, click on BCI2000Start and enter the path to the prog directory of BCI2000 (within which BCI2000Remote.py exists) in Path field and correctkey 0 1 in the States field as shown in the image. correctKey is the name we want the new state to have in BCI2000, 0 is the default value the state should have and 1 is the maximum value the state can take in any case. "PsychoPy Homepage with BCI2000 Components"
  3. Update BCI2000 State - With trial routine on the screen, click on BCI2000UpdateState. Fill in the field State Name with $correctKey (BCI2000 state we want to update; '$' is used to indicate PsychoPy to treat it as a variable) and Response Component Variable with $resp.corr ('resp' is the name of the keyboard which takes the input and 'corr' is its property that stores the whether the key pressed is correct or not; you can see this variable being used in the program when you compile it to script) as shown in the image. This will essentially update the state 'correctKey' with the value of 'resp.corr'. "PsychoPy Homepage with BCI2000 Components"
  4. Run the experiment. BCI2000 will start. Add the newly created state to watch (on BCI2000 operator, click on View -> States and Events.., right click on correctKey and add to watch, click Ok on States and Events screen) and proceed as with your PsychoPy experiment. The output of this experiment would be similar to as shown below. Note: Make sure you don't run the experiments on full screen if you are using a single screen. "PsychoPy Homepage with BCI2000 Components"

Using Use Expression Option

In this tutorial, we will add a new state called responseTime in BCI2000 using PsychoPy. This state will represent the time the user takes to press a key in seconds(with no decimals).

  1. Open the stroopExtended demo in PsychoPy. This is it should looks. "PsychoPy Homepage with BCI2000 Components"
  2. Start BCI2000 - With instruct routine on the screen, click on BCI2000Start and enter the path to the prog directory of BCI2000 (within which BCI2000Remote.py exists) in Path field and responseTime 0 600 in the States field as shown in the image. responseTime is the name we want the new state to have in BCI2000, 0 is the default value the state should have and 600 is the maximum value the state can take in any case, i.e, the maximum response time could be 10 minutes. "PsychoPy Homepage with BCI2000 Components"
  3. Update BCI2000 State - With trial routine on the screen, click on BCI2000UpdateState. Check on the Use Expression option to run the expression that is written in Expression field. Write the lines of code in python language in Expression field as shown in the image. These lines set the state 'responseTime' to the integer value of 'resp.rt' ('resp' is the name of the keyboard which takes the input and 'rt' is its property that stores the time it took user to press the key; you can see this variable being used in the program when you compile it to script) using the bci.SetState command. "PsychoPy Homepage with BCI2000 Components"
  4. Run the experiment. BCI2000 will start. Add the newly created state to watch (on BCI2000 operator, click on View -> States and Events.., right click on correctKey and add to watch, click Ok on States and Events screen) and proceed as with your PsychoPy experiment. The output of this experiment would be similar to as shown below. Note: Make sure you don't run the experiments on full screen if you are using a single screen. "PsychoPy Homepage with BCI2000 Components"


References

1. PsychoPy site

See Also