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_curves = vulnerability.load_vuln_model_from_kvs( self.job_ctxt.job_id) block = general.Block.from_kvs(self.job_ctxt.job_id, block_id) seed, correlation_type = self._get_correlation_type() job_id = self.job_ctxt.job_id def hazard_getter(site): point = self.job_ctxt.region.grid.point_at(site) gmvs = self._get_gmvs_at(general.hazard_input_site( self.job_ctxt, site)) gmf = {"IMLs": gmvs, "TSES": self._tses(), "TimeSpan": self._time_span()} return point, gmf def on_asset_complete(asset, point, loss_ratio_curve, loss_curve, loss_conditionals, insured_curve, insured_loss_ratio_curve): self._loss_ratio_curve_on_kvs( point.column, point.row, loss_ratio_curve, asset) self._loss_curve_on_kvs( point.column, point.row, loss_curve, asset) for loss_poe, loss_conditional in loss_conditionals.items(): key = kvs.tokens.loss_key(job_id, point.row, point.column, asset.asset_ref, loss_poe) kvs.get_client().set(key, loss_conditional) if self.job_ctxt.params.get("INSURED_LOSSES"): self._insured_loss_curve_on_kvs( point.column, point.row, insured_curve, asset) self._insured_loss_ratio_curve_on_kvs( point.column, point.row, insured_loss_ratio_curve, asset) losses = event_based.compute( block.sites, lambda site: general.BaseRiskCalculator.assets_at( self.job_ctxt.job_id, site), self.vulnerability_curves, hazard_getter, self.job_ctxt.oq_job_profile.loss_histogram_bins, general.conditional_loss_poes(self.job_ctxt.params), self.job_ctxt.params.get("INSURED_LOSSES"), seed, correlation_type, on_asset_complete) return losses
def _compute_loss(self, block_id): """ Calculate and store in the kvs the loss data. """ block = general.Block.from_kvs(self.job_ctxt.job_id, block_id) vuln_curves = vulnerability.load_vuln_model_from_kvs( self.job_ctxt.job_id) for site in block.sites: point = self.job_ctxt.region.grid.point_at(site) hazard_curve = self._get_db_curve( general.hazard_input_site(self.job_ctxt, site)) assets = general.BaseRiskCalculator.assets_at( self.job_ctxt.job_id, site) for asset in assets: loss_ratio_curve = self.compute_loss_ratio_curve( point, asset, hazard_curve, vuln_curves) if loss_ratio_curve: loss_curve = self.compute_loss_curve( point, loss_ratio_curve, asset) for poe in conditional_loss_poes(self.job_ctxt.params): compute_conditional_loss( self.job_ctxt.job_id, point.column, point.row, loss_curve, asset, poe) return True
def _compute_loss(self, block_id): """ Calculate and store in the kvs the loss data. """ block = general.Block.from_kvs(self.calc_proxy.job_id, block_id) vuln_curves = vulnerability.load_vuln_model_from_kvs( self.calc_proxy.job_id) for point in block.grid(self.calc_proxy.region): hazard_curve = self._get_db_curve(point.site) asset_key = kvs.tokens.asset_key(self.calc_proxy.job_id, point.row, point.column) for asset in kvs.get_list_json_decoded(asset_key): LOGGER.debug("processing asset %s" % (asset)) loss_ratio_curve = self.compute_loss_ratio_curve( point, asset, hazard_curve, vuln_curves) if loss_ratio_curve: loss_curve = self.compute_loss_curve(point, loss_ratio_curve, asset) for loss_poe in conditional_loss_poes( self.calc_proxy.params): compute_conditional_loss( self.calc_proxy.job_id, point.column, point.row, loss_curve, asset, loss_poe) return True
def _compute_loss(self, block_id): """ Calculate and store in the kvs the loss data. """ block = general.Block.from_kvs(self.job_ctxt.job_id, block_id) vuln_curves = vulnerability.load_vuln_model_from_kvs( self.job_ctxt.job_id) for site in block.sites: point = self.job_ctxt.region.grid.point_at(site) hazard_curve = self._get_db_curve( general.hazard_input_site(self.job_ctxt, site)) assets = general.BaseRiskCalculator.assets_at( self.job_ctxt.job_id, site) for asset in assets: loss_ratio_curve = self.compute_loss_ratio_curve( point, asset, hazard_curve, vuln_curves) if loss_ratio_curve: loss_curve = self.compute_loss_curve( point, loss_ratio_curve, asset) for poe in conditional_loss_poes(self.job_ctxt.params): compute_conditional_loss(self.job_ctxt.job_id, point.column, point.row, loss_curve, asset, poe) return True
def _compute_loss(self, block_id): """ Calculate and store in the kvs the loss data. """ block = general.Block.from_kvs(self.job_ctxt.job_id, block_id) vuln_curves = vulnerability.load_vuln_model_from_kvs( self.job_ctxt.job_id) for point in block.grid(self.job_ctxt.region): hazard_curve = self._get_db_curve(point.site) assets = self.assets_for_cell(self.job_ctxt.job_id, point.site) for asset in assets: LOGGER.debug("processing asset %s" % asset) loss_ratio_curve = self.compute_loss_ratio_curve( point, asset, hazard_curve, vuln_curves) if loss_ratio_curve: loss_curve = self.compute_loss_curve( point, loss_ratio_curve, asset) for poe in conditional_loss_poes(self.job_ctxt.params): compute_conditional_loss( self.job_ctxt.job_id, point.column, point.row, loss_curve, asset, poe) return True
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.slice_gmfs(block_id) self.vuln_curves = vulnerability.load_vuln_model_from_kvs( self.job_ctxt.job_id) block = general.Block.from_kvs(self.job_ctxt.job_id, block_id) # aggregate the losses for this block aggregate_curve = general.AggregateLossCurve() for site in block.sites: point = self.job_ctxt.region.grid.point_at(site) key = kvs.tokens.gmf_set_key( self.job_ctxt.job_id, point.column, point.row) gmf = kvs.get_value_json_decoded(key) assets = general.BaseRiskCalculator.assets_at( self.job_ctxt.job_id, site) for asset in assets: # loss ratios, used both to produce the curve # and to aggregate the losses loss_ratios = self.compute_loss_ratios(asset, gmf) loss_ratio_curve = self.compute_loss_ratio_curve( point.column, point.row, asset, gmf, loss_ratios) aggregate_curve.append(loss_ratios * asset.value) if loss_ratio_curve: loss_curve = self.compute_loss_curve( point.column, point.row, loss_ratio_curve, asset) for loss_poe in general.conditional_loss_poes( self.job_ctxt.params): general.compute_conditional_loss( self.job_ctxt.job_id, point.column, point.row, loss_curve, asset, loss_poe) return aggregate_curve.losses
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.slice_gmfs(block_id) self.vuln_curves = vulnerability.load_vuln_model_from_kvs( self.job_ctxt.job_id) block = general.Block.from_kvs(self.job_ctxt.job_id, block_id) # aggregate the losses for this block aggregate_curve = general.AggregateLossCurve() for site in block.sites: point = self.job_ctxt.region.grid.point_at(site) key = kvs.tokens.gmf_set_key(self.job_ctxt.job_id, point.column, point.row) gmf = kvs.get_value_json_decoded(key) assets = general.BaseRiskCalculator.assets_at( self.job_ctxt.job_id, site) for asset in assets: # loss ratios, used both to produce the curve # and to aggregate the losses loss_ratios = self.compute_loss_ratios(asset, gmf) loss_ratio_curve = self.compute_loss_ratio_curve( point.column, point.row, asset, gmf, loss_ratios) aggregate_curve.append(loss_ratios * asset.value) if loss_ratio_curve: loss_curve = self.compute_loss_curve( point.column, point.row, loss_ratio_curve, asset) for loss_poe in general.conditional_loss_poes( self.job_ctxt.params): general.compute_conditional_loss( self.job_ctxt.job_id, point.column, point.row, loss_curve, asset, loss_poe) return aggregate_curve.losses
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.vuln_curves = vulnerability.load_vuln_model_from_kvs( self.job_ctxt.job_id) block = general.Block.from_kvs(self.job_ctxt.job_id, block_id) # aggregate the losses for this block aggregate_curve = general.AggregateLossCurve() for site in block.sites: point = self.job_ctxt.region.grid.point_at(site) gmvs = self._get_gmvs_at(general.hazard_input_site( self.job_ctxt, site)) gmf = {"IMLs": gmvs, "TSES": self._tses(), "TimeSpan": self._time_span()} assets = general.BaseRiskCalculator.assets_at( self.job_ctxt.job_id, site) for asset in assets: # loss ratios, used both to produce the curve # and to aggregate the losses loss_ratios = self.compute_loss_ratios(asset, gmf) loss_ratio_curve = self.compute_loss_ratio_curve( point.column, point.row, asset, gmf, loss_ratios) aggregate_curve.append(loss_ratios * asset.value) if loss_ratio_curve: loss_curve = self.compute_loss_curve( point.column, point.row, loss_ratio_curve, asset) for loss_poe in general.conditional_loss_poes( self.job_ctxt.params): general.compute_conditional_loss( self.job_ctxt.job_id, point.column, point.row, loss_curve, asset, loss_poe) return aggregate_curve.losses
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.slice_gmfs(block_id) self.vuln_curves = vulnerability.load_vuln_model_from_kvs( self.calc_proxy.job_id) block = general.Block.from_kvs(self.calc_proxy.job_id, block_id) # aggregate the losses for this block aggregate_curve = general.AggregateLossCurve() for point in block.grid(self.calc_proxy.region): key = kvs.tokens.gmf_set_key(self.calc_proxy.job_id, point.column, point.row) gmf_slice = kvs.get_value_json_decoded(key) asset_key = kvs.tokens.asset_key( self.calc_proxy.job_id, point.row, point.column) for asset in kvs.get_list_json_decoded(asset_key): LOGGER.debug("Processing asset %s" % (asset)) # loss ratios, used both to produce the curve # and to aggregate the losses loss_ratios = self.compute_loss_ratios(asset, gmf_slice) loss_ratio_curve = self.compute_loss_ratio_curve( point.column, point.row, asset, gmf_slice, loss_ratios) aggregate_curve.append(loss_ratios * asset["assetValue"]) if loss_ratio_curve: loss_curve = self.compute_loss_curve( point.column, point.row, loss_ratio_curve, asset) for loss_poe in general.conditional_loss_poes( self.calc_proxy.params): general.compute_conditional_loss( self.calc_proxy.job_id, point.column, point.row, loss_curve, asset, loss_poe) return aggregate_curve.losses
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_curves = vulnerability.load_vuln_model_from_kvs( self.job_ctxt.job_id) block = general.Block.from_kvs(self.job_ctxt.job_id, block_id) # aggregate the losses for this block aggregate_curve = general.AggregateLossCurve() for site in block.sites: point = self.job_ctxt.region.grid.point_at(site) gmf = self._load_ground_motion_field(site) assets = general.BaseRiskCalculator.assets_at( self.job_ctxt.job_id, site) for asset in assets: # loss ratios, used both to produce the curve # and to aggregate the losses loss_ratios = self._compute_loss_ratios(asset, gmf) loss_ratio_curve = self._compute_loss_ratio_curve( asset, gmf, loss_ratios) self._loss_ratio_curve_on_kvs( point.column, point.row, loss_ratio_curve, asset) losses = loss_ratios * asset.value aggregate_curve.append(losses) if loss_ratio_curve: loss_curve = self._compute_loss_curve( loss_ratio_curve, asset) self._loss_curve_on_kvs(point.column, point.row, loss_curve, asset) for loss_poe in general.conditional_loss_poes( self.job_ctxt.params): general.compute_conditional_loss( self.job_ctxt.job_id, point.column, point.row, loss_curve, asset, loss_poe) if self.job_ctxt.params.get("INSURED_LOSSES"): insured_losses = general.compute_insured_losses( asset, losses) insured_loss_ratio_curve = ( self._compute_insured_loss_ratio_curve( insured_losses, asset, gmf)) self._insured_loss_ratio_curve_on_kvs(point.column, point.row, insured_loss_ratio_curve, asset) insured_loss_curve = self._compute_loss_curve( insured_loss_ratio_curve, asset) self._insured_loss_curve_on_kvs(point.column, point.row, insured_loss_curve, asset) return aggregate_curve.losses
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_curves = vulnerability.load_vuln_model_from_kvs( self.job_ctxt.job_id) block = general.Block.from_kvs(self.job_ctxt.job_id, block_id) # aggregate the losses for this block aggregate_curve = general.AggregateLossCurve() for site in block.sites: point = self.job_ctxt.region.grid.point_at(site) gmf = self._load_ground_motion_field(site) assets = general.BaseRiskCalculator.assets_at( self.job_ctxt.job_id, site) for asset in assets: # loss ratios, used both to produce the curve # and to aggregate the losses loss_ratios = self._compute_loss_ratios(asset, gmf) loss_ratio_curve = self._compute_loss_ratio_curve( asset, gmf, loss_ratios) self._loss_ratio_curve_on_kvs(point.column, point.row, loss_ratio_curve, asset) losses = loss_ratios * asset.value aggregate_curve.append(losses) if loss_ratio_curve: loss_curve = self._compute_loss_curve( loss_ratio_curve, asset) self._loss_curve_on_kvs(point.column, point.row, loss_curve, asset) for loss_poe in general.conditional_loss_poes( self.job_ctxt.params): general.compute_conditional_loss( self.job_ctxt.job_id, point.column, point.row, loss_curve, asset, loss_poe) if self.job_ctxt.params.get("INSURED_LOSSES"): insured_losses = general.compute_insured_losses( asset, losses) insured_loss_ratio_curve = ( self._compute_insured_loss_ratio_curve( insured_losses, asset, gmf)) self._insured_loss_ratio_curve_on_kvs( point.column, point.row, insured_loss_ratio_curve, asset) insured_loss_curve = self._compute_loss_curve( insured_loss_ratio_curve, asset) self._insured_loss_curve_on_kvs( point.column, point.row, insured_loss_curve, asset) return aggregate_curve.losses
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