def test(self): result_dir = tempfile.mkdtemp() try: cfg = os.path.join(os.path.dirname(__file__), "job.ini") expected_curve_poes_b1_b2 = [0.00995, 0.00076, 9.7e-5, 0.0] expected_curve_poes_b1_b3 = [0.00995, 0.00076, 0.000104, 0.0] job = self.run_hazard(cfg) # Test the poe values for the two curves: curve_b1_b2, curve_b1_b3 = models.HazardCurveData.objects.filter( hazard_curve__output__oq_job=job.id ).order_by("hazard_curve__lt_realization__sm_lt_path") # Sanity check, to make sure we have the curves ordered correctly: self.assertEqual(["b1", "b2"], curve_b1_b2.hazard_curve.lt_realization.sm_lt_path) self.assertEqual(["b1", "b3"], curve_b1_b3.hazard_curve.lt_realization.sm_lt_path) numpy.testing.assert_array_almost_equal(expected_curve_poes_b1_b2, curve_b1_b2.poes, decimal=4) numpy.testing.assert_array_almost_equal(expected_curve_poes_b1_b3, curve_b1_b3.poes, decimal=4) # Test the exports as well: [exported_file_b1_b2] = hazard_export.export(curve_b1_b2.hazard_curve.output.id, result_dir) self.assert_xml_equal(StringIO.StringIO(self.EXPECTED_XML_B1_B2), exported_file_b1_b2) [exported_file_b1_b3] = hazard_export.export(curve_b1_b3.hazard_curve.output.id, result_dir) self.assert_xml_equal(StringIO.StringIO(self.EXPECTED_XML_B1_B3), exported_file_b1_b3) finally: shutil.rmtree(result_dir)
def test(self): result_dir = tempfile.mkdtemp() try: cfg = os.path.join(os.path.dirname(__file__), 'job.ini') expected_curve_poes_b1 = [0.86466, 0.82460, 0.36525] expected_curve_poes_b2 = [0.63212, 0.61186, 0.25110] expected_mean_poes = [0.794898, 0.760778, 0.331005] job = self.run_hazard(cfg) # Test the poe values for the two curves. actual_curve_b1, actual_curve_b2 = \ models.HazardCurveData.objects\ .filter(hazard_curve__output__oq_job=job.id, hazard_curve__lt_realization__isnull=False)\ .order_by('hazard_curve__lt_realization__sm_lt_path') # Sanity check, to make sure we have the curves ordered correctly: self.assertEqual( ['b1'], actual_curve_b1.hazard_curve.lt_realization.sm_lt_path) self.assertEqual( ['b2'], actual_curve_b2.hazard_curve.lt_realization.sm_lt_path) numpy.testing.assert_array_almost_equal(expected_curve_poes_b1, actual_curve_b1.poes, decimal=3) numpy.testing.assert_array_almost_equal(expected_curve_poes_b2, actual_curve_b2.poes, decimal=3) # Test the mean curve: [mean_curve] = models.HazardCurveData.objects\ .filter(hazard_curve__output__oq_job=job.id, hazard_curve__statistics='mean') numpy.testing.assert_array_almost_equal(expected_mean_poes, mean_curve.poes, decimal=3) # Test the exports as well: exported_file_b1 = hazard_export.export( actual_curve_b1.hazard_curve.output.id, result_dir) self.assert_xml_equal(StringIO.StringIO(self.EXPECTED_XML_B1), exported_file_b1) exported_file_b2 = hazard_export.export( actual_curve_b2.hazard_curve.output.id, result_dir) self.assert_xml_equal(StringIO.StringIO(self.EXPECTED_XML_B2), exported_file_b2) # mean: exported_file_mean = hazard_export.export( mean_curve.hazard_curve.output.id, result_dir) self.assert_xml_equal(StringIO.StringIO(self.EXPECTED_XML_MEAN), exported_file_mean) finally: shutil.rmtree(result_dir)
def test(self): result_dir = tempfile.mkdtemp() try: cfg = os.path.join(os.path.dirname(__file__), 'job.ini') expected_curve_poes_b1 = [0.86466, 0.82460, 0.36525] expected_curve_poes_b2 = [0.63212, 0.61186, 0.25110] expected_mean_poes = [0.794898, 0.760778, 0.331005] job = self.run_hazard(cfg) # Test the poe values for the two curves. actual_curve_b1, actual_curve_b2 = ( models.HazardCurveData.objects .filter(hazard_curve__output__oq_job=job.id, hazard_curve__lt_realization__isnull=False) .order_by('hazard_curve__lt_realization__lt_model__sm_lt_path') ) # Sanity check, to make sure we have the curves ordered correctly: self.assertEqual( ['b1'], actual_curve_b1.hazard_curve.lt_realization.sm_lt_path) self.assertEqual( ['b2'], actual_curve_b2.hazard_curve.lt_realization.sm_lt_path) numpy.testing.assert_array_almost_equal( expected_curve_poes_b1, actual_curve_b1.poes, decimal=3) numpy.testing.assert_array_almost_equal( expected_curve_poes_b2, actual_curve_b2.poes, decimal=3) # Test the mean curve: [mean_curve] = models.HazardCurveData.objects\ .filter(hazard_curve__output__oq_job=job.id, hazard_curve__statistics='mean') numpy.testing.assert_array_almost_equal( expected_mean_poes, mean_curve.poes, decimal=3) # Test the exports as well: exported_file_b1 = hazard_export.export( actual_curve_b1.hazard_curve.output.id, result_dir) self.assert_xml_equal( StringIO.StringIO(self.EXPECTED_XML_B1), exported_file_b1) exported_file_b2 = hazard_export.export( actual_curve_b2.hazard_curve.output.id, result_dir) self.assert_xml_equal( StringIO.StringIO(self.EXPECTED_XML_B2), exported_file_b2) # mean: exported_file_mean = hazard_export.export( mean_curve.hazard_curve.output.id, result_dir) self.assert_xml_equal( StringIO.StringIO(self.EXPECTED_XML_MEAN), exported_file_mean) finally: shutil.rmtree(result_dir)
def test(self): result_dir = tempfile.mkdtemp() try: cfg = os.path.join(os.path.dirname(__file__), 'job.ini') expected_curve_poes_b1_b2 = [0.095163, 0.012362, 0.002262, 0.0] expected_curve_poes_b1_b3 = [0.009950, 0.00076, 9.99995E-6, 0.0] expected_curve_poes_b1_b4 = [0.0009995, 4.5489E-5, 4.07365E-6, 0.0] job = self.run_hazard(cfg) # Test the poe values for the three curves: curve_b1_b2, curve_b1_b3, curve_b1_b4 = \ models.HazardCurveData.objects\ .filter(hazard_curve__output__oq_job=job.id)\ .order_by('hazard_curve__lt_realization__sm_lt_path') # Sanity check, to make sure we have the curves ordered correctly: self.assertEqual( ['b1', 'b2'], curve_b1_b2.hazard_curve.lt_realization.sm_lt_path) self.assertEqual( ['b1', 'b3'], curve_b1_b3.hazard_curve.lt_realization.sm_lt_path) self.assertEqual( ['b1', 'b4'], curve_b1_b4.hazard_curve.lt_realization.sm_lt_path) numpy.testing.assert_array_almost_equal( expected_curve_poes_b1_b2, curve_b1_b2.poes, decimal=3) numpy.testing.assert_array_almost_equal( expected_curve_poes_b1_b3, curve_b1_b3.poes, decimal=3) numpy.testing.assert_array_almost_equal( expected_curve_poes_b1_b4, curve_b1_b4.poes, decimal=3) # Test the exports as well: [exported_file_b1_b2] = hazard_export.export( curve_b1_b2.hazard_curve.output.id, result_dir) self.assert_xml_equal( StringIO.StringIO(self.EXPECTED_XML_B1_B2), exported_file_b1_b2) [exported_file_b1_b3] = hazard_export.export( curve_b1_b3.hazard_curve.output.id, result_dir) self.assert_xml_equal( StringIO.StringIO(self.EXPECTED_XML_B1_B3), exported_file_b1_b3) [exported_file_b1_b4] = hazard_export.export( curve_b1_b4.hazard_curve.output.id, result_dir) self.assert_xml_equal( StringIO.StringIO(self.EXPECTED_XML_B1_B4), exported_file_b1_b4) finally: shutil.rmtree(result_dir)
def test(self): result_dir = tempfile.mkdtemp() try: cfg = os.path.join(os.path.dirname(__file__), 'job.ini') expected_curve_poes_b1_b2 = [0.095163, 0.012362, 0.002262, 0.0] expected_curve_poes_b1_b3 = [0.009950, 0.00076, 9.99995E-6, 0.0] expected_curve_poes_b1_b4 = [0.0009995, 4.5489E-5, 4.07365E-6, 0.0] job = self.run_hazard(cfg) # Test the poe values for the three curves: curve_b1_b2, curve_b1_b3, curve_b1_b4 = \ models.HazardCurveData.objects\ .filter(hazard_curve__output__oq_job=job.id)\ .order_by('hazard_curve__lt_realization__sm_lt_path') # Sanity check, to make sure we have the curves ordered correctly: self.assertEqual( ['b1', 'b2'], curve_b1_b2.hazard_curve.lt_realization.sm_lt_path) self.assertEqual( ['b1', 'b3'], curve_b1_b3.hazard_curve.lt_realization.sm_lt_path) self.assertEqual( ['b1', 'b4'], curve_b1_b4.hazard_curve.lt_realization.sm_lt_path) numpy.testing.assert_array_almost_equal( expected_curve_poes_b1_b2, curve_b1_b2.poes, decimal=3) numpy.testing.assert_array_almost_equal( expected_curve_poes_b1_b3, curve_b1_b3.poes, decimal=3) numpy.testing.assert_array_almost_equal( expected_curve_poes_b1_b4, curve_b1_b4.poes, decimal=3) # Test the exports as well: exported_file_b1_b2 = hazard_export.export( curve_b1_b2.hazard_curve.output.id, result_dir) self.assert_xml_equal( StringIO.StringIO(self.EXPECTED_XML_B1_B2), exported_file_b1_b2) exported_file_b1_b3 = hazard_export.export( curve_b1_b3.hazard_curve.output.id, result_dir) self.assert_xml_equal( StringIO.StringIO(self.EXPECTED_XML_B1_B3), exported_file_b1_b3) exported_file_b1_b4 = hazard_export.export( curve_b1_b4.hazard_curve.output.id, result_dir) self.assert_xml_equal( StringIO.StringIO(self.EXPECTED_XML_B1_B4), exported_file_b1_b4) finally: shutil.rmtree(result_dir)
def export(self, *args, **kwargs): """ If requested by the user, automatically export all result artifacts to the specified format. (NOTE: The only export format supported at the moment is NRML XML. :returns: A list of the export filenames, including the absolute path to each file. """ exported_files = [] logs.LOG.debug('> starting exports') if 'exports' in kwargs and 'xml' in kwargs['exports']: outputs = export_core.get_outputs(self.job.id) if not self.hc.export_multi_curves: outputs = outputs.exclude(output_type='hazard_curve_multi') for output in outputs: with self.monitor('exporting %s' % output.output_type): fname = hazard_export.export( output.id, self.job.hazard_calculation.export_dir) exported_files.extend(fname) logs.LOG.debug('exported %s' % fname) logs.LOG.debug('< done with exports') return exported_files
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 export(self, *args, **kwargs): """ If requested by the user, automatically export all result artifacts to the specified format. (NOTE: The only export format supported at the moment is NRML XML. :returns: A list of the export filenames, including the absolute path to each file. """ exported_files = [] logs.LOG.debug('> starting exports') if 'exports' in kwargs and 'xml' in kwargs['exports']: outputs = export_core.get_outputs(self.job.id) for output in outputs: exported_files.extend(hazard_export.export( output.id, self.job.hazard_calculation.export_dir)) for exp_file in exported_files: logs.LOG.debug('exported %s' % exp_file) logs.LOG.debug('< done with exports') return exported_files
def check_export(output_id, target): """ Call hazard.export by checking that the exported file is valid according to our XML schema. """ out_file = hazard.export(output_id, target, 'xml') nrmllib.assert_valid(out_file) return out_file
def _do_export(self, output_id, export_dir, export_type): """ Hazard-specific implementation of :meth:`openquake.engine.calculators.base.Calculator._do_export`. Calls the hazard exporter. """ return hazard_export.export(output_id, export_dir, export_type)
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(self): result_dir = tempfile.mkdtemp() try: cfg = os.path.join(os.path.dirname(__file__), 'job.ini') expected_curve_pga = [0.4570, 0.0587, 0.0069] expected_curve_sa = [ 0.608675003748, 0.330831513139, 0.201472214825 ] job = self.run_hazard(cfg) # Test the poe values of the single curve: curves = models.HazardCurveData.objects.filter( hazard_curve__output__oq_job=job.id ) [pga_curve] = curves.filter(hazard_curve__imt='PGA') numpy.testing.assert_array_almost_equal( expected_curve_pga, pga_curve.poes, decimal=4 ) [sa_curve] = curves.filter( hazard_curve__imt='SA', hazard_curve__sa_period=0.1 ) numpy.testing.assert_array_almost_equal( expected_curve_sa, sa_curve.poes, decimal=4 ) # Test the exports as well: exported_file = hazard_export.export( pga_curve.hazard_curve.output.id, result_dir) self.assert_xml_equal( StringIO.StringIO(self.EXPECTED_PGA_XML), exported_file) exported_file = hazard_export.export( sa_curve.hazard_curve.output.id, result_dir) self.assert_xml_equal( StringIO.StringIO(self.EXPECTED_SA_XML), exported_file) finally: shutil.rmtree(result_dir)
def test(self): result_dir = tempfile.mkdtemp() try: cfg = os.path.join(os.path.dirname(__file__), 'job.ini') expected_curve_pga = [0.4570, 0.0587, 0.0069] expected_curve_sa = [ 0.608675003748, 0.330831513139, 0.201472214825 ] job = self.run_hazard(cfg) # Test the poe values of the single curve: curves = models.HazardCurveData.objects.filter( hazard_curve__output__oq_job=job.id ) [pga_curve] = curves.filter(hazard_curve__imt='PGA') numpy.testing.assert_array_almost_equal( expected_curve_pga, pga_curve.poes, decimal=4 ) [sa_curve] = curves.filter( hazard_curve__imt='SA', hazard_curve__sa_period=0.1 ) numpy.testing.assert_array_almost_equal( expected_curve_sa, sa_curve.poes, decimal=4 ) # Test the exports as well: [exported_file] = hazard_export.export( pga_curve.hazard_curve.output.id, result_dir) self.assert_xml_equal( StringIO.StringIO(self.EXPECTED_PGA_XML), exported_file) [exported_file] = hazard_export.export( sa_curve.hazard_curve.output.id, result_dir) self.assert_xml_equal( StringIO.StringIO(self.EXPECTED_SA_XML), exported_file) finally: shutil.rmtree(result_dir)
def test(self): aaae = numpy.testing.assert_array_almost_equal # TODO(LB): This is a temporary test case which tests for stability # until we can write proper QA tests. cfg = os.path.join(os.path.dirname(__file__), 'job.ini') job = self.run_hazard(cfg) results = models.DisaggResult.objects.filter(output__oq_job=job) poe_002_pga = results.filter(imt='PGA', poe=0.02) rlz1, rlz2 = poe_002_pga.order_by('lt_realization') aaae(test_data.RLZ_1_POE_002_PGA, rlz1.matrix) aaae(test_data.RLZ_2_POE_002_PGA, rlz2.matrix) poe_002_sa = results.filter(imt='SA', poe=0.02) rlz1, rlz2 = poe_002_sa.order_by('lt_realization') aaae(test_data.RLZ_1_POE_002_SA, rlz1.matrix) aaae(test_data.RLZ_2_POE_002_SA, rlz2.matrix) poe_01_pga = results.filter(imt='PGA', poe=0.1) rlz1, rlz2 = poe_01_pga.order_by('lt_realization') aaae(test_data.RLZ_1_POE_01_PGA, rlz1.matrix) aaae(test_data.RLZ_2_POE_01_PGA, rlz2.matrix) poe_01_sa = results.filter(imt='SA', poe=0.1) rlz1, rlz2 = poe_01_sa.order_by('lt_realization') aaae(test_data.RLZ_1_POE_01_SA, rlz1.matrix) aaae(test_data.RLZ_2_POE_01_SA, rlz2.matrix) # Lastly, we should an export of at least one of these results to # ensure that the disagg export/serialization is working properly. # The export isn't just a simple dump from the database; it requires # extraction of PMFs (Probability Mass Function) from a 6d matrix, # which are then serialized to XML. # This is not a trivial operation. try: target_dir = tempfile.mkdtemp() [result_file] = haz_export.export(rlz1.output.id, target_dir) expected = StringIO.StringIO(test_data.EXPECTED_XML_DISAGG) self.assert_xml_equal(expected, result_file) self.assertTrue(qa_utils.validates_against_xml_schema(result_file)) 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 test(self): # TODO(LB): This is a temporary test case which tests for stability # until we can write proper QA tests. cfg = os.path.join(os.path.dirname(__file__), 'job.ini') job = self.run_hazard(cfg) results = models.DisaggResult.objects.filter(output__oq_job=job) poe_002_pga = results.filter(imt='PGA', poe=0.02) rlz1, rlz2 = poe_002_pga.order_by('lt_realization') aaae(test_data.RLZ_1_POE_002_PGA, rlz1.matrix) aaae(test_data.RLZ_2_POE_002_PGA, rlz2.matrix) poe_002_sa = results.filter(imt='SA', poe=0.02) rlz1, rlz2 = poe_002_sa.order_by('lt_realization') aaae(test_data.RLZ_1_POE_002_SA, rlz1.matrix) aaae(test_data.RLZ_2_POE_002_SA, rlz2.matrix) poe_01_pga = results.filter(imt='PGA', poe=0.1) rlz1, rlz2 = poe_01_pga.order_by('lt_realization') aaae(test_data.RLZ_1_POE_01_PGA, rlz1.matrix) aaae(test_data.RLZ_2_POE_01_PGA, rlz2.matrix) poe_01_sa = results.filter(imt='SA', poe=0.1) rlz1, rlz2 = poe_01_sa.order_by('lt_realization') aaae(test_data.RLZ_1_POE_01_SA, rlz1.matrix) aaae(test_data.RLZ_2_POE_01_SA, rlz2.matrix) # Lastly, we should check an export of at least one of these results to # ensure that the disagg export/serialization is working properly. # The export isn't just a simple dump from the database; it requires # extraction of PMFs (Probability Mass Function) from a 6d matrix, # which are then serialized to XML. # This is not a trivial operation. try: target_dir = tempfile.mkdtemp() result_file = haz_export.export(rlz1.output.id, target_dir) expected = StringIO.StringIO(test_data.EXPECTED_XML_DISAGG) self.assert_disagg_xml_almost_equal(expected, result_file) self.assertTrue(qa_utils.validates_against_xml_schema(result_file)) finally: shutil.rmtree(target_dir)
def test(self): result_dir = tempfile.mkdtemp() try: cfg = os.path.join(os.path.dirname(__file__), "job.ini") expected_curve_poes = [0.86466, 0.82460, 0.36525] job = self.run_hazard(cfg) # Test the poe values of the single curve: [actual_curve] = models.HazardCurveData.objects.filter(hazard_curve__output__oq_job=job.id) numpy.testing.assert_array_almost_equal(expected_curve_poes, actual_curve.poes, decimal=2) # Test the export as well: [exported_file] = hazard_export.export(actual_curve.hazard_curve.output.id, result_dir) self.assert_xml_equal(StringIO.StringIO(self.EXPECTED_XML), exported_file) finally: shutil.rmtree(result_dir)
def test(self): result_dir = tempfile.mkdtemp() aaae = numpy.testing.assert_array_almost_equal try: cfg = os.path.join(os.path.dirname(__file__), "job.ini") expected_curve_poes = [0.75421006, 0.08098179, 0.00686616] job = self.run_hazard(cfg) # Test the poe values of the single curve: [curve] = models.HazardCurveData.objects.filter(hazard_curve__output__oq_job=job.id) aaae(expected_curve_poes, curve.poes, decimal=2) # Test the exports as well: exported_file = hazard_export.export(curve.hazard_curve.output.id, result_dir) self.assert_xml_equal(StringIO.StringIO(self.EXPECTED_XML), exported_file) finally: shutil.rmtree(result_dir)
def test(self): result_dir = tempfile.mkdtemp() try: cfg = os.path.join(os.path.dirname(__file__), 'job.ini') expected_curve_poes = [0.63212, 0.61186, 0.25110] job = self.run_hazard(cfg) # Test the poe values of the single curve: [actual_curve] = models.HazardCurveData.objects.filter( hazard_curve__output__oq_job=job.id) numpy.testing.assert_array_almost_equal( expected_curve_poes, actual_curve.poes, decimal=3) # Test the export as well: exported_file = hazard_export.export( actual_curve.hazard_curve.output.id, result_dir) self.assert_xml_equal( StringIO.StringIO(self.EXPECTED_XML), exported_file) finally: shutil.rmtree(result_dir)
def test(self): result_dir = tempfile.mkdtemp() aaae = numpy.testing.assert_array_almost_equal try: cfg = os.path.join(os.path.dirname(__file__), 'job.ini') expected_curve_poes = [0.75421006, 0.08098179, 0.00686616] job = self.run_hazard(cfg) # Test the poe values of the single curve: [curve] = models.HazardCurveData.objects.filter( hazard_curve__output__oq_job=job.id) aaae(expected_curve_poes, curve.poes, decimal=2) # Test the exports as well: exported_file = hazard_export.export( curve.hazard_curve.output.id, result_dir) self.assert_xml_equal( StringIO.StringIO(self.EXPECTED_XML), exported_file) finally: shutil.rmtree(result_dir)
def test(self): result_dir = tempfile.mkdtemp() aaae = numpy.testing.assert_array_almost_equal try: cfg = os.path.join(os.path.dirname(__file__), 'job.ini') expected_curve_poes_b1_b2 = [0.0055, 0.00042, 5.77E-5, 0.0] expected_curve_poes_b1_b3 = [0.00995, 0.00076, 9.7E-5, 0.0] expected_curve_poes_b1_b4 = [0.018, 0.0013, 0.00014, 0.0] expected_mean_poes = [0.01067, 0.0008, 9.774E-5, 0.0] expected_q0_1_poes = [0.0055, 0.00042, 5.77E-5, 0.0] expected_q0_9_poes = [0.013975, 0.00103, 0.0001185, 0.0] job = self.run_hazard(cfg) # Test the poe values for the two curves: curve_b1_b2, curve_b1_b3, curve_b1_b4 = \ models.HazardCurveData.objects\ .filter(hazard_curve__output__oq_job=job.id, hazard_curve__lt_realization__isnull=False)\ .order_by('hazard_curve__lt_realization__sm_lt_path') # Sanity check, to make sure we have the curves ordered correctly: self.assertEqual( ['b1', 'b2'], curve_b1_b2.hazard_curve.lt_realization.sm_lt_path) self.assertEqual( ['b1', 'b3'], curve_b1_b3.hazard_curve.lt_realization.sm_lt_path) self.assertEqual( ['b1', 'b4'], curve_b1_b4.hazard_curve.lt_realization.sm_lt_path) aaae(expected_curve_poes_b1_b2, curve_b1_b2.poes, decimal=4) aaae(expected_curve_poes_b1_b3, curve_b1_b3.poes, decimal=4) aaae(expected_curve_poes_b1_b4, curve_b1_b4.poes, decimal=4) # Test the mean curve: [mean_curve] = models.HazardCurveData.objects\ .filter(hazard_curve__output__oq_job=job.id, hazard_curve__statistics='mean') aaae(expected_mean_poes, mean_curve.poes, decimal=4) # Test the quantile curves: quantile_0_1_curve, quantile_0_9_curve = \ models.HazardCurveData.objects\ .filter(hazard_curve__output__oq_job=job.id, hazard_curve__statistics='quantile')\ .order_by('hazard_curve__quantile') aaae(expected_q0_1_poes, quantile_0_1_curve.poes, decimal=4) aaae(expected_q0_9_poes, quantile_0_9_curve.poes, decimal=4) # Test the exports as well: exported_file_b1_b2 = hazard_export.export( curve_b1_b2.hazard_curve.output.id, result_dir) self.assert_xml_equal(StringIO.StringIO(self.EXPECTED_XML_B1_B2), exported_file_b1_b2) exported_file_b1_b3 = hazard_export.export( curve_b1_b3.hazard_curve.output.id, result_dir) self.assert_xml_equal(StringIO.StringIO(self.EXPECTED_XML_B1_B3), exported_file_b1_b3) exported_file_b1_b4 = hazard_export.export( curve_b1_b4.hazard_curve.output.id, result_dir) self.assert_xml_equal(StringIO.StringIO(self.EXPECTED_XML_B1_B4), exported_file_b1_b4) exported_file_mean = hazard_export.export( mean_curve.hazard_curve.output.id, result_dir) self.assert_xml_equal(StringIO.StringIO(self.EXPECTED_XML_MEAN), exported_file_mean) q01_file = hazard_export.export( quantile_0_1_curve.hazard_curve.output.id, result_dir) self.assert_xml_equal( StringIO.StringIO(self.EXPECTED_XML_QUANTILE_0_1), q01_file) q09_file = hazard_export.export( quantile_0_9_curve.hazard_curve.output.id, result_dir) self.assert_xml_equal( StringIO.StringIO(self.EXPECTED_XML_QUANTILE_0_9), q09_file) finally: shutil.rmtree(result_dir)
def check_export(output_id, target_dir): """ Call hazard.export by checking that the exported file is valid according to our XML schema. """ return hazard.export(output_id, target_dir, check_schema=True)
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.get_data_path('simple_fault_demo_hazard/job.ini') # run the calculation to create something to export helpers.run_hazard_job(cfg) job = models.OqJob.objects.latest('id') self.assertEqual(job.status, 'complete') outputs = export_core.get_outputs(job.id) # 10 hazard curves, 20 maps, 10 uhs, 5 multi curves expected_outputs = 45 self.assertEqual(expected_outputs, outputs.count()) # Number of curves: # (2 imts * 2 realizations) # + (2 imts * (1 mean + 2 quantiles) # = 10 curves = outputs.filter(output_type='hazard_curve') self.assertEqual(10, curves.count()) # Number of multi-curves # (2 realizations + 1 mean + 2 quantiles) multi_curves = outputs.filter(output_type="hazard_curve_multi") self.assertEqual(5, multi_curves.count()) # Number of maps: # (2 poes * 2 imts * 2 realizations) # + (2 poes * 2 imts * (1 mean + 2 quantiles)) # = 20 # Number of UHS: maps = outputs.filter(output_type='hazard_map') self.assertEqual(20, maps.count()) # Number of UHS: # (20 maps_PGA_SA / 2 poes) # = 10 uhs = outputs.filter(output_type='uh_spectra') self.assertEqual(10, uhs.count()) # Test hazard curve export: hc_files = [] for curve in curves: hc_files.extend(check_export(curve.id, target_dir)) self.assertEqual(10, len(hc_files)) # Test multi hazard curve export: hc_files = [] for curve in multi_curves: hc_files.extend(hazard.export(curve.id, target_dir)) self.assertEqual(5, len(hc_files)) for f in hc_files: self._test_exported_file(f) # Test hazard map export: hm_files = [] for haz_map in maps: hm_files.extend(check_export(haz_map.id, target_dir)) self.assertEqual(20, len(hm_files)) for f in hm_files: self._test_exported_file(f) # Test UHS export: uhs_files = [] for u in uhs: uhs_files.extend(check_export(u.id, target_dir)) for f in uhs_files: self._test_exported_file(f) finally: shutil.rmtree(target_dir)
def test(self): result_dir = tempfile.mkdtemp() aaae = numpy.testing.assert_array_almost_equal try: cfg = os.path.join(os.path.dirname(__file__), 'job.ini') expected_curve_poes_b1_b2 = [0.0055, 0.00042, 5.77E-5, 0.0] expected_curve_poes_b1_b3 = [0.00995, 0.00076, 9.7E-5, 0.0] expected_curve_poes_b1_b4 = [0.018, 0.0013, 0.00014, 0.0] expected_mean_poes = [0.01067, 0.0008, 9.774E-5, 0.0] expected_q0_1_poes = [0.0055, 0.00042, 5.77E-5, 0.0] expected_q0_9_poes = [0.013975, 0.00103, 0.0001185, 0.0] job = self.run_hazard(cfg) # Test the poe values for the two curves: curve_b1_b2, curve_b1_b3, curve_b1_b4 = \ models.HazardCurveData.objects\ .filter(hazard_curve__output__oq_job=job.id, hazard_curve__lt_realization__isnull=False)\ .order_by('hazard_curve__lt_realization__sm_lt_path') # Sanity check, to make sure we have the curves ordered correctly: self.assertEqual( ['b1', 'b2'], curve_b1_b2.hazard_curve.lt_realization.sm_lt_path) self.assertEqual( ['b1', 'b3'], curve_b1_b3.hazard_curve.lt_realization.sm_lt_path) self.assertEqual( ['b1', 'b4'], curve_b1_b4.hazard_curve.lt_realization.sm_lt_path) aaae(expected_curve_poes_b1_b2, curve_b1_b2.poes, decimal=4) aaae(expected_curve_poes_b1_b3, curve_b1_b3.poes, decimal=4) aaae(expected_curve_poes_b1_b4, curve_b1_b4.poes, decimal=4) # Test the mean curve: [mean_curve] = models.HazardCurveData.objects\ .filter(hazard_curve__output__oq_job=job.id, hazard_curve__statistics='mean') aaae(expected_mean_poes, mean_curve.poes, decimal=4) # Test the quantile curves: quantile_0_1_curve, quantile_0_9_curve = \ models.HazardCurveData.objects\ .filter(hazard_curve__output__oq_job=job.id, hazard_curve__statistics='quantile')\ .order_by('hazard_curve__quantile') aaae(expected_q0_1_poes, quantile_0_1_curve.poes, decimal=4) aaae(expected_q0_9_poes, quantile_0_9_curve.poes, decimal=4) # Test the exports as well: [exported_file_b1_b2] = hazard_export.export( curve_b1_b2.hazard_curve.output.id, result_dir) self.assert_xml_equal( StringIO.StringIO(self.EXPECTED_XML_B1_B2), exported_file_b1_b2) [exported_file_b1_b3] = hazard_export.export( curve_b1_b3.hazard_curve.output.id, result_dir) self.assert_xml_equal( StringIO.StringIO(self.EXPECTED_XML_B1_B3), exported_file_b1_b3) [exported_file_b1_b4] = hazard_export.export( curve_b1_b4.hazard_curve.output.id, result_dir) self.assert_xml_equal( StringIO.StringIO(self.EXPECTED_XML_B1_B4), exported_file_b1_b4) [exported_file_mean] = hazard_export.export( mean_curve.hazard_curve.output.id, result_dir) self.assert_xml_equal( StringIO.StringIO(self.EXPECTED_XML_MEAN), exported_file_mean) [q01_file] = hazard_export.export( quantile_0_1_curve.hazard_curve.output.id, result_dir) self.assert_xml_equal( StringIO.StringIO(self.EXPECTED_XML_QUANTILE_0_1), q01_file) [q09_file] = hazard_export.export( quantile_0_9_curve.hazard_curve.output.id, result_dir) self.assert_xml_equal( StringIO.StringIO(self.EXPECTED_XML_QUANTILE_0_9), q09_file) finally: shutil.rmtree(result_dir)
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)