def template_clustering(data_path, levels, **kwargs): print("Sample: '%s'." % os.path.basename(data_path)) density_threshold = kwargs.get("density_threshold", 0.0) amount_threshold = kwargs.get("amount_threshold", 0) data = read_sample(data_path) bang_instance = bang(data, levels, density_threshold=density_threshold, amount_threshold=amount_threshold) bang_instance.process() clusters = bang_instance.get_clusters() noise = bang_instance.get_noise() directory = bang_instance.get_directory() dendrogram = bang_instance.get_dendrogram() bang_visualizer.show_blocks(directory) bang_visualizer.show_dendrogram(dendrogram) bang_visualizer.show_clusters(data, clusters, noise) if len(data[0]) == 2: animator = bang_animator(directory, clusters) animator.animate()
def template_segmentation(source, levels, threshold): data = read_image(source) bang_instance = bang(data, levels, threshold) bang_instance.process() clusters = bang_instance.get_clusters() draw_image_mask_segments(source, clusters)
def template_segmentation(source, levels, threshold): data = read_image(source) bang_instance = bang(data, levels, threshold) bang_instance.process() clusters = bang_instance.get_clusters() draw_image_mask_segments(source, clusters)
def visualize(path, levels, threshold, ccore, **kwargs): sample = read_sample(path) bang_instance = bang(sample, levels, threshold, ccore) bang_instance.process() directory = bang_instance.get_directory() dendrogram = bang_instance.get_dendrogram() bang_visualizer.show_blocks(directory) bang_visualizer.show_dendrogram(dendrogram)
def animate(path, levels, threshold, ccore, **kwargs): sample = read_sample(path) bang_instance = bang(sample, levels, threshold, ccore) bang_instance.process() directory = bang_instance.get_directory() clusters = bang_instance.get_clusters() noise = bang_instance.get_noise() animator = bang_animator(directory, clusters, noise) animator.animate()
def getCluster(arrayNormalizedCharacters,scaler,clusterName): if(clusterName == 'K-means'): return kMeans(arrayNormalizedCharacters,scaler,3) if(clusterName == 'WARD'): return hierarchicalWard(arrayNormalizedCharacters,scaler, 3) if(clusterName == 'Spectral'): return spectral(arrayNormalizedCharacters,scaler, 3) if(clusterName == 'DBSCAN'): return dbscan(arrayNormalizedCharacters,scaler) if(clusterName == 'BANG'): return bang(arrayNormalizedCharacters,scaler) if(clusterName == 'SOM'): return som(arrayNormalizedCharacters,scaler,3) if(clusterName == 'Fuzzy C-Means'): return cMeans(arrayNormalizedCharacters,scaler,3)
def exception(type, sample_storage, levels, threshold, ccore): try: sample = sample_storage if isinstance(sample_storage, str): sample = read_sample(sample_storage) bang_instance = bang(sample, levels, threshold, ccore) bang_instance.process() except type: return except Exception as ex: raise AssertionError("Expected: '%s', Actual: '%s'" % (type, type(ex).__name__)) raise AssertionError("Expected: '%s', Actual: 'None'" % type)
def clustering(path, levels, density_threshold, expected_clusters, expected_noise, ccore, **kwargs): sample = read_sample(path) amount_threshold = kwargs.get('amount_threshold', 0) bang_instance = bang(sample, levels, ccore, density_threshold=density_threshold, amount_threshold=amount_threshold) bang_instance.process() clusters = bang_instance.get_clusters() noise = bang_instance.get_noise() directory = bang_instance.get_directory() dendrogram = bang_instance.get_dendrogram() assertion.eq(len(clusters), len(dendrogram)) obtained_length = len(noise) obtained_cluster_length = [] for cluster in clusters: obtained_length += len(cluster) obtained_cluster_length.append(len(cluster)) obtained_cluster_length.sort() assertion.eq(len(sample), obtained_length) assertion.eq(expected_noise, len(noise)) if expected_clusters is not None: assertion.eq(len(expected_clusters), len(clusters)) assertion.eq(expected_clusters, obtained_cluster_length) leafs = directory.get_leafs() covered_points = set() for leaf in leafs: points = leaf.get_points() for index_point in points: covered_points.add(index_point) assertion.eq(len(sample), len(covered_points)) return bang_instance
def setup(self, keywords={}): """ Setup the algorithms """ for p in keywords.keys(): setattr(self, p, keywords[p]) if self.method == "bang": self.obj = bang(self.data_list, self.levels, ccore=self.ccore, density_threshold=self.density_threshold, amount_threshold=self.amount_threshold) if self.method == "clique": self.obj = clique(self.data_list, self.amount_threshold, self.density_threshold, ccore=self.ccore) return
def template_clustering(data_path, levels, **kwargs): print("Sample: '%s'." % os.path.basename(data_path)) density_threshold = kwargs.get("density_threshold", 0.0) amount_threshold = kwargs.get("amount_threshold", 0) data = read_sample(data_path) bang_instance = bang(data, levels, density_threshold=density_threshold, amount_threshold=amount_threshold) bang_instance.process() clusters = bang_instance.get_clusters() noise = bang_instance.get_noise() directory = bang_instance.get_directory() dendrogram = bang_instance.get_dendrogram() bang_visualizer.show_blocks(directory) bang_visualizer.show_dendrogram(dendrogram) bang_visualizer.show_clusters(data, clusters, noise) if len(data[0]) == 2: animator = bang_animator(directory, clusters) animator.animate()
def bang(arrayNormalizedCharacters,scaler): from pyclustering.cluster.bang import bang, bang_visualizer # Read data three dimensional data. data = arrayNormalizedCharacters # Prepare algorithm's parameters. levels = 11 # Create instance of BANG algorithm. bang_instance = bang(data, levels) bang_instance.process() # Obtain clustering results. clusters = bang_instance.get_clusters() #dendrogram = bang_instance.get_dendrogram() # Visualize BANG clustering results. #bang_visualizer.show_dendrogram(dendrogram) clustersResult = [] k = len(clusters) labels = [] for i in range(0,k): clustersResult.append([]) i=0 for player in arrayNormalizedCharacters: j=0 for cluster in clusters: if(i in cluster): clustersResult[j].append(arrayNormalizedCharacters[i]) labels.append(j) j= j+1 i=i+1 for i in range(0,k): clustersResult[i] = scaler.inverse_transform(clustersResult[i]) return (clustersResult,labels)