Пример #1
0
def compute_stddev_loss(vuln_function, ground_motion_field_set,
                        epsilon_provider, asset):
    """Compute the standard deviation of the losses for the given asset
    using the related ground motion field set and vulnerability function.

    :param vuln_function: the vulnerability function used to
        compute the loss ratios.
    :type vuln_function: :py:class:`openquake.shapes.VulnerabilityFunction`
    :param ground_motion_field_set: the set of ground motion
        fields used to compute the loss ratios.
    :type ground_motion_field_set: :py:class:`dict` with the following
        keys:
        **IMLs** - tuple of ground motion fields (float)
    :param epsilon_provider: service used to get the epsilon when
        using the sampled based algorithm.
    :type epsilon_provider: object that defines an :py:meth:`epsilon` method
    :param asset: the asset used to compute the loss ratios and losses.
    :type asset: :py:class:`dict` as provided by
        :py:class:`openquake.parser.exposure.ExposurePortfolioFile`
    """

    loss_ratios = general.compute_loss_ratios(
        vuln_function, ground_motion_field_set, epsilon_provider, asset)

    return _stddev_loss_from_loss_ratios(loss_ratios, asset)
Пример #2
0
def compute_uninsured_losses(vuln_function, gmvs, epsilon_provider, asset):
    """
    Compute losses for the given asset using the related set of ground
    motion values and vulnerability function.

    :param vuln_function: the vulnerability function used to compute the loss
        ratios.
    :type vuln_function: :py:class:`openquake.shapes.VulnerabilityFunction`
    :param gmvs: the set of ground motion values used to compute the loss
        ratios.
    :type gmvs: :py:class:`dict` with the following keys: **IMLs** - tuple
        of ground motion values (float).
    :param epsilon_provider: service used to get the epsilon when using the
        sampled based algorithm.
    :type epsilon_provider: object that defines an :py:meth:`epsilon` method
    :param asset: the asset used to compute the loss ratios and losses.
    :type asset: an :py:class:`openquake.db.model.ExposureData` instance
    """

    loss_ratios = general.compute_loss_ratios(
        vuln_function, gmvs, epsilon_provider, asset)

    losses = loss_ratios * asset.value

    return losses
Пример #3
0
 def get_loss_curve(point, vuln_function, asset):
     "Compute loss curve basing on GMF data"
     gmf_slice = gmf_slices[point.site]
     loss_ratios = general.compute_loss_ratios(
         vuln_function, gmf_slice, epsilon_provider, asset)
     loss_ratio_curve = general.compute_loss_ratio_curve(
         vuln_function, gmf_slice, epsilon_provider, asset,
         self.calc_proxy.oq_job_profile.loss_histogram_bins,
         loss_ratios=loss_ratios)
     return loss_ratio_curve.rescale_abscissae(asset["assetValue"])
Пример #4
0
        def get_loss_curve(point, vuln_function, asset):
            "Compute loss curve basing on GMF data"
            gmf_slice = gmf_slices[point.site]
            loss_ratios = general.compute_loss_ratios(
                vuln_function, gmf_slice, epsilon_provider, asset)
            loss_ratio_curve = general.compute_loss_ratio_curve(
                vuln_function, gmf_slice, epsilon_provider, asset,
                self.job_ctxt.oq_job_profile.loss_histogram_bins,
                loss_ratios=loss_ratios)

            aggregate_curve.append(loss_ratios * asset.value)

            return loss_ratio_curve.rescale_abscissae(asset.value)
Пример #5
0
    def compute_loss_ratios(self, asset, gmf_slice):
        """For a given asset and ground motion field, computes
        the loss ratios used to obtain the related loss ratio curve
        and aggregate loss curve."""

        epsilon_provider = general.EpsilonProvider(self.job_ctxt.params)

        vuln_function = self.vuln_curves.get(asset.taxonomy, None)

        if not vuln_function:
            LOGGER.error("Unknown vulnerability function %s for asset %s"
                         % (asset.taxonomy, asset.asset_ref))
            return None

        return general.compute_loss_ratios(vuln_function, gmf_slice,
                                           epsilon_provider, asset)
