results.py 25.6 KB
Newer Older
1 2 3 4 5
import rdflib
from rdflib.namespace import Namespace
from rdflib.graph import Graph
import sys
import dateutil.parser
6
import datetime
Nicolas Seydoux's avatar
Nicolas Seydoux committed
7
import pytz
8 9 10
import glob
import json
import numpy as np
11
# The two following lines allow to compute results without an X server
Nicolas Seydoux's avatar
Nicolas Seydoux committed
12 13
import matplotlib as mpl
mpl.use('Agg')
14
import matplotlib.pyplot as plt
15 16 17 18
import scipy.stats as stats
import matplotlib.mlab as mlab
from sklearn.neighbors import KernelDensity
import argparse
19
import pickle
20 21 22 23 24 25 26 27 28 29 30 31 32 33

###########################################################
###########################################################
## Raw data formatting functions
###########################################################
###########################################################

def prepare_trace(logs_folder_path):
    trace=[]
    for tracefile_path in glob.glob(logs_folder_path+"/*.trace"):
        with open(tracefile_path, "r") as tracefile:
            for line in tracefile:
                event = json.loads(line)
                trace.append(event)
Nicolas Seydoux's avatar
Nicolas Seydoux committed
34
    trace.sort(key=lambda x: x["time"])
35 36 37 38 39
    return trace

def prepare_graph(logs_folder_path):
    g = Graph()
    for file in glob.glob(logs_folder_path+"/*.ttl"):
40
        print("Parsing "+file)
41 42 43 44 45 46 47 48
        g.parse(file, format="ttl")
    return g

###########################################################
###########################################################
## Data refining functions
###########################################################
###########################################################
49 50

def compute_delay(graph):
51
    print("Querying graph...")
52 53 54 55 56
    qres = graph.query(
        """
        PREFIX ssn: <http://purl.oclc.org/NET/ssnx/ssn#>
        PREFIX edr: <http://w3id.org/laas-iot/edr#>
        PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
57
        PREFIX san: <http://www.irit.fr/recherches/MELODI/ontologies/SAN#>
Nicolas Seydoux's avatar
Nicolas Seydoux committed
58
        SELECT ?ded ?obstimestamp ?dedtime ?rule ?node ?obs ?dedAppTimestamp
59 60
        WHERE {
            ?ded edr:deducedAt ?dedtime;
61 62
                edr:deducedWith ?rule;
                edr:deducedBy ?node;
Nicolas Seydoux's avatar
Nicolas Seydoux committed
63
                edr:receivedAt ?dedAppTimestamp;
64 65
                edr:deducedFrom  ?obs.
            ?obs a ssn:Observation ;
Nicolas Seydoux's avatar
Nicolas Seydoux committed
66
                edr:receivedAt ?obstimestamp.
67 68
         }
        """)
69
    print("Graph queried, analyzing results")
70
    rules = {}
71
    for row in qres.result:
72 73
        rule = row[3]
        node = row[4]
Nicolas Seydoux's avatar
Nicolas Seydoux committed
74
        obs_uri = row[5]
75 76
        if not rule in rules:
            rules[rule] = {}
77 78
        deduction_uri = row[0]
        deduction_time = dateutil.parser.parse(row[2])
Nicolas Seydoux's avatar
Nicolas Seydoux committed
79
        deduction_received = dateutil.parser.parse(row[6])
80
        last_symptom = dateutil.parser.parse(row[1])
81
        if not(deduction_uri in rules[rule]):
82
            rules[rule][deduction_uri] = {"uri":str(deduction_uri), "deduction_received":deduction_received, "deduction_time":deduction_time, "last_symptom":last_symptom, "node":node, "obs_uri":str(obs_uri)}
83
        else:
84 85
            if last_symptom > rules[rule][deduction_uri]["last_symptom"]:
                rules[rule][deduction_uri]["last_symptom"] = last_symptom
Nicolas Seydoux's avatar
Nicolas Seydoux committed
86
                rules[rule][deduction_uri]["obs_uri"] = str(obs_uri)
87
    print("Data collected, finalyzing delays")
88 89 90
    # Computation of the delays
    for rule in rules:
        for deduction in rules[rule]:
