示例#1
0
    def compute_loss_ratio_curve(self, col, row, asset, gmf_slice):
        """Compute the loss ratio curve for a single site."""

        # fail if the asset has an unknown vulnerability code
        vuln_function = self.vuln_curves.get(
                asset["vulnerabilityFunctionReference"], None)

        if not vuln_function:
            LOGGER.error(
                "Unknown vulnerability function %s for asset %s"
                % (asset["vulnerabilityFunctionReference"], asset["assetID"]))

            return None

        epsilon_provider = risk_job.EpsilonProvider(self.params)
        loss_ratio_curve = probabilistic_event_based.compute_loss_ratio_curve(
                vuln_function, gmf_slice, epsilon_provider, asset,
                self._get_number_of_samples())

        # NOTE(JMC): Early exit if the loss ratio is all zeros
        if not False in (loss_ratio_curve.ordinates == 0.0):
            return None

        key = kvs.tokens.loss_ratio_key(self.id, row, col, asset["assetID"])
        kvs.set(key, loss_ratio_curve.to_json())

        LOGGER.warn("RESULT: loss ratio curve is %s, write to key %s" % (
                loss_ratio_curve, key))

        return loss_ratio_curve
示例#2
0
    def compute_loss_ratio_curve(self, point, asset, hazard_curve):
        """ Computes the loss ratio curve and stores in kvs
            the curve itself """

        # we get the vulnerability function related to the asset
        vuln_function = self.vuln_curves.get(
            asset["vulnerabilityFunctionReference"], None)

        if not vuln_function:
            LOGGER.error(
                "Unknown vulnerability function %s for asset %s"
                % (asset["vulnerabilityFunctionReference"],
                asset["assetID"]))

            return None

        loss_ratio_curve = cpsha_based.compute_loss_ratio_curve(
            vuln_function, hazard_curve)

        loss_key = kvs.tokens.loss_ratio_key(
            self.job_id, point.row, point.column, asset['assetID'])

        kvs.set(loss_key, loss_ratio_curve.to_json())

        return loss_ratio_curve
示例#3
0
    def compute_loss_ratio_curve(self, col, row, asset, gmf_slice, loss_ratios):
        """Compute the loss ratio curve for a single asset."""

        vuln_function = self.vuln_curves.get(asset["vulnerabilityFunctionReference"], None)

        if not vuln_function:
            LOGGER.error(
                "Unknown vulnerability function %s for asset %s"
                % (asset["vulnerabilityFunctionReference"], asset["assetID"])
            )

            return None

        epsilon_provider = general.EpsilonProvider(self.params)

        loss_ratio_curve = prob.compute_loss_ratio_curve(
            vuln_function, gmf_slice, epsilon_provider, asset, self._get_number_of_samples(), loss_ratios=loss_ratios
        )

        # NOTE (jmc): Early exit if the loss ratio is all zeros
        if not False in (loss_ratio_curve.ordinates == 0.0):
            return None

        key = kvs.tokens.loss_ratio_key(self.job_id, row, col, asset["assetID"])

        kvs.set(key, loss_ratio_curve.to_json())

        LOGGER.debug("Loss ratio curve is %s, write to key %s" % (loss_ratio_curve, key))

        return loss_ratio_curve
示例#4
0
    def compute_loss_ratio_curve(self, col, row, asset, gmf_slice):
        """Compute the loss ratio curve for a single site."""

        # fail if the asset has an unknown vulnerability code
        vuln_function = self.vuln_curves.get(
            asset["vulnerabilityFunctionReference"], None)

        if not vuln_function:
            LOGGER.error(
                "Unknown vulnerability function %s for asset %s" %
                (asset["vulnerabilityFunctionReference"], asset["assetID"]))

            return None

        epsilon_provider = risk_job.EpsilonProvider(self.params)
        loss_ratio_curve = probabilistic_event_based.compute_loss_ratio_curve(
            vuln_function, gmf_slice, epsilon_provider, asset,
            self._get_number_of_samples())

        # NOTE(JMC): Early exit if the loss ratio is all zeros
        if not False in (loss_ratio_curve.ordinates == 0.0):
            return None

        key = kvs.tokens.loss_ratio_key(self.id, row, col, asset["assetID"])
        kvs.set(key, loss_ratio_curve.to_json())

        LOGGER.warn("RESULT: loss ratio curve is %s, write to key %s" %
                    (loss_ratio_curve, key))

        return loss_ratio_curve
