Esempio n. 1
0
    def test_computes_the_iml(self):
        self.params[self.poes_levels] = "0.10"

        mean_curve = {"site_lon": 3.0, "site_lat": 3.0,
                "curve": classical_psha._reconstruct_curve_list_from(
                [9.8784e-01, 9.8405e-01, 9.5719e-01, 9.1955e-01,
                8.5019e-01, 7.4038e-01, 5.9153e-01, 4.2626e-01, 2.9755e-01,
                2.7731e-01, 1.6218e-01, 8.8035e-02, 4.3499e-02, 1.9065e-02,
                7.0442e-03, 2.1300e-03, 4.9498e-04, 8.1768e-05, 7.3425e-06])}

        self._store_curve_at(shapes.Site(3.0, 3.0), mean_curve)

        self._run()

        im_level = self._get_iml_at(shapes.Site(2.0, 5.0), 0.10)
        self.assertEqual(2.0, im_level["site_lon"])
        self.assertEqual(5.0, im_level["site_lat"])

        self.assertTrue(numpy.allclose([1.6789e-01],
                numpy.array(im_level["IML"]), atol=0.005))

        im_level = self._get_iml_at(shapes.Site(3.0, 3.0), 0.10)
        self.assertEqual(3.0, im_level["site_lon"])
        self.assertEqual(3.0, im_level["site_lat"])

        self.assertTrue(numpy.allclose([1.9078e-01],
                numpy.array(im_level["IML"]), atol=0.005))
Esempio n. 2
0
    def test_accepts_also_signs(self):
        self.params[self.quantiles_levels] = "-0.33 +0.0 XYZ +0.5 +1.00"
        self._run([shapes.Site(2.0, 5.0)])

        self._has_computed_quantile_for_site(shapes.Site(2.0, 5.0), 0.00)
        self._has_computed_quantile_for_site(shapes.Site(2.0, 5.0), 0.50)
        self._has_computed_quantile_for_site(shapes.Site(2.0, 5.0), 1.00)
Esempio n. 3
0
    def test_write_multiple_result_nodes(self):
        result_data = [
            dict(groundMotionValue=0.25, path="filea"),
            dict(groundMotionValue=0.76, path="fileb"),
            dict(groundMotionValue=0.11, path="filec"),
        ]

        expected_result_attrib = [
            dict(groundMotionValue="0.25", path="filea"),
            dict(groundMotionValue="0.76", path="fileb"),
            dict(groundMotionValue="0.11", path="filec"),
        ]

        self.writer.open()
        self.writer.write(shapes.Site(1.0, 2.0), result_data[0])
        self.writer.write(shapes.Site(2.0, 3.0), result_data[1])
        self.writer.write(shapes.Site(3.0, 4.0), result_data[2])
        self.writer.close()

        site_nodes = self._xpath("//gml:pos")
        disagg_nodes = self._xpath("//nrml:disaggregationResultNode")
        results = self._xpath("//nrml:disaggregationResult")

        self.assertEquals(3, len(site_nodes))
        self.assertEquals(3, len(disagg_nodes))
        self.assertEquals(3, len(results))

        self.assertEquals("1.0 2.0", site_nodes[0].text)
        self.assertEquals("2.0 3.0", site_nodes[1].text)
        self.assertEquals("3.0 4.0", site_nodes[2].text)

        for i, res in enumerate(results):
            self.assertEquals(expected_result_attrib[i], res.attrib)

        self.assertTrue(xml.validates_against_xml_schema(self.FILENAME))
Esempio n. 4
0
    def make_assets(self):
        [ism] = models.inputs4job(self.job.id, input_type="exposure")

        em = models.ExposureModel(
            owner=ism.owner, input=ism,
            name="AAA", category="single_asset",
            reco_type="aggregated", reco_unit="USD",
            stco_type="aggregated", stco_unit="USD")

        em.save()

        site_1 = shapes.Site(-116.0, 41.0)
        site_2 = shapes.Site(-117.0, 42.0)

        asset_1 = models.ExposureData(
            exposure_model=em, taxonomy="RC",
            asset_ref="asset_1", number_of_units=100, stco=1,
            site=geos.GEOSGeometry(site_1.point.to_wkt()), reco=1)

        asset_2 = models.ExposureData(
            exposure_model=em, taxonomy="RM",
            asset_ref="asset_2", number_of_units=40, stco=1,
            site=geos.GEOSGeometry(site_2.point.to_wkt()), reco=1)

        asset_3 = models.ExposureData(
            exposure_model=em, taxonomy="RM",
            asset_ref="asset_3", number_of_units=40, stco=1,
            site=geos.GEOSGeometry(site_2.point.to_wkt()), reco=1)

        asset_1.save()
        asset_2.save()
        asset_3.save()

        return asset_1, asset_2, asset_3
Esempio n. 5
0
    def test_computes_all_the_levels_specified(self):
        self.params[self.quantiles_levels] = "0.25 0.50 0.75"
        self._run([shapes.Site(2.0, 5.0)])

        self._has_computed_quantile_for_site(shapes.Site(2.0, 5.0), 0.25)
        self._has_computed_quantile_for_site(shapes.Site(2.0, 5.0), 0.50)
        self._has_computed_quantile_for_site(shapes.Site(2.0, 5.0), 0.75)
Esempio n. 6
0
    def test_eq(self):
        # Test the __eq__ method.
        # __eq__ is a shallow test and only compares ids.
        block1 = Block(7, 0, [shapes.Site(1.0, 1.0)])
        block2 = Block(7, 0, [shapes.Site(1.0, 0.0)])

        self.assertTrue(block1 == block2)
Esempio n. 7
0
    def test_reads_multiple_curves_in_multiple_branches_nrml(self):
        self.python_client.set("KEY", MULTIPLE_CURVES_MULTIPLE_BRANCHES)
        nrmls = self.reader.for_nrml("KEY")

        data = {shapes.Site(4.0, 4.0): {
                    "IMT": "PGA",
                    "IDmodel": "FIXED",
                    "timeSpanDuration": 50.0,
                    "endBranchLabel": "label1",
                    "IMLValues": [1.0, 2.0, 3.0],
                    "Values": [1.8,2.8,3.8]},
                shapes.Site(4.0, 1.0): {
                    "IMT": "PGA",
                    "IDmodel": "FIXED",
                    "timeSpanDuration": 50.0,
                    "endBranchLabel": "label2",
                    "IMLValues": [1.0, 2.0, 3.0],
                    "Values": [1.5,2.5,3.5]}
                }

        print "data: %s" % data
        print nrmls

        self.assertEqual(2, len(nrmls.items()))
        self.assertEquals(data, nrmls)
Esempio n. 8
0
    def test_the_same_calculator_is_used_between_multiple_invocations(self):
        calculator = scenario.ScenarioHazardCalculator(self.job_ctxt)

        gmf_calculator1 = calculator.gmf_calculator([shapes.Site(1.0, 1.0)])
        gmf_calculator2 = calculator.gmf_calculator([shapes.Site(1.0, 1.0)])

        self.assertTrue(gmf_calculator1 == gmf_calculator2)
Esempio n. 9
0
    def setUp(self):
        self.loss_curve_path = helpers.get_output_path(LOSS_XML_OUTPUT_FILE)
        self.loss_ratio_curve_path = helpers.get_output_path(
            LOSS_RATIO_XML_OUTPUT_FILE)

        self.single_loss_curve_path = helpers.get_output_path(
            SINGLE_LOSS_XML_OUTPUT_FILE)
        self.single_loss_ratio_curve_path = helpers.get_output_path(
            SINGLE_LOSS_RATIO_XML_OUTPUT_FILE)

        self.schema_path = NRML_SCHEMA_PATH

        # Build up some sample loss/loss ratio curves here
        first_site = shapes.Site(-117.0, 38.0)
        second_site = shapes.Site(-118.0, 39.0)

        first_asset = models.ExposureData(asset_ref='a1711')
        second_asset = models.ExposureData(asset_ref='a1712')

        self.loss_curves = [(first_site, (TEST_LOSS_CURVE, first_asset)),
                            (second_site, (TEST_LOSS_CURVE, second_asset))]

        self.loss_ratio_curves = [
            (first_site, (TEST_LOSS_RATIO_CURVE, first_asset)),
            (second_site, (TEST_LOSS_RATIO_CURVE, second_asset))
        ]

        self.single_loss_curve = [(first_site, (TEST_LOSS_CURVE, first_asset))]

        self.single_loss_ratio_curve = [(first_site, (TEST_LOSS_RATIO_CURVE,
                                                      first_asset))]

        # loss curve that fails with inconsistent sites for an asset
        self.loss_curves_fail = [(first_site, (TEST_LOSS_CURVE, first_asset)),
                                 (second_site, (TEST_LOSS_CURVE, first_asset))]
Esempio n. 10
0
    def test_asset_losses_per_site(self):
        mm = mock.MagicMock(spec=redis.Redis)
        mm.get.return_value = 0.123
        with helpers.patch('openquake.kvs.get_client') as mgc:
            mgc.return_value = mm

            def coords(item):
                return item[0].coords

            expected = [(shapes.Site(10.0, 10.0), [({
                'value': 0.123
            }, GRID_ASSETS[(0, 0)])]),
                        (shapes.Site(10.1, 10.0), [({
                            'value': 0.123
                        }, GRID_ASSETS[(0, 1)])]),
                        (shapes.Site(10.0, 10.1), [({
                            'value': 0.123
                        }, GRID_ASSETS[(1, 0)])]),
                        (shapes.Site(10.1, 10.1), [({
                            'value': 0.123
                        }, GRID_ASSETS[(1, 1)])])]

            calculator = general.BaseRiskCalculator(self.job_ctxt)
            actual = calculator.asset_losses_per_site(0.5, self.grid_assets)
            expected = sorted(expected, key=coords)
            actual = sorted(actual, key=coords)

            self.assertEqual(expected, actual)
Esempio n. 11
0
    def test_region_sites_boundary_2(self):
        # same as above, but for latitude
        region = shapes.Region.from_coordinates([(1.0, 2.0), (2.0, 2.0),
                                                 (2.0, 0.6), (1.0, 0.6)])

        region.cell_size = 0.5

        self.assertTrue(
            region.grid.site_at(region.grid.point_at(shapes.Site(1.5, 0.61)))
            in region.grid.centers())

        self.assertTrue(
            region.grid.site_at(region.grid.point_at(shapes.Site(1.5, 0.60)))
            in region.grid.centers())

        self.assertTrue(
            region.grid.site_at(region.grid.point_at(shapes.Site(1.5, 2.0))) in
            region.grid.centers())

        self.assertTrue(
            region.grid.site_at(region.grid.point_at(shapes.Site(1.5, 1.9))) in
            region.grid.centers())

        self.assertTrue(
            region.grid.site_at(region.grid.point_at(shapes.Site(1.5, 1.8))) in
            region.grid.centers())

        # check we can ask for valid grid points from
        # the sites that represent the center of the cells
        for cell_center in region.grid.centers():
            self.assertTrue(region.grid.site_inside(cell_center))
Esempio n. 12
0
    def test_writes_the_config_only_once(self):
        data = [(shapes.Site(-122.5000, 37.5000), {
            "IDmodel": "MMI_3_1",
            "investigationTimeSpan": 50.0,
            "endBranchLabel": "3_1",
            "IMLValues": [5.0, 6.0, 7.0],
            "saPeriod": 0.1,
            "saDamping": 1.0,
            "IMT": "PGA",
            "PoEValues": [0.1, 0.2, 0.3]
        }),
                (shapes.Site(-122.4000, 37.5000), {
                    "IDmodel": "MMI_3_1",
                    "investigationTimeSpan": 50.0,
                    "endBranchLabel": "3_2",
                    "IMLValues": [5.0, 6.0, 7.0],
                    "saPeriod": 0.1,
                    "saDamping": 1.0,
                    "IMT": "PGA",
                    "PoEValues": [0.4, 0.5, 0.6]
                })]

        path = test.do_test_output_file(TEST_FILE_CONFIG_ONCE)
        self._initialize_writer(path)

        self.writer.serialize(data)
        self._is_xml_valid(path)
Esempio n. 13
0
    def test_the_same_calculator_is_used_between_multiple_invocations(self):
        calculator = det.DeterministicEventBasedMixin(None, None)
        calculator.params = self.engine.params

        gmf_calculator1 = calculator.gmf_calculator([shapes.Site(1.0, 1.0)])
        gmf_calculator2 = calculator.gmf_calculator([shapes.Site(1.0, 1.0)])

        self.assertTrue(gmf_calculator1 == gmf_calculator2)
Esempio n. 14
0
    def test_computes_all_the_levels_specified(self):
        self._store_dummy_hazard_curve(shapes.Site(2.0, 5.0))

        self._run([shapes.Site(2.0, 5.0)], 1, [0.25, 0.50, 0.75])

        self._has_computed_quantile_for_site(shapes.Site(2.0, 5.0), 0.25)
        self._has_computed_quantile_for_site(shapes.Site(2.0, 5.0), 0.50)
        self._has_computed_quantile_for_site(shapes.Site(2.0, 5.0), 0.75)
Esempio n. 15
0
    def test_computes_all_the_levels_specified(self):
        self.params[self.poes_levels] = "0.10 0.20 0.50"

        self._run()

        self._has_computed_IML_for_site(shapes.Site(2.0, 5.0), 0.10)
        self._has_computed_IML_for_site(shapes.Site(2.0, 5.0), 0.20)
        self._has_computed_IML_for_site(shapes.Site(2.0, 5.0), 0.50)
Esempio n. 16
0
    def test_eq_with_rounded_lon_lat(self):
        """
        Test Site equality comparisons when using high-precision lon/lat values
        (which are rounded down when the Site object is created).
        """
        site1 = shapes.Site(-121.0, 29.0000001)
        site2 = shapes.Site(-121.00000004, 29.00000006)

        self.assertEqual(site1, site2)
Esempio n. 17
0
    def test_not_eq(self):
        # Test __eq__ with 2 Blocks that should not be equal
        block1 = Block(7, 0, [shapes.Site(1.0, 1.0)])
        block2 = Block(8, 0, [shapes.Site(1.0, 1.0)])
        self.assertFalse(block1 == block2)

        block1 = Block(7, 0, [shapes.Site(1.0, 1.0)])
        block2 = Block(7, 1, [shapes.Site(1.0, 1.0)])
        self.assertFalse(block1 == block2)
Esempio n. 18
0
    def test_hazard_input_is_the_exposure_site(self):
        # when `COMPUTE_HAZARD_AT_ASSETS_LOCATIONS` is specified,
        # the hazard must be looked up on the same risk location
        # (the input parameter of the function)
        params = {config.COMPUTE_HAZARD_AT_ASSETS: True}
        job_ctxt = engine.JobContext(params, None)

        self.assertEqual(shapes.Site(1.0, 1.0),
                         hazard_input_site(job_ctxt, shapes.Site(1.0, 1.0)))
Esempio n. 19
0
 def __init__(self, *args, **kwargs):
     super(DoCurvesTestCase, self).__init__(*args, **kwargs)
     self.keys = []
     self.sites = [
         shapes.Site(-121.9, 38.0),
         shapes.Site(-121.8, 38.0),
         shapes.Site(-122.9, 38.0),
         shapes.Site(-122.8, 38.0)
     ]
Esempio n. 20
0
    def test_hash_with_rounded_lon_lat(self):
        """
        Test the __hash__() equality of two Sites when using high-precision
        lon/lat values (which are rounded down when the Site object is
        created).
        """
        site1 = shapes.Site(-121.0, 29.0000001)
        site2 = shapes.Site(-121.00000004, 29.00000006)

        self.assertEqual(site1.__hash__(), site2.__hash__())
Esempio n. 21
0
    def test_hash(self):
        """
        Verify that two Sites with the same lon/lat have the same __hash__().
        """
        lon = 121.0
        lat = 29.0

        site1 = shapes.Site(lon, lat)
        site2 = shapes.Site(lon, lat)

        self.assertEqual(site1.__hash__(), site2.__hash__())
Esempio n. 22
0
 def test_site_precision_matters(self):
     FLAGS.distance_precision = 11
     lat = 10.5
     lon = -49.5
     first_site = shapes.Site(lon, lat)
     lat += 0.0000001
     lon += 0.0000001
     second_site = shapes.Site(lon, lat)
     self.assertEqual(first_site, second_site)
     FLAGS.distance_precision = 12
     self.assertNotEqual(first_site, second_site)
Esempio n. 23
0
    def test_just_numeric_values_are_allowed(self):
        self.params[self.quantiles_levels] = \
                "-0.33 0.00 XYZ 0.50 ;;; 1.00 BBB"

        self._run([shapes.Site(2.0, 5.0)])

        self._has_computed_quantile_for_site(shapes.Site(2.0, 5.0), 0.00)
        self._has_computed_quantile_for_site(shapes.Site(2.0, 5.0), 0.50)
        self._has_computed_quantile_for_site(shapes.Site(2.0, 5.0), 1.00)

        self._no_computed_quantiles_for(0.33)
Esempio n. 24
0
    def test_eq(self):
        """
        Test Site equality comparisons. Two sites with the same lon/lat should
        be considered equal.
        """
        lon = 121.0
        lat = 29.0

        site1 = shapes.Site(lon, lat)
        site2 = shapes.Site(lon, lat)

        self.assertEqual(site1, site2)
Esempio n. 25
0
    def test_an_empty_hazard_curve_produces_an_empty_quantile_curve(self):
        hazard_curve = []
        self._store_hazard_curve_at(shapes.Site(2.0, 5.0), hazard_curve)

        self._run([shapes.Site(2.0, 5.0)], 1, [0.75])

        result = kvs.get_value_json_decoded(
            kvs.tokens.quantile_hazard_curve_key(self.job_id,
                                                 shapes.Site(2.0, 5.0), 0.75))

        # no values
        self.assertTrue(numpy.allclose([], numpy.array(result)))
Esempio n. 26
0
    def test_serialize(self):
        data = [(shapes.Site(1.0, 1.0), {
            "groundMotionValue": 0.25,
            "path": "filea"
        }),
                (shapes.Site(1.0, 2.0), {
                    "groundMotionValue": 0.35,
                    "path": "fileb"
                })]

        self.writer.serialize(data)

        self.assertTrue(xml.validates_against_xml_schema(self.FILENAME))
Esempio n. 27
0
    def test_prepares_blocks_using_the_exposure(self):
        a_job = Job({
            EXPOSURE:
            os.path.join(test.SCHEMA_EXAMPLES_DIR, EXPOSURE_TEST_FILE)
        })
        a_job._partition()
        blocks_keys = a_job.blocks_keys

        expected_block = job.Block(
            (shapes.Site(9.15000, 45.16667), shapes.Site(9.15333, 45.12200),
             shapes.Site(9.14777, 45.17999)))

        self.assertEqual(1, len(blocks_keys))
        self.assertEqual(expected_block, job.Block.from_kvs(blocks_keys[0]))
Esempio n. 28
0
    def test_sites_can_be_keys(self):
        """ Site objects can be dictionary keys,
        So must hash reliably."""
        lat = 10.5
        lon = -49.5
        first_site = shapes.Site(lon, lat)
        second_site = shapes.Site(lon, lat)
        sites = {}
        sites[first_site] = "one"
        sites[second_site] = "two"
        # BOTH will now be "two"! This is correct

        self.assertEqual(first_site, second_site)
        self.assertEqual(sites[first_site], "two")
        self.assertEqual(sites[second_site], "two")
Esempio n. 29
0
    def test_block_kvs_serialization(self):
        # Test that a Block is properly serialized/deserialized from the cache.
        job_id = 7
        block_id = 0
        expected_block = Block(job_id, block_id,
                               [shapes.Site(1.0, 1.0),
                                shapes.Site(2.0, 2.0)])
        expected_block.to_kvs()

        actual_block = Block.from_kvs(job_id, block_id)

        self.assertEqual(expected_block, actual_block)
        # The sites are not compared in Block.__eq__; we need to check those
        # also.
        self.assertEqual(expected_block.sites, actual_block.sites)
Esempio n. 30
0
    def test_hazard_input_is_the_cell_center(self):
        # when `COMPUTE_HAZARD_AT_ASSETS_LOCATIONS` is not specified,
        # the hazard must be looked up on the center of the cell
        # where the given site falls in
        params = {config.INPUT_REGION: \
            "1.0, 1.0, 2.0, 1.0, 2.0, 2.0, 1.0, 2.0",
            config.REGION_GRID_SPACING: 0.5}

        job_ctxt = engine.JobContext(params, None)

        self.assertEqual(shapes.Site(1.0, 1.0),
                         hazard_input_site(job_ctxt, shapes.Site(1.2, 1.2)))

        self.assertEqual(shapes.Site(1.5, 1.5),
                         hazard_input_site(job_ctxt, shapes.Site(1.6, 1.6)))