3 @brief Utils that are used by modules of pyclustering.
5 @authors Andrei Novikov (pyclustering@yandex.ru)
7 @copyright BSD-3-Clause
15 from numpy
import array
18 import matplotlib.pyplot
as plt
19 from mpl_toolkits.mplot3d
import Axes3D
21 from sys
import platform
as _platform
32 @brief Returns data sample from simple text file.
33 @details This function should be used for text file with following format:
35 point_1_coord_1 point_1_coord_2 ... point_1_coord_n
36 point_2_coord_1 point_2_coord_2 ... point_2_coord_n
40 @param[in] filename (string): Path to file with data.
42 @return (list) Points where each point represented by list of coordinates.
46 file = open(filename,
'r')
48 sample = [[float(val)
for val
in line.split()]
for line
in file
if len(line.strip()) > 0]
56 @brief Calculates distance matrix for data sample (sequence of points) using specified metric (by default Euclidean distance).
58 @param[in] sample (array_like): Data points that are used for distance calculation.
59 @param[in] metric (distance_metric): Metric that is used for distance calculation between two points.
61 @return (list) Matrix distance between data points.
65 amount_rows = len(sample)
66 return [[metric(sample[i], sample[j])
for j
in range(amount_rows)]
for i
in range(amount_rows)]
71 @brief Returns image as N-dimension (depends on the input image) matrix, where one element of list describes pixel.
73 @param[in] filename (string): Path to image.
75 @return (list) Pixels where each pixel described by list of RGB-values.
79 with Image.open(filename)
as image_source:
80 data = [list(pixel)
for pixel
in image_source.getdata()]
86 @brief Returns image as 1-dimension (gray colored) matrix, where one element of list describes pixel.
87 @details Luma coding is used for transformation and that is calculated directly from gamma-compressed primary intensities as a weighted sum:
89 \f[Y = 0.2989R + 0.587G + 0.114B\f]
91 @param[in] image_rgb_array (list): Image represented by RGB list.
93 @return (list) Image as gray colored matrix, where one element of list describes pixel.
96 colored_image = read_image(file_name);
97 gray_image = rgb2gray(colored_image);
104 image_gray_array = [0.0] * len(image_rgb_array)
105 for index
in range(0, len(image_rgb_array), 1):
106 image_gray_array[index] = float(image_rgb_array[index][0]) * 0.2989 \
107 + float(image_rgb_array[index][1]) * 0.5870 \
108 + float(image_rgb_array[index][2]) * 0.1140
110 return image_gray_array
115 @brief Returns stretched content as 1-dimension (gray colored) matrix with size of input image.
117 @param[in] image_source (Image): PIL Image instance.
119 @return (list, Image) Stretched image as gray colored matrix and source image.
122 wsize, hsize = image_source.size
126 image_source = image_source.crop((ws, hs, we, he))
129 image_source = image_source.resize((wsize, hsize), Image.ANTIALIAS)
132 data = [pixel
for pixel
in image_source.getdata()]
135 return image_pattern, image_source
140 @brief Returns coordinates of gray image content on the input image.
142 @param[in] image (Image): PIL Image instance that is processed.
144 @return (tuple) Returns coordinates of gray image content as (width_start, height_start, width_end, height_end).
148 width, height = image.size
152 height_start = height
156 for pixel
in image.getdata():
157 value = float(pixel[0]) * 0.2989 + float(pixel[1]) * 0.5870 + float(pixel[2]) * 0.1140
166 if width_start > col:
169 if height_start > row:
177 return width_start, height_start, width_end + 1, height_end + 1
182 @brief Returns average distance for establish links between specified number of nearest neighbors.
184 @param[in] points (list): Input data, list of points where each point represented by list.
185 @param[in] num_neigh (uint): Number of neighbors that should be used for distance calculation.
187 @return (double) Average distance for establish links between 'num_neigh' in data set 'points'.
191 if num_neigh > len(points) - 1:
192 raise NameError(
'Impossible to calculate average distance to neighbors '
193 'when number of object is less than number of neighbors.')
195 dist_matrix = [[0.0
for i
in range(len(points))]
for _
in range(len(points))]
196 for i
in range(0, len(points), 1):
197 for j
in range(i + 1, len(points), 1):
199 dist_matrix[i][j] = distance
200 dist_matrix[j][i] = distance
202 dist_matrix[i] = sorted(dist_matrix[i])
205 for i
in range(0, len(points), 1):
207 for j
in range(0, num_neigh, 1):
208 total_distance += dist_matrix[i][j + 1]
210 return total_distance / (num_neigh * len(points))
213 def medoid(data, indexes=None, **kwargs):
215 @brief Calculate medoid for input points.
217 @param[in] data (list): Set of points for that median should be calculated.
218 @param[in] indexes (list): Indexes of input set of points that will be taken into account during median calculation.
219 @param[in] **kwargs: Arbitrary keyword arguments (available arguments: 'metric', 'data_type').
221 <b>Keyword Args:</b><br>
222 - metric (distance_metric): Metric that is used for distance calculation between two points.
223 - data_type (string): Data type of input sample 'data' (available values: 'points', 'distance_matrix').
225 @return (uint) index of point in input set that corresponds to median.
230 distance = float(
'Inf')
232 metric = kwargs.get(
'metric', type_metric.EUCLIDEAN_SQUARE)
233 data_type = kwargs.get(
'data_type',
'points')
235 if data_type ==
'points':
236 calculator =
lambda index1, index2: metric(data[index1], data[index2])
237 elif data_type ==
'distance_matrix':
238 if isinstance(data, numpy.matrix):
239 calculator =
lambda index1, index2: data.item(index1, index2)
242 calculator =
lambda index1, index2: data[index1][index2]
244 raise TypeError(
"Unknown type of data is specified '%s'." % data_type)
247 range_points = range(len(data))
249 range_points = indexes
251 for index_candidate
in range_points:
252 distance_candidate = 0.0
253 for index
in range_points:
254 distance_candidate += calculator(index_candidate, index)
256 if distance_candidate < distance:
257 distance = distance_candidate
258 index_median = index_candidate
265 @brief Calculate Euclidean distance between vector a and b.
266 @details The Euclidean between vectors (points) a and b is calculated by following formula:
269 dist(a, b) = \sqrt{ \sum_{i=0}^{N}(b_{i} - a_{i})^{2}) };
272 Where N is a length of each vector.
274 @param[in] a (list): The first vector.
275 @param[in] b (list): The second vector.
277 @return (double) Euclidian distance between two vectors.
279 @note This function for calculation is faster then standard function in ~100 times!
284 return distance**(0.5);
289 @brief Calculate square Euclidian distance between vector a and b.
291 @param[in] a (list): The first vector.
292 @param[in] b (list): The second vector.
294 @return (double) Square Euclidian distance between two vectors.
298 if ( ((type(a) == float)
and (type(b) == float))
or ((type(a) == int)
and (type(b) == int)) ):
302 for i
in range(0, len(a)):
303 distance += (a[i] - b[i])**2.0;
310 @brief Calculate Manhattan distance between vector a and b.
312 @param[in] a (list): The first cluster.
313 @param[in] b (list): The second cluster.
315 @return (double) Manhattan distance between two vectors.
319 if ( ((type(a) == float)
and (type(b) == float))
or ((type(a) == int)
and (type(b) == int)) ):
325 for i
in range(0, dimension):
326 distance += abs(a[i] - b[i]);
333 @brief Calculates average inter-cluster distance between two clusters.
334 @details Clusters can be represented by list of coordinates (in this case data shouldn't be specified),
335 or by list of indexes of points from the data (represented by list of points), in this case
336 data should be specified.
338 @param[in] cluster1 (list): The first cluster where each element can represent index from the data or object itself.
339 @param[in] cluster2 (list): The second cluster where each element can represent index from the data or object itself.
340 @param[in] data (list): If specified than elements of clusters will be used as indexes,
341 otherwise elements of cluster will be considered as points.
343 @return (double) Average inter-cluster distance between two clusters.
350 for i
in range(len(cluster1)):
351 for j
in range(len(cluster2)):
354 for i
in range(len(cluster1)):
355 for j
in range(len(cluster2)):
358 distance /= float(len(cluster1) * len(cluster2));
359 return distance ** 0.5;
364 @brief Calculates average intra-cluster distance between two clusters.
365 @details Clusters can be represented by list of coordinates (in this case data shouldn't be specified),
366 or by list of indexes of points from the data (represented by list of points), in this case
367 data should be specified.
369 @param[in] cluster1 (list): The first cluster.
370 @param[in] cluster2 (list): The second cluster.
371 @param[in] data (list): If specified than elements of clusters will be used as indexes,
372 otherwise elements of cluster will be considered as points.
374 @return (double) Average intra-cluster distance between two clusters.
380 for i
in range(len(cluster1) + len(cluster2)):
381 for j
in range(len(cluster1) + len(cluster2)):
384 if i < len(cluster1):
385 first_point = cluster1[i]
387 first_point = cluster2[i - len(cluster1)]
390 if j < len(cluster1):
391 second_point = cluster1[j]
393 second_point = cluster2[j - len(cluster1)]
397 if i < len(cluster1):
398 first_point = data[cluster1[i]]
400 first_point = data[cluster2[i - len(cluster1)]]
402 if j < len(cluster1):
403 second_point = data[cluster1[j]]
405 second_point = data[cluster2[j - len(cluster1)]]
409 distance /= float((len(cluster1) + len(cluster2)) * (len(cluster1) + len(cluster2) - 1.0))
410 return distance ** 0.5
415 @brief Calculates variance increase distance between two clusters.
416 @details Clusters can be represented by list of coordinates (in this case data shouldn't be specified),
417 or by list of indexes of points from the data (represented by list of points), in this case
418 data should be specified.
420 @param[in] cluster1 (list): The first cluster.
421 @param[in] cluster2 (list): The second cluster.
422 @param[in] data (list): If specified than elements of clusters will be used as indexes,
423 otherwise elements of cluster will be considered as points.
425 @return (double) Average variance increase distance between two clusters.
431 member_cluster1 = [0.0] * len(cluster1[0])
432 member_cluster2 = [0.0] * len(cluster2[0])
435 member_cluster1 = [0.0] * len(data[0])
436 member_cluster2 = [0.0] * len(data[0])
438 for i
in range(len(cluster1)):
444 for j
in range(len(cluster2)):
457 distance_general = 0.0
458 distance_cluster1 = 0.0
459 distance_cluster2 = 0.0
461 for i
in range(len(cluster1)):
470 for j
in range(len(cluster2)):
479 return distance_general - distance_cluster1 - distance_cluster2
484 @brief Calculates description of ellipse using covariance matrix.
486 @param[in] covariance (numpy.array): Covariance matrix for which ellipse area should be calculated.
487 @param[in] scale (float): Scale of the ellipse.
489 @return (float, float, float) Return ellipse description: angle, width, height.
493 eigh_values, eigh_vectors = numpy.linalg.eigh(covariance)
494 order = eigh_values.argsort()[::-1]
496 values, vectors = eigh_values[order], eigh_vectors[order]
497 angle = numpy.degrees(numpy.arctan2(*vectors[:,0][::-1]))
502 width, height = 2.0 * scale * numpy.sqrt(values)
503 return angle, width, height
508 @brief Finds maximum and minimum corner in each dimension of the specified data.
510 @param[in] data (list): List of points that should be analysed.
511 @param[in] data_filter (list): List of indexes of the data that should be analysed,
512 if it is 'None' then whole 'data' is analysed to obtain corners.
514 @return (list) Tuple of two points that corresponds to minimum and maximum corner (min_corner, max_corner).
518 dimensions = len(data[0])
522 bypass = range(len(data))
524 maximum_corner = list(data[bypass[0]][:])
525 minimum_corner = list(data[bypass[0]][:])
527 for index_point
in bypass:
528 for index_dimension
in range(dimensions):
529 if data[index_point][index_dimension] > maximum_corner[index_dimension]:
530 maximum_corner[index_dimension] = data[index_point][index_dimension]
532 if data[index_point][index_dimension] < minimum_corner[index_dimension]:
533 minimum_corner[index_dimension] = data[index_point][index_dimension]
535 return minimum_corner, maximum_corner
540 @brief Calculates norm of an input vector that is known as a vector length.
542 @param[in] vector (list): The input vector whose length is calculated.
544 @return (double) vector norm known as vector length.
549 for component
in vector:
550 length += component * component
552 length = length ** 0.5
559 @brief Calculates Heaviside function that represents step function.
560 @details If input value is greater than 0 then returns 1, otherwise returns 0.
562 @param[in] value (double): Argument of Heaviside function.
564 @return (double) Value of Heaviside function.
575 @brief Executes specified method or function with measuring of execution time.
577 @param[in] executable_function (pointer): Pointer to a function or method that should be called.
578 @param[in] *args: Arguments of the called function or method.
579 @param[in] **kwargs: Arbitrary keyword arguments of the called function or method.
581 @return (tuple) Execution time and result of execution of function or method (execution_time, result_execution).
585 time_start = time.perf_counter()
586 result = executable_function(*args, **kwargs)
587 time_end = time.perf_counter()
589 return time_end - time_start, result
594 @brief Extracts number of oscillations of specified oscillator.
596 @param[in] osc_dyn (list): Dynamic of oscillators.
597 @param[in] index (uint): Index of oscillator in dynamic.
598 @param[in] amplitude_threshold (double): Amplitude threshold when oscillation is taken into account, for example,
599 when oscillator amplitude is greater than threshold then oscillation is incremented.
601 @return (uint) Number of oscillations of specified oscillator.
605 number_oscillations = 0;
606 waiting_differential =
False;
607 threshold_passed =
False;
608 high_level_trigger =
True if (osc_dyn[0][index] > amplitude_threshold)
else False;
610 for values
in osc_dyn:
611 if ( (values[index] >= amplitude_threshold)
and (high_level_trigger
is False) ):
612 high_level_trigger =
True;
613 threshold_passed =
True;
615 elif ( (values[index] < amplitude_threshold)
and (high_level_trigger
is True) ):
616 high_level_trigger =
False;
617 threshold_passed =
True;
619 if (threshold_passed
is True):
620 threshold_passed =
False;
621 if (waiting_differential
is True and high_level_trigger
is False):
622 number_oscillations += 1;
623 waiting_differential =
False;
626 waiting_differential =
True;
628 return number_oscillations;
633 @brief Allocate clusters in line with ensembles of synchronous oscillators where each
634 synchronous ensemble corresponds to only one cluster.
636 @param[in] dynamic (dynamic): Dynamic of each oscillator.
637 @param[in] tolerance (double): Maximum error for allocation of synchronous ensemble oscillators.
638 @param[in] threshold (double): Amlitude trigger when spike is taken into account.
639 @param[in] ignore (bool): Set of indexes that shouldn't be taken into account.
641 @return (list) Grours (lists) of indexes of synchronous oscillators, for example,
642 [ [index_osc1, index_osc3], [index_osc2], [index_osc4, index_osc5] ].
646 descriptors = [ []
for _
in range(len(dynamic[0])) ];
649 for index_dyn
in range(0, len(dynamic[0]), 1):
650 if ((ignore
is not None)
and (index_dyn
in ignore)):
653 time_stop_simulation = len(dynamic) - 1;
654 active_state =
False;
656 if (dynamic[time_stop_simulation][index_dyn] > threshold):
660 if (active_state
is True):
661 while ( (dynamic[time_stop_simulation][index_dyn] > threshold)
and (time_stop_simulation > 0) ):
662 time_stop_simulation -= 1;
665 if (time_stop_simulation == 0):
669 active_state =
False;
672 for t
in range(time_stop_simulation, 0, -1):
673 if ( (dynamic[t][index_dyn] > threshold)
and (active_state
is False) ):
676 elif ( (dynamic[t][index_dyn] < threshold)
and (active_state
is True) ):
678 active_state =
False;
682 if (desc == [0, 0, 0]):
685 desc[2] = desc[1] + (desc[0] - desc[1]) / 2.0;
686 descriptors[index_dyn] = desc;
691 desc_sync_ensembles = [];
693 for index_desc
in range(0, len(descriptors), 1):
694 if (descriptors[index_desc] == []):
697 if (len(sync_ensembles) == 0):
698 desc_ensemble = descriptors[index_desc];
699 reducer = (desc_ensemble[0] - desc_ensemble[1]) * tolerance;
701 desc_ensemble[0] = desc_ensemble[2] + reducer;
702 desc_ensemble[1] = desc_ensemble[2] - reducer;
704 desc_sync_ensembles.append(desc_ensemble);
705 sync_ensembles.append([ index_desc ]);
707 oscillator_captured =
False;
708 for index_ensemble
in range(0, len(sync_ensembles), 1):
709 if ( (desc_sync_ensembles[index_ensemble][0] > descriptors[index_desc][2])
and (desc_sync_ensembles[index_ensemble][1] < descriptors[index_desc][2])):
710 sync_ensembles[index_ensemble].append(index_desc);
711 oscillator_captured =
True;
714 if (oscillator_captured
is False):
715 desc_ensemble = descriptors[index_desc];
716 reducer = (desc_ensemble[0] - desc_ensemble[1]) * tolerance;
718 desc_ensemble[0] = desc_ensemble[2] + reducer;
719 desc_ensemble[1] = desc_ensemble[2] - reducer;
721 desc_sync_ensembles.append(desc_ensemble);
722 sync_ensembles.append([ index_desc ]);
724 return sync_ensembles;
727 def draw_clusters(data, clusters, noise = [], marker_descr = '.', hide_axes = False, axes = None, display_result = True):
729 @brief Displays clusters for data in 2D or 3D.
731 @param[in] data (list): Points that are described by coordinates represented.
732 @param[in] clusters (list): Clusters that are represented by lists of indexes where each index corresponds to point in data.
733 @param[in] noise (list): Points that are regarded to noise.
734 @param[in] marker_descr (string): Marker for displaying points.
735 @param[in] hide_axes (bool): If True - axes is not displayed.
736 @param[in] axes (ax) Matplotlib axes where clusters should be drawn, if it is not specified (None) then new plot will be created.
737 @param[in] display_result (bool): If specified then matplotlib axes will be used for drawing and plot will not be shown.
739 @return (ax) Matplotlib axes where drawn clusters are presented.
744 if ( (data
is not None)
and (clusters
is not None) ):
745 dimension = len(data[0]);
746 elif ( (data
is None)
and (clusters
is not None) ):
747 dimension = len(clusters[0][0]);
749 raise NameError(
'Data or clusters should be specified exactly.');
752 colors = [
'red',
'blue',
'darkgreen',
'brown',
'violet',
753 'deepskyblue',
'darkgrey',
'lightsalmon',
'deeppink',
'yellow',
754 'black',
'mediumspringgreen',
'orange',
'darkviolet',
'darkblue',
755 'silver',
'lime',
'pink',
'gold',
'bisque' ];
757 if (len(clusters) > len(colors)):
758 raise NameError(
'Impossible to represent clusters due to number of specified colors.');
764 if ((dimension) == 1
or (dimension == 2)):
765 axes = fig.add_subplot(111);
766 elif (dimension == 3):
767 axes = fig.gca(projection=
'3d');
769 raise NameError(
'Drawer supports only 2d and 3d data representation');
772 for cluster
in clusters:
773 color = colors[color_index];
777 axes.plot(item[0], 0.0, color = color, marker = marker_descr);
779 axes.plot(data[item][0], 0.0, color = color, marker = marker_descr);
783 axes.plot(item[0], item[1], color = color, marker = marker_descr);
785 axes.plot(data[item][0], data[item][1], color = color, marker = marker_descr);
787 elif (dimension == 3):
789 axes.scatter(item[0], item[1], item[2], c = color, marker = marker_descr);
791 axes.scatter(data[item][0], data[item][1], data[item][2], c = color, marker = marker_descr);
798 axes.plot(item[0], 0.0,
'w' + marker_descr);
800 axes.plot(data[item][0], 0.0,
'w' + marker_descr);
804 axes.plot(item[0], item[1],
'w' + marker_descr);
806 axes.plot(data[item][0], data[item][1],
'w' + marker_descr);
808 elif (dimension == 3):
810 axes.scatter(item[0], item[1], item[2], c =
'w', marker = marker_descr);
812 axes.scatter(data[item][0], data[item][1], data[item][2], c =
'w', marker = marker_descr);
816 if (hide_axes
is True):
817 axes.xaxis.set_ticklabels([]);
818 axes.yaxis.set_ticklabels([]);
821 axes.zaxis.set_ticklabels([]);
823 if (display_result
is True):
829 def draw_dynamics(t, dyn, x_title = None, y_title = None, x_lim = None, y_lim = None, x_labels = True, y_labels = True, separate = False, axes = None):
831 @brief Draw dynamics of neurons (oscillators) in the network.
832 @details It draws if matplotlib is not specified (None), othewise it should be performed manually.
834 @param[in] t (list): Values of time (used by x axis).
835 @param[in] dyn (list): Values of output of oscillators (used by y axis).
836 @param[in] x_title (string): Title for Y.
837 @param[in] y_title (string): Title for X.
838 @param[in] x_lim (double): X limit.
839 @param[in] y_lim (double): Y limit.
840 @param[in] x_labels (bool): If True - shows X labels.
841 @param[in] y_labels (bool): If True - shows Y labels.
842 @param[in] separate (list): Consists of lists of oscillators where each such list consists of oscillator indexes that will be shown on separated stage.
843 @param[in] axes (ax): If specified then matplotlib axes will be used for drawing and plot will not be shown.
845 @return (ax) Axes of matplotlib.
852 if (x_lim
is not None):
855 stage_xlim = [0, t[len(t) - 1]];
857 if ( (isinstance(separate, bool)
is True)
and (separate
is True) ):
858 if (isinstance(dyn[0], list)
is True):
859 number_lines = len(dyn[0]);
863 elif (isinstance(separate, list)
is True):
864 number_lines = len(separate);
869 dysplay_result =
False;
871 dysplay_result =
True;
872 (fig, axes) = plt.subplots(number_lines, 1);
875 if (isinstance(dyn[0], list)
is True):
876 num_items = len(dyn[0]);
877 for index
in range(0, num_items, 1):
878 y = [item[index]
for item
in dyn];
880 if (number_lines > 1):
884 if (isinstance(separate, bool)
is True):
887 elif (isinstance(separate, list)
is True):
888 for index_group
in range(0, len(separate), 1):
889 if (index
in separate[index_group]):
890 index_stage = index_group;
893 if (index_stage != -1):
894 if (index_stage != number_lines - 1):
895 axes[index_stage].get_xaxis().set_visible(
False);
897 axes[index_stage].plot(t, y,
'b-', linewidth = 0.5);
898 set_ax_param(axes[index_stage], x_title, y_title, stage_xlim, y_lim, x_labels, y_labels,
True);
901 axes.plot(t, y,
'b-', linewidth = 0.5);
902 set_ax_param(axes, x_title, y_title, stage_xlim, y_lim, x_labels, y_labels,
True);
904 axes.plot(t, dyn,
'b-', linewidth = 0.5);
905 set_ax_param(axes, x_title, y_title, stage_xlim, y_lim, x_labels, y_labels,
True);
907 if (dysplay_result
is True):
913 def set_ax_param(ax, x_title = None, y_title = None, x_lim = None, y_lim = None, x_labels = True, y_labels = True, grid = True):
915 @brief Sets parameters for matplotlib ax.
917 @param[in] ax (Axes): Axes for which parameters should applied.
918 @param[in] x_title (string): Title for Y.
919 @param[in] y_title (string): Title for X.
920 @param[in] x_lim (double): X limit.
921 @param[in] y_lim (double): Y limit.
922 @param[in] x_labels (bool): If True - shows X labels.
923 @param[in] y_labels (bool): If True - shows Y labels.
924 @param[in] grid (bool): If True - shows grid.
927 from matplotlib.font_manager
import FontProperties;
928 from matplotlib
import rcParams;
930 if (_platform ==
"linux")
or (_platform ==
"linux2"):
931 rcParams[
'font.sans-serif'] = [
'Liberation Serif'];
933 rcParams[
'font.sans-serif'] = [
'Arial'];
935 rcParams[
'font.size'] = 12;
937 surface_font = FontProperties();
938 if (_platform ==
"linux")
or (_platform ==
"linux2"):
939 surface_font.set_name(
'Liberation Serif');
941 surface_font.set_name(
'Arial');
943 surface_font.set_size(
'12');
945 if (y_title
is not None): ax.set_ylabel(y_title, fontproperties = surface_font);
946 if (x_title
is not None): ax.set_xlabel(x_title, fontproperties = surface_font);
948 if (x_lim
is not None): ax.set_xlim(x_lim[0], x_lim[1]);
949 if (y_lim
is not None): ax.set_ylim(y_lim[0], y_lim[1]);
951 if (x_labels
is False): ax.xaxis.set_ticklabels([]);
952 if (y_labels
is False): ax.yaxis.set_ticklabels([]);
957 def draw_dynamics_set(dynamics, xtitle = None, ytitle = None, xlim = None, ylim = None, xlabels = False, ylabels = False):
959 @brief Draw lists of dynamics of neurons (oscillators) in the network.
961 @param[in] dynamics (list): List of network outputs that are represented by values of output of oscillators (used by y axis).
962 @param[in] xtitle (string): Title for Y.
963 @param[in] ytitle (string): Title for X.
964 @param[in] xlim (double): X limit.
965 @param[in] ylim (double): Y limit.
966 @param[in] xlabels (bool): If True - shows X labels.
967 @param[in] ylabels (bool): If True - shows Y labels.
971 number_dynamics = len(dynamics);
972 if (number_dynamics == 1):
973 draw_dynamics(dynamics[0][0], dynamics[0][1], xtitle, ytitle, xlim, ylim, xlabels, ylabels);
976 number_cols = int(numpy.ceil(number_dynamics ** 0.5));
977 number_rows = int(numpy.ceil(number_dynamics / number_cols));
980 double_indexer =
True;
981 if ( (number_cols == 1)
or (number_rows == 1) ):
983 double_indexer =
False;
985 (_, axarr) = plt.subplots(number_rows, number_cols);
988 for dynamic
in dynamics:
989 axarr[real_index] =
draw_dynamics(dynamic[0], dynamic[1], xtitle, ytitle, xlim, ylim, xlabels, ylabels, axes = axarr[real_index]);
992 if (double_indexer
is True):
993 real_index = real_index[0], real_index[1] + 1;
994 if (real_index[1] >= number_cols):
995 real_index = real_index[0] + 1, 0;
1004 @brief Shows image segments using colored image.
1005 @details Each color on result image represents allocated segment. The first image is initial and other is result of segmentation.
1007 @param[in] source (string): Path to image.
1008 @param[in] clusters (list): List of clusters (allocated segments of image) where each cluster
1009 consists of indexes of pixel from source image.
1010 @param[in] hide_axes (bool): If True then axes will not be displayed.
1014 image_source = Image.open(source);
1015 image_size = image_source.size;
1017 (fig, axarr) = plt.subplots(1, 2);
1019 plt.setp([ax
for ax
in axarr], visible =
False);
1021 available_colors = [ (0, 162, 232), (34, 177, 76), (237, 28, 36),
1022 (255, 242, 0), (0, 0, 0), (237, 28, 36),
1023 (255, 174, 201), (127, 127, 127), (185, 122, 87),
1024 (200, 191, 231), (136, 0, 21), (255, 127, 39),
1025 (63, 72, 204), (195, 195, 195), (255, 201, 14),
1026 (239, 228, 176), (181, 230, 29), (153, 217, 234),
1029 image_color_segments = [(255, 255, 255)] * (image_size[0] * image_size[1]);
1031 for index_segment
in range(len(clusters)):
1032 for index_pixel
in clusters[index_segment]:
1033 image_color_segments[index_pixel] = available_colors[index_segment];
1035 stage = array(image_color_segments, numpy.uint8);
1036 stage = numpy.reshape(stage, (image_size[1], image_size[0]) + ((3),));
1037 image_cluster = Image.fromarray(stage,
'RGB');
1039 axarr[0].imshow(image_source, interpolation =
'none');
1040 axarr[1].imshow(image_cluster, interpolation =
'none');
1043 plt.setp(axarr[i], visible =
True);
1045 if (hide_axes
is True):
1046 axarr[i].xaxis.set_ticklabels([]);
1047 axarr[i].yaxis.set_ticklabels([]);
1048 axarr[i].xaxis.set_ticks_position(
'none');
1049 axarr[i].yaxis.set_ticks_position(
'none');
1056 @brief Shows image segments using black masks.
1057 @details Each black mask of allocated segment is presented on separate plot.
1058 The first image is initial and others are black masks of segments.
1060 @param[in] source (string): Path to image.
1061 @param[in] clusters (list): List of clusters (allocated segments of image) where each cluster
1062 consists of indexes of pixel from source image.
1063 @param[in] hide_axes (bool): If True then axes will not be displayed.
1066 if len(clusters) == 0:
1067 print(
"Warning: Nothing to draw - list of clusters is empty.")
1070 image_source = Image.open(source)
1071 image_size = image_source.size
1074 number_clusters = len(clusters) + 1
1076 number_cols = int(numpy.ceil(number_clusters ** 0.5))
1077 number_rows = int(numpy.ceil(number_clusters / number_cols))
1080 double_indexer =
True
1081 if (number_cols == 1)
or (number_rows == 1):
1083 double_indexer =
False
1085 (fig, axarr) = plt.subplots(number_rows, number_cols)
1086 plt.setp([ax
for ax
in axarr], visible=
False)
1088 axarr[real_index].imshow(image_source, interpolation=
'none')
1089 plt.setp(axarr[real_index], visible=
True)
1091 if hide_axes
is True:
1092 axarr[real_index].xaxis.set_ticklabels([])
1093 axarr[real_index].yaxis.set_ticklabels([])
1094 axarr[real_index].xaxis.set_ticks_position(
'none')
1095 axarr[real_index].yaxis.set_ticks_position(
'none')
1097 if double_indexer
is True:
1102 for cluster
in clusters:
1103 stage_cluster = [(255, 255, 255)] * (image_size[0] * image_size[1])
1104 for index
in cluster:
1105 stage_cluster[index] = (0, 0, 0)
1107 stage = array(stage_cluster, numpy.uint8)
1108 stage = numpy.reshape(stage, (image_size[1], image_size[0]) + ((3),))
1110 image_cluster = Image.fromarray(stage,
'RGB')
1112 axarr[real_index].imshow(image_cluster, interpolation =
'none')
1113 plt.setp(axarr[real_index], visible =
True)
1115 if hide_axes
is True:
1116 axarr[real_index].xaxis.set_ticklabels([])
1117 axarr[real_index].yaxis.set_ticklabels([])
1119 axarr[real_index].xaxis.set_ticks_position(
'none')
1120 axarr[real_index].yaxis.set_ticks_position(
'none')
1122 if double_indexer
is True:
1123 real_index = real_index[0], real_index[1] + 1
1124 if real_index[1] >= number_cols:
1125 real_index = real_index[0] + 1, 0
1134 @brief Returns the element's index at the left side from the right border with the same value as the
1135 last element in the range `sorted_data`.
1137 @details The element at the right is considered as target to search. `sorted_data` must
1138 be sorted collection. The complexity of the algorithm is `O(log(n))`. The
1139 algorithm is based on the binary search algorithm.
1141 @param[in] sorted_data: input data to find the element.
1142 @param[in] right: the index of the right element from that search is started.
1143 @param[in] comparator: comparison function object which returns `True` if the first argument
1144 is less than the second.
1146 @return The element's index at the left side from the right border with the same value as the
1147 last element in the range `sorted_data`.
1150 if len(sorted_data) == 0:
1151 raise ValueError(
"Input data is empty.")
1154 middle = (right - left) // 2
1155 target = sorted_data[right]
1158 if comparator(sorted_data[middle], target):
1163 offset = (right - left) // 2
1164 middle = left + offset
1171 @brief Calculates linear sum of vector that is represented by list, each element can be represented by list - multidimensional elements.
1173 @param[in] list_vector (list): Input vector.
1175 @return (list|double) Linear sum of vector that can be represented by list in case of multidimensional elements.
1180 list_representation = (type(list_vector[0]) == list)
1182 if list_representation
is True:
1183 dimension = len(list_vector[0])
1184 linear_sum = [0] * dimension
1186 for index_element
in range(0, len(list_vector)):
1187 if list_representation
is True:
1188 for index_dimension
in range(0, dimension):
1189 linear_sum[index_dimension] += list_vector[index_element][index_dimension]
1191 linear_sum += list_vector[index_element]
1198 @brief Calculates square sum of vector that is represented by list, each element can be represented by list - multidimensional elements.
1200 @param[in] list_vector (list): Input vector.
1202 @return (double) Square sum of vector.
1207 list_representation = (type(list_vector[0]) == list)
1209 for index_element
in range(0, len(list_vector)):
1210 if list_representation
is True:
1213 square_sum += list_vector[index_element] * list_vector[index_element]
1220 @brief Calculates subtraction of two lists.
1221 @details Each element from list 'a' is subtracted by element from list 'b' accordingly.
1223 @param[in] a (list): List of elements that supports mathematical subtraction.
1224 @param[in] b (list): List of elements that supports mathematical subtraction.
1226 @return (list) Results of subtraction of two lists.
1229 return [a[i] - b[i]
for i
in range(len(a))]
1234 @brief Calculates subtraction between list and number.
1235 @details Each element from list 'a' is subtracted by number 'b'.
1237 @param[in] a (list): List of elements that supports mathematical subtraction.
1238 @param[in] b (list): Value that supports mathematical subtraction.
1240 @return (list) Results of subtraction between list and number.
1243 return [a[i] - b
for i
in range(len(a))]
1248 @brief Addition of two lists.
1249 @details Each element from list 'a' is added to element from list 'b' accordingly.
1251 @param[in] a (list): List of elements that supports mathematic addition..
1252 @param[in] b (list): List of elements that supports mathematic addition..
1254 @return (list) Results of addtion of two lists.
1257 return [a[i] + b[i]
for i
in range(len(a))]
1262 @brief Addition between list and number.
1263 @details Each element from list 'a' is added to number 'b'.
1265 @param[in] a (list): List of elements that supports mathematic addition.
1266 @param[in] b (double): Value that supports mathematic addition.
1268 @return (list) Result of addtion of two lists.
1271 return [a[i] + b
for i
in range(len(a))]
1276 @brief Division between list and number.
1277 @details Each element from list 'a' is divided by number 'b'.
1279 @param[in] a (list): List of elements that supports mathematic division.
1280 @param[in] b (double): Value that supports mathematic division.
1282 @return (list) Result of division between list and number.
1285 return [a[i] / b
for i
in range(len(a))]
1290 @brief Division of two lists.
1291 @details Each element from list 'a' is divided by element from list 'b' accordingly.
1293 @param[in] a (list): List of elements that supports mathematic division.
1294 @param[in] b (list): List of elements that supports mathematic division.
1296 @return (list) Result of division of two lists.
1299 return [a[i] / b[i]
for i
in range(len(a))]
1304 @brief Multiplication between list and number.
1305 @details Each element from list 'a' is multiplied by number 'b'.
1307 @param[in] a (list): List of elements that supports mathematic division.
1308 @param[in] b (double): Number that supports mathematic division.
1310 @return (list) Result of division between list and number.
1313 return [a[i] * b
for i
in range(len(a))]
1318 @brief Multiplication of two lists.
1319 @details Each element from list 'a' is multiplied by element from list 'b' accordingly.
1321 @param[in] a (list): List of elements that supports mathematic multiplication.
1322 @param[in] b (list): List of elements that supports mathematic multiplication.
1324 @return (list) Result of multiplication of elements in two lists.
1327 return [a[i] * b[i]
for i
in range(len(a))]