コード例 #1
0
ファイル: core.py プロジェクト: Chunghan/oq-engine
def compute_hazard_curves_task(job_id, src_ids, lt_rlz_id, ltp):
    """
    Task wrapper around

    :func:`openquake.engine.calculators.hazard.classical.core.compute_hazard_curves`.
    """
    core.compute_hazard_curves(job_id, src_ids, lt_rlz_id, ltp)
コード例 #2
0
ファイル: core_test.py プロジェクト: Chunghan/oq-engine
    def test_workflow(self):
        # Test `pre_execute` to ensure that all stats are properly initialized.
        # Then test the core disaggregation function.
        self.calc.pre_execute()
        engine.save_job_stats(self.job)
        job_stats = models.JobStats.objects.get(oq_job=self.job.id)
        self.assertEqual(2, job_stats.num_sites)

        # To test the disagg function, we first need to compute the hazard
        # curves:
        for args in self.calc.task_arg_gen(1):
            cls_core.compute_hazard_curves(*args)
        self.calc.finalize_hazard_curves()

        diss1, diss2, diss3, diss4 = list(self.calc.disagg_task_arg_gen(1))

        base_path = 'openquake.engine.calculators.hazard.disaggregation.core'

        disagg_calc_func = (
            'openquake.hazardlib.calc.disagg.disaggregation_poissonian'
        )
        with mock.patch(disagg_calc_func) as disagg_mock:
            disagg_mock.return_value = (None, None)
            with mock.patch('%s.%s' % (base_path, '_save_disagg_matrix')
                            ) as save_mock:
                # Some of these tasks will not compute anything, since the
                # hazard  curves for these few are all 0.0s.

                # Here's what we expect:
                # diss1: compute
                # diss2: skip
                # diss3: compute
                # diss4: skip

                disagg_core.compute_disagg(*diss1)
                # 2 poes * 2 imts * 1 site = 4
                self.assertEqual(4, disagg_mock.call_count)
                self.assertEqual(0, save_mock.call_count)  # no rupt generated

                disagg_core.compute_disagg(*diss2)
                self.assertEqual(4, disagg_mock.call_count)
                self.assertEqual(0, save_mock.call_count)  # no rupt generated

                disagg_core.compute_disagg(*diss3)
                self.assertEqual(8, disagg_mock.call_count)
                self.assertEqual(0, save_mock.call_count)  # no rupt generated

                disagg_core.compute_disagg(*diss4)
                self.assertEqual(8, disagg_mock.call_count)
                self.assertEqual(0, save_mock.call_count)  # no rupt generated
コード例 #3
0
def disagg_task(job_id, block, lt_rlz_id, ltp, calc_type):
    """
    Task wrapper around core hazard curve/disaggregation computation functions.

    :param int job_id:
        ID of the currently running job.
    :param block:
        A sequence of work items for this task to process. In the case of
        hazard curve computation, this is a sequence of source IDs. In the case
        of disaggregation, this is a list of
        :class:`openquake.hazardlib.site.Site` objects.

        For more info, see
        :func:`openquake.engine.calculators.hazard.classical.core.\
compute_hazard_curves`
        if ``calc_type`` is 'hazard_curve' and :func:`compute_disagg` if
        ``calc_type`` is 'disagg'.
    :param lt_rlz_id:
        ID of the :class:`openquake.engine.db.models.LtRealization` for this
        part of the computation.
    :param ltp:
        a :class:`openquake.engine.input.LogicTreeProcessor` instance
    :param calc_type:
        'hazard_curve' or 'disagg'. This indicates more or less the calculation
        phase; first we must computed all of the hazard curves, then we can
        compute the disaggregation histograms.
    """
    if calc_type == 'hazard_curve':
        classical.compute_hazard_curves(job_id, block, lt_rlz_id, ltp)
    elif calc_type == 'disagg':
        compute_disagg(job_id, block, lt_rlz_id, ltp)
    else:
        msg = ('Invalid calculation type "%s";'
               ' expected "hazard_curve" or "disagg"')
        msg %= calc_type
        raise RuntimeError(msg)

    base.signal_task_complete(job_id=job_id,
                              num_items=len(block),
                              calc_type=calc_type)
コード例 #4
0
ファイル: core.py プロジェクト: 4x/oq-engine
def disagg_task(job_id, block, lt_rlz_id, calc_type):
    """
    Task wrapper around core hazard curve/disaggregation computation functions.

    :param int job_id:
        ID of the currently running job.
    :param block:
        A sequence of work items for this task to process. In the case of
        hazard curve computation, this is a sequence of source IDs. In the case
        of disaggregation, this is a list of
        :class:`openquake.hazardlib.site.Site` objects.

        For more info, see
        :func:`openquake.engine.calculators.hazard.classical.core.\
compute_hazard_curves`
        if ``calc_type`` is 'hazard_curve' and :func:`compute_disagg` if
        ``calc_type`` is 'disagg'.
    :param lt_rlz_id:
        ID of the :class:`openquake.engine.db.models.LtRealization` for this
        part of the computation.
    :param calc_type:
        'hazard_curve' or 'disagg'. This indicates more or less the calculation
        phase; first we must computed all of the hazard curves, then we can
        compute the disaggregation histograms.
    """
    if calc_type == 'hazard_curve':
        classical.compute_hazard_curves(job_id, block, lt_rlz_id)
    elif calc_type == 'disagg':
        compute_disagg(job_id, block, lt_rlz_id)
    else:
        msg = ('Invalid calculation type "%s";'
               ' expected "hazard_curve" or "disagg"')
        msg %= calc_type
        raise RuntimeError(msg)

    base.signal_task_complete(
        job_id=job_id, num_items=len(block), calc_type=calc_type)
