Commit 8256d5a9 authored by Jarrod Pas's avatar Jarrod Pas
Browse files

Add metadata file support to csv trace

parent a55182d1
......@@ -35,6 +35,7 @@ __author__ = 'Jarrod Pas <j.pas@usask.ca>'
from collections import ChainMap, defaultdict, namedtuple, OrderedDict
import csv
import json
from itertools import count
from random import Random
from time import time
......@@ -176,6 +177,10 @@ class Network:
stats['broadcasts'] = sum(nodes['broadcasts'])
stats['delivery-cost'] = stats['broadcasts'] / stats['recieved']
for stat, values in packets.items():
if stat.startswith('hops'):
stats[stat] = sum(values)
stats.update(self._stats)
return stats
......@@ -569,39 +574,49 @@ class CSVTrace(Trace):
The first row is used as headers.
The second row is the following information about the trace:
[ int(duration), int(node_count), ignored, ignored ]
The csv uses the following columns:
[ int(time), int(node_a), int(node_b), int(join) ]
time -- simulation time that the contact even happens
node_{a,b} -- nodes involved in contact
join -- whether the contact is going up (1) or going down (0)
Optionally accepts a metadata file so it does not have to read and store
the entire trace at once the beginning.
"""
def __init__(self, path):
def __init__(self, path, metadata=None):
"""Create a csv trace generator."""
with open(path) as csv_file:
csv_file = csv.reader(csv_file)
# skip header
next(csv_file)
# get node count from first row of csv. it is additional stats
# about the trace
# duration, placeholder, placehold, node_count
nodes = int(next(csv_file)[1])
self.path = path
self._contacts = None
if metadata is None:
nodes = len(list(self.contacts))
else:
with open(metadata) as metadata_file:
nodes = json.load(metadata_file)['nodes']
super().__init__(nodes)
self.path = path
def __iter__(self):
"""Yield contacts from csv file."""
@property
def contacts(self):
if self._contacts is not None:
yield from self._contacts
contacts = []
with open(self.path) as csv_file:
csv_file = csv.reader(csv_file)
# skip header and first row
next(csv_file)
# skip header
next(csv_file)
for row in csv_file:
yield self.create_contact(*map(int, row))
contact = self.create_contact(*map(int, row))
contacts.append(contact)
yield contact
self._contacts = contacts
def __iter__(self):
"""Yield contacts from csv file."""
yield from self.contacts
class RandomTrace(Trace):
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment