def _compute_bcr(self, block_id): """ Calculate and store in the kvs the benefit-cost ratio data for block. A value is stored with key :func:`openquake.kvs.tokens.bcr_block_key`. See :func:`openquake.risk.job.general.compute_bcr_for_block` for result data structure spec. """ result = defaultdict(list) seed, correlation_type = self._get_correlation_type() block = general.Block.from_kvs(self.job_ctxt.job_id, block_id) loss_histogram_bins = self.job_ctxt.oq_job_profile.loss_histogram_bins vulnerability_model_original = vulnerability.load_vuln_model_from_kvs( self.job_ctxt.job_id) vulnerability_model_retrofitted = ( vulnerability.load_vuln_model_from_kvs( self.job_ctxt.job_id, retrofitted=True)) assets_getter = lambda site: general.BaseRiskCalculator.assets_at( self.job_ctxt.job_id, site) def hazard_getter(site): gmvs = self._get_gmvs_at(general.hazard_input_site( self.job_ctxt, site)) return {"IMLs": gmvs, "TSES": self._tses(), "TimeSpan": self._time_span()} bcr = api.bcr(api.probabilistic_event_based( vulnerability_model_original, loss_histogram_bins, seed, correlation_type), api.probabilistic_event_based( vulnerability_model_retrofitted, loss_histogram_bins, seed, correlation_type), float(self.job_ctxt.params["INTEREST_RATE"]), float(self.job_ctxt.params["ASSET_LIFE_EXPECTANCY"])) for asset_output in api.compute_on_sites( block.sites, assets_getter, hazard_getter, bcr): asset = asset_output.asset result[(asset.site.x, asset.site.y)].append(({ "bcr": asset_output.bcr, "eal_original": asset_output.eal_original, "eal_retrofitted": asset_output.eal_retrofitted}, asset.asset_ref)) bcr_block_key = kvs.tokens.bcr_block_key( self.job_ctxt.job_id, block_id) result = result.items() kvs.set_value_json_encoded(bcr_block_key, result) LOGGER.debug("bcr result for block %s: %r", block_id, result)
def _compute_bcr(self, block_id): """ Calculate and store in the kvs the benefit-cost ratio data for block. A value is stored with key :func:`openquake.kvs.tokens.bcr_block_key`. See :func:`openquake.risk.job.general.compute_bcr_for_block` for result data structure spec. """ result = defaultdict(list) block = Block.from_kvs(self.job_ctxt.job_id, block_id) vulnerability_model_original = vulnerability.load_vuln_model_from_kvs( self.job_ctxt.job_id) vulnerability_model_retrofitted = ( vulnerability.load_vuln_model_from_kvs( self.job_ctxt.job_id, retrofitted=True)) steps = self.job_ctxt.oq_job_profile.lrem_steps_per_interval assets_getter = lambda site: BaseRiskCalculator.assets_at( self.job_ctxt.job_id, site) hazard_getter = lambda site: ( self._get_db_curve(hazard_input_site(self.job_ctxt, site))) bcr = api.bcr(api.classical(vulnerability_model_original, steps=steps), api.classical(vulnerability_model_retrofitted, steps=steps), float(self.job_ctxt.params["INTEREST_RATE"]), float(self.job_ctxt.params["ASSET_LIFE_EXPECTANCY"])) for asset_output in api.compute_on_sites( block.sites, assets_getter, hazard_getter, bcr): asset = asset_output.asset result[(asset.site.x, asset.site.y)].append(({ "bcr": asset_output.bcr, "eal_original": asset_output.eal_original, "eal_retrofitted": asset_output.eal_retrofitted}, asset.asset_ref)) bcr = result.items() bcr_block_key = kvs.tokens.bcr_block_key( self.job_ctxt.job_id, block_id) kvs.set_value_json_encoded(bcr_block_key, bcr) LOGGER.debug("bcr result for block %s: %r", block_id, bcr) return True