def _make_pmap(ctxs, cmaker, investigation_time): RuptureContext.temporal_occurrence_model = PoissonTOM(investigation_time) # easy case of independent ruptures, useful for debugging pmap = ProbabilityMap(len(cmaker.loglevels.array), len(cmaker.gsims)) for ctx, poes in cmaker.gen_ctx_poes(ctxs): pnes = ctx.get_probability_no_exceedance(poes) # (N, L, G) for sid, pne in zip(ctx.sids, pnes): pmap.setdefault(sid, 1.).array *= pne return ~pmap
def make(self, src, sites, pmap, rup_data): """ :param src: a hazardlib source :param sites: the sites affected by it :returns: the probability map generated by the source """ with self.cmaker.mon('iter_ruptures', measuremem=False): self.mag_rups = [(mag, list(rups)) for mag, rups in itertools.groupby( src.iter_ruptures(shift_hypo=self.shift_hypo), key=operator.attrgetter('mag'))] rupdata = RupData(self.cmaker) totrups, numrups, nsites = 0, 0, 0 L, G = len(self.imtls.array), len(self.gsims) poemap = ProbabilityMap(L, G) for rups, sites in self._gen_rups_sites(src, sites): with self.ctx_mon: ctxs = self.cmaker.make_ctxs(rups, sites) if ctxs: totrups += len(ctxs) ctxs = self.collapse(ctxs) numrups += len(ctxs) for rup, r_sites, dctx in ctxs: if self.fewsites: # store rupdata rupdata.add(rup, r_sites, dctx) sids, poes = self._sids_poes(rup, r_sites, dctx, src.id) with self.pne_mon: pnes = rup.get_probability_no_exceedance(poes) if self.rup_indep: for sid, pne in zip(sids, pnes): poemap.setdefault(sid, self.rup_indep).array *= pne else: for sid, pne in zip(sids, pnes): poemap.setdefault( sid, self.rup_indep).array += (1. - pne) * rup.weight nsites += len(sids) poemap.totrups = totrups poemap.numrups = numrups poemap.nsites = nsites self._update(pmap, poemap, src) if len(rupdata.data): for gid in src.src_group_ids: rup_data['grp_id'].extend([gid] * numrups) for k, v in rupdata.data.items(): rup_data[k].extend(v) return poemap
def make_pmap(ctxs, gsims, imtls, trunclevel, investigation_time): RuptureContext.temporal_occurrence_model = PoissonTOM(investigation_time) # easy case of independent ruptures, useful for debugging imts = [from_string(im) for im in imtls] loglevels = DictArray(imtls) for imt, imls in imtls.items(): if imt != 'MMI': loglevels[imt] = numpy.log(imls) pmap = ProbabilityMap(len(loglevels.array), len(gsims)) for ctx in ctxs: mean_std = ctx.get_mean_std(imts, gsims) # shape (2, N, M, G) poes = base.get_poes(mean_std, loglevels, trunclevel, gsims, None, ctx.mag, None, ctx.rrup) # (N, L, G) pnes = ctx.get_probability_no_exceedance(poes) for sid, pne in zip(ctx.sids, pnes): pmap.setdefault(sid, 1.).array *= pne return ~pmap
def get_pmap(self, src, s_sites, rup_indep=True): """ :param src: a hazardlib source :param s_sites: the sites affected by it :returns: the probability map generated by the source """ imts = self.imts sitecol = s_sites.complete N, M = len(sitecol), len(imts) fewsites = N <= self.max_sites_disagg rupdata = RupData(self) nrups, nsites = 0, 0 L, G = len(self.imtls.array), len(self.gsims) poemap = ProbabilityMap(L, G) for rup, sites in self._gen_rup_sites(src, s_sites): try: with self.ctx_mon: r_sites, dctx = self.make_contexts(sites, rup) except FarAwayRupture: continue with self.gmf_mon: mean_std = numpy.zeros((2, len(r_sites), M, G)) for g, gsim in enumerate(self.gsims): dctx_ = dctx.roundup(gsim.minimum_distance) mean_std[:, :, :, g] = gsim.get_mean_std(r_sites, rup, dctx_, imts) with self.poe_mon: pairs = zip(r_sites.sids, self._make_pnes(rup, mean_std)) with self.pne_mon: if rup_indep: for sid, pne in pairs: poemap.setdefault(sid, rup_indep).array *= pne else: for sid, pne in pairs: poemap.setdefault( sid, rup_indep).array += (1. - pne) * rup.weight nrups += 1 nsites += len(r_sites) if fewsites: # store rupdata rupdata.add(rup, src.id, r_sites, dctx) poemap.nrups = nrups poemap.nsites = nsites poemap.data = rupdata.data return poemap
def get_pmap(self, src, s_sites, rup_indep=True): """ :param src: a hazardlib source :param s_sites: the sites affected by it :returns: the probability map generated by the source """ imts = self.imts fewsites = len(s_sites.complete) <= self.max_sites_disagg rupdata = RupData(self) nrups, nsites = 0, 0 L, G = len(self.imtls.array), len(self.gsims) poemap = ProbabilityMap(L, G) dists = [] for rup, sites, maxdist in self._gen_rup_sites(src, s_sites): if maxdist is not None: dists.append(maxdist) try: with self.ctx_mon: r_sites, dctx = self.make_contexts(sites, rup, maxdist) except FarAwayRupture: continue with self.gmf_mon: mean_std = base.get_mean_std( # shape (2, N, M, G) r_sites, rup, dctx, imts, self.gsims) with self.poe_mon: pairs = zip(r_sites.sids, self._make_pnes(rup, mean_std)) with self.pne_mon: if rup_indep: for sid, pne in pairs: poemap.setdefault(sid, rup_indep).array *= pne else: for sid, pne in pairs: poemap.setdefault(sid, rup_indep).array += ( 1.-pne) * rup.weight nrups += 1 nsites += len(r_sites) if fewsites: # store rupdata rupdata.add(rup, src.id, r_sites, dctx) poemap.nrups = nrups poemap.nsites = nsites poemap.maxdist = numpy.mean(dists) if dists else None poemap.data = rupdata.data return poemap
def make(self): """ :param src: a hazardlib source :param s_sites: the sites affected by it :returns: the probability map generated by the source """ totrups, numrups, nsites = 0, 0, 0 L, G = len(self.imtls.array), len(self.gsims) poemap = ProbabilityMap(L, G) dists = [] for rups, sites, mdist in self._gen_rups_sites(): if mdist is not None: dists.append(mdist) with self.ctx_mon: ctxs = self.cmaker.make_ctxs(rups, sites, mdist) if ctxs: totrups += len(ctxs) ctxs = self.collapse(ctxs) numrups += len(ctxs) for rup, r_sites, dctx in ctxs: sids, poes = self._sids_poes(rup, r_sites, dctx) with self.pne_mon: pnes = rup.get_probability_no_exceedance(poes) if self.rup_indep: for sid, pne in zip(sids, pnes): poemap.setdefault(sid, self.rup_indep).array *= pne else: for sid, pne in zip(sids, pnes): poemap.setdefault( sid, self.rup_indep).array += (1. - pne) * rup.weight nsites += len(sids) poemap.totrups = totrups poemap.numrups = numrups poemap.nsites = nsites poemap.maxdist = numpy.mean(dists) if dists else None poemap.data = self.rupdata.data return poemap
def pmap_from_grp(group, src_filter, gsims, param, monitor=Monitor()): """ Compute the hazard curves for a set of sources belonging to the same tectonic region type for all the GSIMs associated to that TRT. The arguments are the same as in :func:`calc_hazard_curves`, except for ``gsims``, which is a list of GSIM instances. :returns: a dictionary {grp_id: ProbabilityMap instance} """ mutex_weight = { src.source_id: weight for src, weight in zip(group.sources, group.srcs_weights) } maxdist = src_filter.integration_distance srcs = sum([split_source(src) for src in group.sources], []) with GroundShakingIntensityModel.forbid_instantiation(): imtls = param['imtls'] trunclevel = param.get('truncation_level') cmaker = ContextMaker(gsims, maxdist) ctx_mon = monitor('make_contexts', measuremem=False) poe_mon = monitor('get_poes', measuremem=False) pmap = ProbabilityMap(len(imtls.array), len(gsims)) calc_times = [] # pairs (src_id, delta_t) for src, s_sites in src_filter(srcs): t0 = time.time() poemap = cmaker.poe_map(src, s_sites, imtls, trunclevel, ctx_mon, poe_mon, group.rup_interdep == 'indep') weight = mutex_weight[src.source_id] for sid in poemap: pcurve = pmap.setdefault(sid, 0) pcurve += poemap[sid] * weight calc_times.append( (src.source_id, src.weight, len(s_sites), time.time() - t0)) if group.grp_probability is not None: pmap *= group.grp_probability acc = AccumDict({group.id: pmap}) # adding the number of contributing ruptures too acc.eff_ruptures = {group.id: ctx_mon.counts} acc.calc_times = calc_times return acc
def get_pmap(self, ctxs, probmap=None): """ :param ctxs: a list of contexts :param probmap: if not None, update it :returns: a new ProbabilityMap if probmap is None """ tom = self.tom rup_indep = self.rup_indep if probmap is None: # create new pmap pmap = ProbabilityMap(self.imtls.size, len(self.gsims)) else: # update passed probmap pmap = probmap for block in block_splitter(ctxs, 20_000, len): for ctx, poes in self.gen_poes(block): # pnes and poes of shape (N, L, G) with self.pne_mon: pnes = get_probability_no_exceedance(ctx, poes, tom) for sid, pne in zip(ctx.sids, pnes): probs = pmap.setdefault(sid, self.rup_indep).array if rup_indep: probs *= pne else: # rup_mutex probs += (1. - pne) * ctx.weight
def get_pmap(ctxs, cmaker, probmap=None): """ :param ctxs: a list of contexts :param cmaker: the ContextMaker used to create the contexts :param probmap: if not None, update it :returns: a new ProbabilityMap if probmap is None """ tom = cmaker.tom rup_indep = cmaker.rup_indep if probmap is None: # create new pmap pmap = ProbabilityMap(cmaker.imtls.size, len(cmaker.gsims)) else: # update passed probmap pmap = probmap for ctx, poes in zip(ctxs, gen_poes(ctxs, cmaker)): # pnes and poes of shape (N, L, G) pnes = ctx.get_probability_no_exceedance(poes, tom) for sid, pne in zip(ctx.sids, pnes): probs = pmap.setdefault(sid, cmaker.rup_indep).array if rup_indep: probs *= pne else: # rup_mutex probs += (1. - pne) * ctx.weight if probmap is None: # return the new pmap return ~pmap if rup_indep else pmap
def pmap_from_grp(sources, src_filter, gsims, param, monitor=Monitor()): """ Compute the hazard curves for a set of sources belonging to the same tectonic region type for all the GSIMs associated to that TRT. The arguments are the same as in :func:`calc_hazard_curves`, except for ``gsims``, which is a list of GSIM instances. :returns: a ProbabilityMap instance """ if isinstance(sources, SourceGroup): group = sources sources = group.sources trt = sources[0].tectonic_region_type mutex_weight = { src.source_id: weight for src, weight in zip(group.sources, group.srcs_weights) } else: # list of sources trt = sources[0].tectonic_region_type group = SourceGroup(trt, sources, 'src_group', 'indep', 'indep') grp_id = sources[0].src_group_id maxdist = src_filter.integration_distance if hasattr(gsims, 'keys'): # dictionary trt -> gsim gsims = [gsims[trt]] srcs = [] for src in sources: if hasattr(src, '__iter__'): # MultiPointSource srcs.extend(src) else: srcs.append(src) del sources with GroundShakingIntensityModel.forbid_instantiation(): imtls = param['imtls'] trunclevel = param.get('truncation_level') cmaker = ContextMaker(gsims, maxdist) ctx_mon = monitor('making contexts', measuremem=False) pne_mons = [ monitor('%s.get_poes' % gsim, measuremem=False) for gsim in gsims ] src_indep = group.src_interdep == 'indep' pmap = ProbabilityMap(len(imtls.array), len(gsims)) pmap.calc_times = [] # pairs (src_id, delta_t) pmap.grp_id = grp_id for src, s_sites in src_filter(srcs): t0 = time.time() poemap = poe_map(src, s_sites, imtls, cmaker, trunclevel, ctx_mon, pne_mons, group.rup_interdep == 'indep') if src_indep: # usual composition of probabilities pmap |= poemap else: # mutually exclusive probabilities weight = mutex_weight[src.source_id] for sid in poemap: pcurve = pmap.setdefault(sid, 0) pcurve += poemap[sid] * weight pmap.calc_times.append( (src.source_id, src.weight, len(s_sites), time.time() - t0)) # storing the number of contributing ruptures too pmap.eff_ruptures = {pmap.grp_id: pne_mons[0].counts} if group.grp_probability is not None: return pmap * group.grp_probability return pmap