1
0
mirror of https://github.com/gsi-upm/soil synced 2024-11-14 15:32:29 +00:00
soil/tests/test_exporters.py

105 lines
3.0 KiB
Python
Raw Normal View History

2019-04-30 07:28:25 +00:00
import os
import io
import tempfile
import shutil
from unittest import TestCase
from soil import exporters
from soil import simulation
2022-10-13 20:43:16 +00:00
from soil import agents
2019-04-30 07:28:25 +00:00
class Dummy(exporters.Exporter):
started = False
trials = 0
ended = False
total_time = 0
called_start = 0
called_trial = 0
called_end = 0
2019-04-30 07:28:25 +00:00
2022-09-13 16:16:31 +00:00
def sim_start(self):
self.__class__.called_start += 1
2019-04-30 07:28:25 +00:00
self.__class__.started = True
2022-09-16 16:13:39 +00:00
def trial_end(self, env):
2019-04-30 07:28:25 +00:00
assert env
self.__class__.trials += 1
self.__class__.total_time += env.now
self.__class__.called_trial += 1
2019-04-30 07:28:25 +00:00
2022-09-16 16:13:39 +00:00
def sim_end(self):
2019-04-30 07:28:25 +00:00
self.__class__.ended = True
self.__class__.called_end += 1
2019-04-30 07:28:25 +00:00
class Exporters(TestCase):
def test_basic(self):
2022-10-13 20:43:16 +00:00
# We need to add at least one agent to make sure the scheduler
# ticks every step
num_trials = 5
max_time = 2
2019-04-30 07:28:25 +00:00
config = {
'name': 'exporter_sim',
2022-10-13 20:43:16 +00:00
'model_params': {
'agents': [{
'agent_class': agents.BaseAgent
}]
},
'max_time': max_time,
'num_trials': num_trials,
2019-04-30 07:28:25 +00:00
}
s = simulation.from_config(config)
2022-10-13 20:43:16 +00:00
for env in s.run_simulation(exporters=[Dummy], dry_run=True):
2022-10-13 20:43:16 +00:00
assert len(env.agents) == 1
assert env.now == max_time
2019-04-30 07:28:25 +00:00
assert Dummy.started
assert Dummy.ended
assert Dummy.called_start == 1
assert Dummy.called_end == 1
2022-10-13 20:43:16 +00:00
assert Dummy.called_trial == num_trials
assert Dummy.trials == num_trials
assert Dummy.total_time == max_time * num_trials
2019-04-30 07:28:25 +00:00
def test_writing(self):
2022-10-13 20:43:16 +00:00
'''Try to write CSV, sqlite and YAML (without dry_run)'''
2019-04-30 07:28:25 +00:00
n_trials = 5
config = {
'name': 'exporter_sim',
'network_params': {
'generator': 'complete_graph',
'n': 4
},
2022-10-06 13:49:10 +00:00
'agent_class': 'CounterModel',
2019-04-30 07:28:25 +00:00
'max_time': 2,
'num_trials': n_trials,
2022-03-21 11:53:40 +00:00
'dry_run': False,
2019-04-30 07:28:25 +00:00
'environment_params': {}
}
output = io.StringIO()
s = simulation.from_config(config)
tmpdir = tempfile.mkdtemp()
envs = s.run_simulation(exporters=[
exporters.default,
exporters.csv,
],
2022-03-21 11:53:40 +00:00
dry_run=False,
2019-04-30 07:28:25 +00:00
outdir=tmpdir,
exporter_params={'copy_to': output})
result = output.getvalue()
simdir = os.path.join(tmpdir, s.group or '', s.name)
with open(os.path.join(simdir, '{}.dumped.yml'.format(s.name))) as f:
result = f.read()
assert result
try:
for e in envs:
2022-10-13 20:43:16 +00:00
with open(os.path.join(simdir, '{}.env.csv'.format(e.id))) as f:
2019-04-30 07:28:25 +00:00
result = f.read()
assert result
finally:
shutil.rmtree(tmpdir)