Performance Metrics¶
Functions for computing task performance metrics including region usage, entropy measures, and heatmaps.
Overview¶
This module provides comprehensive metrics for analyzing behavioral performance in the labyrinth task, including:
- Frame counting and session duration
- Target zone usage analysis
- Region-based heatmaps
- Shannon entropy calculations
- Statistical testing (ANOVA, mixed models)
Session and Frame Analysis¶
compute_frames_per_session¶
compass_labyrinth.behavior.behavior_metrics.task_performance_analysis.performance_metrics.compute_frames_per_session
¶
get_max_session_row_bracket¶
compass_labyrinth.behavior.behavior_metrics.task_performance_analysis.performance_metrics.get_max_session_row_bracket
¶
Finds the session with the maximum number of rows and returns the largest lower multiple of 10,000.
Parameters:
-
df_combined(DataFrame) –Combined dataframe containing multiple sessions.
-
session_col(str, default:'session') –Name of the column representing session ID.
Returns:
-
int–Lower bracketed row count (e.g., 20000 if max session has 23567 rows).
Source code in src/compass_labyrinth/behavior/behavior_metrics/task_performance_analysis/performance_metrics.py
Target Zone Usage¶
compute_target_zone_usage¶
compass_labyrinth.behavior.behavior_metrics.task_performance_analysis.performance_metrics.compute_target_zone_usage
¶
compute_target_zone_usage(
df: DataFrame,
pivot_dict: dict,
region: str = "target_zone",
difference: int = 10000,
) -> pd.DataFrame
Compute target zone usage from a time-binned pivot dictionary.
Parameters:
-
df(DataFrame) –DataFrame containing 'session' and 'genotype' columns.
-
pivot_dict(dict) –Dictionary with genotype as keys and list of pivot DataFrames as values.
-
region(str, default:'target_zone') –The region to compute usage for.
-
difference(int, default:10000) –Bin size.
Returns:
-
DataFrame–DataFrame containing target zone usage information.
Source code in src/compass_labyrinth/behavior/behavior_metrics/task_performance_analysis/performance_metrics.py
summarize_target_usage¶
compass_labyrinth.behavior.behavior_metrics.task_performance_analysis.performance_metrics.summarize_target_usage
¶
summarize_target_usage(
region_target: str,
frames_df: DataFrame,
cohort_metadata: DataFrame,
) -> pd.DataFrame
Summarize target zone usage per session.
Parameters:
-
region_target(str) –The target region to summarize.
-
frames_df(DataFrame) –DataFrame containing frame information.
-
cohort_metadata(DataFrame) –DataFrame containing cohort metadata.
Returns:
-
DataFrame–DataFrame containing the summary of target zone usage.
Source code in src/compass_labyrinth/behavior/behavior_metrics/task_performance_analysis/performance_metrics.py
exclude_low_performing_sessions¶
compass_labyrinth.behavior.behavior_metrics.task_performance_analysis.performance_metrics.exclude_low_performing_sessions
¶
exclude_low_performing_sessions(
df: DataFrame,
summary_df: DataFrame,
usage_threshold: float | None = 0.4,
min_frames: int | None = 30000,
) -> pd.DataFrame
Exclude sessions based on target usage and frame count thresholds.
Parameters:
-
df(DataFrame) –The original DataFrame containing session data.
-
summary_df(DataFrame) –The summary DataFrame containing session performance metrics.
-
usage_threshold(float | None, default:0.4) –The minimum target usage threshold for excluding sessions.
-
min_frames(int | None, default:30000) –The minimum number of frames threshold for excluding sessions.
Returns:
-
DataFrame–The cleaned DataFrame with low-performing sessions excluded.
Source code in src/compass_labyrinth/behavior/behavior_metrics/task_performance_analysis/performance_metrics.py
Visualization Functions¶
plot_target_usage_vs_frames¶
compass_labyrinth.behavior.behavior_metrics.task_performance_analysis.performance_metrics.plot_target_usage_vs_frames
¶
plot_target_usage_vs_frames(
config: dict,
summary_df: DataFrame,
save_fig: bool = True,
show_fig: bool = True,
return_fig: bool = False,
) -> None | plt.Figure
Plot target zone usage vs number of frames.
Parameters:
-
config(dict) –Configuration dictionary containing project settings.
-
summary_df(DataFrame) –DataFrame containing the summary of target zone usage.
-
save_fig(bool, default:True) –Whether to save the figure.
-
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 figure object if return_fig is True, otherwise None.
Source code in src/compass_labyrinth/behavior/behavior_metrics/task_performance_analysis/performance_metrics.py
plot_target_usage_with_exclusions¶
compass_labyrinth.behavior.behavior_metrics.task_performance_analysis.performance_metrics.plot_target_usage_with_exclusions
¶
plot_target_usage_with_exclusions(
config: dict,
summary_df: DataFrame,
sessions_to_exclude: list,
save_fig: bool = True,
show_fig: bool = True,
return_fig: bool = False,
) -> None | plt.Figure
Plot target zone usage vs number of frames, marking excluded sessions.
Parameters:
-
config(dict) –Configuration dictionary containing project settings.
-
summary_df(DataFrame) –DataFrame containing the summary of target zone usage.
-
sessions_to_exclude(list) –List of session IDs to exclude from the plot.
-
save_fig(bool, default:True) –Whether to save the figure.
-
show_fig(bool, default:True) –Whether to show the figure.
-
return_fig(bool, default:False) –Whether to return the figure.
Returns:
-
None | Figure–The created figure, if return_fig is True.
Source code in src/compass_labyrinth/behavior/behavior_metrics/task_performance_analysis/performance_metrics.py
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 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 | |
Region Heatmaps¶
generate_region_heatmap_pivots¶
compass_labyrinth.behavior.behavior_metrics.task_performance_analysis.performance_metrics.generate_region_heatmap_pivots
¶
generate_region_heatmap_pivots(
df: DataFrame,
lower_lim: int = 0,
upper_lim: int = 80000,
difference: int = 10000,
region_columns: list = [
"entry_zone",
"loops",
"dead_ends",
"neutral_zone",
"reward_path",
"target_zone",
],
region_lengths: dict = REGION_LENGTHS,
) -> dict
Create binned pivot tables for each genotype showing region occupancy over time windows.
Parameters:
-
df(DataFrame) –Input DataFrame containing 'session', 'genotype', and 'region' columns.
-
lower_lim(int, default:0) –Start index for binning.
-
upper_lim(int, default:80000) –End index for binning.
-
difference(int, default:10000) –Bin size.
-
region_columns(list, default:['entry_zone', 'loops', 'dead_ends', 'neutral_zone', 'reward_path', 'target_zone']) –List of region names to consider.
-
region_lengths(dict, default:REGION_LENGTHS) –Dictionary with total lengths for each region.
Returns:
-
pivot_dict(dict) –Dictionary with genotype as keys and list of pivot DataFrames as values.
Source code in src/compass_labyrinth/behavior/behavior_metrics/task_performance_analysis/performance_metrics.py
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 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 | |
subset_pivot_dict_sessions¶
compass_labyrinth.behavior.behavior_metrics.task_performance_analysis.performance_metrics.subset_pivot_dict_sessions
¶
Subset an existing pivot_dict to only include valid sessions from df_all_csv.
Parameters:
-
pivot_dict(dict) –Original pivot_dict with all sessions.
-
df_all_csv(DataFrame) –Must contain 'session' and 'genotype' columns.
Returns:
-
dict–Filtered pivot_dict with only valid sessions per genotype.
Source code in src/compass_labyrinth/behavior/behavior_metrics/task_performance_analysis/performance_metrics.py
plot_region_heatmaps¶
compass_labyrinth.behavior.behavior_metrics.task_performance_analysis.performance_metrics.plot_region_heatmaps
¶
plot_region_heatmaps(
config: dict,
pivot_dict: dict,
group_name: str,
lower_lim: int,
upper_lim: int,
difference: int,
included_sessions: list | None = None,
vmax: float = 0.6,
region_desired_order: list | None = None,
cmap: str = "viridis",
save_fig: bool = True,
show_fig: bool = True,
return_fig: bool = False,
) -> None | plt.Figure
Clean and aesthetically pleasing vertically stacked heatmaps with one colorbar per bin.
Parameters:
-
config(dict) –Configuration dictionary containing project settings.
-
pivot_dict(dict) –Dictionary with genotype as keys and list of pivot DataFrames as values.
-
group_name(str) –The genotype or group to plot.
-
lower_lim(int) –Start frame.
-
upper_lim(int) –End frame.
-
difference(int) –Bin size.
-
included_sessions(list | None, default:None) –List of session IDs to include. If None, include all sessions.
-
vmax(float, default:0.6) –Colorbar upper limit.
-
region_desired_order(list | None, default:None) –Desired order of regions for the heatmap. If None, use default order.
-
cmap(str, default:'viridis') –Colormap name.
-
save_fig(bool, default:True) –Whether to save the figure.
-
show_fig(bool, default:True) –Whether to display the figure.
-
return_fig(bool, default:False) –Whether to return the figure object.
Returns:
-
None | Figure–The figure object if return_fig is True, otherwise None.
Source code in src/compass_labyrinth/behavior/behavior_metrics/task_performance_analysis/performance_metrics.py
487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 | |
plot_region_heatmaps_all_genotypes¶
compass_labyrinth.behavior.behavior_metrics.task_performance_analysis.performance_metrics.plot_region_heatmaps_all_genotypes
¶
plot_region_heatmaps_all_genotypes(
config: dict,
pivot_dict: dict,
df_all_csv: DataFrame,
lower_lim: int,
upper_lim: int,
difference: int,
vmax: float = 0.6,
region_desired_order: list | None = None,
cmap: str = "viridis",
included_genotypes: list | None = None,
figsize_per_genotype: tuple = (4.5, 2.8),
spacing_w: float = 0.6,
spacing_h: float = 0.2,
show_colorbar: bool = True,
save_fig: bool = True,
show_fig: bool = True,
return_fig: bool = False,
) -> None | plt.Figure
Plot region occupancy heatmaps for each genotype and bin: - Rows: bins - Columns: genotypes
Parameters:
-
config(dict) –Configuration dictionary containing project settings.
-
pivot_dict(dict) –Dictionary with genotype as keys and list of pivot DataFrames as values.
-
df_all_csv(DataFrame) –DataFrame with valid 'genotype' and 'session' combinations.
-
lower_lim(int) –Start frame
-
upper_lim(int) –End frame
-
difference(int) –Bin size
-
vmax(float, default:0.6) –Colorbar upper limit
-
region_desired_order(list, default:None) –Optional order of regions
-
cmap(str, default:'viridis') –Colormap name
-
included_genotypes(list, default:None) –Genotype order to include
-
figsize_per_genotype(tuple, default:(4.5, 2.8)) –width x height scaling per genotype
-
spacing_w(float, default:0.6) –Space between genotype columns
-
spacing_h(float, default:0.2) –Space between time-bin rows
-
show_colorbar(bool, default:True) –If True, show colorbar in last column per row
-
save_fig(bool, default:True) –If True, save the figure
-
show_fig(bool, default:True) –If True, display the figure
-
return_fig(bool, default:False) –If True, return the figure object
Returns:
-
None | Figure–The figure object if return_fig is True, otherwise None.
Source code in src/compass_labyrinth/behavior/behavior_metrics/task_performance_analysis/performance_metrics.py
624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 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 | |
Entropy Analysis¶
compute_shannon_entropy_per_bin¶
compass_labyrinth.behavior.behavior_metrics.task_performance_analysis.performance_metrics.compute_shannon_entropy_per_bin
¶
compute_shannon_entropy_per_bin(
pivot_dict: dict,
df_all_csv: DataFrame,
bin_size: int = 10000,
) -> pd.DataFrame
Computes Shannon entropy per bin per session. Uses df_all_csv for genotype mapping and ensures robust merging.
Parameters:
-
pivot_dict(dict) –Dictionary with genotype as keys and list of pivot DataFrames as values.
-
df_all_csv(DataFrame) –DataFrame containing 'session' and 'genotype' columns.
-
bin_size(int, default:10000) –Size of each time bin.
Returns:
-
DataFrame–DataFrame with columns: 'session', 'bin', 'entropy', 'genotype'.
Source code in src/compass_labyrinth/behavior/behavior_metrics/task_performance_analysis/performance_metrics.py
plot_entropy_over_bins¶
compass_labyrinth.behavior.behavior_metrics.task_performance_analysis.performance_metrics.plot_entropy_over_bins
¶
plot_entropy_over_bins(
config: dict,
entropy_df: DataFrame,
palette: list | None = None,
ylim: tuple = (0, 5),
save_fig: bool = True,
show_fig: bool = True,
return_fig: bool = False,
) -> None | plt.Figure
Plot Shannon's entropy across bins for each genotype.
Parameters:
-
config(dict) –Configuration dictionary containing project settings.
-
entropy_df(DataFrame) –DataFrame containing 'session', 'bin', 'entropy', and 'genotype' columns
-
palette(list | None, default:None) –List of colors for genotypes.
-
ylim(tuple, default:(0, 5)) –Y-axis limits.
-
save_fig(bool, default:True) –Whether to save the figure.
-
show_fig(bool, default:True) –Whether to display the figure.
-
return_fig(bool, default:False) –Whether to return the figure object.
Returns:
-
None | Figure–The figure object if return_fig is True, otherwise None.
Source code in src/compass_labyrinth/behavior/behavior_metrics/task_performance_analysis/performance_metrics.py
Statistical Testing¶
run_entropy_anova¶
compass_labyrinth.behavior.behavior_metrics.task_performance_analysis.performance_metrics.run_entropy_anova
¶
Run repeated measures ANOVA using Bin as within-subject factor. Fills missing Entropy values with 0 (only here).
Parameters:
-
entropy_df(DataFrame) –DataFrame containing 'session', 'bin', 'entropy', and 'genotype' columns
Returns:
-
AnovaRM | None–The fitted ANOVA model or None if it fails.
Source code in src/compass_labyrinth/behavior/behavior_metrics/task_performance_analysis/performance_metrics.py
run_fdr_pairwise_tests¶
compass_labyrinth.behavior.behavior_metrics.task_performance_analysis.performance_metrics.run_fdr_pairwise_tests
¶
For each bin, performs pairwise t-tests between all genotype pairs. Applies FDR correction across all tests.
Parameters:
-
entropy_df(DataFrame) –DataFrame containing 'session', 'bin', 'entropy', and 'genotype' columns
Returns:
-
DataFrame | None–DataFrame with pairwise test results.
Source code in src/compass_labyrinth/behavior/behavior_metrics/task_performance_analysis/performance_metrics.py
run_mixed_model_per_genotype_pair¶
compass_labyrinth.behavior.behavior_metrics.task_performance_analysis.performance_metrics.run_mixed_model_per_genotype_pair
¶
For each genotype pair, test if bin x genotype interaction is significant. Does NOT fill NaNs. Uses only complete-case rows per model.
Parameters:
-
entropy_df(DataFrame) –DataFrame containing 'session', 'bin', 'entropy', and 'genotype' columns
Returns:
-
result_dict(dict) –Model summaries
-
interaction_table(DataFrame) –p-values of interaction terms
Source code in src/compass_labyrinth/behavior/behavior_metrics/task_performance_analysis/performance_metrics.py
Region Usage Over Time¶
compute_region_usage_over_bins¶
compass_labyrinth.behavior.behavior_metrics.task_performance_analysis.performance_metrics.compute_region_usage_over_bins
¶
compute_region_usage_over_bins(
pivot_dict: dict,
df_all_csv: DataFrame,
region: str,
bin_size: int,
) -> pd.DataFrame
Computes binned region usage across sessions for the given region.
Parameters:
-
pivot_dict(dict) –Dictionary with genotype keys and binned pivot tables.
-
df_all_csv(DataFrame) –DataFrame with session and genotype mapping.
-
region(str) –Region to compute usage for (e.g., "Target Zone").
-
bin_size(int) –Size of each bin (in frames).
Returns:
-
DataFrame–Binned region usage across sessions with genotype labels.
Source code in src/compass_labyrinth/behavior/behavior_metrics/task_performance_analysis/performance_metrics.py
plot_region_usage_over_bins¶
compass_labyrinth.behavior.behavior_metrics.task_performance_analysis.performance_metrics.plot_region_usage_over_bins
¶
plot_region_usage_over_bins(
config: dict,
region_data: DataFrame,
region_name: str,
palette: list | None = None,
ylim: tuple = (0, 1),
save_fig: bool = True,
show_fig: bool = True,
return_fig: bool = False,
)
Plots the proportion of usage over time bins for a specific region.
Parameters:
-
config(dict) –Configuration dictionary containing project settings.
-
region_data(DataFrame) –Output from compute_region_usage_over_bins().
-
region_name(str) –Display name for the region.
-
palette(list or dict, default:None) –Optional Seaborn color palette for genotypes.
-
ylim(tuple, default:(0, 1)) –Y-axis limits.
-
save_fig(bool, default:True) –Whether to save the figure.
-
show_fig(bool, default:True) –Whether to display the figure.
-
return_fig(bool, default:False) –Whether to return the figure object.
Returns:
-
None | Figure–The figure object if return_fig is True, otherwise None.
Source code in src/compass_labyrinth/behavior/behavior_metrics/task_performance_analysis/performance_metrics.py
1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 | |
plot_all_regions_usage_over_bins¶
compass_labyrinth.behavior.behavior_metrics.task_performance_analysis.performance_metrics.plot_all_regions_usage_over_bins
¶
plot_all_regions_usage_over_bins(
config: dict,
pivot_dict: dict,
df_all_csv: DataFrame,
region_list: list,
bin_size: int = 10000,
palette: list | None = None,
ylim: tuple = (0, 1),
save_fig: bool = True,
show_fig: bool = True,
return_fig: bool = False,
)
Plots usage over bins for multiple regions in a 2x3 subplot layout with a shared legend outside.
Parameters:
-
config(dict) –Configuration dictionary containing project settings.
-
pivot_dict(dict) –Dictionary with genotype keys and binned pivot tables.
-
df_all_csv(DataFrame) –DataFrame with session and genotype mapping.
-
region_list(list) –List of regions to plot (max 6).
-
bin_size(int, default:10000) –Size of each bin (in frames).
-
palette(list or dict, default:None) –Optional Seaborn color palette for genotypes.
-
ylim(tuple, default:(0, 1)) –Y-axis limits.
-
save_fig(bool, default:True) –Whether to save the figure.
-
show_fig(bool, default:True) –Whether to display the figure.
-
return_fig(bool, default:False) –Whether to return the figure object.
Returns:
-
None | Figure–The figure object if return_fig is True, otherwise None.
Source code in src/compass_labyrinth/behavior/behavior_metrics/task_performance_analysis/performance_metrics.py
1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 | |
run_region_usage_stats_mixedlm¶
compass_labyrinth.behavior.behavior_metrics.task_performance_analysis.performance_metrics.run_region_usage_stats_mixedlm
¶
Mixed Effects Model (bin x genotype) with missing bins dropped.
Parameters:
-
reg_binned(DataFrame) –DataFrame from compute_region_usage_over_bins().
-
region_col(str, default:'target_zone') –Column name for the region of interest. Default is "target_zone".
Returns:
-
None–
Source code in src/compass_labyrinth/behavior/behavior_metrics/task_performance_analysis/performance_metrics.py
run_region_usage_stats_fdr¶
compass_labyrinth.behavior.behavior_metrics.task_performance_analysis.performance_metrics.run_region_usage_stats_fdr
¶
run_region_usage_stats_fdr(
reg_binned: DataFrame, region_col: str = "target_zone"
) -> pd.DataFrame | None
Pairwise genotype comparisons at each bin (FDR corrected).
Parameters:
-
reg_binned(DataFrame) –DataFrame from compute_region_usage_over_bins().
-
region_col(str, default:'target_zone') –Column name for the region of interest. Default is "target_zone".
Returns:
-
DataFrame | None–DataFrame with pairwise test results or None if an error occurs.