Nicolas Seydoux's avatar
Nicolas Seydoux committed
91
            delay = (rules[rule][deduction]["deduction_received"]-rules[rule][deduction]["last_symptom"]).total_seconds()
92
            rules[rule][deduction]["delay"]=delay
93
    return rules
94 95

# Returns (number of messages, average duration, std deviation)
96
def compute_hop_time(trace):
97 98 99 100 101
    trips = 0
    negative_trips = 0
    trip_times = []
    for record_origin in [x for x in trace if x["event"] == "Sending data"]:
        for record_destination in [y for y in trace if y["event"] == "Received observation"]:
102
            if record_origin["target"] == record_destination["node"] and record_origin["observation"] == record_destination["observation"]:
103 104 105 106 107 108 109 110
                trips += 1
                sent_time = dateutil.parser.parse(record_origin["time"])
                received_time = dateutil.parser.parse(record_destination["time"])
                trip_time = received_time - sent_time
                if(trip_time.total_seconds() > 0):
                    trip_times.append(trip_time.total_seconds())
                else:
                    negative_trips +=1
111
                break
112
    return trip_times
113 114

def compute_reasoning(trace):
115
    reasonings = {}
116 117 118 119 120 121 122
    for record_origin in [x for x in trace if x["event"] == "Reasoning starts" and "rule" in x]:
        for record_destination in [y for y in trace if y["event"] == "Reasoning ends" and "rule" in y]:
            if(record_origin["host"] == record_destination["host"]) and (record_origin["observation"] == record_destination["observation"]) and (record_origin["rule"] == record_destination["rule"]):
                begin_time = dateutil.parser.parse(record_origin["time"])
                end_time = dateutil.parser.parse(record_destination["time"])
                reasoning_time = end_time - begin_time
                # The actual reasoning event
123 124 125 126 127
                if(not record_origin["rule"] in reasonings):
                    reasonings[record_origin["rule"]] = []
                reasoning = {"time":reasoning_time.total_seconds(), "observation":record_origin["observation"], "host":record_origin["host"], "deduction":record_destination["deduction"], "moment":record_destination["time"]}
                if reasoning["time"]>0:
                    reasonings[record_origin["rule"]].append(reasoning)
128
                break
129 130 131 132 133
    return reasonings

# deduction is a boolean
def compute_reasoning_times(reasonings_trace, deduction):
    reasoning_times = {}
134 135 136 137 138 139 140
    for rule in reasonings_trace:
        for r in reasonings_trace[rule]:
            if r["deduction"] == str(deduction).lower() and r["time"] > 0:
                if not rule in reasoning_times:
                    reasoning_times[rule] = [r["time"]]
                else:
                    reasoning_times[rule].append(r["time"])
141 142 143 144 145 146 147 148 149 150
    return reasoning_times

def compute_avg_reasoning(reasoning_times):
    avg = {}
    dev = {}
    for rule in reasoning_times:
        avg[rule] = np.average([x for x in reasoning_times[rule]])
        dev[rule] = np.std([x for x in reasoning_times[rule]])
    return (avg, dev)

151
def compute_single_journey(deduction_uri, obs_uri, trace):
Nicolas Seydoux's avatar
Nicolas Seydoux committed
152 153
    journey = []
    for event in trace:
154
        if "observation" in event and (event["observation"] == obs_uri or event["observation"] == deduction_uri):
Nicolas Seydoux's avatar
Nicolas Seydoux committed
155 156 157 158 159 160 161 162
            journey.append(event)
    return journey

def display_journey(journey):
    for event in journey:
        print(event)

def summarize_journey(journey, deduction, rule):
163
    summary = {"idle":0, "transit":0, "processing":0, "additional-processing":0, "additional-idle":0}
Nicolas Seydoux's avatar
Nicolas Seydoux committed
164
    deduced = False
165
    for i in range(len(journey)):
Nicolas Seydoux's avatar
Nicolas Seydoux committed
166 167 168
        event = journey[i]
        if event["event"] == "Enrichment ended":
            summary["creation"] = event["time"]
169 170
            if i<len(journey)-1:
                summary["idle"] = (dateutil.parser.parse(journey[i+1]["time"]) - dateutil.parser.parse(event["time"])).total_seconds()
Nicolas Seydoux's avatar
Nicolas Seydoux committed
171
        # If the deducer sent the data, it is processed elsewhere
