Skip to content
Snippets Groups Projects

pydtn agkmeans and version 1.0

Merged Hunter McConnell (rtm534) requested to merge summer2022 into develop
3 files
+ 72
33
Compare changes
  • Side-by-side
  • Inline
Files
3
+ 54
31
@@ -11,6 +11,7 @@ from typing import ChainMap
from progress.bar import Bar
import sys
import os
import copy
import csv
import time
@@ -51,10 +52,11 @@ def run_simulation(simulation):
# record packets stats
with open("_packets_.txt", "a", encoding="utf-8") as file:
file.write("__" + str(simulation.node_type) + "__\n")
file.write("__" + str(simulation.node_type) + "__" + simulation.trace + "__\n")
for packet in network.stats["packets"]:
#if packet["source"] is None or packet["destination"] is None:
file.write("S" + str(packet["source"].name) + "\tD" + str(packet["destination"].name) +
"\ttrace: " + packet["traceroute"] + "\n")
"\ttrace: " + packet["traceroute"] + "\n")
stats = {
"trace": simulation.trace,
@@ -78,14 +80,25 @@ def bar_plot(graphing_options, results):
dependant = graphing_options["dependant"]
data = [[inp] for inp in graphing_options["input"]]
columns = [choice]
columns.extend(results.keys())
# ordering
epidemic = results.pop("EpidemicNode", None)
direct = results.pop("Node", None)
ordered_results = sorted(results.items())
if direct is not None:
ordered_results.insert(0, ("Node", direct))
if epidemic is not None:
ordered_results.append(("EpidemicNode", epidemic))
if len(dependant) > 1:
print("len(dep) > 1")
fig, axes = plt.subplots(len(dependant))
for ax, dep in zip(axes, dependant):
this_data = copy.deepcopy(data)
for node, stats in results.items():
for node, stats in ordered_results:
if node not in columns:
columns.append(node)
sorted_results = sorted(stats, key=lambda x: x["tag"])
batch_y = [[] for _ in range(len(data))]
@@ -97,10 +110,12 @@ def bar_plot(graphing_options, results):
dat.append(mean(batch))
pd.DataFrame(this_data, columns=columns).plot(
x=choice, kind="bar", stacked=False, rot=0, ylabel=dep, ax=ax
).legend(bbox_to_anchor=(1.05, 1), loc="upper left", borderaxespad=0)
x=choice, kind="bar", stacked=False, rot=0, ylabel=dep, ax=ax, colormap="Purples", edgecolor="black", legend=False
)
else:
for node, stats in results.items():
for node, stats in ordered_results:
if node not in columns:
columns.append(node)
sorted_results = sorted(stats, key=lambda x: x["tag"])
batch_y = [[] for _ in range(len(data))]
@@ -112,9 +127,10 @@ def bar_plot(graphing_options, results):
dat.append(mean(batch))
pd.DataFrame(data, columns=columns).plot(
x=choice, kind="bar", stacked=False, rot=0, ylabel=dependant[0]
).legend(bbox_to_anchor=(1.05, 1), loc="upper left", borderaxespad=0)
x=choice, kind="bar", stacked=False, rot=0, ylabel=dependant[0], colormap="Purples", edgecolor="black"
)
plt.legend(bbox_to_anchor=(1.05, 1), loc="upper left", borderaxespad=0)
plt.subplots_adjust(right=0.75, hspace=0.35)
plt.xlabel(choice)
plt.show()
@@ -168,6 +184,8 @@ def create_sim_list(config):
{"start": config["NodeOptions"]["epoch"]}, config["TrafficOptions"]
)
# todo: put batches here
# overwrite seeds with random ones for fatemeh testing
# batch = config["SimOptions"]["batch"]
# seeds = []
@@ -235,6 +253,10 @@ def create_sim_list(config):
def main(args):
"""Run a simulation for each seed, for each independant variable, graph the results."""
# os.remove("_packets_.txt")
# os.remove("send_data_.txt")
if args["no_gui"]:
with open(args["config"], "r", newline="", encoding="utf8") as f:
config = yaml.load(f, Loader=yaml.FullLoader)
@@ -253,32 +275,33 @@ def main(args):
simulations = create_sim_list(config)
results = {}
batches = config["SimOptions"]["batch"]
with Bar("Progress:", max = batches*len(simulations), suffix='sim:%(index)d/%(max)d\testimated time remaining: %(eta_td)s\n\t\telapsed time: %(elapsed_td)s') as bar:
for _ in range(batches):
for stats in pool.imap_unordered(run_simulation, simulations):
node_type = stats["node_type"]
if node_type not in results:
results[node_type] = []
results[node_type].append(stats)
bar.next()
with Bar("Progress:", max = len(simulations), suffix='sim:%(index)d/%(max)d\testimated time remaining: %(eta_td)s\n\t\telapsed time: %(elapsed_td)s') as bar:
for stats in pool.imap_unordered(run_simulation, simulations):
node_type = stats["node_type"]
if node_type not in results:
results[node_type] = []
results[node_type].append(stats)
bar.next()
try:
# dump stats in csv
with open("testing.csv", "w", newline="") as results_file:
for node_type in results:
fieldnames = results[node_type][0].keys()
writer = csv.DictWriter(
results_file, fieldnames=fieldnames, extrasaction="ignore"
) # extrasaction avoids intermittent dictwriter valueerror
writer.writeheader()
for result in results[node_type]:
writer.writerow(result)
except:
pass
# graph results
bar_plot(config["GraphingOptions"], results)
# dump stats in csv
with open("testing.csv", "w", newline="") as results_file:
for node_type in results:
fieldnames = results[node_type][0].keys()
writer = csv.DictWriter(
results_file, fieldnames=fieldnames, extrasaction="ignore"
) # extrasaction avoids intermittent dictwriter valueerror
writer.writeheader()
for result in results[node_type]:
writer.writerow(result)
Loading