Data Loaders¶
Functions for loading behavioral data from the database into pandas DataFrames.
Main data loading functions for Ethopy analysis.
This module provides user-friendly functions to load behavioral data and return it as pandas DataFrames or DataJoint expressions ready for analysis and visualization.
get_first_lick_after_state(animal_id, session, state='Response', sub_state='')
¶
Get the first lick per trial after a specific state onset.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
animal_id
|
int
|
The animal identifier. |
required |
session
|
int
|
The session number. |
required |
state
|
str
|
State after whose onset to measure the first
lick. Defaults to |
'Response'
|
sub_state
|
str
|
If non-empty, restrict results to trials
that also contain this state. Defaults to |
''
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
pd.DataFrame: One row per trial. Columns include |
Source code in src/ethopy_analysis/data/loaders.py
get_first_port_exit_after_state(animal_id, session, state='Trial', port=3)
¶
Get the first time the animal leaves a proximity sensor after a state onset.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
animal_id
|
int
|
The animal identifier. |
required |
session
|
int
|
The session number. |
required |
state
|
str
|
State after whose onset to look for the first
off-position event. Defaults to |
'Trial'
|
port
|
Optional[int]
|
Proximity port to monitor. If |
3
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
pd.DataFrame: One row per trial with columns |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If no state onset or proximity data is found for the given animal and session. |
Source code in src/ethopy_analysis/data/loaders.py
get_licks_during_proximity(animal_id, session, port=None)
¶
Report licks that occurred while the animal was inside a proximity sensor.
This is primarily a quality-control function: while in_position=1 the
animal is at the sensor, so licks on a different port are unexpected.
Windows where has_lick=True indicate a potential anomaly.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
animal_id
|
int
|
The animal identifier. |
required |
session
|
int
|
The session number. |
required |
port
|
Optional[int]
|
Filter to a specific sensor port. If
|
None
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
pd.DataFrame: One row per ON-OFF proximity window with columns
|
Source code in src/ethopy_analysis/data/loaders.py
get_licks_per_state(animal_id, session, states=None)
¶
Get all licks with their corresponding state window.
Uses :func:pandas.merge_asof to assign each lick to the state active at
that moment, then filters to only licks that fall before the state ends.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
animal_id
|
int
|
The animal identifier. |
required |
session
|
int
|
The session number. |
required |
states
|
Optional[List[str]]
|
If provided, only licks that fall within the listed states are returned. Defaults to None (all states). |
None
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
pd.DataFrame: DataFrame with columns |
Source code in src/ethopy_analysis/data/loaders.py
get_proximity_on_off_pairs(trial_states, proximities)
¶
Find all ON-OFF proximity pairs across the full trial window.
Spans from the first state onset to the last state end of the trial. Carry-over is handled: if the trial starts with the animal already inside the sensor (no ON event within the trial), the preceding ON event is included.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
trial_states
|
DataFrame
|
All state rows for one trial. Must
have columns |
required |
proximities
|
DataFrame
|
Full session proximity data for one port.
Must have columns |
required |
Returns:
| Type | Description |
|---|---|
List[Dict[str, Any]]
|
List[Dict[str, Any]]: Each element is a dict with keys |
Source code in src/ethopy_analysis/data/loaders.py
772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 | |
get_session_classes(animal_id, session)
¶
Retrieve session information and experimental classes for a specific animal session.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
animal_id
|
int
|
The animal identifier |
required |
session
|
int
|
The session number |
required |
Returns:
| Type | Description |
|---|---|
DataFrame
|
pd.DataFrame: Combined DataFrame containing session information and unique combinations of stimulus_class, behavior_class, and experiment_class |
Raises:
| Type | Description |
|---|---|
Exception
|
If no session found for the given animal_id and session |
Source code in src/ethopy_analysis/data/loaders.py
get_session_duration(animal_id, session)
¶
Calculate the duration of a session based on the last state onset time.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
animal_id
|
int
|
The animal identifier |
required |
session
|
int
|
The session number |
required |
Returns:
| Type | Description |
|---|---|
Optional[str]
|
Optional[str]: Formatted duration string (e.g., "1.2 hours (4320.0 seconds)") or None if no state times found |
Source code in src/ethopy_analysis/data/loaders.py
get_session_proximity_data(animal_id, session, trials=None, main_state='Trial', port=3)
¶
Build a per-trial DataFrame with proximity and lick data for a session.
For each trial the output provides scalar columns for the key events (suitable for alignment and sorting) and list columns for full plotting context.
Scalar columns (easy to use for alignment/sorting):
main_on_time:time_onof the first ON-OFF pair whose OFF falls withinmain_state.main_off_time:time_offof that pair.main_on_off_dur: duration of that pair (ms).response_lick_time: first lick aftermain_off_time.reaction_time:response_lick_time - main_off_time(ms).outcome: outcome state name ("Reward","Punish","Abort").outcome_time: onset time of the outcome state (ms).state_times: dict mapping state name → onset time for all states in the trial.
List columns (for full plotting context):
all_on_off_pairs: list of dicts[{time_on, time_off, duration, state}, ...].all_lick_times: list of lick times within the trial window.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
animal_id
|
int
|
The animal identifier. |
required |
session
|
int
|
The session number. |
required |
trials
|
Optional[List[int]]
|
Subset of trial indices to
process. |
None
|
main_state
|
str
|
State used to identify the main ON-OFF
pair per trial (the first pair whose |
'Trial'
|
port
|
int
|
Proximity port to use. Defaults to |
3
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
pd.DataFrame: One row per trial. |
Source code in src/ethopy_analysis/data/loaders.py
900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 | |
get_session_task(animal_id, session, save_file=True)
¶
Retrieve and optionally save the task configuration file for a specific session.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
animal_id
|
int
|
Animal identifier |
required |
session
|
int
|
Session identifier |
required |
save_file
|
bool
|
Whether to save the file to disk. Defaults to True. |
True
|
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The task filename. |
Note
If save_file is True, the file is saved with a modified name including animal_id and session for uniqueness.
Code version information is not part of this table. Use
:func:get_session_version for the git hash or package version that
produced the session.
Source code in src/ethopy_analysis/data/loaders.py
get_session_version(animal_id, session, format='df')
¶
Retrieve the code version records for a session.
EthoPy writes one row per project directory to experiment.Session.Version
- typically the EthoPy package and the plugins directory - recording the git
hash or the installed package version that produced the session.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
animal_id
|
int
|
The animal identifier |
required |
session
|
int
|
The session number |
required |
format
|
str
|
Return format, either "df" for DataFrame or "dj" for DataJoint expression. Defaults to "df". |
'df'
|
Returns:
| Type | Description |
|---|---|
Union[DataFrame, Any]
|
Union[pd.DataFrame, Any]: Version records DataFrame if format="df", DataJoint expression if format="dj". The DataFrame is empty when the session has no version records. |
Note
Columns are project_path, source_type ("git", "pypi" or "None"),
version (git hash or package version), repository_url and
is_dirty (uncommitted changes present at run time).
Not every session has version records: sessions predating the table, or runs where EthoPy could not resolve a project, have none.
Source code in src/ethopy_analysis/data/loaders.py
get_sessions(animal_id, from_date='', to_date='', format='df', min_trials=None)
¶
Get sessions for an animal within a specified date range.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
animal_id
|
int
|
The animal identifier |
required |
from_date
|
str
|
Start date in format 'YYYY-MM-DD'. Defaults to ''. |
''
|
to_date
|
str
|
End date in format 'YYYY-MM-DD'. Defaults to ''. |
''
|
format
|
str
|
if format equals 'dj' return datajoint expression. |
'df'
|
min_trials
|
int
|
minimum number of trials per session. |
None
|
Returns:
| Type | Description |
|---|---|
|
Union[pd.DataFrame, Any]: Session DataFrame if format="df", Session expression if format="dj" |
Source code in src/ethopy_analysis/data/loaders.py
get_state_windows(animal_id, session, states=None)
¶
Get start and end times for each state occurrence per trial.
End time is inferred as the onset of the next state within the same trial.
The last state of each trial has NaN as its end time.
This is the foundation for any analysis that needs to bound events to a specific state (e.g. licks during Reward, port entries during Trial).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
animal_id
|
int
|
The animal identifier. |
required |
session
|
int
|
The session number. |
required |
states
|
Optional[List[str]]
|
If provided, only rows for the listed states are returned. Defaults to None (all states). |
None
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
pd.DataFrame: DataFrame with columns |
Source code in src/ethopy_analysis/data/loaders.py
get_trial_behavior(animal_id, session, format='df')
¶
Retrieve trial behavior condition data for a specific animal session.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
animal_id
|
int
|
The animal identifier |
required |
session
|
int
|
The session number |
required |
format
|
str
|
Return format, either "df" for DataFrame or "dj" for DataJoint expression. Defaults to "df". |
'df'
|
Returns:
| Type | Description |
|---|---|
Union[DataFrame, Any]
|
Union[pd.DataFrame, Any]: Trial behavior conditions DataFrame if format="df", DataJoint expression if format="dj" |
Note
This function combines trial data with behavior conditions, handling cases where multiple behavior child tables need to be combined.
Source code in src/ethopy_analysis/data/loaders.py
get_trial_experiment(animal_id, session, format='df')
¶
Retrieve trial experiment condition data for a specific animal session.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
animal_id
|
int
|
The animal identifier |
required |
session
|
int
|
The session number |
required |
format
|
str
|
Return format, either "df" for DataFrame or "dj" for DataJoint expression. Defaults to "df". |
'df'
|
Returns:
| Type | Description |
|---|---|
Union[DataFrame, Any]
|
Union[pd.DataFrame, Any]: Trial experiment conditions DataFrame if format="df", DataJoint expression if format="dj" |
Note
This function combines trial data with experiment conditions based on the experiment_type from the session classes.
Source code in src/ethopy_analysis/data/loaders.py
get_trial_licks(animal_id, session, format='df')
¶
Retrieve all licks of a session.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
animal_id
|
int
|
The animal identifier |
required |
session
|
int
|
The session number |
required |
format
|
str
|
Return format, either "df" for DataFrame or "dj" for DataJoint expression. Defaults to "df". |
'df'
|
Returns:
| Type | Description |
|---|---|
Union[DataFrame, Any]
|
Union[pd.DataFrame, Any]: Trial behavior conditions DataFrame if format="df", DataJoint expression if format="dj" |
Source code in src/ethopy_analysis/data/loaders.py
get_trial_proximities(animal_id, session, ports=None, format='df')
¶
Retrieve proximity sensor data for a specific animal session.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
animal_id
|
int
|
The animal identifier |
required |
session
|
int
|
The session number |
required |
ports
|
Optional[List]
|
List of port numbers to filter by |
None
|
format
|
str
|
Return format, either "df" for DataFrame or "dj" for DataJoint expression. Defaults to "df". |
'df'
|
Returns:
| Type | Description |
|---|---|
|
Union[pd.DataFrame, Any]: Proximity data DataFrame if format="df", DataJoint expression if format="dj" |
Source code in src/ethopy_analysis/data/loaders.py
get_trial_proximity_timings(animal_id, session, port=3)
¶
Get all valid ON-OFF proximity timings for each trial across the full session.
Captures every ON-OFF pair across all states (PreTrial → InterTrial), including carry-overs where the animal enters the sensor before a state boundary.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
animal_id
|
int
|
The animal identifier. |
required |
session
|
int
|
The session number. |
required |
port
|
int
|
Proximity port to analyse. Defaults to |
3
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
pd.DataFrame: One row per ON-OFF pair with columns |
Source code in src/ethopy_analysis/data/loaders.py
get_trial_states(animal_id, session, format='df')
¶
Retrieve trial state onset data for a specific animal session.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
animal_id
|
int
|
The animal identifier |
required |
session
|
int
|
The session number |
required |
format
|
str
|
Return format, either "df" for DataFrame or "dj" for DataJoint expression. Defaults to "df". |
'df'
|
Returns:
| Type | Description |
|---|---|
Union[DataFrame, Any]
|
Union[pd.DataFrame, Any]: Trial states DataFrame if format="df", DataJoint expression if format="dj" |
Source code in src/ethopy_analysis/data/loaders.py
get_trial_stimulus(animal_id, session, stim_class=None, format='df')
¶
Retrieve trial stimulus condition data for a specific animal session.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
animal_id
|
int
|
The animal identifier |
required |
session
|
int
|
The session number |
required |
stim_class
|
Optional[str]
|
Specific stimulus class to use. If None, uses the stimulus class from session data. Defaults to None. |
None
|
format
|
str
|
Return format, either "df" for DataFrame or "dj" for DataJoint expression. Defaults to "df". |
'df'
|
Returns:
| Type | Description |
|---|---|
Union[DataFrame, Any]
|
Union[pd.DataFrame, Any]: Trial behavior conditions DataFrame if format="df", DataJoint expression if format="dj" |
Raises: Exception: If an explicitly requested stimulus class table is not found in the stimulus schema
Note
This function combines trial data with stimulus conditions and all related child tables that contain data for the session.
Sessions run with EthoPy's base Stimulus class have no dedicated
condition table - their conditions live directly on StimCondition.
Those sessions fall back to StimCondition instead of raising.
Source code in src/ethopy_analysis/data/loaders.py
get_trials(animal_id, session, format='df', remove_abort=False)
¶
Retrieve trial data for a specific animal session.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
animal_id
|
int
|
The animal identifier |
required |
session
|
int
|
The session number |
required |
format
|
str
|
Return format, either "df" for DataFrame or "dj" for DataJoint expression. Defaults to "df". |
'df'
|
remove_abort
|
bool
|
remove abort trials |
False
|
Returns:
| Type | Description |
|---|---|
Union[DataFrame, Any]
|
Union[pd.DataFrame, Any]: Trial DataFrame if format="df", DataJoint expression if format="dj" |