Commit 74d01f92 authored by Jarrod Pas's avatar Jarrod Pas
Browse files

Create base trace and rewrite csv and random traces

parent 66d7b883
......@@ -19,8 +19,9 @@ __all__ = [
......@@ -540,31 +541,87 @@ class FloodingNode(Node):
Contact = namedtuple('Contact', ['time', 'a', 'b', 'join'])
def random_trace(nodes, seed=None, step=1):
"""Generate a random contact trace."""
random = Random(seed)
class Trace:
"""Contact trace base class."""
if isinstance(nodes, dict):
nodes = list(nodes)
def __init__(self, nodes):
"""Create a contact trace."""
self._nodes = nodes
for now in count(step=step):
join = bool(random.getrandbits(1))
node_a, node_b = random.sample(nodes, 2)
yield Contact(now, node_a, node_b, join)
def nodes(self):
"""Return number of nodes in trace."""
return self._nodes
def __iter__(self):
"""Yield no contacts."""
def create_contact(now, node_a, node_b, join):
"""Create a contact."""
return Contact(time=now, a=node_a, b=node_b, join=join)
class CSVTrace(Trace):
Contact trace from csv file.
The first row is used as headers.
The second row is the following information about the trace:
[ int(duration), int(node_count), ignored, ignored ]
def csv_trace(path):
"""Generate contact trace from csv file."""
with open(path) as trace_file:
reader = csv.reader(trace_file)
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)
def __init__(self, path):
"""Create a csv trace generator."""
with open(path) as csv_file:
csv_file = csv.reader(csv_file)
# skip header
# skip first line, it is additional stats about the trace:
# get node count from first row of csv. it is additional stats
# about the trace
# duration, placeholder, placehold, node_count
for row in reader:
now, source, destination, join = map(int, row)
yield Contact(now, source, destination, join)
nodes = int(next(csv_file)[1])
self.path = path
def __iter__(self):
"""Yield contacts from csv file."""
with open(self.path) as csv_file:
csv_file = csv.reader(csv_file)
# skip header and first row
for row in csv_file:
yield self.create_contact(*map(int, row))
class RandomTrace(Trace):
"""Contact trace from complete randomness."""
def __init__(self, nodes, seed=None, step=1):
"""Create a random trace generator."""
self._node_list = list(nodes)
self.step = step
self.seed = seed
def __iter__(self):
"""Yield random trace."""
random = Random(self.seed)
for now in count(step=self.step):
join = bool(random.getrandbits(1))
node_a, node_b = random.sample(self._node_list, 2)
yield self.create_contact(now, node_a, node_b, join)
class Traffic:
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