Trajectory Analysis¶
Functions for analyzing movement trajectories including velocity, deviation patterns, and fitting models.
Overview¶
This module provides tools for:
- Computing velocity from position data
- Analyzing deviation from optimal paths
- Fitting exponential decay models to trajectories
- Visualizing trajectory patterns
Data Preparation¶
ensure_velocity_column¶
compass_labyrinth.behavior.behavior_metrics.task_performance_analysis.trajectory_analysis.ensure_velocity_column
¶
ensure_velocity_column(
df: DataFrame,
x_col: str = "x",
y_col: str = "y",
frame_rate: float = 5.0,
) -> pd.DataFrame
Ensure the DataFrame contains a 'Velocity' column. If not, it is computed as the Euclidean distance between consecutive (x, y) coordinates multiplied by the frame rate.
Parameters:
-
df(DataFrame) –Input DataFrame.
-
x_col(str, default:'x') –Column name for x coordinates.
-
y_col(str, default:'y') –Column name for y coordinates.
-
frame_rate(float, default:5.0) –Sampling rate (Hz) to convert framewise displacement into velocity.
Returns:
-
DataFrame–Updated DataFrame with 'Velocity' column.
Source code in src/compass_labyrinth/behavior/behavior_metrics/task_performance_analysis/trajectory_analysis.py
assign_bout_indices_from_entry_node¶
compass_labyrinth.behavior.behavior_metrics.task_performance_analysis.trajectory_analysis.assign_bout_indices_from_entry_node
¶
Assigns bout indices to each row in the DataFrame based on the occurrence of a delimiter node.
Parameters:
-
df(DataFrame) –Input DataFrame.
-
delimiter_node(int, default:47) –The grid number that serves as the delimiter for new bouts.
Returns:
-
DataFrame–DataFrame with assigned bout indices.
Source code in src/compass_labyrinth/behavior/behavior_metrics/task_performance_analysis/trajectory_analysis.py
ensure_bout_indices¶
compass_labyrinth.behavior.behavior_metrics.task_performance_analysis.trajectory_analysis.ensure_bout_indices
¶
Ensure Bout indices exist.
Parameters:
-
df(DataFrame) –Input DataFrame.
-
delimiter_node(int, default:47) –The grid number that serves as the delimiter for new bouts.
Returns:
-
DataFrame–DataFrame with ensured bout indices.
Source code in src/compass_labyrinth/behavior/behavior_metrics/task_performance_analysis/trajectory_analysis.py
Deviation Analysis¶
compute_deviation_velocity¶
compass_labyrinth.behavior.behavior_metrics.task_performance_analysis.trajectory_analysis.compute_deviation_velocity
¶
compute_deviation_velocity(
df: DataFrame,
key_regions: list = [
"entry_zone",
"reward_path",
"target_zone",
],
) -> pd.DataFrame
Compute deviation and velocity per bout
Parameters:
-
df(DataFrame) –Input DataFrame with 'Session', 'Genotype', 'Region', 'Grid Number', and 'Velocity' columns.
-
key_regions(list, default:['entry_zone', 'reward_path', 'target_zone']) –List of key regions to consider for deviation calculation.
Returns:
-
DataFrame–DataFrame with computed deviation and velocity per bout.
Source code in src/compass_labyrinth/behavior/behavior_metrics/task_performance_analysis/trajectory_analysis.py
process_deviation_velocity¶
compass_labyrinth.behavior.behavior_metrics.task_performance_analysis.trajectory_analysis.process_deviation_velocity
¶
Process deviation and velocity (normalize, smooth, fit curves).
Parameters:
-
index_df(DataFrame) –Input DataFrame with 'deviation', 'velocity', 'genotype', and 'ind_no' columns.
-
genotype(str) –Genotype to filter the DataFrame.
Returns:
-
DataFrame–Processed DataFrame with smoothed and normalized columns.
Source code in src/compass_labyrinth/behavior/behavior_metrics/task_performance_analysis/trajectory_analysis.py
Model Fitting¶
exp_decreasing¶
compass_labyrinth.behavior.behavior_metrics.task_performance_analysis.trajectory_analysis.exp_decreasing
¶
Visualization¶
plot_deviation_velocity_fit¶
compass_labyrinth.behavior.behavior_metrics.task_performance_analysis.trajectory_analysis.plot_deviation_velocity_fit
¶
plot_deviation_velocity_fit(
config: dict,
df: DataFrame,
params_dev: list,
params_vel: list,
genotype: str,
max_bouts: int | None = None,
save_fig: bool = True,
show_fig: bool = True,
return_fig: bool = False,
) -> None | plt.Figure
Plot deviation and velocity with exponential fits for a given genotype.
Parameters:
-
config(dict) –Project configuration dictionary.
-
df(DataFrame) –DataFrame with smoothed and normalized columns.
-
params_dev(list) –Parameters for the deviation exponential fit.
-
params_vel(list) –Parameters for the velocity exponential fit.
-
genotype(str) –Genotype to filter the DataFrame.
-
max_bouts(int or None, default:None) –Maximum number of bouts to display on the x-axis.
-
save_fig(bool, default:True) –Whether to save the figure as a PDF.
-
show_fig(bool, default:True) –Whether to display the figure.
-
return_fig(bool, default:False) –Whether to return the figure object.
Returns:
-
Figure or None–The matplotlib figure object if return_fig is True, else None.
Source code in src/compass_labyrinth/behavior/behavior_metrics/task_performance_analysis/trajectory_analysis.py
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 | |
plot_deviation_velocity_all¶
compass_labyrinth.behavior.behavior_metrics.task_performance_analysis.trajectory_analysis.plot_deviation_velocity_all
¶
plot_deviation_velocity_all(
config: dict,
index_df: DataFrame,
max_bouts: int | None = None,
save_fig: bool = True,
show_fig: bool = True,
return_fig: bool = False,
) -> None | plt.Figure
Creates a grid of subplots (auto-arranged) for each genotype showing smoothed deviation from reward path and velocity with exponential fits.
Parameters:
-
config(dict) –Project configuration dictionary.
-
index_df(DataFrame) –DataFrame with 'Deviation_smooth', 'Velocity_smooth_normalized', 'Genotype', and 'Ind_no' columns.
-
max_bouts(int or None, default:None) –Maximum number of bouts to display on the x-axis.
-
save_fig(bool, default:True) –Whether to save the figure as a PDF.
-
show_fig(bool, default:True) –Whether to display the figure.
-
return_fig(bool, default:False) –Whether to return the figure object.
Returns:
-
Figure or None–The matplotlib figure object if return_fig is True, else None.
Source code in src/compass_labyrinth/behavior/behavior_metrics/task_performance_analysis/trajectory_analysis.py
304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 | |