コード例 #1
0
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)
コード例 #2
0
def template_graph_coloring(positive_weight, negative_weight, filename, reduction = None, title = None):
    if (title is None): title = filename;
    print("\nGraph Coloring: ", title);
    
    graph = read_graph(filename);
    network = syncgcolor(graph.data, positive_weight, negative_weight, reduction);
    
    (t, dyn) = network.process(order = 0.999, solution = solve_type.FAST, collect_dynamic = True);
    draw_dynamics(t, dyn, x_title = "Time", y_title = "Phase", y_lim = [0, 2 * 3.14]);

    clusters = network.get_clusters();
    
    for index in range(0, len(clusters)):
        print("Color #", index, ": ", clusters[index]);
        
    coloring_map = network.get_map_coloring();
    print("Number colors: ", max(coloring_map));
    
    draw_graph(graph, coloring_map);
    
    # Check validity of colors
    for index_node in range(len(graph.data)):
        color_neighbors = [ coloring_map[index] for index in range(len(graph.data[index_node])) if graph.data[index_node][index] != 0 and index_node != index];
        #print(index_node, map_coloring[index_node], color_neighbors, assigned_colors, map_coloring, "\n\n");
        
        if (coloring_map[index_node] in color_neighbors):
            print("Warining: Incorrect coloring");
            return;
コード例 #3
0
def template_segmentation_image(image_file, parameters, steps, time, ccore_flag = True):
    image = read_image(image_file);
    stimulus = rgb2gray(image);
    
    for pixel_index in range(len(stimulus)):
        if (stimulus[pixel_index] < 235): stimulus[pixel_index] = 1;
        else: stimulus[pixel_index] = 0;
    
    if (parameters is None):
        parameters = legion_parameters();
    
    net = legion_network(len(stimulus), parameters, conn_type.GRID_FOUR, ccore = ccore_flag);
    output_dynamic = net.simulate(steps, time, stimulus);
    
    ensembles = output_dynamic.allocate_sync_ensembles();
    
    draw_image_mask_segments(image_file, ensembles);
    # draw_dynamics(output_dynamic.time, output_dynamic.output, x_title = "Time", y_title = "x(t)", separate = ensembles);
    
    # just for checking correctness of results - let's use classical algorithm
    dbscan_instance = dbscan(image, 3, 4, True);
    dbscan_instance.process();
    trustable_clusters = dbscan_instance.get_clusters();
    
    draw_dynamics(output_dynamic.time, output_dynamic.output, x_title = "Time", y_title = "x(t)", separate = trustable_clusters);
コード例 #4
0
def template_dynamic_sync(num_osc,
                          k=1,
                          q=1,
                          sim_arg=None,
                          conn=conn_type.ALL_TO_ALL,
                          type_solution=solve_type.FAST,
                          collect_dyn=True,
                          ccore_flag=False):
    network = sync_network(num_osc, k, type_conn=conn, ccore=ccore_flag)
    network.cluster = q

    if (sim_arg is not None):
        (t, dyn_phase) = network.simulate(sim_arg[0],
                                          sim_arg[1],
                                          solution=type_solution,
                                          collect_dynamic=collect_dyn)
    else:
        (t, dyn_phase) = network.simulate_dynamic(collect_dynamic=collect_dyn,
                                                  solution=type_solution)

    draw_dynamics(t,
                  dyn_phase,
                  x_title="Time",
                  y_title="Phase",
                  y_lim=[0, 2 * 3.14])
    return network
コード例 #5
0
ファイル: pcnn.py プロジェクト: weihuang0908/pyclustering
 def show_output_dynamic(pcnn_output_dynamic, separate_representation = False):
     """!
     @brief Shows output dynamic (output of each oscillator) during simulation.
     
     """
     
     draw_dynamics(pcnn_output_dynamic.time, pcnn_output_dynamic.output, x_title = "t", y_title = "y(t)", separate = separate_representation);
コード例 #6
0
ファイル: sync.py プロジェクト: alishakiba/pyclustering
 def show_output_dynamic(sync_output_dynamic):
     """!
     @brief Shows output dynamic (output of each oscillator) during simulation.
     
     @param[in] pcnn_output_dynamic (sync_dynamic): Output dynamic of the Sync network.
     
     """
     
     draw_dynamics(sync_output_dynamic.time, sync_output_dynamic.output, x_title = "t", y_title = "phase", y_lim = [0, 2 * 3.14]);
