def test_raise_exceptions_false(): initial_state = { 'state_a': 0 } state_update_blocks = [ { 'policies': {}, 'variables': { 'state_a': update_state_invalid_result } }, ] params = {} TIMESTEPS = 10 RUNS = 1 model = Model(initial_state=initial_state, state_update_blocks=state_update_blocks, params=params) simulation = Simulation(model=model, timesteps=TIMESTEPS, runs=RUNS) experiment = Experiment(simulation) experiment.engine = Engine(raise_exceptions=False) results = experiment.run() _results = experiment.results assert len(results) > 0 assert results == _results exceptions = experiment.exceptions print(exceptions) assert any([True if isinstance(exception['exception'], Exception) else False for exception in exceptions]) assert isinstance(results, list)
def test_regression_lambdas(): initial_state = {'state_a': 0} state_update_blocks = [ { 'policies': {}, 'variables': { 'state_a': update_state_a } }, ] params = {'lambda_param': [lambda timestep: timestep % 5]} TIMESTEPS = 10 RUNS = 1 c = config_sim({"N": RUNS, "T": range(TIMESTEPS), "M": params}) exp = cadCADExperiment() exp.append_configs(initial_state=initial_state, partial_state_update_blocks=state_update_blocks, sim_configs=c) exec_mode = ExecutionMode() local_mode_ctx = ExecutionContext(context=exec_mode.local_mode) simulation = Executor(exec_context=local_mode_ctx, configs=exp.configs) raw_data, tensor_field, sessions = simulation.execute(engine=Engine( backend=Backend.PATHOS)) assert isinstance(raw_data, list)
def __init__(self, model: Model, timesteps=100, runs=1, **kwargs): self.model = model self.timesteps = timesteps self.runs = runs self.index = kwargs.pop("index", 0) self.engine = kwargs.pop("engine", Engine()) self.experiment = Experiment(self) if kwargs: raise Exception(f"Invalid Simulation option in {kwargs}")
def __init__(self, **kwargs) -> None: self.engine = kwargs.pop("engine", Engine()) self.results = [] self.exceptions = [] # Hooks self.before_experiment = kwargs.pop("before_experiment", None) self.after_experiment = kwargs.pop("after_experiment", None) self.before_simulation = kwargs.pop("before_simulation", None) self.after_simulation = kwargs.pop("after_simulation", None) self.before_run = kwargs.pop("before_run", None) self.after_run = kwargs.pop("after_run", None) self.before_subset = kwargs.pop("before_subset", None) self.after_subset = kwargs.pop("after_subset", None)
def test_multiple_partial_state_updates(): initial_state = { 'a': 0 } state_update_blocks = [ { 'policies': {}, 'variables': { 'a': lambda params, substep, state_history, previous_state, policy_input: ('a', previous_state['a'] + 1), } }, { 'policies': {}, 'variables': { 'a': lambda params, substep, state_history, previous_state, policy_input: ('a', previous_state['a'] + 1), } }, { 'policies': {}, 'variables': { 'a': lambda params, substep, state_history, previous_state, policy_input: ('a', previous_state['a'] + 1), } }, ] params = {} TIMESTEPS = 10 RUNS = 1 model = Model(initial_state=initial_state, state_update_blocks=state_update_blocks, params=params) simulation = Simulation(model=model, timesteps=TIMESTEPS, runs=RUNS) experiment = Experiment(simulation) experiment.engine = Engine(backend=Backend.SINGLE_PROCESS) result = experiment.run() df = pd.DataFrame(result) assert df.query('timestep == 10 and substep == 3')['a'].item() == 30
def test_run(): states = basic.states state_update_blocks = basic.state_update_blocks params = basic.params TIMESTEPS = 10 RUNS = 1 model = Model(initial_state=states, state_update_blocks=state_update_blocks, params=params) simulation = Simulation(model=model, timesteps=TIMESTEPS, runs=RUNS) experiment = Experiment(simulations=[simulation]) experiment.engine = Engine(drop_substeps=True) drop_substeps_result = pd.DataFrame(experiment.run()) simulation_result = pd.DataFrame(simulation.run()) keep = (simulation_result.substep == simulation_result['substep'].max()) keep |= (simulation_result.substep == 0) simulation_result = simulation_result.loc[keep] assert simulation_result.reset_index(drop=True).equals( drop_substeps_result.reset_index(drop=True))
def __init__(self, simulations=[], **kwargs): self.engine = kwargs.pop("engine", Engine()) self.simulations = [] self.results = [] self.exceptions = [] # Add and validate simulations self.add_simulations(simulations) # Hooks self.before_experiment = kwargs.pop("before_experiment", None) self.after_experiment = kwargs.pop("after_experiment", None) self.before_simulation = kwargs.pop("before_simulation", None) self.after_simulation = kwargs.pop("after_simulation", None) self.before_run = kwargs.pop("before_run", None) self.after_run = kwargs.pop("after_run", None) self.before_subset = kwargs.pop("before_subset", None) self.after_subset = kwargs.pop("after_subset", None) if kwargs: raise Exception(f"Invalid Experiment option in {kwargs}")
def execute(self, engine=Engine()): simulations = [] for config in self.configs: initial_state = config.initial_state state_update_blocks = config.partial_state_update_blocks timesteps = max(list(config.sim_config["T"])) + 1 runs = config.sim_config["N"] params = config.sim_config[ "M"] # {key: [value] for key, value in config.sim_config['M'].items()} model = Model( initial_state=initial_state, state_update_blocks=state_update_blocks, params=params, ) simulation = Simulation(model=model, timesteps=timesteps, runs=1) simulations.append(simulation) experiment = Experiment(simulations=simulations) experiment.engine = engine result = experiment.run() return result, None, None
import pytest import pandas as pd from radcad import Model, Simulation, Experiment from radcad.engine import Engine, Backend from tests.test_cases import benchmark_model states = benchmark_model.states state_update_blocks = benchmark_model.state_update_blocks params = benchmark_model.params TIMESTEPS = 100_000 RUNS = 5 model = Model(initial_state=states, state_update_blocks=state_update_blocks, params=params) simulation = Simulation(model=model, timesteps=TIMESTEPS, runs=RUNS) experiment = Experiment([simulation]) experiment.engine = Engine(backend=Backend.SINGLE_PROCESS) if __name__ == "__main__": results = experiment.run() assert len(results) > 0
from cadCAD import configs from tests.test_cases import benchmark_model states = benchmark_model.states state_update_blocks = benchmark_model.state_update_blocks params = benchmark_model.params TIMESTEPS = benchmark_model.TIMESTEPS RUNS = benchmark_model.RUNS model = Model(initial_state=states, state_update_blocks=state_update_blocks, params=params) simulation_radcad = Simulation(model=model, timesteps=TIMESTEPS, runs=RUNS) experiment = Experiment(simulation_radcad) experiment.engine = Engine(backend=Backend.BASIC) c = config_sim({"N": RUNS, "T": range(TIMESTEPS), "M": params}) exp = cadCADExperiment() exp.append_configs(initial_state=states, partial_state_update_blocks=state_update_blocks, sim_configs=c) exec_mode = ExecutionMode() local_mode_ctx = ExecutionContext(context=exec_mode.single_proc) simulation_cadcad = Executor(exec_context=local_mode_ctx, configs=configs) def test_benchmark_radcad(benchmark): benchmark.pedantic(radcad_simulation, iterations=1, rounds=3)
from cadCAD.engine import Executor import tests.test_cases.predator_prey_model as benchmark_model initial_state = benchmark_model.initial_state state_update_blocks = benchmark_model.state_update_blocks params = benchmark_model.params TIMESTEPS = benchmark_model.TIMESTEPS RUNS = benchmark_model.MONTE_CARLO_RUNS model = Model(initial_state=initial_state, state_update_blocks=state_update_blocks, params=params) simulation_radcad = Simulation(model=model, timesteps=TIMESTEPS, runs=RUNS) experiment = Experiment(simulation_radcad) experiment.engine = Engine(backend=Backend.PATHOS) c = config_sim({"N": RUNS, "T": range(TIMESTEPS), "M": params}) exp = cadCADExperiment() exp.append_configs(initial_state=initial_state, partial_state_update_blocks=state_update_blocks, sim_configs=c) exec_mode = ExecutionMode() local_mode_ctx = ExecutionContext(context=exec_mode.local_mode) simulation_cadcad = Executor(exec_context=local_mode_ctx, configs=exp.configs) def test_benchmark_radcad(benchmark): benchmark.pedantic(radcad_simulation, iterations=1, rounds=3)
def radcad_with_deecopy_simulation(): experiment.engine = Engine(deepcopy=True) data_radcad = experiment.run()
def radcad_no_deecopy_simulation(): experiment.engine = Engine(deepcopy=False) data_radcad = experiment.run()