convert_sequence_to_snapshot

ketos.neural_networks.dev_utils.detection.convert_sequence_to_snapshot(detections, threshold=0.5, highest_score_only=False)[source]

Converts a sequence of scores into a snapshot of events that exceed a threshold.

This assumes the output is from a sequence model. If using a snapshot model already, see neural_networks.dev_utils.detection.filter_by_threshold(),

The input data should be a dictionary containing:

‘filename’: a list of filenames, ‘start’: a list of start times for each input, ‘end’: a list of end times for each input, ‘score’: a 3D array or list of scores for each input, each sublist corresponding to a class.

The output is a dictionary that contains:

‘filename’: the filename where the event occurred, ‘start’: the start time of the event, ‘end’: the end time of the event, ‘label’: the label of the class that triggered the event, ‘score’: the scores of the class that triggered the event.

Args:
detections: dict

A dictionary containing the filenames, start times, end times, and scores.

threshold: float

A threshold value to filter scores. Scores above this threshold are considered as an event. Default is 0.5.

highest_score_only: bool

If True, only the highest score is returned. Default is False.

Returns:

pandas.DataFrame: A DataFrame containing the snapshots of events.

Examples:

>>> data = {
...     'filename': ['file1.wav'],
...     'start': [0.0],
...     'end': [60.0],
...     'score': [[
...         [0.1, 0.6, 0.4],
...         [0.2, 0.3, 0.5]
...     ]]
... }
>>> df = convert_sequence_to_snapshot(data, 0.5)
>>> df.equals(pd.DataFrame({
...     'filename': ['file1.wav'], 
...     'start': [20.0], 
...     'end': [40.0], 
...     'label': [0], 
...     'score': [[0.6]]
... }))
True
>>> data = {
...     'filename': ['file1.wav'],
...     'start': [0.0],
...     'end': [60.0],
...     'score': [[
...         [0.8, 0.9, 0.7, 0.2, 0.1, 0.6, 0.9, 0.9, 0.2],
...         [0.7, 0.4, 0.6, 0.8, 0.6, 0.7, 0.8, 0.6, 0.1]
...     ]]
... }
>>> df = convert_sequence_to_snapshot(data, 0.5, highest_score_only=True)
>>> df = df.round(5) 
>>> df.equals(pd.DataFrame({
...     'filename': ['file1.wav', 'file1.wav', 'file1.wav'], 
...     'start': [0.0, 20.0, 40.0], 
...     'end': [20.0, 40.0, 53.33333], 
...     'label': [0, 1, 0], 
...     'score': [[0.8, 0.9, 0.7], [0.8, 0.6, 0.7], [0.9, 0.9]]
... }))
True