def _get_exposure(fname, ok_cost_types, stop=None): """ :param fname: path of the XML file containing the exposure :param ok_cost_types: a set of cost types (as strings) :param stop: node at which to stop parsing (or None) :returns: a pair (Exposure instance, list of asset nodes) """ [exposure] = nrml.read(fname, stop=stop) description = exposure.description try: conversions = exposure.conversions except AttributeError: conversions = Node('conversions', nodes=[Node('costTypes', [])]) try: inslimit = conversions.insuranceLimit except AttributeError: inslimit = Node('insuranceLimit', text=True) try: deductible = conversions.deductible except AttributeError: deductible = Node('deductible', text=True) try: area = conversions.area except AttributeError: # NB: the area type cannot be an empty string because when sending # around the CostCalculator object we would run into this numpy bug # about pickling dictionaries with empty strings: # https://github.com/numpy/numpy/pull/5475 area = Node('area', dict(type='?')) # read the cost types and make some check cost_types = [] for ct in conversions.costTypes: if ct['name'] in ok_cost_types: with context(fname, ct): cost_types.append( (ct['name'], valid.cost_type_type(ct['type']), ct['unit'])) if 'occupants' in ok_cost_types: cost_types.append(('occupants', 'per_area', 'people')) cost_types.sort(key=operator.itemgetter(0)) cost_types = numpy.array(cost_types, cost_type_dt) insurance_limit_is_absolute = inslimit.attrib.get('isAbsolute', True) deductible_is_absolute = deductible.attrib.get('isAbsolute', True) time_events = set() cc = riskmodels.CostCalculator({}, {}, {}, deductible_is_absolute, insurance_limit_is_absolute) for ct in cost_types: name = ct['name'] # structural, nonstructural, ... cc.cost_types[name] = ct['type'] # aggregated, per_asset, per_area cc.area_types[name] = area['type'] cc.units[name] = ct['unit'] exp = Exposure(exposure['id'], exposure['category'], ~description, cost_types, time_events, insurance_limit_is_absolute, deductible_is_absolute, area.attrib, [], set(), [], cc) return exp, exposure.assets
def get_exposure_lazy(fname, ok_cost_types): """ :param fname: path of the XML file containing the exposure :param ok_cost_types: a set of cost types (as strings) :returns: a pair (Exposure instance, list of asset nodes) """ [exposure] = nrml.read_lazy(fname, ['assets']) description = exposure.description try: conversions = exposure.conversions except NameError: conversions = LiteralNode('conversions', nodes=[LiteralNode('costTypes', [])]) try: inslimit = conversions.insuranceLimit except NameError: inslimit = LiteralNode('insuranceLimit', text=True) try: deductible = conversions.deductible except NameError: deductible = LiteralNode('deductible', text=True) try: area = conversions.area except NameError: # NB: the area type cannot be an empty string because when sending # around the CostCalculator object one runs into this numpy bug on # pickling dictionaries with empty strings: # https://github.com/numpy/numpy/pull/5475 area = LiteralNode('area', dict(type='?')) # read the cost types and make some check cost_types = [] for ct in conversions.costTypes: if ct['name'] in ok_cost_types: with context(fname, ct): cost_types.append( (ct['name'], valid_cost_type(ct['type']), ct['unit'])) if 'occupants' in ok_cost_types: cost_types.append(('occupants', 'per_area', 'people')) cost_types.sort(key=operator.itemgetter(0)) time_events = set() exp = Exposure(exposure['id'], exposure['category'], ~description, numpy.array(cost_types, cost_type_dt), time_events, ~inslimit, ~deductible, area.attrib, [], set(), []) cc = riskmodels.CostCalculator({}, {}, exp.deductible_is_absolute, exp.insurance_limit_is_absolute) for ct in exp.cost_types: name = ct['name'] # structural, nonstructural, ... cc.cost_types[name] = ct['type'] # aggregated, per_asset, per_area cc.area_types[name] = exp.area['type'] return exp, exposure.assets, cc