示例#5
0
    def compute_conditional_loss(self, col, row, loss_curve, asset, loss_poe):
        """ Compute the conditional loss for a loss curve and probability of 
        exceedance """

        loss_conditional = common.compute_conditional_loss(loss_curve, loss_poe)
        key = kvs.tokens.loss_key(self.id, row, col, asset["AssetID"], loss_poe)

        LOGGER.debug("RESULT: conditional loss is %s, write to key %s" % (
            loss_conditional, key))
        kvs.set(key, loss_conditional)
示例#6
0
    def compute_loss_curve(self, column, row, loss_ratio_curve, asset):
        """Compute the loss curve for a single site."""
        if asset is None:
            return None

        loss_curve = loss_ratio_curve.rescale_abscissae(asset["assetValue"])
        key = kvs.tokens.loss_curve_key(self.id, row, column, asset["assetID"])

        LOGGER.warn("RESULT: loss curve is %s, write to key %s" %
                    (loss_curve, key))
        kvs.set(key, loss_curve.to_json())
        return loss_curve
示例#7
0
    def compute_loss_curve(self, column, row, loss_ratio_curve, asset):
        """Compute the loss curve for a single site."""
        if asset is None:
            return None

        loss_curve = loss_ratio_curve.rescale_abscissae(asset["assetValue"])
        key = kvs.tokens.loss_curve_key(self.id, row, column, asset["assetID"])

        LOGGER.warn("RESULT: loss curve is %s, write to key %s" % (
                loss_curve, key))
        kvs.set(key, loss_curve.to_json())
        return loss_curve
示例#8
0
    def compute_conditional_loss(self, col, row, loss_curve, asset, loss_poe):
        """ Compute the conditional loss for a loss curve and probability of
        exceedance """

        loss_conditional = common.compute_conditional_loss(
            loss_curve, loss_poe)

        key = kvs.tokens.loss_key(self.id, row, col, asset["assetID"],
                                  loss_poe)

        LOGGER.debug("RESULT: conditional loss is %s, write to key %s" %
                     (loss_conditional, key))
        kvs.set(key, loss_conditional)
示例#9
0
def compute_conditional_loss(job_id, col, row, loss_curve, asset, loss_poe):
    """Compute the conditional loss for a loss curve and Probability of
    Exceedance (PoE)."""

    loss_conditional = common.compute_conditional_loss(
        loss_curve, loss_poe)

    key = kvs.tokens.loss_key(
            job_id, row, col, asset["assetID"], loss_poe)

    LOG.debug("Conditional loss is %s, write to key %s" %
            (loss_conditional, key))

    kvs.set(key, loss_conditional)
示例#10
0
    def setUp(self):
        self.job_id = 1234
        
        self.params = {}
        self.params["OUTPUT_DIR"] = test.OUTPUT_DIR
        
        # parameters needed to compute tses and time span
        self.params["NUMBER_OF_SEISMICITY_HISTORIES"] = 10
        self.params["NUMBER_OF_LOGIC_TREE_SAMPLES"] = 2
        self.params["INVESTIGATION_TIME"] = 50.0
        self.params["AGGREGATE_LOSS_CURVE"] = 1

        self.engine = job.Job(self.params,  self.job_id, ".")
        
        # adding the curves to kvs
        kvs.set(kvs.tokens.loss_curve_key(self.job_id, 1, 1, 5),
                shapes.Curve([(1.0, 0.1), (2.0, 0.1)]).to_json())

        kvs.set(kvs.tokens.loss_curve_key(self.job_id, 1, 2, 5),
                shapes.Curve([(3.0, 0.1), (4.0, 0.1)]).to_json())

        kvs.set(kvs.tokens.loss_curve_key(self.job_id, 1, 3, 5),
                shapes.Curve([(5.0, 0.1), (6.0, 0.1)]).to_json())
        
        # deleting old file
        self._delete_test_file()
