Behavior Preprocessing¶
Utilities for preprocessing behavioral data before analysis.
Preprocessing Functions¶
compass_labyrinth.behavior.preprocessing.preprocessing_utils
¶
DATA PREPROCESSING Author: Shreya Bangera Goal: ├── Concatenating all Pose estimation CSV files ├── Preprocessing all the tracking data
NODE_TYPE_MAPPING
module-attribute
¶
NODE_TYPE_MAPPING = {
"decision_reward": DECISION_REWARD,
"nondecision_reward": NONDECISION_REWARD,
"corner_reward": CORNER_REWARD,
"decision_nonreward": DECISION_NONREWARD,
"nondecision_nonreward": NONDECISION_NONREWARD,
"corner_nonreward": CORNER_NONREWARD,
"entry_zone": ENTRY_ZONE_NODES,
"target_zone": TARGET_ZONE_NODES,
"decision_3way": DECISION_3WAY,
"decision_4way": DECISION_4WAY,
}
REGION_MAPPING
module-attribute
¶
REGION_MAPPING = {
"target_zone": TARGET_ZONE,
"entry_zone": ENTRY_ZONE,
"reward_path": REWARD_PATH,
"dead_ends": DEAD_ENDS,
"neutral_zone": NEUTRAL_ZONE,
"loops": LOOPS,
}
ADJACENCY_MATRIX
module-attribute
¶
load_cohort_metadata
¶
Loads cohort metadata from the CSV file specified in the project configuration.
Parameters:
-
config(dict) –The project configuration dictionary containing the path to the cohort metadata CSV.
Returns:
-
metadata_df(DataFrame) –A DataFrame containing cohort metadata.
Source code in src/compass_labyrinth/utils.py
load_and_preprocess_session_data
¶
load_and_preprocess_session_data(
filename: str,
bp: str,
DLCscorer: str,
region_mapping: dict = REGION_MAPPING,
) -> pd.DataFrame
Loads DLC-tracked session data and assigns spatial regions based on grid numbers.
Parameters:
-
filename(str) –CSV file path for a session.
-
bp(str) –Body part name (e.g., 'sternum').
-
DLCscorer(str) –DLC scorer name from the CSV header.
-
region_mapping(dict, default:REGION_MAPPING) –Dictionary mapping region names to grid number lists.
Returns:
-
DataFrame–Cleaned and region-labeled DataFrame for the session.
Source code in src/compass_labyrinth/behavior/preprocessing/preprocessing_utils.py
compile_mouse_sessions
¶
compile_mouse_sessions(
config: dict,
bp: str,
region_mapping: dict = REGION_MAPPING,
) -> pd.DataFrame
Compiles all sessions into a single DataFrame.
Parameters:
-
config(dict) –Project configuration dictionary.
-
bp(str) –Body part name (e.g., 'sternum').
-
region_mapping(dict, default:REGION_MAPPING) –Region name → grid number list.
Returns:
-
DataFrame–Combined session dataframe with Region, Genotype, Sex.
Source code in src/compass_labyrinth/behavior/preprocessing/preprocessing_utils.py
remove_until_initial_node
¶
Removes all rows in the dataframe until the first occurrence of a grid node in the provided initial_nodes list.
Parameters:
-
df(DataFrame) –The input session dataframe.
-
initial_nodes(list, default:[47, 46, 34, 22]) –List of grid node integers to detect.
Returns:
-
DataFrame–Truncated dataframe starting from the first initial node.
Source code in src/compass_labyrinth/behavior/preprocessing/preprocessing_utils.py
remove_invalid_grid_transitions
¶
remove_invalid_grid_transitions(
df: DataFrame,
adjacency_matrix: DataFrame = ADJACENCY_MATRIX,
) -> pd.DataFrame
Removes rows from the dataframe where the transition between consecutive grid numbers is not valid (i.e., not adjacent in the adjacency matrix).
Parameters:
-
df(DataFrame) –The session dataframe after initial truncation.
-
adjacency_matrix(DataFrame, default:ADJACENCY_MATRIX) –Square adjacency matrix with binary values.
Returns:
-
DataFrame–Cleaned dataframe with only valid grid transitions.
Source code in src/compass_labyrinth/behavior/preprocessing/preprocessing_utils.py
preprocess_sessions
¶
preprocess_sessions(
df_comb: DataFrame,
adjacency_matrix: DataFrame = ADJACENCY_MATRIX,
initial_nodes: list = [47, 46, 34, 22],
) -> pd.DataFrame
Full preprocessing pipeline for all sessions: trims to initial nodes and removes invalid transitions.
Parameters:
-
df_comb(DataFrame) –Combined dataframe with all sessions.
-
adjacency_matrix(DataFrame, default:ADJACENCY_MATRIX) –Grid adjacency matrix.
-
initial_nodes(list, default:[47, 46, 34, 22]) –Nodes that mark the true session start.
Returns:
-
DataFrame–Fully cleaned and combined dataframe across all sessions.
Source code in src/compass_labyrinth/behavior/preprocessing/preprocessing_utils.py
ensure_velocity_column
¶
ensure_velocity_column(
df: DataFrame,
x_col: str = "x",
y_col: str = "y",
velocity_col: str = "Velocity",
fps: float = 5,
) -> pd.DataFrame
Adds a velocity column to the DataFrame if it doesn't already exist. Velocity is calculated as Euclidean displacement between frames, scaled by fps.
Parameters:
-
df(DataFrame) –Input DataFrame with coordinate data.
-
x_col(str, default:'x') –Name of x-coordinate column.
-
y_col(str, default:'y') –Name of y-coordinate column.
-
velocity_col(str, default:'Velocity') –Name of the new velocity column to add.
-
fps(float, default:5) –Frames per second to scale velocity to units/sec.
Returns:
-
DataFrame–DataFrame with velocity column added.
Source code in src/compass_labyrinth/behavior/preprocessing/preprocessing_utils.py
save_preprocessed_to_csv
¶
Saves Preprocessed data to CSV files
Parameters:
-
config(dict) –Project configuration dictionary.
-
df(DataFrame) –Preprocessed DataFrame to save.
Returns:
-
None–