Exemplo n.º 1
0
def template_clustering(path_sample,
                        eps,
                        minpts,
                        amount_clusters=None,
                        visualize=True,
                        ccore=False):
    sample = read_sample(path_sample)

    optics_instance = optics(sample, eps, minpts, amount_clusters, ccore)
    (ticks, _) = timedcall(optics_instance.process)

    print("Sample: ", path_sample, "\t\tExecution time: ", ticks, "\n")

    if (visualize is True):
        clusters = optics_instance.get_clusters()
        noise = optics_instance.get_noise()

        visualizer = cluster_visualizer()
        visualizer.append_clusters(clusters, sample)
        visualizer.append_cluster(noise, sample, marker='x')
        visualizer.show()

        ordering = optics_instance.get_ordering()
        analyser = ordering_analyser(ordering)

        ordering_visualizer.show_ordering_diagram(analyser, amount_clusters)
    def templateClusteringResults(path, radius, neighbors, amount_clusters,
                                  expected_length_clusters, ccore):
        sample = read_sample(path)

        optics_instance = optics(sample, radius, neighbors, amount_clusters,
                                 ccore)
        optics_instance.process()

        clusters = optics_instance.get_clusters()
        noise = optics_instance.get_noise()

        assert sum([len(cluster)
                    for cluster in clusters]) + len(noise) == len(sample)
        assert len(clusters) == len(expected_length_clusters)
        assert sum([len(cluster)
                    for cluster in clusters]) == sum(expected_length_clusters)
        assert sorted([len(cluster) for cluster in clusters
                       ]) == sorted(expected_length_clusters)

        if (amount_clusters is not None):
            analyser = ordering_analyser(optics_instance.get_ordering())
            assert len(analyser) > 0

            amount_clusters, borders = analyser.extract_cluster_amount(
                optics_instance.get_radius())
            assert amount_clusters == len(expected_length_clusters)
            assert len(borders) == amount_clusters - 1
Exemplo n.º 3
0
    def templateClusteringResultsSpecificData(data_type, path, radius, neighbors, amount_clusters, expected_length_clusters, ccore):
        sample = read_sample(path);

        if data_type == 'distance_matrix':
            input_data = calculate_distance_matrix(sample);
        else:
            input_data = sample;

        optics_instance = optics(input_data, radius, neighbors, amount_clusters, ccore, data_type=data_type);
        optics_instance.process();

        clusters = optics_instance.get_clusters();
        noise = optics_instance.get_noise();

        assert sum([len(cluster) for cluster in clusters]) + len(noise) == len(sample);
        assert len(clusters) == len(expected_length_clusters);
        assert sum([len(cluster) for cluster in clusters]) == sum(expected_length_clusters);
        assert sorted([len(cluster) for cluster in clusters]) == sorted(expected_length_clusters);

        if (amount_clusters is not None):
            analyser = ordering_analyser(optics_instance.get_ordering());
            assert len(analyser) > 0;

            amount_clusters, borders = analyser.extract_cluster_amount(optics_instance.get_radius());
            assert amount_clusters == len(expected_length_clusters);
            assert len(borders) == amount_clusters - 1;
Exemplo n.º 4
0
 def testClusteringOrderVisualizer(self):
     sample = read_sample(SIMPLE_SAMPLES.SAMPLE_SIMPLE4);
     
     optics_instance = optics(sample, 6.0, 3, 5);
     optics_instance.process();
     
     analyser = ordering_analyser(optics_instance.get_ordering());
     ordering_visualizer.show_ordering_diagram(analyser);
Exemplo n.º 5
0
 def testClusteringOrderVisualizer(self):
     sample = read_sample(SIMPLE_SAMPLES.SAMPLE_SIMPLE4);
        
     optics_instance = optics(sample, 6.0, 3, 5);
     optics_instance.process();
        
     analyser = ordering_analyser(optics_instance.get_ordering());
     ordering_visualizer.show_ordering_diagram(analyser, 5);
Exemplo n.º 6
0
    def testClusterOrderingOneClusterExtraction(self):
        analyser = ordering_analyser([5.0, 5.0, 5.0, 5.0, 5.0, 5.0])

        amount_clusters, borders = analyser.extract_cluster_amount(6.5)
        assert 1 == amount_clusters
        assert 0 == len(borders)

        amount_clusters, borders = analyser.extract_cluster_amount(4.5)
        assert 0 == amount_clusters
        assert 0 == len(borders)
Exemplo n.º 7
0
 def testClusterOrderingOneClusterExtraction(self):
     analyser = ordering_analyser([5.0, 5.0, 5.0, 5.0, 5.0, 5.0]);
     
     amount_clusters, borders = analyser.extract_cluster_amount(6.5);
     assert 1 == amount_clusters;
     assert 0 == len(borders);
     
     amount_clusters, borders = analyser.extract_cluster_amount(4.5);
     assert 0 == amount_clusters;
     assert 0 == len(borders);
