def test_wrong_riskmodel_association(self): cfm = os.path.join(FF_DIR, 'contents_fragility_model.xml') # passing a fragility model instead of a consequence model oq = mock.Mock() oq.inputs = dict(contents_consequence=cfm) with self.assertRaises(ValueError) as ctx: riskmodels.get_risk_models(oq, 'consequence') self.assertIn('is of kind FragilityModel, ' 'expected ConsequenceModel', str(ctx.exception))
def test_wrong_riskmodel_association(self): cfm = os.path.join(FF_DIR, 'contents_fragility_model.xml') # passing a fragility model instead of a consequence model oq = mock.Mock() oq.inputs = dict(contents_consequence=cfm) with self.assertRaises(ValueError) as ctx: riskmodels.get_risk_models(oq, 'consequence') self.assertIn('is of kind FragilityModel, ' 'expected ConsequenceModel', str(ctx.exception))
def test_wrong_loss_type_association(self): scm = os.path.join(FF_DIR, 'structural_consequence_model.xml') ccm = os.path.join(FF_DIR, 'contents_consequence_model.xml') # exchanging the associations on purpose oq = mock.Mock() oq.inputs = dict(structural_consequence=ccm, contents_consequence=scm) with self.assertRaises(ValueError) as ctx: riskmodels.get_risk_models(oq, 'consequence') self.assertIn('structural_consequence_model.xml": lossCategory is of ' 'type "structural", expected "contents"', str(ctx.exception))
def test_wrong_loss_type_association(self): scm = os.path.join(FF_DIR, 'structural_consequence_model.xml') ccm = os.path.join(FF_DIR, 'contents_consequence_model.xml') # exchanging the associations on purpose oq = mock.Mock() oq.inputs = dict(structural_consequence=ccm, contents_consequence=scm) with self.assertRaises(ValueError) as ctx: riskmodels.get_risk_models(oq, 'consequence') self.assertIn('structural_consequence_model.xml": lossCategory is of ' 'type "structural", expected "contents"', str(ctx.exception))
def get_risk_model(oqparam): """ Return a :class:`openquake.risklib.riskinput.CompositeRiskModel` instance :param oqparam: an :class:`openquake.commonlib.oqvalidation.OqParam` instance """ rmdict = get_risk_models(oqparam) oqparam.set_risk_imtls(rmdict) if oqparam.calculation_mode.endswith('_bcr'): retro = get_risk_models(oqparam, 'vulnerability_retrofitted') else: retro = {} return riskinput.CompositeRiskModel(oqparam, rmdict, retro)
def get_risk_model(oqparam): """ Return a :class:`openquake.risklib.riskinput.CompositeRiskModel` instance :param oqparam: an :class:`openquake.commonlib.oqvalidation.OqParam` instance """ rmdict = get_risk_models(oqparam) oqparam.set_risk_imtls(rmdict) if oqparam.calculation_mode.endswith('_bcr'): retro = get_risk_models(oqparam, 'vulnerability_retrofitted') else: retro = {} return riskinput.CompositeRiskModel(oqparam, rmdict, retro)
def load_riskmodel(self): """ Read the risk model and set the attribute .riskmodel. The riskmodel can be empty for hazard calculations. Save the loss ratios (if any) in the datastore. """ rmdict = riskmodels.get_risk_models(self.oqparam) if not rmdict: # can happen only in a hazard calculation return self.oqparam.set_risk_imtls(rmdict) self.save_params() # re-save oqparam self.riskmodel = rm = readinput.get_risk_model(self.oqparam, rmdict) # save the risk models and loss_ratios in the datastore for taxonomy, rmodel in rm.items(): self.datastore['composite_risk_model/' + taxonomy] = ( rmodel.risk_functions) if hasattr(rmodel, 'retro_functions'): self.datastore[ 'composite_risk_model/%s-retrofitted' % taxonomy] = ( rmodel.retro_functions) attrs = self.datastore['composite_risk_model'].attrs attrs['loss_types'] = hdf5.array_of_vstr(rm.loss_types) attrs['min_iml'] = hdf5.array_of_vstr(sorted(rm.get_min_iml().items())) if rm.damage_states: attrs['damage_states'] = hdf5.array_of_vstr(rm.damage_states) self.datastore['loss_ratios'] = rm.get_loss_ratios() self.datastore.set_nbytes('composite_risk_model') self.datastore.set_nbytes('loss_ratios') self.datastore.hdf5.flush()
def load_riskmodel(self): """ Read the risk model and set the attribute .riskmodel. The riskmodel can be empty for hazard calculations. Save the loss ratios (if any) in the datastore. """ rmdict = riskmodels.get_risk_models(self.oqparam) if not rmdict: # can happen only in a hazard calculation return self.oqparam.set_risk_imtls(rmdict) self.save_params() # re-save oqparam self.riskmodel = rm = readinput.get_risk_model(self.oqparam, rmdict) if "taxonomies" in self.datastore: # check that we are covering all the taxonomies in the exposure missing = set(self.taxonomies) - set(rm.taxonomies) if rm and missing: raise RuntimeError( "The exposure contains the taxonomies %s " "which are not in the risk model" % missing ) # save the risk models and loss_ratios in the datastore for taxonomy, rmodel in rm.items(): self.datastore["composite_risk_model/" + taxonomy] = rmodel.risk_functions if hasattr(rmodel, "retro_functions"): self.datastore["composite_risk_model/%s-retrofitted" % taxonomy] = rmodel.retro_functions attrs = self.datastore["composite_risk_model"].attrs attrs["loss_types"] = rm.loss_types attrs["min_iml"] = sorted(rm.get_min_iml().items()) if rm.damage_states: attrs["damage_states"] = rm.damage_states self.datastore["loss_ratios"] = rm.get_loss_ratios() self.datastore.set_nbytes("composite_risk_model") self.datastore.set_nbytes("loss_ratios") self.datastore.hdf5.flush()
def __init__(self, **names_vals): super(OqParam, self).__init__(**names_vals) self.risk_investigation_time = ( self.risk_investigation_time or self.investigation_time) if ('intensity_measure_types_and_levels' in names_vals and 'intensity_measure_types' in names_vals): logging.warn('Ignoring intensity_measure_types since ' 'intensity_measure_types_and_levels is set') if 'intensity_measure_types_and_levels' in names_vals: self.hazard_imtls = self.intensity_measure_types_and_levels delattr(self, 'intensity_measure_types_and_levels') elif 'intensity_measure_types' in names_vals: self.hazard_imtls = dict.fromkeys(self.intensity_measure_types) delattr(self, 'intensity_measure_types') file_type, file_by_ct = get_risk_files(self.inputs) rmdict.clear() rmodels = get_risk_models(self, file_type) rmdict.update(rmodels) self.risk_imtls = get_imtls(rmdict) # check the IMTs vs the GSIMs if 'gsim_logic_tree' in self.inputs: if self.gsim: raise ValueError('If `gsim_logic_tree_file` is set, there ' 'must be no `gsim` key') path = os.path.join( self.base_path, self.inputs['gsim_logic_tree']) for gsims in logictree.GsimLogicTree(path, []).values.values(): self.check_gsims(gsims) elif self.gsim is not None: self.check_gsims([self.gsim])
def load_riskmodel(self): """ Read the risk model and set the attribute .riskmodel. The riskmodel can be empty for hazard calculations. Save the loss ratios (if any) in the datastore. """ rmdict = riskmodels.get_risk_models(self.oqparam) self.oqparam.set_risk_imtls(rmdict) # save risk_imtls in the datastore: this is crucial self.datastore.hdf5.attrs['risk_imtls'] = repr(self.oqparam.risk_imtls) self.riskmodel = rm = readinput.get_risk_model(self.oqparam, rmdict) if 'taxonomies' in self.datastore: # check that we are covering all the taxonomies in the exposure missing = set(self.taxonomies) - set(rm.taxonomies) if rm and missing: raise RuntimeError('The exposure contains the taxonomies %s ' 'which are not in the risk model' % missing) # save the loss ratios in the datastore pairs = [(cb.loss_type, (numpy.float64, len(cb.ratios))) for cb in rm.curve_builders if cb.user_provided] if not pairs: return loss_ratios = numpy.zeros(len(rm), numpy.dtype(pairs)) for cb in rm.curve_builders: if cb.user_provided: loss_ratios_lt = loss_ratios[cb.loss_type] for i, imt_taxo in enumerate(sorted(rm)): loss_ratios_lt[i] = rm[imt_taxo].loss_ratios[cb.loss_type] self.datastore['loss_ratios'] = loss_ratios self.datastore['loss_ratios'].attrs['imt_taxos'] = sorted(rm) self.datastore['loss_ratios'].attrs['nbytes'] = loss_ratios.nbytes
def load_riskmodel(self): """ Read the risk model and set the attribute .riskmodel. The riskmodel can be empty for hazard calculations. Save the loss ratios (if any) in the datastore. """ rmdict = riskmodels.get_risk_models(self.oqparam) if not rmdict: # can happen only in a hazard calculation return self.oqparam.set_risk_imtls(rmdict) self.save_params() # re-save oqparam self.riskmodel = rm = readinput.get_risk_model(self.oqparam, rmdict) if 'taxonomies' in self.datastore: # check that we are covering all the taxonomies in the exposure missing = set(self.taxonomies) - set(rm.taxonomies) if rm and missing: raise RuntimeError('The exposure contains the taxonomies %s ' 'which are not in the risk model' % missing) # save the risk models and loss_ratios in the datastore for taxonomy, rmodel in rm.items(): self.datastore['composite_risk_model/' + taxonomy] = (rmodel.risk_functions) if hasattr(rmodel, 'retro_functions'): self.datastore['composite_risk_model/%s-retrofitted' % taxonomy] = (rmodel.retro_functions) attrs = self.datastore['composite_risk_model'].attrs attrs['loss_types'] = rm.loss_types attrs['min_iml'] = sorted(rm.get_min_iml().items()) if rm.damage_states: attrs['damage_states'] = rm.damage_states self.datastore['loss_ratios'] = rm.get_loss_ratios() self.datastore.set_nbytes('composite_risk_model') self.datastore.set_nbytes('loss_ratios') self.datastore.hdf5.flush()
def read_risk_data(self): """ Read the exposure (if any), the risk model (if any) and then the site collection, possibly extracted from the exposure. """ oq = self.oqparam with self.monitor('reading site collection', autoflush=True): haz_sitecol = readinput.get_site_collection(oq) if haz_sitecol is not None: logging.info('Read %d hazard site(s)', len(haz_sitecol)) oq_hazard = (self.datastore.parent['oqparam'] if self.datastore.parent else None) if 'exposure' in oq.inputs: self.read_exposure() self.load_riskmodel() # must be called *after* read_exposure num_assets = self.count_assets() if self.datastore.parent: haz_sitecol = self.datastore.parent['sitecol'] if haz_sitecol is not None and haz_sitecol != self.sitecol: with self.monitor('assoc_assets_sites', autoflush=True): self.sitecol, self.assetcol = \ self.assoc_assets_sites(haz_sitecol.complete) ok_assets = self.count_assets() num_sites = len(self.sitecol) logging.warn('Associated %d assets to %d sites, %d discarded', ok_assets, num_sites, num_assets - ok_assets) elif oq.job_type == 'risk': raise RuntimeError('Missing exposure_file in %(job_ini)s' % oq.inputs) else: # no exposure self.load_riskmodel() self.sitecol = haz_sitecol if oq_hazard: parent = self.datastore.parent if 'assetcol' in parent: check_time_event(oq, parent['assetcol'].time_events) if oq_hazard.time_event and oq_hazard.time_event != oq.time_event: raise ValueError( 'The risk configuration file has time_event=%s but the ' 'hazard was computed with time_event=%s' % (oq.time_event, oq_hazard.time_event)) if self.oqparam.job_type == 'risk': taxonomies = set(self.taxonomies) # check that we are covering all the taxonomies in the exposure missing = taxonomies - set(self.riskmodel.taxonomies) if self.riskmodel and missing: raise RuntimeError('The exposure contains the taxonomies %s ' 'which are not in the risk model' % missing) # same check for the consequence models, if any consequence_models = riskmodels.get_risk_models( self.oqparam, 'consequence') for lt, cm in consequence_models.items(): missing = taxonomies - set(cm) if missing: raise ValueError('Missing consequenceFunctions for %s' % ' '.join(missing))
def __init__(self, **names_vals): super(OqParam, self).__init__(**names_vals) self.risk_investigation_time = (self.risk_investigation_time or self.investigation_time) if ('intensity_measure_types_and_levels' in names_vals and 'intensity_measure_types' in names_vals): logging.warn('Ignoring intensity_measure_types since ' 'intensity_measure_types_and_levels is set') if 'intensity_measure_types_and_levels' in names_vals: self.hazard_imtls = self.intensity_measure_types_and_levels delattr(self, 'intensity_measure_types_and_levels') elif 'intensity_measure_types' in names_vals: self.hazard_imtls = dict.fromkeys(self.intensity_measure_types) delattr(self, 'intensity_measure_types') file_type, file_by_ct = get_risk_files(self.inputs) rmdict.clear() rmodels = get_risk_models(self, file_type) rmdict.update(rmodels) self.risk_imtls = get_imtls(rmdict) # check the IMTs vs the GSIMs if 'gsim_logic_tree' in self.inputs: if self.gsim: raise ValueError('If `gsim_logic_tree_file` is set, there ' 'must be no `gsim` key') path = os.path.join(self.base_path, self.inputs['gsim_logic_tree']) for gsims in logictree.GsimLogicTree(path, []).values.values(): self.check_gsims(gsims) elif self.gsim is not None: self.check_gsims([self.gsim])
def setUpClass(cls): cls.oqparam = readinput.get_oqparam('job_loss.ini', pkg=case_2) cls.oqparam.insured_losses = True cls.sitecol, cls.assets_by_site = readinput.get_sitecol_assets( cls.oqparam, readinput.get_exposure(cls.oqparam)) rmdict = riskmodels.get_risk_models(cls.oqparam) cls.riskmodel = readinput.get_risk_model(cls.oqparam, rmdict)
def read_risk_data(self): """ Read the exposure (if any), the risk model (if any) and then the site collection, possibly extracted from the exposure. """ oq = self.oqparam with self.monitor('reading site collection', autoflush=True): haz_sitecol = readinput.get_site_collection(oq) if haz_sitecol is not None: logging.info('There are %d hazard site(s)', len(haz_sitecol)) oq_hazard = (self.datastore.parent['oqparam'] if self.datastore.parent else None) if 'exposure' in oq.inputs: self.read_exposure(haz_sitecol) self.load_riskmodel() # must be called *after* read_exposure self.datastore['assetcol'] = self.assetcol elif 'assetcol' in self.datastore.parent: region = wkt.loads(self.oqparam.region_constraint) self.sitecol = haz_sitecol.within(region) assetcol = self.datastore.parent['assetcol'] self.assetcol = assetcol.reduce(self.sitecol.sids) self.datastore['assetcol'] = self.assetcol logging.info('There are %d/%d assets in the region', len(self.assetcol), len(assetcol)) self.load_riskmodel() else: # no exposure self.load_riskmodel() self.sitecol = haz_sitecol if oq_hazard: parent = self.datastore.parent if 'assetcol' in parent: check_time_event(oq, parent['assetcol'].occupancy_periods) if oq_hazard.time_event and oq_hazard.time_event != oq.time_event: raise ValueError( 'The risk configuration file has time_event=%s but the ' 'hazard was computed with time_event=%s' % (oq.time_event, oq_hazard.time_event)) if self.oqparam.job_type == 'risk': taxonomies = set(taxo for taxo in self.assetcol.tagcol.taxonomy if taxo != '?') # check that we are covering all the taxonomies in the exposure missing = taxonomies - set(self.riskmodel.taxonomies) if self.riskmodel and missing: raise RuntimeError('The exposure contains the taxonomies %s ' 'which are not in the risk model' % missing) # same check for the consequence models, if any consequence_models = riskmodels.get_risk_models( self.oqparam, 'consequence') for lt, cm in consequence_models.items(): missing = taxonomies - set(cm) if missing: raise ValueError('Missing consequenceFunctions for %s' % ' '.join(missing))
def pre_execute(self): if 'gmfs' in self.oqparam.inputs: self.pre_calculator = None base.RiskCalculator.pre_execute(self) self.monitor.consequence_models = riskmodels.get_risk_models( self.oqparam, 'consequence') self.etags, gmfs = calc.get_gmfs(self.datastore) self.riskinputs = self.build_riskinputs(gmfs) self.monitor.taxonomies = sorted(self.taxonomies)
def pre_execute(self): if 'gmfs' in self.oqparam.inputs: self.pre_calculator = None base.RiskCalculator.pre_execute(self) self.monitor.consequence_models = riskmodels.get_risk_models( self.oqparam, 'consequence') self.datastore['etags'], gmfs = calc.get_gmfs(self.datastore) self.riskinputs = self.build_riskinputs(gmfs) self.monitor.taxonomies = sorted(self.taxonomies)
def get_gmfs_from_txt(oqparam, fname): """ :param oqparam: an :class:`openquake.commonlib.oqvalidation.OqParam` instance :param fname: the full path of the CSV file :returns: a composite array of shape (N, R) read from a CSV file with format `etag indices [gmv1 ... gmvN] * num_imts` """ with open(fname) as csvfile: firstline = next(csvfile) try: coords = valid.coordinates(firstline) except: raise InvalidFile( 'The first line of %s is expected to contain comma separated' 'ordered coordinates, got %s instead' % (fname, firstline)) sitecol = sitecol_from_coords(oqparam, coords) if not oqparam.imtls: oqparam.set_risk_imtls(get_risk_models(oqparam)) imts = list(oqparam.imtls) imt_dt = numpy.dtype([(imt, F32) for imt in imts]) num_gmfs = oqparam.number_of_ground_motion_fields gmf_by_imt = numpy.zeros((num_gmfs, len(sitecol)), imt_dt) etags = [] for lineno, line in enumerate(csvfile, 2): row = line.split(',') try: indices = list(map(valid.positiveint, row[1].split())) except: raise InvalidFile( 'The second column in %s is expected to contain integer ' 'indices, got %s' % (fname, row[1])) r_sites = ( sitecol if not indices else site.FilteredSiteCollection(indices, sitecol)) for i in range(len(imts)): try: array = numpy.array(valid.positivefloats(row[i + 2])) # NB: i + 2 because the first 2 fields are etag and indices except: raise InvalidFile( 'The column #%d in %s is expected to contain positive ' 'floats, got %s instead' % (i + 3, fname, row[i + 2])) gmf_by_imt[imts[i]][lineno - 2] = r_sites.expand(array, 0) etags.append(row[0]) if lineno < num_gmfs + 1: raise InvalidFile('%s contains %d rows, expected %d' % ( fname, lineno, num_gmfs + 1)) if etags != sorted(etags): raise InvalidFile('The etags in %s are not ordered: %s' % (fname, etags)) return sitecol, numpy.array([encode(e) for e in etags]), gmf_by_imt.T
def get_gmfs_from_txt(oqparam, fname): """ :param oqparam: an :class:`openquake.commonlib.oqvalidation.OqParam` instance :param fname: the full path of the CSV file :returns: a composite array of shape (N, R) read from a CSV file with format `etag indices [gmv1 ... gmvN] * num_imts` """ with open(fname) as csvfile: firstline = next(csvfile) try: coords = valid.coordinates(firstline) except: raise InvalidFile( 'The first line of %s is expected to contain comma separated' 'ordered coordinates, got %s instead' % (fname, firstline)) lons, lats, depths = zip(*coords) sitecol = site.SiteCollection.from_points(lons, lats, depths, oqparam) if not oqparam.imtls: oqparam.set_risk_imtls(get_risk_models(oqparam)) imts = list(oqparam.imtls) imt_dt = numpy.dtype([(imt, F32) for imt in imts]) num_gmfs = oqparam.number_of_ground_motion_fields gmf_by_imt = numpy.zeros((num_gmfs, len(sitecol)), imt_dt) etags = [] for lineno, line in enumerate(csvfile, 2): row = line.split(',') try: indices = list(map(valid.positiveint, row[1].split())) except: raise InvalidFile( 'The second column in %s is expected to contain integer ' 'indices, got %s' % (fname, row[1])) r_sites = (sitecol if not indices else site.FilteredSiteCollection( indices, sitecol)) for i in range(len(imts)): try: array = numpy.array(valid.positivefloats(row[i + 2])) # NB: i + 2 because the first 2 fields are etag and indices except: raise InvalidFile( 'The column #%d in %s is expected to contain positive ' 'floats, got %s instead' % (i + 3, fname, row[i + 2])) gmf_by_imt[imts[i]][lineno - 2][r_sites.sids] = array etags.append(row[0]) if lineno < num_gmfs + 1: raise InvalidFile('%s contains %d rows, expected %d' % (fname, lineno, num_gmfs + 1)) if etags != sorted(etags): raise InvalidFile('The etags in %s are not ordered: %s' % (fname, etags)) return sitecol, numpy.array([encode(e) for e in etags]), gmf_by_imt.T
def get_risk_model(oqparam, rmdict): """ Return a :class:`openquake.risklib.riskinput.RiskModel` instance :param oqparam: an :class:`openquake.commonlib.oqvalidation.OqParam` instance :param rmdict: a dictionary (imt, taxonomy) -> loss_type -> risk_function """ wfs = {} # (imt, taxonomy) -> workflow riskmodel = riskinput.RiskModel(wfs) if getattr(oqparam, 'limit_states', []): # classical_damage/scenario_damage calculator if oqparam.calculation_mode in ('classical', 'scenario'): # case when the risk files are in the job_hazard.ini file oqparam.calculation_mode += '_damage' riskmodel.damage_states = ['no_damage'] + oqparam.limit_states delattr(oqparam, 'limit_states') for imt_taxo, ffs_by_lt in rmdict.items(): wfs[imt_taxo] = workflows.get_workflow( imt_taxo[0], imt_taxo[1], oqparam, fragility_functions=ffs_by_lt) elif oqparam.calculation_mode.endswith('_bcr'): # classical_bcr calculator retro = get_risk_models(oqparam, 'vulnerability_retrofitted') for (imt_taxo, vf_orig), (imt_taxo_, vf_retro) in \ zip(rmdict.items(), retro.items()): assert imt_taxo == imt_taxo_ # same imt and taxonomy wfs[imt_taxo] = workflows.get_workflow( imt_taxo[0], imt_taxo[1], oqparam, vulnerability_functions_orig=vf_orig, vulnerability_functions_retro=vf_retro) else: # classical, event based and scenario calculators for imt_taxo, vfs in rmdict.items(): for vf in vfs.values(): # set the seed; this is important for the case of # VulnerabilityFunctionWithPMF vf.seed = oqparam.random_seed wfs[imt_taxo] = workflows.get_workflow( imt_taxo[0], imt_taxo[1], oqparam, vulnerability_functions=vfs) riskmodel.make_curve_builders(oqparam) taxonomies = set() for imt_taxo, workflow in wfs.items(): taxonomies.add(imt_taxo[1]) workflow.riskmodel = riskmodel # save the number of nonzero coefficients of variation for vf in workflow.risk_functions.values(): if hasattr(vf, 'covs') and vf.covs.any(): riskmodel.covs += 1 riskmodel.taxonomies = sorted(taxonomies) return riskmodel
def pre_execute(self): if 'gmfs' in self.oqparam.inputs: self.pre_calculator = None base.RiskCalculator.pre_execute(self) self.param['number_of_ground_motion_fields'] = ( self.oqparam.number_of_ground_motion_fields) self.param['consequence_models'] = riskmodels.get_risk_models( self.oqparam, 'consequence') _, gmfs = calc.get_gmfs(self.datastore, self.precalc) self.riskinputs = self.build_riskinputs('gmf', gmfs) self.param['taxonomies'] = sorted(self.taxonomies)
def pre_execute(self): if 'gmfs' in self.oqparam.inputs: self.pre_calculator = None base.RiskCalculator.pre_execute(self) eids, self.R = base.get_gmfs(self) self.param['number_of_ground_motion_fields'] = ( self.oqparam.number_of_ground_motion_fields) self.param['consequence_models'] = riskmodels.get_risk_models( self.oqparam, 'consequence') self.riskinputs = self.build_riskinputs('gmf', eids=eids) self.param['tags'] = self.assetcol.tags()
def pre_execute(self): if 'gmfs' in self.oqparam.inputs: self.pre_calculator = None base.RiskCalculator.pre_execute(self) self.monitor.consequence_models = riskmodels.get_risk_models( self.oqparam, 'consequence') self.datastore['etags'], gmfs = calc.get_gmfs( self.datastore, self.precalc) rlzs = self.csm_info.get_rlzs_assoc().realizations self.riskinputs = self.build_riskinputs( {rlz: gmf for rlz, gmf in zip(rlzs, gmfs)}) self.monitor.taxonomies = sorted(self.taxonomies)
def get_risk_model(oqparam, rmdict): """ Return a :class:`openquake.risklib.riskinput.CompositeRiskModel` instance :param oqparam: an :class:`openquake.commonlib.oqvalidation.OqParam` instance :param rmdict: a dictionary (imt, taxonomy) -> loss_type -> risk_function """ riskmod = {} # taxonomy -> riskmodel crm = riskinput.CompositeRiskModel(riskmod) if getattr(oqparam, 'limit_states', []): # classical_damage/scenario_damage calculator if oqparam.calculation_mode in ('classical', 'scenario'): # case when the risk files are in the job_hazard.ini file oqparam.calculation_mode += '_damage' crm.damage_states = ['no_damage'] + oqparam.limit_states delattr(oqparam, 'limit_states') for taxonomy, ffs_by_lt in rmdict.items(): riskmod[taxonomy] = riskmodels.get_riskmodel( taxonomy, oqparam, fragility_functions=ffs_by_lt) elif oqparam.calculation_mode.endswith('_bcr'): # classical_bcr calculator retro = get_risk_models(oqparam, 'vulnerability_retrofitted') for (taxonomy, vf_orig), (taxonomy_, vf_retro) in \ zip(rmdict.items(), retro.items()): assert taxonomy == taxonomy_ # same imt and taxonomy riskmod[taxonomy] = riskmodels.get_riskmodel( taxonomy, oqparam, vulnerability_functions_orig=vf_orig, vulnerability_functions_retro=vf_retro) else: # classical, event based and scenario calculators for taxonomy, vfs in rmdict.items(): for vf in vfs.values(): # set the seed; this is important for the case of # VulnerabilityFunctionWithPMF vf.seed = oqparam.random_seed riskmod[taxonomy] = riskmodels.get_riskmodel( taxonomy, oqparam, vulnerability_functions=vfs) crm.make_curve_builders(oqparam) taxonomies = set() for taxonomy, riskmodel in riskmod.items(): taxonomies.add(taxonomy) riskmodel.compositemodel = crm # save the number of nonzero coefficients of variation for vf in riskmodel.risk_functions.values(): if hasattr(vf, 'covs') and vf.covs.any(): crm.covs += 1 crm.taxonomies = sorted(taxonomies) return crm
def get_scenario_from_nrml(oqparam, fname): """ :param oqparam: an :class:`openquake.commonlib.oqvalidation.OqParam` instance :param fname: the NRML files containing the GMFs :returns: a triple (sitecol, eids, gmf array) """ if not oqparam.imtls: oqparam.set_risk_imtls(get_risk_models(oqparam)) imts = sorted(oqparam.imtls) num_imts = len(imts) imt_dt = numpy.dtype([(imt, F32) for imt in imts]) gmfset = nrml.read(fname).gmfCollection.gmfSet eids, sitecounts = _extract_eids_sitecounts(gmfset) coords = sorted(sitecounts) oqparam.sites = [(lon, lat, 0) for lon, lat in coords] site_idx = {lonlat: i for i, lonlat in enumerate(coords)} oqparam.number_of_ground_motion_fields = num_events = len(eids) sitecol = get_site_collection(oqparam) num_sites = len(oqparam.sites) gmf_by_imt = numpy.zeros((num_events, num_sites), imt_dt) counts = collections.Counter() for i, gmf in enumerate(gmfset): if len(gmf) != num_sites: # there must be one node per site raise InvalidFile( 'Expected %d sites, got %d nodes in %s, line %d' % (num_sites, len(gmf), fname, gmf.lineno)) counts[gmf['ruptureId']] += 1 imt = gmf['IMT'] if imt == 'SA': imt = 'SA(%s)' % gmf['saPeriod'] for node in gmf: sid = site_idx[node['lon'], node['lat']] gmf_by_imt[imt][i % num_events, sid] = node['gmv'] for etag, count in sorted(counts.items()): if count < num_imts: raise InvalidFile("Found a missing etag '%s' in %s" % (etag, fname)) elif count > num_imts: raise InvalidFile("Found a duplicated etag '%s' in %s" % (etag, fname)) expected_gmvs_per_site = num_imts * len(eids) for lonlat, counts in sitecounts.items(): if counts != expected_gmvs_per_site: raise InvalidFile('%s: expected %d gmvs at location %s, found %d' % (fname, expected_gmvs_per_site, lonlat, counts)) return sitecol, eids, gmf_by_imt.T
def get_scenario_from_nrml(oqparam, fname): """ :param oqparam: an :class:`openquake.commonlib.oqvalidation.OqParam` instance :param fname: the NRML files containing the GMFs :returns: a triple (sitecol, etags, gmf array) """ if not oqparam.imtls: oqparam.set_risk_imtls(get_risk_models(oqparam)) imts = sorted(oqparam.imtls) num_imts = len(imts) imt_dt = numpy.dtype([(imt, F32) for imt in imts]) gmfset = nrml.read(fname).gmfCollection.gmfSet etags, sitecounts = _extract_etags_sitecounts(gmfset) oqparam.sites = sorted(sitecounts) site_idx = {lonlat: i for i, lonlat in enumerate(oqparam.sites)} oqparam.number_of_ground_motion_fields = num_events = len(etags) sitecol = get_site_collection(oqparam) num_sites = len(oqparam.sites) gmf_by_imt = numpy.zeros((num_events, num_sites), imt_dt) counts = collections.Counter() for i, gmf in enumerate(gmfset): if len(gmf) != num_sites: # there must be one node per site raise InvalidFile('Expected %d sites, got %d nodes in %s, line %d' % (num_sites, len(gmf), fname, gmf.lineno)) counts[gmf['ruptureId']] += 1 imt = gmf['IMT'] if imt == 'SA': imt = 'SA(%s)' % gmf['saPeriod'] for node in gmf: sid = site_idx[node['lon'], node['lat']] gmf_by_imt[imt][i % num_events, sid] = node['gmv'] for etag, count in sorted(counts.items()): if count < num_imts: raise InvalidFile("Found a missing etag '%s' in %s" % (etag, fname)) elif count > num_imts: raise InvalidFile("Found a duplicated etag '%s' in %s" % (etag, fname)) expected_gmvs_per_site = num_imts * len(etags) for lonlat, counts in sitecounts.items(): if counts != expected_gmvs_per_site: raise InvalidFile( '%s: expected %d gmvs at location %s, found %d' % (fname, expected_gmvs_per_site, lonlat, counts)) return sitecol, etags, gmf_by_imt.T
def pre_execute(self): if 'gmfs' in self.oqparam.inputs: self.pre_calculator = None base.RiskCalculator.pre_execute(self) self.param['number_of_ground_motion_fields'] = ( self.oqparam.number_of_ground_motion_fields) self.param['consequence_models'] = riskmodels.get_risk_models( self.oqparam, 'consequence') self.datastore['etags'], gmfs = calc.get_gmfs(self.datastore, self.precalc) rlzs = self.csm_info.get_rlzs_assoc().realizations self.riskinputs = self.build_riskinputs( 'gmf', {rlz: gmf for rlz, gmf in zip(rlzs, gmfs)}) self.param['taxonomies'] = sorted(self.taxonomies)
def get_scenario_from_nrml(oqparam, fname): """ :param oqparam: an :class:`openquake.commonlib.oqvalidation.OqParam` instance :param fname: the NRML files containing the GMFs :returns: a triple (sitecol, rupture_tags, gmf array) """ if not oqparam.imtls: oqparam.set_risk_imtls(get_risk_models(oqparam)) imts = list(oqparam.imtls) imt_dt = numpy.dtype([(imt, float) for imt in imts]) gmfset = nrml.read(fname).gmfCollection.gmfSet tags, oqparam.sites = _extract_tags_sites(gmfset) oqparam.number_of_ground_motion_fields = num_events = len(tags) sitecol = get_site_collection(oqparam) num_sites = len(oqparam.sites) gmf_by_imt = numpy.zeros((num_events, num_sites), imt_dt) num_imts = len(imts) counts = collections.Counter() for i, gmf in enumerate(gmfset): if len(gmf) != num_sites: # there must be one node per site raise InvalidFile('Expected %d sites, got %d in %s, line %d' % ( num_sites, len(gmf), fname, gmf.lineno)) counts[gmf['ruptureId']] += 1 imt = gmf['IMT'] if imt == 'SA': imt = 'SA(%s)' % gmf['saPeriod'] for site_idx, lon, lat, node in zip( range(num_sites), sitecol.lons, sitecol.lats, gmf): if (node['lon'], node['lat']) != (lon, lat): raise InvalidFile('The site mesh is not ordered in %s, line %d' % (fname, node.lineno)) try: gmf_by_imt[imt][i % num_events, site_idx] = node['gmv'] except IndexError: raise InvalidFile('Something wrong in %s, line %d' % (fname, node.lineno)) for tag, count in counts.items(): if count < num_imts: raise InvalidFile('Found a missing tag %r in %s' % (tag, fname)) elif count > num_imts: raise InvalidFile('Found a duplicated tag %r in %s' % (tag, fname)) return sitecol, tags, gmf_by_imt.T
def get_hcurves_from_csv(oqparam, fname): """ :param oqparam: an :class:`openquake.commonlib.oqvalidation.OqParam` instance :param fname: a .txt file with format `IMT lon lat poe1 ... poeN` :returns: the site collection and the hazard curves read by the .txt file """ if not oqparam.imtls: oqparam.set_risk_imtls(get_risk_models(oqparam)) if not oqparam.imtls: raise ValueError("Missing intensity_measure_types_and_levels in %s" % oqparam.inputs["job_ini"]) num_values = list(map(len, list(oqparam.imtls.values()))) with open(oqparam.inputs["hazard_curves"]) as csvfile: mesh, hcurves_by_imt = get_mesh_csvdata( csvfile, list(oqparam.imtls), num_values, valid.decreasing_probabilities ) sitecol = get_site_collection(oqparam, mesh) return sitecol, hcurves_by_imt
def get_hcurves_from_csv(oqparam, fname): """ :param oqparam: an :class:`openquake.commonlib.oqvalidation.OqParam` instance :param fname: a .txt file with format `IMT lon lat poe1 ... poeN` :returns: the site collection and the hazard curves read by the .txt file """ if not oqparam.imtls: oqparam.set_risk_imtls(get_risk_models(oqparam)) if not oqparam.imtls: raise ValueError('Missing intensity_measure_types_and_levels in %s' % oqparam.inputs['job_ini']) num_values = list(map(len, list(oqparam.imtls.values()))) with open(oqparam.inputs['hazard_curves']) as csvfile: mesh, hcurves_by_imt = get_mesh_csvdata(csvfile, list(oqparam.imtls), num_values, valid.decreasing_probabilities) sitecol = get_site_collection(oqparam, mesh) return sitecol, hcurves_by_imt
def get_pmap_from_csv(oqparam, fname): """ :param oqparam: an :class:`openquake.commonlib.oqvalidation.OqParam` instance :param fname: a .txt file with format `IMT lon lat poe1 ... poeN` :returns: the site mesh and the hazard curves read by the .txt file """ if not oqparam.imtls: oqparam.set_risk_imtls(get_risk_models(oqparam)) if not oqparam.imtls: raise ValueError('Missing intensity_measure_types_and_levels in %s' % oqparam.inputs['job_ini']) num_values = list(map(len, list(oqparam.imtls.values()))) with open(oqparam.inputs['hazard_curves']) as csvfile: mesh, hcurves = get_mesh_csvdata(csvfile, list(oqparam.imtls), num_values, valid.decreasing_probabilities) array = numpy.zeros((len(mesh), sum(num_values))) for imt_ in hcurves: array[:, oqparam.imtls.slicedic[imt_]] = hcurves[imt_] return mesh, ProbabilityMap.from_array(array, range(len(mesh)))
def load_riskmodel(self): """ Read the risk model and set the attribute .riskmodel. The riskmodel can be empty for hazard calculations. Save the loss ratios (if any) in the datastore. """ rmdict = riskmodels.get_risk_models(self.oqparam) if not rmdict: # can happen only in a hazard calculation return self.oqparam.set_risk_imtls(rmdict) self.save_params() # re-save oqparam self.riskmodel = rm = readinput.get_risk_model(self.oqparam, rmdict) if 'taxonomies' in self.datastore: # check that we are covering all the taxonomies in the exposure missing = set(self.taxonomies) - set(rm.taxonomies) if rm and missing: raise RuntimeError('The exposure contains the taxonomies %s ' 'which are not in the risk model' % missing) # save the risk models and loss_ratios in the datastore for taxonomy, rmodel in rm.items(): self.datastore['composite_risk_model/' + taxonomy] = ( rmodel.risk_functions) if hasattr(rmodel, 'retro_functions'): self.datastore[ 'composite_risk_model/%s-retrofitted' % taxonomy] = ( rmodel.retro_functions) attrs = self.datastore['composite_risk_model'].attrs attrs['loss_types'] = hdf5.array_of_vstr(rm.loss_types) attrs['min_iml'] = hdf5.array_of_vstr(sorted(rm.get_min_iml().items())) if rm.damage_states: attrs['damage_states'] = hdf5.array_of_vstr(rm.damage_states) self.datastore['loss_ratios'] = rm.get_loss_ratios() self.datastore.set_nbytes('composite_risk_model') self.datastore.set_nbytes('loss_ratios') self.datastore.hdf5.flush()