示例#1
0
    def test_pre_execute_check_imts_raises(self):
        haz_job = engine.prepare_job()
        cfg = helpers.get_data_path('classical_job.ini')
        params, files = engine.parse_config(open(cfg, 'r'))
        haz_job.hazard_calculation = engine.create_hazard_calculation(
            haz_job.owner, params, files.values())
        haz_job.save()

        hazard_curve_output = models.Output.objects.create_output(
            haz_job, 'test_hazard_curve', 'hazard_curve'
        )
        models.HazardCurve.objects.create(
            output=hazard_curve_output,
            investigation_time=50.0,
            imt='PGV',  # the vulnerability model only defines SA(0.1)
            statistics='mean'
        )

        cfg = helpers.get_data_path(
            'end-to-end-hazard-risk/job_risk_classical.ini')
        risk_job = helpers.get_risk_job(
            cfg, hazard_output_id=hazard_curve_output.id
        )
        models.JobStats.objects.create(oq_job=risk_job)
        calc = classical.ClassicalRiskCalculator(risk_job)

        # Check for compatibility between the IMTs defined in the vulnerability
        # model and the chosen hazard output (--hazard-output-id)
        with self.assertRaises(ValueError) as ar:
            calc.pre_execute()
        self.assertEqual(
            "There is no hazard output for: SA(0.1). "
            "The available IMTs are: PGA.",
            ar.exception.message)
示例#2
0
    def test_pre_execute_check_imts_no_errors(self):
        haz_job = engine.prepare_job()

        cfg = helpers.get_data_path(
            'end-to-end-hazard-risk/job_haz_classical.ini')
        params, files = engine.parse_config(open(cfg, 'r'))
        haz_job.hazard_calculation = engine.create_hazard_calculation(
            haz_job.owner, params, files.values())
        haz_job.save()

        hazard_curve_output = models.Output.objects.create_output(
            haz_job, 'test_hazard_curve', 'hazard_curve'
        )
        models.HazardCurve.objects.create(
            output=hazard_curve_output,
            investigation_time=50.0,
            # this imt is compatible with the vuln model
            imt='SA',
            sa_period=0.025,
            sa_damping=5.0,
            statistics='mean'
        )

        cfg = helpers.get_data_path(
            'end-to-end-hazard-risk/job_risk_classical.ini')
        risk_job = helpers.get_risk_job(
            cfg, hazard_output_id=hazard_curve_output.id
        )
        models.JobStats.objects.create(oq_job=risk_job)
        calc = classical.ClassicalRiskCalculator(risk_job)

        # In contrast to the test above (`test_pre_execute_check_imts_raises`),
        # we expect no errors to be raised.
        calc.pre_execute()
示例#3
0
    def test_del_risk_calc(self):
        risk_job, _ = helpers.get_risk_job(
            self.risk_cfg, self.hazard_cfg,
            output_type='curves', username=getpass.getuser()
        )
        risk_calc = risk_job.risk_calculation

        models.Output.objects.create_output(
            risk_job, 'test_curves_1', output_type='loss_curve'
        )
        models.Output.objects.create_output(
            risk_job, 'test_curves_2', output_type='loss_curve'
        )

        # Sanity check: make sure the risk calculation and outputs exist in
        # the database:
        risk_calcs = models.RiskCalculation.objects.filter(
            id=risk_calc.id
        )
        self.assertEqual(1, risk_calcs.count())

        outputs = models.Output.objects.filter(oq_job=risk_job.id)
        self.assertEqual(2, outputs.count())

        # Delete the calculation
        engine2.del_risk_calc(risk_calc.id)

        # Check that the risk calculation and its outputs were deleted:
        outputs = models.Output.objects.filter(oq_job=risk_job.id)
        self.assertEqual(0, outputs.count())

        risk_calcs = models.RiskCalculation.objects.filter(
            id=risk_calc.id
        )
        self.assertEqual(0, risk_calcs.count())
    def setUp(self):
        self.job, _ = helpers.get_risk_job(
            'classical_psha_based_risk/job.ini',
            'simple_fault_demo_hazard/job.ini')

        models.HazardCurveData.objects.create(
            hazard_curve=self.job.risk_calculation.hazard_output.hazardcurve,
            poes=[0.2, 0.3, 0.4],
            location="POINT(3 3)")
