Exemplo n.º 1
0
    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])
Exemplo n.º 2
0
    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)
Exemplo n.º 3
0
    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])
Exemplo n.º 4
0
    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_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)
Exemplo n.º 6
0
    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])
Exemplo n.º 7
0
    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)
Exemplo n.º 8
0
    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]
                ]
            ]
        ])
Exemplo n.º 9
0
    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]
                ]
            ]
        ])
Exemplo n.º 10
0
 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)
Exemplo n.º 11
0
 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)
Exemplo n.º 12
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)
Exemplo n.º 13
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)
Exemplo n.º 14
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'
                  ]]]
            })
Exemplo n.º 15
0
    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())
Exemplo n.º 16
0
    def test_maximum_generator_incremental_distance(self):
        rg = RandomGenerator(relative_matching=1)
        alg = IncrementalDistanceAlgorithm()
        alg.prototypes = [rg.prototype]

        decorator = DistanceDecorator(normalized=True)
        decorator.algorithm = alg

        decorator.start_tree()
        for event in rg:
            distance = decorator.add_event(event)[0]
        decorator.finish_tree()
        self.assertEqual(distance[0], [0])
Exemplo n.º 17
0
    def test_default_generator_incremental_distance(self):
        rg = RandomGenerator(seed=1234)
        alg = IncrementalDistanceAlgorithm()
        alg.prototypes = [rg.prototype]

        vector_decorator = DistanceDecorator()
        vector_decorator._algorithm = alg

        vector_decorator.start_tree()
        for event in rg:
            distance = vector_decorator.add_event(event)[0]
        vector_decorator.finish_tree()
        self.assertEqual(distance[0], [110])
Exemplo n.º 18
0
    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)
Exemplo n.º 19
0
    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])
Exemplo n.º 20
0
    def test_count_signature_for_correct_zero_distance(self):
        signature = ParentCountedChildrenByNameTopologySignature(count=3)
        algorithm = IncrementalDistanceAlgorithm(signature=signature)
        decorator = DistanceMatrixDecorator(normalized=False)
        decorator.wrap_algorithm(algorithm)
        algorithm.prototypes = [real_tree()]

        algorithm.start_tree()
        for event in real_tree().event_iter(include_marker=True, supported=algorithm.supported):
            try:
                algorithm.add_event(event)
            except EventNotSupportedException:
                pass
        algorithm.finish_tree()
        print(algorithm._signature_prototypes._prototype_dict[0]._prototype_dict.keys())
        self.assertEqual([[[0]]], decorator.data())
Exemplo n.º 21
0
    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]
             }
         })
Exemplo n.º 23
0
    def test_bigger_tree(self):
        rg = RandomGenerator(prototype_node_count=100,
                             tree_node_count=200,
                             relative_matching=.9,
                             relative_repetition=.5,
                             seed=1234)
        signature = ParentChildByNameTopologySignature()
        alg = IncrementalDistanceAlgorithm(signature=signature)
        alg.prototypes = [rg.prototype]

        decorator = DistanceDecorator(normalized=True)
        decorator.wrap_algorithm(alg)

        decorator.start_tree()
        for event in rg:
            decorator.add_event(event)
        decorator.finish_tree()
        self.assertAlmostEqual(decorator.data()[-1][-1][-1][-1], 0.185, 2)
