• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2021 Huawei Technologies Co., Ltd
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14# ============================================================================
15import os
16import sys
17import json
18import matplotlib.ticker as ticker
19import matplotlib.pyplot as plt
20import openpyxl as opx
21
22
23def parse_arguments():
24    log_path = sys.argv[1]
25    log_data = sys.argv[2]
26    me_report = sys.argv[3]
27    n_days = sys.argv[4]
28    assert n_days.isdigit()
29    return log_path, log_data, me_report, int(n_days)
30
31
32def read_data(log_data, me_report_path, n_days):
33    with open(log_data) as f:
34        log = json.load(f)
35
36    wb = opx.load_workbook(me_report_path)
37    sheet = wb["Sheet"]
38    n_row = sheet.max_row
39    date = [cell[0].value for cell in sheet["A2":"A%d" % n_row]]
40    reid_data = [float(cell[0].value) for cell in sheet["B2":"B%d" % n_row]]
41    bert_data = [float(cell[0].value) for cell in sheet["C2":"C%d" % n_row]]
42    resnet_data = [float(cell[0].value) for cell in sheet["D2":"D%d" % n_row]]
43    gpt_data = [float(cell[0].value) for cell in sheet["E43":"E%d" % n_row]]
44    if n_days > 0:
45        date = date[-n_days:]
46        reid_data = reid_data[-n_days:]
47        bert_data = bert_data[-n_days:]
48        resnet_data = resnet_data[-n_days:]
49        gpt_data = gpt_data[-n_days:]
50
51    return log, date, reid_data, bert_data, resnet_data, gpt_data
52
53
54def draw_figure(x_data, y_data, labels, title, out, height=24, width=8, tick_space=2):
55    print("Generating figure to: %s" % out)
56    plt.figure(figsize=(height, width))
57    for y, label in zip(y_data, labels):
58        x = x_data[-len(y):]
59        n_data = len(x)
60        assert len(x) == len(
61            y), "assume len(x) == len(y), while %d != %d" % (len(x), len(y))
62        plt.plot(x, y, linewidth=2, marker='o', markersize=5, label=label)
63        ax = plt.gca()
64        ax.xaxis.set_major_locator(ticker.MultipleLocator(tick_space))
65        for i in range(n_data):
66            if i % 2 == 0:
67                plt.text(x[i], y[i], y[i], ha='center',
68                         va='bottom', fontsize=8)
69
70    plt.title(title)
71    plt.xlabel("Date")
72    plt.ylabel("Time(s)")
73    plt.grid()
74    plt.legend()
75    plt.savefig(out)
76
77
78def generate_report(log, labels, log_path):
79    for label in labels:
80        fname = log[label]["min_file"]
81        fname_path = os.path.join(log_path, fname)
82        out_path = os.path.join(log_path, "reports", label+"_me.log")
83        print("Generating report to: %s" % out_path)
84        os.system("grep -A 230 'TotalTime = ' %s > %s" %
85                  (fname_path, out_path))
86
87
88def process_data():
89    log_path, log_data, me_report, n_days = parse_arguments()
90    log, date, reid_data, bert_data, resnet_data, gpt_data = read_data(
91        log_data, me_report, n_days)
92    draw_figure(date,
93                [reid_data, bert_data, gpt_data],
94                ["ReID", "BERT", "GPT"],
95                "ReID&BERT&GPT",
96                os.path.join(log_path, "reports", "reid_bert_gpt.png")
97                )
98    draw_figure(date, [resnet_data], ["ResNet"], "ResNet",
99                os.path.join(log_path, "reports", "resnet.png"))
100    generate_report(log, list(log.keys()), log_path)
101
102
103if __name__ == "__main__":
104    process_data()
105