示例#5
0
    def test_a_few_inputs(self):
        job, files = helpers.get_risk_job("classical_psha_based_risk/job.ini", "simple_fault_demo_hazard/job.ini")
        rc = job.risk_calculation

        expected_ids = sorted([x.id for x in files.values()])

        inputs = models.inputs4rcalc(rc.id)

        actual_ids = sorted([x.id for x in inputs])

        self.assertEqual(expected_ids, actual_ids)
示例#6
0
    def setUp(self):
        self.job, _ = helpers.get_risk_job(
            'event_based_risk/job.ini',
            'event_based_hazard/job.ini', output_type="gmf")

        self.calculator = event_based.EventBasedRiskCalculator(self.job)
        self.calculator.pre_execute()

        self.job.is_running = True
        self.job.status = 'executing'
        self.job.save()
示例#7
0
    def setUp(self):
        self.job, _ = helpers.get_risk_job(
            demo_file('scenario_risk/job.ini'),
            demo_file('scenario_hazard/job.ini'), output_type="gmf_scenario")

        self.calculator = scenario.ScenarioRiskCalculator(self.job)
        self.calculator.pre_execute()

        self.job.is_running = True
        self.job.status = 'executing'
        self.job.save()
示例#8
0
    def test_del_risk_calc_no_access(self):
        # Test the case where we try to delete a risk calculation which does
        # not belong to current user.
        # In this case, deletion is now allowed and should raise an exception.
        risk_job, _ = helpers.get_risk_job(
            self.risk_cfg, self.hazard_cfg,
            output_type='curves', username=helpers.random_string()
        )
        risk_calc = risk_job.risk_calculation

        self.assertRaises(RuntimeError, engine2.del_risk_calc, risk_calc.id)
示例#9
0
    def test_with_input_type(self):
        job, files = helpers.get_risk_job("classical_psha_based_risk/job.ini", "simple_fault_demo_hazard/job.ini")
        rc = job.risk_calculation

        # It should only be 1 id, actually.
        expected_ids = [x.id for x in files.values() if x.input_type == "exposure"]

        inputs = models.inputs4rcalc(rc.id, input_type="exposure")

        actual_ids = sorted([x.id for x in inputs])

        self.assertEqual(expected_ids, actual_ids)
示例#10
0
 def setUp(self):
     job, _ = helpers.get_risk_job('classical_psha_based_risk/job.ini',
                                   'simple_fault_demo_hazard/job.ini')
     self.compulsory_arguments = dict(
         calculation_mode="classical",
         lrem_steps_per_interval=5)
     self.other_args = dict(
         owner=helpers.default_user(),
         region_constraint=(
             'POLYGON((-122.0 38.113, -122.114 38.113, -122.57 38.111, '
             '-122.0 38.113))'),
         hazard_output=job.risk_calculation.hazard_output)
示例#11
0
    def test_del_haz_calc_output_referenced_by_risk_calc(self):
        # Test the case where a risk calculation is referencing one of the
        # belonging to the hazard calculation we want to delete.
        # In this case, deletion is not allowed and should raise an exception.
        risk_job, _ = helpers.get_risk_job(
            self.risk_cfg, self.hazard_cfg,
            output_type='curves', username=getpass.getuser()
        )
        hazard_job = risk_job.risk_calculation.hazard_output.oq_job
        hazard_calc = hazard_job.hazard_calculation

        self.assertRaises(RuntimeError, engine2.del_haz_calc, hazard_calc.id)
示例#12
0
    def setUp(self):
        self.job, _ = helpers.get_risk_job(
            self.risk_demo, self.hazard_demo, self.hazard_output_type)

        # need to run pre-execute to parse exposure model
        calc = BaseRiskCalculator(self.job)
        calc.pre_execute()

        self._assets = models.ExposureData.objects.filter(
            exposure_model=self.job.risk_calculation.exposure_model).order_by(
                'asset_ref')

        self.getter = self.getter_class(
            self.ho().id, "PGA", self.assets(), 500000)
示例#13
0
    def setUp(self):
        self.job, _ = helpers.get_risk_job(
            demo_file('classical_psha_based_risk/job.ini'),
            demo_file('simple_fault_demo_hazard/job.ini'))
        calculator = general_risk.BaseRiskCalculator(self.job)
        calculator.pre_execute()
        self.model = self.job.risk_calculation.exposure_model

        common_fake_args = dict(
            exposure_model=self.model,
            stco=1,
            number_of_units=10,
            reco=1,
            taxonomy="test")

        asset = models.ExposureData(site=Point(0.5, 0.5),
                                    asset_ref="test1",
                                    **common_fake_args)
        asset.save()

        asset = models.ExposureData(site=Point(179.1, 0),
                                    asset_ref="test2",
                                    **common_fake_args)
        asset.save()
