Ejemplo n.º 1
0
    def test_end_to_end(self):
        # manually computed values by Vitor Silva
        psha.STEPS_PER_INTERVAL = 2
        hazard_curve = shapes.Curve([
              (0.01, 0.99), (0.08, 0.96),
              (0.17, 0.89), (0.26, 0.82),
              (0.36, 0.70), (0.55, 0.40),
              (0.70, 0.01)])

        imls = [0.1, 0.2, 0.4, 0.6]
        loss_ratios = [0.05, 0.08, 0.2, 0.4]
        covs = [0.5, 0.3, 0.2, 0.1]
        vuln_function = shapes.VulnerabilityFunction(imls, loss_ratios, covs)

        loss_ratio_curve = psha.compute_loss_ratio_curve(
                vuln_function, hazard_curve)

        lr_curve_expected = shapes.Curve([(0.0, 0.96),
                (0.025, 0.96), (0.05, 0.91), (0.065, 0.87),
                (0.08, 0.83), (0.14, 0.75), (0.2, 0.60),
                (0.3, 0.47), (0.4, 0.23), (0.7, 0.00),
                (1.0, 0.00)])

        for x_value in lr_curve_expected.abscissae:
            self.assertTrue(numpy.allclose(
                    lr_curve_expected.ordinate_for(x_value),
                    loss_ratio_curve.ordinate_for(x_value), atol=0.005))
Ejemplo n.º 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
Ejemplo n.º 3
0
    def test_end_to_end(self):
        # manually computed values by Vitor Silva
        psha.STEPS_PER_INTERVAL = 2
        hazard_curve = shapes.Curve([
              (0.01, 0.99), (0.08, 0.96),
              (0.17, 0.89), (0.26, 0.82),
              (0.36, 0.70), (0.55, 0.40),
              (0.70, 0.01)])

        vuln_function = shapes.VulnerabilityFunction([(0.1, (0.05, 0.5)),
              (0.2, (0.08, 0.3)), (0.4, (0.2, 0.2)), (0.6, (0.4, 0.1))])

        loss_ratio_curve = psha.compute_loss_ratio_curve(
                vuln_function, hazard_curve)

        lr_curve_expected = shapes.Curve([(0.0, 0.96),
                (0.025, 0.96), (0.05, 0.91), (0.065, 0.87),
                (0.08, 0.83), (0.14, 0.75), (0.2, 0.60),
                (0.3, 0.47), (0.4, 0.23), (0.7, 0.00),
                (1.0, 0.00)])

        for x_value in lr_curve_expected.abscissae:
            self.assertTrue(numpy.allclose(
                    lr_curve_expected.ordinate_for(x_value),
                    loss_ratio_curve.ordinate_for(x_value), atol=0.005))
Ejemplo n.º 4
0
    def compute_loss_ratio_curve(self, gridpoint):
        """ Returns the loss ratio curve for a single gridpoint"""

        # check in kvs if hazard and exposure for gridpoint are there
        kvs_key_hazard = kvs.generate_product_key(self.job_id, 
            kvs.tokens.HAZARD_CURVE_KEY_TOKEN, self.block_id, gridpoint)
       
        hazard_curve_json = kvs.get_client(binary=False).get(kvs_key_hazard)
        LOGGER.debug("hazard curve as JSON: %s" % hazard_curve_json)
 
        hazard_curve = shapes.EMPTY_CURVE
        hazard_curve.from_json(hazard_curve_json)

        LOGGER.debug("hazard curve at key %s is %s" % (kvs_key_hazard, 
            hazard_curve))

        if hazard_curve is None:
            LOGGER.debug("no hazard curve found")
            return None

        kvs_key_exposure = kvs.generate_product_key(self.job_id, 
            kvs.tokens.EXPOSURE_KEY_TOKEN, self.block_id, gridpoint)
        
        asset = kvs.get_value_json_decoded(kvs_key_exposure)

        LOGGER.debug("asset at key %s is %s" % (kvs_key_exposure, asset))

        if asset is None:
            LOGGER.debug("no asset found")
            return None

        LOGGER.debug("compute method: vuln curves are")
        for k, v in self.vulnerability_curves.items(): #pylint: disable=E1101
            LOGGER.debug("%s: %s" % (k, v.values))

        #pylint: disable=E1101
        vulnerability_curve = \
            self.vulnerability_curves[asset['VulnerabilityFunction']]

        # selected vuln function is Curve
        return classical_psha_based.compute_loss_ratio_curve(
            vulnerability_curve, hazard_curve)
Ejemplo n.º 5
0
    def test_end_to_end(self):
        # manually computed values by Vitor Silva
        hazard_curve = shapes.Curve(
                [(5.0, 0.4), (6.0, 0.2), (7.0, 0.05)])

        lrem = psha._compute_lrem(shapes.Curve.from_json(
                self.vulnerability_curves[self.vuln_curve_code_test]))

        loss_ratio_curve = psha.compute_loss_ratio_curve(
                shapes.Curve.from_json(
                self.vulnerability_curves[self.vuln_curve_code_test]),
                hazard_curve)

        lr_curve_expected = shapes.Curve([(0.0, 0.650), 
                (0.05, 0.650), (0.10, 0.632), (0.15, 0.569),
                (0.20, 0.477), (0.25, 0.382), (0.28, 0.330),
                (0.31, 0.283), (0.34, 0.241), (0.37, 0.205),
                (0.40, 0.173), (0.44, 0.137), (0.48, 0.108),
                (0.52, 0.085), (0.56, 0.066), (0.60, 0.051)])

        for x_value in lr_curve_expected.abscissae:
            self.assertAlmostEqual(lr_curve_expected.ordinate_for(x_value),
                    loss_ratio_curve.ordinate_for(x_value), 3)
Ejemplo n.º 6
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
Ejemplo n.º 7
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
Ejemplo n.º 8
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
Ejemplo n.º 9
0
 def test_empty_loss_ratio_curve(self):
     self.assertEqual(shapes.EMPTY_CURVE,
             psha.compute_loss_ratio_curve(None, []))