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 glob |
---|
12 | # import datetime as dt |
---|
13 | import numpy as np |
---|
14 | import matplotlib.pyplot as plt |
---|
15 | from matplotlib.backends.backend_pdf import PdfPages |
---|
16 | |
---|
17 | from gencmip6 import * |
---|
18 | |
---|
19 | |
---|
20 | # ######################################## |
---|
21 | # def string_to_percent(x): |
---|
22 | # """ |
---|
23 | # """ |
---|
24 | # return float(x.strip("%"))/100. |
---|
25 | |
---|
26 | |
---|
27 | # ######################################## |
---|
28 | # def string_to_size_unit(x): |
---|
29 | # """ |
---|
30 | # """ |
---|
31 | # (size, unit) = (float(x[:-1]), x[-1]) |
---|
32 | # return SizeUnit(size, unit) |
---|
33 | |
---|
34 | |
---|
35 | # ######################################## |
---|
36 | # def string_to_float(x): |
---|
37 | # """ |
---|
38 | # """ |
---|
39 | # return float(x.strip("h")) |
---|
40 | |
---|
41 | |
---|
42 | # ######################################## |
---|
43 | # def string_to_date(ssaammjj, fmt="%Y-%m-%d"): |
---|
44 | # """ |
---|
45 | # """ |
---|
46 | # return dt.datetime.strptime(ssaammjj, fmt) |
---|
47 | |
---|
48 | |
---|
49 | # ######################################## |
---|
50 | # def date_to_string(dtdate, fmt="%Y-%m-%d"): |
---|
51 | # """ |
---|
52 | # """ |
---|
53 | # return dt.datetime.strftime(dtdate, fmt) |
---|
54 | |
---|
55 | |
---|
56 | # ######################################## |
---|
57 | # def get_last_file(dir_data, pattern): |
---|
58 | # """ |
---|
59 | # """ |
---|
60 | # current_dir = os.getcwd() |
---|
61 | # os.chdir(dir_data) |
---|
62 | # filename = pattern + "*" |
---|
63 | # return_value = sorted(glob.glob(os.path.join(dir_data, filename)))[-1] |
---|
64 | # os.chdir(current_dir) |
---|
65 | # return return_value |
---|
66 | |
---|
67 | |
---|
68 | # ######################################## |
---|
69 | # class Project(object): |
---|
70 | |
---|
71 | # #--------------------------------------- |
---|
72 | # def __init__(self): |
---|
73 | # self.project = "" |
---|
74 | # self.date_init = "" |
---|
75 | # self.deadline = "" |
---|
76 | # self.alloc = 0 |
---|
77 | |
---|
78 | # #--------------------------------------- |
---|
79 | # def fill_data(self, filein): |
---|
80 | # import json |
---|
81 | # dico = json.load(open(filein, "r")) |
---|
82 | # self.project = dico["project"] |
---|
83 | # self.deadline = string_to_date(dico["deadline"]) + \ |
---|
84 | # dt.timedelta(days=-1) |
---|
85 | # self.alloc = dico["alloc"] |
---|
86 | |
---|
87 | # #--------------------------------------- |
---|
88 | # def get_date_init(self, filein): |
---|
89 | # data = np.genfromtxt( |
---|
90 | # filein, |
---|
91 | # skip_header=1, |
---|
92 | # converters={0: string_to_date, |
---|
93 | # 1: string_to_percent}, |
---|
94 | # missing_values="nan", |
---|
95 | # ) |
---|
96 | # dates, utheos = zip(*data) |
---|
97 | |
---|
98 | # (x1, x2) = (np.nanargmin(utheos), np.nanargmax(utheos)) |
---|
99 | |
---|
100 | # m = np.array([[x1, 1.], [x2, 1.]]) |
---|
101 | # n = np.array([utheos[x1], utheos[x2]]) |
---|
102 | |
---|
103 | # (a, b) = np.linalg.solve(m, n) |
---|
104 | |
---|
105 | # delta = int(round((-b/a)-x1 + 1)) |
---|
106 | |
---|
107 | # d1 = dates[x1] |
---|
108 | # self.date_init = d1 + dt.timedelta(days=delta) |
---|
109 | |
---|
110 | |
---|
111 | # ######################################## |
---|
112 | # class SizeUnit(object): |
---|
113 | # #--------------------------------------- |
---|
114 | # def __init__(self, size, unit): |
---|
115 | # self.size = size |
---|
116 | # self.unit = unit |
---|
117 | |
---|
118 | # #--------------------------------------- |
---|
119 | # def __repr__(self): |
---|
120 | # return "{:6.2f}{}o".format(self.size, self.unit) |
---|
121 | |
---|
122 | # #--------------------------------------- |
---|
123 | # def convert_size(self, unit_out): |
---|
124 | # """ |
---|
125 | # """ |
---|
126 | # prefixes = ["K", "M", "G", "T", "P", "H"] |
---|
127 | |
---|
128 | # if not self.size or \ |
---|
129 | # self.unit == unit_out: |
---|
130 | # size_out = self.size |
---|
131 | # else: |
---|
132 | # idx_deb = prefixes.index(self.unit) |
---|
133 | # idx_fin = prefixes.index(unit_out) |
---|
134 | # size_out = self.size |
---|
135 | # for i in xrange(abs(idx_fin-idx_deb)): |
---|
136 | # if idx_fin > idx_deb: |
---|
137 | # size_out = size_out / 1024 |
---|
138 | # else: |
---|
139 | # size_out = size_out * 1024 |
---|
140 | |
---|
141 | # return SizeUnit(size_out, unit_out) |
---|
142 | |
---|
143 | |
---|
144 | ######################################## |
---|
145 | class DirVolume(object): |
---|
146 | #--------------------------------------- |
---|
147 | def __init__(self, date, login, dirname, size): |
---|
148 | self.date = date |
---|
149 | self.login = login |
---|
150 | self.dirname = dirname |
---|
151 | self.dirsize = size |
---|
152 | |
---|
153 | #--------------------------------------- |
---|
154 | def __repr__(self): |
---|
155 | return "{}={}".format(self.dirname, self.dirsize) |
---|
156 | |
---|
157 | |
---|
158 | ######################################## |
---|
159 | class StoreDict(dict): |
---|
160 | #--------------------------------------- |
---|
161 | def __init__(self): |
---|
162 | self = {} |
---|
163 | |
---|
164 | #--------------------------------------- |
---|
165 | def fill_data(self, filein): |
---|
166 | data = np.genfromtxt( |
---|
167 | filein, |
---|
168 | skip_header=1, |
---|
169 | converters={0: string_to_date, |
---|
170 | 1: str, |
---|
171 | 2: string_to_size_unit, |
---|
172 | 3: str}, |
---|
173 | missing_values="nan", |
---|
174 | ) |
---|
175 | |
---|
176 | for date, login, dirsize, dirname in data: |
---|
177 | self.add_item(date, login, dirsize, dirname) |
---|
178 | |
---|
179 | #--------------------------------------- |
---|
180 | def add_item(self, date, login, dirsize, dirname): |
---|
181 | """ |
---|
182 | """ |
---|
183 | if login not in self: |
---|
184 | self[login] = Login(date, login) |
---|
185 | self[login].add_directory(date, login, dirsize, dirname) |
---|
186 | |
---|
187 | #--------------------------------------- |
---|
188 | def get_items(self): |
---|
189 | """ |
---|
190 | """ |
---|
191 | items = (subitem for item in self.itervalues() |
---|
192 | for subitem in item.listdir) |
---|
193 | items = sorted(items, key=lambda item: item.login) |
---|
194 | |
---|
195 | return items |
---|
196 | |
---|
197 | #--------------------------------------- |
---|
198 | def get_items_by_name(self, pattern): |
---|
199 | """ |
---|
200 | """ |
---|
201 | # items = (item for item in self.itervalues() if item.dir) |
---|
202 | items = (subitem for item in self.itervalues() |
---|
203 | for subitem in item.listdir |
---|
204 | if pattern in subitem.dirname) |
---|
205 | items = sorted(items, key=lambda item: item.login) |
---|
206 | |
---|
207 | return items |
---|
208 | |
---|
209 | |
---|
210 | ######################################## |
---|
211 | class Login(object): |
---|
212 | #--------------------------------------- |
---|
213 | def __init__(self, date, login): |
---|
214 | self.date = date |
---|
215 | self.login = login |
---|
216 | self.total = SizeUnit(0., "K") |
---|
217 | self.listdir = [] |
---|
218 | |
---|
219 | #--------------------------------------- |
---|
220 | def __repr__(self): |
---|
221 | return "{}/{:%F}: {}".format(self.login, self.date, self.listdir) |
---|
222 | |
---|
223 | #--------------------------------------- |
---|
224 | def add_to_total(self, dirsize): |
---|
225 | """ |
---|
226 | """ |
---|
227 | somme = self.total.convert_size("K").size + \ |
---|
228 | dirsize.convert_size("K").size |
---|
229 | self.total = SizeUnit(somme, "K") |
---|
230 | |
---|
231 | #--------------------------------------- |
---|
232 | def add_directory(self, date, login, dirsize, dirname): |
---|
233 | """ |
---|
234 | """ |
---|
235 | self.listdir.append(DirVolume(date, login, dirname, dirsize)) |
---|
236 | self.add_to_total(dirsize) |
---|
237 | |
---|
238 | |
---|
239 | ######################################## |
---|
240 | def plot_init(): |
---|
241 | paper_size = np.array([29.7, 21.0]) |
---|
242 | fig, ax = plt.subplots(figsize=(paper_size/2.54)) |
---|
243 | |
---|
244 | return fig, ax |
---|
245 | |
---|
246 | |
---|
247 | ######################################## |
---|
248 | def plot_data(ax, coords, ylabels, values): |
---|
249 | """ |
---|
250 | """ |
---|
251 | ax.barh(coords, values, align="center", color="linen", |
---|
252 | linewidth=0.2, label="volume sur STORE ($To$)") |
---|
253 | |
---|
254 | |
---|
255 | ######################################## |
---|
256 | def plot_config(ax, coords, ylabels, dirnames, title, tot_volume): |
---|
257 | """ |
---|
258 | """ |
---|
259 | # ... Config axes ... |
---|
260 | # ------------------- |
---|
261 | # 1) Range |
---|
262 | ymin, ymax = coords[0]-1, coords[-1]+1 |
---|
263 | ax.set_ylim(ymin, ymax) |
---|
264 | |
---|
265 | # 2) Ticks labels |
---|
266 | ax.ticklabel_format(axis="x", style="sci", scilimits=(0, 0)) |
---|
267 | ax.set_yticks(coords, minor=False) |
---|
268 | ax.set_yticklabels(ylabels, size="x-small", fontweight="bold") |
---|
269 | ax.invert_yaxis() |
---|
270 | |
---|
271 | xmin, xmax = ax.get_xlim() |
---|
272 | xpos = xmin + (xmax-xmin)/50. |
---|
273 | for (ypos, text) in zip(coords, dirnames): |
---|
274 | ax.text(s=text, x=xpos, y=ypos, va="center", ha="left", |
---|
275 | size="xx-small", color="gray", style="italic") |
---|
276 | |
---|
277 | # 3) Define axes title |
---|
278 | ax.set_xlabel("$To$", fontweight="bold") |
---|
279 | |
---|
280 | # ... Main title and legend ... |
---|
281 | # ----------------------------- |
---|
282 | ax.set_title(title, fontweight="bold", size="large") |
---|
283 | ax.legend(loc="best", fontsize="x-small", frameon=False) |
---|
284 | |
---|
285 | tot_label = "volume total = {}".format(tot_volume) |
---|
286 | plt.figtext(x=0.95, y=0.93, s=tot_label, backgroundcolor="linen", |
---|
287 | ha="right", va="bottom", fontsize="small") |
---|
288 | |
---|
289 | |
---|
290 | ######################################## |
---|
291 | def plot_save(img_name): |
---|
292 | """ |
---|
293 | """ |
---|
294 | dpi = 200. |
---|
295 | |
---|
296 | with PdfPages(img_name) as pdf: |
---|
297 | pdf.savefig(dpi=dpi) |
---|
298 | |
---|
299 | # pdf file's metadata |
---|
300 | d = pdf.infodict() |
---|
301 | d["Title"] = "Occupation GENCMIP6 sur STORE par login" |
---|
302 | d["Author"] = "plot_bilan.py" |
---|
303 | # d["Subject"] = "Time spent over specific commands during create_ts \ |
---|
304 | # jobs at IDRIS and four configurations at TGCC" |
---|
305 | # d["Keywords"] = "bench create_ts TGCC IDRIS ncrcat" |
---|
306 | # d["CreationDate"] = dt.datetime(2009, 11, 13) |
---|
307 | # d["ModDate"] = dt.datetime.today() |
---|
308 | |
---|
309 | |
---|
310 | ######################################## |
---|
311 | def get_arguments(): |
---|
312 | parser = ArgumentParser() |
---|
313 | parser.add_argument("-v", "--verbose", action="store_true", |
---|
314 | help="Verbose mode") |
---|
315 | parser.add_argument("-f", "--full", action="store_true", |
---|
316 | help="plot all the directories in IGCM_OUT" + |
---|
317 | "(default: plot IPSLCM6 directories)") |
---|
318 | parser.add_argument("-p", "--pattern", action="store", |
---|
319 | default="IPSLCM6", |
---|
320 | help="plot the whole period") |
---|
321 | |
---|
322 | return parser.parse_args() |
---|
323 | |
---|
324 | |
---|
325 | ######################################## |
---|
326 | if __name__ == '__main__': |
---|
327 | |
---|
328 | # .. Initialization .. |
---|
329 | # ==================== |
---|
330 | # ... Command line arguments ... |
---|
331 | # ------------------------------ |
---|
332 | args = get_arguments() |
---|
333 | if args.verbose: |
---|
334 | print(args) |
---|
335 | |
---|
336 | # ... Files and directories ... |
---|
337 | # ----------------------------- |
---|
338 | dir_data = os.path.join("..", "output") |
---|
339 | file_pattern = "OUT_CONSO_" |
---|
340 | file_param = get_last_file(dir_data, file_pattern+"PARAM") |
---|
341 | file_utheo = get_last_file(dir_data, file_pattern+"UTHEO") |
---|
342 | file_bilan = get_last_file(dir_data, file_pattern+"BILAN") |
---|
343 | file_login = get_last_file(dir_data, file_pattern+"LOGIN") |
---|
344 | file_store = get_last_file(dir_data, file_pattern+"STORE") |
---|
345 | |
---|
346 | # .. Get project info .. |
---|
347 | # ====================== |
---|
348 | gencmip6 = Project() |
---|
349 | gencmip6.fill_data(file_param) |
---|
350 | gencmip6.get_date_init(file_utheo) |
---|
351 | |
---|
352 | # .. Fill in data dict .. |
---|
353 | # ======================= |
---|
354 | stores = StoreDict() |
---|
355 | stores.fill_data(file_store) |
---|
356 | |
---|
357 | # .. Extract data depending on C.L. arguments .. |
---|
358 | # ============================================== |
---|
359 | if args.full: |
---|
360 | selected_items = stores.get_items() |
---|
361 | else: |
---|
362 | selected_items = stores.get_items_by_name(args.pattern) |
---|
363 | |
---|
364 | if args.verbose: |
---|
365 | for item in selected_items: |
---|
366 | print( |
---|
367 | "{:8s} {:%F} {} {:>18s} {} ".format( |
---|
368 | item.login, |
---|
369 | item.date, |
---|
370 | item.dirsize, |
---|
371 | item.dirsize.convert_size("K"), |
---|
372 | item.dirname, |
---|
373 | ) |
---|
374 | ) |
---|
375 | |
---|
376 | # .. Compute data to be plotted .. |
---|
377 | # ================================ |
---|
378 | ylabels = [item.login for item in selected_items] |
---|
379 | values = np.array([item.dirsize.convert_size("T").size |
---|
380 | for item in selected_items], |
---|
381 | dtype=float) |
---|
382 | dirnames = [item.dirname for item in selected_items] |
---|
383 | date = selected_items[0].date |
---|
384 | |
---|
385 | nb_items = len(ylabels) |
---|
386 | coords = np.linspace(1, nb_items, num=nb_items) |
---|
387 | |
---|
388 | # .. Plot stuff .. |
---|
389 | # ================ |
---|
390 | # ... Initialize figure ... |
---|
391 | # ------------------------- |
---|
392 | (fig, ax) = plot_init() |
---|
393 | |
---|
394 | # ... Plot data ... |
---|
395 | # ----------------- |
---|
396 | plot_data(ax, coords, ylabels, values) |
---|
397 | |
---|
398 | # ... Tweak figure ... |
---|
399 | # -------------------- |
---|
400 | title = "Occupation {} de STORE par login\n{:%d/%m/%Y}".format( |
---|
401 | gencmip6.project.upper(), |
---|
402 | date |
---|
403 | ) |
---|
404 | plot_config(ax, coords, ylabels, dirnames, title, |
---|
405 | SizeUnit(np.sum(values), "T")) |
---|
406 | |
---|
407 | # ... Save figure ... |
---|
408 | # ------------------- |
---|
409 | dirout = "img" |
---|
410 | img_name = "store.pdf" |
---|
411 | plot_save(os.path.join(dirout, img_name)) |
---|
412 | |
---|
413 | plt.show() |
---|
414 | exit() |
---|