Пример #6
0
    def compute_loss_ratios(self, asset, gmf_slice):
        """For a given asset and ground motion field, computes
        the loss ratios used to obtain the related loss ratio curve
        and aggregate loss curve."""

        epsilon_provider = general.EpsilonProvider(self.job_ctxt.params)

        vuln_function = self.vuln_curves.get(asset.taxonomy, None)

        if not vuln_function:
            LOGGER.error("Unknown vulnerability function %s for asset %s" %
                         (asset.taxonomy, asset.asset_ref))
            return None

        return general.compute_loss_ratios(vuln_function, gmf_slice,
                                           epsilon_provider, asset)
Пример #7
0
        def get_loss_curve(point, vuln_function, asset):
            "Compute loss curve basing on GMF data"
            gmf_slice = gmf_slices[point.site]
            loss_ratios = general.compute_loss_ratios(vuln_function, gmf_slice,
                                                      epsilon_provider, asset)
            loss_ratio_curve = general.compute_loss_ratio_curve(
                vuln_function,
                gmf_slice,
                epsilon_provider,
                asset,
                self.job_ctxt.oq_job_profile.loss_histogram_bins,
                loss_ratios=loss_ratios)

            aggregate_curve.append(loss_ratios * asset.value)

            return loss_ratio_curve.rescale_abscissae(asset.value)
Пример #8
0
    def _compute_loss_ratios(self, asset, gmf):
        """
        Compute the loss ratios for the given asset and associated ground
        motion field.
        """

        epsilon_provider = general.EpsilonProvider(self.job_ctxt.params)
        vulnerability_function = self.vulnerability_curves.get(
                                                        asset.taxonomy, None)

        if not vulnerability_function:
            LOGGER.error("Unknown vulnerability function %s for asset %s"
                         % (asset.taxonomy, asset.asset_ref))
            return None

        return general.compute_loss_ratios(vulnerability_function, gmf,
            epsilon_provider, asset)
Пример #9
0
    def _compute_loss_ratios(self, asset, gmf):
        """
        Compute the loss ratios for the given asset and associated ground
        motion field.
        """

        epsilon_provider = general.EpsilonProvider(self.job_ctxt.params)
        vulnerability_function = self.vulnerability_curves.get(
            asset.taxonomy, None)

        if not vulnerability_function:
            LOGGER.error("Unknown vulnerability function %s for asset %s" %
                         (asset.taxonomy, asset.asset_ref))
            return None

        return general.compute_loss_ratios(vulnerability_function, gmf,
                                           epsilon_provider, asset)
Пример #10
0
        def get_loss_curve(site, vuln_function, asset):
            "Compute loss curve basing on GMF data"
            gmvs = self._get_gmvs_at(general.hazard_input_site(
                    self.job_ctxt, site))

            gmf_slice = {"IMLs": gmvs, "TSES": self._tses(),
                    "TimeSpan": self._time_span()}

            loss_ratios = general.compute_loss_ratios(
                vuln_function, gmf_slice, epsilon_provider, asset)
            loss_ratio_curve = general.compute_loss_ratio_curve(
                vuln_function, gmf_slice, epsilon_provider, asset,
                self.job_ctxt.oq_job_profile.loss_histogram_bins,
                loss_ratios=loss_ratios)

            aggregate_curve.append(loss_ratios * asset.value)

            return loss_ratio_curve.rescale_abscissae(asset.value)
Пример #11
0
def compute_stddev_loss(vuln_function, gmf_set, epsilon_provider, asset):
    """Compute the standard deviation of the losses for the given asset
    using the related ground motion field set and vulnerability function.

    :param vuln_function: the vulnerability function used to
        compute the loss ratios.
    :type vuln_function: :py:class:`openquake.shapes.VulnerabilityFunction`
    :param gmf_set: the ground motion fields used to compute the loss ratios
    :type gmf_set: :py:class:`dict` with the following
        keys:
        **IMLs** - tuple of ground motion fields (float)
    :param epsilon_provider: service used to get the epsilon when
        using the sampled based algorithm.
    :type epsilon_provider: object that defines an :py:meth:`epsilon` method
    :param asset: the asset used to compute the loss ratios and losses.
    :type asset: an :py:class:`openquake.db.model.ExposureData` instance
    """

    loss_ratios = general.compute_loss_ratios(vuln_function, gmf_set,
                                              epsilon_provider, asset)

    return _stddev_loss_from_loss_ratios(loss_ratios, asset)
