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_loss(self, block_id): """Compute risk for a block of sites, that means: * loss ratio curves * loss curves * conditional losses * (partial) aggregate loss curve """ self.vulnerability_model = vulnerability.load_vuln_model_from_kvs( self.job_ctxt.job_id) 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 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()} assets_getter = lambda site: general.BaseRiskCalculator.assets_at( self.job_ctxt.job_id, site) probabilistic_event_based_calculator = api.probabilistic_event_based( self.vulnerability_model, loss_histogram_bins, seed, correlation_type) calculator = api.conditional_losses(general.conditional_loss_poes( self.job_ctxt.params), probabilistic_event_based_calculator) if self.job_ctxt.params.get("INSURED_LOSSES"): calculator = api.insured_curves(self.vulnerability_model, loss_histogram_bins, seed, correlation_type, api.insured_losses(calculator)) for asset_output in api.compute_on_sites(block.sites, assets_getter, hazard_getter, calculator): location = asset_output.asset.site point = self.job_ctxt.region.grid.point_at( shapes.Site(location.x, location.y)) self._loss_ratio_curve_on_kvs( point.column, point.row, asset_output.loss_ratio_curve, asset_output.asset) self._loss_curve_on_kvs( point.column, point.row, asset_output.loss_curve, asset_output.asset) for poe, loss in asset_output.conditional_losses.items(): key = kvs.tokens.loss_key( self.job_ctxt.job_id, point.row, point.column, asset_output.asset.asset_ref, poe) kvs.get_client().set(key, loss) if self.job_ctxt.params.get("INSURED_LOSSES"): self._insured_loss_curve_on_kvs( point.column, point.row, asset_output.insured_loss_curve, asset_output.asset) self._insured_loss_ratio_curve_on_kvs( point.column, point.row, asset_output.insured_loss_ratio_curve, asset_output.asset) return probabilistic_event_based_calculator.aggregate_losses