def build_xml(): test_string = """<Event magnitude_type='Mw'> <polygon> <boundary> -20 120 -30 120 -20 120 </boundary> <depth distribution = "constant" mean = "10" ></depth> </polygon> <polygon> <boundary> -30 121 -35 122 -30 135 -30 121 </boundary> <exclude> -32 121 -34 122 -32 121 </exclude> <exclude> -35 121 -36 122 -35 121 </exclude> <depth distribution = "constant" mean = "5" ></depth> </polygon> </Event> """ xml = Xml_Interface(string=test_string) return xml
def polygons_from_xml(filename): doc = Xml_Interface(filename=filename) if not doc['Source_Model'] == []: raise Exception('zone source file format incorrect.') else: generation_polygons, magnitude_type = polygons_from_xml_horspool(doc) return generation_polygons, magnitude_type
def from_xml(cls, eqrm_flags): """ Load a GEM NRML vulnerability file in the format described in resources/nrml/schema/risk/vulnerability.xsd e.g. <discreteVulnerabilitySet vulnerabilitySetID="HAZUS" assetCategory="buildings" lossCategory="economic_loss"> <IML IMT="MMI"> 0.00 5.27 6.15 6.67 7.04 7.33 7.56 7.76 7.93 8.08 8.22 8.34 8.45 8.58 8.67 8.76 8.84 8.91 8.99 9.05 9.12 9.18 9.24 9.30 9.35 9.40 9.45 9.50 9.55 9.59 9.63 9.68 9.72 9.75 9.79 9.83 9.87 9.90 9.94 9.98 10.0</IML> <discreteVulnerability vulnerabilityFunctionID="W1TIMBERMETAL" probabilisticDistribution="LN"> <lossRatio> 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.01 0.01 0.02 0.03 0.04 0.06 0.08 0.10 0.13 0.15 0.18 0.21 0.24 0.27 0.31 0.34 0.37 0.40 0.43 0.46 0.49 0.52 0.55 0.58 0.60 0.63 0.65 0.67 0.70 0.72 1.00</lossRatio> <coefficientsVariation> 0.30 0.30 0.30 0.30 0.30 0.30 0.30 0.30 0.30 0.30 0.30 0.30 0.30 0.30 0.30 0.30 0.30 0.30 0.30 0.30 0.30 0.30 0.30 0.30 0.30 0.30 0.30 0.30 0.30 0.30 0.30 0.30 0.30 0.30 0.30 0.30 0.30 0.30 0.30 0.30 0.30</coefficientsVariation> </discreteVulnerability> <discreteVulnerability vulnerabilityFunctionID="W1BVMETAL" probabilisticDistribution="LN"> <lossRatio> 0.00 0.04 0.15 0.25 0.34 0.42 0.49 0.55 0.60 0.64 0.67 0.71 0.73 0.76 0.78 0.80 0.81 0.83 0.84 0.85 0.87 0.87 0.88 0.89 0.90 0.91 0.91 0.92 0.92 0.93 0.93 0.94 0.94 0.94 0.95 0.95 0.95 0.95 0.96 0.96 1.00</lossRatio> <coefficientsVariation> 0.30 0.30 0.30 0.30 0.30 0.30 0.30 0.30 0.30 0.30 0.30 0.30 0.30 0.30 0.30 0.30 0.30 0.30 0.30 0.30 0.30 0.30 0.30 0.30 0.30 0.30 0.30 0.30 0.30 0.30 0.30 0.30 0.30 0.30 0.30 0.30 0.30 0.30 0.30 0.30 0.30</coefficientsVariation> </discreteVulnerability> </discreteVulnerabilitySet> This function looks for the file in <eqrm_flags.input_dir>/<eqrm_flags.site_tag>_vulnerability.xml TODO: no XSD validation is currently performed. This requires lxml, which is not available in GA's runtime environment currently. """ # Find location of site database (i.e. building database) and get FID filename = os.path.join(eqrm_flags.input_dir, '%s_vulnerability.xml' % eqrm_flags.site_tag) if not os.path.exists(filename): msg = "No vulnerability XML was loaded. Check file name " + filename raise RuntimeError(msg) doc = Xml_Interface(filename=filename) vulnerability_set = doc['discreteVulnerabilitySet'][0] IML = vulnerability_set['IML'][0] intensity_measure_level = IML.array[0] intensity_measure_type = IML.attributes['IMT'] vulnerabilities = vulnerability_set['discreteVulnerability'] vulnerability_functions = {} for func in vulnerabilities: func_id = func.attributes['vulnerabilityFunctionID'] prob_dist = func.attributes['probabilisticDistribution'] loss_ratio = func['lossRatio'][0].array[0] coefficient_of_variation = func['coefficientsVariation'][0].array[ 0] var_method = eqrm_flags.vulnerability_variability_method vulnerability_functions[func_id] = Vulnerability_Function( func_id, loss_ratio, coefficient_of_variation, prob_dist, var_method) return cls(intensity_measure_level, intensity_measure_type, vulnerability_functions)
def setUp(self): # correctly formed old-style XML str1 = '\n'.join(['<Event magnitude_type="Mw">', ' <polygon>', ' <boundary>', '-20 120', '-30 120', '-20 120', ' </boundary>', ' <depth distribution="constant" mean="10"></depth>', ' </polygon>', ' <polygon>', ' <boundary>', '-30 121', '-35 122', '-30 135', '-30 121', ' </boundary>', ' <exclude>', '-32 121', '-34 122', '-32 121', ' </exclude>', ' <exclude>', '-35 121', '-36 122', '-35 121', ' </exclude>', ' <depth distribution="constant" mean="5" />', ' </polygon>', '</Event>']) self.xml = Xml_Interface(string=str1) # similar to above but simplified, wrong name for top-level tag str2 = '\n'.join(['<EventX magnitude_type="Mw">', ' <polygon>', ' <depth distribution="constant" mean="10"></depth>', ' </polygon>', ' <polygon>', ' <depth distribution="constant" mean="5" />', ' </polygon>', '</EventX>']) self.xml2 = Xml_Interface(string=str2)
def site_model_from_xml(regolith_polygon_filename, eqrm_dir='..'): import sys # imports important system function for use in Python if not eqrm_dir + '/eqrm_code/' in sys.path: # adds the EQRM code subdirectory to the PATH sys.path.append(eqrm_dir + '/eqrm_code/') from eqrm_code.xml_interface import Xml_Interface from scipy import array doc = Xml_Interface(filename=regolith_polygon_filename) xml_source_model = doc['site_class_polygons'][0] # opened the xml source_polygon_filename site_polygons = [] polygon_site_classes = [] xml_polygons = doc['site_class_polygon'] # get a list of xml polygons for xml_polygon in xml_polygons: boundary = xml_polygon.array site_class = xml_polygon.attributes['site_class'] # make the polygon an array polygon = array(boundary) site_polygons.append(polygon) polygon_site_classes.append(site_class) doc.unlink() return site_polygons, polygon_site_classes
def site_model_from_xml(regolith_polygon_filename, eqrm_dir='..'): import sys # imports important system function for use in Python if not eqrm_dir+'/eqrm_code/' in sys.path: # adds the EQRM code subdirectory to the PATH sys.path.append(eqrm_dir+'/eqrm_code/') from eqrm_code.xml_interface import Xml_Interface from scipy import array doc=Xml_Interface(filename=regolith_polygon_filename) xml_source_model=doc['site_class_polygons'][0] # opened the xml source_polygon_filename site_polygons=[] polygon_site_classes=[] xml_polygons = doc['site_class_polygon'] # get a list of xml polygons for xml_polygon in xml_polygons: boundary = xml_polygon.array site_class=xml_polygon.attributes['site_class'] # make the polygon an array polygon=array(boundary) site_polygons.append(polygon) polygon_site_classes.append(site_class) doc.unlink() return site_polygons,polygon_site_classes
def source_model_from_xml(filename): doc = Xml_Interface(filename=filename) xml_source_model = doc['source_model_zone'][0] magnitude_type = xml_source_model.attributes['magnitude_type'] source_zone_polygons = [] xml_polygons = doc['zone'] for i, xml_polygon in enumerate(xml_polygons): try: polygon_name = xml_polygon.attributes['name'] except KeyError: polygon_name = str(i) geometry = xml_polygon['geometry'][0] boundary = geometry['boundary'][0].array recurrence_models, event_gen = get_recurrence_elements(xml_polygon) area = float(xml_polygon.attributes['area']) event_type = xml_polygon.attributes['event_type'] generation_min_mag = float(event_gen.attributes[ 'generation_min_mag']) exclude = [] for exclusion_zone in xml_polygon['excludes']: exclude.append(exclusion_zone.array) # print 'LAMBDAMIN 1 ',A_min source_zone_polygon = Source_Zone( boundary, exclude, [RecurrenceModel(**rm.attributes) for rm in recurrence_models], generation_min_mag, event_type, polygon_name) source_zone_polygons.append(source_zone_polygon) doc.unlink() return Source_Model(source_zone_polygons, magnitude_type)
def setUp(self): # correctly formed old-style XML str1 = '\n'.join([ '<Event magnitude_type="Mw">', ' <polygon>', ' <boundary>', '-20 120', '-30 120', '-20 120', ' </boundary>', ' <depth distribution="constant" mean="10"></depth>', ' </polygon>', ' <polygon>', ' <boundary>', '-30 121', '-35 122', '-30 135', '-30 121', ' </boundary>', ' <exclude>', '-32 121', '-34 122', '-32 121', ' </exclude>', ' <exclude>', '-35 121', '-36 122', '-35 121', ' </exclude>', ' <depth distribution="constant" mean="5" />', ' </polygon>', '</Event>' ]) self.xml = Xml_Interface(string=str1) # similar to above but simplified, wrong name for top-level tag str2 = '\n'.join([ '<EventX magnitude_type="Mw">', ' <polygon>', ' <depth distribution="constant" mean="10"></depth>', ' </polygon>', ' <polygon>', ' <depth distribution="constant" mean="5" />', ' </polygon>', '</EventX>' ]) self.xml2 = Xml_Interface(string=str2)
def amplification_model_parameters_from_xml(filename): """ load an xml amplitfication model from file. return pga,moment_magnitude,periods,log_amplifications,log_stds """ xml_model = Xml_Interface(filename=filename) moment_magnitude_bins = \ asarray(xml_model['moment_magnitude_bins'][0].array[0]) pga_bins = asarray(xml_model['pga_bins'][0].array[0]) periods = asarray(xml_model['periods'][0].array[0]) log_amplifications = {} log_stds = {} for xml_site_class in xml_model['site_class']: log_amplification = zeros( (len(moment_magnitude_bins), len(pga_bins), len(periods)), dtype=float) log_std = zeros( (len(moment_magnitude_bins), len(pga_bins), len(periods)), dtype=float) site_class = xml_site_class.attributes['class'] for i in range(len(xml_site_class['moment_magnitude'])): xml_moment_magnitude = xml_site_class['moment_magnitude'][i] for j in range(len(xml_moment_magnitude['pga'])): xml_pga = xml_moment_magnitude['pga'][j] xml_log_amplification = xml_pga['log_amplification'][0] log_amplification[i, j, :] = xml_log_amplification.array[0] xml_log_std = xml_pga['log_std'][0] log_std[i, j, :] = xml_log_std.array[0] assert not site_class in log_amplifications # assert no duplicates log_amplifications[site_class] = log_amplification log_stds[site_class] = log_std return (pga_bins, moment_magnitude_bins, periods, log_amplifications, log_stds)
def xml_fault_generators(filename): """Read Horspool style XML. attributes: filename - the path to the XML file to read Returns: a tuple (gen_objects, mag_type) where 'gen_objects' is a list of Fault_Source_Generator objects and 'mag_type' is the magnitude type string. """ # get XML doc and top-level tag object try: doc = Xml_Interface(filename=filename) except Exception as e: msg = 'Malformed XML in file %s: %s' % (filename, str(e)) raise Exception(msg) top_tag = doc['source_model_fault'][0] if len(doc['source_model_fault']) == 0: msg = ("XML file %s: expected 'source_model_fault' tag, got '%s'" % (filename, doc.xml_node.documentElement.nodeName)) raise Exception(msg) # get magnitude type attribute try: try: magnitude_type = top_tag.attributes['magnitude_type'] except KeyError: magnitude_type = 'Mw' if magnitude_type != 'Mw': msg = "XML Error: magnitude_type must be 'Mw'." raise FileError(msg) # check that we have one or more 'fault' tags faults = doc['fault'] if len(faults) == 0: msg = "XML file %s: expected one or more 'fault' tags" % filename raise Exception(msg) # now cycle through 'fault' tags fsg_list = [] for i, fault in enumerate(faults): # get <fault> attributes try: fault_name = fault.attributes['name'] except KeyError: fault_name = 'fault_' + str(i) fault_event_type = fault.attributes['event_type'] # now get <geometry> attributes/children geometry = fault['geometry'] if len(geometry) != 1: msg = ("Badly formed XML in file %s: Expected exactly one " "'geometry' tag in fault named '%s'" % (filename, fault_name)) raise Exception(msg) geometry = geometry[0] geometry_dict = geometry.attributes # get <trace> data from <geometry> tag trace = geometry['trace'] if len(trace) != 1: msg = ("Badly formed XML in file %s: Expected exactly one 'trace' " "tag in fault named '%s'" % (filename, fault_name)) raise Exception(msg) trace = trace[0] start = trace['start'][0] start_point = start.attributes end = trace['end'][0] end_point = end.attributes trace_dict = {'start': start_point, 'end': end_point} geometry_dict['trace'] = trace_dict # get <recurrence_model> attributes/children recurrence_models, event_generation = get_recurrence_elements(fault) event_generation_dict = event_generation.attributes fault_obj = Fault_Source_Generator(filename, fault_name, fault_event_type, geometry_dict, recurrence_models, event_generation_dict) fsg_list.append(fault_obj) return (fsg_list, magnitude_type)
class Test_Xml_Interface(unittest.TestCase): def setUp(self): # correctly formed old-style XML str1 = '\n'.join([ '<Event magnitude_type="Mw">', ' <polygon>', ' <boundary>', '-20 120', '-30 120', '-20 120', ' </boundary>', ' <depth distribution="constant" mean="10"></depth>', ' </polygon>', ' <polygon>', ' <boundary>', '-30 121', '-35 122', '-30 135', '-30 121', ' </boundary>', ' <exclude>', '-32 121', '-34 122', '-32 121', ' </exclude>', ' <exclude>', '-35 121', '-36 122', '-35 121', ' </exclude>', ' <depth distribution="constant" mean="5" />', ' </polygon>', '</Event>' ]) self.xml = Xml_Interface(string=str1) # similar to above but simplified, wrong name for top-level tag str2 = '\n'.join([ '<EventX magnitude_type="Mw">', ' <polygon>', ' <depth distribution="constant" mean="10"></depth>', ' </polygon>', ' <polygon>', ' <depth distribution="constant" mean="5" />', ' </polygon>', '</EventX>' ]) self.xml2 = Xml_Interface(string=str2) def tearDown(self): self.xml.unlink() self.xml2.unlink() def test_attributes(self): assert self.xml['Event'][0].attributes['magnitude_type'] == 'Mw' def test_getting_polygons(self): assert len(self.xml['polygon']) == 2 def test_getting_excludes(self): assert len(self.xml['polygon'][0]['exclude']) == 0 assert len(self.xml['polygon'][1]['exclude']) == 2 def test_getting_array(self): exclude_array = self.xml['polygon'][1]['exclude'][0].array expected_exclude_array = [[-32.0, 121], [-34, 122], [-32, 121]] assert allclose(asarray(exclude_array), asarray(expected_exclude_array)) def Xtest_build_zone_xml(self): # Looking into something different root = build_xml() def test_top_level_tag_name(self): """Test getting some info about the XML document. Mainly to test various operations we will use. """ # test getting name of top-level tag # NOTE: There *must* be a better way to do this!? top_tag = self.xml.xml_node.documentElement.nodeName msg = "Expected top-level tag of 'Event', got '%s'" % top_tag self.failUnlessEqual(top_tag, 'Event', msg)
class Test_Xml_Interface(unittest.TestCase): def setUp(self): # correctly formed old-style XML str1 = '\n'.join(['<Event magnitude_type="Mw">', ' <polygon>', ' <boundary>', '-20 120', '-30 120', '-20 120', ' </boundary>', ' <depth distribution="constant" mean="10"></depth>', ' </polygon>', ' <polygon>', ' <boundary>', '-30 121', '-35 122', '-30 135', '-30 121', ' </boundary>', ' <exclude>', '-32 121', '-34 122', '-32 121', ' </exclude>', ' <exclude>', '-35 121', '-36 122', '-35 121', ' </exclude>', ' <depth distribution="constant" mean="5" />', ' </polygon>', '</Event>']) self.xml = Xml_Interface(string=str1) # similar to above but simplified, wrong name for top-level tag str2 = '\n'.join(['<EventX magnitude_type="Mw">', ' <polygon>', ' <depth distribution="constant" mean="10"></depth>', ' </polygon>', ' <polygon>', ' <depth distribution="constant" mean="5" />', ' </polygon>', '</EventX>']) self.xml2 = Xml_Interface(string=str2) def tearDown(self): self.xml.unlink() self.xml2.unlink() def test_attributes(self): assert self.xml['Event'][0].attributes['magnitude_type']=='Mw' def test_getting_polygons(self): assert len(self.xml['polygon'])==2 def test_getting_excludes(self): assert len(self.xml['polygon'][0]['exclude'])==0 assert len(self.xml['polygon'][1]['exclude'])==2 def test_getting_array(self): exclude_array = self.xml['polygon'][1]['exclude'][0].array expected_exclude_array=[[-32.0,121],[-34,122],[-32,121]] assert allclose(asarray(exclude_array),asarray(expected_exclude_array)) def Xtest_build_zone_xml(self): # Looking into something different root = build_xml() def test_top_level_tag_name(self): """Test getting some info about the XML document. Mainly to test various operations we will use. """ # test getting name of top-level tag # NOTE: There *must* be a better way to do this!? top_tag = self.xml.xml_node.documentElement.nodeName msg = "Expected top-level tag of 'Event', got '%s'" % top_tag self.failUnlessEqual(top_tag, 'Event', msg)
def event_control_from_xml(filename): """Routine to read in event control data from an XML file and return a list of Event_Group objects containing the raw data. filename is the path to the event control XML file """ class Event_Group(object): """Class to hold event group data as attributes.""" def __init__(self, filename, event_type, fault_type, branch_list, scaling_dict): """Construct an object from event group data. filename name of file we are reading (for debug) event_type a string describing the event type fault_type a string describing the fault type branch_list a list of dictionaries {'model': '...', 'weight': '0.5'} scaling_dict a dictionary {'scaling_rule': '...', 'scaling_fault_type': '...'} The constructed object will have the following attributes: .event_type - event type string .fault_type - fault type string .branch_models - list of branch models .branch_weights - list of branch wrights .scaling_dict - event scaling information """ self.event_type = event_type self.fault_type = fault_type self.branch_models = [] self.branch_weights = [] for b in branch_list: self.branch_models.append(b['model']) weight = float(b['weight']) self.branch_weights.append(weight) self.branch_weights = asarray(self.branch_weights) msg = ("XML file %s: weights for event group '%s' should sum " "to 1.0, got %.1f" % (filename, event_type, sum(self.branch_weights))) self.branch_weights = parse_in_parameters.check_sum_1_normalise( self.branch_weights, msg) try: _ = scaling_dict['scaling_rule'] except KeyError: msg = ("XML file %s: missing 'scaling_rule' attribute" " in event group '%s'" % (filename, event_type)) raise Exception(msg) self.scaling_dict = scaling_dict # get XML doc and top-level tag object try: doc = Xml_Interface(filename=filename) except Exception as e: msg = 'Malformed XML in file %s: %s' % (filename, str(e)) raise Exception(msg) top_tag = doc['event_type_controlfile'][0] if len(doc['event_type_controlfile']) == 0: msg = ("XML file %s: expected 'event_type_controlfile' tag, got '%s'" % (filename, doc.xml_node.documentElement.nodeName)) raise Exception(msg) # check that we have one or more 'event_group' tags event_groups = doc['event_group'] if len(event_groups) == 0: msg = "XML file %s: expected one or more 'event_group' tags" % filename raise Exception(msg) # now cycle through 'event_group' tags eg_list = [] for eg in event_groups: # get <event_group> attributes event_type = eg.attributes['event_type'] # ensure only one <GMPE> tag and get attributes gmpe = eg['GMPE'] if len(gmpe) != 1: msg = ("Badly formed XML in file %s: Expected exactly one " "'GMPE' tag in event group '%s'" % (filename, event_group)) raise Exception(msg) gmpe = gmpe[0] fault_type = gmpe.attributes['fault_type'] # check that we have one or more 'branch' tags under <GMPE> branches = gmpe['branch'] if len(branches) == 0: msg = ("XML file %s: expected one or more 'branch' tags in event " "group '%s'" % (filename, event_group)) raise Exception(msg) branch_list = [] for b in branches: branch_dict = b.attributes branch_list.append(branch_dict) # get <scaling> dictionary scaling = eg['scaling'] if len(scaling) != 1: msg = ("Badly formed XML in file %s: Expected exactly one " "'scaling' tag in event_group '%s'" % (filename, event_group)) raise Exception(msg) scaling = scaling[0] scaling_dict = scaling.attributes eg_obj = Event_Group(filename, event_type, fault_type, branch_list, scaling_dict) eg_list.append(eg_obj) return eg_list