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
¶
ADJACENCY_MATRIX = pd.read_csv(
files("compass_labyrinth.data").joinpath(
"4step_adjacency_matrix.csv"
)
)
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,
region_mapping: dict = REGION_MAPPING,
) -> pd.DataFrame
Loads pose estimation data from NetCDF and assigns spatial regions based on grid numbers.
Parameters:
-
filename(str) –NetCDF file path for a session.
-
bp(str) –Body part name (e.g., 'sternum').
-
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. (Sessions with missing files are skipped and reported.)
Source code in src/compass_labyrinth/behavior/preprocessing/preprocessing_utils.py
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 | |
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.
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
¶
save_preprocessed_to_csv(
config: dict,
df: DataFrame,
save_combined: bool = True,
save_individual: bool = True,
) -> None
Saves preprocessed data to CSV files.
Parameters:
-
config(dict) –Project configuration dictionary.
-
df(DataFrame) –Preprocessed DataFrame to save.
-
save_combined(bool, default:True) –If True, saves a single combined CSV file. Default is True.
-
save_individual(bool, default:True) –If True, saves per-session individual CSV files. Default is True.
Returns:
-
None–