示例#11
0
    def compute_loss_curve(self, point, loss_ratio_curve, asset):
        """
        Computes the loss ratio and store it in kvs to provide
        data to the @output decorator which does the serialization
        in the RiskJobMixin, more details inside
        openquake.risk.job.RiskJobMixin -- for details see
        RiskJobMixin._write_output_for_block and the output decorator

        :param point: the point of the grid we want to compute
        :type point: :py:class:`openquake.shapes.GridPoint`
        :param loss_ratio_curve: the loss ratio curve
        :type loss_ratio_curve: :py:class `openquake.shapes.Curve`
        :param asset: the asset for which to compute the loss curve
        :type asset: :py:class:`dict` as provided by
               :py:class:`openquake.parser.exposure.ExposurePortfolioFile`
        """

        loss_curve = compute_loss_curve(loss_ratio_curve, asset['assetValue'])
        loss_key = kvs.tokens.loss_curve_key(self.job_id, point.row,
                                             point.column, asset['assetID'])

        kvs.set(loss_key, loss_curve.to_json())
示例#12
0
    def compute_loss_curve(self, point, loss_ratio_curve, asset):
        """
        Computes the loss ratio and store it in kvs to provide
        data to the @output decorator which does the serialization
        in the RiskJobMixin, more details inside
        openquake.risk.job.RiskJobMixin -- for details see
        RiskJobMixin._write_output_for_block and the output decorator

        :param point: the point of the grid we want to compute
        :type point: :py:class:`openquake.shapes.GridPoint`
        :param loss_ratio_curve: the loss ratio curve
        :type loss_ratio_curve: :py:class `openquake.shapes.Curve`
        :param asset: the asset for which to compute the loss curve
        :type asset: :py:class:`dict` as provided by
               :py:class:`openquake.parser.exposure.ExposurePortfolioFile`
        """

        loss_curve = compute_loss_curve(loss_ratio_curve, asset['assetValue'])
        loss_key = kvs.tokens.loss_curve_key(self.job_id, point.row,
            point.column, asset['assetID'])

        kvs.set(loss_key, loss_curve.to_json())
示例#13
0
    def compute_loss_ratio_curve(self, col, row, asset, gmf_slice ): # site_id
        """Compute the loss ratio curve for a single site."""
        # If the asset has a vuln function code we don't have loaded, return
        # fail
        vuln_function = self.vuln_curves.get(
                asset["VulnerabilityFunction"], None)
        if not vuln_function:
            LOGGER.error("Unknown vulnerability function %s for asset %s"
                % (asset["VulnerabilityFunction"], asset["AssetID"]))
            return None

        loss_ratio_curve = probabilistic_event_based.compute_loss_ratio_curve(
                vuln_function, gmf_slice)
        # NOTE(JMC): Early exit if the loss ratio is all zeros
        if not False in (loss_ratio_curve.ordinates == 0.0):
            return None
        key = kvs.tokens.loss_ratio_key(self.id, row, col, asset["AssetID"])
        
        LOGGER.warn("RESULT: loss ratio curve is %s, write to key %s" % (
                loss_ratio_curve, key))
            
        kvs.set(key, loss_ratio_curve.to_json())
        return loss_ratio_curve
