1
0
mirror of https://github.com/gsi-upm/soil synced 2025-08-23 19:52:19 +00:00

WIP: exporters

This commit is contained in:
J. Fernando Sánchez
2019-04-26 19:22:45 +02:00
parent a3ea434f23
commit 9bc036d185
16 changed files with 484 additions and 282 deletions

View File

@@ -2,7 +2,7 @@ from unittest import TestCase
import os
from os.path import join
from soil import utils, simulation
from soil import serialization, simulation
ROOT = os.path.abspath(os.path.dirname(__file__))
EXAMPLES = join(ROOT, '..', 'examples')
@@ -17,31 +17,32 @@ class TestExamples(TestCase):
def make_example_test(path, config):
def wrapped(self):
root = os.getcwd()
os.chdir(os.path.dirname(path))
s = simulation.from_config(config)
iterations = s.max_time * s.num_trials
if iterations > 1000:
s.max_time = 100
s.num_trials = 1
if config.get('skip_test', False) and not FORCE_TESTS:
self.skipTest('Example ignored.')
envs = s.run_simulation(dry_run=True)
assert envs
for env in envs:
assert env
try:
n = config['network_params']['n']
assert len(list(env.network_agents)) == n
assert env.now > 2 # It has run
assert env.now <= config['max_time'] # But not further than allowed
except KeyError:
pass
os.chdir(root)
for s in simulation.all_from_config(path):
iterations = s.max_time * s.num_trials
if iterations > 1000:
s.max_time = 100
s.num_trials = 1
if config.get('skip_test', False) and not FORCE_TESTS:
self.skipTest('Example ignored.')
envs = s.run_simulation(dry_run=True)
assert envs
for env in envs:
assert env
try:
n = config['network_params']['n']
assert len(list(env.network_agents)) == n
assert env.now > 2 # It has run
assert env.now <= config['max_time'] # But not further than allowed
except KeyError:
pass
return wrapped
def add_example_tests():
for config, path in utils.load_config(join(EXAMPLES, '**', '*.yml')):
for config, path in serialization.load_files(
join(EXAMPLES, '*', '*.yml'),
join(EXAMPLES, '*.yml'),
):
p = make_example_test(path=path, config=config)
fname = os.path.basename(path)
p.__name__ = 'test_example_file_%s' % fname

View File

@@ -7,7 +7,8 @@ import networkx as nx
from functools import partial
from os.path import join
from soil import simulation, Environment, agents, utils, history
from soil import (simulation, Environment, agents, serialization,
history, utils)
ROOT = os.path.abspath(os.path.dirname(__file__))
@@ -32,7 +33,7 @@ class TestMain(TestCase):
'path': join(ROOT, 'test.gexf')
}
}
G = utils.load_network(config['network_params'])
G = serialization.load_network(config['network_params'])
assert G
assert len(G) == 2
with self.assertRaises(AttributeError):
@@ -42,7 +43,7 @@ class TestMain(TestCase):
'path': join(ROOT, 'unknown.extension')
}
}
G = utils.load_network(config['network_params'])
G = serialization.load_network(config['network_params'])
print(G)
def test_generate_barabasi(self):
@@ -57,10 +58,10 @@ class TestMain(TestCase):
}
}
with self.assertRaises(TypeError):
G = utils.load_network(config['network_params'])
G = serialization.load_network(config['network_params'])
config['network_params']['n'] = 100
config['network_params']['m'] = 10
G = utils.load_network(config['network_params'])
G = serialization.load_network(config['network_params'])
assert len(G) == 100
def test_empty_simulation(self):
@@ -153,7 +154,7 @@ class TestMain(TestCase):
def test_torvalds_example(self):
"""A complete example from a documentation should work."""
config = utils.load_file(join(EXAMPLES, 'torvalds.yml'))[0]
config = serialization.load_file(join(EXAMPLES, 'torvalds.yml'))[0]
config['network_params']['path'] = join(EXAMPLES,
config['network_params']['path'])
s = simulation.from_config(config)
@@ -180,7 +181,7 @@ class TestMain(TestCase):
should be equivalent to the configuration file used
"""
with utils.timer('loading'):
config = utils.load_file(join(EXAMPLES, 'complete.yml'))[0]
config = serialization.load_file(join(EXAMPLES, 'complete.yml'))[0]
s = simulation.from_config(config)
s.dry_run = True
with utils.timer('serializing'):
@@ -189,6 +190,7 @@ class TestMain(TestCase):
recovered = yaml.load(serial)
with utils.timer('deleting'):
del recovered['topology']
del recovered['outdir']
assert config == recovered
def test_configuration_changes(self):
@@ -196,13 +198,14 @@ class TestMain(TestCase):
The configuration should not change after running
the simulation.
"""
config = utils.load_file(join(EXAMPLES, 'complete.yml'))[0]
config = serialization.load_file(join(EXAMPLES, 'complete.yml'))[0]
s = simulation.from_config(config)
s.dry_run = True
for i in range(5):
s.run_simulation(dry_run=True)
nconfig = s.to_dict()
del nconfig['topology']
del nconfig['outdir']
assert config == nconfig
def test_row_conversion(self):
@@ -245,11 +248,11 @@ class TestMain(TestCase):
assert ('finish', 10, None) in values
def test_serialize_class(self):
ser, name = utils.serialize(agents.BaseAgent)
ser, name = serialization.serialize(agents.BaseAgent)
assert name == 'soil.agents.BaseAgent'
assert ser == agents.BaseAgent
ser, name = utils.serialize(CustomAgent)
ser, name = serialization.serialize(CustomAgent)
assert name == 'test_main.CustomAgent'
assert ser == CustomAgent
pickle.dumps(ser)
@@ -257,9 +260,9 @@ class TestMain(TestCase):
def test_serialize_builtin_types(self):
for i in [1, None, True, False, {}, [], list(), dict()]:
ser, name = utils.serialize(i)
ser, name = serialization.serialize(i)
assert type(ser) == str
des = utils.deserialize(name, ser)
des = serialization.deserialize(name, ser)
assert i == des
def test_serialize_agent_type(self):
@@ -336,3 +339,10 @@ class TestMain(TestCase):
assert len(a3.subgraph(limit_neighbors=True)) == 1
assert len(a3.subgraph(limit_neighbors=True, center=False)) == 0
assert len(a3.subgraph(agent_type=agents.NetworkAgent)) == 3
def test_templates(self):
'''Loading a template should result in several configs'''
configs = serialization.load_file(join(EXAMPLES, 'template.yml'))
assert len(configs) > 0