示例#14
0
 def setUp(self):
     self.job, _ = helpers.get_risk_job("classical_psha_based_risk/job.ini", "simple_fault_demo_hazard/job.ini")
示例#15
0
 def setUp(self):
     cfg = helpers.get_data_path(
         'end-to-end-hazard-risk/job_risk_classical.ini')
     self.job = helpers.get_risk_job(cfg)
     self.calc = base.RiskCalculator(self.job)
示例#16
0
 def setUp(self):
     self.job, _ = helpers.get_risk_job(
         demo_file('classical_psha_based_risk/job.ini'),
         demo_file('simple_fault_demo_hazard/job.ini'))
示例#17
0
    def setUp(self):
        self.job, _ = helpers.get_risk_job(
            demo_file('event_based_bcr/job.ini'),
            demo_file('event_based_hazard/job.ini'), output_type="gmf")

        self.calculator = core.EventBasedBCRRiskCalculator(self.job)
示例#18
0
 def setUp(self):
     self.job, _ = helpers.get_risk_job('event_based_risk/job.ini',
                                        'event_based_hazard/job.ini')
示例#19
0
 def setUp(self):
     self.job, _ = helpers.get_risk_job(
         demo_file('event_based_risk/job.ini'),
         demo_file('event_based_hazard/job.ini')
     )
示例#20
0
    def test_event_based_risk_export(self):
        # Tests that outputs of a risk classical calculation are exported
        target_dir = tempfile.mkdtemp()

        try:
            # use get_risk_job to create a fake GmfCollection
            job, _ = helpers.get_risk_job('event_based_risk/job.ini',
                                          'event_based_hazard/job.ini',
                                          'gmf')

            cfg = helpers.demo_file('event_based_risk/job.ini')

            # run the calculation to create something to export

            # at the moment, only gmf for a specific realization are
            # supported as hazard input
            retcode = helpers.run_risk_job_sp(
                cfg, silence=True,
                hazard_id=job.risk_calculation.hazard_output.id)
            self.assertEqual(0, retcode)

            job = models.OqJob.objects.latest('id')

            outputs = export_core.get_outputs(job.id)
            # 1 loss curve set + 3 loss curve map set + 1 insured + 1 aggregate
            expected_outputs = 6
            self.assertEqual(expected_outputs, len(outputs))

            # Export the loss curves...
            curves = outputs.filter(output_type='loss_curve')
            rc_files = []
            for curve in curves:
                rc_files.extend(risk.export(curve.id, target_dir))

            self.assertEqual(1, len(rc_files))

            for f in rc_files:
                self._test_exported_file(f)

            # ... loss map ...
            maps = outputs.filter(output_type='loss_map')
            lm_files = sum(
                [risk.export(loss_map.id, target_dir)
                 for loss_map in maps], [])

            self.assertEqual(3, len(lm_files))

            for f in lm_files:
                self._test_exported_file(f)

            # ... aggregate losses...
            maps = outputs.filter(output_type='agg_loss_curve')
            lm_files = sum(
                [risk.export(loss_map.id, target_dir)
                 for loss_map in maps], [])

            self.assertEqual(1, len(lm_files))

            for f in lm_files:
                self._test_exported_file(f)

            # and insured losses.
            maps = outputs.filter(output_type='ins_loss_curve')
            lm_files = sum(
                [risk.export(loss_map.id, target_dir)
                 for loss_map in maps], [])

            self.assertEqual(1, len(lm_files))

            for f in lm_files:
                self._test_exported_file(f)

        finally:
            shutil.rmtree(target_dir)
示例#21
0
    def setUp(self):
        self.job, _ = helpers.get_risk_job(
            'classical_bcr/job.ini',
            'simple_fault_demo_hazard/job.ini')

        self.calculator = classical_bcr.ClassicalBCRRiskCalculator(self.job)
示例#22
0
 def setUp(self):
     job, _ = helpers.get_risk_job('classical_psha_based_risk/job.ini',
                                   'simple_fault_demo_hazard/job.ini')
     self.hazard_id = job.risk_calculation.hazard_output.id
示例#23
0
    def setUp(self):
        self.job, _ = helpers.get_risk_job(
            demo_file("classical_bcr/job.ini"), demo_file("simple_fault_demo_hazard/job.ini")
        )

        self.calculator = classical_bcr.ClassicalBCRRiskCalculator(self.job)