3 @brief Utils that are used by modules of pyclustering. 5 @authors Andrei Novikov (pyclustering@yandex.ru) 7 @copyright GNU Public License 9 @cond GNU_PUBLIC_LICENSE 10 PyClustering is free software: you can redistribute it and/or modify 11 it under the terms of the GNU General Public License as published by 12 the Free Software Foundation, either version 3 of the License, or 13 (at your option) any later version. 15 PyClustering is distributed in the hope that it will be useful, 16 but WITHOUT ANY WARRANTY; without even the implied warranty of 17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 GNU General Public License for more details. 20 You should have received a copy of the GNU General Public License 21 along with this program. If not, see <http://www.gnu.org/licenses/>. 30 from numpy
import array
34 except Exception
as error_instance:
35 warnings.warn(
"Impossible to import PIL (please, install 'PIL'), pyclustering's visualization " 36 "functionality is partially not available (details: '%s')." % str(error_instance))
39 import matplotlib.pyplot
as plt
40 from mpl_toolkits.mplot3d
import Axes3D
41 except Exception
as error_instance:
42 warnings.warn(
"Impossible to import matplotlib (please, install 'matplotlib'), pyclustering's visualization " 43 "functionality is not available (details: '%s')." % str(error_instance))
45 from sys
import platform
as _platform
56 @brief Returns data sample from simple text file. 57 @details This function should be used for text file with following format: 59 point_1_coord_1 point_1_coord_2 ... point_1_coord_n 60 point_2_coord_1 point_2_coord_2 ... point_2_coord_n 64 @param[in] filename (string): Path to file with data. 66 @return (list) Points where each point represented by list of coordinates. 70 file = open(filename,
'r') 72 sample = [[float(val) for val
in line.split()]
for line
in file
if len(line.strip()) > 0]
80 @brief Calculates distance matrix for data sample (sequence of points) using Euclidean distance as a metric. 82 @param[in] sample (array_like): Data points that are used for distance calculation. 84 @return (list) Matrix distance between data points. 88 amount_rows = len(sample);
89 return [ [
euclidean_distance(sample[i], sample[j])
for j
in range(amount_rows) ]
for i
in range(amount_rows) ];
94 @brief Returns image as N-dimension (depends on the input image) matrix, where one element of list describes pixel. 96 @param[in] filename (string): Path to image. 98 @return (list) Pixels where each pixel described by list of RGB-values. 102 with Image.open(filename)
as image_source:
103 data = [list(pixel)
for pixel
in image_source.getdata()]
109 @brief Returns image as 1-dimension (gray colored) matrix, where one element of list describes pixel. 110 @details Luma coding is used for transformation and that is calculated directly from gamma-compressed primary intensities as a weighted sum: 112 \f[Y = 0.2989R + 0.587G + 0.114B\f] 114 @param[in] image_rgb_array (list): Image represented by RGB list. 116 @return (list) Image as gray colored matrix, where one element of list describes pixel. 119 colored_image = read_image(file_name); 120 gray_image = rgb2gray(colored_image); 127 image_gray_array = [0.0] * len(image_rgb_array);
128 for index
in range(0, len(image_rgb_array), 1):
129 image_gray_array[index] = float(image_rgb_array[index][0]) * 0.2989 + float(image_rgb_array[index][1]) * 0.5870 + float(image_rgb_array[index][2]) * 0.1140;
131 return image_gray_array;
136 @brief Returns stretched content as 1-dimension (gray colored) matrix with size of input image. 138 @param[in] image_source (Image): PIL Image instance. 140 @return (list, Image) Stretched image as gray colored matrix and source image. 143 wsize, hsize = image_source.size;
147 image_source = image_source.crop((ws, hs, we, he));
150 image_source = image_source.resize((wsize, hsize), Image.ANTIALIAS);
153 data = [pixel
for pixel
in image_source.getdata()];
156 return (image_pattern, image_source);
161 @brief Returns coordinates of gray image content on the input image. 163 @param[in] image (Image): PIL Image instance that is processed. 165 @return (tuple) Returns coordinates of gray image content as (width_start, height_start, width_end, height_end). 169 width, height = image.size;
173 height_start = height;
177 for pixel
in image.getdata():
178 value = float(pixel[0]) * 0.2989 + float(pixel[1]) * 0.5870 + float(pixel[2]) * 0.1140;
181 if (width_end < col):
184 if (height_end < row):
187 if (width_start > col):
190 if (height_start > row):
198 return (width_start, height_start, width_end + 1, height_end + 1);
203 @brief Returns average distance for establish links between specified number of nearest neighbors. 205 @param[in] points (list): Input data, list of points where each point represented by list. 206 @param[in] num_neigh (uint): Number of neighbors that should be used for distance calculation. 208 @return (double) Average distance for establish links between 'num_neigh' in data set 'points'. 212 if num_neigh > len(points) - 1:
213 raise NameError(
'Impossible to calculate average distance to neighbors when number of object is less than number of neighbors.');
215 dist_matrix = [ [ 0.0
for i
in range(len(points)) ]
for j
in range(len(points)) ];
216 for i
in range(0, len(points), 1):
217 for j
in range(i + 1, len(points), 1):
219 dist_matrix[i][j] = distance;
220 dist_matrix[j][i] = distance;
222 dist_matrix[i] = sorted(dist_matrix[i]);
225 for i
in range(0, len(points), 1):
227 for j
in range(0, num_neigh, 1):
228 total_distance += dist_matrix[i][j + 1];
230 return ( total_distance / (num_neigh * len(points)) );
233 def medoid(data, indexes=None, **kwargs):
235 @brief Calculate medoid for input points using Euclidean distance. 237 @param[in] data (list): Set of points for that median should be calculated. 238 @param[in] indexes (list): Indexes of input set of points that will be taken into account during median calculation. 239 @param[in] **kwargs: Arbitrary keyword arguments (available arguments: 'metric', 'data_type'). 241 <b>Keyword Args:</b><br> 242 - metric (distance_metric): Metric that is used for distance calculation between two points. 243 - data_type (string): Data type of input sample 'data' (available values: 'points', 'distance_matrix'). 245 @return (uint) index of point in input set that corresponds to median. 250 distance = float(
'Inf')
252 metric = kwargs.get(
'metric', type_metric.EUCLIDEAN_SQUARE)
253 data_type = kwargs.get(
'data_type',
'points')
255 if data_type ==
'points':
256 calculator =
lambda index1, index2: metric(data[index1], data[index2])
257 elif data_type ==
'distance_matrix':
258 if isinstance(data, numpy.matrix):
259 calculator =
lambda index1, index2: data.item(index1, index2)
262 calculator =
lambda index1, index2: data[index1][index2]
264 raise TypeError(
"Unknown type of data is specified '%s'." % data_type)
267 range_points = range(len(data))
269 range_points = indexes
271 for index_candidate
in range_points:
272 distance_candidate = 0.0
273 for index
in range_points:
274 distance_candidate += calculator(index_candidate, index)
276 if distance_candidate < distance:
277 distance = distance_candidate
278 index_median = index_candidate
285 @brief Calculate Euclidean distance between vector a and b. 286 @details The Euclidean between vectors (points) a and b is calculated by following formula: 289 dist(a, b) = \sqrt{ \sum_{i=0}^{N}(b_{i} - a_{i})^{2}) }; 292 Where N is a length of each vector. 294 @param[in] a (list): The first vector. 295 @param[in] b (list): The second vector. 297 @return (double) Euclidian distance between two vectors. 299 @note This function for calculation is faster then standard function in ~100 times! 304 return distance**(0.5);
309 @brief Calculate square Euclidian distance between vector a and b. 311 @param[in] a (list): The first vector. 312 @param[in] b (list): The second vector. 314 @return (double) Square Euclidian distance between two vectors. 318 if ( ((type(a) == float)
and (type(b) == float))
or ((type(a) == int)
and (type(b) == int)) ):
322 for i
in range(0, len(a)):
323 distance += (a[i] - b[i])**2.0;
330 @brief Calculate Manhattan distance between vector a and b. 332 @param[in] a (list): The first cluster. 333 @param[in] b (list): The second cluster. 335 @return (double) Manhattan distance between two vectors. 339 if ( ((type(a) == float)
and (type(b) == float))
or ((type(a) == int)
and (type(b) == int)) ):
345 for i
in range(0, dimension):
346 distance += abs(a[i] - b[i]);
353 @brief Calculates average inter-cluster distance between two clusters. 354 @details Clusters can be represented by list of coordinates (in this case data shouldn't be specified), 355 or by list of indexes of points from the data (represented by list of points), in this case 356 data should be specified. 358 @param[in] cluster1 (list): The first cluster where each element can represent index from the data or object itself. 359 @param[in] cluster2 (list): The second cluster where each element can represent index from the data or object itself. 360 @param[in] data (list): If specified than elements of clusters will be used as indexes, 361 otherwise elements of cluster will be considered as points. 363 @return (double) Average inter-cluster distance between two clusters. 370 for i
in range(len(cluster1)):
371 for j
in range(len(cluster2)):
374 for i
in range(len(cluster1)):
375 for j
in range(len(cluster2)):
378 distance /= float(len(cluster1) * len(cluster2));
379 return distance ** 0.5;
384 @brief Calculates average intra-cluster distance between two clusters. 385 @details Clusters can be represented by list of coordinates (in this case data shouldn't be specified), 386 or by list of indexes of points from the data (represented by list of points), in this case 387 data should be specified. 389 @param[in] cluster1 (list): The first cluster. 390 @param[in] cluster2 (list): The second cluster. 391 @param[in] data (list): If specified than elements of clusters will be used as indexes, 392 otherwise elements of cluster will be considered as points. 394 @return (double) Average intra-cluster distance between two clusters. 400 for i
in range(len(cluster1) + len(cluster2)):
401 for j
in range(len(cluster1) + len(cluster2)):
404 if i < len(cluster1):
405 first_point = cluster1[i]
407 first_point = cluster2[i - len(cluster1)]
410 if j < len(cluster1):
411 second_point = cluster1[j]
413 second_point = cluster2[j - len(cluster1)]
417 if i < len(cluster1):
418 first_point = data[cluster1[i]]
420 first_point = data[cluster2[i - len(cluster1)]]
422 if j < len(cluster1):
423 second_point = data[cluster1[j]]
425 second_point = data[cluster2[j - len(cluster1)]]
429 distance /= float((len(cluster1) + len(cluster2)) * (len(cluster1) + len(cluster2) - 1.0))
430 return distance ** 0.5
435 @brief Calculates variance increase distance between two clusters. 436 @details Clusters can be represented by list of coordinates (in this case data shouldn't be specified), 437 or by list of indexes of points from the data (represented by list of points), in this case 438 data should be specified. 440 @param[in] cluster1 (list): The first cluster. 441 @param[in] cluster2 (list): The second cluster. 442 @param[in] data (list): If specified than elements of clusters will be used as indexes, 443 otherwise elements of cluster will be considered as points. 445 @return (double) Average variance increase distance between two clusters. 451 member_cluster1 = [0.0] * len(cluster1[0])
452 member_cluster2 = [0.0] * len(cluster2[0])
455 member_cluster1 = [0.0] * len(data[0])
456 member_cluster2 = [0.0] * len(data[0])
458 for i
in range(len(cluster1)):
464 for j
in range(len(cluster2)):
477 distance_general = 0.0
478 distance_cluster1 = 0.0
479 distance_cluster2 = 0.0
481 for i
in range(len(cluster1)):
490 for j
in range(len(cluster2)):
499 return distance_general - distance_cluster1 - distance_cluster2
504 @brief Calculates description of ellipse using covariance matrix. 506 @param[in] covariance (numpy.array): Covariance matrix for which ellipse area should be calculated. 507 @param[in] scale (float): Scale of the ellipse. 509 @return (float, float, float) Return ellipse description: angle, width, height. 513 eigh_values, eigh_vectors = numpy.linalg.eigh(covariance)
514 order = eigh_values.argsort()[::-1]
516 values, vectors = eigh_values[order], eigh_vectors[order]
517 angle = numpy.degrees(numpy.arctan2(*vectors[:,0][::-1]))
522 width, height = 2.0 * scale * numpy.sqrt(values)
523 return angle, width, height
528 @brief Finds maximum and minimum corner in each dimension of the specified data. 530 @param[in] data (list): List of points that should be analysed. 531 @param[in] data_filter (list): List of indexes of the data that should be analysed, 532 if it is 'None' then whole 'data' is analysed to obtain corners. 534 @return (list) Tuple of two points that corresponds to minimum and maximum corner (min_corner, max_corner). 538 dimensions = len(data[0])
542 bypass = range(len(data))
544 maximum_corner = list(data[bypass[0]][:])
545 minimum_corner = list(data[bypass[0]][:])
547 for index_point
in bypass:
548 for index_dimension
in range(dimensions):
549 if data[index_point][index_dimension] > maximum_corner[index_dimension]:
550 maximum_corner[index_dimension] = data[index_point][index_dimension]
552 if data[index_point][index_dimension] < minimum_corner[index_dimension]:
553 minimum_corner[index_dimension] = data[index_point][index_dimension]
555 return minimum_corner, maximum_corner
560 @brief Calculates norm of an input vector that is known as a vector length. 562 @param[in] vector (list): The input vector whose length is calculated. 564 @return (double) vector norm known as vector length. 569 for component
in vector:
570 length += component * component
572 length = length ** 0.5
579 @brief Calculates Heaviside function that represents step function. 580 @details If input value is greater than 0 then returns 1, otherwise returns 0. 582 @param[in] value (double): Argument of Heaviside function. 584 @return (double) Value of Heaviside function. 595 @brief Executes specified method or function with measuring of execution time. 597 @param[in] executable_function (pointer): Pointer to function or method. 598 @param[in] args (*): Arguments of called function or method. 600 @return (tuple) Execution time and result of execution of function or method (execution_time, result_execution). 604 time_start = time.clock();
605 result = executable_function(*args);
606 time_end = time.clock();
608 return (time_end - time_start, result);
613 @brief Extracts number of oscillations of specified oscillator. 615 @param[in] osc_dyn (list): Dynamic of oscillators. 616 @param[in] index (uint): Index of oscillator in dynamic. 617 @param[in] amplitude_threshold (double): Amplitude threshold when oscillation is taken into account, for example, 618 when oscillator amplitude is greater than threshold then oscillation is incremented. 620 @return (uint) Number of oscillations of specified oscillator. 624 number_oscillations = 0;
625 waiting_differential =
False;
626 threshold_passed =
False;
627 high_level_trigger =
True if (osc_dyn[0][index] > amplitude_threshold)
else False;
629 for values
in osc_dyn:
630 if ( (values[index] >= amplitude_threshold)
and (high_level_trigger
is False) ):
631 high_level_trigger =
True;
632 threshold_passed =
True;
634 elif ( (values[index] < amplitude_threshold)
and (high_level_trigger
is True) ):
635 high_level_trigger =
False;
636 threshold_passed =
True;
638 if (threshold_passed
is True):
639 threshold_passed =
False;
640 if (waiting_differential
is True and high_level_trigger
is False):
641 number_oscillations += 1;
642 waiting_differential =
False;
645 waiting_differential =
True;
647 return number_oscillations;
652 @brief Allocate clusters in line with ensembles of synchronous oscillators where each 653 synchronous ensemble corresponds to only one cluster. 655 @param[in] dynamic (dynamic): Dynamic of each oscillator. 656 @param[in] tolerance (double): Maximum error for allocation of synchronous ensemble oscillators. 657 @param[in] threshold (double): Amlitude trigger when spike is taken into account. 658 @param[in] ignore (bool): Set of indexes that shouldn't be taken into account. 660 @return (list) Grours (lists) of indexes of synchronous oscillators, for example, 661 [ [index_osc1, index_osc3], [index_osc2], [index_osc4, index_osc5] ]. 665 descriptors = [ []
for _
in range(len(dynamic[0])) ];
668 for index_dyn
in range(0, len(dynamic[0]), 1):
669 if ((ignore
is not None)
and (index_dyn
in ignore)):
672 time_stop_simulation = len(dynamic) - 1;
673 active_state =
False;
675 if (dynamic[time_stop_simulation][index_dyn] > threshold):
679 if (active_state
is True):
680 while ( (dynamic[time_stop_simulation][index_dyn] > threshold)
and (time_stop_simulation > 0) ):
681 time_stop_simulation -= 1;
684 if (time_stop_simulation == 0):
688 active_state =
False;
691 for t
in range(time_stop_simulation, 0, -1):
692 if ( (dynamic[t][index_dyn] > threshold)
and (active_state
is False) ):
695 elif ( (dynamic[t][index_dyn] < threshold)
and (active_state
is True) ):
697 active_state =
False;
701 if (desc == [0, 0, 0]):
704 desc[2] = desc[1] + (desc[0] - desc[1]) / 2.0;
705 descriptors[index_dyn] = desc;
710 desc_sync_ensembles = [];
712 for index_desc
in range(0, len(descriptors), 1):
713 if (descriptors[index_desc] == []):
716 if (len(sync_ensembles) == 0):
717 desc_ensemble = descriptors[index_desc];
718 reducer = (desc_ensemble[0] - desc_ensemble[1]) * tolerance;
720 desc_ensemble[0] = desc_ensemble[2] + reducer;
721 desc_ensemble[1] = desc_ensemble[2] - reducer;
723 desc_sync_ensembles.append(desc_ensemble);
724 sync_ensembles.append([ index_desc ]);
726 oscillator_captured =
False;
727 for index_ensemble
in range(0, len(sync_ensembles), 1):
728 if ( (desc_sync_ensembles[index_ensemble][0] > descriptors[index_desc][2])
and (desc_sync_ensembles[index_ensemble][1] < descriptors[index_desc][2])):
729 sync_ensembles[index_ensemble].append(index_desc);
730 oscillator_captured =
True;
733 if (oscillator_captured
is False):
734 desc_ensemble = descriptors[index_desc];
735 reducer = (desc_ensemble[0] - desc_ensemble[1]) * tolerance;
737 desc_ensemble[0] = desc_ensemble[2] + reducer;
738 desc_ensemble[1] = desc_ensemble[2] - reducer;
740 desc_sync_ensembles.append(desc_ensemble);
741 sync_ensembles.append([ index_desc ]);
743 return sync_ensembles;
746 def draw_clusters(data, clusters, noise = [], marker_descr = '.', hide_axes = False, axes = None, display_result = True):
748 @brief Displays clusters for data in 2D or 3D. 750 @param[in] data (list): Points that are described by coordinates represented. 751 @param[in] clusters (list): Clusters that are represented by lists of indexes where each index corresponds to point in data. 752 @param[in] noise (list): Points that are regarded to noise. 753 @param[in] marker_descr (string): Marker for displaying points. 754 @param[in] hide_axes (bool): If True - axes is not displayed. 755 @param[in] axes (ax) Matplotlib axes where clusters should be drawn, if it is not specified (None) then new plot will be created. 756 @param[in] display_result (bool): If specified then matplotlib axes will be used for drawing and plot will not be shown. 758 @return (ax) Matplotlib axes where drawn clusters are presented. 763 if ( (data
is not None)
and (clusters
is not None) ):
764 dimension = len(data[0]);
765 elif ( (data
is None)
and (clusters
is not None) ):
766 dimension = len(clusters[0][0]);
768 raise NameError(
'Data or clusters should be specified exactly.');
771 colors = [
'red',
'blue',
'darkgreen',
'brown',
'violet',
772 'deepskyblue',
'darkgrey',
'lightsalmon',
'deeppink',
'yellow',
773 'black',
'mediumspringgreen',
'orange',
'darkviolet',
'darkblue',
774 'silver',
'lime',
'pink',
'gold',
'bisque' ];
776 if (len(clusters) > len(colors)):
777 raise NameError(
'Impossible to represent clusters due to number of specified colors.');
783 if ((dimension) == 1
or (dimension == 2)):
784 axes = fig.add_subplot(111);
785 elif (dimension == 3):
786 axes = fig.gca(projection=
'3d');
788 raise NameError(
'Drawer supports only 2d and 3d data representation');
791 for cluster
in clusters:
792 color = colors[color_index];
796 axes.plot(item[0], 0.0, color = color, marker = marker_descr);
798 axes.plot(data[item][0], 0.0, color = color, marker = marker_descr);
802 axes.plot(item[0], item[1], color = color, marker = marker_descr);
804 axes.plot(data[item][0], data[item][1], color = color, marker = marker_descr);
806 elif (dimension == 3):
808 axes.scatter(item[0], item[1], item[2], c = color, marker = marker_descr);
810 axes.scatter(data[item][0], data[item][1], data[item][2], c = color, marker = marker_descr);
817 axes.plot(item[0], 0.0,
'w' + marker_descr);
819 axes.plot(data[item][0], 0.0,
'w' + marker_descr);
823 axes.plot(item[0], item[1],
'w' + marker_descr);
825 axes.plot(data[item][0], data[item][1],
'w' + marker_descr);
827 elif (dimension == 3):
829 axes.scatter(item[0], item[1], item[2], c =
'w', marker = marker_descr);
831 axes.scatter(data[item][0], data[item][1], data[item][2], c =
'w', marker = marker_descr);
835 if (hide_axes
is True):
836 axes.xaxis.set_ticklabels([]);
837 axes.yaxis.set_ticklabels([]);
840 axes.zaxis.set_ticklabels([]);
842 if (display_result
is True):
848 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):
850 @brief Draw dynamics of neurons (oscillators) in the network. 851 @details It draws if matplotlib is not specified (None), othewise it should be performed manually. 853 @param[in] t (list): Values of time (used by x axis). 854 @param[in] dyn (list): Values of output of oscillators (used by y axis). 855 @param[in] x_title (string): Title for Y. 856 @param[in] y_title (string): Title for X. 857 @param[in] x_lim (double): X limit. 858 @param[in] y_lim (double): Y limit. 859 @param[in] x_labels (bool): If True - shows X labels. 860 @param[in] y_labels (bool): If True - shows Y labels. 861 @param[in] separate (list): Consists of lists of oscillators where each such list consists of oscillator indexes that will be shown on separated stage. 862 @param[in] axes (ax): If specified then matplotlib axes will be used for drawing and plot will not be shown. 864 @return (ax) Axes of matplotlib. 871 if (x_lim
is not None):
874 stage_xlim = [0, t[len(t) - 1]];
876 if ( (isinstance(separate, bool)
is True)
and (separate
is True) ):
877 if (isinstance(dyn[0], list)
is True):
878 number_lines = len(dyn[0]);
882 elif (isinstance(separate, list)
is True):
883 number_lines = len(separate);
888 dysplay_result =
False;
890 dysplay_result =
True;
891 (fig, axes) = plt.subplots(number_lines, 1);
894 if (isinstance(dyn[0], list)
is True):
895 num_items = len(dyn[0]);
896 for index
in range(0, num_items, 1):
897 y = [item[index]
for item
in dyn];
899 if (number_lines > 1):
903 if (isinstance(separate, bool)
is True):
906 elif (isinstance(separate, list)
is True):
907 for index_group
in range(0, len(separate), 1):
908 if (index
in separate[index_group]):
909 index_stage = index_group;
912 if (index_stage != -1):
913 if (index_stage != number_lines - 1):
914 axes[index_stage].get_xaxis().set_visible(
False);
916 axes[index_stage].plot(t, y,
'b-', linewidth = 0.5);
917 set_ax_param(axes[index_stage], x_title, y_title, stage_xlim, y_lim, x_labels, y_labels,
True);
920 axes.plot(t, y,
'b-', linewidth = 0.5);
921 set_ax_param(axes, x_title, y_title, stage_xlim, y_lim, x_labels, y_labels,
True);
923 axes.plot(t, dyn,
'b-', linewidth = 0.5);
924 set_ax_param(axes, x_title, y_title, stage_xlim, y_lim, x_labels, y_labels,
True);
926 if (dysplay_result
is True):
932 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):
934 @brief Sets parameters for matplotlib ax. 936 @param[in] ax (Axes): Axes for which parameters should applied. 937 @param[in] x_title (string): Title for Y. 938 @param[in] y_title (string): Title for X. 939 @param[in] x_lim (double): X limit. 940 @param[in] y_lim (double): Y limit. 941 @param[in] x_labels (bool): If True - shows X labels. 942 @param[in] y_labels (bool): If True - shows Y labels. 943 @param[in] grid (bool): If True - shows grid. 946 from matplotlib.font_manager
import FontProperties;
947 from matplotlib
import rcParams;
949 if (_platform ==
"linux")
or (_platform ==
"linux2"):
950 rcParams[
'font.sans-serif'] = [
'Liberation Serif'];
952 rcParams[
'font.sans-serif'] = [
'Arial'];
954 rcParams[
'font.size'] = 12;
956 surface_font = FontProperties();
957 if (_platform ==
"linux")
or (_platform ==
"linux2"):
958 surface_font.set_name(
'Liberation Serif');
960 surface_font.set_name(
'Arial');
962 surface_font.set_size(
'12');
964 if (y_title
is not None): ax.set_ylabel(y_title, fontproperties = surface_font);
965 if (x_title
is not None): ax.set_xlabel(x_title, fontproperties = surface_font);
967 if (x_lim
is not None): ax.set_xlim(x_lim[0], x_lim[1]);
968 if (y_lim
is not None): ax.set_ylim(y_lim[0], y_lim[1]);
970 if (x_labels
is False): ax.xaxis.set_ticklabels([]);
971 if (y_labels
is False): ax.yaxis.set_ticklabels([]);
976 def draw_dynamics_set(dynamics, xtitle = None, ytitle = None, xlim = None, ylim = None, xlabels = False, ylabels = False):
978 @brief Draw lists of dynamics of neurons (oscillators) in the network. 980 @param[in] dynamics (list): List of network outputs that are represented by values of output of oscillators (used by y axis). 981 @param[in] xtitle (string): Title for Y. 982 @param[in] ytitle (string): Title for X. 983 @param[in] xlim (double): X limit. 984 @param[in] ylim (double): Y limit. 985 @param[in] xlabels (bool): If True - shows X labels. 986 @param[in] ylabels (bool): If True - shows Y labels. 990 number_dynamics = len(dynamics);
991 if (number_dynamics == 1):
992 draw_dynamics(dynamics[0][0], dynamics[0][1], xtitle, ytitle, xlim, ylim, xlabels, ylabels);
995 number_cols = int(numpy.ceil(number_dynamics ** 0.5));
996 number_rows = int(numpy.ceil(number_dynamics / number_cols));
999 double_indexer =
True;
1000 if ( (number_cols == 1)
or (number_rows == 1) ):
1002 double_indexer =
False;
1004 (_, axarr) = plt.subplots(number_rows, number_cols);
1007 for dynamic
in dynamics:
1008 axarr[real_index] =
draw_dynamics(dynamic[0], dynamic[1], xtitle, ytitle, xlim, ylim, xlabels, ylabels, axes = axarr[real_index]);
1011 if (double_indexer
is True):
1012 real_index = real_index[0], real_index[1] + 1;
1013 if (real_index[1] >= number_cols):
1014 real_index = real_index[0] + 1, 0;
1023 @brief Shows image segments using colored image. 1024 @details Each color on result image represents allocated segment. The first image is initial and other is result of segmentation. 1026 @param[in] source (string): Path to image. 1027 @param[in] clusters (list): List of clusters (allocated segments of image) where each cluster 1028 consists of indexes of pixel from source image. 1029 @param[in] hide_axes (bool): If True then axes will not be displayed. 1033 image_source = Image.open(source);
1034 image_size = image_source.size;
1036 (fig, axarr) = plt.subplots(1, 2);
1038 plt.setp([ax
for ax
in axarr], visible =
False);
1040 available_colors = [ (0, 162, 232), (34, 177, 76), (237, 28, 36),
1041 (255, 242, 0), (0, 0, 0), (237, 28, 36),
1042 (255, 174, 201), (127, 127, 127), (185, 122, 87),
1043 (200, 191, 231), (136, 0, 21), (255, 127, 39),
1044 (63, 72, 204), (195, 195, 195), (255, 201, 14),
1045 (239, 228, 176), (181, 230, 29), (153, 217, 234),
1048 image_color_segments = [(255, 255, 255)] * (image_size[0] * image_size[1]);
1050 for index_segment
in range(len(clusters)):
1051 for index_pixel
in clusters[index_segment]:
1052 image_color_segments[index_pixel] = available_colors[index_segment];
1054 stage = array(image_color_segments, numpy.uint8);
1055 stage = numpy.reshape(stage, (image_size[1], image_size[0]) + ((3),));
1056 image_cluster = Image.fromarray(stage,
'RGB');
1058 axarr[0].imshow(image_source, interpolation =
'none');
1059 axarr[1].imshow(image_cluster, interpolation =
'none');
1062 plt.setp(axarr[i], visible =
True);
1064 if (hide_axes
is True):
1065 axarr[i].xaxis.set_ticklabels([]);
1066 axarr[i].yaxis.set_ticklabels([]);
1067 axarr[i].xaxis.set_ticks_position(
'none');
1068 axarr[i].yaxis.set_ticks_position(
'none');
1075 @brief Shows image segments using black masks. 1076 @details Each black mask of allocated segment is presented on separate plot. 1077 The first image is initial and others are black masks of segments. 1079 @param[in] source (string): Path to image. 1080 @param[in] clusters (list): List of clusters (allocated segments of image) where each cluster 1081 consists of indexes of pixel from source image. 1082 @param[in] hide_axes (bool): If True then axes will not be displayed. 1085 if (len(clusters) == 0):
1086 print(
"Warning: Nothing to draw - list of clusters is empty.")
1089 image_source = Image.open(source);
1090 image_size = image_source.size;
1093 number_clusters = len(clusters) + 1;
1095 number_cols = int(numpy.ceil(number_clusters ** 0.5));
1096 number_rows = int(numpy.ceil(number_clusters / number_cols));
1100 double_indexer =
True;
1101 if ( (number_cols == 1)
or (number_rows == 1) ):
1103 double_indexer =
False;
1105 (fig, axarr) = plt.subplots(number_rows, number_cols);
1106 plt.setp([ax
for ax
in axarr], visible =
False);
1108 axarr[real_index].imshow(image_source, interpolation =
'none');
1109 plt.setp(axarr[real_index], visible =
True);
1111 if (hide_axes
is True):
1112 axarr[real_index].xaxis.set_ticklabels([]);
1113 axarr[real_index].yaxis.set_ticklabels([]);
1114 axarr[real_index].xaxis.set_ticks_position(
'none');
1115 axarr[real_index].yaxis.set_ticks_position(
'none');
1117 if (double_indexer
is True):
1122 for cluster
in clusters:
1123 stage_cluster = [(255, 255, 255)] * (image_size[0] * image_size[1]);
1124 for index
in cluster:
1125 stage_cluster[index] = (0, 0, 0);
1127 stage = array(stage_cluster, numpy.uint8);
1128 stage = numpy.reshape(stage, (image_size[1], image_size[0]) + ((3),));
1130 image_cluster = Image.fromarray(stage,
'RGB');
1132 axarr[real_index].imshow(image_cluster, interpolation =
'none');
1133 plt.setp(axarr[real_index], visible =
True);
1135 if (hide_axes
is True):
1136 axarr[real_index].xaxis.set_ticklabels([]);
1137 axarr[real_index].yaxis.set_ticklabels([]);
1139 axarr[real_index].xaxis.set_ticks_position(
'none');
1140 axarr[real_index].yaxis.set_ticks_position(
'none');
1142 if (double_indexer
is True):
1143 real_index = real_index[0], real_index[1] + 1;
1144 if (real_index[1] >= number_cols):
1145 real_index = real_index[0] + 1, 0;
1155 @brief Calculates linear sum of vector that is represented by list, each element can be represented by list - multidimensional elements. 1157 @param[in] list_vector (list): Input vector. 1159 @return (list|double) Linear sum of vector that can be represented by list in case of multidimensional elements. 1164 list_representation = (type(list_vector[0]) == list);
1166 if (list_representation
is True):
1167 dimension = len(list_vector[0]);
1168 linear_sum = [0] * dimension;
1170 for index_element
in range(0, len(list_vector)):
1171 if (list_representation
is True):
1172 for index_dimension
in range(0, dimension):
1173 linear_sum[index_dimension] += list_vector[index_element][index_dimension];
1175 linear_sum += list_vector[index_element];
1182 @brief Calculates square sum of vector that is represented by list, each element can be represented by list - multidimensional elements. 1184 @param[in] list_vector (list): Input vector. 1186 @return (double) Square sum of vector. 1191 list_representation = (type(list_vector[0]) == list);
1193 for index_element
in range(0, len(list_vector)):
1194 if (list_representation
is True):
1197 square_sum += list_vector[index_element] * list_vector[index_element];
1204 @brief Calculates subtraction of two lists. 1205 @details Each element from list 'a' is subtracted by element from list 'b' accordingly. 1207 @param[in] a (list): List of elements that supports mathematical subtraction. 1208 @param[in] b (list): List of elements that supports mathematical subtraction. 1210 @return (list) Results of subtraction of two lists. 1213 return [a[i] - b[i]
for i
in range(len(a))];
1218 @brief Calculates subtraction between list and number. 1219 @details Each element from list 'a' is subtracted by number 'b'. 1221 @param[in] a (list): List of elements that supports mathematical subtraction. 1222 @param[in] b (list): Value that supports mathematical subtraction. 1224 @return (list) Results of subtraction between list and number. 1227 return [a[i] - b
for i
in range(len(a))];
1232 @brief Addition of two lists. 1233 @details Each element from list 'a' is added to element from list 'b' accordingly. 1235 @param[in] a (list): List of elements that supports mathematic addition.. 1236 @param[in] b (list): List of elements that supports mathematic addition.. 1238 @return (list) Results of addtion of two lists. 1241 return [a[i] + b[i]
for i
in range(len(a))];
1246 @brief Addition between list and number. 1247 @details Each element from list 'a' is added to number 'b'. 1249 @param[in] a (list): List of elements that supports mathematic addition. 1250 @param[in] b (double): Value that supports mathematic addition. 1252 @return (list) Result of addtion of two lists. 1255 return [a[i] + b
for i
in range(len(a))];
1260 @brief Division between list and number. 1261 @details Each element from list 'a' is divided by number 'b'. 1263 @param[in] a (list): List of elements that supports mathematic division. 1264 @param[in] b (double): Value that supports mathematic division. 1266 @return (list) Result of division between list and number. 1269 return [a[i] / b
for i
in range(len(a))];
1274 @brief Division of two lists. 1275 @details Each element from list 'a' is divided by element from list 'b' accordingly. 1277 @param[in] a (list): List of elements that supports mathematic division. 1278 @param[in] b (list): List of elements that supports mathematic division. 1280 @return (list) Result of division of two lists. 1283 return [a[i] / b[i]
for i
in range(len(a))];
1288 @brief Multiplication between list and number. 1289 @details Each element from list 'a' is multiplied by number 'b'. 1291 @param[in] a (list): List of elements that supports mathematic division. 1292 @param[in] b (double): Number that supports mathematic division. 1294 @return (list) Result of division between list and number. 1297 return [a[i] * b
for i
in range(len(a))];
1302 @brief Multiplication of two lists. 1303 @details Each element from list 'a' is multiplied by element from list 'b' accordingly. 1305 @param[in] a (list): List of elements that supports mathematic multiplication. 1306 @param[in] b (list): List of elements that supports mathematic multiplication. 1308 @return (list) Result of multiplication of elements in two lists. 1311 return [a[i] * b[i]
for i
in range(len(a))];
def euclidean_distance(a, b)
Calculate Euclidean distance between vector a and b.
def square_sum(list_vector)
Calculates square sum of vector that is represented by list, each element can be represented by list ...
def draw_clusters(data, clusters, noise=[], marker_descr='.', hide_axes=False, axes=None, display_result=True)
Displays clusters for data in 2D or 3D.
def heaviside(value)
Calculates Heaviside function that represents step function.
def manhattan_distance(a, b)
Calculate Manhattan distance between vector a and b.
def read_image(filename)
Returns image as N-dimension (depends on the input image) matrix, where one element of list describes...
Module provides various distance metrics - abstraction of the notion of distance in a metric space...
def average_intra_cluster_distance(cluster1, cluster2, data=None)
Calculates average intra-cluster distance between two clusters.
def list_math_multiplication_number(a, b)
Multiplication between list and number.
def extract_number_oscillations(osc_dyn, index=0, amplitude_threshold=1.0)
Extracts number of oscillations of specified oscillator.
def list_math_division(a, b)
Division of two lists.
def list_math_multiplication(a, b)
Multiplication of two lists.
def allocate_sync_ensembles(dynamic, tolerance=0.1, threshold=1.0, ignore=None)
Allocate clusters in line with ensembles of synchronous oscillators where each synchronous ensemble c...
def average_neighbor_distance(points, num_neigh)
Returns average distance for establish links between specified number of nearest neighbors.
def average_inter_cluster_distance(cluster1, cluster2, data=None)
Calculates average inter-cluster distance between two clusters.
def timedcall(executable_function, args)
Executes specified method or function with measuring of execution time.
def read_sample(filename)
Returns data sample from simple text file.
def draw_image_color_segments(source, clusters, hide_axes=True)
Shows image segments using colored image.
def norm_vector(vector)
Calculates norm of an input vector that is known as a vector length.
def list_math_substraction_number(a, b)
Calculates subtraction between list and number.
def stretch_pattern(image_source)
Returns stretched content as 1-dimension (gray colored) matrix with size of input image...
def draw_dynamics_set(dynamics, xtitle=None, ytitle=None, xlim=None, ylim=None, xlabels=False, ylabels=False)
Draw lists of dynamics of neurons (oscillators) in the network.
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)
Draw dynamics of neurons (oscillators) in the network.
def data_corners(data, data_filter=None)
Finds maximum and minimum corner in each dimension of the specified data.
def calculate_distance_matrix(sample)
Calculates distance matrix for data sample (sequence of points) using Euclidean distance as a metric...
def list_math_addition(a, b)
Addition of two lists.
def calculate_ellipse_description(covariance, scale=2.0)
Calculates description of ellipse using covariance matrix.
def euclidean_distance_square(a, b)
Calculate square Euclidian distance between vector a and b.
def gray_pattern_borders(image)
Returns coordinates of gray image content on the input image.
def list_math_division_number(a, b)
Division between list and number.
def linear_sum(list_vector)
Calculates linear sum of vector that is represented by list, each element can be represented by list ...
def medoid(data, indexes=None, kwargs)
Calculate medoid for input points using Euclidean distance.
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)
Sets parameters for matplotlib ax.
def draw_image_mask_segments(source, clusters, hide_axes=True)
Shows image segments using black masks.
def list_math_subtraction(a, b)
Calculates subtraction of two lists.
def list_math_addition_number(a, b)
Addition between list and number.
def variance_increase_distance(cluster1, cluster2, data=None)
Calculates variance increase distance between two clusters.
def rgb2gray(image_rgb_array)
Returns image as 1-dimension (gray colored) matrix, where one element of list describes pixel...