Пример #12
0
        def get_loss_curve(site, vuln_function, asset):
            "Compute loss curve basing on GMF data"
            gmvs = self._get_gmvs_at(
                general.hazard_input_site(self.job_ctxt, site))

            gmf_slice = {
                "IMLs": gmvs,
                "TSES": self._tses(),
                "TimeSpan": self._time_span()
            }

            loss_ratios = general.compute_loss_ratios(vuln_function, gmf_slice,
                                                      epsilon_provider, asset)
            loss_ratio_curve = general.compute_loss_ratio_curve(
                vuln_function,
                gmf_slice,
                epsilon_provider,
                asset,
                self.job_ctxt.oq_job_profile.loss_histogram_bins,
                loss_ratios=loss_ratios)

            aggregate_curve.append(loss_ratios * asset.value)

            return loss_ratio_curve.rescale_abscissae(asset.value)
Пример #13
0
    def compute_risk(self, block_id, **kwargs):
        """
        This method will perform two distinct (but similar) computations and
        return a result for each computation. The computations are as follows:

        First:

        For a given block of sites, compute loss values for all assets in the
        block. This computation will yield a single loss value per realization
        for the region block.

        Second:

        For each asset in the given block of sites, we need compute loss
        (where loss = loss_ratio * asset_value) for each realization. This
        gives 1 loss value _per_ asset _per_ realization. We then need to take
        the mean & standard deviation.

        Other info:

        The GMF data for each realization is stored in the KVS by the preceding
        scenario hazard job.

        :param block_id: id of the region block data we need to pull from the
            KVS
        :type block_id: str
        :keyword vuln_model:
            dict of :py:class:`openquake.shapes.VulnerabilityFunction` objects,
            keyed by the vulnerability function name as a string
        :keyword epsilon_provider:
            :py:class:`openquake.risk.job.EpsilonProvider` object

        :returns: 2-tuple of the following data:
            * 1-dimensional :py:class:`numpy.ndarray` of loss values for this
                region block (again, 1 value per realization)

            * list of 2-tuples containing site, loss, and asset
                information.

                The first element of each 2-tuple shall be a
                :py:class:`openquake.shapes.Site` object, which represents the
                geographical location of the asset loss.

                The second element shall be a list of
                2-tuples of dicts representing the loss and asset data (in that
                order).

                Example::

                    [(<Site(-117.0, 38.0)>, [
                        ({'mean_loss': 200.0, 'stddev_loss': 100},
                            {'assetID': 'a171'}),
                        ({'mean_loss': 200.0, 'stddev_loss': 100},
                            {'assetID': 'a187'})
                    ]),
                     (<Site(-118.0, 39.0)>, [
                        ({'mean_loss': 50, 'stddev_loss': 50.0},
                            {'assetID': 'a192'})
                    ])]
        """

        vuln_model = kwargs["vuln_model"]
        insured_losses = kwargs["insured_losses"]
        epsilon_provider = general.EpsilonProvider(self.job_ctxt.params)
        block = general.Block.from_kvs(self.job_ctxt.job_id, block_id)

        block_losses = []
        loss_map_data = {}

        for site in block.sites:
            gmvs = {"IMLs": general.load_gmvs_at(
                    self.job_ctxt.job_id, general.hazard_input_site(
                    self.job_ctxt, site))}

            assets = general.BaseRiskCalculator.assets_at(
                self.job_ctxt.job_id, site)

            for asset in assets:
                vuln_function = vuln_model[asset.taxonomy]

                loss_ratios = general.compute_loss_ratios(
                    vuln_function, gmvs, epsilon_provider, asset)
                losses = loss_ratios * asset.value

                if insured_losses:
                    losses = general.compute_insured_losses(asset, losses)

                asset_site = shapes.Site(asset.site.x, asset.site.y)

                loss = ({
                    "mean_loss": numpy.mean(losses),
                    "stddev_loss": numpy.std(losses, ddof=1)}, {
                    "assetID": asset.asset_ref
                })

                block_losses.append(losses)
                collect_block_data(loss_map_data, asset_site, loss)

        sum_block_losses = reduce(lambda x, y: x + y, block_losses)
        return sum_block_losses, loss_map_data
