|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | + |
| 3 | +""" |
| 4 | +Result post-processing module. |
| 5 | +
|
| 6 | +This module contains functions that help convert model responses back to timestamps. |
| 7 | +""" |
| 8 | +import numpy as np |
| 9 | +from collections import Counter |
| 10 | + |
| 11 | +def str2ind(text, len_seq, sep=','): |
| 12 | + """Convert a text string to indices. |
| 13 | +
|
| 14 | + Convert a string containing digits into an array of indices. |
| 15 | +
|
| 16 | + Args: |
| 17 | + text (str): |
| 18 | + A string containing indices values. |
| 19 | + len_seq (int): |
| 20 | + The length of processed sequence |
| 21 | + sep (str): |
| 22 | + String that was used to separate each element in text, Default to `","`. |
| 23 | +
|
| 24 | + Returns: |
| 25 | + numpy.ndarray: |
| 26 | + A 1-dimensional array containing parsed elements in `text`. |
| 27 | + """ |
| 28 | + #Remove all characters from text except the digits and sep |
| 29 | + text = ''.join(i for i in text if (i.isdigit() or i == sep)) |
| 30 | + |
| 31 | + values = np.fromstring(text, dtype=int, sep=sep) |
| 32 | + |
| 33 | + #Remove indices that exceed the length of sequence |
| 34 | + values = values[values < len_seq] |
| 35 | + return values |
| 36 | + |
| 37 | + |
| 38 | +def get_anomaly_list_within_seq(res_list, alpha = 0.5): |
| 39 | + """Get the final list of anomalous indices of a sequence |
| 40 | + |
| 41 | + Choose which index is considered anomalous in the sequence based on number of votes from multiple LLM responses |
| 42 | + |
| 43 | + Args: |
| 44 | + res_list (list of numpy.ndarray): |
| 45 | + A list of 1-dimensional array containing anomous indices output by LLM |
| 46 | + alpha (float): |
| 47 | + Percentage of total number of votes that an index needs to have to be considered anomalous. Default: 0.5 |
| 48 | + Returns: |
| 49 | + numpy.ndarray: |
| 50 | + A 1-dimensional array containing final anomalous indices |
| 51 | + """ |
| 52 | + min_vote = np.ceil(alpha*len(res_list)) |
| 53 | + |
| 54 | + flattened_res = np.concatenate(res_list) |
| 55 | + |
| 56 | + unique_elements, counts = np.unique(flattened_res, return_counts=True) |
| 57 | + |
| 58 | + final_list = unique_elements[counts >= min_vote] |
| 59 | + |
| 60 | + return final_list |
| 61 | + |
| 62 | +def get_anomaly_list_across_seq(ano_list, window_size, step_size, beta = 0.5): |
| 63 | + """Get the final list of anomalous indices of a sequence when combining all rolling windows |
| 64 | + |
| 65 | + Args: |
| 66 | + ano_list (list of numpy.ndarray): |
| 67 | + A list of 1-dimensional array containing anomous indices of each window |
| 68 | + window_size (int): |
| 69 | + Length of each window |
| 70 | + step_size (int): |
| 71 | + Indicating the number of steps the window moves forward each round. |
| 72 | + beta (float): |
| 73 | + Percentage of number of containing windows that an index needs to have to be considered anomalous. Default: 0.5 |
| 74 | + Return: |
| 75 | + numpy.ndarray: |
| 76 | + A 1-dimensional array containing final anomalous indices |
| 77 | + """ |
| 78 | + min_vote = np.ceil(beta * window_size/step_size) |
| 79 | + |
| 80 | + flattened_res = np.concatenate(ano_list) |
| 81 | + |
| 82 | + unique_elements, counts = np.unique(flattened_res, return_counts=True) |
| 83 | + |
| 84 | + final_list = unique_elements[counts >= min_vote] |
| 85 | + |
| 86 | + return np.sort(final_list) |
| 87 | + |
| 88 | +def ind2time(sequence, ind_list): |
| 89 | + """Convert list of indices into list of timestamp |
| 90 | + |
| 91 | + Args: |
| 92 | + sequence (pandas.Dataframe): |
| 93 | + Signal with timestamps and values |
| 94 | + ind_list (numpy.ndarray): |
| 95 | + A 1-dimensional array of indices |
| 96 | + Returns: |
| 97 | + numpy.ndarray: |
| 98 | + A 1-dimensional array containing timestamps of `sequence` corresponding to indices in `ind_list` |
| 99 | + """ |
| 100 | + return sequence.iloc[ind_list].timestamp.to_numpy() |
| 101 | + |
| 102 | + |
0 commit comments