forked from Project-OSmOSE/OSmOSE_post_processing
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata_aplose.py
More file actions
573 lines (490 loc) · 18 KB
/
data_aplose.py
File metadata and controls
573 lines (490 loc) · 18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
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
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
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
299
300
301
302
303
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
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
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
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
"""`data_aplose` module provides the `DataAplose` class.
DataAplose class is used for handling, analyzing, and visualizing
APLOSE-formatted annotation data. It includes utilities to bin detections,
plot time-based distributions, and manage metadata such as annotators and labels.
"""
from __future__ import annotations
import logging
from copy import copy
from typing import TYPE_CHECKING
import matplotlib.dates as mdates
import matplotlib.pyplot as plt
from pandas import DataFrame, Series, Timedelta, Timestamp, concat, date_range
from pandas.tseries import offsets
from post_processing.dataclass.detection_filter import DetectionFilter
from post_processing.utils.core_utils import get_count
from post_processing.utils.filtering_utils import (
get_annotators,
get_dataset,
get_labels,
get_timezone,
load_detections,
)
from post_processing.utils.metrics_utils import detection_perf
from post_processing.utils.plot_utils import (
agreement,
heatmap,
histo,
overview,
scatter,
timeline,
)
if TYPE_CHECKING:
from datetime import tzinfo
from pathlib import Path
from pandas.tseries.offsets import BaseOffset
from post_processing.dataclass.recording_period import RecordingPeriod
default_colors = plt.rcParams["axes.prop_cycle"].by_key()["color"]
def _get_locator_from_offset(
offset: int | Timedelta | BaseOffset,
) -> mdates.DateLocator:
"""Map a pandas offset object to the appropriate matplotlib DateLocator."""
if isinstance(offset, int):
return mdates.SecondLocator(interval=offset)
if isinstance(offset, Timedelta):
total_seconds = int(offset.total_seconds())
if total_seconds % 3600 == 0:
return mdates.HourLocator(interval=total_seconds // 3600)
if total_seconds % 60 == 0:
return mdates.MinuteLocator(interval=total_seconds // 60)
return mdates.SecondLocator(interval=total_seconds)
offset_to_locator = {
(
offsets.MonthEnd,
offsets.MonthBegin,
offsets.BusinessMonthEnd,
offsets.BusinessMonthBegin,
): lambda offset: mdates.MonthLocator(interval=offset.n),
(offsets.Week,): lambda offset: mdates.WeekdayLocator(
byweekday=offset.weekday,
interval=offset.n,
),
(offsets.Day,): lambda offset: mdates.DayLocator(interval=offset.n),
(offsets.Hour,): lambda offset: mdates.HourLocator(interval=offset.n),
(offsets.Minute,): lambda offset: mdates.MinuteLocator(interval=offset.n),
}
for offset_classes, locator_fn in offset_to_locator.items():
if isinstance(offset, offset_classes):
return locator_fn(offset)
msg = f"Unsupported offset type: {type(offset)}"
raise ValueError(msg)
class DataAplose:
"""A class to handle APLOSE formatted data."""
def __init__(self, df: DataFrame = None) -> None:
"""Initialize a DataAplose object from a DataFrame.
Parameters
----------
df: DataFrame
APLOSE formatted DataFrame.
"""
self.df = df.sort_values(
by=[
"start_datetime",
"end_datetime",
"annotator",
"annotation",
],
).reset_index(drop=True)
self.annotators = sorted(set(self.df["annotator"])) if df is not None else None
self.labels = sorted(set(self.df["annotation"])) if df is not None else None
self.begin = min(self.df["start_datetime"]) if df is not None else None
self.end = max(self.df["end_datetime"]) if df is not None else None
self.dataset = sorted(set(self.df["dataset"])) if df is not None else None
self.lat = None
self.lon = None
def __str__(self) -> str:
"""Return string representation of DataAplose object."""
return (
f"begin: {self.begin}\n"
f"end: {self.end}\n"
f"annotators: {self.annotators}\n"
f"labels: {self.labels}\n"
f"dataset: {self.dataset}"
)
def __repr__(self) -> str:
"""Return string representation of DataAplose object."""
return self.__str__()
@property
def shape(self) -> tuple[int, int]:
"""Shape of DataFrame."""
return self.df.shape
@property
def lat(self) -> float:
"""Return latitude."""
return self._lat
@lat.setter
def lat(self, value: float) -> None:
self._lat = value
@property
def lon(self) -> float:
"""Return longitude."""
return self._lon
@lon.setter
def lon(self, value: float) -> None:
self._lon = value
@property
def coordinates(self) -> tuple[float, float]:
"""Coordinates of the audio data."""
return self.lat, self.lon
@coordinates.setter
def coordinates(self, value: tuple[float, float]) -> None:
if not isinstance(value, tuple) or len(value) != 2: # noqa: PLR2004
msg = "Coordinates must be a tuple of two floats: (lat, lon)."
raise ValueError(msg)
self.lat, self.lon = value
def __getitem__(self, item: int) -> Series:
"""Return the row from the underlying DataFrame."""
return self.df.iloc[item]
def change_tz(self, tz: str | tzinfo) -> None:
"""Change the timezone of a DataAplose instance.
Examples
--------
>>> import pytz
>>> data = DataAplose(...)
>>> data.change_tz(pytz.timezone("Etc/GMT-2"))
>>> data = DataAplose(...)
>>> data.change_tz("UTC")
>>> data = DataAplose(...)
>>> data.change_tz("UTC+02:00")
"""
self.df["start_datetime"] = [
elem.tz_convert(tz)
for elem in self.df["start_datetime"]
]
self.df["end_datetime"] = [
elem.tz_convert(tz)
for elem in self.df["end_datetime"]
]
self.begin = self.begin.tz_convert(tz)
self.end = self.end.tz_convert(tz)
def filter_df(
self,
annotator: str | list[str],
label: str | list[str],
) -> DataFrame:
"""Filter DataFrame based on annotator and label.
Parameters
----------
annotator: str | list[str]
The annotator or list of annotators to filter.
label: str | list[str]
The label or list of labels to filter.
Returns
-------
The filtered DataFrame.
Raises
------
ValueError
If annotator or label are not valid or if filtered Dataframe is empty.
"""
if isinstance(label, str):
label = [label] if isinstance(annotator, str) else [label] * len(annotator)
if isinstance(annotator, str):
annotator = (
[annotator] if isinstance(label, str) else [annotator] * len(label)
)
if len(annotator) != len(label):
msg = (
f"Length of annotator ({len(annotator)}) and"
f" label ({len(label)}) must match."
)
raise ValueError(msg)
for ant, lbl in zip(annotator, label, strict=False):
if ant not in self.annotators:
msg = f'Annotator "{ant}" not in APLOSE DataFrame'
raise ValueError(msg)
if lbl not in self.labels:
msg = f'Label "{lbl}" not in APLOSE DataFrame'
raise ValueError(msg)
if self.df[
(self.df["type"] == "WEAK")
& (self.df["annotator"] == ant)
& (self.df["annotation"] == lbl)
].empty:
msg = (
f"DataFrame with annotator '{ant}' / label '{lbl}'"
f" contains no weak detection."
)
raise ValueError(msg)
config = list(zip(annotator, label, strict=False))
return self.df[
self.df[["annotator", "annotation"]].apply(tuple, axis=1).isin(config)
].reset_index(drop=True)
def set_ax(
self,
ax: plt.Axes,
x_ticks_res: Timedelta | offsets.BaseOffset,
date_format: str,
) -> plt.Axes:
"""Configure a Matplotlib axis for time-based plot.
Sets up x-axis with appropriate limits, tick spacing,
formatting, and grid styling.
Parameters
----------
ax : matplotlib.axes.Axes
The Axes object to configure.
x_ticks_res : Timedelta | offsets.BaseOffset
Resolution of the x-axis major ticks.
date_format : str
Date format string for x-axis tick labels (e.g., "%b", "%Y-%m-%d %H:%M").
Returns
-------
matplotlib.axes.Axes
The configured Axes object, ready for plotting.
"""
ax.xaxis.set_major_locator(
_get_locator_from_offset(offset=x_ticks_res),
)
date_formatter = mdates.DateFormatter(fmt=date_format, tz=self.begin.tz)
ax.xaxis.set_major_formatter(date_formatter)
ax.grid(linestyle="--", linewidth=0.2, axis="both", zorder=1)
return ax
def overview(self, annotator: list[str] | None = None) -> None:
"""Overview of an APLOSE formatted DataFrame."""
overview(self.df, annotator)
def detection_perf(
self,
annotators: tuple[str, str],
labels: tuple[str, str],
timestamps: list[Timestamp] | None = None,
) -> tuple[float, float, float]:
"""Compute performances metrics for detection.
Performances are computed with a reference annotator in
comparison with a second annotator/detector.
Precision and recall are computed in regard
with a reference annotator/label pair.
Parameters
----------
annotators: [str, str]
List of the two annotators to compare.
First annotator is chosen as reference.
labels: [str, str]
List of the two labels to compare.
First label is chosen as reference.
timestamps: list[Timestamp], optional
A list of Timestamps to base the computation on.
Returns
-------
precision: float
recall: float
f_score: float
"""
df_filtered = self.filter_df(
annotators,
labels,
)
if isinstance(annotators, str):
annotators = [annotators]
if isinstance(labels, str):
labels = [labels]
ref = (annotators[0], labels[0])
return detection_perf(
df=df_filtered,
ref=ref,
timestamps=timestamps,
)
def plot(
self,
mode: str,
ax: plt.Axes,
*,
annotator: str | list[str],
label: str | list[str],
**kwargs: bool | Timedelta | BaseOffset | str | list[str] | RecordingPeriod,
) -> None:
"""Plot filtered annotation data using the specified mode.
Supports multiple plot types depending on the mode:
- "histogram": Plots a histogram of annotation data.
- "scatter" / "heatmap": Maps detections on a timeline.
- "agreement": Plots inter-annotator agreement regression.
Parameters
----------
mode: str
Type of plot to generate.
Must be one of {"histogram", "scatter", "heatmap", "agreement"}.
ax: plt.Axes
Matplotlib Axes object to plot on.
annotator: str | list[str]
The selected annotator or list of annotators.
label: str | list[str]
The selected label or list of labels.
**kwargs: Additional keyword arguments depending on the mode.
- legend: bool
Whether to show the legend.
- season: bool
Whether to show the season.
- show_rise_set: bool
Whether to show sunrise and sunset times.
- color: str | list[str]
Color(s) for the bars.
- bin_size: Timedelta | BaseOffset
Bin size for the histogram.
- effort: Series
The timestamps intervals corresponding to the observation effort.
If provided, data will be normalized by observation effort.
"""
df_filtered = self.filter_df(
annotator,
label,
)
time = date_range(self.begin, self.end)
if mode == "histogram":
bin_size = kwargs.get("bin_size")
legend = kwargs.get("legend", True)
color = kwargs.get("color")
season = kwargs.get("season")
effort = kwargs.get("effort")
if not bin_size:
msg = "'bin_size' missing for histogram plot."
raise ValueError(msg)
df_counts = get_count(df_filtered, bin_size)
detection_size = Timedelta(max(df_filtered["end_time"]), "s")
return histo(
df=df_counts,
ax=ax,
bin_size=bin_size,
time_bin=detection_size,
legend=legend,
color=color,
season=season,
effort=effort,
coordinates=(self.lat, self.lon),
)
if mode == "heatmap":
show_rise_set = kwargs.get("show_rise_set", True)
season = kwargs.get("season", False)
bin_size = kwargs.get("bin_size")
return heatmap(
df=df_filtered,
ax=ax,
bin_size=bin_size,
time_range=time,
show_rise_set=show_rise_set,
season=season,
coordinates=self.coordinates,
)
if mode == "scatter":
show_rise_set = kwargs.get("show_rise_set", True)
season = kwargs.get("season", False)
effort = kwargs.get("effort")
return scatter(
df=df_filtered,
ax=ax,
time_range=time,
show_rise_set=show_rise_set,
season=season,
coordinates=self.coordinates,
effort=effort,
)
if mode == "agreement":
bin_size = kwargs.get("bin_size")
return agreement(df=df_filtered, bin_size=bin_size, ax=ax)
if mode == "timeline":
color = kwargs.get("color")
df_filtered = self.filter_df(
annotator,
label,
)
return timeline(
df=df_filtered,
ax=ax,
color=color,
)
msg = f"Unsupported plot mode: {mode}"
raise ValueError(msg)
@classmethod
def from_yaml(
cls,
file: Path,
*,
concat: bool = True,
) -> DataAplose | list[DataAplose]:
"""Return a DataAplose object from a yaml file.
Parameters
----------
file: Path
The path to a yaml configuration file.
concat: bool
If set to True, the DataAplose objects will be concatenated.
If set to False, the DataAplose objects will be returned as a list.
Returns
-------
DataAplose:
The DataAplose object.
"""
filters = DetectionFilter.from_yaml(file=file)
return cls.from_filters(filters, concat=concat)
@classmethod
def from_filters(
cls,
filters: DetectionFilter | list[DetectionFilter],
*,
concat: bool = False,
) -> DataAplose | list[DataAplose]:
"""Return a DataAplose object from a yaml file.
Parameters
----------
filters: DetectionFilter | list[DetectionFilters]
Object containing the detection filters.
concat: bool
If set to True, the DataAplose objects will be concatenated.
If set to False, the DataAplose objects will be returned as a list.
Returns
-------
DataAplose:
The DataAplose object.
"""
if isinstance(filters, DetectionFilter):
filters = [filters]
cls_list = [cls(load_detections(fil)) for fil in filters]
if len(cls_list) == 1:
return cls_list[0]
if concat:
return cls.concatenate(cls_list)
return cls_list
@classmethod
def concatenate(
cls, data_list: list[DataAplose],
) -> DataAplose:
"""Concatenate a list of DataAplose objects into one."""
df_concat = (
concat([data.df for data in data_list], ignore_index=True)
.sort_values(
by=["start_datetime",
"end_datetime",
"annotator",
"annotation",
],
)
.reset_index(drop=True)
)
obj = cls(df_concat)
if isinstance(get_timezone(df_concat), list):
obj.change_tz("utc")
msg = ("Several timezones found in DataFrame,"
" all timestamps are converted to UTC.")
logging.info(msg)
return obj
def reshape(self, begin: Timestamp = None, end: Timestamp = None) -> DataAplose:
"""Reshape the DataAplose with new begin and/or end."""
new_data = copy(self)
if not any([begin, end]):
msg = "Must provide begin and/or end timestamps."
raise ValueError(msg)
tz = get_timezone(new_data.df)
if begin:
new_data.begin = begin
if not begin.tz:
new_data.begin = begin.tz_localize(tz)
if end:
new_data.end = end
if not end.tz:
new_data.end = end.tz_localize(tz)
new_data.df = new_data.df[
(new_data.df["start_datetime"] >= new_data.begin) &
(new_data.df["end_datetime"] <= new_data.end)
]
new_data.dataset = get_dataset(new_data.df)
new_data.labels = get_labels(new_data.df)
new_data.annotators = get_annotators(new_data.df)
return new_data