class SamplesFromConfigTestCase(unittest.TestCase):
    """Tests for the functionality that reads the number of samples
    for the probabilistic scenario from the configuration file."""
    def setUp(self):
        self.mixin = ProbabilisticEventMixin()

    def test_without_parameter_we_use_the_default_value(self):
        self.assertEqual(None, self.mixin._get_number_of_samples())

    def test_with_empty_parameter_we_use_the_default_value(self):
        self.mixin.__dict__["PROB_NUM_OF_SAMPLES"] = ""
        self.assertEqual(None, self.mixin._get_number_of_samples())

    def test_we_use_the_parameter_when_specified(self):
        self.mixin.__dict__["PROB_NUM_OF_SAMPLES"] = \
                NUMBER_OF_SAMPLES_FROM_CONFIG

        self.assertEqual(int(NUMBER_OF_SAMPLES_FROM_CONFIG),
                         self.mixin._get_number_of_samples())

    def test_default_value_with_wrong_parameter(self):
        self.mixin.__dict__["PROB_NUM_OF_SAMPLES"] = "this-is-wrong"
        self.assertEqual(None, self.mixin._get_number_of_samples())
Esempio n. 2
0
 def setUp(self):
     self.mixin = ProbabilisticEventMixin()
     self.mixin.params = {
         'NUMBER_OF_SEISMICITY_HISTORIES': 0,
         'NUMBER_OF_LOGIC_TREE_SAMPLES': 0,
         'INVESTIGATION_TIME': 0.0,
         'OUTPUT_DIR': 'foo',
     }
     self.mixin.serialize_results_to = ['db', 'xml']
     self.mixin.job_id = -1
     self.mixin.base_path = '/tmp'
     self.mixin.blocks_keys = []
     self.mixin.store_exposure_assets = lambda: None
     self.mixin.store_vulnerability_model = lambda: None
     self.mixin.partition = lambda: None
Esempio n. 3
0
class LossMapCurveSerialization(unittest.TestCase):
    def setUp(self):
        self.mixin = ProbabilisticEventMixin()
        self.mixin.params = {
            'NUMBER_OF_SEISMICITY_HISTORIES': 0,
            'NUMBER_OF_LOGIC_TREE_SAMPLES': 0,
            'INVESTIGATION_TIME': 0.0,
            'OUTPUT_DIR': 'foo',
        }
        self.mixin.serialize_results_to = ['db', 'xml']
        self.mixin.job_id = -1
        self.mixin.base_path = '/tmp'
        self.mixin.blocks_keys = []
        self.mixin.store_exposure_assets = lambda: None
        self.mixin.store_vulnerability_model = lambda: None
        self.mixin.partition = lambda: None

    def test_loss_map_serialized_if_conditional_loss_poes(self):
        self.mixin.params['CONDITIONAL_LOSS_POE'] = '0.01 0.02'

        with patch('openquake.risk.job.probabilistic'
                   '.aggregate_loss_curve.plot_aggregate_curve'):
            with patch('openquake.output.risk.create_loss_map_writer') as clw:
                clw.return_value = None

                self.mixin.execute()
                self.assertTrue(clw.called)

    def test_loss_map_not_serialized_unless_conditional_loss_poes(self):
        with patch('openquake.risk.job.probabilistic'
                   '.aggregate_loss_curve.plot_aggregate_curve'):
            with patch('openquake.output.risk.create_loss_map_writer') as clw:
                clw.return_value = None

                self.mixin.execute()
                self.assertFalse(clw.called)
Esempio n. 4
0
class SamplesFromConfigTestCase(unittest.TestCase):
    """Tests for the functionality that reads the number of samples
    for the probabilistic scenario from the configuration file."""

    def setUp(self):
        self.mixin = ProbabilisticEventMixin()

    def test_without_parameter_we_use_the_default_value(self):
        self.assertEqual(None, self.mixin._get_number_of_samples())

    def test_with_empty_parameter_we_use_the_default_value(self):
        self.mixin.__dict__["PROB_NUM_OF_SAMPLES"] = ""
        self.assertEqual(None, self.mixin._get_number_of_samples())

    def test_we_use_the_parameter_when_specified(self):
        self.mixin.__dict__["PROB_NUM_OF_SAMPLES"] = \
                NUMBER_OF_SAMPLES_FROM_CONFIG

        self.assertEqual(int(NUMBER_OF_SAMPLES_FROM_CONFIG),
                         self.mixin._get_number_of_samples())

    def test_default_value_with_wrong_parameter(self):
        self.mixin.__dict__["PROB_NUM_OF_SAMPLES"] = "this-is-wrong"
        self.assertEqual(None, self.mixin._get_number_of_samples())
Esempio n. 5
0
 def setUp(self):
     self.mixin = ProbabilisticEventMixin()
 def setUp(self):
     self.mixin = ProbabilisticEventMixin()
 def setUp(self):
     self.exposure_parser = exposure.ExposurePortfolioFile(
         os.path.join(test.SCHEMA_EXAMPLES_DIR, TEST_FILE))
     self.mixin = ProbabilisticEventMixin()
