|
1 | 1 | # -*- coding: utf-8 -*- |
| 2 | +import unittest |
2 | 3 |
|
3 | 4 | import numpy as np |
4 | 5 | from pytest import fixture |
5 | 6 |
|
6 | 7 | from sigllm.primitives.prompting.anomalies import ( |
| 8 | + _clean_response, |
| 9 | + _parse_interval_response, |
| 10 | + _parse_list_response, |
7 | 11 | find_anomalies_in_windows, |
8 | 12 | format_anomalies, |
9 | 13 | merge_anomalous_sequences, |
| 14 | + parse_anomaly_response, |
10 | 15 | val2idx, |
11 | 16 | ) |
12 | 17 |
|
@@ -100,10 +105,160 @@ def test_val2idx(anomalous_val, windows): |
100 | 105 |
|
101 | 106 |
|
102 | 107 | # timestamp2interval |
103 | | - |
104 | | - |
105 | 108 | def test_format_anomalies(idx_list, timestamp): |
106 | 109 | expected = [(1000, 1820, 0), (5950, 6950, 0), (7390, 8390, 0), (11530, 12840, 0)] |
107 | 110 | result = format_anomalies(idx_list, timestamp) |
108 | 111 |
|
109 | 112 | assert expected == result |
| 113 | + |
| 114 | + |
| 115 | +def test_clean_response_no_anomalies(): |
| 116 | + test_cases = [ |
| 117 | + 'no anomalies', |
| 118 | + 'NO ANOMALIES', |
| 119 | + ' no anomalies ', |
| 120 | + 'There are no anomalies in this data', |
| 121 | + 'No anomaly detected', |
| 122 | + ' No anomaly ', |
| 123 | + ] |
| 124 | + for text in test_cases: |
| 125 | + assert _clean_response(text) == '' |
| 126 | + |
| 127 | + |
| 128 | +def test_clean_response_with_anomalies(): |
| 129 | + test_cases = [ |
| 130 | + ('[1, 2, 3]', '[1, 2, 3]'), |
| 131 | + (' [1, 2, 3] ', '[1, 2, 3]'), |
| 132 | + ('Anomalies found at [1, 2, 3]', 'anomalies found at [1, 2, 3]'), |
| 133 | + ('ANOMALIES AT [1, 2, 3]', 'anomalies at [1, 2, 3]'), |
| 134 | + ] |
| 135 | + for input_text, expected in test_cases: |
| 136 | + assert _clean_response(input_text) == expected |
| 137 | + |
| 138 | + |
| 139 | +def test_parse_list_response_valid_cases(): |
| 140 | + test_cases = [ |
| 141 | + ('[1, 2, 3]', '1,2,3'), |
| 142 | + (' [1, 2, 3] ', '1,2,3'), |
| 143 | + ('Anomalies found at [1, 2, 3]', '1,2,3'), |
| 144 | + ('[1,2,3]', '1,2,3'), |
| 145 | + ('[1, 2, 3, 4, 5]', '1,2,3,4,5'), |
| 146 | + ] |
| 147 | + for input_text, expected in test_cases: |
| 148 | + assert _parse_list_response(input_text) == expected |
| 149 | + |
| 150 | + |
| 151 | +def test_parse_list_response_invalid_cases(): |
| 152 | + test_cases = [ |
| 153 | + 'no anomalies', |
| 154 | + '[]', |
| 155 | + '[ ]', |
| 156 | + 'text with [no numbers]', |
| 157 | + 'text with [letters, and, symbols]', |
| 158 | + ' ', |
| 159 | + ] |
| 160 | + for text in test_cases: |
| 161 | + assert _parse_list_response(text) == '' |
| 162 | + |
| 163 | + |
| 164 | +def test_parse_list_response_edge_cases(): |
| 165 | + test_cases = [ |
| 166 | + ('[1,2,3,]', '1,2,3'), # trailing comma |
| 167 | + ('[1,,2,3]', '1,2,3'), # double comma |
| 168 | + ('[1, 2, 3], [5]', '1,2,3'), # two lists |
| 169 | + ] |
| 170 | + for input_text, expected in test_cases: |
| 171 | + assert _parse_list_response(input_text) == expected |
| 172 | + |
| 173 | + |
| 174 | +def test_parse_interval_response_valid_cases(): |
| 175 | + test_cases = [ |
| 176 | + ('[[1, 3]]', [1, 2, 3]), |
| 177 | + (' [[1, 3]] ', [1, 2, 3]), |
| 178 | + ('Anomalies found at [[1, 3]]', [1, 2, 3]), |
| 179 | + ('[[1, 3], [5, 7]]', [1, 2, 3, 5, 6, 7]), |
| 180 | + ('[[1, 3], [5, 7], [8, 9]]', [1, 2, 3, 5, 6, 7, 8, 9]), |
| 181 | + ('[[1, 3], [4, 6],]', [1, 2, 3, 4, 5, 6]), |
| 182 | + ('[[1, 2], [3]]', [1, 2]), |
| 183 | + ('[[1,,3]]', [1, 2, 3]), |
| 184 | + ('[[0, 10]]', list(range(11))), |
| 185 | + ] |
| 186 | + for input_text, expected in test_cases: |
| 187 | + assert _parse_interval_response(input_text) == expected |
| 188 | + |
| 189 | + |
| 190 | +def test_parse_interval_response_invalid_cases(): |
| 191 | + test_cases = [ |
| 192 | + '[]', |
| 193 | + '[[]]', |
| 194 | + 'text with [no numbers]', |
| 195 | + '[[1]]', # single number instead of pair |
| 196 | + '[[1, 2, 3]]', # triple instead of pair |
| 197 | + ] |
| 198 | + for text in test_cases: |
| 199 | + assert _parse_interval_response(text) == [] |
| 200 | + |
| 201 | + |
| 202 | +def test_parse_interval_response_multiple_matches(): |
| 203 | + test_cases = [ |
| 204 | + ('Found [[1, 3]] and [[5, 7]]', [1, 2, 3, 5, 6, 7]), |
| 205 | + ('[[1, 2]] in first part and [[3, 4]] in second', [1, 2, 3, 4]), |
| 206 | + ('Multiple intervals: [[1, 3]], [[4, 6]], [[7, 9]]', [1, 2, 3, 4, 5, 6, 7, 8, 9]), |
| 207 | + ('[[1, 2]] and [[1, 2]] and [[1, 2]]', [1, 2, 1, 2, 1, 2]), |
| 208 | + ] |
| 209 | + for input_text, expected in test_cases: |
| 210 | + assert _parse_interval_response(input_text) == expected |
| 211 | + |
| 212 | + |
| 213 | +class ParseAnomalyResponseTest(unittest.TestCase): |
| 214 | + def test_no_anomalies(self): |
| 215 | + data = [['Answer: no anomalies'], ['Answer: no anomaly'], ['no anomaly, with extra']] |
| 216 | + expected = [[''], [''], ['']] |
| 217 | + |
| 218 | + output = parse_anomaly_response(data) |
| 219 | + self.assertEqual(output, expected) |
| 220 | + |
| 221 | + def test_single_anomaly(self): |
| 222 | + data = [['Answer: [123]'], ['Answer: [456]', 'answer: [789]']] |
| 223 | + expected = [['123'], ['456', '789']] |
| 224 | + |
| 225 | + output = parse_anomaly_response(data) |
| 226 | + self.assertEqual(output, expected) |
| 227 | + |
| 228 | + def test_multiple_anomalies(self): |
| 229 | + data = [['Answer: [123, 456, 789]'], ['Answer: [111, 222, 333]']] |
| 230 | + expected = [['123,456,789'], ['111,222,333']] |
| 231 | + |
| 232 | + output = parse_anomaly_response(data) |
| 233 | + self.assertEqual(output, expected) |
| 234 | + |
| 235 | + def test_mixed_responses(self): |
| 236 | + data = [['Answer: no anomalies', 'Answer: [123, 456]'], ['Answer: [789]', 'no anomaly']] |
| 237 | + expected = [['', '123,456'], ['789', '']] |
| 238 | + |
| 239 | + output = parse_anomaly_response(data) |
| 240 | + self.assertEqual(output, expected) |
| 241 | + |
| 242 | + def test_different_formats(self): |
| 243 | + data = [ |
| 244 | + ['Answer: [123, 456]', 'Answer: [ 789 , 101 ]'], |
| 245 | + ['Answer: [1,2,3]', 'Answer: [ 4 , 5 , 6 ]'], |
| 246 | + ] |
| 247 | + expected = [['123,456', '789,101'], ['1,2,3', '4,5,6']] |
| 248 | + |
| 249 | + output = parse_anomaly_response(data) |
| 250 | + self.assertEqual(output, expected) |
| 251 | + |
| 252 | + def test_empty_responses(self): |
| 253 | + data = [[''], ['Answer: no anomalies'], ['answer'], ['no anomly']] |
| 254 | + expected = [[''], [''], [''], ['']] |
| 255 | + |
| 256 | + output = parse_anomaly_response(data) |
| 257 | + self.assertEqual(output, expected) |
| 258 | + |
| 259 | + def test_invalid_format(self): |
| 260 | + data = [['Answer: invalid format'], ['Answer: [123, abc]']] |
| 261 | + expected = [[''], ['']] |
| 262 | + |
| 263 | + output = parse_anomaly_response(data) |
| 264 | + self.assertEqual(output, expected) |
0 commit comments