def test_multiple_runs_not_allowed(mv_kwargs): """Assert that multiple runs are prohibited""" # Create Multiverse and run mv = Multiverse(**mv_kwargs) mv.run_single() # Another run should not be possible with pytest.raises(RuntimeError, match="Could not add simulation task"): mv.run_single()
def test_FrozenMultiverse(mv_kwargs, cluster_env): """Test the FrozenMultiverse class""" # Need a regular Multiverse and corresponding output for that mv = Multiverse(**mv_kwargs) mv.run_single() # NOTE Need to adjust the data directory in order to not create collisions # in the eval directory due to same timestamps ... # Now create a frozen Multiverse from that one # Without run directory, the latest one should be loaded print("\nInitializing FrozenMultiverse without further kwargs") FrozenMultiverse(**mv_kwargs, data_manager=dict(out_dir="eval/{timestamp:}_1")) # With a relative path, the corresponding directory should be found print("\nInitializing FrozenMultiverse with timestamp as run_dir") FrozenMultiverse(**mv_kwargs, run_dir=os.path.basename(mv.dirs['run']), data_manager=dict(out_dir="eval/{timestamp:}_2")) # With an absolute path, that path should be used directly print("\nInitializing FrozenMultiverse with absolute path to run_dir") FrozenMultiverse(**mv_kwargs, run_dir=mv.dirs['run'], data_manager=dict(out_dir="eval/{timestamp:}_3")) # With a relative path, the path relative to the CWD should be used print("\nInitializing FrozenMultiverse with relative path to run_dir") FrozenMultiverse(**mv_kwargs, run_dir=os.path.relpath(mv.dirs['run'], start=os.getcwd()), data_manager=dict(out_dir="eval/{timestamp:}_4")) # Bad type of run directory should fail with pytest.raises(TypeError, match="Argument run_dir needs"): FrozenMultiverse(**mv_kwargs, run_dir=123, data_manager=dict(out_dir="eval/{timestamp:}_5")) # Non-existing directory should fail with pytest.raises(IOError, match="No run directory found at"): FrozenMultiverse(**mv_kwargs, run_dir="my_non-existing_directory", data_manager=dict(out_dir="eval/{timestamp:}_6")) # Cluster mode print("\nInitializing FrozenMultiverse in cluster mode") mv_kwargs['run_cfg_path'] = CLUSTER_MODE_CFG_PATH mv_kwargs['cluster_params'] = dict(env=cluster_env) FrozenMultiverse(**mv_kwargs, run_dir=os.path.relpath(mv.dirs['run'], start=os.getcwd()), data_manager=dict(out_dir="eval/{timestamp:}_7")) with pytest.raises(NotImplementedError, match="use_meta_cfg_from_run_dir"): FrozenMultiverse(**mv_kwargs, run_dir="/some/path/to/a/run_dir", use_meta_cfg_from_run_dir=True, data_manager=dict(out_dir="eval/{timestamp:}_7"))
def dm_after_single(mv_kwargs) -> DataManager: """Initialises a Multiverse with a DataManager, runs a simulation with output going into a temporary directory, then returns the DataManager.""" # Initialise the Multiverse mv_kwargs['run_cfg_path'] = RUN_CFG_PATH mv = Multiverse(**mv_kwargs) # Run a sweep mv.run_single() # Return the data manager return mv.dm
def test_parameter_validation(mv_kwargs): """Tests integration of the parameter validation feature""" # Works mv_kwargs['run_cfg_path'] = RUN_CFG_PATH_VALID mv_kwargs['model_name'] = "ForestFire" mv_kwargs['paths']['model_note'] = "valid" mv = Multiverse(**mv_kwargs) mv.run_single() # Fails mv_kwargs['run_cfg_path'] = RUN_CFG_PATH_INVALID mv_kwargs['model_name'] = "ForestFire" mv_kwargs['paths']['model_note'] = "invalid" with pytest.raises(ValidationError, match="Validation failed for 3 para"): mv = Multiverse(**mv_kwargs)
def test_graph_plots(tmpdir): """Tests the plot_funcs.dag.graph module""" # Create and run simulation raise_exc = {'plot_manager': {'raise_exc': True}} mv = Multiverse(model_name='CopyMeGraph', run_cfg_path=GRAPH_RUN, paths=dict(out_dir=str(tmpdir)), **raise_exc) mv.run_single() # Load mv.dm.load_from_cfg(print_tree=False) # Single graph plots mv.pm.plot_from_cfg( plots_cfg=GRAPH_PLOTS, plot_only=( "Graph", "DiGraph", "MultiGraph", "MultiDiGraph", "ExternalProperties", "Example_graph_plot", "custom_node_positioning_model", "explicit_node_positions", "custom_graph_creation", "custom_graph_arr_creation", ), ) # Animation plots mv.pm.plot_from_cfg( plots_cfg=GRAPH_PLOTS, plot_only=[ "graph_anim1", "graph_anim2", "graph_anim3", "graph_anim_external", "graph_anim4", "graph_anim_custom_graph_creation", ], ) # Test failing cases – if possible these test are done in the (faster) # GraphPlot-class test. # Providing invalid dag tag for external property with pytest.raises( PlotCreatorError, match=( "No tag 'some_state_transformed' found in the data selected by " "the DAG!"), ): mv.pm.plot_from_cfg(plots_cfg=GRAPH_PLOTS, plot_only=["invalid_ext_prop"]) # Ambiguous time specifications for animation with pytest.raises(PlotCreatorError, match="ambiguous animation time specifications"): mv.pm.plot_from_cfg(plots_cfg=GRAPH_PLOTS, plot_only=["anim_amgiguous_time_spec"]) # Trying to animate from single nx.Graph with pytest.raises(PlotCreatorError, match="due to invalid type of the 'graph'"): mv.pm.plot_from_cfg(plots_cfg=GRAPH_PLOTS, plot_only=["anim_not_dataarray"])