<commit_before># Copyright (c) 2010-2013, 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 scenario risk calculator.
"""
import random
import itertools
import numpy
from django import db

from openquake.risklib import api, scientific

from openquake.engine import logs
from openquake.engine.calculators.base import signal_task_complete
from openquake.engine.calculators.risk import base, hazard_getters, writers
from openquake.engine.utils import tasks
from openquake.engine.db import models
from openquake.engine.performance import EnginePerformanceMonitor


@tasks.oqtask
@base.count_progress_risk('r')
def scenario(job_id, units, containers, params):
    """
    Celery task for the scenario risk calculator.

    :param int job_id:
      ID of the currently running job
    :param dict units:
      A dict of :class:`..base.CalculationUnit` instances keyed by
      loss type string
    :param containers:
      An instance of :class:`..writers.OutputDict` containing
      output container instances (in this case only `LossMap`)
    :param params:
      An instance of :class:`..base.CalcParams` used to compute
      derived outputs
    """
    def profile(name):
        return EnginePerformanceMonitor(name, job_id, scenario, tracing=True)

    agg = dict()
    insured = dict()
    with db.transaction.commit_on_success(using='reslt_writer'):
        for loss_type in units:
            # in scenario calculation we have only ONE calculation unit
            unit = units[loss_type][0]
            agg[loss_type], insured[loss_type] = do_scenario(
                loss_type, unit, containers, params, profile)
    num_items = base.get_num_items(units)
    signal_task_complete(
        job_id=job_id, num_items=num_items,
        aggregate_losses=agg, insured_losses=insured)
scenario.ignore_result = False


def do_scenario(loss_type, unit, containers, params, profile):
    """
    See `scenario` for a description of the input parameters
    """

    with profile('getting hazard'):
        assets, ground_motion_values = unit.getter()

    if not len(assets):
        logs.LOG.info("Exit from task as no asset could be processed")
        return

    with profile('computing risk'):
        loss_ratio_matrix = unit.calc(ground_motion_values)

        if params.insured_losses:
            insured_loss_matrix = [
                scientific.insured_losses(
                    loss_ratio_matrix[i], asset.value(loss_type),
                    asset.deductible(loss_type),
                    asset.insurance_limit(deductible))
                for i, asset in enumerate(assets)]

    with profile('saving risk outputs'):
        containers.write(
            assets,
            [losses.mean() for losses in loss_ratio_matrix],
            [losses.std(ddof=1) for losses in loss_ratio_matrix],
            output_type="loss_map",
            loss_type=loss_type,
            hazard_output_id=unit.getter.hazard_output.id,
            insured=False)

        if params.insured_losses:
            containers.write(
                assets,
                [losses.mean() for losses in insured_loss_matrix],
                [losses.std(ddof=1) for losses in insured_loss_matrix],
                itertools.cycle([True]),
                output_type="loss_map",
                loss_type=loss_type,
                hazard_output_id=unit.getter.hazard_output.id,
                insured=True)

    aggregate_losses = sum(loss_ratio_matrix[i] * asset.value(loss_type)
                           for i, asset in enumerate(assets))

    if params.insured_losses:
        insured_losses = (<commit_msg>fix scenario: insured losses<commit_after># Copyright (c) 2010-2013, 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 scenario risk calculator.
"""
import random
import itertools
import numpy
from django import db

from openquake.risklib import api, scientific

from openquake.engine import logs
from openquake.engine.calculators.base import signal_task_complete
from openquake.engine.calculators.risk import base, hazard_getters, writers
from openquake.engine.utils import tasks
from openquake.engine.db import models
from openquake.engine.performance import EnginePerformanceMonitor


@tasks.oqtask
@base.count_progress_risk('r')
def scenario(job_id, units, containers, params):
    """
    Celery task for the scenario risk calculator.

    :param int job_id:
      ID of the currently running job
    :param dict units:
      A dict of :class:`..base.CalculationUnit` instances keyed by
      loss type string
    :param containers:
      An instance of :class:`..writers.OutputDict` containing
      output container instances (in this case only `LossMap`)
    :param params:
      An instance of :class:`..base.CalcParams` used to compute
      derived outputs
    """
    def profile(name):
        return EnginePerformanceMonitor(name, job_id, scenario, tracing=True)

    agg = dict()
    insured = dict()
    with db.transaction.commit_on_success(using='reslt_writer'):
        for loss_type in units:
            # in scenario calculation we have only ONE calculation unit
            unit = units[loss_type][0]
            agg[loss_type], insured[loss_type] = do_scenario(
                loss_type, unit, containers, params, profile)
    num_items = base.get_num_items(units)
    signal_task_complete(
        job_id=job_id, num_items=num_items,
        aggregate_losses=agg, insured_losses=insured)
scenario.ignore_result = False


def do_scenario(loss_type, unit, containers, params, profile):
    """
    See `scenario` for a description of the input parameters
    """

    with profile('getting hazard'):
        assets, ground_motion_values = unit.getter()

    if not len(assets):
        logs.LOG.info("Exit from task as no asset could be processed")
        return

    with profile('computing risk'):
        loss_ratio_matrix = unit.calc(ground_motion_values)

        if params.insured_losses:
            insured_loss_matrix = [
                scientific.insured_losses(
                    loss_ratio_matrix[i], asset.value(loss_type),
                    asset.deductible(loss_type),
                    asset.insurance_limit(loss_type))
                for i, asset in enumerate(assets)]

    with profile('saving risk outputs'):
        containers.write(
            assets,
            [losses.mean() for losses in loss_ratio_matrix],
            [losses.std(ddof=1) for losses in loss_ratio_matrix],
            output_type="loss_map",
            loss_type=loss_type,
            hazard_output_id=unit.getter.hazard_output.id,
            insured=False)

        if params.insured_losses:
            containers.write(
                assets,
                [losses.mean() for losses in insured_loss_matrix],
                [losses.std(ddof=1) for losses in insured_loss_matrix],
                itertools.cycle([True]),
                output_type="loss_map",
                loss_type=loss_type,
                hazard_output_id=unit.getter.hazard_output.id,
                insured=True)

    aggregate_losses = sum(loss_ratio_matrix[i] * asset.value(loss_type)
                           for i, asset in enumerate(assets))

    if params.insured_losses:
        insured_losses = (
{'fraction_non_alphanumeric': 0.07886694875411378, 'fraction_numerical': 0.0028208744710860366, 'mean_word_length': 2.8020554066130474, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, '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': '1210414', 'n_tokens_mistral': 2428, 'n_tokens_neox': 2293, 'n_words': 725}