コード例 #5
0
ファイル: core_test.py プロジェクト: kenxshao/oq-engine
    def test_workflow(self):
        # Test `pre_execute` to ensure that all stats are properly initialized.
        # Then test the core disaggregation function.
        self.calc.pre_execute()

        job_stats = models.JobStats.objects.get(oq_job=self.job.id)
        self.assertEqual(2, job_stats.num_realizations)
        self.assertEqual(2, job_stats.num_sites)
        self.assertEqual(12, job_stats.num_tasks)

        self.assertEqual(
            {'hc_computed': 0, 'total': 12, 'hc_total': 8, 'computed': 0,
             'in_queue': 0},
            self.calc.progress
        )

        # To test the disagg function, we first need to compute the hazard
        # curves:
        for args in self.calc.task_arg_gen(1):
            # drop the calc_type from the args:
            cls_core.compute_hazard_curves(*args[0:-1])
        self.calc.finalize_hazard_curves()

        diss1, diss2, diss3, diss4 = list(self.calc.disagg_task_arg_gen(1))

        base_path = 'openquake.engine.calculators.hazard.disaggregation.core'

        disagg_calc_func = (
            'openquake.hazardlib.calc.disagg.disaggregation_poissonian'
        )
        with mock.patch(disagg_calc_func) as disagg_mock:
            disagg_mock.return_value = (None, None)
            with mock.patch('%s.%s' % (base_path, '_save_disagg_matrix')
                            ) as save_mock:
                # Some of these tasks will not compute anything, since the
                # hazard  curves for these few are all 0.0s.

                # Here's what we expect:
                # diss1: compute
                # diss2: skip
                # diss3: compute
                # diss4: skip

                disagg_core.compute_disagg(*diss1[0:-1])
                # 2 poes * 2 imts * 1 site = 4
                self.assertEqual(4, disagg_mock.call_count)
                self.assertEqual(4, save_mock.call_count)

                disagg_core.compute_disagg(*diss2[0:-1])
                self.assertEqual(4, disagg_mock.call_count)
                self.assertEqual(4, save_mock.call_count)

                disagg_core.compute_disagg(*diss3[0:-1])
                self.assertEqual(8, disagg_mock.call_count)
                self.assertEqual(8, save_mock.call_count)

                disagg_core.compute_disagg(*diss4[0:-1])
                self.assertEqual(8, disagg_mock.call_count)
                self.assertEqual(8, save_mock.call_count)

        # Finally, test that realization data is up to date and correct:
        rlzs = models.LtRealization.objects.filter(
            hazard_calculation=self.calc.hc)

        for rlz in rlzs:
            self.assertEqual(6, rlz.total_items)
            self.assertEqual(6, rlz.completed_items)
            self.assertTrue(rlz.is_complete)
コード例 #6
0
    def test_workflow(self):
        # Test `pre_execute` to ensure that all stats are properly initialized.
        # Then test the core disaggregation function.
        self.calc.pre_execute()

        job_stats = models.JobStats.objects.get(oq_job=self.job.id)
        self.assertEqual(2, job_stats.num_realizations)
        self.assertEqual(2, job_stats.num_sites)
        self.assertEqual(12, job_stats.num_tasks)

        self.assertEqual(
            {'hc_computed': 0, 'total': 12, 'hc_total': 8, 'computed': 0,
             'in_queue': 0},
            self.calc.progress
        )

        # To test the disagg function, we first need to compute the hazard
        # curves:
        for args in self.calc.task_arg_gen(1):
            # drop the calc_type from the args:
            cls_core.compute_hazard_curves(*args[0:-1])
        self.calc.finalize_hazard_curves()

        diss1, diss2, diss3, diss4 = list(self.calc.disagg_task_arg_gen(1))

        base_path = 'openquake.engine.calculators.hazard.disaggregation.core'

        disagg_calc_func = (
            'openquake.hazardlib.calc.disagg.disaggregation_poissonian'
        )
        with mock.patch(disagg_calc_func) as disagg_mock:
            disagg_mock.return_value = (None, None)
            with mock.patch('%s.%s' % (base_path, '_save_disagg_matrix')
                            ) as save_mock:
                # Some of these tasks will not compute anything, since the
                # hazard  curves for these few are all 0.0s.

                # Here's what we expect:
                # diss1: compute
                # diss2: skip
                # diss3: compute
                # diss4: skip

                disagg_core.compute_disagg(*diss1[0:-1])
                # 2 poes * 2 imts * 1 site = 4
                self.assertEqual(4, disagg_mock.call_count)
                self.assertEqual(0, save_mock.call_count)  # no rupt generated

                disagg_core.compute_disagg(*diss2[0:-1])
                self.assertEqual(4, disagg_mock.call_count)
                self.assertEqual(0, save_mock.call_count)  # no rupt generated

                disagg_core.compute_disagg(*diss3[0:-1])
                self.assertEqual(8, disagg_mock.call_count)
                self.assertEqual(0, save_mock.call_count)  # no rupt generated

                disagg_core.compute_disagg(*diss4[0:-1])
                self.assertEqual(8, disagg_mock.call_count)
                self.assertEqual(0, save_mock.call_count)  # no rupt generated

        # Finally, test that realization data is up to date and correct:
        rlzs = models.LtRealization.objects.filter(
            hazard_calculation=self.calc.hc)

        for rlz in rlzs:
            self.assertEqual(6, rlz.total_items)
            self.assertEqual(6, rlz.completed_items)
            self.assertTrue(rlz.is_complete)