Creating a Custom Stimulus: Dot Example¶
This guide explains how to create a custom stimulus in Ethopy by examining the dot.py example, which implements a simple dot stimulus for visual experiments.
Overview of the Dot Stimulus¶
The Dot stimulus displays a configurable dot (rectangular or oval) on the screen at specified coordinates and sizes. This type of stimulus is commonly used in:
- Visual attention studies
- Simple detection tasks
- Eye movement tracking experiments
- Timing response experiments
Defining the Stimulus Database Table¶
Each stimulus requires defining a database table to store its parameters. Here's how the Dot stimulus defines its table:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | |
This table definition:
- Uses the
@stimulus.schemadecorator to associate with the database - Inherits from both
Stimulus(base class) anddj.Manual(DataJoint table class) - Defines a foreign key relationship with
stimulus.StimCondition(parent table) - Specifies parameters for the dot's appearance:
- Background and dot colors
- Position (x, y coordinates as fractions of screen width)
- Size (width and height as fractions of screen width)
- Shape (rectangular or oval)
- Duration (how long the dot persists)
Implementing the Stimulus Class¶
The Dot class implements several key methods to control the stimulus lifecycle:
1 2 3 4 5 6 7 | |
1. __init__() method¶
This initializes the stimulus and defines:
self.cond_tables: List of condition tables this stimulus uses (just 'Dot' in this case)self.required_fields: Parameters that must be provided for this stimulusself.default_key: Default values for optional parameters
2. prepare() method¶
1 2 3 4 5 6 7 8 9 10 11 12 | |
This method:
- Sets the background color
- Calculates the dot's position and size based on:
- Monitor resolution (to maintain aspect ratio)
- Condition parameters for position and size
- Conversion from normalized coordinates to actual screen coordinates
- Creates a rectangle tuple (left, top, right, bottom) for drawing
3. start() method¶
1 2 3 | |
This method:
- Calls the parent class's start method (which initializes timing)
- Draws the rectangle using the precalculated coordinates and the specified color
4. present() method¶
1 2 3 4 | |
This method:
- Checks if the dot's display time has elapsed (converting seconds to milliseconds)
- If the time has elapsed:
- Marks the stimulus as no longer in operation
- Fills the screen with the background color (removing the dot)
5. stop() and exit() methods¶
1 2 3 4 5 6 7 8 | |
These methods handle cleanup:
- stop(): Called when the stimulus needs to be stopped during operation
- Logs the stop event
- Clears the screen
- Marks the stimulus as no longer in operation
exit(): Called when the experiment is ending- Clears the screen
- Calls the parent class's exit method for additional cleanup
Stimulus Lifecycle¶
The dot stimulus follows this lifecycle:
- Initialization: Set up parameters and default values
- Preparation: Calculate positioning based on current conditions
- Start: Draw the dot on the screen
- Present: Monitor timing and remove the dot when its time expires
- Stop/Exit: Clean up resources and reset the display
How to Create Your Own Stimulus¶
To create your own stimulus:
- Define a database table with appropriate parameters for your stimulus
- Create a stimulus class that inherits from the base
Stimulusclass - Specify required fields and defaults in the
__init__method - Implement preparation logic to set up your stimulus based on conditions
- Create presentation methods that control how the stimulus appears:
start(): Initial displaypresent(): Continuous updates (if needed)stop()andexit(): Cleanup
Your stimulus should handle: - Positioning on the screen (considering aspect ratio) - Timing of presentation - Transitions stimulus conditions between states - Cleanup to ensure the display returns to a neutral state
By following this pattern, you can create diverse visual stimuli for behavioral experiments, from simple dots and shapes to complex moving patterns or interactive elements.