def setUp(self):
        # get random ID as job_id
        self.job_id = str(uuid.uuid4())

        # load vulnerability and write to memcache
        vulnerability.load_vulnerability_model(self.job_id,
            os.path.join(test.DATA_DIR, TEST_FILE))

        self.vulnerability_curves = \
            vulnerability.load_vuln_curves_from_kvs(self.job_id)
Exemple #2
0
    def __init__(self, job_id, block_id):
        """ Prepare the calculator for computations"""

        self.job_id = job_id
        self.block_id = block_id

        self.vuln_curves = \
                vulnerability.load_vuln_curves_from_kvs(self.job_id)

        # self.vuln_curves is a dict of {string: Curve}
        LOGGER.debug("ProbabilisticLossRatioCalculator init: vuln curves are")

        for k, v in self.vuln_curves.items():
            LOGGER.debug("%s: %s" % (k, v))
Exemple #3
0
    def compute_risk(self, block_id, **kwargs): #pylint: disable=W0613
        """This task computes risk for a block of sites. It requires to have
        pre-initialized in memcache:
         1) list of sites
         2) gmfs
         3) exposure portfolio (=assets)
         4) vulnerability

        TODO(fab): make conditional_loss_poe (set of probabilities of exceedance
        for which the loss computation is done) a list of floats, and read it from
        the job configuration.
        """

        conditional_loss_poes = [float(x) for x in self.params.get(
                    'CONDITIONAL_LOSS_POE', "0.01").split()]
        self.slice_gmfs(block_id)

        #pylint: disable=W0201 
        self.vuln_curves = \
                vulnerability.load_vuln_curves_from_kvs(self.job_id)

        # TODO(jmc): DONT assumes that hazard and risk grid are the same
        block = job.Block.from_kvs(block_id)
        
        for point in block.grid(self.region):
            key = kvs.generate_product_key(self.job_id, 
                kvs.tokens.GMF_KEY_TOKEN, point.column, point.row)
            gmf_slice = kvs.get_value_json_decoded(key)
            
            asset_key = kvs.tokens.asset_key(self.id, point.row, point.column)
            asset_list = kvs.get_client().lrange(asset_key, 0, -1)
            for asset in [json.JSONDecoder().decode(x) for x in asset_list]:
                LOGGER.debug("processing asset %s" % (asset))
                loss_ratio_curve = self.compute_loss_ratio_curve(
                        point.column, point.row, asset, gmf_slice)
                if loss_ratio_curve is not None:

                    # compute loss curve
                    loss_curve = self.compute_loss_curve(
                            point.column, point.row, 
                            loss_ratio_curve, asset)
                    
                    for loss_poe in conditional_loss_poes:
                        self.compute_conditional_loss(point.column, point.row,
                                loss_curve, asset, loss_poe)
        return True