Completed
Push — 0.7.dev ( 22bad4...e81af0 )
by Andrei
02:59
created

dynamic_descr.__init__()   A

Complexity

Conditions 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
1
"""!
2
3
@brief Output dynamic visualizer
4
5
@authors Andrei Novikov ([email protected])
6
@date 2014-2018
7
@copyright GNU Public License
8
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.
14
    
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.
19
    
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/>.
22
@endcond
23
24
"""
25
26
27
import matplotlib.pyplot as plt;
0 ignored issues
show
Configuration introduced by
The import matplotlib.pyplot could not be resolved.

This can be caused by one of the following:

1. Missing Dependencies

This error could indicate a configuration issue of Pylint. Make sure that your libraries are available by adding the necessary commands.

# .scrutinizer.yml
before_commands:
    - sudo pip install abc # Python2
    - sudo pip3 install abc # Python3
Tip: We are currently not using virtualenv to run pylint, when installing your modules make sure to use the command for the correct version.

2. Missing __init__.py files

This error could also result from missing __init__.py files in your module folders. Make sure that you place one file in each sub-folder.

Loading history...
28
29
from pyclustering.utils import set_ax_param;
30
31
32
class canvas_descr:
33
    def __init__(self, x_title=None, y_title=None, x_lim=None, y_lim=None, x_labels=True, y_labels=True):
34
        self.x_title  = x_title;
35
        self.y_title  = y_title;
36
        self.x_lim    = x_lim;
37
        self.y_lim    = y_lim;
38
        self.x_labels = x_labels;
39
        self.y_labels = y_labels;
40
41
42
class dynamic_descr:
43
    def __init__(self, canvas, time, dynamics, separate, color):
44
        self.canvas     = canvas;
45
        self.time       = time;
46
        self.dynamics   = dynamics;
47
        self.separate   = separate;
48
        self.color      = color;
49
        
50
        self.separate   = self.__get_canonical_separate();
51
52
53
    def get_axis_index(self, index_dynamic):
54
        return self.separate[index_dynamic];
55
56
57
    def __get_canonical_separate(self):
58
        if (isinstance(self.separate, list)):
59
            separate = [0] * len(self.dynamics[0]);
60
            for canvas_index in range(len(self.separate)):
61
                dynamic_indexes = self.separate[canvas_index];
62
                for dynamic_index in dynamic_indexes:
63
                    separate[dynamic_index] = canvas_index;
64
            
65
            return separate;
66
        
67
        elif (self.separate is False):
68
            if (isinstance(self.dynamics[0], list) is True):
69
                return [ self.canvas ] * len(self.dynamics[0]);
70
            else:
71
                return [ self.canvas ];
72
        
73
        elif (self.separate is True):
74
            if (isinstance(self.dynamics[0], list) is True):
75
                return range(self.canvas, self.canvas + len(self.dynamics[0]));
76
            else:
77
                return [ self.canvas ];
78
79
        else:
80
            raise Exception("Incorrect type of argument 'separate' '%s'." % type(self.separate));
81
82
83
class dynamic_visualizer:
84
    def __init__(self, canvas, x_title=None, y_title=None, x_lim=None, y_lim=None, x_labels=True, y_labels=True):
85
        self.__size = canvas;
86
        self.__canvases = [ canvas_descr(x_title, y_title, x_lim, y_lim, x_labels, y_labels) for _ in range(canvas) ];
87
        self.__dynamic_storage = [];
88
89
90
    def set_canvas_properties(self, canvas, x_title=None, y_title=None, x_lim=None, y_lim=None, x_labels=True, y_labels=True):
91
        self.__canvases[canvas] = canvas_descr(x_title, y_title, x_lim, y_lim, x_labels, y_labels);
92
93
94
    def append_dynamic(self, t, dynamic, canvas=0, color='blue'):
95
        description = dynamic_descr(canvas, t, dynamic, False, color);
96
        self.__dynamic_storage.append(description);
97
        self.__update_canvas_xlim(description.time, description.separate);
98
99
100
    def append_dynamics(self, t, dynamics, canvas=0, separate=False, color='blue'):
101
        description = dynamic_descr(canvas, t, dynamics, separate, color);
102
        self.__dynamic_storage.append(description);
103
        self.__update_canvas_xlim(description.time, description.separate);
104
105
106
    def show(self):
107
        (_, axis) = plt.subplots(self.__size, 1);
108
        
109
        self.__format_canvases(axis);
110
        
111
        for dynamic in self.__dynamic_storage:
112
            self.__display_dynamic(axis, dynamic);
113
        
114
        plt.show();
115
116
117
    def __display_dynamic(self, axis, dyn_descr):
118
        if (isinstance(dyn_descr.dynamics[0], list) is True):
119
            self.__display_multiple_dynamic(axis, dyn_descr);
120
        
121
        else:
122
            self.__display_single_dynamic(axis, dyn_descr);
123
124
125
    def __display_multiple_dynamic(self, axis, dyn_descr):
126
        num_items = len(dyn_descr.dynamics[0]);
127
        for index in range(0, num_items, 1):
128
            y = [item[index] for item in dyn_descr.dynamics];
129
            
130
            axis_index = dyn_descr.get_axis_index(index);
131
            ax = self.__get_axis(axis, axis_index);
132
            
133
            ax.plot(dyn_descr.time, y, 'b-', linewidth = 0.5);
134
135
136
    def __display_single_dynamic(self, axis, dyn_descr):
137
        ax = self.__get_axis(axis, dyn_descr.canvas);
138
        ax.plot(dyn_descr.time, dyn_descr.dynamics, 'b-', linewidth = 0.5);
139
140
141
    def __format_canvases(self, axis):
142
        for index in range(self.__size):
143
            canvas = self.__canvases[index];
144
            
145
            ax = self.__get_axis(axis, index);
146
            set_ax_param(ax, canvas.x_title, canvas.y_title, canvas.x_lim, canvas.y_lim, canvas.x_labels, canvas.y_labels, True);
147
            
148
            if ( (len(self.__canvases) > 1) and (index != len(self.__canvases) - 1) ):
149
                ax.get_xaxis().set_visible(False);
150
151
152
    def __update_canvas_xlim(self, t, separate):
153
        for index in separate:
154
            self.__update_single_canvas_xlim(index, t);
155
156
157
    def __update_single_canvas_xlim(self, canvas_index, t):
158
        dynamic_xlim = [0, t[len(t) - 1]];
159
        if ( (self.__canvases[canvas_index].x_lim is None) or (self.__canvases[canvas_index].x_lim < dynamic_xlim) ):
160
            self.__canvases[canvas_index].x_lim = dynamic_xlim;
161
162
163
    def __get_axis(self, axis, index):
164
        if (index >= len(self.__canvases)):
165
            raise Exception("Impossible to get axis with index '%d' - total number of canvases '%d'."
166
                            % index, len(self.__canvases));
167
        
168
        ax = axis;
169
        if (self.__size > 1):
170
            ax = axis[index];
171
        
172
        return ax;