def test_multi_rolling_aggregate_mismatching_start(self): factory = NodeFactory.transformer('test', 'MultiRollingAggregate') factory.set_param_value('window', '1') factory.set_param_value('center', False) factory.set_param_value('min_periods', '0') factory.set_param_value('agg_method', 'proportion') factory.add_source(InputRef('lhs')) factory.add_source(InputRef('rhs')) ram = factory.build() s1 = Series.from_array([[0, 1], [1, 2], [2, 3], [3, 4]]) s2 = Series.from_array([[1, 5], [2, 5], [3, 5]]) self.case(ram, s1, s2, list(s2.pdseries.index), [2 / 5, 3 / 5, 4 / 5])
def test_multi_rolling_aggregate_correlation(self): factory = NodeFactory.transformer('test', 'MultiRollingAggregate') factory.set_param_value('window', '2') factory.set_param_value('center', False) factory.set_param_value('min_periods', '0') factory.set_param_value('agg_method', 'correlation_pearson') factory.add_source(InputRef('lhs')) factory.add_source(InputRef('rhs')) ram = factory.build() s1 = Series.from_array([[0, 1], [1, 2], [2, 3], [3, 4], [4, 5]]) s2 = Series.from_array([[0, 1], [1, 2], [2, 3], [3, 2], [4, 1]]) self.case(ram, s1, s2, list(s1.pdseries.index)[1:], [1, 1, -1, -1])
def test_multi_rolling_aggregate_proportion_window_min_periods_default( self): factory = NodeFactory.transformer('test', 'MultiRollingAggregate') factory.set_param_value('window', '2') factory.set_param_value('center', False) factory.set_param_value('min_periods', '') factory.set_param_value('agg_method', 'proportion') factory.add_source(InputRef('lhs')) factory.add_source(InputRef('rhs')) ram = factory.build() s1 = Series.from_array([[0, 1], [1, 2], [2, 3], [3, 4]]) s2 = Series.from_array([[0, 5], [1, 5], [2, 5], [3, 5]]) self.case(ram, s1, s2, list(s1.pdseries.index[1:]), [0.3, 0.5, 0.7])
def test_analysis_histogram_heatmap(self): self.maxDiff = None series = Series.from_array([ [1628294400, 0], [1628337600, 0], [1628380800, 1], [1628424000, 1], [1628467200, 1], [1628510400, 0], [1628553600, 0], [1628596800, 0], ]) factory = NodeFactory.detector('test_node', 'SimpleThreshold') factory.set_param_value('inside', False) factory.set_param_value('strict', False) factory.set_param_value('lower', None) factory.set_param_value('upper', 1) factory.add_source(InputRef('input')) node = factory.build() pipeline = Pipeline([node]) analyzer = Analyzer(pipeline=pipeline, debug=True) analysis = analyzer.analyze({'input': series}) actual_output = analysis.output_format() expected_file = os.path.join(os.path.dirname(__file__), 'resources/analysis/expected_histogram_heatmap.json') # Uncomment to fix test # print(json.dumps(actual_output, indent=2), file=open(expected_file, 'w')) expected_output = json.loads(Path(expected_file).read_text()) self.assertEqual(expected_output, actual_output)
def test_rolling_aggregate_mean(self): factory = NodeFactory.transformer('test', 'RollingAggregate') factory.set_param_value('window', 2) factory.set_param_value('center', False) factory.set_param_value('min_periods', 0) factory.set_param_value('agg_method', 'mean') factory.add_source(InputRef('input')) ram = factory.build() self.case(ram, [0.0, 0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.0, 8.5, 7.5, 6.5, 5.5, 4.5, 3.5, 2.5, 1.5, 0.5])
def test_dropout_mean_prop(self): factory = NodeFactory.transformer('test', 'Dropout') factory.set_param_value('context_window', 2) factory.set_param_value('dropout_window', 1) factory.set_param_value('center', False) factory.set_param_value('min_periods', None) factory.set_param_value('agg_method', 'mean') factory.set_param_value('combine_method', 'prop') factory.set_param_value('combine_method_order', 'dropout-context') factory.add_source(InputRef('input')) ram = factory.build() self.case(ram, [1, 1, 3, 1, 1 / 3, 1, 1])
def test_exponential_moving_average_mean(self): factory = NodeFactory.transformer('test', 'ExponentialMovingAverage') factory.set_param_value('span', '4') factory.set_param_value('min_periods', '') factory.set_param_value('recursive', False) factory.set_param_value('agg_method', 'mean') factory.add_source(InputRef('input')) ewma = factory.build() expected_debug_info = {'alpha': 0.4} self.case(ewma, [2.095, 2.487, 2.283, 1.755, 1.041], expected_debug_info)
def test_dropout_centered_mean_sub(self): factory = NodeFactory.transformer('test', 'Dropout') factory.set_param_value('context_window', 4) factory.set_param_value('dropout_window', 2) factory.set_param_value('center', True) factory.set_param_value('min_periods', None) factory.set_param_value('agg_method', 'mean') factory.set_param_value('combine_method', 'sub') factory.set_param_value('combine_method_order', 'context-dropout') factory.add_source(InputRef('input')) ram = factory.build() self.case(ram, [1, 0, -2, 0, 1])
def test_analysis_without_debug(self): self.maxDiff = None series = self.build_triangle() factory = NodeFactory.transformer('test_node', 'RollingAggregate') factory.set_param_value('window', 5) factory.set_param_value('center', False) factory.set_param_value('min_periods', 0) factory.set_param_value('agg_method', 'max') factory.add_source(InputRef('input')) node = factory.build() pipeline = Pipeline([node]) analyzer = Analyzer(pipeline=pipeline, debug=False) analysis = analyzer.analyze({'input':series}) expected = { "anomalies": [], "series": { "input": [ [0, 0.0], [1000, 1.0], [2000, 2.0], [3000, 3.0], [4000, 4.0], [5000, 5.0], [6000, 6.0], [7000, 7.0], [8000, 8.0], [9000, 9.0], [10000, 9.0], [11000, 8.0], [12000, 7.0], [13000, 6.0], [14000, 5.0], [15000, 4.0], [16000, 3.0], [17000, 2.0], [18000, 1.0], [19000, 0.0], ] }, } self.assertEqual(expected, analysis.output_format())
def test_std_normalize(self): series = self.build_triangle() self.assertEqual( [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0], series.as_list()) factory = NodeFactory.transformer('std_normalize', 'StdNormalize') factory.add_source(InputRef('input')) stdnormalize = factory.build() self.case(series, stdnormalize, [ -1.5270292013639366, -1.1876893788386174, -0.8483495563132981, -0.5090097337879789, -0.16966991126265962, 0.16966991126265962, 0.5090097337879789, 0.8483495563132981, 1.1876893788386174, 1.5270292013639366, 1.5270292013639366, 1.1876893788386174, 0.8483495563132981, 0.5090097337879789, 0.16966991126265962, -0.16966991126265962, -0.5090097337879789, -0.8483495563132981, -1.1876893788386174, -1.5270292013639366 ])
def test_boxcox_fixed_lambda(self): series = self.build_triangle() factory = NodeFactory.transformer('boxcox', 'BoxCox') factory.set_param_value('lambda', 1) factory.add_source(InputRef('input')) boxcox = factory.build() expected_debug = { 'lambda': 1, 'fitted_lambda': 1, } self.maxDiff = None self.case(series, boxcox, [ 0.0, 5.0, 10.000000000000002, 14.999999999999998, 20.0, 20.0, 18.999999999999996, 17.999999999999996, 16.999999999999996, 16.0, 14.999999999999998, 14.0, 12.999999999999996, 12.0, 11.0, 10.000000000000002, 9.000000000000002, 8.000000000000002, 6.999999999999998, 5.999999999999999, 5.0, 3.999999999999999, 3.0, 2.0000000000000004, 1.0 ], expected_debug)
def test_fft_filter(self): series = self.build_triangle() self.assertEqual( [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0], series.as_list()) factory = NodeFactory.transformer('fft_filter', 'FFTFilter') factory.set_param_value('cutoff', '50%') factory.set_param_value('output', 'resid') factory.add_source(InputRef('input')) fft_filter = factory.build() self.case(series, fft_filter, [ 0.002508563093691407, 0.9927198663527221, 2.0113390739957944, 2.9857119332223716, 4.015838444032453, 4.984161555967546, 6.014288066777628, 6.988660926004205, 8.00728013364728, 8.997491436906309, 8.99749143690631, 8.007280133647278, 6.988660926004206, 6.014288066777628, 4.984161555967547, 4.015838444032454, 2.985711933222371, 2.0113390739957935, 0.9927198663527218, 0.002508563093691407 ])
def test_boxcox_implied_lambda(self): series = self.build_triangle() factory = NodeFactory.transformer('boxcox', 'BoxCox') # factory.set_param_value('lambda', 1) factory.add_source(InputRef('input')) boxcox = factory.build() expected_debug = { 'lambda': None, 'fitted_lambda': 0.7569500835813396, } self.case(series, boxcox, [ 0.0, 3.8069844496343204, 6.792535931588043, 9.45326887138231, 11.915823446529231, 11.915823446529231, 11.435878585907972, 10.950062851581707, 10.457989331193566, 9.959222501860888, 9.45326887138231, 8.939565111800931, 8.417462798503934, 7.88620846728669, 7.344917074353449, 6.792535931588043, 6.227794497220499, 5.649132457366781, 5.054593159635213, 4.441659075192572, 3.8069844496343204, 3.1459316503179107, 2.45169446968289, 1.7134270251968304, 0.9114396216016829 ], expected_debug)
def test_analysis_with_debug(self): self.maxDiff = None series = self.build_triangle() factory = NodeFactory.transformer('test_node', 'RollingAggregate') factory.set_param_value('window', 5) factory.set_param_value('center', False) factory.set_param_value('min_periods', 0) factory.set_param_value('agg_method', 'max') factory.add_source(InputRef('input')) node = factory.build() pipeline = Pipeline([node]) analyzer = Analyzer(pipeline=pipeline, debug=True) analysis = analyzer.analyze({'input': series}) actual_output = analysis.output_format() expected_file = os.path.join(os.path.dirname(__file__), 'resources/analysis/expected_simplified.json') # Uncomment to fix test # print(json.dumps(actual_output, indent=2), file=open(expected_file, 'w')) expected_output = json.loads(Path(expected_file).read_text()) self.assertEqual(expected_output, actual_output)
def prepare_factory(self): factory = NodeFactory.detector('test', 'Quantile') factory.add_source(InputRef('input')) return factory
def prepare_factory(self): factory = NodeFactory.detector('test', 'SimpleThreshold') factory.add_source(InputRef('input')) return factory