|
| 1 | +# Getting Started with EthoPy |
| 2 | + |
| 3 | +This guide will walk you through the process of setting up and running your first experiment with EthoPy. After completing this guide, you'll have a solid understanding of how to configure and run basic behavioral experiments. |
| 4 | + |
| 5 | +## Prerequisites |
| 6 | + |
| 7 | +Before starting, ensure you have: |
| 8 | + |
| 9 | +- Python 3.8 or higher (but less than 3.12) installed |
| 10 | +- Basic understanding of Python programming |
| 11 | +- Docker (for database setup) |
| 12 | +- For hardware experiments, appropriate hardware setup (Raspberry Pi, ports, etc.) |
| 13 | + |
| 14 | +## Installation |
| 15 | + |
| 16 | +1. **Install EthoPy package:** |
| 17 | + |
| 18 | + For the basic installation with core functionality: |
| 19 | + |
| 20 | + ```bash |
| 21 | + pip install ethopy |
| 22 | + ``` |
| 23 | + |
| 24 | + From source (latest development version): |
| 25 | + |
| 26 | + ```bash |
| 27 | + pip install git+https://github.com/ef-lab/ethopy_package |
| 28 | + ``` |
| 29 | + |
| 30 | + For development installation: |
| 31 | + |
| 32 | + ```bash |
| 33 | + git clone https://github.com/ef-lab/ethopy_package.git |
| 34 | + cd ethopy_package |
| 35 | + pip install -e ".[dev,docs]" |
| 36 | + ``` |
| 37 | + |
| 38 | +2. **Verify installation:** |
| 39 | + |
| 40 | + ```bash |
| 41 | + ethopy --version |
| 42 | + ``` |
| 43 | + |
| 44 | +## Database Setup |
| 45 | + |
| 46 | +EthoPy relies on a database for experiment configuration and data logging. If there is not a databse availabe, here is a quick setup of setting mysql databse with docker: |
| 47 | + |
| 48 | +1. **Start the database container:** |
| 49 | + |
| 50 | + ```bash |
| 51 | + ethopy-setup-djdocker |
| 52 | + ``` |
| 53 | + |
| 54 | +2. **Configure the database connection:** |
| 55 | + |
| 56 | + Create a configuration file at: |
| 57 | + - Linux/macOS: `~/.ethopy/local_conf.json` |
| 58 | + - Windows: `%USERPROFILE%\.ethopy\local_conf.json` |
| 59 | + |
| 60 | + ```json |
| 61 | + { |
| 62 | + "dj_local_conf": { |
| 63 | + "database.host": "127.0.0.1", |
| 64 | + "database.user": "root", |
| 65 | + "database.password": "your_password", |
| 66 | + "database.port": 3306 |
| 67 | + } |
| 68 | + } |
| 69 | + ``` |
| 70 | + |
| 71 | +3. **Verify database connection:** |
| 72 | + |
| 73 | + ```bash |
| 74 | + ethopy-db-connection |
| 75 | + ``` |
| 76 | + |
| 77 | +4. **Create required schemas:** |
| 78 | + |
| 79 | + ```bash |
| 80 | + ethopy-setup-schema |
| 81 | + ``` |
| 82 | + |
| 83 | +> **Note:** For testing without a database connection, see the simplified configuration in the next section. |
| 84 | +
|
| 85 | +## Setting Up Your First Experiment |
| 86 | + |
| 87 | +### 1. Local Configuration |
| 88 | + |
| 89 | +Create a local configuration file to specify your database connection and hardware setup. |
| 90 | + |
| 91 | +1. **Create a configuration file** at: |
| 92 | + - Linux/macOS: `~/.ethopy/local_conf.json` |
| 93 | + - Windows: `%USERPROFILE%\.ethopy\local_conf.json` |
| 94 | + |
| 95 | + Basic configuration structure: |
| 96 | + |
| 97 | + ```json |
| 98 | + { |
| 99 | + "dj_local_conf": { |
| 100 | + "database.host": "127.0.0.1", |
| 101 | + "database.user": "your_username", |
| 102 | + "database.password": "your_password", |
| 103 | + "database.port": 3306, |
| 104 | + "database.reconnect": true, |
| 105 | + "database.use_tls": false, |
| 106 | + "datajoint.loglevel": "WARNING" |
| 107 | + }, |
| 108 | + "source_path": "/path/to/data", |
| 109 | + "target_path": "/path/to/backup", |
| 110 | + "logging": { |
| 111 | + "level": "INFO", |
| 112 | + "directory": "~/.ethopy/", |
| 113 | + "filename": "ethopy.log" |
| 114 | + }, |
| 115 | + "SCHEMATA": { |
| 116 | + "experiment": "lab_experiments", |
| 117 | + "stimulus": "lab_stimuli", |
| 118 | + "behavior": "lab_behavior", |
| 119 | + "recording": "lab_recordings", |
| 120 | + "mice": "lab_mice" |
| 121 | + } |
| 122 | + } |
| 123 | + ``` |
| 124 | + |
| 125 | +2. **For hardware experiments**, configure GPIO pin mappings (Raspberry Pi): |
| 126 | + |
| 127 | + ```json |
| 128 | + { |
| 129 | + "channels": { |
| 130 | + "Liquid": {"1": 22, "2": 23}, // Liquid reward valves |
| 131 | + "Lick": {"1": 17, "2": 27}, // Lick detection sensors |
| 132 | + "Proximity": {"1": 5, "2": 6}, // Proximity sensors |
| 133 | + "Odor": {"1": 24, "2": 25}, // Odor delivery valves |
| 134 | + "Sound": {"1": 13}, // Sound output |
| 135 | + "Sync": {"in": 21, "rec": 26, "out": 16}, // Synchronization pins |
| 136 | + "Status": 20 // Status LED |
| 137 | + } |
| 138 | + } |
| 139 | + ``` |
| 140 | + |
| 141 | +3. **Programmatically accessing configuration** in your scripts: |
| 142 | + |
| 143 | + ```python |
| 144 | + from ethopy.core.config import ConfigurationManager |
| 145 | + |
| 146 | + # Initialize with default configuration |
| 147 | + config = ConfigurationManager() |
| 148 | + |
| 149 | + # Get a configuration value |
| 150 | + db_host = config.get('database.host') |
| 151 | + log_level = config.get('logging.level', 'INFO') # With default value |
| 152 | + |
| 153 | + # Set a configuration value |
| 154 | + config.set('logging.level', 'DEBUG') |
| 155 | + |
| 156 | + # Save changes |
| 157 | + config.save() |
| 158 | + ``` |
| 159 | + |
| 160 | +### 2. Create a Simple Task |
| 161 | + |
| 162 | +Let's create a simple task that displays a stimulus and waits for a response. |
| 163 | + |
| 164 | +1. Create a file named `simple_task.py`: |
| 165 | + |
| 166 | + ```python |
| 167 | + # Orientation discrimination experiment |
| 168 | + from ethopy.behaviors.multi_port import MultiPort |
| 169 | + from ethopy.experiments.match_port import Experiment |
| 170 | + from ethopy.stimuli.grating import Grating |
| 171 | + |
| 172 | + # define session parameters |
| 173 | + session_params = { |
| 174 | + "trial_selection": "staircase", |
| 175 | + "max_reward": 3000, |
| 176 | + "min_reward": 30, |
| 177 | + "setup_conf_idx": 0, |
| 178 | + } |
| 179 | + |
| 180 | + exp = Experiment() |
| 181 | + exp.setup(logger, MultiPort, session_params) |
| 182 | + |
| 183 | + # define stimulus conditions |
| 184 | + key = { |
| 185 | + "contrast": 100, |
| 186 | + "spatial_freq": 0.05, # cycles/deg |
| 187 | + "square": 0, # squarewave or Guassian |
| 188 | + "temporal_freq": 0, # cycles/sec |
| 189 | + "flatness_correction": 1, # adjustment of spatiotemporal frequencies based on animal distance |
| 190 | + "duration": 5000, |
| 191 | + "difficulty": 1, |
| 192 | + "trial_duration": 5000, |
| 193 | + "intertrial_duration": 0, |
| 194 | + "reward_amount": 8, |
| 195 | + } |
| 196 | + |
| 197 | + conditions = [] |
| 198 | + |
| 199 | + ports = {1: 0, |
| 200 | + 2: 90} |
| 201 | + |
| 202 | + block = exp.Block(difficulty=1, next_up=1, next_down=1, trial_selection='staircase', metric='dprime', stair_up=1, stair_down=0.5) |
| 203 | + |
| 204 | + # For port 1 and theta 0 |
| 205 | + conditions += exp.make_conditions(stim_class=Grating(), |
| 206 | + conditions={ |
| 207 | + **block.dict(), |
| 208 | + **key, |
| 209 | + 'theta' : 0, |
| 210 | + 'reward_port' : 1, |
| 211 | + 'response_port': 1 |
| 212 | + } |
| 213 | + ) |
| 214 | + |
| 215 | + # For port 2 and theta 90 |
| 216 | + conditions += exp.make_conditions(stim_class=Grating(), |
| 217 | + conditions={ |
| 218 | + **block.dict(), |
| 219 | + **key, |
| 220 | + 'theta' : 90, |
| 221 | + 'reward_port' : 2, |
| 222 | + 'response_port': 2 |
| 223 | + } |
| 224 | + ) |
| 225 | + |
| 226 | + # run experiments |
| 227 | + exp.push_conditions(conditions) |
| 228 | + exp.start() |
| 229 | + ``` |
| 230 | + |
| 231 | +### 3. Run Your Experiment |
| 232 | + |
| 233 | +Now that you have your task defined, you can run it: |
| 234 | + |
| 235 | +```bash |
| 236 | +ethopy -p simple_task.py --log-console |
| 237 | +``` |
| 238 | + |
| 239 | +This will: |
| 240 | +1. Set up your experiment with the specified configuration |
| 241 | +2. Initialize the behavior and stimulus classes |
| 242 | +3. Begin the experiment state machine |
| 243 | +4. Log all events and data to the database |
| 244 | + |
| 245 | +## Common Experiment Types |
| 246 | + |
| 247 | +EthoPy comes with several built-in experiment types that you can use or customize: |
| 248 | + |
| 249 | +1. **FreeWater**: Simple reward delivery when an animal licks a port |
| 250 | + ```python |
| 251 | + from ethopy.experiments.freewater import Experiment |
| 252 | + ``` |
| 253 | + |
| 254 | +2. **MatchPort**: Associate specific stimuli with reward ports |
| 255 | + ```python |
| 256 | + from ethopy.experiments.match_port import Experiment |
| 257 | + ``` |
| 258 | + |
| 259 | +3. **Passive**: Present stimuli without requiring responses |
| 260 | + ```python |
| 261 | + from ethopy.experiments.passive import Experiment |
| 262 | + ``` |
| 263 | + |
| 264 | +4. **Calibrate**: Calibrate water delivery amounts |
| 265 | + ```python |
| 266 | + from ethopy.experiments.calibrate import Experiment |
| 267 | + ``` |
| 268 | + |
| 269 | +Each experiment type has a predefined state machine with states like Entry, Trial, Response, Reward, and Exit. These states control the flow of the experiment. |
| 270 | + |
| 271 | +## Behavior and Stimulus Classes |
| 272 | + |
| 273 | +EthoPy's modular design allows you to combine different experiment types with behavior and stimulus classes: |
| 274 | + |
| 275 | +### Behavior Types |
| 276 | +- **MultiPort**: Standard setup with lick, liquid delivery, and proximity ports |
| 277 | + ```python |
| 278 | + from ethopy.behaviors,multi_port import MultiPort |
| 279 | + ``` |
| 280 | + |
| 281 | +- **MultiPort**: Standard setup with lick, liquid delivery, and proximity ports |
| 282 | + ```python |
| 283 | + from ethopy.behaviors.head_fixed import HeadFixed |
| 284 | + ``` |
| 285 | + |
| 286 | +### Stimulus Types |
| 287 | +- **Grating**: Orientation gratings |
| 288 | + ```python |
| 289 | + from ethopy.stimuli.grating import Grating |
| 290 | + ``` |
| 291 | +- **Bar**: Moving bar for retinotopic mapping |
| 292 | + ```python |
| 293 | + from ethopy.stimuli.bar import Bar |
| 294 | + ``` |
| 295 | +- **Dot**: Moving dot patterns |
| 296 | + ```python |
| 297 | + from ethopy.stimuli.dot import Dot |
| 298 | + ``` |
| 299 | + |
| 300 | +## Next Steps |
| 301 | + |
| 302 | +After successfully running your first experiment, you can: |
| 303 | + |
| 304 | +1. **Customize your task** by modifying parameters |
| 305 | +2. **Create a new experiment type** by subclassing from the base classes |
| 306 | +3. **Add hardware interfaces** for real behavioral experiments |
| 307 | +4. **Configure database logging** for data analysis |
| 308 | + |
| 309 | +## State Machine Architecture |
| 310 | + |
| 311 | +Understanding the state machine is crucial for working with EthoPy: |
| 312 | + |
| 313 | +1. Each experiment has a **StateMachine** dictionary mapping state names to methods |
| 314 | +2. The current state is tracked in the **State** variable |
| 315 | +3. Each state function is called repeatedly until it changes the State |
| 316 | +4. Every state function can have four special methods: |
| 317 | + - **entry_state**: Called once when entering the state |
| 318 | + - **run_state**: Called repeatedly while in the state |
| 319 | + - **exit_state**: Called once when exiting the state |
| 320 | + - **state**: Combined method that manages all the above |
| 321 | + |
| 322 | +## Sample Projects and Templates |
| 323 | + |
| 324 | +Explore these sample projects in the `ethopy/task/` directory: |
| 325 | + |
| 326 | +1. **calibrate_ports.py** - Calibrate water delivery ports |
| 327 | +2. **free_water.py** - Reward delivery without complex stimuli |
| 328 | +3. **no_stimulus.py** - Run experiment without visual stimuli |
| 329 | +4. **grating_test.py** - Grating stimulus presentation |
| 330 | +5. **bar_test.py** - Moving bar stimulus |
| 331 | +6. **dot_test.py** - Moving dot patterns |
| 332 | + |
| 333 | +## Troubleshooting and Help |
| 334 | + |
| 335 | +If you encounter issues, refer to the [Troubleshooting Guide](troubleshooting.md). |
| 336 | + |
| 337 | +For specific questions, check the: |
| 338 | +- [API Reference](logger.md) for detailed module documentation |
| 339 | +- [GitHub Issues](https://github.com/ef-lab/ethopy_package/issues) for known problems |
| 340 | + |
| 341 | +--- |
| 342 | + |
| 343 | +## Where to Go Next |
| 344 | + |
| 345 | +Now that you have a basic understanding of EthoPy: |
| 346 | + |
| 347 | +1. Dive deeper into [Local Configuration](local_conf.md) for advanced settings |
| 348 | +2. Learn about [Database Setup](database.md) for data storage |
| 349 | +3. Explore the [Plugin System](plugin.md) to extend functionality |
| 350 | +4. Study the [API Reference](logger.md) for detailed documentation |
| 351 | +5. Check [Contributing](contributing.md) if you want to help improve EthoPy |
0 commit comments