parse.py 3.54 KB
Newer Older
1
2
3
4
5
6
7
import numpy as np
import math
import pandas as pd
import collections
from operator import itemgetter
from sklearn import datasets, linear_model

8
9
10
11
12
13
14
15
16
17
18
19
20
21
def _extract_duration_by_src(all_durations, src):
    return [c for c,v in all_durations.items() if v["Source"] == src]

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

def _extract_stamps_by_type(all_stamps, src, kind=None):
    columns = _stamp_name_by_src_and_type(all_stamps, src, kind)
    return _stamp_names(columns)

def _stamp_names(columns):
22
23
24
25
    stamps = [x + "_T" for x in columns] + [x + "_C" for x in columns]
    return stamps

def _evaluate_file(fileName, stamps, kind, sender=False):
26
27
    # Remove first line, as this is the dummy line for intermittently storing data.
    df = pd.read_csv(fileName)[1:]
28
    df = df[df["Kind"] == kind].drop(["Kind"], axis=1).set_index("SeqNo")
29
    if sender:
30
        df.drop(_extract_stamps_by_type(stamps,"receiver"),axis=1,inplace=True)
31
    else:
32
33
        df.drop(_extract_stamps_by_type(stamps,"sender"),axis=1,inplace=True)
    df = df[pd.notnull(df).all(axis=1)]
34
35
36
    return df

def _restore_timestamp(df, column, cycle_time, base_c, base_t):
37
    df[column + "_T"] = ((df[column + "_C"] - base_c) * df[cycle_time + "Cycle_D"] + base_t).astype(int)
38
39
40
41
42
43
44
45

def _diff_t_c(df, name, start, stop):
    time = df[stop + "_T"] - df[start + "_T"]
    cycles = (df[stop + "_C"] - df[start + "_C"])
    return (time.astype(float), cycles.astype(float))

def _generate_processing_durations(df, name, start, stop):
    time, cycles = _diff_t_c(df, name, start, stop)
46
47
    df[name + "_D"] = time
    df[name + "_C"] = cycles
48
49
50

def _generate_cycle_time(df, name, start, stop):
    time, cycles = _diff_t_c(df, name, start, stop)
51
    df[name + "Cycle_D"] =  time / cycles
52
53
54
55

def _generate_duration(df, name, start, stop, cycleTimeColumn):
    diff = df[stop + "_C"] - df[start + "_C"]
    df[name + "Cycles"] = diff
56
    df[name + "Time"] = diff * df[cycleTimeColumn + "Cycle_D"]
57

58
59
60
def evaluate(sender_file, receiver_file, config, kind=0):
    df1 = _evaluate_file(sender_file,config["stamps"], kind,True)
    df2 = _evaluate_file(receiver_file,config["stamps"], kind)
61
62
63
64
65
66
67
68
69
70
    df = df1.join(df2)

    # Processing Times and Cycle Durations
    _generate_processing_durations(df, "Sender", "PrrtSendStart", "LinkTransmitEnd")
    _generate_cycle_time(df, "Sender", "PrrtSendStart", "LinkTransmitEnd")

    _generate_processing_durations(df, "Receiver", "LinkReceive", "PrrtDeliver")
    _generate_cycle_time(df, "Receiver", "PrrtReceivePackage", "PrrtDeliver")

    df["ChannelTime"] = df["LinkReceive_T"] - df["LinkTransmitEnd_T"]
71
    df["EndToEndTime"] = df["Sender_D"] + df["Receiver_D"]
72
73
74
75
76
77
78


    # Correlate Receiver Times with Sender Times
    df["LinkReceive_T"] -= df["ChannelTime"]
    df["PrrtReceivePackage_T"] -= df["ChannelTime"]
    df["PrrtDeliver_T"] -= df["ChannelTime"]

79
80
81
    # Generate Durations
    for d, duration in config["durations"].items():
        _generate_duration(df, d, duration["Start"], duration["Stop"], duration["Source"].capitalize())
82
83

    # Recreate missing timestamps from cycles
84
85
86
87
88
    for stamp in _stamp_name_by_src_and_type(config["stamps"], "sender", "cycle"):
        _restore_timestamp(df, stamp, "Sender", df["PrrtSendStart_C"], df["PrrtSendStart_T"])

    for stamp in _stamp_name_by_src_and_type(config["stamps"], "receiver", "cycle"):
        _restore_timestamp(df, stamp, "Receiver", df["LinkReceive_C"], df["LinkReceive_T"])
89
90

    return df