コード例 #7
0
def template_dynamic_legion(num_osc, steps, time, conn_type = conn_type.NONE, stimulus = None, params = None, separate_repr = True):
    net = legion_network(num_osc, stimulus, type_conn = conn_type, parameters = params);
    (t, x, z) = net.simulate(steps, time, solution = solve_type.RK4);
    
    draw_dynamics(t, x, x_title = "Time", y_title = "x(t)", separate = separate_repr);
    draw_dynamics(t, z, x_title = "Time", y_title = "z(t)");
    
    ensembles = net.allocate_sync_ensembles(0.1);
    print(ensembles);
コード例 #8
0
ファイル: pcnn.py プロジェクト: alishakiba/pyclustering
 def show_output_dynamic(pcnn_output_dynamic, separate_representation = False):
     """!
     @brief Shows output dynamic (output of each oscillator) during simulation.
     
     @param[in] pcnn_output_dynamic (pcnn_dynamic): Output dynamic of the pulse-coupled neural network.
     @param[in] separate_representation (list): Consists of lists of oscillators where each such list consists of oscillator indexes that will be shown on separated stage.
     
     """
     
     draw_dynamics(pcnn_output_dynamic.time, pcnn_output_dynamic.output, x_title = "t", y_title = "y(t)", separate = separate_representation);
コード例 #9
0
def template_dynamic_pcnn(num_osc, steps, stimulus = None, params = None, conn_type = conn_type.NONE, separate_representation = True, ccore_flag = True):
    net = pcnn_network(num_osc, params, conn_type, ccore = ccore_flag);
    dynamic = net.simulate(steps, stimulus);
    
    ensembles = dynamic.allocate_sync_ensembles();
    print("Number of objects:", len(ensembles), "\nEnsembles:", ensembles);
    
    draw_dynamics(dynamic.time, dynamic.output, x_title = "t", y_title = "y(t)", separate = separate_representation);
    
    return ensembles;
コード例 #10
0
def template_dynamic(num_osc, own_weight = -3, neigh_weight = -1, initial_states = None, initial_outputs = None, steps = 1000, time = 10):
    network = hysteresis_network(num_osc, own_weight, neigh_weight);
    
    if (initial_states is not None):
        network.states = initial_states;
        
    if (initial_outputs is not None):
        network.outputs = initial_outputs;
    
    (t, x) = network.simulate(steps, time);
    draw_dynamics(t, x, x_title = "Time", y_title = "x(t)");
コード例 #11
0
def template_dynamic_sync(num_osc, k = 1, q = 1, sim_arg = None, conn = conn_type.ALL_TO_ALL, type_solution = solve_type.FAST, collect_dyn = True, ccore_flag = False):
    network = sync_network(num_osc, k, type_conn = conn, ccore = ccore_flag);
    network.cluster = q;

    if (sim_arg is not None):
        (t, dyn_phase) = network.simulate(sim_arg[0], sim_arg[1], solution = type_solution, collect_dynamic = collect_dyn);
    else:
        (t, dyn_phase) = network.simulate_dynamic(collect_dynamic = collect_dyn, solution = type_solution);
        
    draw_dynamics(t, dyn_phase, x_title = "Time", y_title = "Phase", y_lim = [0, 2 * 3.14]);
    return network;
コード例 #12
0
def template_clustering(file, number_clusters, arg_order = 0.999, arg_collect_dynamic = True, ccore_flag = False):
        sample = read_sample(file);
        network = hsyncnet(sample, number_clusters, ccore = ccore_flag);
        
        (time, dynamic) = network.process(arg_order, collect_dynamic = arg_collect_dynamic);
        clusters = network.get_clusters();
        
        if (arg_collect_dynamic == True):
            draw_dynamics(time, dynamic, x_title = "Time", y_title = "Phase", y_lim = [0, 2 * 3.14]);
        
        draw_clusters(sample, clusters);
コード例 #13
0
def template_dynamic_legion(num_osc, steps, time, conn_type, stimulus, params = None, separate_repr = True, ccore_flag = True):
    net = legion_network(num_osc, params, conn_type, ccore = ccore_flag);
    print("Created");
    
    dynamic = net.simulate(steps, time, stimulus, solution = solve_type.RK4);
    print("Simulated");
    
    draw_dynamics(dynamic.time, dynamic.output, x_title = "Time", y_title = "x(t)", separate = separate_repr);
    draw_dynamics(dynamic.time, dynamic.inhibitor, x_title = "Time", y_title = "z(t)");
    
    ensembles = dynamic.allocate_sync_ensembles(0.1);
    print(ensembles);
