|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | + |
| 3 | +""" |
| 4 | +Result post-processing module. |
| 5 | +
|
| 6 | +This module contains functions that help convert model responses back to indices and timestamps. |
| 7 | +""" |
| 8 | +import numpy as np |
| 9 | + |
| 10 | + |
| 11 | +def str2sig(text, sep=',', decimal=0): |
| 12 | + """Convert a text string to a signal. |
| 13 | +
|
| 14 | + Convert a string containing digits into an array of numbers. |
| 15 | +
|
| 16 | + Args: |
| 17 | + text (str): |
| 18 | + A string containing signal values. |
| 19 | + sep (str): |
| 20 | + String that was used to separate each element in text, Default to `","`. |
| 21 | + decimal (int): |
| 22 | + Number of decimal points to shift each element in text to. Default to `0`. |
| 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 and decimal point |
| 29 | + text = ''.join(i for i in text if (i.isdigit() or i == sep or i == '.')) |
| 30 | + values = np.fromstring(text, dtype=float, sep=sep) |
| 31 | + return values * 10**(-decimal) |
| 32 | + |
| 33 | + |
| 34 | +def str2idx(text, len_seq, sep=','): |
| 35 | + """Convert a text string to indices. |
| 36 | +
|
| 37 | + Convert a string containing digits into an array of indices. |
| 38 | +
|
| 39 | + Args: |
| 40 | + text (str): |
| 41 | + A string containing indices values. |
| 42 | + len_seq (int): |
| 43 | + The length of processed sequence |
| 44 | + sep (str): |
| 45 | + String that was used to separate each element in text, Default to `","`. |
| 46 | +
|
| 47 | + Returns: |
| 48 | + numpy.ndarray: |
| 49 | + A 1-dimensional array containing parsed elements in `text`. |
| 50 | + """ |
| 51 | + # Remove all characters from text except the digits and sep |
| 52 | + text = ''.join(i for i in text if (i.isdigit() or i == sep)) |
| 53 | + |
| 54 | + values = np.fromstring(text, dtype=int, sep=sep) |
| 55 | + |
| 56 | + # Remove indices that exceed the length of sequence |
| 57 | + values = values[values < len_seq] |
| 58 | + return values |
| 59 | + |
| 60 | + |
| 61 | +def get_anomaly_list_within_seq(res_list, alpha=0.5): |
| 62 | + """Get the final list of anomalous indices of a sequence |
| 63 | +
|
| 64 | + Choose anomalous index in the sequence based on multiple LLM responses |
| 65 | +
|
| 66 | + Args: |
| 67 | + res_list (List[numpy.ndarray]): |
| 68 | + A list of 1-dimensional array containing anomous indices output by LLM |
| 69 | + alpha (float): |
| 70 | + Percentage of votes needed for an index to be deemed anomalous. Default: 0.5 |
| 71 | +
|
| 72 | + Returns: |
| 73 | + numpy.ndarray: |
| 74 | + A 1-dimensional array containing final anomalous indices |
| 75 | + """ |
| 76 | + min_vote = np.ceil(alpha * len(res_list)) |
| 77 | + |
| 78 | + flattened_res = np.concatenate(res_list) |
| 79 | + |
| 80 | + unique_elements, counts = np.unique(flattened_res, return_counts=True) |
| 81 | + |
| 82 | + final_list = unique_elements[counts >= min_vote] |
| 83 | + |
| 84 | + return final_list |
| 85 | + |
| 86 | + |
| 87 | +def merge_anomaly_seq(anomalies, start_indices, window_size, step_size, beta=0.5): |
| 88 | + """Get the final list of anomalous indices of a sequence when merging all rolling windows |
| 89 | +
|
| 90 | + Args: |
| 91 | + anomalies (List[numpy.ndarray]): |
| 92 | + A list of 1-dimensional array containing anomous indices of each window |
| 93 | + start_indices (numpy.ndarray): |
| 94 | + A 1-dimensional array contaning the first index of each window |
| 95 | + window_size (int): |
| 96 | + Length of each window |
| 97 | + step_size (int): |
| 98 | + Indicating the number of steps the window moves forward each round. |
| 99 | + beta (float): |
| 100 | + Percentage of containing windows needed for index to be deemed anomalous. Default: 0.5 |
| 101 | +
|
| 102 | + Return: |
| 103 | + numpy.ndarray: |
| 104 | + A 1-dimensional array containing final anomalous indices |
| 105 | + """ |
| 106 | + anomalies = [arr + first_idx for (arr, first_idx) in zip(anomalies, start_indices)] |
| 107 | + |
| 108 | + min_vote = np.ceil(beta * window_size / step_size) |
| 109 | + |
| 110 | + flattened_res = np.concatenate(anomalies) |
| 111 | + |
| 112 | + unique_elements, counts = np.unique(flattened_res, return_counts=True) |
| 113 | + |
| 114 | + final_list = unique_elements[counts >= min_vote] |
| 115 | + |
| 116 | + return np.sort(final_list) |
| 117 | + |
| 118 | + |
| 119 | +def idx2time(sequence, idx_list): |
| 120 | + """Convert list of indices into list of timestamp |
| 121 | +
|
| 122 | + Args: |
| 123 | + sequence (pandas.Dataframe): |
| 124 | + Signal with timestamps and values |
| 125 | + idx_list (numpy.ndarray): |
| 126 | + A 1-dimensional array of indices |
| 127 | +
|
| 128 | + Returns: |
| 129 | + numpy.ndarray: |
| 130 | + A 1-dimensional array containing timestamps |
| 131 | + """ |
| 132 | + return sequence.iloc[idx_list].timestamp.to_numpy() |
0 commit comments