def setUpClass(cls) -> None: cls.macrosim = SuperSimulationFactory.build_super_simulation( get_inputs_path("super_simulation_data_ingest.yaml")) cls.microsim = SuperSimulationFactory.build_super_simulation( get_inputs_path("super_simulation_microsim_model_inputs.yaml")) cls.microsim_excluded_pop = SuperSimulationFactory.build_super_simulation( get_inputs_path( "super_simulation_microsim_excluded_pop_model_inputs.yaml")) for sim in [cls.microsim, cls.microsim_excluded_pop]: sim.simulate_baseline(["PRISON"])
def test_reference_year_must_be_integer_time_steps_from_start_year( self) -> None: """Tests macrosimulation enforces compatibility of start year and time step""" with self.assertRaises(ValueError): SuperSimulationFactory.build_super_simulation( get_inputs_path( "super_simulation_broken_start_year_model_inputs.yaml")) with self.assertRaises(ValueError): SuperSimulationFactory.build_super_simulation( get_inputs_path( "super_simulation_broken_time_step_model_inputs.yaml"))
def test_build_super_simulation_microsim(self) -> None: microsim = SuperSimulationFactory.build_super_simulation( get_inputs_path("super_simulation_microsim_model_inputs.yaml")) self.assertTrue(microsim.initializer.microsim) self.assertTrue(microsim.simulator.microsim) self.assertTrue(microsim.validator.microsim) self.assertTrue(microsim.exporter.microsim)
def test_build_super_simulation_macrosim(self) -> None: macrosim = SuperSimulationFactory.build_super_simulation( get_inputs_path("super_simulation_data_ingest.yaml")) self.assertFalse(macrosim.initializer.microsim) self.assertFalse(macrosim.simulator.microsim) self.assertFalse(macrosim.validator.microsim) self.assertFalse(macrosim.exporter.microsim)
def test_existing_model_inputs(self) -> None: for dir_path, _, files in os.walk(root_dir_path): for file in files: if not file.endswith("yaml") and not file.endswith("yml"): continue file_path = os.path.join(dir_path, file) try: _ = SuperSimulationFactory.build_super_simulation(file_path) except Exception as e: e.args = ( f"Invalid configuration at {file}. " + e.args[0], ) + e.args[1:] raise
def test_invalid_yaml_configs(self) -> None: with self.assertRaises(ValueError): SuperSimulationFactory.build_super_simulation( get_inputs_path( "super_simulation_invalid_reference_date.yaml")) with self.assertRaises(ValueError): SuperSimulationFactory.build_super_simulation( get_inputs_path("super_simulation_extra_data_inputs.yaml")) with self.assertRaises(ValueError): SuperSimulationFactory.build_super_simulation( get_inputs_path("super_simulation_extra_user_inputs.yaml"))
def test_b_using_remaining_sentences_reduces_prison_population( self) -> None: """Tests microsim is using remaining sentence data in the right way""" assert isinstance(self.microsim, SuperSimulation) microsim = SuperSimulationFactory.build_super_simulation( get_inputs_path( "super_simulation_microsim_excluded_pop_model_inputs.yaml")) microsim.simulate_baseline(["PRISON"]) # get time before starting cohort filters out of prison affected_time_frame = self.microsim.initializer.data_dict[ "transitions_data"][ (self.microsim.initializer.data_dict["transitions_data"]. compartment == "PRISON") & (self.microsim.initializer.data_dict["transitions_data"]. total_population > 0)].compartment_duration.max() # get projected prison population from simulation substituting transitions data for remaining sentences substitute_outputs = microsim.simulator.pop_simulations[ "baseline_middle"].population_projections substitute_prison_population = (substitute_outputs[ (substitute_outputs.compartment == "PRISON") & (substitute_outputs.time_step > microsim.initializer.user_inputs["start_time_step"] + 1) & (substitute_outputs.time_step - microsim.initializer.user_inputs["start_time_step"] < affected_time_frame)].groupby("time_step").sum().total_population) # get projected prison population from regular simulation regular_outputs = self.microsim.validator.pop_simulations[ "baseline_middle"].population_projections regular_prison_population = (regular_outputs[ (regular_outputs.compartment == "PRISON") & (regular_outputs.time_step > self.microsim.initializer.user_inputs["start_time_step"] + 1) & (regular_outputs.time_step - self.microsim.initializer.user_inputs["start_time_step"] < affected_time_frame)].groupby("time_step").sum().total_population) self.assertTrue( (substitute_prison_population > regular_prison_population).all())
def test_simulation_architecture_must_match_compartment_costs( self) -> None: with self.assertRaises(ValueError): SuperSimulationFactory.build_super_simulation( get_inputs_path( "super_simulation_mismatched_compartments.yaml"))