def test_signatures(self): decorator = SignatureDecorator() algorithm = IncrementalDistanceAlgorithm( signature=ParentChildByNameTopologySignature()) algorithm.prototypes = [simple_prototype()] decorator.wrap_algorithm(algorithm) decorator.start_tree() self.assertEqual(decorator.descriptive_data(), {"signature": [[[]]]}) decorator.finish_tree() self.assertEqual(decorator.descriptive_data(), {"signature": [[[]]]}) decorator.wrap_algorithm(algorithm) decorator.start_tree() for event in Event.from_tree(simple_monitoring_tree(), supported={ProcessStartEvent: True}): decorator.add_event(event) decorator.finish_tree() description = decorator.descriptive_data() self.assertEqual(len(description["signature"]), 1) self.assertEqual(len(set(description["signature"][0][0])), 3) self.assertEqual(len(description["signature"][0][0]), 4) self.assertEqual(description["signature"][0][0][0], "root_1") self.assertEqual(description["signature"][0][0][1], "test_149160533") self.assertEqual(description["signature"][0][0][3], "muh_149160533") decorator.start_tree() for event in Event.from_tree(simple_monitoring_tree(), supported={ProcessStartEvent: True}): decorator.add_event(event) decorator.finish_tree() description = decorator.descriptive_data() self.assertEqual(len(description["signature"]), 2)
def test_accumulated_decorator(self): decorator = SignaturePerformanceDecorator() algorithm = IncrementalDistanceAlgorithm() algorithm.prototypes = [simple_prototype()] decorator.wrap_algorithm(algorithm) decorator.start_tree() self.assertEqual(decorator.descriptive_data(), {'accumulated_signature_performance': [[None]]}) decorator.finish_tree() self.assertEqual(decorator.descriptive_data(), {'accumulated_signature_performance': [[None]]}) decorator.wrap_algorithm(algorithm) decorator.start_tree() for event in Event.from_tree(simple_monitoring_tree(), supported={ProcessStartEvent: True}): decorator.add_event(event) decorator.finish_tree() description = decorator.descriptive_data() self.assertEqual( type(description["accumulated_signature_performance"][0][0]), float) decorator.start_tree() for event in Event.from_tree(simple_monitoring_tree(), supported={ProcessStartEvent: True}): decorator.add_event(event) decorator.finish_tree() description = decorator.descriptive_data() print(description) self.assertEqual(len(description["accumulated_signature_performance"]), 2) self.assertEqual( type(description["accumulated_signature_performance"][1][0]), float)
def test_number_of_events(self): tree = Prototype() root = tree.add_node("root", pid=1, ppid=0, tme=0, exit_tme=0, param=2) for i in range(5): root.add_node("child_%d" % i, pid=i+2, ppid=1, tme=0, exit_tme=0, param=i*2) child = next(root.children()) child.add_node("child", pid=10, ppid=child.pid, tme=0, exit_tme=0, param=5) event_count = 0 for _ in Event.from_tree(tree, supported={ ProcessStartEvent: True, ProcessExitEvent: False, ParameterEvent: False }): event_count += 1 self.assertEqual(7, event_count) event_count = 0 for _ in Event.from_tree(tree, supported={ ProcessStartEvent: True, ProcessExitEvent: True, ParameterEvent: False }): event_count += 1 self.assertEqual(14, event_count) event_count = 0 for _ in Event.from_tree(tree, supported={ ProcessStartEvent: True, ProcessExitEvent: True, ParameterEvent: True }): event_count += 1 self.assertEqual(21, event_count)
def test_decorator(self): decorator = DistancePerformanceDecorator(accumulated=False) algorithm = IncrementalDistanceAlgorithm() algorithm.prototypes = [simple_prototype()] decorator.wrap_algorithm(algorithm) self.assertIsNone(decorator.data()) decorator.start_tree() self.assertEqual(decorator.descriptive_data(), {"distance_performance": [[]]}) decorator.finish_tree() self.assertEqual(decorator.descriptive_data(), {"distance_performance": [[]]}) decorator.wrap_algorithm(algorithm) decorator.start_tree() for event in Event.from_tree(simple_monitoring_tree(), supported={ProcessStartEvent: True}): decorator.add_event(event) decorator.finish_tree() description = decorator.descriptive_data() self.assertEqual(len(description["distance_performance"][0]), 4) decorator.start_tree() for event in Event.from_tree(simple_monitoring_tree(), supported={ProcessStartEvent: True}): decorator.add_event(event) decorator.finish_tree() description = decorator.descriptive_data() self.assertEqual(len(description["distance_performance"]), 2) self.assertEqual(len(description["distance_performance"][1]), 4)
def test_simple(self): decorator = AnomalyDecorator() algorithm = IncrementalDistanceAlgorithm() algorithm.prototypes = [simple_prototype()] decorator.wrap_algorithm(algorithm) algorithm.start_tree() for event in Event.from_tree(simple_monitoring_tree(), supported={ProcessStartEvent: True}): algorithm.add_event(event) algorithm.finish_tree() self.assertEqual(decorator.data(), [ [ [ [False, False, False, False, False] ] ] ]) algorithm.start_tree() for event in Event.from_tree(simple_monitoring_tree(), supported={ProcessStartEvent: True}): algorithm.add_event(event) algorithm.finish_tree() self.assertEqual(decorator.data(), [ [ [ [False, False, False, False, False] ] ], [ [ [False, False, False, False, False] ] ] ])
def test_ensemble_signature(self): decorator = SignatureDecorator() algorithm = IncrementalDistanceAlgorithm(signature=EnsembleSignature( signatures=[ ParentChildByNameTopologySignature(), ParentChildOrderByNameTopologySignature() ])) algorithm.prototypes = [simple_prototype(), simple_monitoring_tree()] decorator.wrap_algorithm(algorithm) algorithm.start_tree() for event in Event.from_tree(simple_monitoring_tree(), supported={ProcessStartEvent: True}): algorithm.add_event(event) algorithm.finish_tree() self.assertEqual( decorator.descriptive_data(), { 'signature': [[[ 'root_1', 'test_149160533', 'test_149160533', 'muh_149160533' ], [ '.0_root_1', '.0.0_test_245236498', '.0.0_test_245236498', '.0.1_muh_245236498' ]]] }) algorithm.start_tree() for event in Event.from_tree(simple_prototype(), supported={ProcessStartEvent: True}): algorithm.add_event(event) algorithm.finish_tree() self.assertEqual( decorator.descriptive_data(), { 'signature': [[[ 'root_1', 'test_149160533', 'test_149160533', 'muh_149160533' ], [ '.0_root_1', '.0.0_test_245236498', '.0.0_test_245236498', '.0.1_muh_245236498' ]], [[ 'root_1', 'test_149160533', 'muh_149160533', 'test_149160533', 'muh_149160533' ], [ '.0_root_1', '.0.0_test_245236498', '.0.1_muh_245236498', '.0.2_test_245236498', '.0.3_muh_245236498' ]]] })
def test_ensemble_with_prototypes(self): decorator = AnomalyDecorator() algorithm = IncrementalDistanceAlgorithm( signature=EnsembleSignature( signatures=[ParentChildByNameTopologySignature(), ParentChildOrderByNameTopologySignature()])) algorithm.prototypes = [simple_prototype(), simple_monitoring_tree()] decorator.wrap_algorithm(algorithm) algorithm.start_tree() for event in Event.from_tree(simple_monitoring_tree(), supported={ProcessStartEvent: True}): algorithm.add_event(event) algorithm.finish_tree() self.assertEqual(decorator.data(), [ [ [ [False, False, False, False, False], [False, False, False, False, False] ], [ [False, False, False, False, True], [False, False, False, False, False] ] ] ])
def test_normalized_results(self): decorator = DistanceDecorator(normalized=True) algorithm = IncrementalDistanceAlgorithm( signature=EnsembleSignature( signatures=[ParentChildByNameTopologySignature(), ParentChildOrderByNameTopologySignature()]) ) algorithm.prototypes = [simple_prototype(), simple_monitoring_tree()] decorator.wrap_algorithm(algorithm) algorithm.start_tree() self.assertEqual([[[[], []], [[], []]]], decorator.data()) for event in Event.from_tree(simple_monitoring_tree(), supported={ProcessStartEvent: True}): algorithm.add_event(event) algorithm.finish_tree() self.assertEqual([[ [ # ParentChildByNameTopologySignature [2 / 3, 1 / 3, 1 / 3, 0.0], # simple_prototype [2 / 3, 1 / 3, 1 / 3, 0.0] # simple_monitoring_tree ], [ # ParentChildOrderByNameTopologySignature [4 / 5, 3 / 5, 3 / 5, 2 / 5], [2 / 3, 1 / 3, 1 / 3, 0.0] ]]], decorator.data())
def _test_algorithm(self, prototype=None, tree=None): signature = Signature() algorithm = TreeEditDistanceAlgorithm(signature=signature) algorithm.prototypes = [prototype] algorithm.start_tree() algorithm.add_events(Event.from_tree(tree, supported={ProcessStartEvent: True})) result = algorithm.finish_tree() return result[0]
def test_simple_normalized_matrix(self): decorator = DistanceMatrixDecorator(normalized=True) algorithm = IncrementalDistanceAlgorithm() algorithm.prototypes = [simple_prototype()] decorator.wrap_algorithm(algorithm) decorator.start_tree() self.assertEqual(decorator.descriptive_data(), {"normalized_matrix": [[[None]]]}) decorator.finish_tree() self.assertEqual(decorator.descriptive_data(), {"normalized_matrix": [[[1]]]}) decorator.wrap_algorithm(algorithm) decorator.start_tree() for event in Event.from_tree(simple_prototype(), supported={ProcessStartEvent: True}): decorator.add_event(event) decorator.finish_tree() self.assertEqual(decorator.descriptive_data(), {"normalized_matrix": [[[0]]]}) algorithm.prototypes = [ simple_prototype(), simple_additional_monitoring_tree() ] decorator.wrap_algorithm(algorithm) decorator.start_tree() for event in Event.from_tree(simple_prototype(), supported={ProcessStartEvent: True}): decorator.add_event(event) decorator.finish_tree() self.assertEqual(decorator.descriptive_data(), {"normalized_matrix": [[[0, .4]]]}) decorator.start_tree() for event in Event.from_tree(simple_additional_monitoring_tree(), supported={ProcessStartEvent: True}): decorator.add_event(event) decorator.finish_tree() self.assertEqual(decorator.descriptive_data(), {"normalized_matrix": [[[0.0, .4]], [[.4, 0.0]]]}) self.assertRaises(MatrixDoesNotMatchBounds, decorator.start_tree)
def test_two_prototypes(self): decorator = DistanceDecorator(normalized=False) algorithm = IncrementalDistanceAlgorithm() algorithm.prototypes = [simple_prototype(), simple_monitoring_tree()] decorator.wrap_algorithm(algorithm) algorithm.start_tree() self.assertEqual([[[[], []]]], decorator.data()) for event in Event.from_tree(simple_monitoring_tree(), supported={ProcessStartEvent: True}): algorithm.add_event(event) algorithm.finish_tree() self.assertEqual([[[[2, 1, 1, 0], [2, 1, 1, 0]]]], decorator.data())
def test_ensemble(self): decorator = DistanceDecorator(normalized=False) algorithm = IncrementalDistanceAlgorithm( signature=EnsembleSignature( signatures=[ParentChildByNameTopologySignature(), ParentChildOrderByNameTopologySignature()])) algorithm.prototypes = [simple_prototype()] decorator.wrap_algorithm(algorithm) algorithm.start_tree() self.assertEqual([[[[]], [[]]]], decorator.data()) for event in Event.from_tree(simple_monitoring_tree(), supported={ProcessStartEvent: True}): algorithm.add_event(event) algorithm.finish_tree() self.assertEqual([[[[2, 1, 1, 0]], [[4, 3, 3, 2]]]], decorator.data())
def test_compression_with_events(self): decorator = CompressionFactorDecorator() algorithm = IncrementalDistanceAlgorithm() algorithm.prototypes = [simple_prototype(), simple_prototype()] decorator.wrap_algorithm(algorithm) decorator.start_tree() for event in Event.from_tree(simple_monitoring_tree(), supported={ProcessStartEvent: True}): decorator.add_event(event) decorator.finish_tree() self.assertEqual( decorator.descriptive_data(), { "compression": { "prototypes": [[.4, .4]], "monitoring": [[.25]], "accumulated": [.7] } })
def test_decorator(self): decorator = DataDecorator() algorithm = IncrementalDistanceAlgorithm() algorithm.prototypes = [simple_prototype()] decorator.wrap_algorithm(algorithm) decorator.start_tree() self.assertEqual( decorator.descriptive_data(), { "data": { "prototypes": { "original": [[5]], "converted": [[3]] }, "monitoring": {} } }) decorator.finish_tree() self.assertEqual( decorator.descriptive_data(), { "data": { "prototypes": { "original": [[5]], "converted": [[3]] }, "monitoring": { "original": [[0]], "converted": [[0]] } } }) algorithm.prototypes = [simple_prototype(), simple_prototype()] decorator.wrap_algorithm(algorithm) decorator.start_tree() for event in Event.from_tree(simple_monitoring_tree(), supported={ProcessStartEvent: True}): decorator.add_event(event) decorator.finish_tree() self.assertEqual( decorator.descriptive_data(), { "data": { "prototypes": { # FIXME: hier bin ich mir nicht sicher, ob dass das sagt, # was ich gern hätte... "original": [[5, 5]], "converted": [[3, 3]] }, "monitoring": { "original": [[4]], "converted": [[3]] } } }) decorator.start_tree() for event in Event.from_tree(simple_monitoring_tree(), supported={ProcessStartEvent: True}): decorator.add_event(event) decorator.finish_tree() self.assertEqual( decorator.descriptive_data(), { "data": { "prototypes": { "original": [[5, 5]], # FIXME: please check! "converted": [[3, 3]] }, "monitoring": { "original": [[4], [4]], "converted": [[3], [3]] } } })