コード例 #14
0
def template_segmentation_image(source, map_som_size = [5, 5], average_neighbors = 5, sync_order = 0.998, show_dyn = False, show_som_map = False):
    data = read_image(source);
    
    network = syncsom(data, map_som_size[0], map_som_size[1]);
    (ticks, (dyn_time, dyn_phase)) = timedcall(network.process, average_neighbors, show_dyn, sync_order);
    print("Sample: ", source, "\t\tExecution time: ", ticks, "\t\tWinners: ", network.som_layer.get_winner_number(), "\n");
    
    if (show_dyn is True):
        draw_dynamics(dyn_time, dyn_phase);
    
    clusters = network.get_clusters();
    draw_image_mask_segments(source, clusters);
コード例 #15
0
ファイル: pcnn.py プロジェクト: weihuang0908/pyclustering
    def show_output_dynamic(pcnn_output_dynamic,
                            separate_representation=False):
        """!
        @brief Shows output dynamic (output of each oscillator) during simulation.
        
        """

        draw_dynamics(pcnn_output_dynamic.time,
                      pcnn_output_dynamic.output,
                      x_title="t",
                      y_title="y(t)",
                      separate=separate_representation)
コード例 #16
0
def template_dynamic_hhn(num_osc,
                         steps,
                         time,
                         stimulus=None,
                         params=None,
                         separate_representation=False):
    net = hhn_network(num_osc, stimulus, params)

    (t, dyn) = net.simulate(steps, time)

    draw_dynamics(t,
                  dyn,
                  x_title="Time",
                  y_title="V",
                  separate=separate_representation)
コード例 #17
0
def template_graph_coloring(filename, alpha, eps, steps, time, title = None, tolerance = 0.1):
    if (title is None): title = filename;
    
    graph = read_graph(filename);
    network = hysteresisgcolor(graph.data, alpha, eps);
    
    (t, dyn) = network.simulate(steps, time);
    draw_dynamics(t, dyn, x_title = "Time", y_title = "State");
    
    clusters = network.get_clusters(tolerance);
    for index in range(0, len(clusters)):
        print("Color #", index, ": ", clusters[index]);
    
    coloring_map = network.get_map_coloring(tolerance);
    draw_graph(graph, coloring_map);
コード例 #18
0
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);
コード例 #19
0
def template_dynamic(num_osc,
                     own_weight=-3,
                     neigh_weight=-1,
                     initial_states=None,
                     initial_outputs=None,
                     steps=1000,
                     time=10):
    network = hysteresis_network(num_osc, own_weight, neigh_weight)

    if (initial_states is not None):
        network.states = initial_states

    if (initial_outputs is not None):
        network.outputs = initial_outputs

    (t, x) = network.simulate(steps, time)
    draw_dynamics(t, x, x_title="Time", y_title="x(t)")
コード例 #20
0
def template_dynamic_pcnn(num_osc,
                          steps,
                          stimulus=None,
                          params=None,
                          conn_type=conn_type.NONE,
                          separate_representation=True,
                          ccore_flag=True):
    net = pcnn_network(num_osc, params, conn_type, ccore=ccore_flag)
    dynamic = net.simulate(steps, stimulus)

    ensembles = dynamic.allocate_sync_ensembles()
    print("Number of objects:", len(ensembles), "\nEnsembles:", ensembles)

    draw_dynamics(dynamic.time,
                  dynamic.output,
                  x_title="t",
                  y_title="y(t)",
                  separate=separate_representation)

    return ensembles
コード例 #21
0
def template_clustering(file,
                        number_clusters,
                        arg_order=0.999,
                        arg_collect_dynamic=True,
                        ccore_flag=False):
    sample = read_sample(file)
    network = hsyncnet(sample, number_clusters, ccore=ccore_flag)

    (time, dynamic) = network.process(arg_order,
                                      collect_dynamic=arg_collect_dynamic)
    clusters = network.get_clusters()

    if (arg_collect_dynamic == True):
        draw_dynamics(time,
                      dynamic,
                      x_title="Time",
                      y_title="Phase",
                      y_lim=[0, 2 * 3.14])

    draw_clusters(sample, clusters)
