Ejemplo n.º 1
0
 def test_region_grid_spacing(self):
     # if there is a region there must be a region_grid_spacing
     with self.assertRaises(ValueError):
         OqParam(calculation_mode='classical_risk',
                 hazard_calculation_id=None,
                 hazard_output_id=None,
                 maximum_distance=10,
                 region='-78.182 15.615, -78.152 15.615, -78.152 15.565, '
                 '-78.182 15.565',
                 inputs=dict(site_model='')).validate()
Ejemplo n.º 2
0
 def test_site_model(self):
     # if the site_model_file is missing, reference_vs30_type and
     # the other site model parameters cannot be None
     with self.assertRaises(ValueError):
         OqParam(calculation_mode='classical_risk',
                 inputs={},
                 maximum_distance='10',
                 hazard_calculation_id=None,
                 hazard_output_id=None,
                 reference_vs30_type=None).validate()
Ejemplo n.º 3
0
 def test_ambiguous_gsim(self):
     with self.assertRaises(InvalidFile) as ctx:
         OqParam(
             calculation_mode='scenario', inputs=GST,
             gsim='AbrahamsonEtAl2014',
             sites='0.1 0.2',
             maximum_distance='400',
             intensity_measure_types='PGA',
         ).validate()
     self.assertIn('there must be no `gsim` key', str(ctx.exception))
Ejemplo n.º 4
0
 def test_imts_and_imtls(self):
     oq = OqParam(calculation_mode='event_based',
                  inputs=GST,
                  intensity_measure_types_and_levels="{'PGA': [0.1, 0.2]}",
                  intensity_measure_types='PGV',
                  sites='0.1 0.2',
                  reference_vs30_value='200',
                  maximum_distance='400')
     oq.validate()
     self.assertEqual(list(oq.imtls.keys()), ['PGA'])
Ejemplo n.º 5
0
 def test_missing_hazard_curves_from_gmfs(self):
     with self.assertRaises(ValueError) as ctx:
         OqParam(calculation_mode='event_based',
                 inputs={},
                 intensity_measure_types_and_levels="{'PGA': [0.1, 0.2]}",
                 mean_hazard_curves='true',
                 sites='0.1 0.2',
                 maximum_distance='400').validate()
     self.assertIn('You must set `hazard_curves_from_gmfs`',
                   str(ctx.exception))
Ejemplo n.º 6
0
 def test_missing_levels_hazard(self):
     with self.assertRaises(ValueError) as ctx:
         OqParam(
             calculation_mode='classical', inputs=fakeinputs,
             sites='0.1 0.2',
             maximum_distance='400',
             intensity_measure_types='PGA',
         ).validate()
     self.assertIn('`intensity_measure_types_and_levels`',
                   str(ctx.exception))
Ejemplo n.º 7
0
 def test_geometry(self):
     # you cannot have both region and sites
     with self.assertRaises(ValueError):
         OqParam(
             calculation_mode='classical_risk',
             hazard_calculation_id=None, hazard_output_id=None,
             maximum_distance='10',
             region='-78.182 15.615, -78.152 15.615, -78.152 15.565, '
             '-78.182 15.565', sites='0.1 0.2', inputs=dict(site_model='')
         ).validate()
Ejemplo n.º 8
0
 def test_create_export_dir(self):
     EDIR = os.path.join(TMP, 'nonexisting')
     OqParam(
         calculation_mode='event_based', inputs={},
         sites='0.1 0.2',
         intensity_measure_types='PGA',
         maximum_distance='400',
         export_dir=EDIR,
     ).validate()
     self.assertTrue(os.path.exists(EDIR))
Ejemplo n.º 9
0
def get_oqparam(job_ini, pkg=None, calculators=None, kw={}, validate=1):
    """
    Parse a dictionary of parameters from an INI-style config file.

    :param job_ini:
        Path to configuration file/archive or dictionary of parameters
    :param pkg:
        Python package where to find the configuration file (optional)
    :param calculators:
        Sequence of calculator names (optional) used to restrict the
        valid choices for `calculation_mode`
    :param kw:
        Dictionary of strings to override the job parameters
    :param validate:
        Flag. By default it is true and the parameters are validated
    :returns:
        An :class:`openquake.commonlib.oqvalidation.OqParam` instance
        containing the validate and casted parameters/values parsed from
        the job.ini file as well as a subdictionary 'inputs' containing
        absolute paths to all of the files referenced in the job.ini, keyed by
        the parameter name.
    """
    # UGLY: this is here to avoid circular imports
    from openquake.calculators import base

    OqParam.calculation_mode.validator.choices = tuple(
        calculators or base.calculators)
    if not isinstance(job_ini, dict):
        basedir = os.path.dirname(pkg.__file__) if pkg else ''
        job_ini = get_params(os.path.join(basedir, job_ini), kw)
    re = os.environ.get('OQ_REDUCE')  # debugging facility
    if re:
        # reduce the imtls to the first imt
        # reduce the logic tree to one random realization
        # reduce the sites by a factor of `re`
        # reduce the ses by a factor of `re`
        # set save_disk_space = true
        os.environ['OQ_SAMPLE_SITES'] = str(1 / float(re))
        job_ini['number_of_logic_tree_samples'] = '1'
        ses = job_ini.get('ses_per_logic_tree_path')
        if ses:
            ses = str(int(numpy.ceil(int(ses) / float(re))))
            job_ini['ses_per_logic_tree_path'] = ses
        imtls = job_ini.get('intensity_measure_types_and_levels')
        if imtls:
            imtls = valid.intensity_measure_types_and_levels(imtls)
            imt = next(iter(imtls))
            job_ini['intensity_measure_types_and_levels'] = repr(
                {imt: imtls[imt]})
        job_ini['save_disk_space'] = 'true'
    oqparam = OqParam(**job_ini)
    if validate and '_job_id' not in job_ini:
        oqparam.check_source_model()
        oqparam.validate()
    return oqparam
