Ejemplo n.º 1
0
    def save_disagg_results(self, results, out):
        """
        Save the computed PMFs in the datastore

        :param results:
            a dict s -> 9D-matrix of shape (T, Ma, D, Lo, La, E, M, P, Z)
        :para out:
            a dict kind -> PMF matrix to be populated
        """
        outputs = self.oqparam.disagg_outputs
        for s, mat9 in results.items():
            if s not in self.ok_sites:
                continue
            for p, poe in enumerate(self.poes_disagg):
                mat8 = mat9[..., p, :]
                poe2 = pprod(mat8, axis=(0, 1, 2, 3, 4, 5))
                self.datastore['poe4'][s, :, p] = poe2  # shape (M, Z)
                poe_agg = poe2.mean()
                if poe and abs(1 - poe_agg / poe) > .1:
                    logging.warning(
                        'Site #%d: poe_agg=%s is quite different from the '
                        'expected poe=%s; perhaps the number of intensity '
                        'measure levels is too small?', s, poe_agg, poe)
                for m, imt in enumerate(self.imts):
                    mat7 = mat8[..., m, :]
                    mat6 = agg_probs(*mat7)  # 6D
                    for key in outputs:
                        pmf = disagg.pmf_map[key](
                            mat7 if key.endswith('TRT') else mat6)
                        out[key][s, m, p, :] = pmf
Ejemplo n.º 2
0
    def save_disagg_results(self, results):
        """
        Save the computed PMFs in the datastore

        :param results:
            a dict s, m, k -> 6D-matrix of shape (T, Ma, Lo, La, P, Z) or
            (T, Ma, D, E, P, Z) depending if k is 0 or k is 1
        """
        oq = self.oqparam
        out = output_dict(self.shapedic, oq.disagg_outputs)
        count = numpy.zeros(len(self.sitecol), U16)
        _disagg_trt = numpy.zeros(self.N, [(trt, float) for trt in self.trts])
        vcurves = []  # hazard curves with a vertical section for large poes
        for (s, m, k), mat6 in sorted(results.items()):
            # NB: k is an index with value 0 (MagDistEps) or 1 (MagLonLat)
            imt = self.imts[m]
            for p, poe in enumerate(self.poes_disagg):
                mat5 = mat6[..., p, :]
                if k == 0 and m == 0 and poe == self.poes_disagg[-1]:
                    # mat5 has shape (T, Ma, D, E, Z)
                    _disagg_trt[s] = tuple(pprod(mat5[..., 0], axis=(1, 2, 3)))
                poe2 = pprod(mat5, axis=(0, 1, 2, 3))
                self.datastore['poe4'][s, m, p] = poe2  # shape Z
                poe_agg = poe2.mean()
                if (poe and abs(1 - poe_agg / poe) > .1 and not count[s]
                        and self.hmap4[s, m, p].any()):
                    logging.warning(
                        'Site #%d, IMT=%s: poe_agg=%s is quite different from '
                        'the expected poe=%s, perhaps not enough levels',
                        s, imt, poe_agg, poe)
                    vcurves.append(self.curves[s])
                    count[s] += 1
                mat4 = agg_probs(*mat5)  # shape (Ma D E Z) or (Ma Lo La Z)
                for key in oq.disagg_outputs:
                    if key == 'Mag' and k == 0:
                        out[key][s, m, p, :] = pprod(mat4, axis=(1, 2))
                    elif key == 'Dist' and k == 0:
                        out[key][s, m, p, :] = pprod(mat4, axis=(0, 2))
                    elif key == 'TRT' and k == 0:
                        out[key][s, m, p, :] = pprod(mat5, axis=(1, 2, 3))
                    elif key == 'Mag_Dist' and k == 0:
                        out[key][s, m, p, :] = pprod(mat4, axis=2)
                    elif key == 'Mag_Dist_Eps' and k == 0:
                        out[key][s, m, p, :] = mat4
                    elif key == 'Lon_Lat' and k == 1:
                        out[key][s, m, p, :] = pprod(mat4, axis=0)
                    elif key == 'Mag_Lon_Lat' and k == 1:
                        out[key][s, m, p, :] = mat4
                    elif key == 'Lon_Lat_TRT' and k == 1:
                        out[key][s, m, p, :] = pprod(mat5, axis=1).transpose(
                            1, 2, 0, 3)  # T Lo La Z -> Lo La T Z
                    # shape NMP..Z
        self.datastore['disagg'] = out
        # below a dataset useful for debugging, at minimum IMT and maximum RP
        self.datastore['_disagg_trt'] = _disagg_trt
        if len(vcurves):
            NML1 = len(vcurves), self.M, oq.imtls.size // self.M
            self.datastore['_vcurves'] = numpy.array(vcurves).reshape(NML1)
            self.datastore['_vcurves'].attrs['sids'] = numpy.where(count)[0]