コード例 #22
0
def template_segmentation_image(source,
                                map_som_size=[5, 5],
                                average_neighbors=5,
                                sync_order=0.998,
                                show_dyn=False,
                                show_som_map=False):
    data = read_image(source)

    network = syncsom(data, map_som_size[0], map_som_size[1])
    (ticks, (dyn_time, dyn_phase)) = timedcall(network.process,
                                               average_neighbors, show_dyn,
                                               sync_order)
    print("Sample: ", source, "\t\tExecution time: ", ticks, "\t\tWinners: ",
          network.som_layer.get_winner_number(), "\n")

    if (show_dyn is True):
        draw_dynamics(dyn_time, dyn_phase)

    clusters = network.get_clusters()
    draw_image_mask_segments(source, clusters)
コード例 #23
0
def template_clustering(file,
                        map_size,
                        trust_order,
                        sync_order=0.999,
                        show_dyn=False,
                        show_layer1=False,
                        show_layer2=False,
                        show_clusters=True):
    # Read sample
    sample = read_sample(file)

    # Create network
    network = syncsom(sample, map_size[0], map_size[1])

    # Run processing
    (ticks, (dyn_time, dyn_phase)) = timedcall(network.process, trust_order,
                                               show_dyn, sync_order)
    print("Sample: ", file, "\t\tExecution time: ", ticks, "\n")

    # Show dynamic of the last layer.
    if (show_dyn == True):
        draw_dynamics(dyn_time,
                      dyn_phase,
                      x_title="Time",
                      y_title="Phase",
                      y_lim=[0, 2 * 3.14])

    if (show_clusters == True):
        clusters = network.get_som_clusters()
        draw_clusters(network.som_layer.weights, clusters)

    # Show network stuff.
    if (show_layer1 == True):
        network.show_som_layer()

    if (show_layer2 == True):
        network.show_sync_layer()

    if (show_clusters == True):
        clusters = network.get_clusters()
        draw_clusters(sample, clusters)
コード例 #24
0
def template_graph_coloring(filename,
                            alpha,
                            eps,
                            steps,
                            time,
                            title=None,
                            tolerance=0.1):
    if (title is None): title = filename

    graph = read_graph(filename)
    network = hysteresisgcolor(graph.data, alpha, eps)

    (t, dyn) = network.simulate(steps, time)
    draw_dynamics(t, dyn, x_title="Time", y_title="State")

    clusters = network.get_clusters(tolerance)
    for index in range(0, len(clusters)):
        print("Color #", index, ": ", clusters[index])

    coloring_map = network.get_map_coloring(tolerance)
    draw_graph(graph, coloring_map)
コード例 #25
0
def template_graph_coloring(positive_weight,
                            negative_weight,
                            filename,
                            reduction=None,
                            title=None):
    if (title is None): title = filename
    print("\nGraph Coloring: ", title)

    graph = read_graph(filename)
    network = syncgcolor(graph.data, positive_weight, negative_weight,
                         reduction)

    (t, dyn) = network.process(order=0.999,
                               solution=solve_type.FAST,
                               collect_dynamic=True)
    draw_dynamics(t, dyn, x_title="Time", y_title="Phase", y_lim=[0, 2 * 3.14])

    clusters = network.get_clusters()

    for index in range(0, len(clusters)):
        print("Color #", index, ": ", clusters[index])

    coloring_map = network.get_map_coloring()
    print("Number colors: ", max(coloring_map))

    draw_graph(graph, coloring_map)

    # Check validity of colors
    for index_node in range(len(graph.data)):
        color_neighbors = [
            coloring_map[index] for index in range(len(graph.data[index_node]))
            if graph.data[index_node][index] != 0 and index_node != index
        ]
        #print(index_node, map_coloring[index_node], color_neighbors, assigned_colors, map_coloring, "\n\n");

        if (coloring_map[index_node] in color_neighbors):
            print("Warining: Incorrect coloring")
            return
コード例 #26
0
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)
コード例 #27
0
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);
コード例 #28
0
def template_dynamic_hhn(num_osc, steps, time, stimulus = None, params = None, separate_representation = False):
    net = hhn_network(num_osc, stimulus, params);

    (t, dyn) = net.simulate(steps, time);

    draw_dynamics(t, dyn, x_title = "Time", y_title = "V", separate = separate_representation);