Skip to main content

Lightweight framework for PsychoPy

Project description

PsychoPy-Scene

PyPI - Version PyPI - Downloads

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 Context and Scene to 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:

  1. Create the scene.
  2. 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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

psychopy_scene-0.3.0.tar.gz (17.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

psychopy_scene-0.3.0-py3-none-any.whl (18.0 kB view details)

Uploaded Python 3

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

Hashes for psychopy_scene-0.3.0.tar.gz
Algorithm Hash digest
SHA256 55bc12ce2895635bb7eb9a03da8730480f2e99645f3bb6681250bea7e94a9583
MD5 75dd215788a36a223618cfc566e45f2d
BLAKE2b-256 4f19e6ad4cd5d4d0638dbdd71f6855488de8ffb3752b5e04d923d92aad3a2736

See more details on using hashes here.

Provenance

The following attestation bundles were made for psychopy_scene-0.3.0.tar.gz:

Publisher: pypi.yaml on bluebonesx/psychopy-scene

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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

Hashes for psychopy_scene-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 51c12a4f853b2902b68244f353b572bd2fe554c0beebd8b72dfcf2e42d52149e
MD5 8272f60350cd7bbab83f6a513bbf7949
BLAKE2b-256 36578b8b693b3e7f984447fc1252f2285b6fe64074887dca46d512bd8868df2f

See more details on using hashes here.

Provenance

The following attestation bundles were made for psychopy_scene-0.3.0-py3-none-any.whl:

Publisher: pypi.yaml on bluebonesx/psychopy-scene

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page