boris-behav-obs 8.16.5__py3-none-any.whl → 9.7.1__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (125) hide show
  1. boris/__init__.py +1 -1
  2. boris/__main__.py +1 -1
  3. boris/about.py +24 -36
  4. boris/add_modifier.py +88 -80
  5. boris/add_modifier_ui.py +235 -131
  6. boris/advanced_event_filtering.py +23 -29
  7. boris/analysis_plugins/__init__.py +0 -0
  8. boris/analysis_plugins/_latency.py +59 -0
  9. boris/analysis_plugins/irr_cohen_kappa.py +109 -0
  10. boris/analysis_plugins/irr_cohen_kappa_with_modifiers.py +112 -0
  11. boris/analysis_plugins/irr_weighted_cohen_kappa.py +157 -0
  12. boris/analysis_plugins/irr_weighted_cohen_kappa_with_modifiers.py +162 -0
  13. boris/analysis_plugins/list_of_dataframe_columns.py +22 -0
  14. boris/analysis_plugins/number_of_occurences.py +22 -0
  15. boris/analysis_plugins/number_of_occurences_by_independent_variable.py +54 -0
  16. boris/analysis_plugins/time_budget.py +61 -0
  17. boris/behav_coding_map_creator.py +228 -229
  18. boris/behavior_binary_table.py +33 -50
  19. boris/behaviors_coding_map.py +17 -18
  20. boris/boris_cli.py +6 -25
  21. boris/cmd_arguments.py +12 -1
  22. boris/coding_pad.py +16 -34
  23. boris/config.py +102 -50
  24. boris/config_file.py +55 -64
  25. boris/connections.py +105 -58
  26. boris/converters.py +13 -37
  27. boris/converters_ui.py +187 -110
  28. boris/cooccurence.py +250 -0
  29. boris/core.py +2108 -1275
  30. boris/core_qrc.py +15892 -10829
  31. boris/core_ui.py +941 -806
  32. boris/db_functions.py +17 -42
  33. boris/dev.py +27 -7
  34. boris/dialog.py +461 -242
  35. boris/duration_widget.py +9 -14
  36. boris/edit_event.py +61 -31
  37. boris/edit_event_ui.py +208 -97
  38. boris/event_operations.py +405 -281
  39. boris/events_cursor.py +25 -17
  40. boris/events_snapshots.py +36 -82
  41. boris/exclusion_matrix.py +4 -9
  42. boris/export_events.py +180 -203
  43. boris/export_observation.py +60 -73
  44. boris/external_processes.py +123 -98
  45. boris/geometric_measurement.py +427 -218
  46. boris/gui_utilities.py +91 -14
  47. boris/image_overlay.py +4 -4
  48. boris/import_observations.py +190 -98
  49. boris/ipc_mpv.py +304 -0
  50. boris/irr.py +20 -57
  51. boris/latency.py +31 -24
  52. boris/measurement_widget.py +14 -18
  53. boris/media_file.py +17 -19
  54. boris/menu_options.py +16 -6
  55. boris/modifier_coding_map_creator.py +1013 -0
  56. boris/modifiers_coding_map.py +7 -9
  57. boris/mpv2.py +128 -35
  58. boris/observation.py +493 -210
  59. boris/observation_operations.py +1010 -391
  60. boris/observation_ui.py +573 -363
  61. boris/observations_list.py +51 -58
  62. boris/otx_parser.py +74 -68
  63. boris/param_panel.py +45 -59
  64. boris/param_panel_ui.py +254 -138
  65. boris/player_dock_widget.py +91 -56
  66. boris/plot_data_module.py +18 -53
  67. boris/plot_events.py +56 -153
  68. boris/plot_events_rt.py +16 -30
  69. boris/plot_spectrogram_rt.py +80 -56
  70. boris/plot_waveform_rt.py +23 -48
  71. boris/plugins.py +431 -0
  72. boris/portion/__init__.py +18 -8
  73. boris/portion/const.py +35 -18
  74. boris/portion/dict.py +5 -5
  75. boris/portion/func.py +2 -2
  76. boris/portion/interval.py +21 -41
  77. boris/portion/io.py +41 -32
  78. boris/preferences.py +298 -123
  79. boris/preferences_ui.py +664 -225
  80. boris/project.py +293 -270
  81. boris/project_functions.py +610 -537
  82. boris/project_import_export.py +204 -213
  83. boris/project_ui.py +673 -441
  84. boris/qrc_boris.py +6 -3
  85. boris/qrc_boris5.py +6 -3
  86. boris/select_modifiers.py +62 -90
  87. boris/select_observations.py +19 -197
  88. boris/select_subj_behav.py +67 -39
  89. boris/state_events.py +51 -33
  90. boris/subjects_pad.py +6 -8
  91. boris/synthetic_time_budget.py +42 -26
  92. boris/time_budget_functions.py +169 -169
  93. boris/time_budget_widget.py +77 -89
  94. boris/transitions.py +41 -41
  95. boris/utilities.py +562 -222
  96. boris/version.py +3 -3
  97. boris/video_equalizer.py +16 -14
  98. boris/video_equalizer_ui.py +199 -130
  99. boris/video_operations.py +78 -28
  100. boris/view_df.py +104 -0
  101. boris/view_df_ui.py +75 -0
  102. boris/write_event.py +240 -136
  103. boris_behav_obs-9.7.1.dist-info/METADATA +140 -0
  104. boris_behav_obs-9.7.1.dist-info/RECORD +109 -0
  105. {boris_behav_obs-8.16.5.dist-info → boris_behav_obs-9.7.1.dist-info}/WHEEL +1 -1
  106. boris_behav_obs-9.7.1.dist-info/entry_points.txt +2 -0
  107. boris/README.TXT +0 -22
  108. boris/add_modifier.ui +0 -323
  109. boris/converters.ui +0 -289
  110. boris/core.qrc +0 -37
  111. boris/core.ui +0 -1571
  112. boris/edit_event.ui +0 -233
  113. boris/icons/logo_eye.ico +0 -0
  114. boris/map_creator.py +0 -982
  115. boris/observation.ui +0 -814
  116. boris/param_panel.ui +0 -379
  117. boris/preferences.ui +0 -537
  118. boris/project.ui +0 -1074
  119. boris/vlc_local.py +0 -90
  120. boris_behav_obs-8.16.5.dist-info/LICENSE.TXT +0 -674
  121. boris_behav_obs-8.16.5.dist-info/METADATA +0 -134
  122. boris_behav_obs-8.16.5.dist-info/RECORD +0 -107
  123. boris_behav_obs-8.16.5.dist-info/entry_points.txt +0 -2
  124. {boris → boris_behav_obs-9.7.1.dist-info/licenses}/LICENSE.TXT +0 -0
  125. {boris_behav_obs-8.16.5.dist-info → boris_behav_obs-9.7.1.dist-info}/top_level.txt +0 -0