class EpsilonTestCase(unittest.TestCase):
    """Tests the `epsilon` method in class `ProbabilisticEventMixin`"""

    def setUp(self):
        self.exposure_parser = exposure.ExposurePortfolioFile(
            os.path.join(test.SCHEMA_EXAMPLES_DIR, TEST_FILE))
        self.mixin = ProbabilisticEventMixin()

    def test_uncorrelated(self):
        """For uncorrelated jobs we sample epsilon values per asset.

        A new sample should be drawn for each asset irrespective of any
        building typology similarities.
        """
        samples = []
        for _, asset in self.exposure_parser:
            sample = self.mixin.epsilon(asset)
            self.assertTrue(
                sample not in samples,
                "%s is already in %s" % (sample, samples))
            self.assertTrue(
                isinstance(sample, float), "Invalid sample (%s)" % sample)
            samples.append(sample)

    def test_correlated(self):
        """For correlated jobs we sample epsilon values per building typology.

        A sample should be drawn whenever an asset with a new building typology
        is encountered. Assets of the same typology should share sample values.
        Please not that building typologies and structure categories are
        roughly equivalent.
        """
        samples = dict()
        self.mixin.__dict__["ASSET_CORRELATION"] = "perfect"
        for _, asset in self.exposure_parser:
            sample = self.mixin.epsilon(asset)
            category = asset["structureCategory"]
            # This is either the first time we see this structure category or
            # the sample is identical to the one originally drawn for this
            # structure category.
            if category not in samples:
                samples[category] = sample
            else:
                self.assertTrue(sample == samples[category])
        # Make sure we used at least two structure categories in this test.
        self.assertTrue(len(samples.keys()) > 1)
        # Are all samples valid values?
        for category, sample in samples.iteritems():
            self.assertTrue(
                isinstance(sample, float),
                "Invalid sample (%s) for category %s" % (sample, category))

    def test_incorrect_configuration_setting(self):
        """The correctness of the asset correlation configuration is enforced.

        If the `ASSET_CORRELATION` parameter is set in the job configuration
        file it should have a correct value ("perfect").
        """
        self.mixin.__dict__["ASSET_CORRELATION"] = "this-is-wrong"
        for _, asset in self.exposure_parser:
            self.assertRaises(ValueError, self.mixin.epsilon, asset)
            break

    def test_correlated_with_no_structure_category(self):
        """For correlated jobs assets require a structure category property."""
        self.mixin.__dict__["ASSET_CORRELATION"] = "perfect"
        for _, asset in self.exposure_parser:
            del asset["structureCategory"]
            e = self.assertRaises(ValueError, self.mixin.epsilon, asset)
            break
 def setUp(self):
     self.exposure_parser = exposure.ExposurePortfolioFile(
         os.path.join(test.SCHEMA_EXAMPLES_DIR, TEST_FILE))
     self.mixin = ProbabilisticEventMixin()
Esempio n. 10
0
class EpsilonTestCase(unittest.TestCase):
    """Tests the `epsilon` method in class `ProbabilisticEventMixin`"""
    def setUp(self):
        self.exposure_parser = exposure.ExposurePortfolioFile(
            os.path.join(test.SCHEMA_EXAMPLES_DIR, TEST_FILE))
        self.mixin = ProbabilisticEventMixin()

    def test_uncorrelated(self):
        """For uncorrelated jobs we sample epsilon values per asset.

        A new sample should be drawn for each asset irrespective of any
        building typology similarities.
        """
        samples = []
        for _, asset in self.exposure_parser:
            sample = self.mixin.epsilon(asset)
            self.assertTrue(sample not in samples,
                            "%s is already in %s" % (sample, samples))
            self.assertTrue(isinstance(sample, float),
                            "Invalid sample (%s)" % sample)
            samples.append(sample)

    def test_correlated(self):
        """For correlated jobs we sample epsilon values per building typology.

        A sample should be drawn whenever an asset with a new building typology
        is encountered. Assets of the same typology should share sample values.
        Please not that building typologies and structure categories are
        roughly equivalent.
        """
        samples = dict()
        self.mixin.__dict__["ASSET_CORRELATION"] = "perfect"
        for _, asset in self.exposure_parser:
            sample = self.mixin.epsilon(asset)
            category = asset["structureCategory"]
            # This is either the first time we see this structure category or
            # the sample is identical to the one originally drawn for this
            # structure category.
            if category not in samples:
                samples[category] = sample
            else:
                self.assertTrue(sample == samples[category])
        # Make sure we used at least two structure categories in this test.
        self.assertTrue(len(samples.keys()) > 1)
        # Are all samples valid values?
        for category, sample in samples.iteritems():
            self.assertTrue(
                isinstance(sample, float),
                "Invalid sample (%s) for category %s" % (sample, category))

    def test_incorrect_configuration_setting(self):
        """The correctness of the asset correlation configuration is enforced.

        If the `ASSET_CORRELATION` parameter is set in the job configuration
        file it should have a correct value ("perfect").
        """
        self.mixin.__dict__["ASSET_CORRELATION"] = "this-is-wrong"
        for _, asset in self.exposure_parser:
            self.assertRaises(ValueError, self.mixin.epsilon, asset)
            break

    def test_correlated_with_no_structure_category(self):
        """For correlated jobs assets require a structure category property."""
        self.mixin.__dict__["ASSET_CORRELATION"] = "perfect"
        for _, asset in self.exposure_parser:
            del asset["structureCategory"]
            e = self.assertRaises(ValueError, self.mixin.epsilon, asset)
            break