def test_export_for_scenario(self): target_dir = tempfile.mkdtemp() try: cfg = helpers.demo_file('scenario_hazard/job.ini') # run the calculation to create something to export retcode = helpers.run_hazard_job_sp(cfg, silence=True) self.assertEqual(0, retcode) job = models.OqJob.objects.latest('id') outputs = export_core.get_outputs(job.id) self.assertEqual(1, len(outputs)) # 1 GMF gmf_outputs = outputs.filter(output_type='gmf_scenario') self.assertEqual(1, len(gmf_outputs)) exported_files = hazard.export(gmf_outputs[0].id, target_dir) self.assertEqual(1, len(exported_files)) # Check the file paths exist, is absolute, and the file isn't # empty. f = exported_files[0] self._test_exported_file(f) # Check for the correct number of GMFs in the file: tree = etree.parse(f) self.assertEqual(10, number_of('nrml:gmf', tree)) finally: shutil.rmtree(target_dir)
def test(self): cfg = helpers.demo_file('simple_fault_demo_hazard/job.ini') retcode = helpers.run_hazard_job_sp( cfg, silence=True, force_inputs=False ) self.assertEqual(0, retcode) job = models.OqJob.objects.latest('id') job_stats = models.JobStats.objects.get(oq_job=job) self.assertEqual(236, job_stats.num_tasks) # As the bug description explains, run the same job a second time and # check the task count. It should not grow. retcode = helpers.run_hazard_job_sp( cfg, silence=True, force_inputs=False ) self.assertEqual(0, retcode) job = models.OqJob.objects.latest('id') job_stats = models.JobStats.objects.get(oq_job=job) self.assertEqual(236, job_stats.num_tasks)
def test(self): # The bug can be reproduced with any hazard calculation profile which # the following parameters set: # # * number_of_logic_tree_samples = 1 # * mean_hazard_curves = false # * quantile_hazard_curves = # * poes_hazard_maps = at least one PoE cfg = helpers.get_data_path( 'calculators/hazard/classical/haz_map_1rlz_no_stats.ini' ) retcode = helpers.run_hazard_job_sp(cfg, silence=True) self.assertEqual(0, retcode)
def test_classical_hazard_export(self): # Run a hazard calculation to compute some curves and maps # Call the exporter and verify that files were created # Since the hazard curve XML writer is concerned with correctly # generating XML, we won't test that here. target_dir = tempfile.mkdtemp() try: cfg = helpers.demo_file('simple_fault_demo_hazard/job.ini') # run the calculation to create something to export retcode = helpers.run_hazard_job_sp(cfg, silence=True) self.assertEqual(0, retcode) job = models.OqJob.objects.latest('id') outputs = export_core.get_outputs(job.id) expected_outputs = 18 # 6 hazard curves + 12 hazard maps self.assertEqual(expected_outputs, len(outputs)) # Export the hazard curves: curves = outputs.filter(output_type='hazard_curve') hc_files = [] for curve in curves: hc_files.extend(hazard.export(curve.id, target_dir)) self.assertEqual(6, len(hc_files)) for f in hc_files: self._test_exported_file(f) # Test hazard map export as well. maps = outputs.filter(output_type='hazard_map') hm_files = [] for haz_map in maps: hm_files.extend(hazard.export(haz_map.id, target_dir)) self.assertEqual(12, len(hm_files)) for f in hm_files: self._test_exported_file(f) finally: shutil.rmtree(target_dir)
def test_disagg_hazard_export(self): target_dir = tempfile.mkdtemp() try: cfg = helpers.demo_file('disaggregation/job.ini') retcode = helpers.run_hazard_job_sp(cfg, silence=True) self.assertEqual(0, retcode) job = models.OqJob.objects.latest('id') outputs = export_core.get_outputs(job.id) # Test curve export: curves = outputs.filter(output_type='hazard_curve') self.assertEqual(4, len(curves)) curve_files = [] for curve in curves: curve_files.extend(hazard.export(curve.id, target_dir)) self.assertEqual(4, len(curve_files)) for f in curve_files: self._test_exported_file(f) # Test disagg matrix export: matrices = outputs.filter(output_type='disagg_matrix') self.assertEqual(8, len(matrices)) disagg_files = [] for matrix in matrices: disagg_files.extend(hazard.export(matrix.id, target_dir)) self.assertEqual(8, len(disagg_files)) for f in disagg_files: self._test_exported_file(f) finally: shutil.rmtree(target_dir)
def setUpClass(cls): # Run a very small job to produce some sample GMF results, # which we can use for both test cases (gmf_set iter and complete logic # tree iter). cfg = helpers.get_data_path('db/models_test/event-based-job.ini') helpers.run_hazard_job_sp(cfg, silence=True)
def test_export_for_event_based(self): # Run an event-based hazard calculation to compute SESs and GMFs # Call the exporters for both SES and GMF results and verify that # files were created # Since the XML writers (in `openquake.nrmllib.writers`) are concerned # with correctly generating the XML, we don't test that here... # but we should still have an end-to-end QA test. target_dir = tempfile.mkdtemp() try: cfg = helpers.demo_file('event_based_hazard/job.ini') # run the calculation to create something to export retcode = helpers.run_hazard_job_sp(cfg, silence=True) self.assertEqual(0, retcode) job = models.OqJob.objects.latest('id') outputs = export_core.get_outputs(job.id) # 2 GMFs, 2 SESs, 1 complete logic tree SES, 1 complete LT GMF, # and 4 hazard curve collections self.assertEqual(18, len(outputs)) ####### # SESs: ses_outputs = outputs.filter(output_type='ses') self.assertEqual(2, len(ses_outputs)) exported_files = [] for ses_output in ses_outputs: files = hazard.export(ses_output.id, target_dir) exported_files.extend(files) self.assertEqual(2, len(exported_files)) for f in exported_files: self._test_exported_file(f) ################## # Complete LT SES: [complete_lt_ses] = outputs.filter(output_type='complete_lt_ses') [exported_file] = hazard.export(complete_lt_ses.id, target_dir) self._test_exported_file(exported_file) ####### # GMFs: gmf_outputs = outputs.filter(output_type='gmf') self.assertEqual(2, len(gmf_outputs)) exported_files = [] for gmf_output in gmf_outputs: files = hazard.export(gmf_output.id, target_dir) exported_files.extend(files) self.assertEqual(2, len(exported_files)) # Check the file paths exist, are absolute, and the files aren't # empty. for f in exported_files: self._test_exported_file(f) ################## # Complete LT GMF: [complete_lt_gmf] = outputs.filter(output_type='complete_lt_gmf') [exported_file] = hazard.export(complete_lt_gmf.id, target_dir) self._test_exported_file(exported_file) # Check for the correct number of GMFs in the file: tree = etree.parse(exported_file) self.assertEqual(442, number_of('nrml:gmf', tree)) ################ # Hazard curves: haz_curves = outputs.filter(output_type='hazard_curve') for curve in haz_curves: [exported_file] = hazard.export(curve.id, target_dir) self._test_exported_file(exported_file) finally: shutil.rmtree(target_dir)