def __toh5__(self): loss_types = hdf5.array_of_vstr(self._get_loss_types()) limit_states = hdf5.array_of_vstr( self.damage_states[1:] if self.damage_states else []) return self._riskmodels, dict(covs=self.covs, loss_types=loss_types, limit_states=limit_states)
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 __toh5__(self): loss_types = hdf5.array_of_vstr(self.loss_types) limit_states = hdf5.array_of_vstr(self.damage_states[1:] if self.damage_states else []) dic = dict(covs=self.covs, loss_types=loss_types, limit_states=limit_states) rf = next(iter(self.values())) if hasattr(rf, 'loss_ratios'): for lt in self.loss_types: dic['loss_ratios_' + lt] = rf.loss_ratios[lt] return self._riskmodels, dic
def __toh5__(self): attrs = { "time_event": self.time_event or "None", "time_events": self.time_events, "loss_types": hdf5.array_of_vstr(self.loss_types), "deduc": hdf5.array_of_vstr(self.deduc), "i_lim": hdf5.array_of_vstr(self.i_lim), "retro": hdf5.array_of_vstr(self.retro), "nbytes": self.array.nbytes, } return dict(array=self.array, taxonomies=self.taxonomies, cost_calculator=self.cc), attrs
def get_attrs(self): loss_types = hdf5.array_of_vstr(self.loss_types) limit_states = hdf5.array_of_vstr(self.damage_states[1:] if self.damage_states else []) attrs = dict(covs=self.covs, loss_types=loss_types, limit_states=limit_states, tmap=repr(getattr(self, 'tmap', []))) rf = next(iter(self.values())) if hasattr(rf, 'loss_ratios'): for lt in self.loss_types: attrs['loss_ratios_' + lt] = rf.loss_ratios[lt] return attrs
def __toh5__(self): loss_types = hdf5.array_of_vstr(self.loss_types) limit_states = hdf5.array_of_vstr(self.damage_states[1:] if self.damage_states else []) attrs = dict(covs=self.covs, loss_types=loss_types, limit_states=limit_states, tmap=repr(getattr(self, 'tmap', []))) rf = next(iter(self.values())) if hasattr(rf, 'loss_ratios'): for lt in self.loss_types: attrs['loss_ratios_' + lt] = rf.loss_ratios[lt] dic = self._riskmodels.copy() for k, v in self.cons_model.items(): if len(v): dic[k] = v return dic, attrs
def assoc_assets_sites(self, sitecol): """ :param sitecol: a sequence of sites :returns: a pair (filtered sites, asset collection) The new site collection is different from the original one if some assets were discarded or if there were missing assets for some sites. """ maximum_distance = self.oqparam.asset_hazard_distance siteobjects = geo.utils.GeographicObjects( Site(sid, lon, lat) for sid, lon, lat in zip(sitecol.sids, sitecol.lons, sitecol.lats)) assets_by_sid = general.AccumDict() for assets in self.assetcol.assets_by_site(): if len(assets): lon, lat = assets[0].location site, _ = siteobjects.get_closest(lon, lat, maximum_distance) if site: assets_by_sid += {site.sid: list(assets)} if not assets_by_sid: raise AssetSiteAssociationError( 'Could not associate any site to any assets within the ' 'maximum distance of %s km' % maximum_distance) mask = numpy.array([sid in assets_by_sid for sid in sitecol.sids]) assets_by_site = [assets_by_sid.get(sid, []) for sid in sitecol.sids] return sitecol.filter(mask), riskinput.AssetCollection( assets_by_site, self.exposure.cost_calculator, self.oqparam.time_event, time_events=hdf5.array_of_vstr(sorted(self.exposure.time_events)))
def __init__(self, assets_by_site, cost_calculator, time_event, time_events=''): self.cc = cost_calculator self.time_event = time_event self.time_events = hdf5.array_of_vstr(time_events) self.array, self.taxonomies = self.build_asset_collection( assets_by_site, time_event) fields = self.array.dtype.names self.loss_types = hdf5.array_of_vstr( sorted(f for f in fields if not f.startswith(FIELDS))) self.deduc = hdf5.array_of_vstr( n for n in fields if n.startswith('deductible-')) self.i_lim = hdf5.array_of_vstr( n for n in fields if n.startswith('insurance_limit-')) self.retro = hdf5.array_of_vstr( n for n in fields if n.startswith('retrofitted-'))
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'): self.sitecol, self.assets_by_site = \ 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)) # asset collection if hasattr(self, 'assets_by_site'): self.assetcol = riskinput.AssetCollection( self.assets_by_site, self.cost_calculator, oq.time_event, time_events=hdf5.array_of_vstr( sorted(self.exposure.time_events))) elif hasattr(self, '_assetcol'): self.assets_by_site = self.assetcol.assets_by_site() if self.oqparam.job_type == 'risk': # check that we are covering all the taxonomies in the exposure missing = set(self.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)
def save_riskmodel(self): """ Save the risk models in the datastore """ self.datastore['risk_model'] = rm = self.riskmodel attrs = self.datastore.getitem('risk_model').attrs attrs['min_iml'] = hdf5.array_of_vstr(sorted(rm.min_iml.items())) self.datastore.set_nbytes('risk_model')
def save_crmodel(self): """ Save the risk models in the datastore """ if len(self.crmodel): self.datastore['risk_model'] = rm = self.crmodel attrs = self.datastore.getitem('risk_model').attrs attrs['min_iml'] = hdf5.array_of_vstr(sorted(rm.min_iml.items()))
def save_disagg_result(self, site_id, bin_edges, trt_names, matrix, rlz_id, investigation_time, imt_str, iml, poe): """ Save a computed disaggregation matrix to `hzrdr.disagg_result` (see :class:`~openquake.engine.db.models.DisaggResult`). :param site_id: id of the current site :param bin_edges: The 5-uple mag, dist, lon, lat, eps :param trt_names: The list of Tectonic Region Types :param matrix: A probability array :param rlz_id: ordinal of the realization to which the results belong. :param float investigation_time: Investigation time (years) for the calculation. :param imt_str: Intensity measure type string (PGA, SA, etc.) :param float iml: Intensity measure level interpolated (using `poe`) from the hazard curve at the `site`. :param float poe: Disaggregation probability of exceedance value for this result. """ lon = self.sitecol.lons[site_id] lat = self.sitecol.lats[site_id] mag, dist, lons, lats, eps = bin_edges disp_name = DISAGG_RES_FMT % dict( poe='' if poe is None else 'poe-%s-' % poe, rlz=rlz_id, imt=imt_str, lon=lon, lat=lat) self.datastore[disp_name] = dic = { '_'.join(key): mat for key, mat in zip(disagg.pmf_map, matrix) } attrs = self.datastore.hdf5[disp_name].attrs attrs['rlzi'] = rlz_id attrs['imt'] = imt_str attrs['iml'] = iml attrs['trts'] = hdf5.array_of_vstr(trt_names) attrs['mag_bin_edges'] = mag attrs['dist_bin_edges'] = dist attrs['lon_bin_edges'] = lons attrs['lat_bin_edges'] = lats attrs['eps_bin_edges'] = eps attrs['location'] = (lon, lat) if poe is not None: attrs['poe'] = poe # sanity check: all poe_agg should be the same attrs['poe_agg'] = [ 1. - numpy.prod(1. - dic[pmf]) for pmf in sorted(dic) ]
def __init__(self, assets_by_site, cost_calculator, time_event, time_events=""): self.cc = cost_calculator self.time_event = time_event self.time_events = hdf5.array_of_vstr(time_events) self.array, self.taxonomies = self.build_asset_collection(assets_by_site, time_event) fields = self.array.dtype.names self.loss_types = sorted(f[6:] for f in fields if f.startswith("value-")) self.deduc = [n for n in fields if n.startswith("deductible-")] self.i_lim = [n for n in fields if n.startswith("insurance_limit-")] self.retro = [n for n in fields if n.startswith("retrofitted-")]
def __toh5__(self): loss_types = sorted(self.cost_types) dt = numpy.dtype([('cost_type', hdf5.vstr), ('area_type', hdf5.vstr), ('unit', hdf5.vstr)]) array = numpy.zeros(len(loss_types), dt) array['cost_type'] = [self.cost_types[lt] for lt in loss_types] array['area_type'] = [self.area_types[lt] for lt in loss_types] array['unit'] = [self.units[lt] for lt in loss_types] attrs = dict(loss_types=hdf5.array_of_vstr(loss_types)) return array, attrs
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 __toh5__(self): sm_data = [] for sm in self.sm_rlzs: sm_data.append((sm.value, sm.weight, '~'.join(sm.lt_path), sm.samples)) return (dict( source_model_lt=self.source_model_lt, gsim_lt=self.gsim_lt, sm_data=numpy.array(sm_data, source_model_dt)), dict(seed=self.seed, num_samples=self.num_samples, trts=hdf5.array_of_vstr(self.gsim_lt.values)))
def __toh5__(self): # save csm_info/sm_data in the datastore trti = self.trt2i() sm_data = [] for sm in self.source_models: sm_data.append((sm.names, sm.weight, '_'.join(sm.path), sm.samples, sm.offset)) return (dict(gsim_lt=self.gsim_lt, sm_data=numpy.array(sm_data, source_model_dt)), dict(seed=self.seed, num_samples=self.num_samples, trts=hdf5.array_of_vstr(sorted(trti))))
def read_exposure(self, haz_sitecol=None): """ Read the exposure, the riskmodel and update the attributes .exposure, .sitecol, .assetcol """ logging.info('Reading the exposure') with self.monitor('reading exposure', autoflush=True): self.exposure = readinput.get_exposure(self.oqparam) mesh, assets_by_site = (readinput.get_mesh_assets_by_site( self.oqparam, self.exposure)) if haz_sitecol: tot_assets = sum(len(assets) for assets in assets_by_site) all_sids = haz_sitecol.complete.sids sids = set(haz_sitecol.sids) # associate the assets to the hazard sites asset_hazard_distance = self.oqparam.asset_hazard_distance siteobjects = geo.utils.GeographicObjects( Site(sid, lon, lat) for sid, lon, lat in zip( haz_sitecol.sids, haz_sitecol.lons, haz_sitecol.lats)) assets_by_sid = general.AccumDict(accum=[]) for assets in assets_by_site: if len(assets): lon, lat = assets[0].location site, distance = siteobjects.get_closest(lon, lat) if site.sid in sids and distance <= asset_hazard_distance: # keep the assets, otherwise discard them assets_by_sid += {site.sid: list(assets)} if not assets_by_sid: raise AssetSiteAssociationError( 'Could not associate any site to any assets within the ' 'asset_hazard_distance of %s km' % asset_hazard_distance) mask = numpy.array([sid in assets_by_sid for sid in all_sids]) assets_by_site = [assets_by_sid[sid] for sid in all_sids] num_assets = sum(len(assets) for assets in assets_by_site) logging.info('Associated %d/%d assets to the hazard sites', num_assets, tot_assets) self.sitecol = haz_sitecol.complete.filter(mask) else: # use the exposure sites as hazard sites self.sitecol = readinput.get_site_collection(self.oqparam, mesh) self.assetcol = asset.AssetCollection( self.exposure.asset_refs, assets_by_site, self.exposure.tagcol, self.exposure.cost_calculator, self.oqparam.time_event, occupancy_periods=hdf5.array_of_vstr( sorted(self.exposure.occupancy_periods))) logging.info('Considering %d assets on %d sites', len(self.assetcol), len(self.sitecol))
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()
def store_curves(self, kind, curves, rlz=None): """ Store all kind of curves, optionally computing maps and uhs curves. :param kind: the kind of curves to store :param curves: an array of N curves to store :param rlz: hazard realization, if any """ oq = self.oqparam self._store('hcurves/' + kind, curves, rlz, nbytes=curves.nbytes) self.datastore['hcurves'].attrs['imtls'] = hdf5.array_of_vstr([ (imt, len(imls)) for imt, imls in self.oqparam.imtls.items()]) if oq.hazard_maps or oq.uniform_hazard_spectra: # hmaps is a composite array of shape (N, P) hmaps = self.hazard_maps(curves) self._store('hmaps/' + kind, hmaps, rlz, poes=oq.poes, nbytes=hmaps.nbytes)
def save_disagg_result(self, site_id, bin_edges, trt_names, matrix, rlz_id, investigation_time, imt_str, iml, poe): """ Save a computed disaggregation matrix to `hzrdr.disagg_result` (see :class:`~openquake.engine.db.models.DisaggResult`). :param site_id: id of the current site :param bin_edges: The 5-uple mag, dist, lon, lat, eps :param trt_names: The list of Tectonic Region Types :param matrix: A probability array :param rlz_id: ordinal of the realization to which the results belong. :param float investigation_time: Investigation time (years) for the calculation. :param imt_str: Intensity measure type string (PGA, SA, etc.) :param float iml: Intensity measure level interpolated (using `poe`) from the hazard curve at the `site`. :param float poe: Disaggregation probability of exceedance value for this result. """ lon = self.sitecol.lons[site_id] lat = self.sitecol.lats[site_id] mag, dist, lons, lats, eps = bin_edges disp_name = DISAGG_RES_FMT % dict( poe=poe, rlz=rlz_id, imt=imt_str, lon=lon, lat=lat) self.datastore[disp_name] = matrix attrs = self.datastore.hdf5[disp_name].attrs attrs['rlzi'] = rlz_id attrs['imt'] = imt_str attrs['iml'] = iml attrs['poe'] = poe attrs['trts'] = hdf5.array_of_vstr(trt_names) attrs['mag_bin_edges'] = mag attrs['dist_bin_edges'] = dist attrs['lon_bin_edges'] = lons attrs['lat_bin_edges'] = lats attrs['eps_bin_edges'] = eps attrs['location'] = (lon, lat)
def __toh5__(self): data = [] trti = self.trt2i() for sm in self.source_models: for src_group in sm.src_groups: # the number of effective realizations is set by get_rlzs_assoc data.append( (src_group.id, trti[src_group.trt], src_group.eff_ruptures, src_group.tot_ruptures, sm.ordinal)) lst = [(sm.names, sm.weight, '_'.join(sm.path), sm.num_gsim_paths, sm.samples) for i, sm in enumerate(self.source_models)] return (dict(sg_data=numpy.array(data, src_group_dt), sm_data=numpy.array(lst, source_model_dt)), dict(seed=self.seed, num_samples=self.num_samples, trts=hdf5.array_of_vstr(sorted(trti)), gsim_lt_xml=str(self.gsim_lt), gsim_fname=self.gsim_lt.fname, tot_weight=self.tot_weight))
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 get_sitecol_assetcol(oqparam, exposure): """ :param oqparam: an :class:`openquake.commonlib.oqvalidation.OqParam` instance :returns: the site collection and the asset collection """ assets_by_loc = groupby(exposure.assets, key=lambda a: a.location) lons, lats = zip(*sorted(assets_by_loc)) mesh = geo.Mesh(numpy.array(lons), numpy.array(lats)) sitecol = get_site_collection(oqparam, mesh) assets_by_site = [] for lonlat in zip(sitecol.lons, sitecol.lats): assets = assets_by_loc[lonlat] assets_by_site.append(sorted(assets, key=operator.attrgetter('idx'))) assetcol = riskinput.AssetCollection( assets_by_site, exposure.assets_by_tag, exposure.cost_calculator, oqparam.time_event, time_events=hdf5.array_of_vstr( sorted(exposure.time_events))) return sitecol, assetcol
def __toh5__(self): trts = sorted(set(src_group.trt for sm in self.source_models for src_group in sm.src_groups)) trti = {trt: i for i, trt in enumerate(trts)} data = [] for sm in self.source_models: for src_group in sm.src_groups: # the number of effective realizations is set by get_rlzs_assoc data.append((src_group.id, trti[src_group.trt], src_group.eff_ruptures, sm.ordinal)) lst = [(sm.name, sm.weight, '_'.join(sm.path), sm.num_gsim_paths, sm.samples) for i, sm in enumerate(self.source_models)] return (dict( sg_data=numpy.array(data, src_group_dt), sm_data=numpy.array(lst, source_model_dt)), dict(seed=self.seed, num_samples=self.num_samples, trts=hdf5.array_of_vstr(trts), gsim_lt_xml=str(self.gsim_lt), gsim_fname=self.gsim_lt.fname))
def __toh5__(self): # save csm_info/sg_data, csm_info/sm_data in the datastore trti = self.trt2i() sg_data = [] sm_data = [] for sm in self.source_models: trts = set(sg.trt for sg in sm.src_groups) num_gsim_paths = self.gsim_lt.reduce(trts).get_num_paths() sm_data.append((sm.names, sm.weight, '_'.join(sm.path), num_gsim_paths, sm.samples)) for src_group in sm.src_groups: sg_data.append((src_group.id, src_group.name, trti[src_group.trt], src_group.eff_ruptures, src_group.tot_ruptures, sm.ordinal)) return (dict( gsim_lt=self.gsim_lt, sg_data=numpy.array(sg_data, src_group_dt), sm_data=numpy.array(sm_data, source_model_dt)), dict(seed=self.seed, num_samples=self.num_samples, trts=hdf5.array_of_vstr(sorted(trti)), tot_weight=self.tot_weight))
def __toh5__(self): # save csm_info/sg_data, csm_info/sm_data in the datastore trti = self.trt2i() sg_data = [] sm_data = [] for sm in self.source_models: trts = set(sg.trt for sg in sm.src_groups) num_gsim_paths = self.gsim_lt.reduce(trts).get_num_paths() sm_data.append((sm.names, sm.weight, '_'.join(sm.path), num_gsim_paths, sm.samples)) for src_group in sm.src_groups: sg_data.append((src_group.id, src_group.name, trti[src_group.trt], src_group.eff_ruptures, src_group.tot_ruptures, sm.ordinal)) return (dict( gsim_lt=self.gsim_lt, sg_data=numpy.array(sg_data, src_group_dt), sm_data=numpy.array(sm_data, source_model_dt)), dict(seed=self.seed, num_samples=self.num_samples, trts=hdf5.array_of_vstr(sorted(trti)), min_mag=self.min_mag, max_mag=self.max_mag))
def __toh5__(self): loss_types = hdf5.array_of_vstr(self._get_loss_types()) return self._riskmodels, dict(covs=self.covs, loss_types=loss_types)