plot_statistics.py 2.93 KB
Newer Older
1
from datetime import datetime
niklas.baumgarten's avatar
niklas.baumgarten committed
2
import pandas as pd
3
import sys
niklas.baumgarten's avatar
niklas.baumgarten committed
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import re

L1 = " l     M    E[Qf-Qc]       E[Qf]    V[Qf-Qc]       V[Qf]    kurtosis        cost\n"
L2 = "--------------------------------------------------------------------------------\n"


def read_log():
    log_file = '../build/log/log'
    with open(log_file) as file:
        content = file.readlines()

    float_re = r'[+-]?[0-9]+[.]?[0-9]*[eE]?[+-]?[0-9]*'
    pattern = re.compile(float_re)

    columns = ["l", "M", "E[Qf-Qc]", "E[Qf]", "V[Qf-Qc]", "V[Qf]", "kurtosis", "cost"]
    df = pd.DataFrame(columns=columns)

    ctr = 0
    for line in content:
        if line == L1:
            ctr += 1
            continue
        if 0 < ctr < 3:
            if line == L2:
                ctr += 1
                continue
            df_row = []
            matches = pattern.findall(line)
            df_row.append(int(matches[0]))  # l
            df_row.append(int(matches[1]))  # M
            df_row.append(float(matches[2]))  # E[Qf-Qc]
            df_row.append(float(matches[3]))  # E[Qf]
            df_row.append(float(matches[4]))  # V[Qf-Qc]
            df_row.append(float(matches[5]))  # V[Qf]
            df_row.append(float(matches[6]))  # kurtosis
            df_row.append(int(matches[7]))  # cost

            df_to_append = pd.DataFrame([df_row], columns=columns)
            df = df.append(df_to_append)

    if ctr != 3:
        print("No statistics table found in logfile")
        return df

    df["l"] = pd.to_numeric(df["l"], downcast='integer')
    df = df.set_index('l')
    return df


53
def plot_content(df, plot_name):
niklas.baumgarten's avatar
niklas.baumgarten committed
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
    if df.empty:
        return

    import matplotlib.pyplot as plt

    fig = plt.figure(figsize=(13, 5))

    # Expectation value plot
    ax1 = fig.add_subplot(1, 2, 1)
    plt.plot(df.index, df['E[Qf]'], ls='-', label='log(E[Qf])',
             linewidth=2, marker='o', markersize=6)

    plt.plot(df.index[1:], df['E[Qf-Qc]'][1:], ls='--', label='log(E[Qf-Qc])',
             linewidth=2, marker='*', markersize=6)

    plt.title('log(E[.]) vs level')
    plt.ylabel('log(E[.])')
    plt.xlabel('level')
    plt.yscale('log')
    plt.legend()

    ax1.set_xticks(df.index)
    ax1.grid(which='major')

    # Variance plot
    ax2 = fig.add_subplot(1, 2, 2)
    plt.plot(df.index, df['V[Qf]'], ls='-', label='log(V[Qf])',
             linewidth=2, marker='o', markersize=6)

    plt.plot(df.index[1:], df['V[Qf-Qc]'][1:], ls='--', label='log(V[Qf-Qc])',
             linewidth=2, marker='*', markersize=6)

    plt.title('log(V[.]) vs level')
    plt.ylabel('log(V[.])')
    plt.xlabel('level')
    plt.yscale('log')
    plt.legend()

    ax2.set_xticks(df.index)
    ax2.grid(which='major')

95
96
97
    now = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
    path = '../build/data/py/Statistics{} {}.png'.format(plot_name, now)
    plt.savefig(path)
niklas.baumgarten's avatar
niklas.baumgarten committed
98
99


100
101
102
if __name__ == '__main__':
    argv = sys.argv
    argv1 = argv[1]
niklas.baumgarten's avatar
niklas.baumgarten committed
103
    statistics = read_log()
104
    plot_content(statistics, argv1)