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 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 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 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 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 pre_execute(self): """ Set the attributes .riskmodel, .sitecol, .assets_by_site """ HazardCalculator.pre_execute(self) self.riskmodel = readinput.get_risk_model(self.oqparam) if hasattr(self, 'exposure'): missing = self.exposure.taxonomies - set(self.riskmodel.taxonomies) if missing: raise RuntimeError('The exposure contains the taxonomies %s ' 'which are not in the risk model' % missing)
def read_config_file(cfg): gmf_file = cfg['input']['gmf_file'] gmf_file_gmpe_rate = cfg['input']['gmf_file_gmpe_rate'] job_ini = cfg['input']['job_ini'] oq_param = get_oqparam(job_ini) get_risk_model(oq_param) # read risk functions and set imtls haz_sitecol = get_site_collection(oq_param) sites, assets_by_site, _ = get_sitecol_assetcol(oq_param, haz_sitecol) gsimlt = get_gsim_lt(oq_param) gsim_list = [br.uncertainty for br in gsimlt.branches] cinfo = source.CompositionInfo.fake(gsimlt) mean_shift_inter_residuals = float( cfg['input']['mean_shift_inter_residuals']) realizations_inter = int(cfg['input']['realizations_inter']) realizations_intra = int(cfg['input']['realizations_intra']) intra_files_name = cfg['input']['intra_files_name'] intra_files = cfg['input']['intra_files'].split() csv_rate_gmf_file = cfg['output']['csv_rate_gmf_file'] seed = int(cfg['input']['seed']) return (gmf_file, gmf_file_gmpe_rate, sites, gsim_list, cinfo, oq_param, mean_shift_inter_residuals, realizations_inter, realizations_intra, intra_files_name, intra_files, csv_rate_gmf_file, seed)
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. """ logging.info('Reading the risk model if present') self.riskmodel = readinput.get_risk_model(self.oqparam) if not self.riskmodel: parent = self.datastore.parent if 'fragility' in parent or 'vulnerability' in parent: self.riskmodel = riskinput.read_composite_risk_model(parent) return self.save_params() # re-save oqparam
def get_risk_model(self): """ Load fragility model and store damage states """ risk_model = get_risk_model(self.oqparam) for lsi, dstate in enumerate(risk_model.damage_states): models.DmgState.objects.get_or_create( risk_calculation=self.job, dmg_state=dstate, lsi=lsi) damage_state_ids = [d.id for d in models.DmgState.objects.filter( risk_calculation=self.job).order_by('lsi')] self.oqparam.damage_state_ids = damage_state_ids self.loss_types.add('damage') # single loss_type return risk_model
def pre_execute(self): """ Set the attributes .riskmodel, .sitecol, .assets_by_site """ self.riskmodel = readinput.get_risk_model(self.oqparam) self.exposure = readinput.get_exposure(self.oqparam) logging.info('Read an exposure with %d assets of %d taxonomies', len(self.exposure.assets), len(self.exposure.taxonomies)) missing = self.exposure.taxonomies - set( self.riskmodel.get_taxonomies()) if missing: raise RuntimeError('The exposure contains the taxonomies %s ' 'which are not in the risk model' % missing) self.sitecol, self.assets_by_site = readinput.get_sitecol_assets( self.oqparam, self.exposure) logging.info('Extracted %d unique sites from the exposure', len(self.sitecol))
def load_riskmodel(self): # to be called before read_exposure # NB: this is called even if there is no risk model """ 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. """ logging.info('Reading the risk model if present') self.riskmodel = readinput.get_risk_model(self.oqparam) if not self.riskmodel: parent = self.datastore.parent if 'risk_model' in parent: self.riskmodel = riskinput.CompositeRiskModel.read(parent) return if self.oqparam.ground_motion_fields and not self.oqparam.imtls: raise InvalidFile('No intensity_measure_types specified in %s' % self.oqparam.inputs['job_ini']) self.save_params() # re-save oqparam
def load_riskmodel(self): # to be called before read_exposure # NB: this is called even if there is no risk model """ 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. """ logging.info('Reading the risk model if present') self.riskmodel = readinput.get_risk_model(self.oqparam) if not self.riskmodel: parent = self.datastore.parent if 'fragility' in parent or 'vulnerability' in parent: self.riskmodel = riskinput.read_composite_risk_model(parent) return if self.oqparam.ground_motion_fields and not self.oqparam.imtls: raise InvalidFile('No intensity_measure_types specified in %s' % self.oqparam.inputs['job_ini']) self.save_params() # re-save oqparam
def get_risk_model(self): """ Load fragility model and store damage states """ risk_model = get_risk_model(self.oqparam) for lsi, dstate in enumerate(risk_model.damage_states): models.DmgState.objects.get_or_create(risk_calculation=self.job, dmg_state=dstate, lsi=lsi) damage_state_ids = [ d.id for d in models.DmgState.objects.filter( risk_calculation=self.job).order_by('lsi') ] self.oqparam.damage_state_ids = damage_state_ids self.loss_types.add('damage') # single loss_type return risk_model
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. """ self.riskmodel = rm = readinput.get_risk_model(self.oqparam) if not self.riskmodel: # can happen only in a hazard calculation return self.save_params() # re-save oqparam # save the risk models and loss_ratios in the datastore self.datastore['composite_risk_model'] = rm attrs = self.datastore.getitem('composite_risk_model').attrs attrs['min_iml'] = hdf5.array_of_vstr(sorted(rm.get_min_iml().items())) if rm.damage_states: # best not to save them as bytes, they are used as headers attrs['damage_states'] = hdf5.array_of_vstr(rm.damage_states) self.datastore.set_nbytes('composite_risk_model') 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. """ logging.info('Reading the risk model if present') self.riskmodel = rm = readinput.get_risk_model(self.oqparam) if not self.riskmodel: parent = self.datastore.parent if 'composite_risk_model' in parent: self.riskmodel = riskinput.read_composite_risk_model(parent) return self.save_params() # re-save oqparam # save the risk models and loss_ratios in the datastore self.datastore['composite_risk_model'] = rm attrs = self.datastore.getitem('composite_risk_model').attrs attrs['min_iml'] = hdf5.array_of_vstr(sorted(rm.get_min_iml().items())) self.datastore.set_nbytes('composite_risk_model') 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'] = 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()
# GNU General Public License for more details. # You should have received a copy of the GNU Affero General Public License # along with OpenQuake. If not, see <http://www.gnu.org/licenses/>. """ This is an example of script which is useful if you want to play with the RiskInput objects. You can enable the pdb and see what is inside the objects. """ from __future__ import print_function import sys from openquake.commonlib import readinput from openquake.calculators.calc import calc_gmfs if __name__ == '__main__': job_ini = sys.argv[1:] o = readinput.get_oqparam(job_ini) exposure = readinput.get_exposure(o) sitecol, assets_by_site = readinput.get_sitecol_assets(o, exposure) risk_model = readinput.get_risk_model(o) gmfs_by_imt = calc_gmfs(o, sitecol) for imt in gmfs_by_imt: ri = risk_model.build_input(imt, gmfs_by_imt[imt], assets_by_site) print(ri) #for out in risk_model.gen_outputs([ri]): # print out # import pdb; pdb.set_trace()
def test_missing_vulnerability_function(self): oq = readinput.get_oqparam('job.ini', case_caracas) with self.assertRaises(ValidationError): readinput.get_risk_model(oq)
def get_risk_model(self): """ :returns: a :class:`openquake.risklib.riskinput.RiskModel` dictionary """ return get_risk_model(self.oqparam)
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # You should have received a copy of the GNU Affero General Public License # along with OpenQuake. If not, see <http://www.gnu.org/licenses/>. """ This is an example of script which is useful if you want to play with the RiskInput objects. You can enable the pdb and see what is inside the objects. """ from __future__ import print_function import sys from openquake.commonlib import readinput from openquake.commonlib.calculators.calc import calc_gmfs if __name__ == '__main__': job_ini = sys.argv[1:] o = readinput.get_oqparam(job_ini) exposure = readinput.get_exposure(o) sitecol, assets_by_site = readinput.get_sitecol_assets(o, exposure) risk_model = readinput.get_risk_model(o) gmfs_by_imt = calc_gmfs(o, sitecol) for imt in gmfs_by_imt: ri = risk_model.build_input(imt, gmfs_by_imt[imt], assets_by_site) print(ri) #for out in risk_model.gen_outputs([ri]): # print out # import pdb; pdb.set_trace()
def setUpClass(cls): cls.oqparam = readinput.get_oqparam( 'job_haz.ini,job_risk.ini', pkg=case_2) cls.sitecol, cls.assets_by_site = readinput.get_sitecol_assets( cls.oqparam, readinput.get_exposure(cls.oqparam)) cls.riskmodel = readinput.get_risk_model(cls.oqparam)