Database¶
Functions for database connection management and schema access.
Unified database schema management for Ethopy analysis.
This module provides a simple, unified interface for database connections and schema access with automatic caching and configuration management.
clear_schema_cache()
¶
Clear all cached schemas.
Useful for testing, configuration changes, or when you want to force recreation of schemas.
Example
clear_schema_cache() schemas = get_all_schemas() # Will recreate schemas
Source code in src/ethopy_analysis/db/schemas.py
get_all_schemas(config=None)
¶
Get all three schemas (experiment, behavior, stimulus) at once.
This is the main function that handles caching and schema creation.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
config
|
Optional[Dict[str, Any]]
|
Optional database configuration. If None, uses default config. |
None
|
Returns:
Type | Description |
---|---|
Dict[str, Any]
|
Dictionary with keys 'experiment', 'behavior', 'stimulus' containing |
Dict[str, Any]
|
the corresponding DataJoint virtual modules |
Example
from ethopy_analysis.config.settings import load_config config = load_config() schemas = get_all_schemas(config) trials = schemas['experiment'].Trial.fetch(format='frame') licking = schemas['behavior'].Licking.fetch(format='frame') conditions = schemas['stimulus'].Condition.fetch(format='frame')
Source code in src/ethopy_analysis/db/schemas.py
get_schema(schema_name, config=None)
¶
Get a specific schema by name.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
schema_name
|
str
|
Name of the schema to retrieve ('experiment', 'behavior', or 'stimulus') |
required |
config
|
Optional[Dict[str, Any]]
|
Optional database configuration. If None, uses default config. |
None
|
Returns:
Type | Description |
---|---|
DataJoint virtual module for the specified schema, or None if schema not found |
Example
from ethopy_analysis.config.settings import load_config config = load_config() experiment = get_schema('experiment', config) behavior = get_schema('behavior', config) stimulus = get_schema('stimulus', config)
Usage examples:¶
trials_df = experiment.Trial.fetch(format='frame') licking_df = behavior.Licking.fetch(format='frame') conditions_df = stimulus.Condition.fetch(format='frame')
Source code in src/ethopy_analysis/db/schemas.py
show_cached_schemas()
¶
Show information about currently cached schemas.
Returns:
Type | Description |
---|---|
Dict[str, str]
|
Dictionary with cache keys and basic info about cached schemas |
Example
cached_info = show_cached_schemas() print(f"Cached configurations: {list(cached_info.keys())}")
Source code in src/ethopy_analysis/db/schemas.py
test_connection(config=None)
¶
Test database connection and schema access.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
config
|
Optional[Dict[str, Any]]
|
Optional database configuration. If None, uses default config. |
None
|
Returns:
Type | Description |
---|---|
bool
|
True if connection successful, False otherwise |
Example
from ethopy_analysis.config.settings import load_config config = load_config() if test_connection(config): print("Database connection successful!") else: print("Database connection failed!")
Source code in src/ethopy_analysis/db/schemas.py
validate_schema_config(config)
¶
Validate that configuration contains required schema information.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
config
|
Dict[str, Any]
|
Database configuration dictionary |
required |
Returns:
Type | Description |
---|---|
bool
|
True if valid, raises ValueError if invalid |