Nicolas Seydoux's avatar
Nicolas Seydoux committed
172 173
        if event["event"] == "Sending data":
            #if event["node"] != deduction["node"]:
Nicolas Seydoux's avatar
Nicolas Seydoux committed
174 175 176
            for j in range(len(journey[i:])):
                if journey[j]["event"] == "Received observation" and journey[j]["node"] == event["target"]:
                    summary["transit"] += (dateutil.parser.parse(journey[j]["time"]) - dateutil.parser.parse(event["time"])).total_seconds()
Nicolas Seydoux's avatar
Nicolas Seydoux committed
177 178 179 180 181 182 183
                    break
            else:
                # The application does not log the reception event
                naive_deduction_received = deduction["deduction_received"].replace(tzinfo=None)
                day_agnostic_deduction = dateutil.parser.parse(str(naive_deduction_received.time()))
                event_time = dateutil.parser.parse(event["time"])
                summary["transit"] += (day_agnostic_deduction - dateutil.parser.parse(event["time"])).total_seconds()
184
        if event["event"] == "Received observation" and i<len(journey)-1:
Nicolas Seydoux's avatar
Nicolas Seydoux committed
185
            summary["idle"] += (dateutil.parser.parse(journey[i+1]["time"]) - dateutil.parser.parse(event["time"])).total_seconds()
186
        if event["event"] == "Reasoning starts" and i<len(journey)-1:
Nicolas Seydoux's avatar
Nicolas Seydoux committed
187
            summary["processing"] += (dateutil.parser.parse(journey[i+1]["time"]) - dateutil.parser.parse(event["time"])).total_seconds()
188 189 190
            # if event["rule"] == rule:
            #     summary["deduction"] = journey[i+1]["time"]
            #     deduced = True
191
        if event["event"] == "Reasoning ends" and i<len(journey)-1:
Nicolas Seydoux's avatar
Nicolas Seydoux committed
192
            summary["idle"] += (dateutil.parser.parse(journey[i+1]["time"]) - dateutil.parser.parse(event["time"])).total_seconds()
193
        summary["delay"]=summary["idle"]+summary["transit"]+summary["processing"]
194 195 196 197 198
    if summary["transit"] <= 0:
        # print(journey)
        # print(summary)
        # print(deduction)
        return None
Nicolas Seydoux's avatar
Nicolas Seydoux committed
199 200 201 202 203 204 205 206
    return summary

def compute_journeys(graph, trace):
    print("To compute journeys, computing delays")
    rules = compute_delay(graph)
    journeys = []
    for rule in rules:
        for deduction in rules[rule]:
207 208
            # print("Searching for observation "+rules[rule][deduction]["obs_uri"])
            # print("delay is "+str(rules[rule][deduction]["delay"]))
209
            j = compute_single_journey(rules[rule][deduction]["uri"], rules[rule][deduction]["obs_uri"], trace)
210 211
            #display_journey(j)
            sj = summarize_journey(j, rules[rule][deduction], str(rule))
212 213
            if sj != None:
                journeys.append(sj)
214
    return journeys
Nicolas Seydoux's avatar
Nicolas Seydoux committed
215

216 217 218 219 220 221 222 223
def extract_delays_details(journeys):
    delays = []
    idles = []
    transits = []
    processings = []
    for j in journeys:
        delays.append(j["idle"]+j["transit"]+j["processing"])
    return delays
Nicolas Seydoux's avatar
Nicolas Seydoux committed
224

225 226 227 228 229 230
###########################################################
###########################################################
## Plotting functions
###########################################################
###########################################################

