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-2019
8 @copyright GNU Public License
9 
10 @cond GNU_PUBLIC_LICENSE
11  PyClustering is free software: you can redistribute it and/or modify
12  it under the terms of the GNU General Public License as published by
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
16  PyClustering is distributed in the hope that it will be useful,
17  but WITHOUT ANY WARRANTY; without even the implied warranty of
18  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  GNU General Public License for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with this program. If not, see <http://www.gnu.org/licenses/>.
23 @endcond
24 
25 """
26 
27 
28 import pyclustering.core.hsyncnet_wrapper as wrapper
29 
30 from pyclustering.core.wrapper import ccore_library
31 
32 from pyclustering.nnet import initial_type, solve_type
33 
34 from pyclustering.cluster.syncnet import syncnet, syncnet_analyser
35 
36 from pyclustering.utils import average_neighbor_distance
37 
38 
40  """!
41  @brief Class represents clustering algorithm HSyncNet. HSyncNet is bio-inspired algorithm that is based on oscillatory network that uses modified Kuramoto model.
42 
43  Example:
44  @code
45  from pyclustering.cluster.hsyncnet import hsyncnet
46  from pyclustering.nnet.sync import sync_visualizer
47  from pyclustering.utils import read_sample, draw_clusters
48  from pyclustering.samples.definitions import SIMPLE_SAMPLES
49 
50  # Read list of points for cluster analysis.
51  sample = read_sample(SIMPLE_SAMPLES.SAMPLE_SIMPLE2)
52 
53  # Create network for allocation of three clusters.
54  network = hsyncnet(sample, 3)
55 
56  # Run cluster analysis and output dynamic of the network.
57  analyser = network.process(0.995, collect_dynamic=True)
58 
59  # Get allocated clusters.
60  clusters = analyser.allocate_clusters(eps=0.1)
61 
62  # Show output dynamic of the network.
63  sync_visualizer.show_output_dynamic(analyser)
64 
65  # Show allocated clusters.
66  draw_clusters(sample, clusters)
67  @endcode
68  """
69 
70  def __init__(self, source_data, number_clusters, osc_initial_phases=initial_type.RANDOM_GAUSSIAN,
71  initial_neighbors=3, increase_persent=0.15, ccore=True):
72  """!
73  @brief Costructor of the oscillatory network hSyncNet for cluster analysis.
74 
75  @param[in] source_data (list): Input data set defines structure of the network.
76  @param[in] number_clusters (uint): Number of clusters that should be allocated.
77  @param[in] osc_initial_phases (initial_type): Type of initialization of initial values of phases of oscillators.
78  @param[in] initial_neighbors (uint): Defines initial radius connectivity by calculation average distance to connect specify number of oscillators.
79  @param[in] increase_persent (double): Percent of increasing of radius connectivity on each step (input values in range (0.0; 1.0) correspond to (0%; 100%)).
80  @param[in] ccore (bool): If True than DLL CCORE (C++ solution) will be used for solving.
81 
82  """
83 
84  self.__ccore_network_pointer = None
85 
86  if initial_neighbors >= len(source_data):
87  initial_neighbors = len(source_data) - 1
88 
89  if (ccore is True) and ccore_library.workable():
90  self.__ccore_network_pointer = wrapper.hsyncnet_create_network(source_data, number_clusters, osc_initial_phases, initial_neighbors, increase_persent)
91  else:
92  super().__init__(source_data, 0, initial_phases=osc_initial_phases, ccore=False)
93 
94  self.__initial_neighbors = initial_neighbors
95  self.__increase_persent = increase_persent
96  self._number_clusters = number_clusters
97 
98 
99  def __del__(self):
100  """!
101  @brief Destructor of oscillatory network HSyncNet.
102 
103  """
104 
105  if self.__ccore_network_pointer is not None:
106  wrapper.hsyncnet_destroy_network(self.__ccore_network_pointer)
107  self.__ccore_network_pointer = None
108 
109 
110  def process(self, order = 0.998, solution = solve_type.FAST, collect_dynamic = False):
111  """!
112  @brief Performs clustering of input data set in line with input parameters.
113 
114  @param[in] order (double): Level of local synchronization between oscillator that defines end of synchronization process, range [0..1].
115  @param[in] solution (solve_type) Type of solving differential equation.
116  @param[in] collect_dynamic (bool): If True - returns whole history of process synchronization otherwise - only final state (when process of clustering is over).
117 
118  @return (tuple) Returns dynamic of the network as tuple of lists on each iteration (time, oscillator_phases) that depends on collect_dynamic parameter.
119 
120  @see get_clusters()
121 
122  """
123 
124  if self.__ccore_network_pointer is not None:
125  analyser = wrapper.hsyncnet_process(self.__ccore_network_pointer, order, solution, collect_dynamic)
126  return syncnet_analyser(None, None, analyser)
127 
128  number_neighbors = self.__initial_neighbors
129  current_number_clusters = float('inf')
130 
131  dyn_phase = []
132  dyn_time = []
133 
134  radius = average_neighbor_distance(self._osc_loc, number_neighbors)
135 
136  increase_step = int(len(self._osc_loc) * self.__increase_persent)
137  if increase_step < 1:
138  increase_step = 1
139 
140 
141  analyser = None
142  while current_number_clusters > self._number_clusters:
143  self._create_connections(radius)
144 
145  analyser = self.simulate_dynamic(order, solution, collect_dynamic)
146  if collect_dynamic == True:
147  if len(dyn_phase) == 0:
148  self.__store_dynamic(dyn_phase, dyn_time, analyser, True)
149 
150  self.__store_dynamic(dyn_phase, dyn_time, analyser, False)
151 
152  clusters = analyser.allocate_sync_ensembles(0.05)
153 
154  # Get current number of allocated clusters
155  current_number_clusters = len(clusters)
156 
157  # Increase number of neighbors that should be used
158  number_neighbors += increase_step
159 
160  # Update connectivity radius and check if average function can be used anymore
161  radius = self.__calculate_radius(number_neighbors, radius)
162 
163  if (collect_dynamic != True):
164  self.__store_dynamic(dyn_phase, dyn_time, analyser, False)
165 
166  return syncnet_analyser(dyn_phase, dyn_time, None)
167 
168 
169  def __calculate_radius(self, number_neighbors, radius):
170  """!
171  @brief Calculate new connectivity radius.
172 
173  @param[in] number_neighbors (uint): Average amount of neighbors that should be connected by new radius.
174  @param[in] radius (double): Current connectivity radius.
175 
176  @return New connectivity radius.
177 
178  """
179 
180  if (number_neighbors >= len(self._osc_loc)):
181  return radius * self.__increase_persent + radius;
182 
183  return average_neighbor_distance(self._osc_loc, number_neighbors);
184 
185 
186  def __store_dynamic(self, dyn_phase, dyn_time, analyser, begin_state):
187  """!
188  @brief Store specified state of Sync network to hSync.
189 
190  @param[in] dyn_phase (list): Output dynamic of hSync where state should be stored.
191  @param[in] dyn_time (list): Time points that correspond to output dynamic where new time point should be stored.
192  @param[in] analyser (syncnet_analyser): Sync analyser where Sync states are stored.
193  @param[in] begin_state (bool): If True the first state of Sync network is stored, otherwise the last state is stored.
194 
195  """
196 
197  if (begin_state is True):
198  dyn_time.append(0);
199  dyn_phase.append(analyser.output[0]);
200 
201  else:
202  dyn_phase.append(analyser.output[len(analyser.output) - 1]);
203  dyn_time.append(len(dyn_time));
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:871
def __del__(self)
Destructor of oscillatory network HSyncNet.
Definition: hsyncnet.py:99
Utils that are used by modules of pyclustering.
Definition: __init__.py:1
Performs analysis of output dynamic of the oscillatory network syncnet to extract information about c...
Definition: syncnet.py:50
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:71
Class represents clustering algorithm SyncNet.
Definition: syncnet.py:164
def __calculate_radius(self, number_neighbors, radius)
Calculate new connectivity radius.
Definition: hsyncnet.py:169
def __store_dynamic(self, dyn_phase, dyn_time, analyser, begin_state)
Store specified state of Sync network to hSync.
Definition: hsyncnet.py:186
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:110
Class represents clustering algorithm HSyncNet.
Definition: hsyncnet.py:39
Cluster analysis algorithm: Sync.
Definition: syncnet.py:1
Neural and oscillatory network module.
Definition: __init__.py:1
def _create_connections(self, radius)
Create connections between oscillators in line with input radius of connectivity. ...
Definition: syncnet.py:259