pyclustering  0.10.1
pyclustring is a Python, C++ data mining library.
hsyncnet.py
1 """!
2 
3 @brief Cluster analysis algorithm: Hierarchical Sync (HSyncNet)
4 @details Implementation based on paper @cite artcile::hsyncnet::1.
5 
6 @authors Andrei Novikov (pyclustering@yandex.ru)
7 @date 2014-2020
8 @copyright BSD-3-Clause
9 
10 """
11 
12 import pyclustering.core.hsyncnet_wrapper as wrapper
13 
14 from pyclustering.core.wrapper import ccore_library
15 
16 from pyclustering.nnet import initial_type, solve_type
17 
18 from pyclustering.cluster.syncnet import syncnet, syncnet_analyser
19 
20 from pyclustering.utils import average_neighbor_distance
21 
22 
24  """!
25  @brief Class represents clustering algorithm HSyncNet. HSyncNet is bio-inspired algorithm that is based on oscillatory network that uses modified Kuramoto model.
26 
27  Example:
28  @code
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
33 
34  # Read list of points for cluster analysis.
35  sample = read_sample(SIMPLE_SAMPLES.SAMPLE_SIMPLE2)
36 
37  # Create network for allocation of three clusters.
38  network = hsyncnet(sample, 3)
39 
40  # Run cluster analysis and output dynamic of the network.
41  analyser = network.process(0.995, collect_dynamic=True)
42 
43  # Get allocated clusters.
44  clusters = analyser.allocate_clusters(eps=0.1)
45 
46  # Show output dynamic of the network.
47  sync_visualizer.show_output_dynamic(analyser)
48 
49  # Show allocated clusters.
50  draw_clusters(sample, clusters)
51  @endcode
52  """
53 
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):
56  """!
57  @brief Costructor of the oscillatory network hSyncNet for cluster analysis.
58 
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.
65 
66  """
67 
68  self.__ccore_network_pointer = None
69 
70  if initial_neighbors >= len(source_data):
71  initial_neighbors = len(source_data) - 1
72 
73  if (ccore is True) and ccore_library.workable():
74  self.__ccore_network_pointer = wrapper.hsyncnet_create_network(source_data, number_clusters,
75  osc_initial_phases, initial_neighbors,
76  increase_persent)
77  else:
78  super().__init__(source_data, 0, initial_phases=osc_initial_phases, ccore=False)
79 
80  self.__initial_neighbors = initial_neighbors
81  self.__increase_persent = increase_persent
82  self._number_clusters = number_clusters
83 
84  def __del__(self):
85  """!
86  @brief Destructor of oscillatory network HSyncNet.
87 
88  """
89 
90  if self.__ccore_network_pointer is not None:
91  wrapper.hsyncnet_destroy_network(self.__ccore_network_pointer)
92  self.__ccore_network_pointer = None
93 
94  def process(self, order=0.998, solution=solve_type.FAST, collect_dynamic=False):
95  """!
96  @brief Performs clustering of input data set in line with input parameters.
97 
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).
101 
102  @return (tuple) Returns dynamic of the network as tuple of lists on each iteration (time, oscillator_phases) that depends on collect_dynamic parameter.
103 
104  @see get_clusters()
105 
106  """
107 
108  if self.__ccore_network_pointer is not None:
109  analyser = wrapper.hsyncnet_process(self.__ccore_network_pointer, order, solution, collect_dynamic)
110  return syncnet_analyser(None, None, analyser)
111 
112  number_neighbors = self.__initial_neighbors
113  current_number_clusters = float('inf')
114 
115  dyn_phase = []
116  dyn_time = []
117 
118  radius = average_neighbor_distance(self._osc_loc, number_neighbors)
119 
120  increase_step = int(len(self._osc_loc) * self.__increase_persent)
121  if increase_step < 1:
122  increase_step = 1
123 
124  analyser = None
125  while current_number_clusters > self._number_clusters:
126  self._create_connections(radius)
127 
128  analyser = self.simulate_dynamic(order, solution, collect_dynamic)
129  if collect_dynamic:
130  if len(dyn_phase) == 0:
131  self.__store_dynamic(dyn_phase, dyn_time, analyser, True)
132 
133  self.__store_dynamic(dyn_phase, dyn_time, analyser, False)
134 
135  clusters = analyser.allocate_sync_ensembles(0.05)
136 
137  # Get current number of allocated clusters
138  current_number_clusters = len(clusters)
139 
140  # Increase number of neighbors that should be used
141  number_neighbors += increase_step
142 
143  # Update connectivity radius and check if average function can be used anymore
144  radius = self.__calculate_radius(number_neighbors, radius)
145 
146  if not collect_dynamic:
147  self.__store_dynamic(dyn_phase, dyn_time, analyser, False)
148 
149  return syncnet_analyser(dyn_phase, dyn_time, None)
150 
151  def __calculate_radius(self, number_neighbors, radius):
152  """!
153  @brief Calculate new connectivity radius.
154 
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.
157 
158  @return New connectivity radius.
159 
160  """
161 
162  if number_neighbors >= len(self._osc_loc):
163  return radius * self.__increase_persent + radius
164 
165  return average_neighbor_distance(self._osc_loc, number_neighbors)
166 
167  def __store_dynamic(self, dyn_phase, dyn_time, analyser, begin_state):
168  """!
169  @brief Store specified state of Sync network to hSync.
170 
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.
175 
176  """
177 
178  if begin_state is True:
179  dyn_time.append(0)
180  dyn_phase.append(analyser.output[0])
181 
182  else:
183  dyn_phase.append(analyser.output[len(analyser.output) - 1])
184  dyn_time.append(len(dyn_time))
pyclustering.nnet.sync.sync_network.simulate_dynamic
def simulate_dynamic(self, order=0.998, solution=solve_type.FAST, collect_dynamic=False, step=0.1, int_step=0.01, threshold_changes=0.0000001)
Performs dynamic simulation of the network until stop condition is not reached.
Definition: sync.py:851
pyclustering.cluster.hsyncnet.hsyncnet.__del__
def __del__(self)
Destructor of oscillatory network HSyncNet.
Definition: hsyncnet.py:84
pyclustering.cluster.hsyncnet.hsyncnet._number_clusters
_number_clusters
Definition: hsyncnet.py:81
pyclustering.cluster.syncnet
Cluster analysis algorithm: Sync.
Definition: syncnet.py:1
pyclustering.cluster.hsyncnet.hsyncnet
Class represents clustering algorithm HSyncNet.
Definition: hsyncnet.py:23
pyclustering.cluster.hsyncnet.hsyncnet.__calculate_radius
def __calculate_radius(self, number_neighbors, radius)
Calculate new connectivity radius.
Definition: hsyncnet.py:151
pyclustering.cluster.hsyncnet.hsyncnet.__init__
def __init__(self, source_data, number_clusters, osc_initial_phases=initial_type.RANDOM_GAUSSIAN, initial_neighbors=3, increase_persent=0.15, ccore=True)
Costructor of the oscillatory network hSyncNet for cluster analysis.
Definition: hsyncnet.py:54
pyclustering.cluster.syncnet.syncnet
Class represents clustering algorithm SyncNet.
Definition: syncnet.py:146
pyclustering.cluster.syncnet.syncnet._osc_loc
_osc_loc
Definition: syncnet.py:198
pyclustering.cluster.syncnet.syncnet_analyser
Performs analysis of output dynamic of the oscillatory network syncnet to extract information about c...
Definition: syncnet.py:30
pyclustering.cluster.hsyncnet.hsyncnet.__store_dynamic
def __store_dynamic(self, dyn_phase, dyn_time, analyser, begin_state)
Store specified state of Sync network to hSync.
Definition: hsyncnet.py:167
pyclustering.cluster.hsyncnet.hsyncnet.__increase_persent
__increase_persent
Definition: hsyncnet.py:80
pyclustering.cluster.hsyncnet.hsyncnet.process
def process(self, order=0.998, solution=solve_type.FAST, collect_dynamic=False)
Performs clustering of input data set in line with input parameters.
Definition: hsyncnet.py:94
pyclustering.nnet
Neural and oscillatory network module. Consists of models of bio-inspired networks.
Definition: __init__.py:1
pyclustering.cluster.hsyncnet.hsyncnet.__initial_neighbors
__initial_neighbors
Definition: hsyncnet.py:79
pyclustering.cluster.hsyncnet.hsyncnet.__ccore_network_pointer
__ccore_network_pointer
Definition: hsyncnet.py:67
pyclustering.cluster.syncnet.syncnet._create_connections
def _create_connections(self, radius)
Create connections between oscillators in line with input radius of connectivity.
Definition: syncnet.py:241
pyclustering.utils
Utils that are used by modules of pyclustering.
Definition: __init__.py:1