231
# Scatters the evolution of delays against time, to check whether there is a general tendancy
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264
def scatter_delays(delays_list, folder):
    # for rule in delays:
    #     ded_delay = []
    #     ded_moment = []
    #     for deduction in delays[rule]:
    #         ded_delay.append(delays[rule][deduction]["delay"])
    #         ded_moment.append(delays[rule][deduction]["deduction_time"])
    #     plt.plot(ded_moment,ded_delay, "r+")
    #     plt.show()

    rules = set()
    for delays in delays_list:
        rules = rules.union(delays.keys())
    for rule in rules:
        index = 1
        fig_handle = plt.figure(1)
        plt_id = int(str(len(delays_list))+"11")
        ax0 = plt.subplot(plt_id)
        for delays in delays_list:
            if rule in delays:
                subplot_id = int(str(len(delays_list))+"1"+str(index))
                ax_n = plt.subplot(subplot_id, sharex=ax0, sharey=ax0)
                ded_delay = []
                ded_moment = []
                for deduction in delays[rule]:
                    if delays[rule][deduction]["delay"] > 0:
                        ded_delay.append(delays[rule][deduction]["delay"])
                        ded_moment.append(delays[rule][deduction]["deduction_time"])
                plt.plot(ded_moment,ded_delay, "r+")
            index += 1
        plt.xlabel('Time')
        plt.title(rule)
        plt.savefig(folder+"/delay_scatter_"+rule.split("#")[1], format='eps', dpi=600)
265
        plt.show()
266
        plt.close()
267 268 269 270 271 272 273

def get_bin_centers(bin_edges):
    centers = []
    for i in range(len(bin_edges)-1):
        centers.append((bin_edges[i]+bin_edges[i+1])/2.0)
    return centers

274
def plot_histogram_kde(name, ax, data, bins):
275 276 277
    #n, bins, patches = ax.hist(data, density=True, bins='doane')
    n, bins, patches = ax.hist(data, density=True, bins=bins)
    try:
278
        print("{0} values, from {1} to {2} - {3}".format(str(len(data)), str(min(data)), str(max(data)), name))
279 280 281 282 283 284
        if len(data) > 1:
            kernel = stats.gaussian_kde(data)
            # possible bandwidth method : silverman
            kernel.set_bandwidth(0.2)
            centers = get_bin_centers(bins)
            ax.plot(centers, kernel.evaluate(centers), '-', label="kernel = '{0}'".format("gaussian"))
285 286 287
    except np.linalg.linalg.LinAlgError:
        print(data)

288 289 290 291 292 293
def histogram_delays(delays_list, folder):
    rules = set()
    for delays in delays_list:
        rules = rules.union(delays.keys())
    for rule in rules:
        index = 1
294
        fig_handle = plt.figure(1)
295 296
        plt_id = int(str(len(delays_list))+"11")
        ax0 = plt.subplot(plt_id)
297 298 299
        for delays in delays_list:
            if rule in delays:
                subplot_id = int(str(len(delays_list))+"1"+str(index))
300
                ax_n = plt.subplot(subplot_id, sharex=ax0, sharey=ax0)
301 302
                ded_delay = []
                for deduction in delays[rule]:
303 304 305
                    if delays[rule][deduction]["delay"] > 0:
                        ded_delay.append(delays[rule][deduction]["delay"])
                plot_histogram_kde(ax_n, ded_delay, 75)
306 307
                with open(folder+"/ded_delays_"+str(delays_list.index(delays))+"_"+rule.split("#")[1], "wb") as f:
                    pickle.dump(ded_delay, f)
308 309 310
            index += 1
        plt.xlabel('Delays')
        plt.title(rule)
311 312 313
        plt.savefig(folder+"/delay_"+rule.split("#")[1],format='eps', dpi=300)
        with open(folder+"/"+rule.split("#")[1]+"_fig.pkl", "wb") as f:
            pickle.dump(fig_handle, f)
314 315
        plt.close()

316 317 318 319 320
def histogram_journeys(journeys_list, folder):
    index = 1
    fig_handle = plt.figure(1)
    plt_id = int(str(len(journeys_list))+"11")
    ax0 = plt.subplot(plt_id)
321 322 323 324 325 326 327 328 329
    for method, journeys in journeys_list.items():
        if len(journeys) > 0:
            subplot_id = int(str(len(journeys_list))+"1"+str(index))
            ax_n = plt.subplot(subplot_id, sharex=ax0, sharey=ax0)
            ded_delay = []
            plt.title(method)
            for journey in journeys:
                if "delay" in journey and journey["delay"] > 0 and journey["delay"]<7000:
                    ded_delay.append(journey["delay"])
Nicolas Seydoux's avatar
Nicolas Seydoux committed
330 331 332 333 334
            if len(ded_delay) == 0:
                print("For "+method+", a list of delays is empty")
                print(journeys)
            else:
                plot_histogram_kde(method, ax_n, ded_delay, 75)