Ejemplo n.º 10
0
 def test_truncation_level_disaggregation(self):
     # for disaggregation, the truncation level cannot be None
     with self.assertRaises(InvalidFile):
         OqParam(calculation_mode='disaggregation',
                 hazard_calculation_id=None,
                 hazard_output_id=None,
                 inputs=fakeinputs,
                 maximum_distance='10',
                 sites='',
                 intensity_measure_types_and_levels="{'PGA': [0.1, 0.2]}",
                 truncation_level=None).validate()
Ejemplo n.º 11
0
 def test_invalid_export_dir(self):
     with self.assertRaises(ValueError) as ctx:
         OqParam(
             calculation_mode='event_based', inputs={},
             sites='0.1 0.2',
             maximum_distance='400',
             intensity_measure_types='PGA',
             export_dir='/non/existing',
         ).validate()
     self.assertIn('The `export_dir` parameter must refer to a '
                   'directory', str(ctx.exception))
Ejemplo n.º 12
0
 def test_optimize_same_id_sources(self):
     with self.assertRaises(ValueError) as ctx:
         OqParam(
             calculation_mode='event_based', inputs=fakeinputs,
             sites='0.1 0.2',
             maximum_distance='400',
             intensity_measure_types='PGA',
             optimize_same_id_sources='true',
         ).validate()
     self.assertIn('can be true only in the classical\ncalculators',
                   str(ctx.exception))
Ejemplo n.º 13
0
 def test_not_accepted_IMT(self):
     with self.assertRaises(ValueError) as ctx:
         OqParam(
             calculation_mode='scenario',
             gsim='ToroEtAl2002',
             sites='0.1 0.2',
             maximum_distance='400',
             intensity_measure_types='PGV',
         ).validate()
     self.assertIn('The IMT PGV is not accepted by the GSIM ToroEtAl2002',
                   str(ctx.exception))
Ejemplo n.º 14
0
 def test_missing_levels_event_based(self):
     with self.assertRaises(ValueError) as ctx:
         OqParam(
             calculation_mode='event_based', inputs={},
             sites='0.1 0.2',
             maximum_distance='400',
             intensity_measure_types='PGA',
             hazard_curves_from_gmfs='true',
         ).validate()
     self.assertIn('`intensity_measure_types_and_levels`',
                   str(ctx.exception))
Ejemplo n.º 15
0
 def test_required_site_param(self):
     with self.assertRaises(ValueError) as ctx:
         OqParam(
             calculation_mode='scenario',
             gsim='AbrahamsonSilva1997',
             sites='0.1 0.2',
             maximum_distance='400',
             intensity_measure_types='PGA',
         ).validate()
     self.assertIn("Please set a value for 'reference_vs30_value', this is"
                   " required by the GSIM AbrahamsonSilva1997",
                   str(ctx.exception))
Ejemplo n.º 16
0
 def test_poes(self):
     # if hazard_maps or uniform_hazard_spectra are set, poes
     # cannot be empty
     with self.assertRaises(ValueError):
         OqParam(calculation_mode='classical_risk',
                 hazard_calculation_id=None,
                 hazard_output_id=None,
                 inputs=fakeinputs,
                 maximum_distance='10',
                 sites='',
                 hazard_maps='true',
                 poes='').validate()
     with self.assertRaises(ValueError):
         OqParam(calculation_mode='classical_risk',
                 hazard_calculation_id=None,
                 hazard_output_id=None,
                 inputs=fakeinputs,
                 maximum_distance='10',
                 sites='',
                 uniform_hazard_spectra='true',
                 poes='').validate()
