parse.py 2.86 KB
Newer Older
1
import pandas as pd
2

3
4
5
6

def _stamp_name_by_src_and_type(all_stamps, src, kind=None):
    if kind is None:
        kind = ["time", "cycle"]
7
    return [c for c, v in all_stamps.items() if v["Source"] == src and v["Type"] in kind]
8

9

10
11
def _extract_stamps_by_type(all_stamps, src, kind=None):
    columns = _stamp_name_by_src_and_type(all_stamps, src, kind)
12
    return [x + "_T" for x in columns] + [x + "_C" for x in columns]
13

14
15

def _evaluate_file(file_name, stamps, kind, sender=False):
16
    # Remove first line, as this is the dummy line for intermittently storing data.
17
    df = pd.read_csv(file_name)[1:]
18

19
    df = df[df["Kind"] == kind].drop(["Kind"], axis=1).set_index("SeqNo")
20
21

    # Drop columns of opposing side.
22
    if sender:
23
        df.drop(_extract_stamps_by_type(stamps, "receiver"), axis=1, inplace=True)
24
    else:
25
        df.drop(_extract_stamps_by_type(stamps, "sender"), axis=1, inplace=True)
26
27
28

    # Drop empty rows (as they have probably not been written out).
    return df[pd.notnull(df).all(axis=1)]
29
30


31
def _diff_t_c(df, start, stop):
32
33
    time = df[stop + "_T"] - df[start + "_T"]
    cycles = (df[stop + "_C"] - df[start + "_C"])
34
    return time.astype(float), cycles.astype(float)
35
36


37
def evaluate(sender_file, receiver_file, config, kind=0):
38
39
40
    stamps = config["stamps"]
    df1 = _evaluate_file(sender_file, stamps, kind, True)
    df2 = _evaluate_file(receiver_file, stamps, kind)
41
42
    df = df1.join(df2)

43
44
    tr = config["time_reference"]
    cr = config["cycle_reference"]
45

46
47
    # Determine Channel Duration
    df["Channel_D"] = df[tr["receiver"]["Start"] + "_T"] - df[tr["sender"]["Stop"] + "_T"]
48

49
50
51
    # Correlate Receiver Timestamps with Sender Timestamps (subtracting Channel Duration)
    for s in _stamp_name_by_src_and_type(stamps, "receiver", kind=["time"]):
        df[s + "_T"] -= df["Channel_D"]
52

53
54
55
    for src in ["sender", "receiver"]:
        # Generate Processing Duration
        src_name = src.capitalize()
56

57
        time, cycles = _diff_t_c(df, tr[src]["Start"], tr[src]["Stop"])
58
59
        df[src_name + "_D"] = time
        df[src_name + "_C"] = cycles
60

61
        # Generate Cycle Times
62
63
        time, cycles = _diff_t_c(df, cr[src]["Start"], cr[src]["Stop"])
        df[src_name + "Cycle_D"] = time / cycles
64

65
66
67
68
69
        # Recreate missing timestamps from cycles
        for stamp_name in _stamp_name_by_src_and_type(stamps, src, "cycle"):
            start_stamp = tr[src]["Start"]
            diff = df[stamp_name + "_C"] - df[start_stamp + "_C"]
            df[stamp_name + "_T"] = (diff * df[src_name + "Cycle_D"] + df[start_stamp + "_T"]).astype(int)
70

71
72
73
74
75
76
77
    # Generate Durations
    for name, duration in config["durations"].items():
        diff = df[duration["Stop"] + "_C"] - df[duration["Start"] + "_C"]
        df[name + "Cycles"] = diff
        df[name + "_D"] = diff * df[duration["Source"].capitalize() + "Cycle_D"]

    df["EndToEndTime"] = df["Sender_D"] + df["Receiver_D"]
78
79

    return df