Ejemplo n.º 1
0
    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
Ejemplo n.º 2
0
    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
Ejemplo n.º 3
0
    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
Ejemplo n.º 4
0
    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
Ejemplo n.º 5
0
    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
Ejemplo n.º 6
0
    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
Ejemplo n.º 7
0
    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
Ejemplo n.º 8
0
    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
Ejemplo n.º 9
0
    def test_that_conditional_loss_is_in_kvs(self):
        asset = GRID_ASSETS[(0, 1)]
        loss_poe = 0.1
        job_id = "1"
        row = 0
        col = 1
        loss_curve = shapes.Curve([(0.21, 0.131), (0.24, 0.108),
                                   (0.27, 0.089), (0.30, 0.066)])

        # should set in kvs the conditional loss
        general.compute_conditional_loss(job_id, col, row, loss_curve, asset,
                                         loss_poe)
        loss_key = kvs.tokens.loss_key(job_id, row, col, asset.asset_ref,
                                       loss_poe)
        self.assertTrue(kvs.get_client().get(loss_key))
Ejemplo n.º 10
0
    def test_that_conditional_loss_is_in_kvs(self):
        asset = GRID_ASSETS[(0, 1)]
        loss_poe = 0.1
        job_id = "1"
        row = 0
        col = 1
        loss_curve = shapes.Curve([(0.21, 0.131), (0.24, 0.108), (0.27, 0.089),
                                   (0.30, 0.066)])

        # should set in kvs the conditional loss
        general.compute_conditional_loss(job_id, col, row, loss_curve, asset,
                                         loss_poe)
        loss_key = kvs.tokens.loss_key(job_id, row, col, asset.asset_ref,
                                       loss_poe)
        self.assertTrue(kvs.get_client().get(loss_key))
Ejemplo n.º 11
0
    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
Ejemplo n.º 12
0
    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