def test_parse_config_with_files_no_force_inputs(self): site_model_input = helpers.touch(content="foo") source = StringIO.StringIO(""" [general] calculation_mode = classical [site] site_model_file = %s maximum_distance=0 truncation_level=0 random_seed=0 """ % site_model_input) # Add a 'name' to make this look like a real file: source.name = 'path/to/some/job.ini' exp_base_path = os.path.dirname( os.path.join(os.path.abspath('.'), source.name)) expected_params = { 'base_path': exp_base_path, 'force_inputs': False, 'calculation_mode': 'classical', 'truncation_level': '0', 'random_seed': '0', 'maximum_distance': '0' } # Run first with force_inputs=True to create the new Input. params, expected_files = engine2.parse_config( source, force_inputs=True) # In order for us to reuse the existing input, we need to associate # each input with a successful job. job = engine2.prepare_job(getpass.getuser()) job.hazard_calculation = engine2.create_hazard_calculation( job.owner, params, expected_files.values()) job.status = 'complete' job.save() # Now run twice with force_inputs=False (the default). source.seek(0) params1, files1 = engine2.parse_config(source) source.seek(0) params2, files2 = engine2.parse_config(source) # Check the params just for sanity. self.assertEqual(expected_params, params1) self.assertEqual(expected_params, params2) # Finally, check that the Input returned by the latest 2 calls matches # the input we created above. self.assertEqual(len(expected_files), len(files1)) self.assertEqual(len(expected_files), len(files2)) self.assertEqual( expected_files['site_model_file'].id, files1['site_model_file'].id) self.assertEqual( expected_files['site_model_file'].id, files2['site_model_file'].id)
def test_read_and_validate_hazard_config(self): cfg = helpers.demo_file('simple_fault_demo_hazard/job.ini') job = engine2.prepare_job(getpass.getuser()) params, files = engine2.parse_config(open(cfg, 'r')) calculation = engine2.create_hazard_calculation( job.owner, params, files.values()) form = validation.ClassicalHazardForm( instance=calculation, files=files ) self.assertTrue(form.is_valid())
def test_a_few_inputs(self): cfg = helpers.demo_file('simple_fault_demo_hazard/job.ini') params, files = engine2.parse_config(open(cfg, 'r'), force_inputs=True) owner = helpers.default_user() hc = engine2.create_hazard_calculation(owner, params, files.values()) expected_ids = sorted([x.id for x in files.values()]) inputs = models.inputs4hcalc(hc.id) actual_ids = sorted([x.id for x in inputs]) self.assertEqual(expected_ids, actual_ids)
def test_with_input_type(self): cfg = helpers.demo_file("simple_fault_demo_hazard/job.ini") params, files = engine2.parse_config(open(cfg, "r"), force_inputs=True) owner = helpers.default_user() hc = engine2.create_hazard_calculation(owner, params, files.values()) # It should only be 1 id, actually. expected_ids = [x.id for x in files.values() if x.input_type == "lt_source"] inputs = models.inputs4hcalc(hc.id, input_type="lt_source") actual_ids = sorted([x.id for x in inputs]) self.assertEqual(expected_ids, actual_ids)
def get_hazard_job(cfg, username=None): """ Given a path to a config file, create a :class:`openquake.engine.db.models.OqJob` object for a hazard calculation. """ username = username if username is not None else default_user().user_name job = engine2.prepare_job(username) params, files = engine2.parse_config(open(cfg, 'r'), force_inputs=True) haz_calc = engine2.create_hazard_calculation( job.owner, params, files.values()) haz_calc = models.HazardCalculation.objects.get(id=haz_calc.id) job.hazard_calculation = haz_calc job.save() return job
def _create_haz_calc(cls): params = { 'base_path': 'a/fake/path', 'calculation_mode': 'classical', 'region': '1 1 2 2 3 3', 'width_of_mfd_bin': '1', 'rupture_mesh_spacing': '1', 'area_source_discretization': '2', 'investigation_time': 50, 'truncation_level': 0, 'maximum_distance': 200, 'number_of_logic_tree_samples': 1, 'intensity_measure_types_and_levels': dict(PGA=[1, 2, 3, 4]), 'random_seed': 37, } owner = helpers.default_user() hc = engine2.create_hazard_calculation(owner, params, []) return hc
def test_create_hazard_calculation(self): # Just the bare minimum set of params to satisfy not null constraints # in the db. params = { 'base_path': 'path/to/job.ini', 'calculation_mode': 'classical', 'region': '1 1 2 2 3 3', 'width_of_mfd_bin': '1', 'rupture_mesh_spacing': '1', 'area_source_discretization': '2', 'investigation_time': 50, 'truncation_level': 0, 'maximum_distance': 200, 'number_of_logic_tree_samples': 1, 'intensity_measure_types_and_levels': dict(PGA=[1, 2, 3, 4]), 'random_seed': 37, } owner = helpers.default_user() site_model = models.Input(digest='123', path='/foo/bar', size=0, input_type='site_model', owner=owner) site_model.save() files = [site_model] hc = engine2.create_hazard_calculation(owner, params, files) # Normalize/clean fields by fetching a fresh copy from the db. hc = models.HazardCalculation.objects.get(id=hc.id) self.assertEqual(hc.calculation_mode, 'classical') self.assertEqual(hc.width_of_mfd_bin, 1.0) self.assertEqual(hc.rupture_mesh_spacing, 1.0) self.assertEqual(hc.area_source_discretization, 2.0) self.assertEqual(hc.investigation_time, 50.0) self.assertEqual(hc.truncation_level, 0.0) self.assertEqual(hc.maximum_distance, 200.0) # Test the input2haz_calc link: [inp2hcs] = models.Input2hcalc.objects.filter( hazard_calculation=hc.id) self.assertEqual(site_model.id, inp2hcs.input.id)