Exemplo n.º 24
0
    def test_attribute_distance(self):
        def distance_buidler(**kwargs):
            distance = StartExitDistance(weight=0, **kwargs)
            distance.supported = {
                ProcessStartEvent: True,
                ProcessExitEvent: True,
                TrafficEvent: True
            }
            return distance

        tree_builder = CSVTreeBuilder()
        tree_1 = tree_builder.build(
            os.path.join(os.path.dirname(assess_tests.__file__),
                         "data/c01-007-102/2/1129-2-process.csv"))
        tree_2 = tree_builder.build(
            os.path.join(os.path.dirname(assess_tests.__file__),
                         "data/c01-007-102/2/1136-3-process.csv"))
        signature = ParentChildByNameTopologySignature()
        algorithm = IncrementalDistanceAlgorithm(
            signature=signature,
            distance=distance_buidler,
            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()
        print(decorator.data())
        self.assertEqual(4, abs(data[0][0][1] - data[1][0][0]))
Exemplo n.º 25
0
    def test_symmetry_optimisation(self):
        tree = CSVTreeBuilder().build(
            os.path.join(os.path.dirname(assess_tests.__file__),
                         "data/c01-007-102/2/1129-2-process.csv"))
        signature = ParentChildByNameTopologySignature()
        algorithm = IncrementalDistanceAlgorithm(
            signature=signature,
            distance=lambda **kwargs: StartExitDistance(weight=0, **kwargs),
            cache_statistics=SplittedStatistics)
        algorithm.prototypes = [tree]
        decorator = DistanceMatrixDecorator(normalized=False)
        decorator.wrap_algorithm(algorithm)

        algorithm.start_tree()
        for event in tree.event_iter(supported=algorithm.supported):
            try:
                algorithm.add_event(event)
            except EventNotSupportedException:
                pass
        algorithm.finish_tree()
        self.assertEqual(0, decorator.data()[0][0][0])
Exemplo n.º 26
0
    def test_node_count_for_correct_zero_distance(self):
        signature = EnsembleSignature(
            signatures=[ParentChildByNameTopologySignature(),
                        ParentCountedChildrenByNameTopologySignature(count=3)])
        algorithm = IncrementalDistanceAlgorithm(
            signature=signature, distance=SimpleDistance)
        data_decorator = DataDecorator()
        data_decorator.wrap_algorithm(algorithm)
        algorithm.prototypes = [real_tree()]

        algorithm.start_tree()
        for event in real_tree().event_iter(include_marker=True, supported=algorithm.supported):
            try:
                algorithm.add_event(event)
            except EventNotSupportedException:
                pass
        algorithm.finish_tree()
        self.assertEqual([tree_value for values in data_decorator.data().get(
            "prototypes", {}).get("converted", []) for tree_value in values],
            [tree_value for values in data_decorator.data().get(
             "monitoring", {}).get("converted", []) for tree_value in values])
Exemplo n.º 27
0
    def test_start_exit(self):
        signature = ParentChildByNameTopologySignature()
        alg = IncrementalDistanceAlgorithm(signature=signature,
                                           distance=StartExitDistance)
        alg.prototypes = [self._simple_prototype]

        alg.start_tree()
        alg.add_event(Event.start(tme=0, pid=2, ppid=1, name="root_node"))
        alg.add_event(Event.start(tme=1, pid=3, ppid=2, name="first_child"))
        alg.add_event(Event.start(tme=1, pid=4, ppid=2, name="second_child"))
        alg.add_event(
            Event.exit(tme=3, start_tme=1, pid=3, ppid=2, name="first_child"))
        alg.add_event(
            Event.exit(tme=4, start_tme=1, pid=4, ppid=2, name="second_child"))
        alg.add_event(Event.start(tme=5, pid=5, ppid=2, name="first_child"))
        alg.add_event(
            Event.exit(tme=7, start_tme=5, pid=5, ppid=2, name="first_child"))
        distance = alg.add_event(
            Event.exit(tme=10, start_tme=0, pid=2, ppid=1, name="root_node"))
        alg.finish_tree()
        self.assertEqual(distance[0][0], [0])
Exemplo n.º 28
0
    def test_simple(self):
        algorithm = IncrementalDistanceAlgorithm(
            signature=ParentChildByNameTopologySignature(),
            distance=StartDistance)
        algorithm.prototypes = [simple_prototype()]
        distance = algorithm.distance
        distance.init_distance(
            prototypes=algorithm.prototypes,
            signature_prototypes=algorithm.signature_prototypes)

        last_index = 0
        for index, dist in enumerate(
                distance.iter_on_prototypes(algorithm.prototypes)):
            self.assertEqual(dist, [5])
            last_index = index
        self.assertEqual(last_index, 0)
        self.assertTrue(distance.is_prototype_based_on_original())

        for node in simple_monitoring_tree().nodes():
            node_signature = algorithm.signature.get_signature(
                node, node.parent())
            matching_prototypes = algorithm.signature_prototypes.get(
                signature=node_signature)
            distance.update_distance(
                prototypes=algorithm.prototypes,
                signature_prototypes=algorithm.signature_prototypes,
                matches=[{
                    token: matching_prototypes[index]
                } for index, token in enumerate(node_signature)],
                value=float(node.exit_tme) - float(node.tme),
            )
        for result in distance._monitoring_results_dict:
            self.assertEqual(result[algorithm.prototypes[0]], 1)
        result = distance._monitoring_results_dict
        distance.finish_distance(
            prototypes=algorithm.prototypes,
            signature_prototypes=algorithm.signature_prototypes)
        self.assertEqual(result, distance._monitoring_results_dict)
Exemplo n.º 29
0
    def test_attributes_nodes_only(self):
        def distance(**kwargs):
            distance = StartExitDistance(weight=1, **kwargs)
            distance.supported[TrafficEvent] = True
            return distance
        algorithm = IncrementalDistanceAlgorithm(
            signature=ParentChildByNameTopologySignature(),
            distance=distance,
            cache_statistics=SetStatistics
        )
        decorator = DistanceMatrixDecorator(normalized=False)
        decorator.wrap_algorithm(algorithm)
        algorithm.prototypes = [real_tree("data/c01-007-102/2/1129-2-process.csv")]

        monitoring_tree = real_tree("data/c01-007-102/2/1129-2-process.csv")
        algorithm.start_tree()
        for event in monitoring_tree.event_iter(supported=algorithm.supported):
            try:
                algorithm.add_event(event)
            except EventNotSupportedException:
                pass
        algorithm.finish_tree()
        self.assertEqual([[[0]]], decorator.data())
 def test_simple_functionality(self):
     decorator = CompressionFactorDecorator()
     algorithm = IncrementalDistanceAlgorithm()
     decorator.wrap_algorithm(algorithm=algorithm)
     decorator.start_tree()
     self.assertEqual(
         decorator.descriptive_data(), {
             "compression": {
                 "prototypes": [],
                 "monitoring": [None],
                 "accumulated": []
             }
         })
     decorator.finish_tree()
     self.assertEqual(
         decorator.descriptive_data(), {
             "compression": {
                 "prototypes": [],
                 "monitoring": [[0]],
                 "accumulated": []
             }
         })
     algorithm.prototypes = [simple_prototype()]
     decorator.algorithm = algorithm
     decorator.start_tree()
     decorator.finish_tree()
     self.assertEqual(
         decorator.descriptive_data(), {
             "compression": {
                 "prototypes": [[.4]],
                 "monitoring": [[0]],
                 "accumulated": [.4]
             }
         })
     decorator.wrap_algorithm(algorithm)
     self.assertEqual(decorator.descriptive_data(), {"compression": None})