1 | #!/usr/bin/env python |
---|
2 | # -*- coding: utf-8 -*- |
---|
3 | |
---|
4 | # this must come first |
---|
5 | from __future__ import print_function, unicode_literals, division |
---|
6 | |
---|
7 | # standard library imports |
---|
8 | from argparse import ArgumentParser |
---|
9 | import os |
---|
10 | import os.path |
---|
11 | import numpy as np |
---|
12 | |
---|
13 | # Application library imports |
---|
14 | from libconso import * |
---|
15 | |
---|
16 | |
---|
17 | ######################################## |
---|
18 | class LoginDict(dict): |
---|
19 | #--------------------------------------- |
---|
20 | def __init__(self): |
---|
21 | self = {} |
---|
22 | |
---|
23 | #--------------------------------------- |
---|
24 | def fill_data(self, filein): |
---|
25 | """ |
---|
26 | """ |
---|
27 | try: |
---|
28 | data = np.genfromtxt( |
---|
29 | filein, |
---|
30 | skip_header=1, |
---|
31 | converters={ |
---|
32 | 0: string_to_date, |
---|
33 | 1: str, |
---|
34 | }, |
---|
35 | missing_values="nan", |
---|
36 | ) |
---|
37 | except: |
---|
38 | print("Empty file {}".format(filein)) |
---|
39 | exit(1) |
---|
40 | |
---|
41 | for date, login, conso in data: |
---|
42 | self.add_item(date, login, conso) |
---|
43 | |
---|
44 | #--------------------------------------- |
---|
45 | def add_item(self, date, login, conso): |
---|
46 | """ |
---|
47 | """ |
---|
48 | self[login] = Login(date, login, conso) |
---|
49 | |
---|
50 | #--------------------------------------- |
---|
51 | def get_items(self): |
---|
52 | """ |
---|
53 | """ |
---|
54 | items = (item for item in self.itervalues()) |
---|
55 | items = sorted(items, key=lambda item: item.login) |
---|
56 | |
---|
57 | return items |
---|
58 | |
---|
59 | #--------------------------------------- |
---|
60 | def get_items_not_null(self): |
---|
61 | """ |
---|
62 | """ |
---|
63 | items = (item for item in self.itervalues() |
---|
64 | if item.conso > 0.) |
---|
65 | items = sorted(items, key=lambda item: item.login) |
---|
66 | |
---|
67 | return items |
---|
68 | |
---|
69 | |
---|
70 | class Login(object): |
---|
71 | #--------------------------------------- |
---|
72 | def __init__(self, date, login, conso): |
---|
73 | self.date = date |
---|
74 | self.login = login |
---|
75 | self.conso = conso |
---|
76 | |
---|
77 | #--------------------------------------- |
---|
78 | def __repr__(self): |
---|
79 | return "{} ({:.2}h)".format(self.login, self.conso) |
---|
80 | |
---|
81 | |
---|
82 | ######################################## |
---|
83 | def plot_init(): |
---|
84 | paper_size = np.array([29.7, 21.0]) |
---|
85 | fig, ax = plt.subplots(figsize=(paper_size/2.54)) |
---|
86 | |
---|
87 | return fig, ax |
---|
88 | |
---|
89 | |
---|
90 | ######################################## |
---|
91 | def plot_data(ax, ycoord, ylabels, consos): |
---|
92 | """ |
---|
93 | """ |
---|
94 | ax.barh(ycoord, consos, align="center", color="linen", |
---|
95 | linewidth=0.2, label="conso (heures)") |
---|
96 | |
---|
97 | |
---|
98 | ######################################## |
---|
99 | def plot_config(ax, ycoord, ylabels, title): |
---|
100 | """ |
---|
101 | """ |
---|
102 | # ... Config axes ... |
---|
103 | # ------------------- |
---|
104 | # 1) Range |
---|
105 | ymin, ymax = ycoord[0]-1, ycoord[-1]+1 |
---|
106 | ax.set_ylim(ymin, ymax) |
---|
107 | |
---|
108 | # 2) Ticks labels |
---|
109 | ax.ticklabel_format(axis="x", style="sci", scilimits=(0, 0)) |
---|
110 | ax.set_yticks(ycoord, minor=False) |
---|
111 | ax.set_yticklabels(ylabels, size="x-small", fontweight="bold") |
---|
112 | ax.invert_yaxis() |
---|
113 | |
---|
114 | # 3) Define axes title |
---|
115 | ax.set_xlabel("heures", fontweight="bold") |
---|
116 | |
---|
117 | # ... Main title and legend ... |
---|
118 | # ----------------------------- |
---|
119 | ax.set_title(title, fontweight="bold", size="large") |
---|
120 | ax.legend(loc="best", fontsize="x-small", frameon=False) |
---|
121 | |
---|
122 | |
---|
123 | ######################################## |
---|
124 | def get_arguments(): |
---|
125 | parser = ArgumentParser() |
---|
126 | parser.add_argument("-v", "--verbose", action="store_true", |
---|
127 | help="Verbose mode") |
---|
128 | parser.add_argument("-f", "--full", action="store_true", |
---|
129 | help="plot all the logins" + |
---|
130 | " (default: plot only non-zero)") |
---|
131 | parser.add_argument("-s", "--show", action="store_true", |
---|
132 | help="interactive mode") |
---|
133 | parser.add_argument("-d", "--dods", action="store_true", |
---|
134 | help="copy output on dods") |
---|
135 | |
---|
136 | return parser.parse_args() |
---|
137 | |
---|
138 | |
---|
139 | ######################################## |
---|
140 | if __name__ == '__main__': |
---|
141 | |
---|
142 | # .. Initialization .. |
---|
143 | # ==================== |
---|
144 | # ... Command line arguments ... |
---|
145 | # ------------------------------ |
---|
146 | args = get_arguments() |
---|
147 | if args.verbose: |
---|
148 | print(args) |
---|
149 | |
---|
150 | # ... Turn interactive mode off ... |
---|
151 | # --------------------------------- |
---|
152 | if not args.show: |
---|
153 | import matplotlib |
---|
154 | matplotlib.use('Agg') |
---|
155 | |
---|
156 | import matplotlib.pyplot as plt |
---|
157 | # from matplotlib.backends.backend_pdf import PdfPages |
---|
158 | |
---|
159 | if not args.show: |
---|
160 | plt.ioff() |
---|
161 | |
---|
162 | # ... Files and directories ... |
---|
163 | # ----------------------------- |
---|
164 | project_name, DIR, OUT = parse_config("bin/config.ini") |
---|
165 | |
---|
166 | (file_param, file_utheo, file_data) = \ |
---|
167 | get_input_files(DIR["SAVEDATA"], |
---|
168 | [OUT["PARAM"], OUT["UTHEO"], OUT["LOGIN"]]) |
---|
169 | |
---|
170 | img_name = "login" |
---|
171 | today = os.path.basename(file_param).strip(OUT["PARAM"]) |
---|
172 | |
---|
173 | if args.verbose: |
---|
174 | print(file_param) |
---|
175 | print(file_utheo) |
---|
176 | print(file_data) |
---|
177 | print(img_name) |
---|
178 | print(today) |
---|
179 | |
---|
180 | # .. Get project info .. |
---|
181 | # ====================== |
---|
182 | projet = Project() |
---|
183 | projet.fill_data(file_param) |
---|
184 | projet.get_date_init(file_utheo) |
---|
185 | |
---|
186 | # .. Fill in data dict .. |
---|
187 | # ======================= |
---|
188 | # ... Initialization ... |
---|
189 | # ---------------------- |
---|
190 | logins = LoginDict() |
---|
191 | logins.fill_data(file_data) |
---|
192 | |
---|
193 | # .. Extract data depending on C.L. arguments .. |
---|
194 | # ============================================== |
---|
195 | if args.full: |
---|
196 | selected_items = logins.get_items() |
---|
197 | else: |
---|
198 | selected_items = logins.get_items_not_null() |
---|
199 | |
---|
200 | if args.verbose: |
---|
201 | for login in selected_items: |
---|
202 | print(login) |
---|
203 | |
---|
204 | # .. Compute data to be plotted .. |
---|
205 | # ================================ |
---|
206 | nb_items = len(selected_items) |
---|
207 | |
---|
208 | ycoord = np.linspace(1, nb_items, num=nb_items) |
---|
209 | ylabels = [item.login for item in selected_items] |
---|
210 | consos = np.array([item.conso for item in selected_items], |
---|
211 | dtype=float) |
---|
212 | date = selected_items[0].date |
---|
213 | |
---|
214 | # .. Plot stuff .. |
---|
215 | # ================ |
---|
216 | # ... Initialize figure ... |
---|
217 | # ------------------------- |
---|
218 | (fig, ax) = plot_init() |
---|
219 | |
---|
220 | # ... Plot data ... |
---|
221 | # ----------------- |
---|
222 | plot_data(ax, ycoord, ylabels, consos) |
---|
223 | |
---|
224 | # ... Tweak figure ... |
---|
225 | # -------------------- |
---|
226 | title = "Consommation {} par login\n{:%d/%m/%Y}".format( |
---|
227 | projet.project.upper(), |
---|
228 | date |
---|
229 | ) |
---|
230 | plot_config(ax, ycoord, ylabels, title) |
---|
231 | |
---|
232 | # ... Save figure ... |
---|
233 | # ------------------- |
---|
234 | img_in = os.path.join(DIR["PLOT"], "{}.pdf".format(img_name)) |
---|
235 | img_out = os.path.join(DIR["SAVEPLOT"], |
---|
236 | "{}_{}.pdf".format(img_name, today)) |
---|
237 | |
---|
238 | plot_save(img_in, img_out, title, DIR) |
---|
239 | |
---|
240 | # ... Publish figure on dods ... |
---|
241 | # ------------------------------ |
---|
242 | if args.dods: |
---|
243 | dods_cp(img_in, DIR) |
---|
244 | |
---|
245 | if args.show: |
---|
246 | plt.show() |
---|
247 | |
---|
248 | exit(0) |
---|
249 | |
---|