Ejemplo n.º 17
0
 def test_invalid_imt(self):
     with self.assertRaises(ValueError) as ctx:
         OqParam(
             calculation_mode='event_based',
             inputs=fakeinputs,
             sites='0.1 0.2',
             maximum_distance='400',
             ground_motion_correlation_model='JB2009',
             intensity_measure_types_and_levels='{"PGV": [0.4, 0.5, 0.6]}',
         ).validate()
     self.assertEqual(str(ctx.exception),
                      'Correlation model JB2009 does not accept IMT=PGV')
Ejemplo n.º 18
0
 def test_poes(self):
     # if hazard_maps or uniform_hazard_spectra are set, poes
     # cannot be empty
     with self.assertRaises(ValueError):
         OqParam(calculation_mode='classical',
                 hazard_calculation_id=None,
                 hazard_output_id=None,
                 inputs=dict(site_model=''),
                 maximum_distance=10,
                 sites='',
                 hazard_maps='true',
                 poes='')
     with self.assertRaises(ValueError):
         OqParam(calculation_mode='classical',
                 hazard_calculation_id=None,
                 hazard_output_id=None,
                 inputs=dict(site_model=''),
                 maximum_distance=10,
                 sites='',
                 uniform_hazard_spectra='true',
                 poes='')
Ejemplo n.º 19
0
 def test_disaggregation(self):
     with self.assertRaises(ValueError) as ctx:
         OqParam(
             calculation_mode='disaggregation',
             gsim='BooreAtkinson2008',
             reference_vs30_value='200',
             sites='0.1 0.2',
             poes='0.2',
             maximum_distance='400',
             intensity_measure_types_and_levels="{'PGV': [0.1, 0.2, 0.3]}",
             uniform_hazard_spectra='1')
     self.assertIn("poes_disagg or iml_disagg must be set",
                   str(ctx.exception))
Ejemplo n.º 20
0
 def test_create_export_dir(self):
     # FIXME: apparently this fails only when --with-doctest is set
     raise unittest.SkipTest
     EDIR = os.path.join(TMP, 'nonexisting')
     OqParam(
         calculation_mode='event_based',
         sites='0.1 0.2',
         reference_vs30_value='200',
         intensity_measure_types='PGA', inputs=GST,
         maximum_distance='400',
         export_dir=EDIR,
     ).validate()
     self.assertTrue(os.path.exists(EDIR))
Ejemplo n.º 21
0
 def test_unknown_parameter(self):
     # if the job.ini file contains an unknown parameter, print a warning
     with mock.patch('logging.warn') as w:
         OqParam(calculation_mode='classical',
                 inputs=dict(site_model=''),
                 hazard_calculation_id=None,
                 hazard_output_id=None,
                 maximum_distance=10,
                 sites='0.1 0.2',
                 not_existing_param='XXX')
     self.assertEqual(
         w.call_args[0][0],
         "The parameter 'not_existing_param' is unknown, ignoring")
Ejemplo n.º 22
0
 def test_ambiguous_gsim(self):
     with self.assertRaises(ValueError) as ctx:
         OqParam(
             calculation_mode='scenario',
             inputs={
                 'gsim_logic_tree': 'something'
             },
             gsim='AbrahamsonEtAl2014',
             sites='0.1 0.2',
             maximum_distance=400,
             intensity_measure_types='PGA',
         ).validate()
     self.assertIn('there must be no `gsim` key', str(ctx.exception))
Ejemplo n.º 23
0
 def test_required_site_param(self):
     with self.assertRaises(ValueError) as ctx:
         OqParam(
             calculation_mode='scenario',
             gsim='AbrahamsonSilva2008',
             sites='0.1 0.2',
             maximum_distance='400',
             intensity_measure_types='PGA',
             inputs=fakeinputs,
         ).validate()
     self.assertIn(
         "Please set a value for 'reference_depth_to_1pt0km_per_sec', this "
         "is required by the GSIM AbrahamsonSilva2008", str(ctx.exception))
Ejemplo n.º 24
0
 def test_unknown_parameter(self):
     # if the job.ini file contains an unknown parameter, print a warning
     with mock.patch('logging.warn') as w:
         OqParam(
             calculation_mode='classical', inputs=dict(site_model=''),
             hazard_calculation_id=None, hazard_output_id=None,
             maximum_distance='10', sites='0.1 0.2',
             not_existing_param='XXX', export_dir=TMP,
             intensity_measure_types_and_levels="{'PGA': [0.1, 0.2]}",
             rupture_mesh_spacing='1.5').validate()
     self.assertEqual(
         w.call_args[0][0],
         "The parameter 'not_existing_param' is unknown, ignoring")
