jitter.py 1.92 KB
Newer Older
1
2
import pandas as pd
import matplotlib.pyplot as plt
3
from xlap.analyse.util import get_outlier_threshold, extract_durations, box
4

5

Andreas Schmidt's avatar
Andreas Schmidt committed
6
def jitter_causes(df, config, export=False, file_name=None, figsize=(8,3), color="black"):
7
    stats = df["EndToEnd_D"].describe()
Andreas Schmidt's avatar
Andreas Schmidt committed
8
    durations = extract_durations(config)
9
    threshold = get_outlier_threshold(stats)
10
    outliers = df[df["EndToEnd_D"] > threshold]
11

Andreas Schmidt's avatar
Andreas Schmidt committed
12
    reasons = [d + "_D" for d in durations if d != "EndToEnd"]
13
14
15

    df_reasons = pd.DataFrame(index=outliers.index)

16
17
18
19
    for reason in reasons:
        reason_threshold = get_outlier_threshold(df[reason].describe())
        df_reasons[reason] = 0
        df_reasons[reason] = outliers[outliers[reason] > reason_threshold].notnull()
20
21

    df_sum = df_reasons.sum().sort_values(ascending=False)
Andreas Schmidt's avatar
Andreas Schmidt committed
22
    ax = df_sum.plot.bar(x="Reason", y="Frequency", rot=45, grid=True, legend=False, color=color)
23
    fig = ax.get_figure()
24
    plt.ylabel("Frequency")
25
    ax.set_xticklabels(list(map(lambda x: x.get_text().replace("_D", ""), ax.get_xticklabels())))
Andreas Schmidt's avatar
Andreas Schmidt committed
26
27
    fig.set_size_inches(figsize[0], figsize[1])
    plt.tight_layout()
Andreas Schmidt's avatar
Andreas Schmidt committed
28
29
    if export:
        fig.savefig(file_name)
30
    print("Outliers:", len(outliers), ";", "Threshold[us]:", threshold)
31

Andreas Schmidt's avatar
Andreas Schmidt committed
32
def trace_jitter(data_frame, config=None, threshold=None, export=False, file_name=None, figsize=(8, 4.5)):
33
34
35
    """
    Displays (and saves) a stacked boxplot of durations.
    """
36
37
    data_frame = data_frame[[x + "_D" for x in extract_durations(config)]]

38
39
40
    if threshold is None:
        threshold = get_outlier_threshold(data_frame["EndToEnd_D"].describe())
    df_no_outliers = data_frame[data_frame["EndToEnd_D"] <= threshold]
41
    fig = plt.gcf()
Andreas Schmidt's avatar
Andreas Schmidt committed
42
43
44
    fig.set_size_inches(figsize[0], figsize[1])
    box(df_no_outliers, (0, threshold), export, file_name, figsize)
    print("{} / {} are no outliers.".format(len(df_no_outliers), len(data_frame)))
45
46
    fig.canvas.set_window_title('Jitter Analysis')
    plt.show()
Andreas Schmidt's avatar
Andreas Schmidt committed
47
    return None