def setUpClass(cls): default_user = helpers.default_user() cls.job = models.OqJob(owner=default_user) rc = engine2.create_risk_calculation( cls.job.owner, dict(calculation_mode='scenario_damage', base_path='/'), []) cls.job.risk_calculation = rc cls.job.save() # We also need some sample exposure data records (to satisfy the dmg # dist per asset FK). test_input = models.Input( owner=default_user, input_type='exposure', path='fake', size=0) test_input.save() i2j = models.Input2job(input=test_input, oq_job=cls.job) i2j.save() exp_model = models.ExposureModel( owner=default_user, input=test_input, name='test-exp-model', category='economic loss', stco_type='per_asset', stco_unit='CHF') exp_model.save() test_site = Point(3.14, 2.17) cls.exp_data = models.ExposureData( # Asset exposure_model=exp_model, asset_ref=helpers.random_string(), taxonomy=helpers.random_string(), number_of_units=37, site=test_site.to_wkt(), stco=1234.56) cls.exp_data.save() cls.dmg_states = {} for lsi, dmg_state in enumerate(cls.DMG_STATES): dstate = models.DmgState( risk_calculation=cls.job.risk_calculation, dmg_state=dmg_state, lsi=lsi) cls.dmg_states[dmg_state] = dstate dstate.save() cls.invalid_state = models.DmgState( risk_calculation=cls.job.risk_calculation, dmg_state='invalid state', lsi=0)
def test_create_risk_calculation(self): params = { 'hazard_output_id': 1, 'base_path': 'path/to/job.ini', 'export_dir': '/tmp/xxx', 'calculation_mode': 'classical', # just some sample params 'lrem_steps_per_interval': 5, 'conditional_loss_poes': '0.01, 0.02, 0.05', 'region_constraint': '-0.5 0.5, 0.5 0.5, 0.5 -0.5, -0.5, -0.5', } owner = helpers.default_user() vuln_file = models.Input(digest='123', path='/foo/bar', size=0, input_type='vulnerability', owner=owner) vuln_file.save() exposure_file = models.Input(digest='456', path='/foo/baz', size=0, input_type='exposure', owner=owner) exposure_file.save() files = [vuln_file, exposure_file] rc = engine2.create_risk_calculation(owner, params, files) # Normalize/clean fields by fetching a fresh copy from the db. rc = models.RiskCalculation.objects.get(id=rc.id) self.assertEqual(rc.calculation_mode, 'classical') self.assertEqual(rc.lrem_steps_per_interval, 5) self.assertEqual(rc.conditional_loss_poes, [0.01, 0.02, 0.05]) self.assertEqual( rc.region_constraint.wkt, ('POLYGON ((-0.5000000000000000 0.5000000000000000, ' '0.5000000000000000 0.5000000000000000, ' '0.5000000000000000 -0.5000000000000000, ' '-0.5000000000000000 -0.5000000000000000, ' '-0.5000000000000000 0.5000000000000000))'))
def get_risk_job(risk_demo, hazard_demo, output_type="curve", username=None): """ Takes in input the paths (relative to the demos directory) to a risk and hazard demo file config, respectively. Creates the hazard outputs suitable to be used by a risk calculation and then creates a :class:`openquake.engine.db.models.OqJob` object for a risk calculation. It also returns the input files referenced by the risk config file. :param output_type: gmf or curve """ username = username if username is not None else default_user().user_name hazard_cfg = demo_file(hazard_demo) hazard_job = get_hazard_job(hazard_cfg, username) hc = hazard_job.hazard_calculation risk_cfg = demo_file(risk_demo) rlz = models.LtRealization.objects.create( hazard_calculation=hazard_job.hazard_calculation, ordinal=1, seed=1, weight=None, sm_lt_path="test_sm", gsim_lt_path="test_gsim", is_complete=False, total_items=1, completed_items=1) if output_type == "curve": hazard_output = models.HazardCurveData.objects.create( hazard_curve=models.HazardCurve.objects.create( lt_realization=rlz, output=models.Output.objects.create_output( hazard_job, "Test Hazard output", "hazard_curve"), investigation_time=hc.investigation_time, imt="PGA", imls=[0.1, 0.2, 0.3]), poes=[0.1, 0.2, 0.3], location="POINT(1 1)") else: hazard_output = models.Gmf.objects.create( gmf_set=models.GmfSet.objects.create( gmf_collection=models.GmfCollection.objects.create( output=models.Output.objects.create_output( hazard_job, "Test Hazard output", "gmf"), lt_realization=rlz, complete_logic_tree_gmf=False), investigation_time=hc.investigation_time, ses_ordinal=1, complete_logic_tree_gmf=False), imt="PGA", gmvs=[0.1, 0.2, 0.3], result_grp_ordinal=1, location="POINT(1 1)") hazard_job.status = "complete" hazard_job.save() job = engine2.prepare_job(username) params, files = engine2.parse_config( open(risk_cfg, 'r'), force_inputs=True) if output_type == "curve": params.update( dict(hazard_output_id=hazard_output.hazard_curve.output.id)) else: output = hazard_output.gmf_set.gmf_collection.output params.update(dict(hazard_output_id=output.id)) risk_calc = engine2.create_risk_calculation( job.owner, params, files.values()) risk_calc = models.RiskCalculation.objects.get(id=risk_calc.id) job.risk_calculation = risk_calc job.save() return job, files
def get_risk_job(risk_cfg, hazard_cfg, output_type="curve", username=None): """ Takes in input the paths to a risk job config file and a hazard job config file. Creates fake hazard outputs suitable to be used by a risk calculation and then creates a :class:`openquake.engine.db.models.OqJob` object for a risk calculation. It also returns the input files referenced by the risk config file. :param output_type: gmf, gmf_scenario, or curve """ username = username if username is not None else default_user().user_name hazard_job = get_hazard_job(hazard_cfg, username) hc = hazard_job.hazard_calculation rlz = models.LtRealization.objects.create( hazard_calculation=hazard_job.hazard_calculation, ordinal=1, seed=1, weight=None, sm_lt_path="test_sm", gsim_lt_path="test_gsim", is_complete=False, total_items=1, completed_items=1) if output_type == "curve": hazard_output = models.HazardCurve.objects.create( lt_realization=rlz, output=models.Output.objects.create_output( hazard_job, "Test Hazard output", "hazard_curve"), investigation_time=hc.investigation_time, imt="PGA", imls=[0.1, 0.2, 0.3]) for point in ["POINT(-1.01 1.01)", "POINT(0.9 1.01)", "POINT(0.01 0.01)", "POINT(0.9 0.9)"]: models.HazardCurveData.objects.create( hazard_curve=hazard_output, poes=[0.1, 0.2, 0.3], location="%s" % point) elif output_type == "gmf_scenario": output = models.Output.objects.create_output( hazard_job, "Test Hazard output", "gmf_scenario") for point in ["POINT(15.48 38.0900001)", "POINT(15.565 38.17)", "POINT(15.481 38.25)"]: hazard_output = models.GmfScenario.objects.create( output=output, imt="PGA", location=point, gmvs=[0.1, 0.2, 0.3]) else: rupture_ids = get_rupture_ids(hazard_job, hc, rlz, 3) hazard_output = models.GmfCollection.objects.create( output=models.Output.objects.create_output( hazard_job, "Test Hazard output", "gmf"), lt_realization=rlz, complete_logic_tree_gmf=False) gmf_set = models.GmfSet.objects.create( gmf_collection=hazard_output, investigation_time=hc.investigation_time, ses_ordinal=1, complete_logic_tree_gmf=False) for point in ["POINT(15.310 38.225)", "POINT(15.71 37.225)", "POINT(15.48 38.091)", "POINT(15.565 38.17)", "POINT(15.481 38.25)"]: models.Gmf.objects.create( gmf_set=gmf_set, imt="PGA", gmvs=[0.1, 0.2, 0.3], rupture_ids=rupture_ids, result_grp_ordinal=1, location=point) hazard_job.status = "complete" hazard_job.save() job = engine2.prepare_job(username) params, files = engine2.parse_config( open(risk_cfg, 'r'), force_inputs=True) params.update(dict(hazard_output_id=hazard_output.output.id)) risk_calc = engine2.create_risk_calculation( job.owner, params, files.values()) risk_calc = models.RiskCalculation.objects.get(id=risk_calc.id) job.risk_calculation = risk_calc job.save() return job, files