<commit_before># Copyright (c) 2010-2011, GEM Foundation.
#
# OpenQuake is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License version 3
# only, as published by the Free Software Foundation.
#
# 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 Lesser General Public License version 3 for more details
# (a copy is included in the LICENSE file that accompanied this code).
#
# You should have received a copy of the GNU Lesser General Public License
# version 3 along with OpenQuake.  If not, see
# <http://www.gnu.org/licenses/lgpl-3.0.txt> for a copy of the LGPLv3 License.

import os
import shutil
import unittest

from lxml import etree
from nose.plugins.attrib import attr

from openquake.db.models import OqCalculation
from openquake.nrml import nrml_schema_file

from tests.utils import helpers


BCR_DEMO_BASE = 'demos/benefit_cost_ratio'


CONFIG = '%s/config.gem' % BCR_DEMO_BASE
COMPUTED_OUTPUT = '%s/computed_output' % BCR_DEMO_BASE
RESULT = '%s/bcr-map.xml' % COMPUTED_OUTPUT

NRML = 'http://openquake.org/xmlns/nrml/0.3'
GML = 'http://www.opengis.net/gml'


class BCRQATestCase(unittest.TestCase):

    @attr('qa')
    def test_bcr(self):
        expected_result = {
        #    site location
            (-122.0, 38.225): {
                # assetRef  eal_orig  eal_retrof  bcr
                    'a1':   (0.009379,  0.006586,  0.483091)
            }
        }
        delta = 1e-5

        helpers.run_job(CONFIG, debug='debug')
        calc_record = OqCalculation.objects.latest("id")
        self.assertEqual('succeeded', calc_record.status)

        result = self._parse_bcr_map(RESULT)
        try:
            helpers.assertDeepAlmostEqual(self, expected_result, result,
                                          delta=delta)
        finally:
            shutil.rmtree(COMPUTED_OUTPUT)

    def _parse_bcr_map(self, filename):
        self.assertTrue(os.path.exists(filename))
        schema = etree.XMLSchema(file=nrml_schema_file())
        parser = etree.XMLParser(schema=schema)<commit_msg>refactored test to support different deltas for the various result values (EAL and BCR)<commit_after># Copyright (c) 2010-2011, GEM Foundation.
#
# OpenQuake is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License version 3
# only, as published by the Free Software Foundation.
#
# 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 Lesser General Public License version 3 for more details
# (a copy is included in the LICENSE file that accompanied this code).
#
# You should have received a copy of the GNU Lesser General Public License
# version 3 along with OpenQuake.  If not, see
# <http://www.gnu.org/licenses/lgpl-3.0.txt> for a copy of the LGPLv3 License.

import os
import shutil
import unittest

from lxml import etree
from nose.plugins.attrib import attr

from openquake.db.models import OqCalculation
from openquake.nrml import nrml_schema_file

from tests.utils import helpers


BCR_DEMO_BASE = 'demos/benefit_cost_ratio'


CONFIG = '%s/config.gem' % BCR_DEMO_BASE
COMPUTED_OUTPUT = '%s/computed_output' % BCR_DEMO_BASE
RESULT = '%s/bcr-map.xml' % COMPUTED_OUTPUT

NRML = 'http://openquake.org/xmlns/nrml/0.3'
GML = 'http://www.opengis.net/gml'


class BCRQATestCase(unittest.TestCase):

    @attr('qa')
    def test_bcr(self):
        # Verify the EAL (Original and Retrofitted) and BCR values to
        # hand-computed results.

        # For the EAL values, a delta of 0.0009 (3 decimal places of precision)
        # is considered reasonable.

        # For the BCR, a delta of 0.009 (2 decimal places of precision) is
        # considered reasonable.

        expected_result = {
        #    site location
            (-122.0, 38.225): {
                # assetRef  eal_orig  eal_retrof  bcr
                    'a1':   (0.009379,  0.006586,  0.483091)
            }
        }
        delta = 1e-5

        helpers.run_job(CONFIG)
        calc_record = OqCalculation.objects.latest("id")
        self.assertEqual('succeeded', calc_record.status)

        result = self._parse_bcr_map(RESULT)

        try:
            self._assert_bcr_results_equal(expected_result, result)
        finally:
            shutil.rmtree(COMPUTED_OUTPUT)

    def _assert_bcr_results_equal(self, expected, actual, eal_delta=0.0009,
                                  bcr_delta=0.009):
        """Given a pair of dicts assert that they are equal.

        Result values do not have to be exact and the following default deltas
        are used:

        For EAL values, a delta of
        0.0009 (3 decimal places of precision) is allowed. For BCR values, a
        delta of 0.009 (2 decimal places of precision) is allowed."""
        self.assertEqual(len(expected), len(actual))

        for site, exp_value in expected.items():
            for asset_ref, (eal_orig, eal_retrof, bcr) in exp_value.items():

                act_eal_orig, act_eal_retrof, act_bcr = (
                    actual[site][asset_ref])

                # Verify 'EAL, Original'
                self.assertAlmostEqual(eal_orig, act_eal_orig,
                                       delta=eal_delta)
                # Verify 'EAL, Retrofitted'
                self.assertAlmostEqual(eal_retrof, act_eal_retrof,
                                       delta=eal_delta)
                # Verify BCR
                self.assertAlmostEqual(bcr, act_bcr, delta=bcr_delta)

    def _parse_bcr_map(self, filename):
        self.assertTrue(os.path.exists(filename))
        schema = etree.XMLSchema(file=nrml_schema_file())
        parser = etree.XMLParser(schema=schema)
{'fraction_non_alphanumeric': 0.08313778075762655, 'fraction_numerical': 0.021622527656721423, 'mean_word_length': 2.6076178960096734, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 6}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 5, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '482045', 'n_tokens_mistral': 1926, 'n_tokens_neox': 1811, 'n_words': 550}