@@ -0,0 +1,109 @@
1
+ """
2
+ BORIS plugin
3
+
4
+ Inter Rater Reliability (IRR) Unweighted Cohen's Kappa
5
+ """
6
+
7
+ import pandas as pd
8
+ from typing import Dict, Tuple
9
+
10
+ from sklearn.metrics import cohen_kappa_score
11
+ from PySide6.QtWidgets import QInputDialog
12
+
13
+
14
+ __version__ = "0.0.3"
15
+ __version_date__ = "2025-09-02"
16
+ __plugin_name__ = "Inter Rater Reliability - Unweighted Cohen's Kappa"
17
+ __author__ = "Olivier Friard - University of Torino - Italy"
18
+ __description__ = """
19
+ This plugin calculates Cohen's Kappa to measure inter-rater reliability between two observers who code categorical behaviors over time intervals.
20
+ Unlike the weighted version, this approach does not take into account the duration of the intervals.
21
+ Each segment of time is treated equally, regardless of how long it lasts.
22
+ This plugin does not take into account the modifiers.
23
+
24
+ How it works:
25
+
26
+ Time segmentation
27
+ The program identifies all the time boundaries (start and end points) used by both observers.
28
+ These boundaries are merged into a common timeline, which is then divided into a set of non-overlapping elementary intervals.
29
+
30
+ Assigning codes
31
+ For each elementary interval, the program determines which behavior was coded by each observer.
32
+
33
+ Comparison of codes
34
+ The program builds two parallel lists of behavior codes, one for each observer.
35
+ Each elementary interval is counted as one unit of observation, no matter how long the interval actually lasts.
36
+
37
+ Cohen's Kappa calculation
38
+ Using these two lists, the program computes Cohen's Kappa using the cohen_kappa_score function of the sklearn package.
39
+ (see https://scikit-learn.org/stable/modules/generated/sklearn.metrics.cohen_kappa_score.html for details)
40
+ This coefficient measures how much the observers agree on their coding, adjusted for the amount of agreement that would be expected by chance.
41
+
42
+ """
43
+
44
+
45
+ def run(df: pd.DataFrame) -> pd.DataFrame:
46
+ """
47
+ Calculate the Inter Rater Reliability - Unweighted Cohen's Kappa
48
+ """
49
+
50
+ # Attribute all active codes for each interval
51
+ def get_code(t_start, obs):
52
+ active_codes = [seg[2] for seg in obs if seg[0] <= t_start < seg[1]]
53
+ if not active_codes:
54
+ return ""
55
+ # Sort to ensure deterministic representation (e.g., "A+B" instead of "B+A")
56
+ return "+".join(sorted(active_codes))
57
+
58
+ # ask user for the number of decimal places for rounding (can be negative)
59
+ round_decimals, ok = QInputDialog.getInt(
60
+ None, "Rounding", "Enter the number of decimal places for rounding (can be negative)", value=3, minValue=-5, maxValue=3, step=1
61
+ )
62
+
63
+ # round times
64
+ df["Start (s)"] = df["Start (s)"].round(round_decimals)
65
+ df["Stop (s)"] = df["Stop (s)"].round(round_decimals)
66
+
67
+ # Get unique values
68
+ unique_obs_list = df["Observation id"].unique().tolist()
69
+
70
+ # Convert to tuples grouped by observation
71
+ grouped = {
72
+ obs: [
73
+ (row[0], row[1], row[2] + "|" + row[3]) # concatenate subject and behavior with |
74
+ for row in group[["Start (s)", "Stop (s)", "Subject", "Behavior"]].itertuples(index=False, name=None)
75
+ ]
76
+ for obs, group in df.groupby("Observation id")
77
+ }
78
+
79
+ ck_results: Dict[Tuple[str, str], str] = {}
80
+ for idx1, obs_id1 in enumerate(unique_obs_list):
81
+ obs1 = grouped[obs_id1]
82
+
83
+ # Perfect agreement with itself
84
+ ck_results[(obs_id1, obs_id1)] = "1.000"
85
+
86
+ for obs_id2 in unique_obs_list[idx1 + 1 :]:
87
+ obs2 = grouped[obs_id2]
88
+
89
+ # get all the break points
90
+ time_points = sorted(set([t for seg in obs1 for t in seg[:2]] + [t for seg in obs2 for t in seg[:2]]))
91
+
92
+ # elementary intervals
93
+ elementary_intervals = [(time_points[i], time_points[i + 1]) for i in range(len(time_points) - 1)]
94
+
95
+ obs1_codes = [get_code(t[0], obs1) for t in elementary_intervals]
96
+
97
+ obs2_codes = [get_code(t[0], obs2) for t in elementary_intervals]
98
+
99
+ # Cohen's Kappa
100
+ kappa = cohen_kappa_score(obs1_codes, obs2_codes)
101
+ print(f"{obs_id1} - {obs_id2}: Cohen's Kappa : {kappa:.3f}")
102
+
103
+ ck_results[(obs_id1, obs_id2)] = f"{kappa:.3f}"
104
+ ck_results[(obs_id2, obs_id1)] = f"{kappa:.3f}"
105
+
106
+ # DataFrame conversion
107
+ df_results = pd.Series(ck_results).unstack()
108
+
109
+ return df_results
@@ -0,0 +1,112 @@
1
+ """
2
+ BORIS plugin
3
+
4
+ Inter Rater Reliability (IRR) Unweighted Cohen's Kappa with modifiers
5
+ """
6
+
7
+ import pandas as pd
8
+
9
+ from sklearn.metrics import cohen_kappa_score
10
+ from PySide6.QtWidgets import QInputDialog
11
+
12
+ __version__ = "0.0.3"
13
+ __version_date__ = "2025-09-02"
14
+ __plugin_name__ = "Inter Rater Reliability - Unweighted Cohen's Kappa with modifiers"
15
+ __author__ = "Olivier Friard - University of Torino - Italy"
16
+ __description__ = """
17
+ This plugin calculates Cohen's Kappa to measure inter-rater reliability between two observers who code categorical behaviors over time intervals.
18
+ Unlike the weighted version, this approach does not take into account the duration of the intervals.
19
+ Each segment of time is treated equally, regardless of how long it lasts.
20
+ This plugin takes into account the modifiers.
21
+
22
+
23
+ How it works:
24
+
25
+ Time segmentation
26
+ The program identifies all the time boundaries (start and end points) used by both observers.
27
+ These boundaries are merged into a common timeline, which is then divided into a set of non-overlapping elementary intervals.
28
+
29
+ Assigning codes
30
+ For each elementary interval, the program determines which behavior was coded by each observer.
31
+
32
+ Comparison of codes
33
+ The program builds two parallel lists of behavior codes, one for each observer.
34
+ Each elementary interval is counted as one unit of observation, no matter how long the interval actually lasts.
35
+
36
+ Cohen's Kappa calculation
37
+ Using these two lists, the program computes Cohen's Kappa using the cohen_kappa_score function of the sklearn package.
38
+ (see https://scikit-learn.org/stable/modules/generated/sklearn.metrics.cohen_kappa_score.html for details)
39
+ This coefficient measures how much the observers agree on their coding, adjusted for the amount of agreement that would be expected by chance.
40
+
41
+ """
42
+
43
+
44
+ def run(df: pd.DataFrame):
45
+ """
46
+ Calculate the Inter Rater Reliability - Unweighted Cohen's Kappa with modifiers
47
+ """
48
+
49
+ # Attribute all active codes for each interval
50
+ def get_code(t_start, obs):
51
+ active_codes = [seg[2] for seg in obs if seg[0] <= t_start < seg[1]]
52
+ if not active_codes:
53
+ return ""
54
+ # Sort to ensure deterministic representation (e.g., "A+B" instead of "B+A")
55
+ return "+".join(sorted(active_codes))
56
+
57
+ # ask user for the number of decimal places for rounding (can be negative)
58
+ round_decimals, ok = QInputDialog.getInt(
59
+ None, "Rounding", "Enter the number of decimal places for rounding (can be negative)", value=3, minValue=-5, maxValue=3, step=1
60
+ )
61
+
62
+ # round times
63
+ df["Start (s)"] = df["Start (s)"].round(round_decimals)
64
+ df["Stop (s)"] = df["Stop (s)"].round(round_decimals)
65
+
66
+ # Get unique values
67
+ unique_obs_list = df["Observation id"].unique().tolist()
68
+
69
+ # Convert to tuples grouped by observation
70
+ grouped: dict = {}
71
+ modifiers: list = []
72
+ for col in df.columns:
73
+ if isinstance(col, tuple):
74
+ modifiers.append(col)
75
+
76
+ for obs, group in df.groupby("Observation id"):
77
+ o: list = []
78
+ for row in group[["Start (s)", "Stop (s)", "Subject", "Behavior"] + modifiers].itertuples(index=False, name=None):
79
+ modif_list = [row[i] for idx, i in enumerate(range(4, 4 + len(modifiers))) if modifiers[idx][0] == row[3]]
80
+ o.append((row[0], row[1], row[2] + "|" + row[3] + "|" + ",".join(modif_list)))
81
+ grouped[obs] = o
82
+
83
+ ck_results: dict = {}
84
+ for idx1, obs_id1 in enumerate(unique_obs_list):
85
+ obs1 = grouped[obs_id1]
86
+
87
+ ck_results[(obs_id1, obs_id1)] = "1.000"
88
+
89
+ for obs_id2 in unique_obs_list[idx1 + 1 :]:
90
+ obs2 = grouped[obs_id2]
91
+
92
+ # get all the break points
93
+ time_points = sorted(set([t for seg in obs1 for t in seg[:2]] + [t for seg in obs2 for t in seg[:2]]))
94
+
95
+ # elementary intervals
96
+ elementary_intervals = [(time_points[i], time_points[i + 1]) for i in range(len(time_points) - 1)]
97
+
98
+ obs1_codes = [get_code(t[0], obs1) for t in elementary_intervals]
99
+
100
+ obs2_codes = [get_code(t[0], obs2) for t in elementary_intervals]
101
+
102
+ # Cohen's Kappa
103
+ kappa = cohen_kappa_score(obs1_codes, obs2_codes)
104
+ print(f"{obs_id1} - {obs_id2}: Cohen's Kappa : {kappa:.3f}")
105
+
106
+ ck_results[(obs_id1, obs_id2)] = f"{kappa:.3f}"
107
+ ck_results[(obs_id2, obs_id1)] = f"{kappa:.3f}"
108
+
109
+ # DataFrame conversion
110
+ df_results = pd.Series(ck_results).unstack()
111
+
112
+ return df_results
@@ -0,0 +1,157 @@
1
+ """
2
+ BORIS plugin
3
+
4
+ Inter Rater Reliability (IRR) Weighted Cohen's Kappa
5
+ """
6
+
7
+ import pandas as pd
8
+ from typing import List, Tuple, Dict, Optional
9
+
10
+ from PySide6.QtWidgets import QInputDialog
11
+
12
+ __version__ = "0.0.3"
13
+ __version_date__ = "2025-09-02"
14
+ __plugin_name__ = "Inter Rater Reliability - Weighted Cohen's Kappa"
15
+ __author__ = "Olivier Friard - University of Torino - Italy"
16
+ __description__ = """
17
+ This plugin calculates Cohen's Kappa to measure inter-rater reliability between two observers who code categorical behaviors over time intervals.
18
+ Unlike the unweighted version, this approach takes into account the duration of each coded interval, giving more weight to longer intervals in the agreement calculation.
19
+ This plugin does not take into account the modifiers.
20
+
21
+ How it works:
22
+
23
+ Time segmentation
24
+ The program collects all the time boundaries from both observers and merges them into a unified set of time points.
25
+ These define a set of non-overlapping elementary intervals covering the entire observed period.
26
+
27
+ Assigning codes
28
+ For each elementary interval, the program identifies the behavior category assigned by each observer.
29
+
30
+ Weighted contingency table
31
+ Instead of treating each interval equally, the program assigns a weight equal to the duration of the interval.
32
+ These durations are accumulated in a contingency table that records how much time was spent in each combination of categories across the two observers.
33
+
34
+ Agreement calculation
35
+
36
+ Observed agreement (po): The proportion of total time where both observers assigned the same category.
37
+
38
+ Expected agreement (pe): The proportion of agreement expected by chance, based on the time-weighted marginal distributions of each observer's coding.
39
+
40
+ Cohen's Kappa (κ): Computed from the weighted observed and expected agreements.
41
+ """
42
+
43
+
44
+ def run(df: pd.DataFrame):
45
+ """
46
+ Calculate the Inter Rater Reliability - Weighted Cohen's Kappa
47
+ """
48
+
49
+ def cohen_kappa_weighted_by_time(
50
+ obs1: List[Tuple[float, float, str]], obs2: List[Tuple[float, float, str]]
51
+ ) -> Tuple[float, float, float, Dict[Tuple[Optional[str], Optional[str]], float]]:
52
+ """
53
+ Compute Cohen's Kappa weighted by time duration.
54
+
55
+ Args:
56
+ obs1: List of (start_time, end_time, code) for observer 1
57
+ obs2: List of (start_time, end_time, code) for observer 2
58
+
59
+ Returns:
60
+ kappa (float): Cohen's Kappa weighted by duration
61
+ po (float): Observed agreement proportion (weighted)
62
+ pe (float): Expected agreement proportion by chance (weighted)
63
+ contingency (dict): Contingency table {(code1, code2): total_duration}
64
+ """
65
+
66
+ # 1. Collect all time boundaries from both observers
67
+ time_points = sorted(set([t for seg in obs1 for t in seg[:2]] + [t for seg in obs2 for t in seg[:2]]))
68
+
69
+ # 2. Build elementary intervals (non-overlapping time bins)
70
+ elementary_intervals = [(time_points[i], time_points[i + 1]) for i in range(len(time_points) - 1)]
71
+
72
+ # 3. # Attribute all active codes for each interval
73
+ def get_code(t: float, obs: List[Tuple[float, float, str]]) -> Optional[str]:
74
+ active_codes = [seg[2] for seg in obs if seg[0] <= t < seg[1]]
75
+ if not active_codes:
76
+ return None
77
+ return "+".join(sorted(active_codes))
78
+
79
+ # 4. Build weighted contingency table (durations instead of counts)
80
+ contingency: Dict[Tuple[Optional[str], Optional[str]], float] = {}
81
+ total_time = 0.0
82
+
83
+ for start, end in elementary_intervals:
84
+ c1 = get_code(start, obs1)
85
+ c2 = get_code(start, obs2)
86
+ duration = end - start
87
+ total_time += duration
88
+ contingency[(c1, c2)] = contingency.get((c1, c2), 0.0) + duration
89
+
90
+ # 5. Observed agreement (po)
91
+ po = sum(duration for (c1, c2), duration in contingency.items() if c1 == c2) / total_time
92
+
93
+ # Marginal distributions for each observer
94
+ codes1: Dict[Optional[str], float] = {}
95
+ codes2: Dict[Optional[str], float] = {}
96
+ for (c1, c2), duration in contingency.items():
97
+ codes1[c1] = codes1.get(c1, 0.0) + duration
98
+ codes2[c2] = codes2.get(c2, 0.0) + duration
99
+
100
+ # 6. Expected agreement (pe), using marginal proportions
101
+ all_codes = set(codes1) | set(codes2)
102
+ pe = sum((codes1.get(c, 0.0) / total_time) * (codes2.get(c, 0.0) / total_time) for c in all_codes)
103
+
104
+ # 7. Kappa calculation
105
+ kappa = (po - pe) / (1 - pe) if (1 - pe) != 0 else 0.0
106
+
107
+ return kappa, po, pe, contingency
108
+
109
+ # ask user for the number of decimal places for rounding (can be negative)
110
+ round_decimals, ok = QInputDialog.getInt(
111
+ None, "Rounding", "Enter the number of decimal places for rounding (can be negative)", value=3, minValue=-5, maxValue=3, step=1
112
+ )
113
+
114
+ # round times
115
+ df["Start (s)"] = df["Start (s)"].round(round_decimals)
116
+ df["Stop (s)"] = df["Stop (s)"].round(round_decimals)
117
+
118
+ # Get unique values as a numpy array
119
+ unique_obs = df["Observation id"].unique()
120
+
121
+ # Convert to a list
122
+ unique_obs_list = unique_obs.tolist()
123
+
124
+ # Convert to tuples grouped by observation
125
+ grouped = {
126
+ obs: [
127
+ (row[0], row[1], row[2] + "|" + row[3]) # concatenate subject and behavior with |
128
+ for row in group[["Start (s)", "Stop (s)", "Subject", "Behavior"]].itertuples(index=False, name=None)
129
+ ]
130
+ for obs, group in df.groupby("Observation id")
131
+ }
132
+
133
+ ck_results: dict = {}
134
+ str_results: str = ""
135
+ for idx1, obs_id1 in enumerate(unique_obs_list):
136
+ obs1 = grouped[obs_id1]
137
+
138
+ ck_results[(obs_id1, obs_id1)] = "1.000"
139
+
140
+ for obs_id2 in unique_obs_list[idx1 + 1 :]:
141
+ obs2 = grouped[obs_id2]
142
+
143
+ # Cohen's Kappa
144
+ kappa, po, pe, table = cohen_kappa_weighted_by_time(obs1, obs2)
145
+
146
+ print(f"{obs_id1} - {obs_id2}: Cohen's Kappa: {kappa:.3f} Expected agreement: {pe:.3f} Observed agreement: {po:.3f}")
147
+ str_results += (
148
+ f"{obs_id1} - {obs_id2}: Cohen's Kappa: {kappa:.3f} Expected agreement: {pe:.3f} Observed agreement: {po:.3f}\n"
149
+ )
150
+
151
+ ck_results[(obs_id1, obs_id2)] = f"{kappa:.3f}"
152
+ ck_results[(obs_id2, obs_id1)] = f"{kappa:.3f}"
153
+
154
+ # DataFrame conversion
155
+ df_results = pd.Series(ck_results).unstack()
156
+
157
+ return df_results, str_results
@@ -0,0 +1,162 @@
1
+ """
2
+ BORIS plugin
3
+
4
+ Inter Rater Reliability (IRR) Weighted Cohen's Kappa with modifiers
5
+ """
6
+
7
+ import pandas as pd
8
+ from typing import List, Tuple, Dict, Optional
9
+
10
+ from PySide6.QtWidgets import QInputDialog
11
+
12
+ __version__ = "0.0.3"
13
+ __version_date__ = "2025-09-02"
14
+ __plugin_name__ = "Inter Rater Reliability - Weighted Cohen's Kappa with modifiers"
15
+ __author__ = "Olivier Friard - University of Torino - Italy"
16
+ __description__ = """
17
+ This plugin calculates Cohen's Kappa to measure inter-rater reliability between two observers who code categorical behaviors over time intervals.
18
+ Unlike the unweighted version, this approach takes into account the duration of each coded interval, giving more weight to longer intervals in the agreement calculation.
19
+ This plugin takes into account the modifiers.
20
+
21
+ How it works:
22
+
23
+ Time segmentation
24
+ The program collects all the time boundaries from both observers and merges them into a unified set of time points.
25
+ These define a set of non-overlapping elementary intervals covering the entire observed period.
26
+
27
+ Assigning codes
28
+ For each elementary interval, the program identifies the behavior category assigned by each observer.
29
+
30
+ Weighted contingency table
31
+ Instead of treating each interval equally, the program assigns a weight equal to the duration of the interval.
32
+ These durations are accumulated in a contingency table that records how much time was spent in each combination of categories across the two observers.
33
+
34
+ Agreement calculation
35
+
36
+ Observed agreement (po): The proportion of total time where both observers assigned the same category.
37
+
38
+ Expected agreement (pe): The proportion of agreement expected by chance, based on the time-weighted marginal distributions of each observer's coding.
39
+
40
+ Cohen's Kappa (κ): Computed from the weighted observed and expected agreements.
41
+ """
42
+
43
+
44
+ def run(df: pd.DataFrame):
45
+ """
46
+ Calculate the Inter Rater Reliability - Weighted Cohen's Kappa with modifiers
47
+ """
48
+
49
+ def cohen_kappa_weighted_by_time(
50
+ obs1: List[Tuple[float, float, str]], obs2: List[Tuple[float, float, str]]
51
+ ) -> Tuple[float, float, float, Dict[Tuple[Optional[str], Optional[str]], float]]:
52
+ """
53
+ Compute Cohen's Kappa weighted by time duration with modifiers.
54
+
55
+ Args:
56
+ obs1: List of (start_time, end_time, code) for observer 1
57
+ obs2: List of (start_time, end_time, code) for observer 2
58
+
59
+ Returns:
60
+ kappa (float): Cohen's Kappa weighted by duration
61
+ po (float): Observed agreement proportion (weighted)
62
+ pe (float): Expected agreement proportion by chance (weighted)
63
+ contingency (dict): Contingency table {(code1, code2): total_duration}
64
+ """
65
+
66
+ # 1. Collect all time boundaries from both observers
67
+ time_points = sorted(set([t for seg in obs1 for t in seg[:2]] + [t for seg in obs2 for t in seg[:2]]))
68
+
69
+ # 2. Build elementary intervals (non-overlapping time bins)
70
+ elementary_intervals = [(time_points[i], time_points[i + 1]) for i in range(len(time_points) - 1)]
71
+
72
+ # 3. Attribute all active codes for each interval
73
+ def get_code(t: float, obs: List[Tuple[float, float, str]]) -> Optional[str]:
74
+ active_codes = [seg[2] for seg in obs if seg[0] <= t < seg[1]]
75
+ if not active_codes:
76
+ return None
77
+ return "+".join(sorted(active_codes))
78
+
79
+ # 4. Build weighted contingency table (durations instead of counts)
80
+ contingency: Dict[Tuple[Optional[str], Optional[str]], float] = {}
81
+ total_time = 0.0
82
+
83
+ for start, end in elementary_intervals:
84
+ c1 = get_code(start, obs1)
85
+ c2 = get_code(start, obs2)
86
+ duration = end - start
87
+ total_time += duration
88
+ contingency[(c1, c2)] = contingency.get((c1, c2), 0.0) + duration
89
+
90
+ # 5. Observed agreement (po)
91
+ po = sum(duration for (c1, c2), duration in contingency.items() if c1 == c2) / total_time
92
+
93
+ # Marginal distributions for each observer
94
+ codes1: Dict[Optional[str], float] = {}
95
+ codes2: Dict[Optional[str], float] = {}
96
+ for (c1, c2), duration in contingency.items():
97
+ codes1[c1] = codes1.get(c1, 0.0) + duration
98
+ codes2[c2] = codes2.get(c2, 0.0) + duration
99
+
100
+ # 6. Expected agreement (pe), using marginal proportions
101
+ all_codes = set(codes1) | set(codes2)
102
+ pe = sum((codes1.get(c, 0.0) / total_time) * (codes2.get(c, 0.0) / total_time) for c in all_codes)
103
+
104
+ # 7. Kappa calculation
105
+ kappa = (po - pe) / (1 - pe) if (1 - pe) != 0 else 0.0
106
+
107
+ return kappa, po, pe, contingency
108
+
109
+ # ask user for the number of decimal places for rounding (can be negative)
110
+ round_decimals, ok = QInputDialog.getInt(
111
+ None, "Rounding", "Enter the number of decimal places for rounding (can be negative)", value=3, minValue=-5, maxValue=3, step=1
112
+ )
113
+
114
+ # round times
115
+ df["Start (s)"] = df["Start (s)"].round(round_decimals)
116
+ df["Stop (s)"] = df["Stop (s)"].round(round_decimals)
117
+
118
+ # Get unique values as a numpy array
119
+ unique_obs = df["Observation id"].unique()
120
+
121
+ # Convert to a list
122
+ unique_obs_list = unique_obs.tolist()
123
+
124
+ # Convert to tuples grouped by observation
125
+ grouped: dict = {}
126
+ modifiers: list = []
127
+ for col in df.columns:
128
+ if isinstance(col, tuple):
129
+ modifiers.append(col)
130
+
131
+ for obs, group in df.groupby("Observation id"):
132
+ o = []
133
+ for row in group[["Start (s)", "Stop (s)", "Subject", "Behavior"] + modifiers].itertuples(index=False, name=None):
134
+ modif_list = [row[i] for idx, i in enumerate(range(4, 4 + len(modifiers))) if modifiers[idx][0] == row[3]]
135
+ o.append((row[0], row[1], row[2] + "|" + row[3] + "|" + ",".join(modif_list)))
136
+ grouped[obs] = o
137
+
138
+ ck_results: dict = {}
139
+ str_results: str = ""
140
+ for idx1, obs_id1 in enumerate(unique_obs_list):
141
+ obs1 = grouped[obs_id1]
142
+
143
+ ck_results[(obs_id1, obs_id1)] = "1.000"
144
+
145
+ for obs_id2 in unique_obs_list[idx1 + 1 :]:
146
+ obs2 = grouped[obs_id2]
147
+
148
+ # Cohen's Kappa
149
+ kappa, po, pe, table = cohen_kappa_weighted_by_time(obs1, obs2)
150
+
151
+ print(f"{obs_id1} - {obs_id2}: Cohen's Kappa: {kappa:.3f} Expected agreement: {pe:.3f} Observed agreement: {po:.3f}")
152
+ str_results += (
153
+ f"{obs_id1} - {obs_id2}: Cohen's Kappa: {kappa:.3f} Expected agreement: {pe:.3f} Observed agreement: {po:.3f}\n"
154
+ )
155
+
156
+ ck_results[(obs_id1, obs_id2)] = f"{kappa:.3f}"
157
+ ck_results[(obs_id2, obs_id1)] = f"{kappa:.3f}"
158
+
159
+ # DataFrame conversion
160
+ df_results = pd.Series(ck_results).unstack()
161
+
162
+ return df_results, str_results
@@ -0,0 +1,22 @@
1
+ """
2
+ BORIS plugin
3
+
4
+ number of occurences of behaviors
5
+ """
6
+
7
+ import pandas as pd
8
+
9
+ __version__ = "0.0.1"
10
+ __version_date__ = "2025-06-13"
11
+ __plugin_name__ = "List of dataframe columns"
12
+ __author__ = "Olivier Friard - University of Torino - Italy"
13
+
14
+
15
+ def run(df: pd.DataFrame) -> pd.DataFrame:
16
+ """
17
+ List the columns present in the dataframe
18
+ """
19
+
20
+ df_results = pd.DataFrame(df.columns, columns=["column name"])
21
+
22
+ return df_results
@@ -0,0 +1,22 @@
1
+ """
2
+ BORIS plugin
3
+
4
+ number of occurences of behaviors
5
+ """
6
+
7
+ import pandas as pd
8
+
9
+ __version__ = "0.3.0"
10
+ __version_date__ = "2025-03-17"
11
+ __plugin_name__ = "Number of occurences of behaviors"
12
+ __author__ = "Olivier Friard - University of Torino - Italy"
13
+
14
+
15
+ def run(df: pd.DataFrame):
16
+ """
17
+ Calculate the number of occurrences of behaviors by subject.
18
+ """
19
+
20
+ df_results: pd.DataFrame = df.groupby(["Subject", "Behavior"])["Behavior"].count().reset_index(name="number of occurences")
21
+
22
+ return df_results
@@ -0,0 +1,54 @@
1
+ """
2
+ BORIS plugin
3
+
4
+ number of occurences of behaviors by independent_variable
5
+ """
6
+
7
+ import pandas as pd
8
+
9
+ __version__ = "0.4.0"
10
+ __version_date__ = "2025-07-17"
11
+ __plugin_name__ = "Number of occurences of behaviors by subject by independent_variable"
12
+ __author__ = "Olivier Friard - University of Torino - Italy"
13
+
14
+
15
+ def run(df: pd.DataFrame):
16
+ """
17
+ Calculate the number of occurrences of behaviors by subject and by independent_variable.
18
+
19
+ This plugin returns a Pandas dataframe
20
+ """
21
+
22
+ df_results_list: list = []
23
+
24
+ flag_variable_found = False
25
+
26
+ for column in df.columns:
27
+ if isinstance(column, tuple) or (isinstance(column, str) and not column.startswith("independent variable '")):
28
+ continue
29
+
30
+ flag_variable_found = True
31
+ grouped_df: df.DataFrame = (
32
+ df.groupby(
33
+ [
34
+ column,
35
+ "Subject",
36
+ "Behavior",
37
+ ]
38
+ )["Behavior"]
39
+ .count()
40
+ .reset_index(name="number of occurences")
41
+ )
42
+
43
+ grouped_df.rename(columns={column: "Value"}, inplace=True)
44
+
45
+ grouped_df.insert(0, "independent variable name", column)
46
+
47
+ df_results_list.append(grouped_df)
48
+
49
+ df_results = pd.concat(df_results_list, ignore_index=True) if df_results_list else pd.DataFrame([])
50
+
51
+ if not flag_variable_found:
52
+ return "No independent variable found"
53
+ else:
54
+ return df_results