Lightweight framework for PsychoPy
Project description
PsychoPy-Scene
English | 简体中文
A lightweight experimental framework based on PsychoPy, with core source code < 150 lines.
[!NOTE] This project is in its early stages of development. Please pin the version number when using it.
Features
- Lightweight: Only 2 files, no additional dependencies.
- Type-safe: Uses generics for type inference.
- Beginner-friendly: Only requires mastering the concepts of
ContextandSceneto get started.
Installation
pip install psychopy-scene
Quick Start
Experimental Context
The Context represents the global parameters of the experiment, including environmental and task parameters.
The first step in writing an experiment is to create the experimental context.
from psychopy import visual, data
from psychopy_scene import Context
ctx = Context(win=visual.Window(), exp=data.ExperimentHandler())
Scene
An experiment can be thought of as a combination of a series of scenes. Writing an experimental program requires only 2 steps:
- Create the scene.
- Write the scene presentation logic.
Create scenes using decorators:
from psychopy import visual
from psychopy.hardware import keyboard
from psychopy_scene.decorator import duration, hardware_keyboard
# create stimulus
stim_1 = visual.TextStim(ctx.win, text="Hello")
stim_2 = visual.TextStim(ctx.win, text="World")
# create scene
@duration(1)
@ctx.scene
def demo_1(color: str, ori: float):
print('it will be called before first flip')
stim_1.color = color
stim_2.ori = ori
return stim_1, stim_2
@close_on('key_space')
@hardware_keyboard()
@ctx.scene
class demo_2:
scene: Scene
def __call__(self, text: str):
stim_1.text = text
return stim_1
def on_key_space(self, evt: keyboard.KeyPress):
self.scene.data['rt'] = evt.tDown - self.scene.data['frame_times'][0]
# show scene
data_1 = demo_1.show(color="red", ori=45)
data_2 = demo_2.show(text="test")
Some decorators can be overridden, which is useful in scenarios like scenes with variable durations:
@duration(1)
@ctx.scene
def demo():
return stim
data = demo.use(duration(0.5)).show()
Data
Data is automatically collected during the scene presentation:
| Name | Description |
|---|---|
| frame_times | Timestamps for each frame flip |
We can access this data via scene.data:
@close_on('key_f', 'key_j')
@hardware_keyboard()
@ctx.scene
def demo():
return stim
data = demo.show()
show_time = data["frame_times"][0]
We can also collect data manually:
@hardware_keyboard()
@ctx.scene
class demo:
scene: Scene
def __call__(self):
return stim
def on_key_f(self, evt: keyboard.KeyPress):
self.scene.data['pressed_duration'] = evt.duration
data = demo.show()
duration = data['pressed_duration']
Events
Events represent specific timings during program execution, such as key presses or mouse clicks. To perform operations when an event occurs, we need to add callback functions to the events.
Available event types are provided by decorators: hardware_keyboard, event_mouse.
These events will be triggered during the poll phase of the scene lifecycle:
graph LR
Initialization --> `show` --> First_Draw --> `flip` --> c{Keep Drawing?}
c -->|No| Stop_Drawing
c -->|Yes| Timing_Check --> `frame` --> Redraw --> `poll` --> c
Examples
Trial
from psychopy import visual
from psychopy_scene import Context
from psychopy_scene.decorator import duration
def task(ctx: Context, sec = 1):
stim = visual.TextStim(ctx.win, text="")
scene = ctx.scene(lambda: stim).use(duration(sec))
data = scene.show()
ctx.record(time=data['frame_times'][0])
Block
from psychopy import visual
from psychopy_scene import Context
from psychopy_scene.decorator import duration
def task(ctx: Context):
stim = visual.TextStim(ctx.win, text="")
scene = ctx.scene(lambda: stim).use(duration(1))
data = scene.show()
ctx.record(time=data['frame_times'][0])
win = visual.Window()
data = []
for block_index in range(10):
ctx = Context(win)
ctx.exp.extraInfo['block_index'] = block_index
task(ctx)
block_data = ctx.exp.getAllEntries()
data.extend(block_data)
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file psychopy_scene-0.3.0.tar.gz.
File metadata
- Download URL: psychopy_scene-0.3.0.tar.gz
- Upload date:
- Size: 17.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
55bc12ce2895635bb7eb9a03da8730480f2e99645f3bb6681250bea7e94a9583
|
|
| MD5 |
75dd215788a36a223618cfc566e45f2d
|
|
| BLAKE2b-256 |
4f19e6ad4cd5d4d0638dbdd71f6855488de8ffb3752b5e04d923d92aad3a2736
|
Provenance
The following attestation bundles were made for psychopy_scene-0.3.0.tar.gz:
Publisher:
pypi.yaml on bluebonesx/psychopy-scene
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
psychopy_scene-0.3.0.tar.gz -
Subject digest:
55bc12ce2895635bb7eb9a03da8730480f2e99645f3bb6681250bea7e94a9583 - Sigstore transparency entry: 1244124425
- Sigstore integration time:
-
Permalink:
bluebonesx/psychopy-scene@ce4ce120884325050ebd1ea543f5ec5554347057 -
Branch / Tag:
refs/tags/0.3.0 - Owner: https://github.com/bluebonesx
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
pypi.yaml@ce4ce120884325050ebd1ea543f5ec5554347057 -
Trigger Event:
push
-
Statement type:
File details
Details for the file psychopy_scene-0.3.0-py3-none-any.whl.
File metadata
- Download URL: psychopy_scene-0.3.0-py3-none-any.whl
- Upload date:
- Size: 18.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
51c12a4f853b2902b68244f353b572bd2fe554c0beebd8b72dfcf2e42d52149e
|
|
| MD5 |
8272f60350cd7bbab83f6a513bbf7949
|
|
| BLAKE2b-256 |
36578b8b693b3e7f984447fc1252f2285b6fe64074887dca46d512bd8868df2f
|
Provenance
The following attestation bundles were made for psychopy_scene-0.3.0-py3-none-any.whl:
Publisher:
pypi.yaml on bluebonesx/psychopy-scene
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
psychopy_scene-0.3.0-py3-none-any.whl -
Subject digest:
51c12a4f853b2902b68244f353b572bd2fe554c0beebd8b72dfcf2e42d52149e - Sigstore transparency entry: 1244124441
- Sigstore integration time:
-
Permalink:
bluebonesx/psychopy-scene@ce4ce120884325050ebd1ea543f5ec5554347057 -
Branch / Tag:
refs/tags/0.3.0 - Owner: https://github.com/bluebonesx
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
pypi.yaml@ce4ce120884325050ebd1ea543f5ec5554347057 -
Trigger Event:
push
-
Statement type: