Beispiel #1
0
    def test_compute_uhs_with_site_model(self):
        the_job = helpers.prepare_job_context(
            helpers.demo_file('uhs/config_with_site_model.gem'))
        the_job.to_kvs()

        site = Site(0, 0)

        helpers.store_hazard_logic_trees(the_job)

        get_sm_patch = helpers.patch(
            'openquake.calculators.hazard.general.get_site_model')
        get_closest_patch = helpers.patch(
            'openquake.calculators.hazard.general.get_closest_site_model_data')
        compute_patch = helpers.patch(
            'openquake.calculators.hazard.uhs.core._compute_uhs')

        get_sm_mock = get_sm_patch.start()
        get_closest_mock = get_closest_patch.start()
        compute_mock = compute_patch.start()

        get_closest_mock.return_value = SiteModel(
            vs30=800, vs30_type='measured', z1pt0=100, z2pt5=200)
        try:
            compute_uhs(the_job, site)

            self.assertEqual(1, get_sm_mock.call_count)
            self.assertEqual(1, get_closest_mock.call_count)
            self.assertEqual(1, compute_mock.call_count)
        finally:
            get_sm_patch.stop()
            get_closest_patch.stop()
            compute_patch.stop()
Beispiel #2
0
    def test_parameterize_sites_with_site_model(self):
        job_ctxt = helpers.prepare_job_context(
            helpers.demo_file(
                'simple_fault_demo_hazard/config_with_site_model.gem'))

        calc = classical.ClassicalHazardCalculator(job_ctxt)
        calc.initialize()

        # This tests to ensure that the `initialize` implementation for this
        # calculator properly stores the site model in the DB.

        # NOTE: If this test ever breaks, it's probably because the
        # ClassicalHazardCalculator is no longer calling the `initalize` code
        # in its super class (BaseHazardCalculator).
        site_model = hazard_general.get_site_model(job_ctxt.oq_job.id)
        self.assertIsNotNone(site_model)

        set_params_patch = helpers.patch(
            'openquake.calculators.hazard.general.set_java_site_parameters')
        closest_data_patch = helpers.patch(
            'openquake.calculators.hazard.general.get_closest_site_model_data')
        sp_mock = set_params_patch.start()
        cd_mock = closest_data_patch.start()

        try:
            calc.parameterize_sites(job_ctxt.sites_to_compute())

            exp_call_count = len(job_ctxt.sites_to_compute())
            self.assertEqual(exp_call_count, sp_mock.call_count)
            self.assertEqual(exp_call_count, cd_mock.call_count)

        finally:
            # tear down the patches
            set_params_patch.stop()
            closest_data_patch.stop()
Beispiel #3
0
    def test_parameterize_sites_no_site_model(self):
        job_ctxt = helpers.prepare_job_context(
            helpers.demo_file('simple_fault_demo_hazard/config.gem'))

        calc = classical.ClassicalHazardCalculator(job_ctxt)

        jsites = calc.parameterize_sites(job_ctxt.sites_to_compute())

        # expected params:
        jp = job_ctxt.oq_job_profile

        exp_sadigh = job_params.REVERSE_ENUM_MAP[jp.sadigh_site_type]
        exp_vs30 = jp.reference_vs30_value
        exp_vs30_type = jp.vs30_type
        exp_z1pt0 = jp.depth_to_1pt_0km_per_sec
        exp_z2pt5 = jp.reference_depth_to_2pt5km_per_sec_param

        for jsite in jsites:
            self.assertEqual(exp_vs30,
                             jsite.getParameter('Vs30').getValue().value)
            self.assertEqual(exp_vs30_type,
                             jsite.getParameter('Vs30 Type').getValue())
            self.assertEqual(
                exp_z1pt0,
                jsite.getParameter('Depth 1.0 km/sec').getValue().value)
            self.assertEqual(
                exp_z2pt5,
                jsite.getParameter('Depth 2.5 km/sec').getValue().value)
            self.assertEqual(exp_sadigh,
                             jsite.getParameter('Sadigh Site Type').getValue())
Beispiel #4
0
    def test_initialize_calls_validate(self):
        # Test make sure the calculator `initialize` calls
        # `validate_site_model`.
        job_ctxt = helpers.prepare_job_context(
            helpers.demo_file(
                'simple_fault_demo_hazard/config_with_site_model.gem'))

        calc = general.BaseHazardCalculator(job_ctxt)
        patch_path = 'openquake.calculators.hazard.general.validate_site_model'

        with helpers.patch(patch_path) as validate_patch:
            calc.initialize()
            # validate_site_model itself is tested in another test
            # here, we just make sure it gets called
            self.assertEqual(1, validate_patch.call_count)
    def test_initialize_calls_validate(self):
        # Test make sure the calculator `initialize` calls
        # `validate_site_model`.
        job_ctxt = helpers.prepare_job_context(
            helpers.demo_file(
                'simple_fault_demo_hazard/config_with_site_model.gem'
            )
        )

        calc = general.BaseHazardCalculator(job_ctxt)
        patch_path = 'openquake.calculators.hazard.general.validate_site_model'

        with helpers.patch(patch_path) as validate_patch:
            calc.initialize()
            # validate_site_model itself is tested in another test
            # here, we just make sure it gets called
            self.assertEqual(1, validate_patch.call_count)
    def test_compute_disagg_matrix_calls_site_model_fns(self):
        # Test that `compute_disagg_matrix` calls the required site model
        # functions when the configuration defines a site model.
        cfg = helpers.demo_file('disaggregation/config_with_site_model.gem')

        the_job = helpers.prepare_job_context(cfg)
        the_job.to_kvs()
        calc = disagg_core.DisaggHazardCalculator(the_job)
        calc.initialize()  # store the site model

        helpers.store_hazard_logic_trees(the_job)

        site = shapes.Site(0.0, 0.0)
        poe = 0.1
        result_dir = tempfile.gettempdir()

        get_sm_patch = helpers.patch(
            'openquake.calculators.hazard.general.get_site_model')
        get_closest_patch = helpers.patch(
            'openquake.calculators.hazard.general.get_closest_site_model_data')
        compute_patch = helpers.patch(
            'openquake.calculators.hazard.disagg.core._compute_matrix')
        save_patch = helpers.patch(
            'openquake.calculators.hazard.disagg.core.save_5d_matrix_to_h5')

        get_sm_mock = get_sm_patch.start()
        get_closest_mock = get_closest_patch.start()
        compute_mock = compute_patch.start()
        save_mock = save_patch.start()

        try:
            _, _ = disagg_core.compute_disagg_matrix(the_job, site, poe,
                                                     result_dir)

            self.assertEqual(1, get_sm_mock.call_count)
            self.assertEqual(1, get_closest_mock.call_count)
            self.assertEqual(1, compute_mock.call_count)
            self.assertEqual(1, save_mock.call_count)
        finally:
            get_sm_patch.stop()
            get_closest_patch.stop()
            compute_patch.stop()
            save_patch.stop()
Beispiel #7
0
    def test_compute_disagg_matrix_calls_site_model_fns(self):
        # Test that `compute_disagg_matrix` calls the required site model
        # functions when the configuration defines a site model.
        cfg = helpers.demo_file('disaggregation/config_with_site_model.gem')

        the_job = helpers.prepare_job_context(cfg)
        the_job.to_kvs()
        calc = disagg_core.DisaggHazardCalculator(the_job)
        calc.initialize()  # store the site model

        helpers.store_hazard_logic_trees(the_job)

        site = shapes.Site(0.0, 0.0)
        poe = 0.1
        result_dir = tempfile.gettempdir()

        get_sm_patch = helpers.patch(
            'openquake.calculators.hazard.general.get_site_model')
        get_closest_patch = helpers.patch(
            'openquake.calculators.hazard.general.get_closest_site_model_data')
        compute_patch = helpers.patch(
            'openquake.calculators.hazard.disagg.core._compute_matrix')
        save_patch = helpers.patch(
            'openquake.calculators.hazard.disagg.core.save_5d_matrix_to_h5')

        get_sm_mock = get_sm_patch.start()
        get_closest_mock = get_closest_patch.start()
        compute_mock = compute_patch.start()
        save_mock = save_patch.start()

        try:
            _, _ = disagg_core.compute_disagg_matrix(the_job, site, poe,
                                                     result_dir)

            self.assertEqual(1, get_sm_mock.call_count)
            self.assertEqual(1, get_closest_mock.call_count)
            self.assertEqual(1, compute_mock.call_count)
            self.assertEqual(1, save_mock.call_count)
        finally:
            get_sm_patch.stop()
            get_closest_patch.stop()
            compute_patch.stop()
            save_patch.stop()
Beispiel #8
0
    def test_parameterize_sites_with_site_model(self):
        job_ctxt = helpers.prepare_job_context(
            helpers.demo_file(
                'simple_fault_demo_hazard/config_with_site_model.gem'
            )
        )

        calc = classical.ClassicalHazardCalculator(job_ctxt)
        calc.initialize()

        # This tests to ensure that the `initialize` implementation for this
        # calculator properly stores the site model in the DB.

        # NOTE: If this test ever breaks, it's probably because the
        # ClassicalHazardCalculator is no longer calling the `initalize` code
        # in its super class (BaseHazardCalculator).
        site_model = hazard_general.get_site_model(job_ctxt.oq_job.id)
        self.assertIsNotNone(site_model)

        set_params_patch = helpers.patch(
            'openquake.calculators.hazard.general.set_java_site_parameters'
        )
        closest_data_patch = helpers.patch(
            'openquake.calculators.hazard.general.get_closest_site_model_data'
        )
        sp_mock = set_params_patch.start()
        cd_mock = closest_data_patch.start()

        try:
            calc.parameterize_sites(job_ctxt.sites_to_compute())

            exp_call_count = len(job_ctxt.sites_to_compute())
            self.assertEqual(exp_call_count, sp_mock.call_count)
            self.assertEqual(exp_call_count, cd_mock.call_count)

        finally:
            # tear down the patches
            set_params_patch.stop()
            closest_data_patch.stop()
Beispiel #9
0
    def test_parameterize_sites_no_site_model(self):
        job_ctxt = helpers.prepare_job_context(
            helpers.demo_file('simple_fault_demo_hazard/config.gem')
        )

        calc = classical.ClassicalHazardCalculator(job_ctxt)

        jsites = calc.parameterize_sites(job_ctxt.sites_to_compute())

        # expected params:
        jp = job_ctxt.oq_job_profile

        exp_sadigh = job_params.REVERSE_ENUM_MAP[jp.sadigh_site_type]
        exp_vs30 = jp.reference_vs30_value
        exp_vs30_type = jp.vs30_type
        exp_z1pt0 = jp.depth_to_1pt_0km_per_sec
        exp_z2pt5 = jp.reference_depth_to_2pt5km_per_sec_param

        for jsite in jsites:
            self.assertEqual(
                exp_vs30, jsite.getParameter('Vs30').getValue().value
            )
            self.assertEqual(
                exp_vs30_type, jsite.getParameter('Vs30 Type').getValue()
            )
            self.assertEqual(
                exp_z1pt0,
                jsite.getParameter('Depth 1.0 km/sec').getValue().value
            )
            self.assertEqual(
                exp_z2pt5,
                jsite.getParameter('Depth 2.5 km/sec').getValue().value
            )
            self.assertEqual(
                exp_sadigh, jsite.getParameter('Sadigh Site Type').getValue()
            )
    def test_initialize_stores_site_model(self):
        job_ctxt = helpers.prepare_job_context(
            helpers.demo_file(
                'simple_fault_demo_hazard/config_with_site_model.gem'))

        calc = general.BaseHazardCalculator(job_ctxt)

        [site_model_input] = models.inputs4job(
            job_ctxt.oq_job.id, input_type='site_model')

        site_model_nodes = models.SiteModel.objects.filter(
            input=site_model_input)

        # Test precondition: The site_model table shouldn't be populated yet.
        self.assertEqual(0, len(site_model_nodes))

        calc.initialize()

        # Now it should be populated.
        site_model_nodes = models.SiteModel.objects.filter(
            input=site_model_input)
        # It would be overkill to test the contents; just check that the number
        # of records is correct.
        self.assertEqual(2601, len(site_model_nodes))
Beispiel #11
0
    def test_initialize_stores_site_model(self):
        job_ctxt = helpers.prepare_job_context(
            helpers.demo_file(
                'simple_fault_demo_hazard/config_with_site_model.gem'))

        calc = general.BaseHazardCalculator(job_ctxt)

        [site_model_input] = models.inputs4job(
            job_ctxt.oq_job.id, input_type='site_model')

        site_model_nodes = models.SiteModel.objects.filter(
            input=site_model_input)

        # Test precondition: The site_model table shouldn't be populated yet.
        self.assertEqual(0, len(site_model_nodes))

        calc.initialize()

        # Now it should be populated.
        site_model_nodes = models.SiteModel.objects.filter(
            input=site_model_input)
        # It would be overkill to test the contents; just check that the number
        # of records is correct.
        self.assertEqual(2601, len(site_model_nodes))