Exemplo n.º 1
0
    def __process_by_ccore(self):
        """!
        @brief Performs cluster analysis using CCORE (C/C++ part of pyclustering library).

        """
        cure_data_pointer = wrapper.cure_algorithm(
            self.__pointer_data, self.__number_cluster,
            self.__number_represent_points, self.__compression)

        self.__clusters = wrapper.cure_get_clusters(cure_data_pointer)
        self.__representors = wrapper.cure_get_representors(cure_data_pointer)
        self.__means = wrapper.cure_get_means(cure_data_pointer)

        wrapper.cure_data_destroy(cure_data_pointer)
Exemplo n.º 2
0
    def process(self):
        """!
        @brief Performs cluster analysis in line with rules of CURE algorithm.
        
        @remark Results of clustering can be obtained using corresponding get methods.
        
        @see get_clusters()
        
        """

        if (self.__ccore is True):
            cure_data_pointer = wrapper.cure_algorithm(
                self.__pointer_data, self.__number_cluster,
                self.__number_represent_points, self.__compression)

            self.__clusters = wrapper.cure_get_clusters(cure_data_pointer)
            self.__representors = wrapper.cure_get_representors(
                cure_data_pointer)
            self.__means = wrapper.cure_get_means(cure_data_pointer)

            wrapper.cure_data_destroy(cure_data_pointer)

        else:
            while (len(self.__queue) > self.__number_cluster):
                cluster1 = self.__queue[0]
                # cluster that has nearest neighbor.
                cluster2 = cluster1.closest
                # closest cluster.

                #print("Merge decision: \n\t", cluster1, "\n\t", cluster2);

                self.__queue.remove(cluster1)
                self.__queue.remove(cluster2)

                self.__delete_represented_points(cluster1)
                self.__delete_represented_points(cluster2)

                merged_cluster = self.__merge_clusters(cluster1, cluster2)

                self.__insert_represented_points(merged_cluster)

                # Pointers to clusters that should be relocated is stored here.
                cluster_relocation_requests = []

                # Check for the last cluster
                if (len(self.__queue) > 0):
                    merged_cluster.closest = self.__queue[0]
                    # arbitrary cluster from queue
                    merged_cluster.distance = self.__cluster_distance(
                        merged_cluster, merged_cluster.closest)

                    for item in self.__queue:
                        distance = self.__cluster_distance(
                            merged_cluster, item)
                        # Check if distance between new cluster and current is the best than now.
                        if (distance < merged_cluster.distance):
                            merged_cluster.closest = item
                            merged_cluster.distance = distance

                        # Check if current cluster has removed neighbor.
                        if ((item.closest is cluster1)
                                or (item.closest is cluster2)):
                            # If previous distance was less then distance to new cluster then nearest cluster should be found in the tree.
                            #print("Update: ", item);
                            if (item.distance < distance):
                                (item.closest,
                                 item.distance) = self.__closest_cluster(
                                     item, distance)

                                # TODO: investigation of root cause is required.
                                # Itself and merged cluster should be always in list of neighbors in line with specified radius.
                                # But merged cluster may not be in list due to error calculation, therefore it should be added
                                # manually.
                                if (item.closest is None):
                                    item.closest = merged_cluster
                                    item.distance = distance

                            # Otherwise new cluster is nearest.
                            else:
                                item.closest = merged_cluster
                                item.distance = distance

                            cluster_relocation_requests.append(item)
                        elif (item.distance > distance):
                            item.closest = merged_cluster
                            item.distance = distance

                            cluster_relocation_requests.append(item)

                # New cluster and updated clusters should relocated in queue
                self.__insert_cluster(merged_cluster)
                for item in cluster_relocation_requests:
                    self.__relocate_cluster(item)

            # Change cluster representation
            self.__clusters = [
                cure_cluster_unit.indexes for cure_cluster_unit in self.__queue
            ]
            self.__representors = [
                cure_cluster_unit.rep for cure_cluster_unit in self.__queue
            ]
            self.__means = [
                cure_cluster_unit.mean for cure_cluster_unit in self.__queue
            ]