示例#14
0
    def compute_loss_ratio_curve(self, point, asset, hazard_curve):
        """ Computes the loss ratio curve and stores in kvs
            the curve itself

        :param point: the point of the grid we want to compute
        :type point: :py:class:`openquake.shapes.GridPoint`
        :param asset: the asset used to compute the loss curve
        :type asset: :py:class:`dict` as provided by
            :py:class:`openquake.parser.exposure.ExposurePortfolioFile`
        :param hazard_curve: the hazard curve used to compute the
            loss ratio curve
        :type hazard_curve: :py:class:`openquake.shapes.Curve`
        """

        # we get the vulnerability function related to the asset

        vuln_function_reference = asset["vulnerabilityFunctionReference"]
        vuln_function = self.vuln_curves.get(
            vuln_function_reference, None)

        if not vuln_function:
            LOGGER.error(
                "Unknown vulnerability function %s for asset %s"
                % (asset["vulnerabilityFunctionReference"],
                asset["assetID"]))

            return None

        loss_ratio_curve = cpsha_based.compute_loss_ratio_curve(
            vuln_function, hazard_curve)

        loss_ratio_key = kvs.tokens.loss_ratio_key(
            self.job_id, point.row, point.column, asset['assetID'])

        kvs.set(loss_ratio_key, loss_ratio_curve.to_json())

        return loss_ratio_curve
示例#15
0
    def compute_loss_ratio_curve(self, point, asset, hazard_curve):
        """ Computes the loss ratio curve and stores in kvs
            the curve itself

        :param point: the point of the grid we want to compute
        :type point: :py:class:`openquake.shapes.GridPoint`
        :param asset: the asset used to compute the loss curve
        :type asset: :py:class:`dict` as provided by
            :py:class:`openquake.parser.exposure.ExposurePortfolioFile`
        :param hazard_curve: the hazard curve used to compute the
            loss ratio curve
        :type hazard_curve: :py:class:`openquake.shapes.Curve`
        """

        # we get the vulnerability function related to the asset

        vuln_function_reference = asset["vulnerabilityFunctionReference"]
        vuln_function = self.vuln_curves.get(vuln_function_reference, None)

        if not vuln_function:
            LOGGER.error(
                "Unknown vulnerability function %s for asset %s" %
                (asset["vulnerabilityFunctionReference"], asset["assetID"]))

            return None

        loss_ratio_curve = cpsha_based.compute_loss_ratio_curve(
            vuln_function, hazard_curve)

        loss_ratio_key = kvs.tokens.loss_ratio_key(self.job_id, point.row,
                                                   point.column,
                                                   asset['assetID'])

        kvs.set(loss_ratio_key, loss_ratio_curve.to_json())

        return loss_ratio_curve
示例#16
0
    def compute_loss_ratio_curve(self, point, asset, hazard_curve):
        """ Computes the loss ratio curve and stores in kvs
            the curve itself """

        # we get the vulnerability function related to the asset
        vuln_function = self.vuln_curves.get(
            asset["vulnerabilityFunctionReference"], None)

        if not vuln_function:
            LOGGER.error(
                "Unknown vulnerability function %s for asset %s" %
                (asset["vulnerabilityFunctionReference"], asset["assetID"]))

            return None

        loss_ratio_curve = cpsha_based.compute_loss_ratio_curve(
            vuln_function, hazard_curve)

        loss_key = kvs.tokens.loss_ratio_key(self.job_id, point.row,
                                             point.column, asset['assetID'])

        kvs.set(loss_key, loss_ratio_curve.to_json())

        return loss_ratio_curve
示例#17
0
    def test_can_build_an_aggregate_curve_from_kvs(self):
        curve_1 = shapes.Curve([(1.0, 0.0), (2.0, 0.0), 
                (3.0, 0.0), (4.0, 0.0)])

        curve_2 = shapes.Curve([(5.0, 0.0), (6.0, 0.0), 
                (7.0, 0.0), (8.0, 0.0)])

        curve_3 = shapes.Curve([(9.0, 0.0), (10.0, 0.0), 
                (11.0, 0.0), (12.0, 0.0)])
        
        job_id = 1234
        kvs.set(kvs.tokens.loss_curve_key(job_id, 1, 1, 5), curve_1.to_json())
        kvs.set(kvs.tokens.loss_curve_key(job_id, 1, 2, 5), curve_2.to_json())
        kvs.set(kvs.tokens.loss_curve_key(job_id, 1, 3, 5), curve_3.to_json())

        aggregate_curve = prob.AggregateLossCurve.from_kvs(job_id)

        expected_losses = numpy.array((15.0, 18.0, 21.0, 24.0))
        self.assertTrue(numpy.allclose(expected_losses, aggregate_curve.losses))