3 @brief Cluster analysis algorithm: Hierarchical Sync (HSyncNet)
4 @details Implementation based on paper @cite artcile::hsyncnet::1.
6 @authors Andrei Novikov (pyclustering@yandex.ru)
8 @copyright BSD-3-Clause
12 import pyclustering.core.hsyncnet_wrapper
as wrapper
14 from pyclustering.core.wrapper
import ccore_library
25 @brief Class represents clustering algorithm HSyncNet. HSyncNet is bio-inspired algorithm that is based on oscillatory network that uses modified Kuramoto model.
29 from pyclustering.cluster.hsyncnet import hsyncnet
30 from pyclustering.nnet.sync import sync_visualizer
31 from pyclustering.utils import read_sample, draw_clusters
32 from pyclustering.samples.definitions import SIMPLE_SAMPLES
34 # Read list of points for cluster analysis.
35 sample = read_sample(SIMPLE_SAMPLES.SAMPLE_SIMPLE2)
37 # Create network for allocation of three clusters.
38 network = hsyncnet(sample, 3)
40 # Run cluster analysis and output dynamic of the network.
41 analyser = network.process(0.995, collect_dynamic=True)
43 # Get allocated clusters.
44 clusters = analyser.allocate_clusters(eps=0.1)
46 # Show output dynamic of the network.
47 sync_visualizer.show_output_dynamic(analyser)
49 # Show allocated clusters.
50 draw_clusters(sample, clusters)
54 def __init__(self, source_data, number_clusters, osc_initial_phases=initial_type.RANDOM_GAUSSIAN,
55 initial_neighbors=3, increase_persent=0.15, ccore=True):
57 @brief Costructor of the oscillatory network hSyncNet for cluster analysis.
59 @param[in] source_data (list): Input data set defines structure of the network.
60 @param[in] number_clusters (uint): Number of clusters that should be allocated.
61 @param[in] osc_initial_phases (initial_type): Type of initialization of initial values of phases of oscillators.
62 @param[in] initial_neighbors (uint): Defines initial connectivity-radius by average distance to connect specified amount of oscillators (points).
63 @param[in] increase_persent (double): Percent of increasing of radius connectivity on each iteration (input values in range (0.0; 1.0) correspond to (0%; 100%)).
64 @param[in] ccore (bool): If True than DLL CCORE (C++ solution) will be used for solving.
70 if initial_neighbors >= len(source_data):
71 initial_neighbors = len(source_data) - 1
73 if (ccore
is True)
and ccore_library.workable():
75 osc_initial_phases, initial_neighbors,
78 super().
__init__(source_data, 0, initial_phases=osc_initial_phases, ccore=
False)
86 @brief Destructor of oscillatory network HSyncNet.
94 def process(self, order=0.998, solution=solve_type.FAST, collect_dynamic=False):
96 @brief Performs clustering of input data set in line with input parameters.
98 @param[in] order (double): Level of local synchronization between oscillator that defines end of synchronization process, range [0..1].
99 @param[in] solution (solve_type) Type of solving differential equation.
100 @param[in] collect_dynamic (bool): If True - returns whole history of process synchronization otherwise - only final state (when process of clustering is over).
102 @return (tuple) Returns dynamic of the network as tuple of lists on each iteration (time, oscillator_phases) that depends on collect_dynamic parameter.
113 current_number_clusters = float(
'inf')
118 radius = average_neighbor_distance(self.
_osc_loc, number_neighbors)
121 if increase_step < 1:
130 if len(dyn_phase) == 0:
135 clusters = analyser.allocate_sync_ensembles(0.05)
138 current_number_clusters = len(clusters)
141 number_neighbors += increase_step
146 if not collect_dynamic:
151 def __calculate_radius(self, number_neighbors, radius):
153 @brief Calculate new connectivity radius.
155 @param[in] number_neighbors (uint): Average amount of neighbors that should be connected by new radius.
156 @param[in] radius (double): Current connectivity radius.
158 @return New connectivity radius.
162 if number_neighbors >= len(self.
_osc_loc):
165 return average_neighbor_distance(self.
_osc_loc, number_neighbors)
167 def __store_dynamic(self, dyn_phase, dyn_time, analyser, begin_state):
169 @brief Store specified state of Sync network to hSync.
171 @param[in] dyn_phase (list): Output dynamic of hSync where state should be stored.
172 @param[in] dyn_time (list): Time points that correspond to output dynamic where new time point should be stored.
173 @param[in] analyser (syncnet_analyser): Sync analyser where Sync states are stored.
174 @param[in] begin_state (bool): If True the first state of Sync network is stored, otherwise the last state is stored.
178 if begin_state
is True:
180 dyn_phase.append(analyser.output[0])
183 dyn_phase.append(analyser.output[len(analyser.output) - 1])
184 dyn_time.append(len(dyn_time))