3 @brief Neural Network: Pulse Coupled Neural Network 4 @details Implementation based on paper @cite book::image_processing_using_pcnn. 6 @authors Andrei Novikov (pyclustering@yandex.ru) 8 @copyright GNU Public License 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. 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. 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/>. 32 import matplotlib.pyplot
as plt
33 import matplotlib.animation
as animation
34 except Exception
as error_instance:
35 warnings.warn(
"Impossible to import matplotlib (please, install 'matplotlib'), pyclustering's visualization " 36 "functionality is partially not available (details: '%s')." % str(error_instance))
40 except Exception
as error_instance:
41 warnings.warn(
"Impossible to import PIL (please, install 'PIL'), pyclustering's visualization " 42 "functionality is partially not available (details: '%s')." % str(error_instance))
46 from pyclustering.core.wrapper
import ccore_library
48 import pyclustering.core.pcnn_wrapper
as wrapper
55 @brief Parameters for pulse coupled neural network. 61 @brief Default constructor of parameters for pulse-coupled neural network. 62 @details Constructor initializes parameters by default non-zero values that can be 63 used for simple simulation. 102 @brief Represents output dynamic of PCNN (pulse-coupled neural network). 109 @brief (list) Returns oscillato outputs during simulation. 121 @brief (list) Returns sampling times when dynamic is measured during simulation. 127 return list(range(len(self)))
132 @brief Constructor of PCNN dynamic. 134 @param[in] dynamic (list): Dynamic of oscillators on each step of simulation. If ccore pointer is specified than it can be ignored. 135 @param[in] ccore (ctypes.pointer): Pointer to CCORE pcnn_dynamic instance in memory. 147 @brief Default destructor of PCNN dynamic. 156 @brief (uint) Returns number of simulation steps that are stored in dynamic. 167 @brief Allocate clusters in line with ensembles of synchronous oscillators where each 168 synchronous ensemble corresponds to only one cluster. 170 @return (list) Grours (lists) of indexes of synchronous oscillators. 171 For example, [ [index_osc1, index_osc3], [index_osc2], [index_osc4, index_osc5] ]. 179 traverse_oscillators = set()
181 number_oscillators = len(self.
__dynamic[0])
183 for t
in range(len(self.
__dynamic) - 1, 0, -1):
185 for i
in range(number_oscillators):
187 if i
not in traverse_oscillators:
188 sync_ensemble.append(i)
189 traverse_oscillators.add(i)
191 if sync_ensemble != []:
192 sync_ensembles.append(sync_ensemble)
194 return sync_ensembles
199 @brief Analyses output dynamic of network and allocates spikes on each iteration as a list of indexes of oscillators. 200 @details Each allocated spike ensemble represents list of indexes of oscillators whose output is active. 202 @return (list) Spike ensembles of oscillators. 210 number_oscillators = len(self.
__dynamic[0])
215 for index
in range(number_oscillators):
217 spike_ensemble.append(index)
219 if len(spike_ensemble) > 0:
220 spike_ensembles.append(spike_ensemble)
222 return spike_ensembles
227 @brief Analyses output dynamic and calculates time signal (signal vector information) of network output. 229 @return (list) Time signal of network output. 236 signal_vector_information = []
238 signal_vector_information.append(sum(self.
__dynamic[t]))
240 return signal_vector_information
245 @brief Visualizer of output dynamic of pulse-coupled neural network (PCNN). 252 @brief Shows time signal (signal vector information) using network dynamic during simulation. 254 @param[in] pcnn_output_dynamic (pcnn_dynamic): Output dynamic of the pulse-coupled neural network. 258 time_signal = pcnn_output_dynamic.allocate_time_signal()
259 time_axis = range(len(time_signal))
262 plt.plot(time_axis, time_signal,
'-')
263 plt.ylabel(
"G (time signal)")
264 plt.xlabel(
"t (iteration)")
272 @brief Shows output dynamic (output of each oscillator) during simulation. 274 @param[in] pcnn_output_dynamic (pcnn_dynamic): Output dynamic of the pulse-coupled neural network. 275 @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. 279 draw_dynamics(pcnn_output_dynamic.time, pcnn_output_dynamic.output, x_title =
"t", y_title =
"y(t)", separate = separate_representation)
284 @brief Shows animation of output dynamic (output of each oscillator) during simulation. 286 @param[in] pcnn_output_dynamic (pcnn_dynamic): Output dynamic of the pulse-coupled neural network. 287 @param[in] image_size (tuple): Image size represented as (height, width). 291 figure = plt.figure()
293 time_signal = pcnn_output_dynamic.allocate_time_signal()
294 spike_ensembles = pcnn_output_dynamic.allocate_spike_ensembles()
298 for t
in range(len(time_signal)):
299 image_color_segments = [(255, 255, 255)] * (image_size[0] * image_size[1])
301 if time_signal[t] > 0:
302 for index_pixel
in spike_ensembles[ensemble_index]:
303 image_color_segments[index_pixel] = (0, 0, 0)
307 stage = numpy.array(image_color_segments, numpy.uint8)
308 stage = numpy.reshape(stage, image_size + ((3),))
309 image_cluster = Image.fromarray(stage,
'RGB')
311 spike_animation.append( [ plt.imshow(image_cluster, interpolation=
'none') ] )
314 im_ani = animation.ArtistAnimation(figure, spike_animation, interval=75, repeat_delay=3000, blit=
True)
320 @brief Model of oscillatory network that is based on the Eckhorn model. 322 @details CCORE option can be used to use the pyclustering core - C/C++ shared library for processing that significantly increases performance. 326 # Create pulse-coupled neural network: 328 # - default parameters. 329 # - grid type of connections (each oscillator has connection with four neighbors). 330 net = pcnn_network(9, None, conn_type.GRID_FOUR, ccore = ccore_flag) 332 # Create external stimulus. Number of stimulus should be equal to number of neurons. 333 stimulus = [1, 1, 1, 0, 0, 0, 1, 1, 1] 335 # Simulate dynamic of the network during 40 iterations 336 dynamic = net.simulate(40, stimulus) 338 # Allocate synchronous oscillators 339 ensembles = dynamic.allocate_sync_ensembles() 342 # Show output dynamic of the network 343 pcnn_visualizer.show_output_dynamic(dynamic) 345 # Show time signal vector information 346 pcnn_visualizer.show_time_signal(dynamic) 354 def __init__(self, num_osc, parameters=None, type_conn=conn_type.ALL_TO_ALL, type_conn_represent=conn_represent.MATRIX, height=None, width=None, ccore=True):
356 @brief Constructor of oscillatory network is based on Kuramoto model. 358 @param[in] num_osc (uint): Number of oscillators in the network. 359 @param[in] parameters (pcnn_parameters): Parameters of the network. 360 @param[in] type_conn (conn_type): Type of connection between oscillators in the network (all-to-all, grid, bidirectional list, etc.). 361 @param[in] type_conn_represent (conn_represent): Internal representation of connection in the network: matrix or list. 362 @param[in] height (uint): Number of oscillators in column of the network, this argument is used 363 only for network with grid structure (GRID_FOUR, GRID_EIGHT), for other types this argument is ignored. 364 @param[in] width (uint): Number of oscillotors in row of the network, this argument is used only 365 for network with grid structure (GRID_FOUR, GRID_EIGHT), for other types this argument is ignored. 366 @param[in] ccore (bool): If True then all interaction with object will be performed via CCORE library (C++ implementation of pyclustering). 381 if parameters
is not None:
386 if (ccore
is True)
and ccore_library.workable():
387 network_height = height
388 network_width = width
390 if (type_conn == conn_type.GRID_FOUR)
or (type_conn == conn_type.GRID_EIGHT):
391 if (network_height
is None)
or (network_width
is None):
392 side_size = num_osc ** (0.5)
393 if side_size - math.floor(side_size) > 0:
394 raise NameError(
'Invalid number of oscillators in the network in case of grid structure')
396 network_height = int(side_size)
397 network_width = int(side_size)
404 super().
__init__(num_osc, type_conn, type_conn_represent, height, width)
415 @brief Default destructor of PCNN. 425 @brief (uint) Returns size of oscillatory network. 437 @brief Performs static simulation of pulse coupled neural network using. 439 @param[in] steps (uint): Number steps of simulations during simulation. 440 @param[in] stimulus (list): Stimulus for oscillators, number of stimulus should be equal to number of oscillators. 442 @return (pcnn_dynamic) Dynamic of oscillatory network - output of each oscillator on each step of simulation. 446 if len(stimulus) != len(self):
447 raise NameError(
'Number of stimulus should be equal to number of oscillators. Each stimulus corresponds to only one oscillators.')
456 for step
in range(1, steps, 1):
464 def _calculate_states(self, stimulus):
466 @brief Calculates states of oscillators in the network for current step and stored them except outputs of oscillators. 468 @param[in] stimulus (list): Stimulus for oscillators, number of stimulus should be equal to number of oscillators. 470 @return (list) New outputs for oscillators (do not stored it). 479 for index
in range(0, self.
_num_osc, 1):
482 feeding_influence = 0.0
483 linking_influence = 0.0
485 for index_neighbour
in neighbors:
489 feeding_influence *= self.
_params.VF
490 linking_influence *= self.
_params.VL
492 feeding[index] = self.
_params.AF * self.
_feeding[index] + stimulus[index] + feeding_influence
493 linking[index] = self.
_params.AL * self.
_linking[index] + linking_influence
496 internal_activity = feeding[index] * (1.0 + self.
_params.B * linking[index])
499 if internal_activity > self.
_threshold[index]:
505 if self.
_params.FAST_LINKING
is not True:
509 if self.
_params.FAST_LINKING
is True:
511 previous_outputs = outputs[:]
513 while output_change
is True:
514 current_output_change =
False 516 for index
in range(0, self.
_num_osc, 1):
517 linking_influence = 0.0
520 for index_neighbour
in neighbors:
521 linking_influence += previous_outputs[index_neighbour] * self.
_params.W
523 linking_influence *= self.
_params.VL
524 linking[index] = linking_influence
526 internal_activity = feeding[index] * (1.0 + self.
_params.B * linking[index])
529 if internal_activity > self.
_threshold[index]:
534 current_output_change |= (outputs[index] != previous_outputs[index])
536 output_change = current_output_change
538 if output_change
is True:
539 previous_outputs = outputs[:]
542 if self.
_params.FAST_LINKING
is True:
543 for index
in range(0, self.
_num_osc, 1):
Model of oscillatory network that is based on the Eckhorn model.
M
Synaptic weight - neighbours influence on feeding compartment.
FAST_LINKING
Enable/disable Fast-Linking mode.
AF
Multiplier for the feeding compartment at the previous step.
def __del__(self)
Default destructor of PCNN dynamic.
def _calculate_states(self, stimulus)
Calculates states of oscillators in the network for current step and stored them except outputs of os...
AT
Multiplier for the threshold at the previous step.
def animate_spike_ensembles(pcnn_output_dynamic, image_size)
Shows animation of output dynamic (output of each oscillator) during simulation.
def time(self)
(list) Returns sampling times when dynamic is measured during simulation.
Utils that are used by modules of pyclustering.
AL
Multiplier for the linking compartment at the previous step.
Parameters for pulse coupled neural network.
def simulate(self, steps, stimulus)
Performs static simulation of pulse coupled neural network using.
W
Synaptic weight - neighbours influence on linking compartment.
def allocate_spike_ensembles(self)
Analyses output dynamic of network and allocates spikes on each iteration as a list of indexes of osc...
B
Linking strength in the network.
def get_neighbors(self, index)
Finds neighbors of the oscillator with specified index.
def __init__(self, num_osc, parameters=None, type_conn=conn_type.ALL_TO_ALL, type_conn_represent=conn_represent.MATRIX, height=None, width=None, ccore=True)
Constructor of oscillatory network is based on Kuramoto model.
__ccore_pcnn_dynamic_pointer
def __init__(self)
Default constructor of parameters for pulse-coupled neural network.
VT
Multiplier for the threshold at the current step.
Represents output dynamic of PCNN (pulse-coupled neural network).
def __del__(self)
Default destructor of PCNN.
Common network description that consists of information about oscillators and connection between them...
def __len__(self)
(uint) Returns number of simulation steps that are stored in dynamic.
def show_output_dynamic(pcnn_output_dynamic, separate_representation=False)
Shows output dynamic (output of each oscillator) during simulation.
VF
Multiplier for the feeding compartment at the current step.
def allocate_sync_ensembles(self)
Allocate clusters in line with ensembles of synchronous oscillators where each synchronous ensemble c...
def __len__(self)
(uint) Returns size of oscillatory network.
VL
Multiplier for the linking compartment at the current step.
Visualizer of output dynamic of pulse-coupled neural network (PCNN).
def allocate_time_signal(self)
Analyses output dynamic and calculates time signal (signal vector information) of network output...
def __init__(self, dynamic, ccore=None)
Constructor of PCNN dynamic.
def output(self)
(list) Returns oscillato outputs during simulation.
Neural and oscillatory network module.
def show_time_signal(pcnn_output_dynamic)
Shows time signal (signal vector information) using network dynamic during simulation.