Esempio n. 1
0
def test_to_HDF5():
    states = basic.states
    state_update_blocks = basic.state_update_blocks
    params = basic.params
    TIMESTEPS = basic.TIMESTEPS
    RUNS = basic.RUNS

    model = Model(initial_state=states, state_update_blocks=state_update_blocks, params=params)
    simulation = Simulation(model=model, timesteps=TIMESTEPS, runs=RUNS)

    HDF5_store_file = 'experiment_results.hdf5'
    
    experiment = Experiment(simulations=[simulation])
    experiment.after_experiment = lambda experiment: save_to_HDF5(experiment, HDF5_store_file, 'experiment_0')

    raw_result = experiment.run()

    df = pd.read_hdf(HDF5_store_file, 'experiment_0')
    assert df.equals(pd.DataFrame(raw_result))

    experiment = Experiment(simulations=[simulation])
    experiment.after_experiment = lambda experiment: save_to_HDF5(experiment, HDF5_store_file, 'experiment_1')

    raw_result = experiment.run()

    df = pd.read_hdf(HDF5_store_file, 'experiment_1')
    assert df.equals(pd.DataFrame(raw_result))
    
    assert len(raw_result) > 0
    assert raw_result == experiment.results
    assert simulation.run() == raw_result
Esempio n. 2
0
def test_hooks(capfd):
    simulation = Simulation(model=model, timesteps=TIMESTEPS, runs=3)
    experiment = Experiment(simulation)

    experiment.before_experiment = lambda experiment=None: print(f"Before experiment with {len(experiment.simulations)} simulations")
    experiment.after_experiment = lambda experiment=None: print(f"After experiment with {len(experiment.simulations)} simulations")
    experiment.before_simulation = lambda simulation=None: print(f"Before simulation {simulation.index} with params {simulation.model.params}")
    experiment.after_simulation = lambda simulation=None: print(f"After simulation {simulation.index} with params {simulation.model.params}")
    experiment.before_run = lambda context=None: print(f"Before run {context}")
    experiment.after_run = lambda context=None: print(f"After run {context}")
    experiment.before_subset = lambda context=None: print(f"Before subset {context}")
    experiment.after_subset = lambda context=None: print(f"After subset {context}")
    
    experiment.run()
    # out, err = capfd.readouterr()

    assert True
Esempio n. 3
0
def test_hooks(capsys):
    states = basic.states
    state_update_blocks = basic.state_update_blocks
    params = params = {'a': [1, 2], 'b': [1]}
    TIMESTEPS = 1
    RUNS = 2

    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, simulation])

    experiment.before_experiment = lambda experiment=None: print(
        f'before_experiment')
    experiment.before_simulation = lambda simulation=None: print(
        f'before_simulation {simulation.index}')
    experiment.before_run = lambda context=None: print(
        f'before_run {context.run}')
    experiment.before_subset = lambda context=None: print(
        f'before_subset {context.subset}')
    experiment.after_subset = lambda context=None: print(
        f'after_subset {context.subset}')
    experiment.after_run = lambda context=None: print(
        f'after_run {context.run}')
    experiment.after_simulation = lambda simulation=None: print(
        f'after_simulation {simulation.index}')
    experiment.after_experiment = lambda experiment=None: print(
        f'after_experiment')

    experiment.run()
    captured = capsys.readouterr()

    assert captured.out.replace('\n', '').replace(' ', '') == """
    before_experiment
    before_simulation 0
    before_run 0
    before_subset 0
    after_subset 0
    before_subset 1
    after_subset 1
    after_run 0
    before_run 1
    before_subset 0
    after_subset 0
    before_subset 1
    after_subset 1
    after_run 1
    after_simulation 0
    before_simulation 1
    before_run 0
    before_subset 0
    after_subset 0
    before_subset 1
    after_subset 1
    after_run 0
    before_run 1
    before_subset 0
    after_subset 0
    before_subset 1
    after_subset 1
    after_run 1
    after_simulation 1
    after_experiment
    """.replace('\n', '').replace(' ', '')