def templateClustering(self, file, radius, order, solver, initial, storage_flag, conn_weigh_flag, tolerance, connection, expected_cluster_length, ccore_flag): result_testing = False; # If phases crosses each other because of random part of the network then we should try again. for attempt in range(0, 4, 1): sample = read_sample(file); network = syncnet(sample, radius, connection, initial, conn_weigh_flag, ccore_flag); analyser = network.process(order, solver, storage_flag); clusters = analyser.allocate_clusters(tolerance); obtained_cluster_sizes = [len(cluster) for cluster in clusters]; if (len(obtained_cluster_sizes) != len(expected_cluster_length)): continue; obtained_cluster_sizes.sort(); expected_cluster_length.sort(); if (obtained_cluster_sizes != expected_cluster_length): continue; # Unit-test is passed result_testing = True; break; assert result_testing;
def testCoreInterfaceIntInputData(self): result = False; for _ in range(0, 20, 1): syncnet_instance = syncnet([ [1], [2], [3], [20], [21], [22] ], 3, conn_represent.MATRIX, initial_type.EQUIPARTITION, ccore = True); analyser = syncnet_instance.process(0.999, solve_type.FAST); result = (len(analyser.allocate_clusters(0.1)) == 2); if (result is True): break; assert True == result;
def __analyse_colors(self, image_source, collect_dynamic): """! @brief Performs color segmentation by the first layer. @param[in] image_source (string): Path to image file that should be processed. @param[in] collect_dynamic (bool): If 'True' then whole dynamic of the first layer of the network is collected. @return (syncnet_analyser) Analyser of color segmentation results of the first layer. """ network = syncnet(image_source, self.__color_radius, ccore = True); analyser = network.process(self.__order_color, solve_type.FAST, collect_dynamic); return analyser;
def templateConnectionApi(connection_storage_type, ccore_flag): sample = read_sample(SIMPLE_SAMPLES.SAMPLE_SIMPLE1); network = syncnet(sample, 15, conn_repr = connection_storage_type, ccore = ccore_flag); for i in range(len(network)): neighbors = network.get_neighbors(i); assert len(sample) == len(network); assert len(neighbors) == len(network) - 1; assert i not in neighbors; for j in range(len(network)): if (i != j): assert network.has_connection(i, j) == True; else: assert network.has_connection(i, j) == False;
def template_clustering(file, radius, order, show_dyn = False, show_conn = False, show_clusters = True, ena_conn_weight = False, ccore_flag = False): sample = read_sample(file); network = syncnet(sample, radius, enable_conn_weight = ena_conn_weight, ccore = ccore_flag); (ticks, (dyn_time, dyn_phase)) = timedcall(network.process, order, solve_type.FAST, show_dyn); print("Sample: ", file, "\t\tExecution time: ", ticks, "\n"); if (show_dyn == True): draw_dynamics(dyn_time, dyn_phase, x_title = "Time", y_title = "Phase", y_lim = [0, 2 * 3.14]); if (show_conn == True): network.show_network(); if (show_clusters == True): clusters = network.get_clusters(0.1); draw_clusters(sample, clusters);
def template_clustering(file, radius, order, show_dyn = False, show_conn = False, show_clusters = True, ena_conn_weight = False, ccore_flag = True, tolerance = 0.1): sample = read_sample(file); network = syncnet(sample, radius, enable_conn_weight = ena_conn_weight, ccore = ccore_flag); (ticks, analyser) = timedcall(network.process, order, solve_type.FAST, show_dyn); print("Sample: ", file, "\t\tExecution time: ", ticks, "\n"); if (show_dyn == True): sync_visualizer.show_output_dynamic(analyser); sync_visualizer.animate_output_dynamic(analyser); if ( (show_conn == True) and (ccore_flag == False) ): network.show_network(); if (show_clusters == True): clusters = analyser.allocate_clusters(tolerance); draw_clusters(sample, clusters);
def __create_sync_layer(self, weights): """! @brief Creates second layer of the network. @param[in] weights (list): List of weights of SOM neurons. @return (syncnet) Second layer of the network. """ sync_layer = syncnet(weights, 0.0, initial_phases = initial_type.RANDOM_GAUSSIAN, ccore = False); for oscillator_index1 in range(0, len(sync_layer)): for oscillator_index2 in range(oscillator_index1 + 1, len(sync_layer)): if (self.__has_object_connection(oscillator_index1, oscillator_index2)): sync_layer.set_connection(oscillator_index1, oscillator_index2); return sync_layer;
def process(self, number_neighbours, collect_dynamic=False, order=0.999): """! @brief Performs simulation of the oscillatory network. @param[in] number_neighbours (uint): Number of neighbours that should be used for calculation average distance and creation connections between oscillators. @param[in] collect_dynamic (bool): If True - returns whole dynamic of oscillatory network, otherwise returns only last values of dynamics. @param[in] order (double): Order of process synchronization that should be considered as end of clustering, destributed 0..1. @return (tuple) Dynamic of oscillatory network. If argument 'collect_dynamic' = True, than return dynamic for the whole simulation time, otherwise returns only last values (last step of simulation) of dynamic. @see get_som_clusters() @see get_clusters() """ # train self-organization map. self._som.train(self._data, 100) # prepare to build list. weights = list() self._som_osc_table.clear() # must be cleared, if it's used before. for i in range(self._som.size): if self._som.awards[i] > 0: weights.append(self._som.weights[i]) self._som_osc_table.append(i) # calculate trusted distance between objects. radius = 0 if len(weights) >= number_neighbours: radius = average_neighbor_distance(weights, number_neighbours) else: radius = 0 # create oscillatory neural network. self._sync = syncnet(weights, radius, initial_phases=initial_type.EQUIPARTITION) self._analyser = self._sync.process(order, collect_dynamic=collect_dynamic) # Draw SOM clusters. # clusters = self._sync.get_clusters(); # draw_clusters(weights, clusters); # self._som.show_network(awards = False, belongs = True); # return dynamic if it was requested. return (self._analyser.time, self._analyser.output)
def __analyse_color_segment(self, image_size, color_segment, collect_dynamic): """! @brief Performs object segmentation of separate segment. @param[in] image_size (list): Image size presented as a [width x height]. @param[in] color_segment (list): Image segment that should be processed. @param[in] collect_dynamic (bool): If 'True' then whole dynamic of the second layer of the network is collected. @return (syncnet_analyser) Analyser of object segmentation results of the second layer. """ coordinates = self.__extract_location_coordinates(image_size, color_segment); if (len(coordinates) < self.__noise_size): return None; network = syncnet(coordinates, self.__object_radius, ccore = True); analyser = network.process(self.__order_object, solve_type.FAST, collect_dynamic); return analyser;
def template_animated_clustering(file, radius, order, expected_cluster_amount, title_animation = None): sample = read_sample(file); expected_result_obtained = False; analyser = None; while (expected_result_obtained == False): network = syncnet(sample, radius, initial_phases = initial_type.RANDOM_GAUSSIAN, ccore = True); analyser = network.process(order, solve_type.FAST, True); clusters = analyser.allocate_clusters(0.1); if (len(clusters) == expected_cluster_amount): print("Expected result is obtained - start rendering...") expected_result_obtained = True; visualizer = cluster_visualizer(); visualizer.append_clusters(clusters, sample); visualizer.show(); else: print("Expected result is NOT obtained - rendering is NOT started ( actual:", len(clusters), ")..."); syncnet_visualizer.animate_cluster_allocation(sample, analyser, tolerance = 0.1, save_movie = "clustering_animation.mp4", title = title_animation);
def template_clustering(file, radius, order, show_dyn = False, show_conn = False, show_clusters = True, ena_conn_weight = False, ccore_flag = True, tolerance = 0.1): sample = read_sample(file) syncnet_instance = syncnet(sample, radius, enable_conn_weight = ena_conn_weight, ccore = ccore_flag) (ticks, analyser) = timedcall(syncnet_instance.process, order, solve_type.FAST, show_dyn) print("Sample: ", file, "\t\tExecution time: ", ticks) if show_dyn == True: sync_visualizer.show_output_dynamic(analyser) sync_visualizer.animate(analyser) sync_visualizer.show_local_order_parameter(analyser, syncnet_instance) #sync_visualizer.animate_output_dynamic(analyser); #sync_visualizer.animate_correlation_matrix(analyser, colormap = 'hsv') if show_conn == True: syncnet_instance.show_network() if show_clusters == True: clusters = analyser.allocate_clusters(tolerance) print("Amount of allocated clusters: ", len(clusters)) draw_clusters(sample, clusters) print("----------------------------\n") return (sample, clusters)
def template_segmentation_image(source, color_radius, object_radius, noise_size, show_dyn): data = read_image(source) print("Pixel dimension: ", len(data[0])) network = syncnet(data, color_radius, ccore=True) print("Network has been created") (ticks, (t, dyn)) = timedcall(network.process, 0.9995, solve_type.FAST, show_dyn) # (t, dyn) = network.process(0.998, solve_type.FAST, show_dyn); print("Sample: ", source, "\t\tExecution time: ", ticks, "\n") if (show_dyn is True): draw_dynamics(t, dyn) clusters = network.get_clusters() real_clusters = [ cluster for cluster in clusters if len(cluster) > noise_size ] draw_image_mask_segments(source, real_clusters) if (object_radius is None): return # continue analysis pointer_image = Image.open(source) image_size = pointer_image.size object_colored_clusters = [] object_colored_dynamics = [] total_dyn = [] for cluster in clusters: coordinates = [] for index in cluster: y = floor(index / image_size[0]) x = index - y * image_size[0] coordinates.append([x, y]) print(coordinates) # perform clustering analysis of the colored objects if (network is not None): del network network = None if (len(coordinates) < noise_size): continue network = syncnet(coordinates, object_radius, ccore=True) (t, dyn) = network.process(0.999, solve_type.FAST, show_dyn) if (show_dyn is True): object_colored_dynamics.append((t, dyn)) object_clusters = network.get_clusters() # decode it real_description_clusters = [] for object_cluster in object_clusters: real_description = [] for index_object in object_cluster: real_description.append(cluster[index_object]) real_description_clusters.append(real_description) if (len(real_description) > noise_size): object_colored_clusters.append(real_description) # draw_image_mask_segments(source, [ cluster ]); # draw_image_mask_segments(source, real_description_clusters); draw_image_mask_segments(source, object_colored_clusters) if (show_dyn is True): draw_dynamics_set(object_colored_dynamics, None, None, None, [0, 2 * 3.14], False, False)
def templateShowNetwork(file, radius, ccore_flag, connection_storage_type = conn_represent.MATRIX): sample = read_sample(file); network = syncnet(sample, radius, conn_repr = connection_storage_type, ccore = ccore_flag); network.show_network();
def template_segmentation_image(source, color_radius, object_radius, noise_size, show_dyn): data = read_image(source); print("Pixel dimension: ", len(data[0])); network = syncnet(data, color_radius, ccore = True); print("Network has been created"); (ticks, (t, dyn)) = timedcall(network.process, 0.9995, solve_type.FAST, show_dyn); # (t, dyn) = network.process(0.998, solve_type.FAST, show_dyn); print("Sample: ", source, "\t\tExecution time: ", ticks, "\n"); if (show_dyn is True): draw_dynamics(t, dyn); clusters = network.get_clusters(); real_clusters = [cluster for cluster in clusters if len(cluster) > noise_size]; draw_image_mask_segments(source, real_clusters); if (object_radius is None): return; # continue analysis pointer_image = Image.open(source); image_size = pointer_image.size; object_colored_clusters = []; object_colored_dynamics = []; total_dyn = []; for cluster in clusters: coordinates = []; for index in cluster: y = floor(index / image_size[0]); x = index - y * image_size[0]; coordinates.append([x, y]); print(coordinates); # perform clustering analysis of the colored objects if (network is not None): del network; network = None; if (len(coordinates) < noise_size): continue; network = syncnet(coordinates, object_radius, ccore = True); (t, dyn) = network.process(0.999, solve_type.FAST, show_dyn); if (show_dyn is True): object_colored_dynamics.append( (t, dyn) ); object_clusters = network.get_clusters(); # decode it real_description_clusters = []; for object_cluster in object_clusters: real_description = []; for index_object in object_cluster: real_description.append(cluster[index_object]); real_description_clusters.append(real_description); if (len(real_description) > noise_size): object_colored_clusters.append(real_description); # draw_image_mask_segments(source, [ cluster ]); # draw_image_mask_segments(source, real_description_clusters); draw_image_mask_segments(source, object_colored_clusters); if (show_dyn is True): draw_dynamics_set(object_colored_dynamics, None, None, None, [0, 2 * 3.14], False, False);
def testVisualizeClusteringAnimationNoError(self): sample = read_sample(SIMPLE_SAMPLES.SAMPLE_SIMPLE1) network = syncnet(sample, 1.0) analyser = network.process(0.998, solve_type.FAST, True) syncnet_visualizer.animate_cluster_allocation(sample, analyser)
def process_syncnet(sample): instance = syncnet(sample, 3.0, initial_phases = initial_type.EQUIPARTITION) (ticks, _) = timedcall(instance.process) return ticks
def testVisualizerNoFailure(self): sample = read_sample(SIMPLE_SAMPLES.SAMPLE_SIMPLE1); network = syncnet(sample, 1.0, ccore = False); analyser = network.simulate(25, 5, solve_type.FAST, True); syncnet_visualizer.animate_cluster_allocation(sample, analyser);
def templateShowNetwork(self, file, radius, ccore_flag): sample = read_sample(file) network = syncnet(sample, radius, ccore=ccore_flag) network.show_network()
def template_segmentation_image(source, color_radius, object_radius, noise_size, show_dyn): data = read_image(source); print("Pixel dimension: ", len(data[0])); network = syncnet(data, color_radius, ccore = True); print("Network has been created"); (ticks, analyser) = timedcall(network.process, 0.9995, solve_type.FAST, show_dyn); print("Sample: ", source, "\t\tExecution time: ", ticks, "\n"); if (show_dyn is True): sync_visualizer.show_output_dynamic(analyser); clusters = analyser.allocate_clusters(); real_clusters = [cluster for cluster in clusters if len(cluster) > noise_size]; draw_image_mask_segments(source, real_clusters); if (object_radius is None): return; # continue analysis pointer_image = Image.open(source); image_size = pointer_image.size; object_colored_clusters = []; object_colored_dynamics = []; total_dyn = []; for cluster in clusters: coordinates = []; for index in cluster: y = floor(index / image_size[0]); x = index - y * image_size[0]; coordinates.append([x, y]); print(coordinates); # perform clustering analysis of the colored objects if (network is not None): del network; network = None; if (len(coordinates) < noise_size): continue; network = syncnet(coordinates, object_radius, ccore = True); analyser = network.process(0.999, solve_type.FAST, show_dyn); if (show_dyn is True): object_colored_dynamics.append( (analyser.time, analyser.output) ); object_clusters = analyser.allocate_clusters(); # decode it real_description_clusters = []; for object_cluster in object_clusters: real_description = []; for index_object in object_cluster: real_description.append(cluster[index_object]); real_description_clusters.append(real_description); if (len(real_description) > noise_size): object_colored_clusters.append(real_description); # draw_image_mask_segments(source, [ cluster ]); # draw_image_mask_segments(source, real_description_clusters); draw_image_mask_segments(source, object_colored_clusters); if (show_dyn is True): draw_dynamics_set(object_colored_dynamics, None, None, None, [0, 2 * 3.14], False, False);
def process_syncnet(sample): instance = syncnet(sample, 3.0, initial_phases=initial_type.EQUIPARTITION) (ticks, _) = timedcall(instance.process) return ticks