source: TOOLS/ConsoGENCMIP6/plot/plot_login.py @ 2411

Last change on this file since 2411 was 2411, checked in by labetoulle, 7 years ago

Add ConsoGENCMIP6 tools

  • Property svn:executable set to *
File size: 8.2 KB
Line 
1#!/usr/bin/env python
2# -*- coding: utf-8 -*-
3
4# this must come first
5from __future__ import print_function, unicode_literals, division
6
7# standard library imports
8from argparse import ArgumentParser
9import os
10import os.path
11import glob
12import datetime as dt
13import numpy as np
14import matplotlib.pyplot as plt
15from matplotlib.backends.backend_pdf import PdfPages
16
17from 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_float(x):
29#   """
30#   """
31#   return float(x.strip("h"))
32
33
34# ########################################
35# def string_to_date(ssaammjj, fmt="%Y-%m-%d"):
36#   """
37#   """
38#   return dt.datetime.strptime(ssaammjj, fmt)
39
40
41# ########################################
42# def date_to_string(dtdate, fmt="%Y-%m-%d"):
43#   """
44#   """
45#   return dt.datetime.strftime(dtdate, fmt)
46
47
48# ########################################
49# def get_last_file(dir_data, pattern):
50#   """
51#   """
52#   current_dir = os.getcwd()
53#   os.chdir(dir_data)
54#   filename = pattern + "*"
55#   return_value = sorted(glob.glob(os.path.join(dir_data, filename)))[-1]
56#   os.chdir(current_dir)
57#   return return_value
58
59
60# ########################################
61# class Project(object):
62
63#   #---------------------------------------
64#   def __init__(self):
65#     self.project   = ""
66#     self.date_init = ""
67#     self.deadline  = ""
68#     self.alloc     = 0
69
70#   #---------------------------------------
71#   def fill_data(self, filein):
72#     import json
73#     dico = json.load(open(filein, "r"))
74#     self.project = dico["project"]
75#     self.deadline = string_to_date(dico["deadline"]) + \
76#                     dt.timedelta(days=-1)
77#     self.alloc = dico["alloc"]
78
79#   #---------------------------------------
80#   def get_date_init(self, filein):
81#     data = np.genfromtxt(
82#       filein,
83#       skip_header=1,
84#       converters={0: string_to_date,
85#                   1: string_to_percent},
86#       missing_values="nan",
87#     )
88#     dates, utheos = zip(*data)
89
90#     (x1, x2) = (np.nanargmin(utheos), np.nanargmax(utheos))
91
92#     m = np.array([[x1, 1.], [x2, 1.]])
93#     n = np.array([utheos[x1], utheos[x2]])
94
95#     (a, b) = np.linalg.solve(m, n)
96
97#     delta = int(round((-b/a)-x1 + 1))
98
99#     d1 = dates[x1]
100#     self.date_init = d1 + dt.timedelta(days=delta)
101
102
103########################################
104class LoginDict(dict):
105  #---------------------------------------
106  def __init__(self):
107    self = {}
108
109  #---------------------------------------
110  def fill_data(self, filein):
111    data = np.genfromtxt(
112      filein,
113      skip_header=1,
114      converters={0: string_to_date,
115                  1: str},
116      missing_values="nan",
117    )
118
119    for date, login, conso in data:
120      self.add_item(date, login, conso)
121
122  #---------------------------------------
123  def add_item(self, date, login, conso):
124    """
125    """
126    self[login] = Login(date, login, conso)
127
128  # #---------------------------------------
129  # def get_items_in_full_range(self, inc=1):
130  #   """
131  #   """
132  #   items = (item for item in self.itervalues())
133  #   items = sorted(items, key=lambda item: item.date)
134
135  #   return items[::inc]
136
137  #---------------------------------------
138  def get_items(self):
139    """
140    """
141    items = (item for item in self.itervalues())
142    items = sorted(items, key=lambda item: item.login)
143
144    return items
145
146  #---------------------------------------
147  def get_items_not_null(self):
148    """
149    """
150    items = (item for item in self.itervalues()
151                   if item.conso > 0.)
152    items = sorted(items, key=lambda item: item.login)
153
154    return items
155
156
157class Login(object):
158  #---------------------------------------
159  def __init__(self, date, login, conso):
160    self.date  = date
161    self.login = login
162    self.conso = conso
163
164  #---------------------------------------
165  def __repr__(self):
166    return "{} ({:.2}h)".format(self.login, self.conso)
167
168  # #---------------------------------------
169  # def isfilled(self):
170  #   return self.filled
171
172  # #---------------------------------------
173  # def fill(self):
174  #   self.filled = True
175
176
177########################################
178def plot_init():
179  paper_size  = np.array([29.7, 21.0])
180  fig, ax = plt.subplots(figsize=(paper_size/2.54))
181
182  return fig, ax
183
184
185########################################
186def plot_data(ax, ycoord, ylabels, consos):
187  """
188  """
189  print(ycoord)
190  print(consos)
191
192  ax.barh(ycoord, consos, align="center", color="linen",
193          linewidth=0.2, label="conso (heures)")
194
195
196########################################
197def plot_config(ax, ycoord, ylabels, title):
198  """
199  """
200  # ... Config axes ...
201  # -------------------
202  # 1) Range
203  ymin, ymax = ycoord[0]-1, ycoord[-1]+1
204  ax.set_ylim(ymin, ymax)
205
206  # 2) Ticks labels
207  ax.ticklabel_format(axis="x", style="sci", scilimits=(0, 0))
208  ax.set_yticks(ycoord, minor=False)
209  ax.set_yticklabels(ylabels, size="x-small", fontweight="bold")
210  ax.invert_yaxis()
211
212  # 3) Define axes title
213  ax.set_xlabel("heures", fontweight="bold")
214
215  # ... Main title and legend ...
216  # -----------------------------
217  ax.set_title(title, fontweight="bold", size="large")
218  ax.legend(loc="best", fontsize="x-small", frameon=False)
219
220
221########################################
222def plot_save(img_name):
223  """
224  """
225  dpi = 200.
226
227  with PdfPages(img_name) as pdf:
228    pdf.savefig(dpi=dpi)
229
230    # pdf file's metadata
231    d = pdf.infodict()
232    d["Title"]   = "Conso GENCMIP6 par login"
233    d["Author"]  = "plot_bilan.py"
234    # d["Subject"] = "Time spent over specific commands during create_ts \
235    #                 jobs at IDRIS and four configurations at TGCC"
236    # d["Keywords"] = "bench create_ts TGCC IDRIS ncrcat"
237    # d["CreationDate"] = dt.datetime(2009, 11, 13)
238    # d["ModDate"] = dt.datetime.today()
239
240
241########################################
242def get_arguments():
243  parser = ArgumentParser()
244  parser.add_argument("-v", "--verbose", action="store_true",
245                      help="Verbose mode")
246  parser.add_argument("-f", "--full", action="store_true",
247                      help="plot all the logins" +
248                           " (default: plot only non-zero)")
249
250  return parser.parse_args()
251
252
253########################################
254if __name__ == '__main__':
255
256  # .. Initialization ..
257  # ====================
258  # ... Command line arguments ...
259  # ------------------------------
260  args = get_arguments()
261
262  # ... Files and directories ...
263  # -----------------------------
264  dir_data = os.path.join("..", "output")
265  file_pattern = "OUT_CONSO_"
266  file_param = get_last_file(dir_data, file_pattern+"PARAM")
267  file_utheo = get_last_file(dir_data, file_pattern+"UTHEO")
268  file_bilan = get_last_file(dir_data, file_pattern+"BILAN")
269  file_login = get_last_file(dir_data, file_pattern+"LOGIN")
270  file_store = get_last_file(dir_data, file_pattern+"STORE")
271
272  # .. Get project info ..
273  # ======================
274  gencmip6 = Project()
275  gencmip6.fill_data(file_param)
276  gencmip6.get_date_init(file_utheo)
277
278  # .. Fill in data dict ..
279  # =======================
280  # ... Initialization ...
281  # ----------------------
282  logins = LoginDict()
283  logins.fill_data(file_login)
284
285  # .. Extract data depending on C.L. arguments ..
286  # ==============================================
287  if args.full:
288    selected_items = logins.get_items()
289  else:
290    selected_items = logins.get_items_not_null()
291
292  if args.verbose:
293    for login in selected_items:
294      print(login)
295
296  # .. Compute data to be plotted ..
297  # ================================
298  nb_items = len(selected_items)
299
300  ycoord  = np.linspace(1, nb_items, num=nb_items)
301  ylabels = [item.login for item in selected_items]
302  consos  = np.array([item.conso for item in selected_items],
303                      dtype=float)
304  date = selected_items[0].date
305
306  # .. Plot stuff ..
307  # ================
308  # ... Initialize figure ...
309  # -------------------------
310  (fig, ax) = plot_init()
311
312  # ... Plot data ...
313  # -----------------
314  plot_data(ax, ycoord, ylabels, consos)
315
316  # # ... Tweak figure ...
317  # # --------------------
318  title = "Consommation {} par login\n{:%d/%m/%Y}".format(
319    gencmip6.project.upper(),
320    date
321  )
322  plot_config(ax, ycoord, ylabels, title)
323
324  # ... Save figure ...
325  # -------------------
326  dirout = "img"
327  img_name = "login.pdf"
328  plot_save(os.path.join(dirout, img_name))
329
330  plt.show()
331  exit()
Note: See TracBrowser for help on using the repository browser.