formatter.py 5.08 KB
Newer Older
1
2
from itertools import repeat

3
4
5
class Formatter(object):

    def __init__(self):
6
        self.axes = None
7

8
9
10
    def init(self):
        pass
        
11
12
13
    def shape(self, rawdata):
        pass

14
    def set_axes(self, axes):
15
        self.axes = axes
16
        self.init()
17

18
19
20
class FormatterListToLine(Formatter):

    def __init__(self, xlabel=None, ylabel=None):
21
22
23

        Formatter.__init__(self)
        
24
25
26
27
28
        self.xlabel = xlabel
        self.ylabel = ylabel
    
    def shape(self, rawdata):

29
        ydata = rawdata
30
        xdata = range(0, len(ydata))
31
32
33
34
        
        data = {'ydatas': [ydata],
                'xdata': xdata,
                'xlabel': self.xlabel,
35
                'ylabel': self.ylabel}
36
37
38
39
40

        return(data)

class FormatterArrayToLines(Formatter):

41
    def __init__(self, xdata_index=None, xlabel=None, ylabel=None):
42
43
44
        
        Formatter.__init__(self)
        
45
46
        self.xlabel = xlabel
        self.ylabel = ylabel
47
        self.xdata_index = xdata_index
48
49
50
    
    def shape(self, rawdata):

51
        if self.xdata_index is not None:
52
            ydatas = rawdata[:]
53
            xdata = ydatas.pop(self.xdata_index)
54
        else:
55
            ydatas = rawdata
56
            xdata = range(0, len(ydatas[0])) # generic xdata
57
58
59
60
            
        data = {'ydatas': ydatas,
                'xdata': xdata,
                'xlabel': self.xlabel,
61
                'ylabel': self.ylabel}
62
63
64
65
66

        return(data)

class FormatterAppendToLines(Formatter):

67
    def __init__(self, xdata_index=None, xlabel=None, ylabel=None, with_label=False):
68
69
70
        
        Formatter.__init__(self)
        
71
72
        self.xlabel = xlabel
        self.ylabel = ylabel
73
        self.xdata_index = xdata_index
74
        self.with_label = with_label
75
76
77
78
79
80

    def shape(self, rawdata):

        ydatas = []
        xdata = []

81
        if self.with_label:
82
            labels = rawdata[0]
83
            i_index = 1
84
        else:
85
            i_index = 0
86
            labels = []
87
        
88
        for item in rawdata[i_index]:
89
90
            ydatas.append([item])
        
91
        for line in rawdata[i_index+1:]:
92
93
94
            for i in range(0, len(line)):
                ydatas[i].append(line[i])
                
95
96
        if self.xdata_index is not None:
            xdata = ydatas.pop(self.xdata_index)
97

98
            if labels != []:
99
                if self.xlabel is None:
100
                    self.xlabel = labels.pop(self.xdata_index)
101
                else:
102
                    labels.pop(self.xdata_index)
103
104
            else:
                labels = repeat(None)
105
        else:
106
            xdata = range(0, len(ydatas[0])) # generic xdata
107
108
109
110
111

        data = {'ydatas': ydatas,
                'xdata': xdata,
                'xlabel': self.xlabel,
                'ylabel': self.ylabel,
112
                'labels': labels}
113
114

        return(data)
115
116

class FormatterDictToLines(Formatter):
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191

    def __init__(self, xvar=None, yvars=None, xlabel=None, ylabel=None, with_label=False):

        Formatter.__init__(self)
        
        self.xlabel = xlabel
        self.ylabel = ylabel
        self.with_label = with_label

        self.xvar = xvar # key of xdata 
        self.yvars = yvars # keys of ydatas
        
    def init(self): # called only when formatter is set to an axes
        self.axes.keywords.update({'xvar': self.xvar}) 
        self.axes.keywords.update({'yvars': self.yvars}) 
        
    def shape(self, rawdata):

        # Init
        xdata = None
        ydatas = []
        tmp_labels = []
        labels = []

        # YDATAS
        if self.axes.keywords['yvars'] is not None:
            
            for var_name in self.axes.keywords['yvars']:
                
                try:
                    # DATA AND LABEL
                    ydatas.append(rawdata[var_name])
                    tmp_labels.append(var_name)

                except KeyError:
                    print(WARN + "The key {0} does not exist in the data. IGNORED.".format(var_name))

            if ydatas == []:
                print(SEVR + "There were no data corresponding to yvars in data.")

                
        else:
            print(SEVR + "The keyword yvars should not be None! Set it either in the Formatter constructor or modifiy the axes's keywords directly")


        # XDATA
        if self.axes.keywords['xvar'] is not None:
            
            try:
                # JUST DATA
                xdata = rawdata[self.axes.keywords['xvar']]
                
            except KeyError:
                print(SEVR + "The requested xvar {0} does not exist in the data!".format(self.axes.keywords['xvar']))

                
        else:
            print(SEVR + "The keyword xvar should not be None! Set it either in the Formatter constructor or modifiy the axes's keywords directly")
                
            
        if self.with_label:
            
            if self.xlabel is None:
                self.xlabel = self.axes.keywords['xvar']

            labels = tmp_labels

            
        data = {'ydatas': ydatas,
                'xdata': xdata,
                'xlabel': self.xlabel,
                'ylabel': self.ylabel,
                'labels': labels}
            
        return(data)