merge_consecutive_detections

ketos.neural_networks.dev_utils.detection.merge_consecutive_detections(detections_df, step_size)[source]

Merges consecutive detections in the given dataframe.

Consecutive detections are merged into a single detection event represented by the time interval start-end. Consecutive detections are determined by the step_size parameter. The start time of the merged detection will be the start time of the first consecutive detection. The end time of the merged detection will be adjusted to be ‘step_size units after the start of the last consecutive detection.

If the detections are not consecutive, each detection will have its end time adjusted to be ‘step_size units after the start.

See examples below.

Args:
detections_df: pandas DataFrame

Dataframe with detections. It should have the following columns: - ‘filename’: The name of the file containing the detection. - ‘start’: The start time of the detection in seconds. - ‘end’: The end time of the detection in seconds. - ‘label’: The label associated with the detection. - ‘score’: The score associated with the detection.

step_size: float

The time interval (in seconds) between the starts of each continuous inputs. For example, a step=0.5 indicates that the first spectrogram starts at time 0.0s (from the beginning of the audio file), the second at 0.5s, etc.

Returns:
merged: pandas DataFrame

DataFrame with the merged detections.

Examples:

>>> import pandas as pd
>>> detections_df = pd.DataFrame({
...     'filename': ['file1', 'file1', 'file2', 'file2'],
...     'start': [0.0, 2.0, 0.0, 10.0],
...     'end': [3.0, 5.0, 3.0, 13.0],
...     'label': [1, 1, 1, 1],
...     'score': [0.8, 0.6, 0.9, 0.7]
... })
>>> step_size = 2.0
>>> merged = merge_consecutive_detections(detections_df, step_size)
>>> merged
  filename  start   end  label  score
0    file1    0.0   4.0      1    0.7
1    file2    0.0   2.0      1    0.9
2    file2   10.0  12.0      1    0.7