trace.py 2.96 KB
Newer Older
1 2
import numpy as np
import matplotlib.pyplot as plt
Andreas Schmidt's avatar
Andreas Schmidt committed
3 4
import matplotlib.patches as mpatches
from matplotlib.lines import Line2D
5

6
from xlap.analyse.util import extract_durations
7 8
from ipywidgets import interact
import ipywidgets as widgets
9 10


Andreas Schmidt's avatar
Andreas Schmidt committed
11 12 13 14 15 16 17 18 19 20 21 22 23
colors = {
    "sender": "#CA0020",
    "receiver": "#0571B0",
    "EndToEnd": "black"
}

linestyles = {
    "sender": (0,(1,1,3,1,1,1)),
    "receiver": (0,(1,1)),
    "EndToEnd": (0,())
}


24 25 26 27 28
def _create_line(config):
    tr = config["time_reference"]

    def _creator(duration_name):
        if duration_name == "EndToEnd":
Andreas Schmidt's avatar
Andreas Schmidt committed
29
            return [tr["sender"]["Start"] + "_T", tr["receiver"]["Stop"] + "_T", colors["EndToEnd"], linestyles["EndToEnd"], "EndToEnd"]
30
        elif duration_name == "Sender":
Andreas Schmidt's avatar
Andreas Schmidt committed
31
            return [tr["sender"]["Start"] + "_T", tr["sender"]["Stop"] + "_T", colors["sender"], linestyles["sender"], "Sender"]
32
        elif duration_name == "Receiver":
Andreas Schmidt's avatar
Andreas Schmidt committed
33
            return [tr["receiver"]["Start"] + "_T", tr["receiver"]["Stop"] + "_T", colors["receiver"], linestyles["receiver"], "Receiver"]
34 35
        else:
            duration = config["durations"][duration_name]
Andreas Schmidt's avatar
Andreas Schmidt committed
36
            return [duration["Start"] + "_T", duration["Stop"] + "_T", colors[duration["Source"]], linestyles[duration["Source"]], duration_name]
37 38 39 40

    return _creator


Andreas Schmidt's avatar
Andreas Schmidt committed
41
def trace(data_frame, config, export=False, file_name="PacketTrace.pdf", figsize=(8, 4.5), bbox_to_anchor=(1.05, 1), loc=2, t_max=None):
42 43 44 45 46 47 48 49 50
    """

    :param data_frame:
    :param config:
    :param export:
    :param file_name:
    :return:
    """

Andreas Schmidt's avatar
Andreas Schmidt committed
51
    fig, ax = plt.subplots(figsize=figsize)
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
    plt.grid()

    line_creator = _create_line(config)

    durations = [line_creator(x) for x in extract_durations(config)]

    series = np.transpose(np.array(durations))
    n = series.shape[1]

    # Starts and Ends
    tr = config["time_reference"]
    base = data_frame[tr["sender"]["Start"] + "_T"]
    starts = data_frame[series[0]] - base
    ends = data_frame[series[1]] - base

Andreas Schmidt's avatar
Andreas Schmidt committed
67
    plt.hlines(range(n), starts, ends, series[2], series[3], linewidths=[5])
68
    plt.xlabel("Time [us]")
Andreas Schmidt's avatar
Andreas Schmidt committed
69 70
    if t_max is not None:
        ax.set_xlim([0,t_max])
71 72
    fig.canvas.draw()

Andreas Schmidt's avatar
Andreas Schmidt committed
73
    ax.set_yticklabels(series[4])
74 75
    ax.yaxis.set_ticks(np.arange(0, n, 1))

Andreas Schmidt's avatar
Andreas Schmidt committed
76 77 78 79 80 81
    sides = ["sender","receiver","EndToEnd"]
    handles = list(map(lambda x: Line2D([0], [0], color=colors[x], linestyle=linestyles[x], lw=5, label=x.title() if x != "EndToEnd" else x), sides))

    plt.legend(handles=handles,bbox_to_anchor=bbox_to_anchor, loc=loc, borderaxespad=0.)

    plt.tight_layout()
82 83 84
    if export:
        plt.savefig(file_name)
    plt.show()
85

86

Andreas Schmidt's avatar
Andreas Schmidt committed
87
def traces(df, config, figsize=(10, 4.5), global_xaxis=False):
88 89 90
    """
    Display a slider to select sequence numbers and the respective trace.
    """
Andreas Schmidt's avatar
Andreas Schmidt committed
91 92 93
    t_max = None
    if global_xaxis:
        t_max = df["EndToEnd_D"].max()
94

Andreas Schmidt's avatar
Andreas Schmidt committed
95
    @interact(seq_no=widgets.IntSlider(min=1, max=len(df), step=1, value=47,description="Seq. No."))
96
    def _f(seq_no):
Andreas Schmidt's avatar
Andreas Schmidt committed
97
        trace(df.iloc[seq_no], config, figsize=figsize, t_max=t_max)