def draw_dodag(path):
"""
This function draws the DODAG (to ./results) from the list of motes (from ./simulation.csc) and the list of
edges (from ./data/relationships.log).
:param path: path to the experiment (including [with-|without-malicious])
"""
pyplot.clf()
with_malicious = (basename(normpath(path)) == 'with-malicious')
data, results = join(path, 'data'), join(path, 'results')
with open(join(data, 'relationships.log')) as f:
relationships = f.read()
# first, check if the mote relationships were recorded
if len(relationships.strip()) == 0:
return
# retrieve motes and their colors
dodag = networkx.DiGraph()
motes = get_motes_from_simulation(join(path, 'simulation.csc'))
dodag.add_nodes_from(motes.keys())
colors = []
for n, p in motes.items():
x, y = p
dodag.node[n]['pos'] = motes[n] = (x, -y)
colors.append('green' if n == 0 else ('yellow' if not with_malicious or
(with_malicious and 0 < n < len(motes) - 1) else 'red'))
# retrieve edges from relationships.log
edges = {}
for relationship in relationships.split('\n'):
try:
d = match(RELATIONSHIP_REGEX, relationship.strip()).groupdict()
if int(d['flag']) == 0:
continue
mote, parent = int(d['mote_id']), int(d['parent_id'])
edges[mote] = parent
except AttributeError:
continue
# now, fill in the graph with edges
dodag.add_edges_from(edges.items())
# finally, draw the graph
networkx.draw(dodag, motes, node_color=colors, with_labels=True)
pyplot.savefig(join(results, 'dodag.png'), arrow_style=FancyArrowPatch)
评论列表
文章目录