<commit_before># Copyright (c) 2010-2012, GEM Foundation.
#
# OpenQuake is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# OpenQuake is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with OpenQuake.  If not, see <http://www.gnu.org/licenses/>.


"""
Core functionality for the classical PSHA risk calculator.
"""

from django import db

from openquake.calculators.risk import general
from openquake.db import models
from openquake.utils import tasks, stats
from openquake import logs
from openquake.calculators import base

from risklib import api, scientific


@tasks.oqtask
@stats.count_progress('r')
def event_based(job_id, assets, hazard_getter, hazard_id, seed,
                loss_curve_id, loss_map_ids,
                insured_curve_id, aggregate_loss_curve_id,
                conditional_loss_poes, insured_losses,
                imt, time_span, tses,
                loss_curve_resolution, asset_correlation):
    """
    Celery task for the event based risk calculator.
    """
    vulnerability_model = general.fetch_vulnerability_model(job_id)

    # FIXME(lp): refactor risklib. there is no reason to propagate
    # time_span and tses in an hazard getter
    hazard_getter = general.hazard_getter(
        hazard_getter, hazard_id, imt)

    calculator = api.ProbabilisticEventBased(
        vulnerability_model,
        curve_resolution=loss_curve_resolution,
        time_span=time_span,
        tses=tses,
        seed=seed,
        correlation_type=asset_correlation)

    # FIXME. Save unmodified calculator, as the decorated one does not
    # support aggregate losses at the moment.
    eb_calculator = calculator

    # if we need to compute the insured losses, we add the proper
    # risklib aggregator
    if insured_losses:
        calculator = api.InsuredLosses(calculator)

    # if we need to compute the loss maps, we add the proper risk
    # aggregator
    if conditional_loss_poes:
        calculator = api.ConditionalLosses(conditional_loss_poes, calculator)

    with db.transaction.commit_on_success(using='reslt_writer'):
        logs.LOG.debug(
            'launching compute_on_assets over %d assets' % len(assets))
        for asset_output in api.compute_on_assets(
                assets, hazard_getter, calculator):

            general.write_loss_curve(loss_curve_id, asset_output)

            if asset_output.conditional_losses:
                general.write_loss_map(loss_map_ids, asset_output)

            if asset_output.insured_losses:
                general.write_loss_curve(insured_curve_id, asset_output)

    general.update_aggregate_losses(
        aggregate_loss_curve_id, eb_calculator.aggregate_losses)
    base.signal_task_complete(job_id=job_id, num_items=len(assets))
event_based.ignore_result = False


class EventBasedRiskCalculator(general.BaseRiskCalculator):
    """
    Probabilistic Event Based PSHA risk calculator. Computes loss
    curves, loss maps, aggregate losses and insured losses for a given
    set of assets.
    """

    #: The core calculation celery task function
    core_calc_task = event_based

    def pre_execute(self):
        """
        Override the default pre_execute to provide more detailed
        validation.

        1) In Event Based we get the intensity measure type considered
        from the vulnerability model, then we check that the hazard
        calculation includes outputs with that intensity measure type

        2) If insured losses are required we check for the presence of
        the deductible and insurance limit
        """
        super(EventBasedRiskCalculator, self).pre_execute()<commit_msg>Reflected the changes in the API of aggregate losses in risklib (https://bugs.launchpad.net/openquake/+bug/1096880)<commit_after># Copyright (c) 2010-2012, GEM Foundation.
#
# OpenQuake is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# OpenQuake is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with OpenQuake.  If not, see <http://www.gnu.org/licenses/>.


"""
Core functionality for the classical PSHA risk calculator.
"""

from django import db

from openquake.calculators.risk import general
from openquake.db import models
from openquake.utils import tasks, stats
from openquake import logs
from openquake.calculators import base

from risklib import api, scientific


@tasks.oqtask
@stats.count_progress('r')
def event_based(job_id, assets, hazard_getter, hazard_id, seed,
                loss_curve_id, loss_map_ids,
                insured_curve_id, aggregate_loss_curve_id,
                conditional_loss_poes, insured_losses,
                imt, time_span, tses,
                loss_curve_resolution, asset_correlation):
    """
    Celery task for the event based risk calculator.
    """
    vulnerability_model = general.fetch_vulnerability_model(job_id)

    # FIXME(lp): refactor risklib. there is no reason to propagate
    # time_span and tses in an hazard getter
    hazard_getter = general.hazard_getter(
        hazard_getter, hazard_id, imt)

    calculator = api.ProbabilisticEventBased(
        vulnerability_model,
        curve_resolution=loss_curve_resolution,
        time_span=time_span,
        tses=tses,
        seed=seed,
        correlation_type=asset_correlation)

    # if we need to compute the insured losses, we can aggregate on them, too
    if insured_losses:
        calculator = api.InsuredLosses(calculator)

    # if we need to compute the loss maps, we add the proper risk
    # aggregator
    if conditional_loss_poes:
        calculator = api.ConditionalLosses(conditional_loss_poes, calculator)

    losses = None
    with db.transaction.commit_on_success(using='reslt_writer'):
        logs.LOG.debug(
            'launching compute_on_assets over %d assets' % len(assets))
        for asset_output in api.compute_on_assets(
                assets, hazard_getter, calculator):

            general.write_loss_curve(loss_curve_id, asset_output)

            if asset_output.conditional_losses:
                general.write_loss_map(loss_map_ids, asset_output)

            if asset_output.insured_losses:
                general.write_loss_curve(insured_curve_id, asset_output)

            losses = api.aggregate_losses([asset_output], losses)

    general.update_aggregate_losses(aggregate_loss_curve_id, losses)
    base.signal_task_complete(job_id=job_id, num_items=len(assets))
event_based.ignore_result = False


class EventBasedRiskCalculator(general.BaseRiskCalculator):
    """
    Probabilistic Event Based PSHA risk calculator. Computes loss
    curves, loss maps, aggregate losses and insured losses for a given
    set of assets.
    """

    #: The core calculation celery task function
    core_calc_task = event_based

    def pre_execute(self):
        """
        Override the default pre_execute to provide more detailed
        validation.

        1) In Event Based we get the intensity measure type considered
        from the vulnerability model, then we check that the hazard
        calculation includes outputs with that intensity measure type

        2) If insured losses are required we check for the presence of
        the deductible and insurance limit
        """
        super(EventBasedRiskCalculator, self).pre_execute()
{'fraction_non_alphanumeric': 0.05945945945945946, 'fraction_numerical': 0.0035626535626535625, 'mean_word_length': 3.273490813648294, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 1, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '969758', 'n_tokens_mistral': 2378, 'n_tokens_neox': 2195, 'n_words': 754}