Newer
Older
"""Example to run a batch of simlations on SHED data."""
__authors__ = 'Jarrod Pas <j.pas@usask.ca>, Hunter McConnell <hunter.mcconnell@usask.ca>'
from random import randint
from pydtnsim import Network, RandomTraffic, Node, EpidemicNode, CSVTrace
from pydtnsim.community import BubbleKCliqueNode, BubbleLouvainNode
from pydtnsim.community import HCBFKCliqueNode, HCBFLouvainNode
Simulation = namedtuple('Simulation', ['trace', 'node_type', 'seed'])
def run_simulation(simulation):
"""Run a simulation."""
seed = simulation.seed
csv = path.join(simulation.trace, 'contact.csv')
metadata = path.join(simulation.trace, 'metadata.json')
trace = CSVTrace(csv, metadata=metadata)
traffic_options = {
'seed': seed,
'start': epoch,
traffic = RandomTraffic(nodes, **traffic_options)
network = Network(nodes, traffic=traffic, trace=trace)
network.run()
stats = {
'node_type': node_type.__name__,
'seed': seed,
}
stats.update(network.stats_summary)
# return stats because we can't pickle the network as it is a generator.
BubbleKCliqueNode,
HCBFKCliqueNode,
BubbleLouvainNode,
HCBFLouvainNode,
if args['batch'] > 1: # batch mode with random seeds
for _ in range(args['batch']):
seed = randint(0, 500)
for node_type in node_types:
sim = Simulation(trace=trace, node_type=node_type, seed=seed)
simulations.append(sim)
else:
for seed in args['seeds']: # seed mode with inputted seeds
for node_type in node_types:
sim = Simulation(trace=trace, node_type=node_type, seed=seed)
simulations.append(sim)
for stats in pool.imap_unordered(run_simulation, simulations):
if not args['quiet']:
log(stats)
type = stats['node_type']
if type not in results:
results[type] = []
results[type].append(stats)
# find unused filename
i = 0
while os.path.exists(f"results{i}.csv"):
i += 1
# dump sim stats in csv
with open(f"results{i}.csv", 'w', newline='') as results_file:
for node_type in results:
fieldnames = results[node_type][0].keys()
writer = csv.DictWriter(results_file, fieldnames=fieldnames)
writer.writeheader()
for result in results[node_type]:
writer.writerow(result)
parser = ArgumentParser()
parser.add_argument('shed')
parser.add_argument('--quiet', '-q', action='store_true')
parser.add_argument('--batch', '-b',
metavar='BATCH', type=int, default=1)
action='append', metavar='SEED', type=int, default=[None])
args = parser.parse_args(args)
return vars(args)
if __name__ == '__main__':