pyclustering  0.10.1
pyclustring is a Python, C++ data mining library.
dynamic_visualizer.py
1 """!
2 
3 @brief Output dynamic visualizer
4 
5 @authors Andrei Novikov (pyclustering@yandex.ru)
6 @date 2014-2020
7 @copyright BSD-3-Clause
8 
9 """
10 
11 import matplotlib.pyplot as plt
12 
13 from pyclustering.utils import set_ax_param
14 
15 
17  """!
18  @brief Describes plot where dynamic is displayed.
19  @details Used by 'dynamic_visualizer' class.
20 
21  """
22 
23  def __init__(self, x_title=None, y_title=None, x_lim=None, y_lim=None, x_labels=True, y_labels=True):
24  """!
25  @brief Constructor of canvas.
26 
27  @param[in] x_title (string): Title for X axis, if 'None', then nothing is displayed.
28  @param[in] y_title (string): Title for Y axis, if 'None', then nothing is displayed.
29  @param[in] x_lim (list): Defines borders of X axis like [from, to], for example [0, 3.14], if 'None' then
30  borders are calculated automatically.
31  @param[in] y_lim (list): Defines borders of Y axis like [from, to], if 'None' then borders are calculated
32  automatically.
33  @param[in] x_labels (bool): If True then labels of X axis are displayed.
34  @param[in] y_labels (bool): If True then labels of Y axis are displayed.
35 
36  """
37 
38 
39  self.x_title = x_title;
40 
41 
42  self.y_title = y_title;
43 
44 
45  self.x_lim = x_lim;
46 
47 
48  self.y_lim = y_lim;
49 
50 
51  self.x_labels = x_labels;
52 
53 
54  self.y_labels = y_labels;
55 
56 
58  """!
59  @brief Output dynamic description that used to display.
60  @details Used by 'dynamic_visualizer' class.
61 
62  """
63 
64  def __init__(self, canvas, time, dynamics, separate, color):
65  """!
66  @brief Constructor of output dynamic descriptor.
67 
68  @param[in] canvas (uint): Index of canvas where dynamic should be displayed, in case of 'separate'
69  representation this argument is considered as a first canvas from that displaying should be done.
70  @param[in] time (list): Time points that are considered as a X axis.
71  @param[in] dynamics (list): Dynamic or dynamics that should be displayed.
72  @param[in] separate (bool|list): If 'True' then each dynamic is displayed on separate canvas, if it is defined
73  by list, for example, [ [1, 2], [3, 4] ], then the first and the second dynamics are displayed on
74  the canvas with index 'canvas' and the third and forth are displayed on the next 'canvas + 1'
75  canvas.
76  @param[in] color (string): Color that is used to display output dynamic(s).
77 
78  """
79 
80 
81  self.canvas = canvas;
82 
83 
84  self.time = time;
85 
86 
87  self.dynamics = dynamics;
88 
89 
90  self.separate = self.__get_canonical_separate(separate);
91 
92 
93  self.color = color;
94 
95 
96  def get_axis_index(self, index_dynamic):
97  """!
98  @brief Returns index of canvas where specified dynamic (by index 'index_dynamic') should be displayed.
99 
100  @param[in] index_dynamic (uint): Index of dynamic that should be displayed.
101 
102  @return (uint) Index of canvas.
103 
104  """
105  return self.separate[index_dynamic];
106 
107 
108  def __get_canonical_separate(self, input_separate):
109  """!
110  @brief Return unified representation of separation value.
111  @details It represents list whose size is equal to amount of dynamics, where index of dynamic will show
112  where it should be displayed.
113 
114  @param[in] input_separate (bool|list): Input separate representation that should transformed.
115 
116  @return (list) Indexes where each dynamic should be displayed.
117 
118  """
119  if (isinstance(input_separate, list)):
120  separate = [0] * len(self.dynamics[0]);
121  for canvas_index in range(len(input_separate)):
122  dynamic_indexes = input_separate[canvas_index];
123  for dynamic_index in dynamic_indexes:
124  separate[dynamic_index] = canvas_index;
125 
126  return separate;
127 
128  elif (input_separate is False):
129  if (isinstance(self.dynamics[0], list) is True):
130  return [ self.canvas ] * len(self.dynamics[0]);
131  else:
132  return [ self.canvas ];
133 
134  elif (input_separate is True):
135  if (isinstance(self.dynamics[0], list) is True):
136  return range(self.canvas, self.canvas + len(self.dynamics[0]));
137  else:
138  return [ self.canvas ];
139 
140  else:
141  raise Exception("Incorrect type of argument 'separate' '%s'." % type(input_separate));
142 
143 
145  """!
146  @brief Basic output dynamic visualizer.
147  @details The aim of the visualizer is to displayed output dynamic of any process, for example, output dynamic of
148  oscillatory network.
149 
150  """
151 
152  def __init__(self, canvas, x_title=None, y_title=None, x_lim=None, y_lim=None, x_labels=True, y_labels=True):
153  """!
154  @brief Construct dynamic visualizer.
155  @details Default properties that are generalized in the constructor, for example, X axis title, can be
156  changed by corresponding method: 'set_canvas_properties'.
157 
158  @param[in] canvas (uint): Amount of canvases that is used for visualization.
159  @param[in] x_title (string): Title for X axis of canvases, if 'None', then nothing is displayed.
160  @param[in] y_title (string): Title for Y axis of canvases, if 'None', then nothing is displayed.
161  @param[in] x_lim (list): Defines borders of X axis like [from, to], for example [0, 3.14], if 'None' then
162  borders are calculated automatically.
163  @param[in] y_lim (list): Defines borders of Y axis like [from, to], if 'None' then borders are calculated
164  automatically.
165  @param[in] x_labels (bool): If True then labels of X axis are displayed.
166  @param[in] y_labels (bool): If True then labels of Y axis are displayed.
167 
168  """
169  self.__size = canvas;
170  self.__canvases = [ canvas_descr(x_title, y_title, x_lim, y_lim, x_labels, y_labels) for _ in range(canvas) ];
171  self.__dynamic_storage = [];
172 
173 
174  def set_canvas_properties(self, canvas, x_title=None, y_title=None, x_lim=None, y_lim=None, x_labels=True, y_labels=True):
175  """!
176  @brief Set properties for specified canvas.
177 
178  @param[in] canvas (uint): Index of canvas whose properties should changed.
179  @param[in] x_title (string): Title for X axis, if 'None', then nothing is displayed.
180  @param[in] y_title (string): Title for Y axis, if 'None', then nothing is displayed.
181  @param[in] x_lim (list): Defines borders of X axis like [from, to], for example [0, 3.14], if 'None' then
182  borders are calculated automatically.
183  @param[in] y_lim (list): Defines borders of Y axis like [from, to], if 'None' then borders are calculated
184  automatically.
185  @param[in] x_labels (bool): If True then labels of X axis are displayed.
186  @param[in] y_labels (bool): If True then labels of Y axis are displayed.
187 
188  """
189  self.__canvases[canvas] = canvas_descr(x_title, y_title, x_lim, y_lim, x_labels, y_labels);
190 
191 
192  def append_dynamic(self, t, dynamic, canvas=0, color='blue'):
193  """!
194  @brief Append single dynamic to specified canvas (by default to the first with index '0').
195 
196  @param[in] t (list): Time points that corresponds to dynamic values and considered on a X axis.
197  @param[in] dynamic (list): Value points of dynamic that are considered on an Y axis.
198  @param[in] canvas (uint): Canvas where dynamic should be displayed.
199  @param[in] color (string): Color that is used for drawing dynamic on the canvas.
200 
201  """
202  description = dynamic_descr(canvas, t, dynamic, False, color);
203  self.__dynamic_storage.append(description);
204  self.__update_canvas_xlim(description.time, description.separate);
205 
206 
207  def append_dynamics(self, t, dynamics, canvas=0, separate=False, color='blue'):
208  """!
209  @brief Append several dynamics to canvas or canvases (defined by 'canvas' and 'separate' arguments).
210 
211  @param[in] t (list): Time points that corresponds to dynamic values and considered on a X axis.
212  @param[in] dynamics (list): Dynamics where each of them is considered on Y axis.
213  @param[in] canvas (uint): Index of canvas where dynamic should be displayed, in case of 'separate'
214  representation this argument is considered as a first canvas from that displaying should be done.
215  @param[in] separate (bool|list): If 'True' then each dynamic is displayed on separate canvas, if it is defined
216  by list, for example, [ [1, 2], [3, 4] ], then the first and the second dynamics are displayed on
217  the canvas with index 'canvas' and the third and forth are displayed on the next 'canvas + 1'
218  canvas.
219  @param[in] color (string): Color that is used to display output dynamic(s).
220 
221  """
222  description = dynamic_descr(canvas, t, dynamics, separate, color);
223  self.__dynamic_storage.append(description);
224  self.__update_canvas_xlim(description.time, description.separate);
225 
226 
227  def show(self, axis=None, display=True):
228  """!
229  @brief Draw and show output dynamics.
230 
231  @param[in] axis (axis): If is not 'None' then user specified axis is used to display output dynamic.
232  @param[in] display (bool): Whether output dynamic should be displayed or not, if not, then user
233  should call 'plt.show()' by himself.
234 
235  """
236 
237  if (not axis):
238  (_, axis) = plt.subplots(self.__size, 1);
239 
240  self.__format_canvases(axis);
241 
242  for dynamic in self.__dynamic_storage:
243  self.__display_dynamic(axis, dynamic);
244 
245  if (display):
246  plt.show();
247 
248 
249  def __display_dynamic(self, axis, dyn_descr):
250  if (isinstance(dyn_descr.dynamics[0], list) is True):
251  self.__display_multiple_dynamic(axis, dyn_descr);
252 
253  else:
254  self.__display_single_dynamic(axis, dyn_descr);
255 
256 
257  def __display_multiple_dynamic(self, axis, dyn_descr):
258  num_items = len(dyn_descr.dynamics[0]);
259  for index in range(0, num_items, 1):
260  y = [item[index] for item in dyn_descr.dynamics];
261 
262  axis_index = dyn_descr.get_axis_index(index);
263  ax = self.__get_axis(axis, axis_index);
264 
265  ax.plot(dyn_descr.time, y, 'b-', linewidth = 0.5);
266 
267 
268  def __display_single_dynamic(self, axis, dyn_descr):
269  ax = self.__get_axis(axis, dyn_descr.canvas);
270  ax.plot(dyn_descr.time, dyn_descr.dynamics, 'b-', linewidth = 0.5);
271 
272 
273  def __format_canvases(self, axis):
274  for index in range(self.__size):
275  canvas = self.__canvases[index];
276 
277  ax = self.__get_axis(axis, index);
278  set_ax_param(ax, canvas.x_title, canvas.y_title, canvas.x_lim, canvas.y_lim, canvas.x_labels, canvas.y_labels, True);
279 
280  if ( (len(self.__canvases) > 1) and (index != len(self.__canvases) - 1) ):
281  ax.get_xaxis().set_visible(False);
282 
283 
284  def __update_canvas_xlim(self, t, separate):
285  for index in separate:
286  self.__update_single_canvas_xlim(index, t);
287 
288 
289  def __update_single_canvas_xlim(self, canvas_index, t):
290  dynamic_xlim = [0, t[len(t) - 1]];
291  if ( (self.__canvases[canvas_index].x_lim is None) or (self.__canvases[canvas_index].x_lim < dynamic_xlim) ):
292  self.__canvases[canvas_index].x_lim = dynamic_xlim;
293 
294 
295  def __get_axis(self, axis, index):
296  if (index >= len(self.__canvases)):
297  raise Exception("Impossible to get axis with index '%d' - total number of canvases '%d'."
298  % index, len(self.__canvases));
299 
300  ax = axis;
301  if (self.__size > 1):
302  ax = axis[index];
303 
304  return ax;
pyclustering.nnet.dynamic_visualizer.canvas_descr.x_title
x_title
Title for X axis.
Definition: dynamic_visualizer.py:39
pyclustering.nnet.dynamic_visualizer.canvas_descr.x_lim
x_lim
Borders of X axis.
Definition: dynamic_visualizer.py:45
pyclustering.nnet.dynamic_visualizer.canvas_descr.y_title
y_title
Title for Y axis.
Definition: dynamic_visualizer.py:42
pyclustering.nnet.dynamic_visualizer.dynamic_descr
Output dynamic description that used to display.
Definition: dynamic_visualizer.py:57
pyclustering.nnet.dynamic_visualizer.dynamic_visualizer.__init__
def __init__(self, canvas, x_title=None, y_title=None, x_lim=None, y_lim=None, x_labels=True, y_labels=True)
Construct dynamic visualizer.
Definition: dynamic_visualizer.py:152
pyclustering.nnet.dynamic_visualizer.dynamic_visualizer.__size
__size
Definition: dynamic_visualizer.py:169
pyclustering.nnet.dynamic_visualizer.dynamic_visualizer.__format_canvases
def __format_canvases(self, axis)
Definition: dynamic_visualizer.py:273
pyclustering.nnet.dynamic_visualizer.dynamic_visualizer
Basic output dynamic visualizer.
Definition: dynamic_visualizer.py:144
pyclustering.nnet.dynamic_visualizer.dynamic_descr.canvas
canvas
Index of canvas where (or from which) dynamic should be displayed.
Definition: dynamic_visualizer.py:81
pyclustering.nnet.dynamic_visualizer.dynamic_visualizer.__display_single_dynamic
def __display_single_dynamic(self, axis, dyn_descr)
Definition: dynamic_visualizer.py:268
pyclustering.nnet.dynamic_visualizer.canvas_descr.y_lim
y_lim
Borders of Y axis.
Definition: dynamic_visualizer.py:48
pyclustering.nnet.dynamic_visualizer.dynamic_visualizer.__canvases
__canvases
Definition: dynamic_visualizer.py:170
pyclustering.nnet.dynamic_visualizer.dynamic_visualizer.__display_multiple_dynamic
def __display_multiple_dynamic(self, axis, dyn_descr)
Definition: dynamic_visualizer.py:257
pyclustering.nnet.dynamic_visualizer.dynamic_visualizer.__dynamic_storage
__dynamic_storage
Definition: dynamic_visualizer.py:171
pyclustering.nnet.dynamic_visualizer.dynamic_visualizer.__get_axis
def __get_axis(self, axis, index)
Definition: dynamic_visualizer.py:295
pyclustering.nnet.dynamic_visualizer.dynamic_descr.separate
separate
Defines how dynamic(s) should be displayed.
Definition: dynamic_visualizer.py:90
pyclustering.nnet.dynamic_visualizer.dynamic_visualizer.__update_canvas_xlim
def __update_canvas_xlim(self, t, separate)
Definition: dynamic_visualizer.py:284
pyclustering.nnet.dynamic_visualizer.canvas_descr
Describes plot where dynamic is displayed.
Definition: dynamic_visualizer.py:16
pyclustering.nnet.dynamic_visualizer.canvas_descr.__init__
def __init__(self, x_title=None, y_title=None, x_lim=None, y_lim=None, x_labels=True, y_labels=True)
Constructor of canvas.
Definition: dynamic_visualizer.py:23
pyclustering.nnet.dynamic_visualizer.dynamic_descr.time
time
Time points.
Definition: dynamic_visualizer.py:84
pyclustering.nnet.dynamic_visualizer.dynamic_visualizer.__update_single_canvas_xlim
def __update_single_canvas_xlim(self, canvas_index, t)
Definition: dynamic_visualizer.py:289
pyclustering.nnet.dynamic_visualizer.dynamic_visualizer.append_dynamics
def append_dynamics(self, t, dynamics, canvas=0, separate=False, color='blue')
Append several dynamics to canvas or canvases (defined by 'canvas' and 'separate' arguments).
Definition: dynamic_visualizer.py:207
pyclustering.nnet.dynamic_visualizer.dynamic_visualizer.show
def show(self, axis=None, display=True)
Draw and show output dynamics.
Definition: dynamic_visualizer.py:227
pyclustering.nnet.dynamic_visualizer.canvas_descr.y_labels
y_labels
Defines whether Y label should be displayed.
Definition: dynamic_visualizer.py:54
pyclustering.utils
Utils that are used by modules of pyclustering.
Definition: __init__.py:1
pyclustering.nnet.dynamic_visualizer.dynamic_descr.dynamics
dynamics
Dynamic or dynamics.
Definition: dynamic_visualizer.py:87
pyclustering.nnet.dynamic_visualizer.canvas_descr.x_labels
x_labels
Defines whether X label should be displayed.
Definition: dynamic_visualizer.py:51
pyclustering.nnet.dynamic_visualizer.dynamic_visualizer.set_canvas_properties
def set_canvas_properties(self, canvas, x_title=None, y_title=None, x_lim=None, y_lim=None, x_labels=True, y_labels=True)
Set properties for specified canvas.
Definition: dynamic_visualizer.py:174
pyclustering.nnet.dynamic_visualizer.dynamic_descr.get_axis_index
def get_axis_index(self, index_dynamic)
Returns index of canvas where specified dynamic (by index 'index_dynamic') should be displayed.
Definition: dynamic_visualizer.py:96
pyclustering.nnet.dynamic_visualizer.dynamic_visualizer.append_dynamic
def append_dynamic(self, t, dynamic, canvas=0, color='blue')
Append single dynamic to specified canvas (by default to the first with index '0').
Definition: dynamic_visualizer.py:192
pyclustering.nnet.dynamic_visualizer.dynamic_descr.__get_canonical_separate
def __get_canonical_separate(self, input_separate)
Return unified representation of separation value.
Definition: dynamic_visualizer.py:108
pyclustering.nnet.dynamic_visualizer.dynamic_descr.__init__
def __init__(self, canvas, time, dynamics, separate, color)
Constructor of output dynamic descriptor.
Definition: dynamic_visualizer.py:64
pyclustering.nnet.dynamic_visualizer.dynamic_visualizer.__display_dynamic
def __display_dynamic(self, axis, dyn_descr)
Definition: dynamic_visualizer.py:249
pyclustering.nnet.dynamic_visualizer.dynamic_descr.color
color
Color of dynamic.
Definition: dynamic_visualizer.py:93