335 336 337
            with open(folder+"/journ_delays_"+method, "wb") as f:
                pickle.dump(ded_delay, f)
            index += 1
338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368
    plt.xlabel('Delays')
    plt.savefig(folder+"/journ_delay",format='eps', dpi=300)
    with open(folder+"/"+"journey_fig.pkl", "wb") as f:
        pickle.dump(fig_handle, f)
    plt.close()

def stack_journeys(journeys_list, folder):
    centralized_transit = [j["transit"] for j in journeys_list[0]]
    centralized_idle = [j["idle"] for j in journeys_list[0]]
    centralized_processing = [j["processing"] for j in journeys_list[0]]
    width = 0.35       # the width of the bars: can also be len(x) sequence

    transit_mean = [np.mean(centralized_transit)]
    transit_std = [np.std(centralized_transit)]

    processing_mean = np.mean(centralized_processing)
    processing_std = np.std(centralized_processing)


    p1 = plt.bar([0], transit_mean, width, yerr=transit_std)
    p2 = plt.bar(0, processing_mean, width,
                 bottom=transit_mean, yerr=processing_std)

    # plt.ylabel('Scores')
    # plt.title('Scores by group and gender')
    # plt.xticks(0, ('G1'))
    # plt.yticks(np.arange(0, 81, 10))
    plt.legend((p1[0], p2[0]), ('Transit', 'Processing'))
    plt.show()


369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386
def scatter_reasonings(reasonings):
    for rule in reasonings:
        r_duration_deduction = []
        r_moment_deduction = []
        r_duration_nodeduction = []
        r_moment_nodeduction = []
        print(rule)
        for r in reasonings[rule]:
            if r["deduction"] == "true":
                r_duration_deduction.append(r["time"])
                r_moment_deduction.append(dateutil.parser.parse(r["moment"]))
            else:
                r_duration_nodeduction.append(r["time"])
                r_moment_nodeduction.append(dateutil.parser.parse(r["moment"]))
        plt.plot(r_moment_deduction,r_duration_deduction, "b+")
        plt.plot(r_moment_nodeduction,r_duration_nodeduction, "rx")
        plt.show()

387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447
# def histogram_reasonings(reasonings):
#     for rule in reasonings:
#         r_deduction_duration = []
#         r_nodeduction_duration = []
#         print(rule)
#         for r in reasonings[rule]:
#             if r["deduction"] == "true":
#                 r_deduction_duration.append(r["time"])
#             else:
#                 r_nodeduction_duration.append(r["time"])
#         # Two subplots, the axes array is 1-d
#         f, axarr = plt.subplots(2, sharex=True)
#         (mu_ded, sigma_ded) = norm.fit(r_deduction_duration)
#         n_ded, bins_ded, patches_ded = axarr[0].hist(r_deduction_duration,  normed=1,bins='auto', facecolor='blue')
#         y = mlab.normpdf(bins_ded, mu_ded, sigma_ded)
#         l = axarr[0].plot(bins_ded, y, 'k--', linewidth=2)
#         axarr[1].hist(r_nodeduction_duration,  bins=200, facecolor='red')
#         (mu_noded, sigma_noded) = norm.fit(r_nodeduction_duration)
#         n_noded, bins_noded, patches_noded = axarr[0].hist(r_deduction_duration, normed=1, bins='auto', facecolor='blue')
#         y = mlab.normpdf(bins_noded, mu_noded, sigma_noded)
#         l = axarr[1].plot(bins_noded, y, 'k--', linewidth=2)
#         plt.show()