Ejemplo n.º 25
0
    def test_missing_maximum_distance(self):
        with self.assertRaises(ValueError):
            OqParam(calculation_mode='classical_risk',
                    inputs=fakeinputs,
                    hazard_calculation_id=None,
                    hazard_output_id=None,
                    sites='0.1 0.2').validate()

        with self.assertRaises(ValueError):
            OqParam(calculation_mode='classical_risk',
                    inputs=fakeinputs,
                    hazard_calculation_id=None,
                    hazard_output_id=None,
                    sites='0.1 0.2',
                    maximum_distance='0').validate()

        oq = OqParam(calculation_mode='event_based',
                     inputs=GST,
                     intensity_measure_types_and_levels="{'PGA': [0.1, 0.2]}",
                     intensity_measure_types='PGV',
                     sites='0.1 0.2',
                     reference_vs30_value='200',
                     maximum_distance='{"wrong TRT": 200}')
        oq.inputs['source_model_logic_tree'] = 'something'

        oq._gsims_by_trt = {'Active Shallow Crust': []}
        self.assertFalse(oq.is_valid_maximum_distance())
        self.assertIn('setting the maximum_distance for wrong TRT', oq.error)

        oq._gsims_by_trt = {
            'Active Shallow Crust': [],
            'Stable Continental Crust': []
        }
        oq.maximum_distance = {'Active Shallow Crust': 200}
        self.assertFalse(oq.is_valid_maximum_distance())
        self.assertEqual(
            'missing distance for Stable Continental Crust '
            'and no default', oq.error)
Ejemplo n.º 26
0
 def test_event_based_risk(self):
     with self.assertRaises(InvalidFile) as ctx:
         OqParam(
             calculation_mode='event_based_risk',
             inputs=fakeinputs,
             gsim='BooreAtkinson2008',
             reference_vs30_value='200',
             sites='0.1 0.2',
             poes='0.2',
             maximum_distance='400',
             intensity_measure_types_and_levels="{'PGV': [0.1, 0.2, 0.3]}",
             conditional_loss_poes='0.02')
     self.assertIn("asset_loss_table is not set, probably you want to "
                   "remove conditional_loss_poes", str(ctx.exception))
Ejemplo n.º 27
0
 def test_invalid_export_dir(self):
     # FIXME: apparently this fails only when --with-doctest is set
     raise unittest.SkipTest
     with self.assertRaises(ValueError) as ctx:
         OqParam(
             calculation_mode='event_based', inputs=GST,
             sites='0.1 0.2',
             maximum_distance='400',
             reference_vs30_value='200',
             intensity_measure_types='PGA',
             export_dir='/non/existing',
         ).validate()
     self.assertIn('The `export_dir` parameter must refer to a '
                   'directory', str(ctx.exception))
Ejemplo n.º 28
0
 def test_uniform_hazard_spectra(self):
     with self.assertRaises(ValueError) as ctx:
         OqParam(
             calculation_mode='classical',
             gsim='BooreAtkinson2008',
             reference_vs30_value='200',
             sites='0.1 0.2',
             poes='0.2',
             maximum_distance='400',
             intensity_measure_types_and_levels="{'PGV': [0.1, 0.2, 0.3]}",
             uniform_hazard_spectra='1',
         ).set_risk_imtls({})
     self.assertIn(
         "The `uniform_hazard_spectra` can be True only if "
         "the IMT set contains SA(...) or PGA", str(ctx.exception))
Ejemplo n.º 29
0
def get_oq(text):
    """
    Returns an OqParam instance from a configuration string. For instance:

    >>> get_oq('maximum_distance=200')
    <OqParam calculation_mode='classical', collapse_level=0, inputs={'job_ini': '<in-memory>'}, maximum_distance={'default': 200}, risk_investigation_time=None>
    """
    # UGLY: this is here to avoid circular imports
    from openquake.calculators import base
    OqParam.calculation_mode.validator.choices = tuple(base.calculators)
    cp = configparser.ConfigParser()
    cp.read_string('[general]\ncalculation_mode=classical\n' + text)
    dic = dict(cp['general'])
    dic['inputs'] = dict(job_ini='<in-memory>')
    oq = OqParam(**dic)
    return oq
Ejemplo n.º 30
0
 def test_duplicated_levels(self):
     with self.assertRaises(ValueError) as ctx:
         OqParam(
             calculation_mode='classical', inputs={},
             sites='0.1 0.2',
             reference_vs30_type='measured',
             reference_vs30_value='200',
             reference_depth_to_2pt5km_per_sec='100',
             reference_depth_to_1pt0km_per_sec='150',
             maximum_distance='400',
             intensity_measure_types_and_levels='{"PGA": [0.4, 0.4, 0.6]}',
         ).validate()
     self.assertEqual(
         str(ctx.exception),
         'Found duplicated levels for PGA: [0.4, 0.4, 0.6]: could not '
         'convert to intensity_measure_types_and_levels: '
         'intensity_measure_types_and_levels={"PGA": [0.4, 0.4, 0.6]}')