def setUp(self): inputs = [("fragility", ""), ("exposure", "")] self.job = self.setup_classic_job(inputs=inputs) kvs.mark_job_as_current(self.job.id) kvs.cache_gc(self.job.id) self.site = Site(1.0, 1.0) block = Block(self.job.id, BLOCK_ID, [self.site]) block.to_kvs() # this region contains a single site, that is exactly # a site with longitude == 1.0 and latitude == 1.0 params = {"REGION_VERTEX": "1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0", "REGION_GRID_SPACING": "0.5", "BASE_PATH": ".", "OUTPUT_DIR": "."} self.job_ctxt = JobContext(params, self.job.id, oq_job=self.job) self.em = self._store_em() self._store_gmvs([0.40, 0.30, 0.45, 0.35, 0.40]) self.calculator = ScenarioDamageRiskCalculator(self.job_ctxt) # just stubbing out some preprocessing stuff... ScenarioDamageRiskCalculator.store_exposure_assets = lambda self: None ScenarioDamageRiskCalculator.store_fragility_model = lambda self: None ScenarioDamageRiskCalculator.partition = lambda self: None
def test_block_kvs_serialization(self): # Test that a Block is properly serialized/deserialized from the cache. job_id = 7 block_id = 0 expected_block = Block(job_id, block_id, [shapes.Site(1.0, 1.0), shapes.Site(2.0, 2.0)]) expected_block.to_kvs() actual_block = Block.from_kvs(job_id, block_id) self.assertEqual(expected_block, actual_block) # The sites are not compared in Block.__eq__; we need to check those # also. self.assertEqual(expected_block.sites, actual_block.sites)
def test_compute_bcr(self): cfg_path = helpers.demo_file( 'probabilistic_event_based_risk/config.gem') helpers.delete_profile(self.job) job_profile, params, sections = engine.import_job_profile( cfg_path, self.job) job_profile.calc_mode = 'event_based_bcr' job_profile.interest_rate = 0.05 job_profile.asset_life_expectancy = 50 job_profile.region = GEOSGeometry(shapes.polygon_ewkt_from_coords( '0.0, 0.0, 0.0, 2.0, 2.0, 2.0, 2.0, 0.0')) job_profile.region_grid_spacing = 0.1 job_profile.maximum_distance = 200.0 job_profile.gmf_random_seed = None job_profile.save() params.update(dict(CALCULATION_MODE='Event Based BCR', INTEREST_RATE='0.05', ASSET_LIFE_EXPECTANCY='50', MAXIMUM_DISTANCE='200.0', REGION_VERTEX=('0.0, 0.0, 0.0, 2.0, ' '2.0, 2.0, 2.0, 0.0'), REGION_GRID_SPACING='0.1')) job_ctxt = engine.JobContext( params, self.job_id, sections=sections, oq_job_profile=job_profile) calculator = eb_core.EventBasedRiskCalculator(job_ctxt) self.block_id = 7 SITE = shapes.Site(1.0, 1.0) block = Block(self.job_id, self.block_id, (SITE, )) block.to_kvs() location = GEOSGeometry(SITE.point.to_wkt()) asset = models.ExposureData(exposure_model=self.emdl, taxonomy="ID", asset_ref=22.61, stco=1, reco=123.45, site=location) asset.save() calculator.compute_risk(self.block_id) result_key = kvs.tokens.bcr_block_key(self.job_id, self.block_id) result = kvs.get_value_json_decoded(result_key) expected_result = {'bcr': 0.0, 'eal_original': 0.0, 'eal_retrofitted': 0.0} helpers.assertDeepAlmostEqual( self, [[[1, 1], [[expected_result, "22.61"]]]], result)
def _compute_risk_classical_psha_setup(self): SITE = shapes.Site(1.0, 1.0) # deletes all keys from kvs kvs.get_client().flushall() # at the moment the hazard part doesn't do exp on the 'x' # so it's done on the risk part. To adapt the calculation # we do the reverse of the exp, i.e. log(x) self.hazard_curve = [ (SITE, {'IMLValues': [0.001, 0.080, 0.170, 0.260, 0.360, 0.550, 0.700], 'PoEValues': [0.99, 0.96, 0.89, 0.82, 0.70, 0.40, 0.01], 'statistics': 'mean'})] # Vitor provided this Vulnerability Function imls_1 = [0.03, 0.04, 0.07, 0.1, 0.12, 0.22, 0.37, 0.52] loss_ratios_1 = [0.001, 0.022, 0.051, 0.08, 0.1, 0.2, 0.405, 0.700] covs_1 = [0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1] self.vuln_function = vulnerability_function.VulnerabilityFunction( imls_1, loss_ratios_1, covs_1, "LN") imls_2 = [0.1, 0.2, 0.4, 0.6] loss_ratios_2 = [0.05, 0.08, 0.2, 0.4] covs_2 = [0.5, 0.3, 0.2, 0.1] self.vuln_function_2 = vulnerability_function.VulnerabilityFunction( imls_2, loss_ratios_2, covs_2, "LN") self.asset_1 = {"taxonomy": "ID", "assetValue": 124.27} self.region = shapes.RegionConstraint.from_simple( (0.0, 0.0), (2.0, 2.0)) block = Block(self.job_id, self.block_id, (SITE, )) block.to_kvs() writer = hazard.HazardCurveDBWriter('test_path.xml', self.job_id) writer.serialize(self.hazard_curve) kvs.set_value_json_encoded( kvs.tokens.vuln_key(self.job_id), {"ID": self.vuln_function.to_json()}) kvs.set_value_json_encoded( kvs.tokens.vuln_key(self.job_id, retrofitted=True), {"ID": self.vuln_function.to_json()})