def test_simple_cache(self): signature = EnsembleSignature( signatures=[ParentChildByNameTopologySignature()]) cache = signature.signature_cache_class() for node in simple_unique_node_tree().nodes(): tokens = signature.get_signature(node, parent=node.parent()) if not cache.node_count(): # nothing added yet self.assertEqual([], cache.multiplicity(signature=tokens)) else: self.assertEqual([0], cache.multiplicity(signature=tokens)) cache[tokens, ProcessStartEvent] = {"value": 0} self.assertEqual([1], cache.multiplicity(signature=tokens)) self.assertEqual([4], cache.node_count()) self.assertEqual([4], cache.multiplicity()) signature = EnsembleSignature( signatures=[ParentChildByNameTopologySignature()]) cache = signature.signature_cache_class() for node in simple_monitoring_tree().nodes(): cache[signature.get_signature(node, parent=node.parent()), ProcessStartEvent] = { "value": 0 } self.assertEqual([3], cache.node_count()) self.assertEqual([4], cache.multiplicity())
def test_to_index(self): prototype = simple_prototype() index = prototype.to_index(signature=ParentChildByNameTopologySignature()) self.assertEqual(2, index.multiplicity(signature="root_1")) self.assertEqual(4, index.multiplicity(signature="test_149160533")) self.assertEqual(4, index.multiplicity(signature="muh_149160533")) self.assertEqual(3, index.node_count()) self.assertEqual(2, index.get_statistics( signature="muh_149160533", key="value", event_type=ProcessExitEvent)._statistics[1].mean) self.assertEqual(0, index.get_statistics( signature="muh_149160533", key="value", event_type=ProcessExitEvent).distance(2)) index = prototype.to_index( signature=ParentChildByNameTopologySignature(), supported={ ProcessStartEvent: True, ProcessExitEvent: False }) self.assertEqual(3, index.node_count()) self.assertEqual(1, index.multiplicity(signature="root_1")) self.assertEqual(2, index.multiplicity(signature="test_149160533")) self.assertEqual(2, index.multiplicity(signature="muh_149160533"))
def test_simple_prototype_cache(self): signature = EnsembleSignature( signatures=[ParentChildByNameTopologySignature()]) cache = signature.prototype_signature_cache_class( supported={ProcessExitEvent: True}) tree = simple_unique_node_tree() for node in tree.nodes(): tokens = signature.get_signature(node, parent=node.parent()) if not cache.node_count(): # nothing added yet self.assertEqual([], cache.get(signature=tokens)) else: self.assertTrue( isinstance(cache.get(signature=tokens)[0], dict)) cache[tokens, tree, ProcessExitEvent] = {"value": 1} stats = cache.get_statistics(signature=tokens, prototype=tree, key="value", event_type=ProcessExitEvent) self.assertTrue(stats[0].count() >= 1) self.assertEqual([4], cache.node_count()) self.assertEqual([4], cache.multiplicity()) signature = EnsembleSignature( signatures=[ParentChildByNameTopologySignature()]) cache = signature.signature_cache_class() for node in simple_monitoring_tree().nodes(): cache[signature.get_signature(node, parent=node.parent()), ProcessStartEvent] = { "value": 0 } self.assertEqual([3], cache.node_count()) self.assertEqual([4], cache.multiplicity())
def test_real_trees(self): def distance_builder(**kwargs): distance = StartExitDistance() distance.supported[TrafficEvent] = True return distance tree_one = real_tree() tree_two = real_tree(path="data/c01-007-102/2/1129-2-process.csv") prototype_names = ["1", "2"] prototype_caches = [] for index, tree in enumerate([tree_one, tree_two]): prototype_caches.append(PrototypeSignatureCache.from_signature_caches( [tree.to_index(signature=ParentChildByNameTopologySignature(), supported={ ProcessStartEvent: True, ProcessExitEvent: True, TrafficEvent: True }, statistics_cls=SetStatistics)], prototype=prototype_names[index], threshold=0)) decorator = AnomalyDecorator() algorithm = IncrementalDistanceAlgorithm( signature=ParentChildByNameTopologySignature(), distance=distance_builder, cache_statistics=SetStatistics ) prototype_cache = prototype_caches[0] for cache in prototype_caches[1:]: prototype_cache += cache algorithm.cluster_representatives( signature_prototypes=[prototype_cache], prototypes=prototype_names ) decorator.wrap_algorithm(algorithm) for tree in [tree_one, tree_two]: algorithm.start_tree() for event in tree.event_iter(supported=algorithm.supported): try: algorithm.add_event(event) except EventNotSupportedException: pass algorithm.finish_tree() # First tree vs. first is False for start self.assertFalse(decorator.data()[0][0][0][0]) # ... and end self.assertFalse(decorator.data()[0][0][0][-1]) # First tree vs. second is False for start self.assertFalse(decorator.data()[0][0][1][0]) # and True for end self.assertTrue(decorator.data()[0][0][1][-1]) # Second tree vs. first is False for start self.assertFalse(decorator.data()[1][0][0][0]) # ... and True for end self.assertTrue(decorator.data()[1][0][0][-1]) # Second tree vs. second is False for start self.assertFalse(decorator.data()[1][0][1][0]) # ... and False for end self.assertFalse(decorator.data()[1][0][1][-1])
def test_simple_clustering(self): clusterer = Clustering(distance=StartExitDistance()) # create an index to cluster builder = CSVTreeBuilder() tree = builder.build(self.file_path_one) tree_two = builder.build(self.file_path_two) tree_index = tree.to_index( signature=ParentChildByNameTopologySignature()) tree_two_index = tree_two.to_index( signature=ParentChildByNameTopologySignature()) clusterer[1] = tree_index clusterer[1] = tree_two_index self.assertEqual(0, len(clusterer.clusterer.clusters)) self.assertEqual(2, len(clusterer.clusterer.noise))
def test_two_prototype_ensembles(self): signature = EnsembleSignature(signatures=[ ParentChildByNameTopologySignature(), ParentChildOrderTopologySignature() ]) cache = signature.prototype_signature_cache_class() prototype = simple_unique_node_tree() for node in prototype.nodes(): tokens = signature.get_signature(node, parent=node.parent()) if not cache.node_count(): self.assertEqual([], cache.get(signature=tokens)) else: received = cache.get(signature=tokens) self.assertEqual(2, len(received)) self.assertTrue(isinstance(received[0], dict)) cache[tokens, prototype, ProcessStartEvent] = {"value": 1} received = cache.get(signature=tokens) self.assertEqual(2, len(received)) self.assertTrue(isinstance(received[0], dict)) self.assertTrue( received[0][prototype][ProcessStartEvent]["value"].count() >= 1 ) self.assertEqual([4, 4], cache.node_count()) self.assertEqual([4, 4], cache.multiplicity())
def test_parameter_distance(self): prototype = Prototype() root = prototype.add_node("root", tme=0, exit_tme=0, pid=1, ppid=0, param=1) for i in range(5): root.add_node("child_%d" % i, tme=0, exit_tme=0, pid=i + 2, ppid=1, param=1) next(root.children()).add_node("child", tme=0, exit_tme=0, pid=8, ppid=2, param=1) tree = Prototype() root = tree.add_node("root", tme=0, exit_tme=0, pid=1, ppid=0, param=1) for i in range(5): root.add_node("child_%d" % i, tme=0, exit_tme=0, pid=i + 2, ppid=1, param=4) next(root.children()).add_node("child", tme=0, exit_tme=0, pid=8, ppid=2, param=4) for weight, result in [(1, 0), (.5, 6), (0, 12)]: def distance(**kwargs): distance = StartExitDistance(weight=weight, **kwargs) distance.supported = { ProcessStartEvent: True, ProcessExitEvent: True, ParameterEvent: True } return distance signature = EnsembleSignature(signatures=[ParentChildByNameTopologySignature()]) algorithm = IncrementalDistanceAlgorithm( signature=signature, distance=distance, cache_statistics=SetStatistics ) decorator = DistanceMatrixDecorator(normalized=False) decorator.wrap_algorithm(algorithm) algorithm.prototypes = [prototype] algorithm.start_tree() algorithm.add_events(tree.event_iter(supported=algorithm.supported)) algorithm.finish_tree() self.assertEqual(result, decorator.data()[0][0][0])
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_more_cluster_representatives(self): tree_1 = real_tree() tree_2 = real_tree() signature = ParentChildByNameTopologySignature() signature_caches = [] for tree in [tree_1, tree_2]: signature_caches.append( tree.to_index(signature=signature, supported={ ProcessStartEvent: True, ProcessExitEvent: True, TrafficEvent: True }, statistics_cls=SetStatistics)) cr = PrototypeSignatureCache.from_signature_caches(signature_caches, prototype=tree_1) for token in cr: self.assertEqual(cr.multiplicity(token, tree_1), signature_caches[0].multiplicity(token)) self.assertEqual( cr.get_statistics(signature=token, key="value", prototype=tree_1, event_type=ProcessExitEvent).count(), signature_caches[0].get_statistics( signature=token, key="value", event_type=ProcessExitEvent).count())
def test_negative_values(self): def distance_builder(**kwargs): distance = SimpleDistance(**kwargs) return distance tree_one = real_tree(path="data/c01-007-102/2/1078-2-process.csv", absolute=True) tree_two = real_tree(path="data/c01-007-102/2/1165-2-process.csv", absolute=True) signature = ParentChildByNameTopologySignature() algorithm = IncrementalDistanceAlgorithm( signature=signature, distance=distance_builder, cache_statistics=SetStatistics) algorithm.prototypes = [tree_one, tree_two] decorator = DistanceMatrixDecorator(normalized=True) decorator.wrap_algorithm(algorithm) algorithm.start_tree() for event in tree_one.event_iter(supported=algorithm.supported): try: algorithm.add_event(event) except EventNotSupportedException: pass algorithm.finish_tree() algorithm.start_tree() for event in tree_two.event_iter(supported=algorithm.supported): try: algorithm.add_event(event) except EventNotSupportedException: pass algorithm.finish_tree() print(decorator.data()) self.assertTrue(False)
def test_external(self): def distance(**kwargs): distance = StartExitDistance(weight=0, **kwargs) distance.supported[TrafficEvent] = True return distance algorithm = IncrementalDistanceAlgorithm( signature=ParentChildByNameTopologySignature(), distance=distance, cache_statistics=SplittedStatistics) decorator = DistanceMatrixDecorator(normalized=False) decorator.wrap_algorithm(algorithm) the_tree = real_tree( path="data/c01-007-102/2/1146-2-process.csv", absolute=True ) algorithm.prototypes = [the_tree] algorithm.start_tree() for event in the_tree.event_iter(supported=algorithm.supported): try: algorithm.add_event(event) except EventNotSupportedException: pass algorithm.finish_tree() print(decorator.data()) self.assertTrue(False)
def test_from_signature_frequency(self): def cache_builder(): return SignatureCache(supported={ ProcessStartEvent: True, ProcessExitEvent: True, TrafficEvent: True }, statistics_cls=SetStatistics) prototype = simple_prototype() signature = ParentChildByNameTopologySignature() validation_index = prototype.to_index(signature=signature, cache=cache_builder()) prototype_index = PrototypeSignatureCache.from_signature_caches( [ prototype.to_index(signature, cache=cache_builder()) for _ in range(10) ], prototype=prototype, threshold=0) self.assertEqual(validation_index.multiplicity(), prototype_index.multiplicity()) for token in validation_index: self.assertEqual( validation_index.multiplicity(signature=token), prototype_index.multiplicity(signature=token, prototype=prototype))
def test_ensemble(self): def distance(**kwargs): distance = StartExitDistance(weight=.5, **kwargs) distance.supported[TrafficEvent] = False return distance signature = EnsembleSignature( signatures=[ ParentChildByNameTopologySignature(), ParentSiblingSignature(width=2)]) algorithm = IncrementalDistanceAlgorithm( signature=signature, distance=distance, cache_statistics=SetStatistics ) decorator = DistanceMatrixDecorator(normalized=True) decorator.wrap_algorithm(algorithm) algorithm.prototypes = [simple_prototype()] algorithm.start_tree() for event in simple_monitoring_tree().event_iter(supported=algorithm.supported): try: algorithm.add_event(event) except EventNotSupportedException: pass algorithm.finish_tree() print(decorator.data()) self.assertTrue(False)
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_same_attributes_different_count(self): tree_1 = Prototype() root = tree_1.add_node("root", pid=1, ppid=0, tme=0, exit_tme=0) for _ in range(5): root.add_node("node", pid=2, ppid=1, tme=0, exit_tme=0) tree_2 = Prototype() root = tree_2.add_node("root", pid=1, ppid=0, tme=0, exit_tme=0) for _ in range(35): root.add_node("node", pid=2, ppid=1, tme=0, exit_tme=0) signature = ParentChildByNameTopologySignature() algorithm = IncrementalDistanceAlgorithm( signature=signature, distance=lambda **kwargs: StartExitDistance(weight=0, **kwargs), cache_statistics=SplittedStatistics) algorithm.prototypes = [tree_1, tree_2] decorator = DistanceMatrixDecorator(normalized=False) decorator.wrap_algorithm(algorithm) algorithm.start_tree() for event in tree_1.event_iter(supported=algorithm.supported): try: algorithm.add_event(event) except EventNotSupportedException: pass algorithm.finish_tree() algorithm.start_tree() for event in tree_2.event_iter(supported=algorithm.supported): try: algorithm.add_event(event) except EventNotSupportedException: pass algorithm.finish_tree() data = decorator.data() self.assertEqual(data[0][0][1], data[1][0][0])
def test_merging(self): signature = ParentChildByNameTopologySignature() one = simple_prototype() two = simple_additional_monitoring_tree() prototype_one = one.to_prototype(signature=signature, supported={ ProcessStartEvent: True, ProcessExitEvent: False, TrafficEvent: False }, statistics_cls=SetStatistics) prototype_two = two.to_prototype(signature=signature, supported={ ProcessStartEvent: True, ProcessExitEvent: False, TrafficEvent: False }, statistics_cls=SetStatistics) self.assertEqual(one.node_count(), prototype_one.multiplicity(prototype=one)) self.assertEqual(two.node_count(), prototype_two.multiplicity(prototype=two)) self.assertEqual(0, prototype_one.multiplicity(two)) prototype_one += prototype_two self.assertEqual(one.node_count(), prototype_one.multiplicity(prototype=one)) self.assertEqual(two.node_count(), prototype_one.multiplicity(prototype=two)) self.assertEqual(one.node_count() + two.node_count(), prototype_one.multiplicity()) prototype_three = one.to_prototype(signature=signature, supported={ ProcessStartEvent: True, ProcessExitEvent: True, TrafficEvent: False }, statistics_cls=SetStatistics) prototype_four = two.to_prototype(signature=signature, supported={ ProcessStartEvent: True, ProcessExitEvent: True, TrafficEvent: False }, statistics_cls=SetStatistics) self.assertEqual(one.node_count() * 2, prototype_three.multiplicity(prototype=one)) self.assertEqual(two.node_count() * 2, prototype_four.multiplicity(prototype=two)) self.assertEqual(0, prototype_three.multiplicity(two)) prototype_three += prototype_four self.assertEqual(one.node_count() * 2, prototype_three.multiplicity(prototype=one)) self.assertEqual(two.node_count() * 2, prototype_three.multiplicity(prototype=two)) self.assertEqual(one.node_count() * 2 + two.node_count() * 2, prototype_three.multiplicity())
def test_simple_signature(self): signature = EnsembleSignature(signatures=[ParentChildByNameTopologySignature()]) self.assertEqual(1, signature.count) for node in simple_monitoring_tree().nodes(): signature.prepare_signature(node, parent=node.parent()) for node in simple_monitoring_tree().nodes(): token = signature.get_signature(node, parent=node.parent()) self.assertIsNotNone(token) self.assertEqual(1, len(token))
def test_mean(self): distance = ClusterDistance(distance=StartExitDistance()) prototype_one = simple_prototype() prototype_two = simple_monitoring_tree() signature = ParentChildByNameTopologySignature() mean_prototype = distance.mean([ prototype_one.to_index(signature), prototype_two.to_index(signature) ]) self.assertEqual(3, mean_prototype.node_count())
def test_ensemble_result(self): """ I recognised that apparently sometimes (or ever?) values of ensemble methods don't match results from single calculations. Therefore this test should uniquely test for this. """ tree = real_tree() tree_generator = TEDGenerator( operation_generator=RandomOperation( insert_probability=.5, delete_probability=.5), costs=[TreeEditDistanceCost()], seed=1234) disturbed_tree = tree_generator.generate(tree) signatures = [ParentChildByNameTopologySignature(), ParentCountedChildrenByNameTopologySignature(count=2)] # First test results from ensemble ensemble_signature = EnsembleSignature(signatures=signatures) decorator = DistanceMatrixDecorator(normalized=False) algorithm = IncrementalDistanceAlgorithm( signature=ensemble_signature, distance=StartDistance ) decorator.wrap_algorithm(algorithm) algorithm.prototypes = [tree] algorithm.start_tree() for event in disturbed_tree.event_iter(supported=algorithm.supported): try: algorithm.add_event(event) except EventNotSupportedException: pass algorithm.finish_tree() ensemble_data = decorator.data() # Second, validate this result with single measurements single_data = {} for index, signature in enumerate(signatures): decorator = DistanceMatrixDecorator(normalized=False) algorithm = IncrementalDistanceAlgorithm( signature=signature, distance=StartDistance ) decorator.wrap_algorithm(algorithm) algorithm.prototypes = [real_tree()] algorithm.start_tree() for event in disturbed_tree.event_iter(supported=algorithm.supported): try: algorithm.add_event(event) except EventNotSupportedException: pass algorithm.finish_tree() single_data[index] = decorator.data() for index, _ in enumerate(signatures): self.assertEqual(ensemble_data[0][index][0], single_data[index][0][0][0])
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_two_ensembles_different_frequency(self): signature = EnsembleSignature(signatures=[ ParentChildByNameTopologySignature(), ParentChildOrderTopologySignature() ]) cache = signature.signature_cache_class() for node in simple_monitoring_tree().nodes(): cache[signature.get_signature(node, parent=node.parent()), ProcessStartEvent] = { "value": 0 } self.assertEqual([3, 4], cache.node_count()) self.assertEqual([4, 4], cache.multiplicity())
def test_maximum_generator_newerincremental_with_signature_distance(self): rg = RandomGenerator(relative_matching=1) signature = ParentChildByNameTopologySignature() alg = IncrementalDistanceAlgorithm(signature=signature) alg.prototypes = [rg.prototype] decorator = DistanceDecorator(normalized=True) decorator.algorithm = alg decorator.start_tree() for event in rg: decorator.add_event(event) decorator.finish_tree() self.assertEqual(decorator.data()[-1][-1][-1][-1], 0)
def test_from_signatures(self): cache = SignatureCache(supported={ ProcessStartEvent: True, ProcessExitEvent: True, TrafficEvent: True }, statistics_cls=SetStatistics) cache_two = SignatureCache(supported={ ProcessStartEvent: True, ProcessExitEvent: True, TrafficEvent: True }, statistics_cls=SetStatistics) prototype = simple_additional_monitoring_tree() other_prototype = simple_prototype() tree_index = prototype.to_index( signature=ParentChildByNameTopologySignature(), cache=cache) prototype_index = other_prototype.to_index( signature=ParentChildByNameTopologySignature(), cache=cache_two) prototype_cache = PrototypeSignatureCache.from_signature_caches( [tree_index, prototype_index], prototype=1, threshold=.9) self.assertEqual(9, prototype_cache.multiplicity()) self.assertEqual(3, prototype_cache.node_count())
def test_initialisation_of_cluster_representatitives(self): def distance_builder(**kwargs): distance = StartExitDistance(weight=.5, **kwargs) distance.supported[TrafficEvent] = True return distance tree_one = real_tree() tree_two = real_tree(path="data/c01-007-102/2/1129-2-process.csv") tree_three = real_tree(path="data/c01-007-102/2/1136-3-process.csv") distance = StartExitDistance(weight=.5) distance.supported[TrafficEvent] = True signature = ParentChildByNameTopologySignature() tree_profiles = [ tree_one.to_index(signature=signature, supported=distance.supported, statistics_cls=SetStatistics), tree_two.to_index(signature=signature, supported=distance.supported, statistics_cls=SetStatistics), tree_three.to_index(signature=signature, supported=distance.supported, statistics_cls=SetStatistics) ] algorithm = IncrementalDistanceAlgorithm( signature=ParentChildByNameTopologySignature(), distance=distance_builder, cache_statistics=SetStatistics) cluster_distance = ClusterDistance(distance=distance) prototype_names = ["test"] prototype_signatures = [] for prototype in prototype_names: prototype_signatures.append( cluster_distance.mean(tree_profiles, prototype=prototype)) algorithm.cluster_representatives( signature_prototypes=prototype_signatures, prototypes=prototype_names) algorithm.start_tree()
def test_creation(self): test_algorithm = algorithm(ParentChildByNameTopologySignature()) test_algorithm.prototypes = ["1", "2", "3"] distance = Distance(signature_count=test_algorithm.signature.count) distance.init_distance( prototypes=test_algorithm.prototypes, signature_prototypes=test_algorithm.signature_prototypes) last_index = 0 for index, dist in enumerate( distance.iter_on_prototypes(test_algorithm.prototypes)): self.assertEqual(dist, [0]) last_index = index self.assertEqual(last_index, 2) self.assertEqual(distance.node_count(), [0]) self.assertFalse(distance.is_prototype_based_on_original())
def test_empty_nodes(self): signature = EnsembleSignature( signatures=[ParentChildByNameTopologySignature(), ParentCountedChildrenByNameTopologySignature(count=3)]) algorithm = IncrementalDistanceAlgorithm(signature=signature) algorithm.prototypes = [simple_prototype()] algorithm.start_tree() for event in simple_monitoring_tree().event_iter(supported=algorithm.supported): try: distance = algorithm.add_event(event) except EventNotSupportedException: pass algorithm.finish_tree() self.assertEqual([[0], [7]], distance[-1])
def test_two_prototype_ensembles_different_frequency(self): signature = EnsembleSignature(signatures=[ ParentChildByNameTopologySignature(), ParentChildOrderTopologySignature() ]) cache = signature.prototype_signature_cache_class( supported={ProcessExitEvent: True}) prototype = simple_monitoring_tree() for node in prototype.nodes(): cache[signature.get_signature(node, parent=node.parent()), prototype, ProcessExitEvent] = { "value": 1 } self.assertEqual([3, 4], cache.node_count()) self.assertEqual([4, 4], cache.multiplicity())
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_two_ensembles(self): signature = EnsembleSignature(signatures=[ ParentChildByNameTopologySignature(), ParentChildOrderTopologySignature() ]) cache = signature.signature_cache_class() for node in simple_unique_node_tree().nodes(): tokens = signature.get_signature(node, parent=node.parent()) if not cache.node_count(): self.assertEqual([], cache.multiplicity(signature=tokens)) else: self.assertEqual([0, 0], cache.multiplicity(signature=tokens)) cache[tokens, ProcessStartEvent] = {"value": 0} self.assertEqual([1, 1], cache.multiplicity(signature=tokens)) self.assertEqual([4, 4], cache.node_count()) self.assertEqual([4, 4], cache.multiplicity())