def get_oqparam(job_ini, pkg=None, calculators=None): """ Parse a dictionary of parameters from one or more 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` :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.commonlib.calculators import base OqParam.calculation_mode.validator.choices = tuple( calculators or base.calculators) if isinstance(job_ini, dict): oqparam = OqParam(**job_ini) else: basedir = os.path.dirname(pkg.__file__) if pkg else '' inis = [os.path.join(basedir, ini) for ini in job_ini.split(',')] oqparam = OqParam(**get_params(inis)) oqparam.validate() return oqparam
def test_imts_and_imtls(self): oq = OqParam( calculation_mode='event_based', inputs={}, intensity_measure_types_and_levels="{'PGA': [0.1, 0.2]}", intensity_measure_types='PGV', sites='0.1 0.2', maximum_distance=400) oq.validate() self.assertEqual(list(oq.imtls.keys()), ['PGA'])
def test_missing_export_dir(self): oq = OqParam( calculation_mode='event_based', inputs={}, sites='0.1 0.2', intensity_measure_types='PGA', maximum_distance=400) oq.validate() self.assertEqual(oq.export_dir, os.path.expanduser('~'))
def test_missing_export_dir(self): oq = OqParam( calculation_mode='event_based', inputs=GST, sites='0.1 0.2', intensity_measure_types='PGA', reference_vs30_value='200', maximum_distance='400') oq.validate() self.assertEqual(oq.export_dir, os.getcwd())
def test_missing_export_dir(self): oq = OqParam(calculation_mode='event_based', inputs=GST, sites='0.1 0.2', intensity_measure_types='PGA', reference_vs30_value='200', maximum_distance='400') oq.validate() self.assertEqual(oq.export_dir, os.path.expanduser('~'))
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'])
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
def test_missing_levels_event_based(self): with self.assertRaises(ValueError) as ctx: oq = OqParam(calculation_mode='event_based', inputs=fakeinputs, sites='0.1 0.2', maximum_distance='400', intensity_measure_types='PGA', hazard_curves_from_gmfs='true') oq.validate() self.assertIn('`intensity_measure_types_and_levels`', str(ctx.exception))
def get_oqparam(job_ini, pkg=None, calculators=None, hc_id=None, validate=1, **kw): """ 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 hc_id: Not None only when called from a post calculation :param validate: Flag. By default it is true and the parameters are validated :param kw: String-valued keyword arguments used to override the job.ini parameters :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)) if hc_id: job_ini.update(hazard_calculation_id=str(hc_id)) job_ini.update(kw) oqparam = OqParam(**job_ini) if validate and '_job_id' not in job_ini: oqparam.check_source_model() oqparam.validate() return oqparam
def get_oqparam(job_ini, pkg=None, calculators=None, hc_id=None, validate=1, **kw): """ 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 hc_id: Not None only when called from a post calculation :param validate: Flag. By default it is true and the parameters are validated :param kw: String-valued keyword arguments used to override the job.ini parameters :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)]) if hc_id: job_ini.update(hazard_calculation_id=str(hc_id)) job_ini.update(kw) oqparam = OqParam(**job_ini) if validate: oqparam.validate() return oqparam