Exemplo n.º 8
0
    def templateClusteringResultsSpecificData(data_type, path, radius,
                                              neighbors, amount_clusters,
                                              expected_length_clusters, ccore):
        sample = read_sample(path)

        if data_type == 'distance_matrix':
            input_data = calculate_distance_matrix(sample)
        else:
            input_data = sample

        optics_instance = optics(input_data,
                                 radius,
                                 neighbors,
                                 amount_clusters,
                                 ccore,
                                 data_type=data_type)
        optics_instance.process()

        clusters = optics_instance.get_clusters()
        noise = optics_instance.get_noise()
        optics_objects = optics_instance.get_optics_objects()

        object_indexes = set([obj.index_object for obj in optics_objects])
        assertion.eq(len(optics_objects), len(object_indexes))
        for obj in optics_objects:
            if obj.core_distance is not None:
                assertion.ge(obj.core_distance, 0)

            if obj.reachability_distance is not None:
                assertion.ge(obj.reachability_distance, 0)

        assert sum([len(cluster)
                    for cluster in clusters]) + len(noise) == len(sample)
        assert len(clusters) == len(expected_length_clusters)
        assert sum([len(cluster)
                    for cluster in clusters]) == sum(expected_length_clusters)
        assert sorted([len(cluster) for cluster in clusters
                       ]) == sorted(expected_length_clusters)

        if amount_clusters is not None:
            analyser = ordering_analyser(optics_instance.get_ordering())
            assert len(analyser) > 0

            amount_clusters, borders = analyser.extract_cluster_amount(
                optics_instance.get_radius())
            assert amount_clusters == len(expected_length_clusters)
            assert len(borders) == amount_clusters - 1
Exemplo n.º 9
0
def analyze(sample, radius, neighbors):
    # Run cluster analysis where connectivity radius is bigger than real

    # Create OPTICS algorithm for cluster analysis
    optics_instance = optics(sample, radius, neighbors)

    # Run cluster analysis
    optics_instance.process()

    # Obtain results of clustering
    clusters = optics_instance.get_clusters()
    noise = optics_instance.get_noise()

    # Obtain reachability-distances
    ordering = ordering_analyser(optics_instance.get_ordering())

    return ordering, clusters, noise
Exemplo n.º 10
0
def analyze(sample, radius, neighbors):
  # Run cluster analysis where connectivity radius is bigger than real

  # Create OPTICS algorithm for cluster analysis
  optics_instance = optics(sample, radius, neighbors)

  # Run cluster analysis
  optics_instance.process()

  # Obtain results of clustering
  clusters = optics_instance.get_clusters()
  noise = optics_instance.get_noise()

  # Obtain reachability-distances
  ordering = ordering_analyser(optics_instance.get_ordering())

  return ordering, clusters, noise
    def optics_temp(self, point_list):
        # data = np.array( point_list)
        sample = point_list
        start = time.time()
        optics_instance = optics(sample, 0.5, 6, ccore=True)
        optics_instance.process()
        clusters = optics_instance.get_clusters()
        end = time.time()
        print("imte", end - start)

        noise = optics_instance.get_noise()
        visualizer = cluster_visualizer()
        visualizer.append_clusters(clusters, sample)
        visualizer.append_cluster(noise, sample, marker='x')
        visualizer.show()
        ordering = optics_instance.get_ordering()
        analyser = ordering_analyser(ordering)
        ordering_visualizer.show_ordering_diagram(analyser, amount_clusters)
Exemplo n.º 12
0
    def templateClusteringResultsSpecificData(data_type, path, radius, neighbors, amount_clusters, expected_length_clusters, ccore):
        sample = read_sample(path)

        if data_type == 'distance_matrix':
            input_data = calculate_distance_matrix(sample)
        else:
            input_data = sample

        optics_instance = optics(input_data, radius, neighbors, amount_clusters, ccore, data_type=data_type)
        optics_instance.process()

        clusters = optics_instance.get_clusters()
        noise = optics_instance.get_noise()
        optics_objects = optics_instance.get_optics_objects()

        object_indexes = set( [ obj.index_object for obj in optics_objects ] )
        assertion.eq(len(optics_objects), len(object_indexes))
        for obj in optics_objects:
            if obj.core_distance is not None:
                assertion.ge(obj.core_distance, 0)

            if obj.reachability_distance is not None:
                assertion.ge(obj.reachability_distance, 0)

        assert sum([len(cluster) for cluster in clusters]) + len(noise) == len(sample)
        assert len(clusters) == len(expected_length_clusters)
        assert sum([len(cluster) for cluster in clusters]) == sum(expected_length_clusters)
        assert sorted([len(cluster) for cluster in clusters]) == sorted(expected_length_clusters)

        if amount_clusters is not None:
            analyser = ordering_analyser(optics_instance.get_ordering())
            assert len(analyser) > 0

            amount_clusters, borders = analyser.extract_cluster_amount(optics_instance.get_radius())
            assert amount_clusters == len(expected_length_clusters)
            assert len(borders) == amount_clusters - 1
Exemplo n.º 13
0
 def testImpossibleClusterOrderingAllocationGeterogeneous(self):
     analyser = ordering_analyser(
         [5.0, 5.0, 5.0, 5.0, 6.0, 8.0, 6.0, 5.0, 5.0, 5.0])
     amount_clusters, borders = analyser.calculate_connvectivity_radius(3)
     assert None == amount_clusters
     assert 0 == len(borders)
Exemplo n.º 14
0
 def testImpossibleClusterOrderingAllocationGeterogeneous(self):
     analyser = ordering_analyser([5.0, 5.0, 5.0, 5.0, 6.0, 8.0, 6.0, 5.0, 5.0, 5.0]);
     amount_clusters, borders = analyser.calculate_connvectivity_radius(3);
     assert None == amount_clusters;
     assert 0 == len(borders);
Exemplo n.º 15
0
 def testImpossibleClusterOrderingAllocationHomogeneous(self):
     analyser = ordering_analyser(
         [5.0, 5.0, 5.0, 5.0, 5.0, 5.0, 5.0, 5.0, 5.0, 5.0])
     assert None == analyser.calculate_connvectivity_radius(2)