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