def run_calc(job, log_level, log_file, exports): """ Run a calculation. :param job: :class:`openquake.engine.db.model.OqJob` instance :param str log_level: The desired logging level. Valid choices are 'debug', 'info', 'progress', 'warn', 'error', and 'critical'. :param str log_file: Complete path (including file name) to file where logs will be written. If `None`, logging will just be printed to standard output. :param exports: A comma-separated string of export types. """ # let's import the calculator classes here, when they are needed # the reason is that the command `$ oq-engine --upgrade-db` # does not need them and would raise strange errors during installation # time if the PYTHONPATH is not set and commonlib is not visible from openquake.engine.calculators import calculators # first of all check the database version and exit if the db is outdated upgrader.check_versions(django_db.connections['admin']) calculator = calculators(job) with logs.handle(job, log_level, log_file), job_stats(job): # run the job _do_run_calc(calculator, exports) return calculator
def setUpClass(cls): cfg = helpers.get_data_path( 'calculators/hazard/classical/haz_map_test_job.ini') job = helpers.get_job(cfg) models.JobStats.objects.create(oq_job=job) cls.calc = calculators(job) cls.calc.initialize_site_collection() num_sites = len(cls.calc.site_collection) assert num_sites == 2, num_sites
def setUpClass(cls): cfg = helpers.get_data_path( 'calculators/hazard/classical/haz_map_test_job.ini') job = helpers.get_job(cfg) models.JobStats.objects.create(oq_job=job) cls.calc = calculators(job) cls.calc.initialize_site_collection() num_sites = len(cls.calc.site_collection) assert num_sites == 2, num_sites
def test_check_limits_event_based(self): # this is a based on a demo with 2 realizations, 5 ses, # 2 imt and 121 sites cfg = helpers.get_data_path('event_based_hazard/job.ini') job = helpers.get_job(cfg) models.JobStats.objects.create(oq_job=job) calc = calculators(job) input_weight, output_weight = calc.pre_execute() self.assertEqual(input_weight, 2705.5) self.assertAlmostEqual(output_weight, 1210.0)
def test_check_limits_event_based(self): # this is a based on a demo with 2 realizations, 5 ses, # 2 imt and 121 sites cfg = helpers.get_data_path( 'event_based_hazard/job.ini') job = helpers.get_job(cfg) models.JobStats.objects.create(oq_job=job) calc = calculators(job) input_weight, output_weight = calc.pre_execute() self.assertEqual(input_weight, 1352.75) self.assertAlmostEqual(output_weight, 1210.0)
def test(self): # check that if risk models are provided, then the sites # and the imls are got from there cfg = helpers.get_data_path('classical_job-sd-imt.ini') job = engine.job_from_file(cfg, helpers.default_user()) job.is_running = True job.save() calc = calculators(job) calc.parse_risk_model() self.assertEqual(['PGA'], list(calc.oqparam.imtls)) self.assertEqual(3, calc.job.exposuremodel.exposuredata_set.count()) return job
def test(self): # check that if risk models are provided, then the sites # and the imls are got from there cfg = helpers.get_data_path('classical_job-sd-imt.ini') job = engine.job_from_file(cfg, helpers.default_user()) job.is_running = True job.save() calc = calculators(job) calc.parse_risk_model() self.assertEqual(['PGA'], list(calc.oqparam.imtls)) self.assertEqual(3, calc.job.exposuremodel.exposuredata_set.count()) return job
def run_calc(job, log_level, log_file, exports, lite=False): """ Run a calculation. :param job: :class:`openquake.engine.db.model.OqJob` instance :param str log_level: The desired logging level. Valid choices are 'debug', 'info', 'progress', 'warn', 'error', and 'critical'. :param str log_file: Complete path (including file name) to file where logs will be written. If `None`, logging will just be printed to standard output. :param exports: A comma-separated string of export types. :param lite: Flag set when the oq-lite calculators are used """ # let's import the calculator classes here, when they are needed; # the reason is that the command `$ oq-engine --upgrade-db` # does not need them and would raise strange errors during installation # time if the PYTHONPATH is not set and commonlib is not visible if lite: calc_dir = os.path.join(datastore.DATADIR, 'calc_%d' % job.id) if os.path.exists(calc_dir): os.rename(calc_dir, calc_dir + '.bak') print 'Generated %s.bak' % calc_dir from openquake.commonlib.calculators import base calculator = base.calculators(job.get_oqparam(), calc_id=job.id) calculator.job = job calculator.monitor = EnginePerformanceMonitor('', job.id) else: from openquake.engine.calculators import calculators calculator = calculators(job) # first of all check the database version and exit if the db is outdated upgrader.check_versions(django_db.connections['admin']) with logs.handle(job, log_level, log_file), job_stats(job): # run the job try: _do_run_calc(calculator, exports) except: tb = traceback.format_exc() logs.LOG.critical(tb) raise return calculator
def test_check_limits_classical(self): # this is a based on a demo with 3 realizations, 2 sites and 4 rlzs cfg = helpers.get_data_path( 'calculators/hazard/classical/haz_map_test_job.ini') job = helpers.get_job(cfg) models.JobStats.objects.create(oq_job=job) calc = calculators(job) input_weight, output_weight = calc.pre_execute() self.assertEqual(input_weight, 225) self.assertEqual(output_weight, 24) calc.max_input_weight = 1 with self.assertRaises(general.InputWeightLimit): calc.check_limits(input_weight, output_weight) calc.max_input_weight = 1000 calc.max_output_weight = 1 with self.assertRaises(general.OutputWeightLimit): calc.check_limits(input_weight, output_weight)
def test_check_limits_classical(self): # this is a based on a demo with 3 realizations, 2 sites and 4 rlzs cfg = helpers.get_data_path( 'calculators/hazard/classical/haz_map_test_job.ini') job = helpers.get_job(cfg) models.JobStats.objects.create(oq_job=job) calc = calculators(job) input_weight, output_weight = calc.pre_execute() self.assertEqual(input_weight, 225) self.assertEqual(output_weight, 24) calc.max_input_weight = 1 with self.assertRaises(general.InputWeightLimit): calc.check_limits(input_weight, output_weight) calc.max_input_weight = 1000 calc.max_output_weight = 1 with self.assertRaises(general.OutputWeightLimit): calc.check_limits(input_weight, output_weight)
def run_calc(job, log_level, log_file, exports, lite=False): """ Run a calculation. :param job: :class:`openquake.engine.db.model.OqJob` instance :param str log_level: The desired logging level. Valid choices are 'debug', 'info', 'progress', 'warn', 'error', and 'critical'. :param str log_file: Complete path (including file name) to file where logs will be written. If `None`, logging will just be printed to standard output. :param exports: A comma-separated string of export types. :param lite: Flag set when the oq-lite calculators are used """ # let's import the calculator classes here, when they are needed # the reason is that the command `$ oq-engine --upgrade-db` # does not need them and would raise strange errors during installation # time if the PYTHONPATH is not set and commonlib is not visible if lite: from openquake.commonlib.calculators import base calculator = base.calculators(job.get_oqparam()) calculator.job = job calculator.monitor = EnginePerformanceMonitor('', job.id) else: from openquake.engine.calculators import calculators calculator = calculators(job) # first of all check the database version and exit if the db is outdated upgrader.check_versions(django_db.connections['admin']) with logs.handle(job, log_level, log_file), job_stats(job): # run the job try: _do_run_calc(calculator, exports) except: tb = traceback.format_exc() logs.LOG.critical(tb) raise return calculator