Пример #14
0
    def compute_risk(self, block_id, **kwargs):
        """
        This method will perform two distinct (but similar) computations and
        return a result for each computation. The computations are as follows:

        First:

        For a given block of sites, compute loss values for all assets in the
        block. This computation will yield a single loss value per realization
        for the region block.

        Second:

        For each asset in the given block of sites, we need compute loss
        (where loss = loss_ratio * asset_value) for each realization. This
        gives 1 loss value _per_ asset _per_ realization. We then need to take
        the mean & standard deviation.

        Other info:

        The GMF data for each realization is stored in the KVS by the preceding
        scenario hazard job.

        :param block_id: id of the region block data we need to pull from the
            KVS
        :type block_id: str
        :keyword vuln_model:
            dict of :py:class:`openquake.shapes.VulnerabilityFunction` objects,
            keyed by the vulnerability function name as a string
        :keyword epsilon_provider:
            :py:class:`openquake.risk.job.EpsilonProvider` object

        :returns: 2-tuple of the following data:
            * 1-dimensional :py:class:`numpy.ndarray` of loss values for this
                region block (again, 1 value per realization)

            * list of 2-tuples containing site, loss, and asset
                information.

                The first element of each 2-tuple shall be a
                :py:class:`openquake.shapes.Site` object, which represents the
                geographical location of the asset loss.

                The second element shall be a list of
                2-tuples of dicts representing the loss and asset data (in that
                order).

                Example::

                    [(<Site(-117.0, 38.0)>, [
                        ({'mean_loss': 200.0, 'stddev_loss': 100},
                            {'assetID': 'a171'}),
                        ({'mean_loss': 200.0, 'stddev_loss': 100},
                            {'assetID': 'a187'})
                    ]),
                     (<Site(-118.0, 39.0)>, [
                        ({'mean_loss': 50, 'stddev_loss': 50.0},
                            {'assetID': 'a192'})
                    ])]
        """

        vuln_model = kwargs["vuln_model"]
        insured_losses = kwargs["insured_losses"]
        epsilon_provider = general.EpsilonProvider(self.job_ctxt.params)
        block = general.Block.from_kvs(self.job_ctxt.job_id, block_id)

        block_losses = []
        loss_map_data = {}

        for site in block.sites:
            gmvs = {
                "IMLs":
                general.load_gmvs_at(
                    self.job_ctxt.job_id,
                    general.hazard_input_site(self.job_ctxt, site))
            }

            assets = general.BaseRiskCalculator.assets_at(
                self.job_ctxt.job_id, site)

            for asset in assets:
                vuln_function = vuln_model[asset.taxonomy]

                loss_ratios = general.compute_loss_ratios(
                    vuln_function, gmvs, epsilon_provider, asset)
                losses = loss_ratios * asset.value

                if insured_losses:
                    losses = general.compute_insured_losses(asset, losses)

                asset_site = shapes.Site(asset.site.x, asset.site.y)

                loss = ({
                    "mean_loss": numpy.mean(losses),
                    "stddev_loss": numpy.std(losses, ddof=1)
                }, {
                    "assetID": asset.asset_ref
                })

                block_losses.append(losses)
                collect_block_data(loss_map_data, asset_site, loss)

        sum_block_losses = reduce(lambda x, y: x + y, block_losses)
        return sum_block_losses, loss_map_data