def histogram_reasonings(reasonings_list, folder):
    rules = set()
    for reasonings in reasonings_list:
        rules = rules.union(reasonings.keys())
    for rule in rules:
        index = 1
        plt.figure(1)
        ax0 = plt.subplot(len(reasonings_list), 2, 1)
        for reasonings in reasonings_list:
            if rule in reasonings:
                r_deduction_duration = []
                r_nodeduction_duration = []
                for r in reasonings[rule]:
                    if r["deduction"] == "true":
                        r_deduction_duration.append(r["time"])
                    else:
                        r_nodeduction_duration.append(r["time"])
                if len(r_deduction_duration) > 1:
                    # Plotting reasoning leading to deductions
                    ax_n_ded = plt.subplot(len(reasonings_list), 2, index, sharex=ax0)
                    ax_n_ded.relim()
                    # update ax.viewLim using the new dataLim
                    ax_n_ded.autoscale_view()
                    plt.title("Index : "+str(index)+", deductions")
                    plot_histogram_kde(ax_n_ded, r_deduction_duration, 30)
                if len(r_nodeduction_duration) > 1:
                    # Plotting reasoning leading to nothing
                    ax_n_noded = plt.subplot(len(reasonings_list), 2, index+len(reasonings_list), sharex=ax0)
                    ax_n_noded.relim()
                    # update ax.viewLim using the new dataLim
                    ax_n_noded.autoscale_view()
                    plt.title("Index : "+str(index)+", no deductions")
                    plot_histogram_kde(ax_n_noded, r_nodeduction_duration, 20)
            index += 1
        plt.suptitle('Reasoning '+rule)
        plt.savefig(folder+"/reasoning_"+rule.split("#")[1], dpi=300)
        plt.close()

Nicolas Seydoux's avatar
Nicolas Seydoux committed
448 449 450 451 452 453 454
def count_messages(trace):
    counter = 0
    for event in trace:
        if event["event"] == "Sending data":
            counter+=1
    return counter

455 456 457
parser = argparse.ArgumentParser(description='Processes the traces of an edr execution.')
parser.add_argument('-c', '--centralized', help="the folder containing all logs files for the centralized approach", type=str)
parser.add_argument('-d', '--distributed', help="the folder containing all logs files for the distributed approach", type=str)
458 459 460 461 462
parser.add_argument('--adp', type=str)
parser.add_argument('--cir', type=str)
parser.add_argument('--cdp', type=str)
parser.add_argument('--cdr', type=str)
parser.add_argument('--cip', type=str)
463
parser.add_argument('-o', '--output', help="the folder to which results are sent", type=str)
464
parser.add_argument('--delay', help="computes delay times", action="store_true")
465
parser.add_argument('--scatter', help="compute a scatter plot instead of histograms", action="store_true")
466 467
parser.add_argument('--reasoning', help="computes reasoning times", action="store_true")
parser.add_argument('--hop', help="computes hop times", action="store_true")
Nicolas Seydoux's avatar
Nicolas Seydoux committed
468
parser.add_argument('--journey', help="computes the different elements of the delay (hop time, reasoning time, wait time)", action="store_true")
469 470 471 472 473 474 475 476 477 478

args = parser.parse_args()
if args.output == None:
    exit("Defining an output is mandatory")

# Raw data
traces = []
graphs = []

if args.centralized != None:
479
    print("Compiling centralized data from "+args.centralized)
480 481 482
    # if args.reasoning or args.hop or args.journey or args.delay:
    print("Appending centralized trace...")
    traces.append(prepare_trace(args.centralized))
Nicolas Seydoux's avatar
Nicolas Seydoux committed
483 484
    if args.delay or args.journey:
        print("Appending centralized graph...")
485
        graphs.append(prepare_graph(args.centralized))
486 487

if args.distributed  != None:
488
    print("Compiling distributed data from "+args.distributed)
489 490 491
    # if args.reasoning or args.hop or args.journey:
    print("Appending distributed trace...")
    traces.append(prepare_trace(args.distributed))
Nicolas Seydoux's avatar
Nicolas Seydoux committed
492 493
    if args.delay or args.journey:
        print("Appending distributed graph...")
494
        graphs.append(prepare_graph(args.distributed))
495 496

# Refined data
497
if args.delay and (args.centralized != None or args.distributed  != None):
498
    print("Computing delays...")
499
    journeys = []
500 501
    delays = []
    for graph in graphs:
502 503 504 505 506 507
        j = compute_journeys(graph, traces[graphs.index(graph)])
        journeys.append(j)
        delays.append(extract_delays_details(j))
        # delays.append(compute_delay(graph))
    with open(args.output+"/journeys_c.pkl", "wb") as f:
        pickle.dump(journeys[0], f)
508 509 510
    if len(journeys) > 1:
        with open(args.output+"/journeys_d.pkl", "wb") as f:
            pickle.dump(journeys[1], f)
511 512 513
    if args.scatter:
        scatter_delays(delays, args.output)
    else:
514 515 516 517
        #histogram_delays(delays, args.output)
        if len(journeys) > 0:
             histogram_journeys(journeys, args.output)
        pass
518 519
    with open(args.output+"/delays_c.pkl", "wb") as f:
        pickle.dump(delays[0], f)
520 521 522
    if len(delays) > 1:
        with open(args.output+"/delays_d.pkl", "wb") as f:
            pickle.dump(delays[1], f)
523
    del(graphs)
524

525 526
if args.adp != None or args.cir != None or args.cdp != None or args.cip != None or args.cdr != None:
    journeys = {}
527
    for method, folder in {"adp":args.adp,
528 529 530 531
            "cdp":args.cdp,
            "cip":args.cip,
            "cdr":args.cdr,
            "cir":args.cir}.items():
532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547
        if folder != None:
            print("Collecting results for "+method)
            trace = prepare_trace(folder)
            graph = prepare_graph(folder)
            journey = compute_journeys(graph, trace)
            with open(args.output+"/journeys_{0}.pkl".format(method), "wb") as f:
                pickle.dump(journey, f)
            journeys[method]=journey
            delay = extract_delays_details(journey)
            with open(args.output+"/delays_{0}.pkl".format(method), "wb") as f:
                pickle.dump(delay, f)
            with open(args.output+"/messages_{0}.txt".format(method), "w") as f:
                f.write(str(count_messages(trace)))
            del(trace)
            del(graph)
            del(delay)
548 549 550 551
    if len(journeys) > 0:
         histogram_journeys(journeys, args.output)


552 553
if args.hop:
    print("Computing hop times...")
554
    hop_times = []
555 556 557 558 559
    for trace in traces:
        hop_times.append(compute_hop_time(trace))

if args.reasoning:
    print("Computing reasoning times")
560
    reasonings = []
561 562
    for trace in traces:
        reasonings.append(compute_reasoning(trace))
563 564
    histogram_reasonings(reasonings, args.output)

Nicolas Seydoux's avatar
Nicolas Seydoux committed
565 566 567 568
if args.journey:
    print("Computing journey...")
    journeys = []
    compute_journeys(graphs[0], traces[0])
569

570 571 572 573




574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608
#
# g = Graph()
# for file in glob.glob(logs_folder_path+"/*.ttl"):
#     g.parse(file, format="ttl")
# delays = compute_delay(g)
# # scatter_delays(delays)
# histogram_delays(delays)
#
# reasonings = compute_reasoning(trace)

# maintenant il faut superposer les graphiques centralisé et distribué pour pouvoir les comparer

# scatter_reasonings(reasonings)
# histogram_reasonings(reasonings)

# reasoning_times_deduction = compute_reasoning_times(reasonings, True)
# reasoning_times_no_deduction = compute_reasoning_times(reasonings, False)
# avg_deduction, std_deduction = compute_avg_reasoning(reasoning_times_deduction)
# avg_no, std_no = compute_avg_reasoning(reasoning_times_no_deduction)
# rules = []
# times = []
# for rule in avg_deduction:
#     rules.append(rule)
#     times.append(avg_deduction[rule])
#
# with open(result_path, "w") as result_file:
#     result_file.write("{0},{1},{2},{3},{4},\n".format("Rule", "Processing time (deduction)", "Deviation (deduction)", "Processing time (no deduction)", "Deviation (no deduction)"))
#     for rule in avg_deduction:
#         result_file.write("{0},{1},{2},{3},{4},\n".format(rule, avg_deduction[rule], std_deduction[rule], avg_no[rule], std_no[rule]))
#     result_file.write(",,,,\n")
#     result_file.write("Hops, Hop time (avg), Hop time (deviation)\n")
#     hop_time = compute_hop_time(trace, g)
#     result_file.write("{0}, {1}, {2}\n".format(hop_time[0], hop_time[1], hop_time[2]))
    # result_file.write("Delay (avg), Delay (deviation)\n")
    # result_file.write("{0}, {1}\n".format(delay[0], delay[1]))
609 610 611 612 613 614 615

# y_pos = np.arange(len(rules))
# plt.bar(y_pos, times, align='center', alpha=0.5)
# plt.xticks(y_pos, rules)
# plt.ylabel('Avg time')
#
# plt.show()