Ejemplo n.º 3
0
 def get_mean_hcurve(self, src=None, imt=None, site_id=0, gsim_idx=None):
     """
     Return the mean curve associated to the given src, imt and gsim_idx
     as an array of shape L
     """
     if src is None:
         hcurves = [self.get_mean_hcurve(src) for src in self.sslt]
         return general.agg_probs(*hcurves)
     weights = [rlz.weight for rlz in self.sslt[src]]
     curves = self.get_hcurves(src, imt, site_id, gsim_idx)
     return weights @ curves
Ejemplo n.º 4
0
 def get_hcurves(self, pmap, rlzs_by_gsim):  # used in in disagg_by_src
     """
     :param pmap: a ProbabilityMap
     :param rlzs_by_gsim: a dictionary gsim -> rlz IDs
     :returns: an array of PoEs of shape (N, R, M, L)
     """
     res = numpy.zeros((self.N, self.R, self.L))
     for sid, pc in pmap.items():
         for gsim_idx, rlzis in enumerate(rlzs_by_gsim.values()):
             poes = pc.array[:, gsim_idx]
             for rlz in rlzis:
                 res[sid, rlz] = general.agg_probs(res[sid, rlz], poes)
     return res.reshape(self.N, self.R, self.M, -1)
Ejemplo n.º 5
0
 def get_hcurves(self, pmap_by_grp):
     """
     :param pmap_by_grp: a dictionary of ProbabilityMaps by group
     :returns: an array of PoEs of shape (N, R, M, L)
     """
     self.init()
     res = numpy.zeros((self.N, self.R, self.L))
     for grp, pmap in pmap_by_grp.items():
         for sid, pc in pmap.items():
             for gsim_idx, rlzis in enumerate(self.rlzs_by_grp[grp]):
                 poes = pc.array[:, gsim_idx]
                 for rlz in rlzis:
                     res[sid, rlz] = general.agg_probs(res[sid, rlz], poes)
     return res.reshape(self.N, self.R, self.M, -1)
Ejemplo n.º 6
0
def combine_probs(values_by_grp, cmakers, rlz):
    """
    :param values_by_grp: C arrays of shape (D1, D2..., G)
    :param cmakers: C ContextMakers with G gsims each
    :param rlz: a realization index
    :returns: array of shape (D1, D2, ...)
    """
    probs = []
    for values, cmaker in zip(values_by_grp, cmakers):
        assert values.shape[-1] == len(cmaker.gsims)
        for g, rlzs in enumerate(cmaker.gsims.values()):
            if rlz in rlzs:
                probs.append(values[..., g])
    return agg_probs(*probs)
Ejemplo n.º 7
0
    def agg_result(self, acc, result):
        """
        Collect the results coming from compute_disagg into self.results.

        :param acc: dictionary sid -> trti, magi -> 6D array
        :param result: dictionary with the result coming from a task
        """
        # 7D array of shape (#distbins, #lonbins, #latbins, #epsbins, M, P, Z)
        with self.monitor('aggregating disagg matrices'):
            trti = result.pop('trti')
            magi = result.pop('magi')
            for sid, probs in result.items():
                before = acc[sid].get((trti, magi), 0)
                acc[sid][trti, magi] = agg_probs(before, probs)
        return acc