NWB Export Documentation¶
Overview¶
The NWB export module provides functionality to export experimental data from Ethopy DataJoint tables to NWB (Neurodata Without Borders) format files. This documentation covers the main export functions and their usage.
Main Functions¶
export_to_nwb()
¶
The primary function for exporting a single experimental session to NWB format.
Required Parameters¶
Parameter | Type | Description |
---|---|---|
animal_id |
int |
Unique identifier for the animal |
session_id |
int |
Session identifier |
Optional Parameters¶
NWB File Parameters¶
Parameter | Type | Default | Description |
---|---|---|---|
experimenter |
str |
"Unknown" |
Name of the experimenter |
lab |
str |
"Your Lab Name" |
Laboratory name |
institution |
str |
"Your Institution" |
Institution name |
session_description |
str |
Auto-generated | Description of the experimental session |
Subject Parameters¶
Parameter | Type | Default | Description |
---|---|---|---|
age |
str |
"Unknown" |
Age in ISO 8601 duration format |
subject_description |
str |
"laboratory mouse" |
Description of the subject |
species |
str |
Unknown" |
Species of the subject |
sex |
str |
"U" |
Sex: "M", "F", "U" (unknown), or "O" (other) |
Additional Options¶
Parameter | Type | Default | Description |
---|---|---|---|
output_filename |
str |
Auto-generated | Output filename for the NWB file |
overwrite |
bool |
False |
Whether to overwrite existing files |
return_nwb_object |
bool |
False |
Return both filename and NWB object |
config_path |
str |
~/.ethopy/local_conf.json |
Path to the local configuration file for DataJoint and schema setup. If not provided, the default Ethopy config is used. This parameter is passed to the DataJoint connection setup and allows exporting from different databases or configurations. |
Returns¶
str
: Path to the saved NWB file (default)Tuple[str, NWBFile]
: Path and NWB object ifreturn_nwb_object=True
Raises¶
ValueError
: If no session is found for the provided animal_id and session_idFileExistsError
: If output file exists andoverwrite=False
batch_export_to_nwb()
¶
Export multiple sessions to NWB format in batch.
Parameters¶
Parameter | Type | Description |
---|---|---|
session_list |
List[Tuple[int, int]] |
List of (animal_id, session_id) tuples |
output_directory |
str |
Directory to save NWB files (default: "nwb_exports") |
**kwargs |
Additional parameters passed to export_to_nwb() |
Returns¶
List[str]
: List of successfully exported filenames
Usage Examples¶
Basic Usage¶
Export with minimal parameters using all defaults:
1 |
|
This will create a file named nwb_animal_123_session_1.nwb
with default metadata.
Custom Parameters¶
Export with custom metadata and subject information:
1 2 3 4 5 6 7 8 9 10 11 12 |
|
Custom Filename¶
Specify a custom output filename:
1 2 3 4 5 6 |
|
Clarify the configuration path¶
1 2 3 4 5 |
|
The configuration file is a JSON file that specifies how to connect to your DataJoint database and which schema names to use for your experiment. You can provide a custom configuration file using the config_path
parameter in export_to_nwb
. This allows you to export data from different databases or with different schema setups, without changing your code.
Parameter explanations:
- dj_local_conf:
Contains the database connection settings for DataJoint. database.host
: The hostname or IP address of your MySQL server.database.user
: The username for connecting to the database.database.password
: The password for the database user.database.port
: The port number for the database (default is usually 3306).database.reconnect
: Whether to automatically reconnect if the connection drops.database.use_tls
: Whether to use TLS/SSL for the connection.-
datajoint.loglevel
: The logging level for DataJoint messages (e.g., "WARNING", "INFO"). -
SCHEMATA:
Maps logical names to the actual schema names in your database. This tells Ethopy which schemas to use for each data type. experiment
: The schema name for experiment/session tables.stimulus
: The schema name for stimulus tables.behavior
: The schema name for behavior tables.
Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
|
Return NWB Object for Further Processing¶
Get both the filename and NWB object for additional processing:
1 2 3 4 5 6 7 8 |
|
Overwriting¶
The code raises FileExistsError if the file exists and overwrite=False.
1 2 3 4 5 |
|
Batch Export¶
Export multiple sessions at once:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
|
Parameter Guidelines¶
Age Format (ISO 8601 Duration)¶
The age parameter should follow ISO 8601 duration format:
P90D
= 90 daysP3M
= 3 monthsP1Y6M
= 1 year and 6 monthsP2Y
= 2 yearsP1Y2M15D
= 1 year, 2 months, and 15 days
Sex Values¶
Value | Description |
---|---|
"M" |
Male |
"F" |
Female |
"U" |
Unknown |
"O" |
Other |
Data Included in NWB Files¶
The export function includes the following data types:
Core Data¶
- Session metadata: Timestamps, experimenter, lab, institution
- Subject information: Species, age, sex, description
- Trials: Trial timing, conditions, and metadata
Experimental Data¶
- Conditions: Experiment, stimulus, and behavior condition parameters
- Activity data: Behavioral activity measurements
- Reward data: Reward delivery timestamps and amounts
- Stimulus data: Stimulus presentation timing and parameters
- States data: State transition timestamps for each trial
Data Organization¶
- Processing modules: Data organized into logical modules (Conditions, Activity, Reward, States)
- Dynamic tables: Flexible storage for experimental parameters
- Time series: Timestamped behavioral events
- Behavioral events: Reward delivery and other behavioral markers
Error Handling¶
The export functions provide comprehensive error handling:
Common Errors¶
- Session not found: If the specified animal_id and session_id combination doesn't exist
- File exists: If the output file already exists and
overwrite=False
- No valid trials: If no trials with both PreTrial and InterTrial states are found
- Database connection: If DataJoint connection fails
Missing parameters If certain data (such as specific trial fields, behavioral events, or condition tables) are missing or incomplete for a session, the export function will skip those data elements and continue the export process. A warning message will be logged to inform you about any missing or incomplete data, so you can review and address these issues if needed. This ensures that a valid NWB file is still created even when some parts of the dataset are unavailable.
Error Recovery¶
- The function will attempt to create the NWB file even if some data is missing
- Warning messages are displayed for missing or incomplete data
- Batch export continues processing remaining sessions if individual exports fail
Best Practices¶
File Naming¶
- Use descriptive filenames that include animal ID, session ID, and experiment type
- Use consistent naming conventions across your lab
Metadata Completeness¶
- Always provide experimenter name and institution
- Include detailed session descriptions
- Specify accurate subject information (age, sex, strain)
Batch Processing¶
- Use batch export for multiple sessions with similar parameters
- Create separate output directories for different experiments
- Monitor the console output for failed exports
File Management¶
- Regularly backup NWB files
- Use version control for analysis scripts
- Document any post-processing steps applied to NWB files
Troubleshooting¶
Common Issues¶
- Import errors: Ensure all required packages are installed (pynwb, datajoint, numpy, pandas)
- Database connection: Verify DataJoint configuration and database access
- Memory issues: For large sessions, consider processing in smaller batches
- File permissions: Ensure write permissions for the output directory
Advanced Usage¶
Custom Processing¶
If you need to modify the NWB file after creation:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
|
Integration with Analysis Pipelines¶
The exported NWB files can be easily integrated with analysis pipelines:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
|