def test_serialize_taxonomies(self): expected = file( os.path.join(os.path.dirname(__file__), "../../../../examples/loss-fractions-taxonomies.xml")) writers.LossFractionsWriter( self.filename, "taxonomy", loss_unit="EUR", loss_category="building", hazard_metadata=HazardMetadata(investigation_time=50., statistics=None, quantile=None, sm_path="b1_b2_b4", gsim_path="b1_b2"), poe=0.1, loss_type="structural").serialize( dict(RC=(400, 0.2), RM=(1600, 0.8)), { (0., 0.): dict(RC=(200, 0.5), RM=(200, 0.5)), (1., 1.): dict(RC=(200, 0.25), RM=(1400, 0.75)) }) etree.XMLSchema(etree.parse(nrmllib.nrml_schema_file())).assert_( etree.parse(self.filename)) _utils.assert_xml_equal(expected, self.filename)
def test_serialize_taxonomies_from_statistics(self): writers.LossFractionsWriter( self.filename, "taxonomy", loss_unit="EUR", loss_category="building", hazard_metadata=HazardMetadata( investigation_time=50.0, statistics="quantile", quantile=0.3, sm_path=None, gsim_path=None ), poe=None, ).serialize( dict(RC=(400, 0.2), RM=(1600, 0.8)), {(0.0, 0.0): dict(RC=(200, 0.5), RM=(200, 0.5)), (1.0, 1.0): dict(RC=(200, 0.25), RM=(1400, 0.75))}, ) etree.XMLSchema(etree.parse(nrmllib.nrml_schema_file())).assert_(etree.parse(self.filename))
def test_serialize_taxonomies_from_statistics(self): writers.LossFractionsWriter( self.filename, "taxonomy", loss_unit="EUR", loss_category="building", hazard_metadata=HazardMetadata( investigation_time=50., statistics="quantile", quantile=0.3, sm_path=None, gsim_path=None), poe=None).serialize( dict(RC=(400, 0.2), RM=(1600, 0.8)), {(0., 0.): dict(RC=(200, 0.5), RM=(200, 0.5)), (1., 1.): dict(RC=(200, 0.25), RM=(1400, 0.75))}) etree.XMLSchema( etree.parse(nrmllib.nrml_schema_file())).assert_( etree.parse(self.filename))
def test_serialize_taxonomies(self): expected = file(os.path.join(os.path.dirname(__file__), "../../examples/loss-fractions-taxonomies.xml")) writers.LossFractionsWriter( self.filename, "taxonomy", loss_unit="EUR", loss_category="building", hazard_metadata=HazardMetadata( investigation_time=50.0, statistics=None, quantile=None, sm_path="b1_b2_b4", gsim_path="b1_b2" ), poe=0.1, ).serialize( dict(RC=(400, 0.2), RM=(1600, 0.8)), {(0.0, 0.0): dict(RC=(200, 0.5), RM=(200, 0.5)), (1.0, 1.0): dict(RC=(200, 0.25), RM=(1400, 0.75))}, ) etree.XMLSchema(etree.parse(nrmllib.nrml_schema_file())).assert_(etree.parse(self.filename)) _utils.assert_xml_equal(expected, self.filename)
def read_file(self, fault_mesh_spacing=1.0, validation=False): """ Parse the source XML content and generate a source model in object form. :param float fault_mesh_spacing: Mesh spacing to use for fault sources (km) :param bool validation: Option to validate against nrml 0.4 schema - should only be set to true when inputting a fully defined source model :returns: :class:`hmtk.sources.source_model.SourceModel` instance. """ self.mesh_spacing = fault_mesh_spacing src_model = mtkSourceModel() if validation: # Validate against nrml schema schema = etree.XMLSchema(etree.parse(nrml.nrml_schema_file())) tree = etree.iterparse(self.source, events=('start', 'end'), schema=schema) else: tree = etree.iterparse(self.source, events=('start', 'end')) for event, element in tree: # Find the <sourceModel> element and get the 'name' attr. if event == 'start': if element.tag == self._SM_TAG: src_model.name = element.get('name') break else: # If we get to here, we didn't find the <sourceModel> element. raise ValueError('<sourceModel> element not found.') src_model.sources = self._source_gen(tree) src_model.sources = list(src_model.sources) return src_model