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
718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 | |
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
846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 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 | |
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:
| Type | Description |
|---|---|
Tuple[str, str]
|
Tuple[str, str]: A tuple containing (filename, git_hash) |
Note
If save_file is True, the file is saved with a modified name including animal_id and session for uniqueness.
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 the specified 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.
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" |