def _hmap4(rlzs, iml_disagg, imtls, poes_disagg, curves): # an ArrayWrapper of shape (N, M, P, Z) N, Z = rlzs.shape P = len(poes_disagg) M = len(imtls) arr = numpy.empty((N, M, P, Z)) for m, imt in enumerate(imtls): for (s, z), rlz in numpy.ndenumerate(rlzs): curve = curves[s][z] if poes_disagg == (None, ): arr[s, m, 0, z] = imtls[imt] elif curve: rlz = rlzs[s, z] max_poe = curve[imt].max() arr[s, m, :, z] = calc.compute_hazard_maps(curve[imt], imtls[imt], poes_disagg) for iml, poe in zip(arr[s, m, :, z], poes_disagg): if iml == 0: logging.warning( 'Cannot disaggregate for site %d, %s, ' 'poe=%s, rlz=%d: the hazard is zero', s, imt, poe, rlz) elif poe > max_poe: logging.warning(POE_TOO_BIG, s, poe, max_poe, rlz, imt) return hdf5.ArrayWrapper(arr, {'rlzs': rlzs})
def hazard_maps(self, curves): """ Compute the hazard maps associated to the curves """ maps = zero_maps( len(self.sitecol), self.oqparam.imtls, self.oqparam.poes) for imt in curves.dtype.fields: # build a matrix of size (N, P) data = calc.compute_hazard_maps( curves[imt], self.oqparam.imtls[imt], self.oqparam.poes) for poe, hmap in zip(self.oqparam.poes, data.T): maps['%s-%s' % (imt, poe)] = hmap return maps
def hazard_maps(self, curves): """ Compute the hazard maps associated to the curves """ maps = zero_maps(len(self.sitecol), self.oqparam.imtls, self.oqparam.poes) for imt in curves.dtype.fields: # build a matrix of size (N, P) data = calc.compute_hazard_maps(curves[imt], self.oqparam.imtls[imt], self.oqparam.poes) for poe, hmap in zip(self.oqparam.poes, data.T): maps['%s-%s' % (imt, poe)] = hmap return maps
def test_compute_hazard_map(self): curves = [ [0.8, 0.5, 0.1], [0.98, 0.15, 0.05], [0.6, 0.5, 0.4], [0.1, 0.01, 0.001], [0.8, 0.2, 0.1], ] imls = [0.005, 0.007, 0.0098] poe = 0.2 expected = [[0.00847798, 0.00664814, 0.0098, 0, 0.007]] actual = calc.compute_hazard_maps(numpy.array(curves), imls, poe) aaae(expected, actual.T)
def test_compute_hazard_map_multi_poe(self): curves = [ [0.8, 0.5, 0.1], [0.98, 0.15, 0.05], [0.6, 0.5, 0.4], [0.1, 0.01, 0.001], [0.8, 0.2, 0.1], ] imls = [0.005, 0.007, 0.0098] poes = [0.1, 0.2] expected = [[0.0098, 0.00792555, 0.0098, 0.005, 0.0098], [0.00847798, 0.00664814, 0.0098, 0, 0.007]] actual = calc.compute_hazard_maps(numpy.array(curves), imls, poes) aaae(expected, actual.T)
def test_compute_hazard_map(self): curves = [ [0.8, 0.5, 0.1], [0.98, 0.15, 0.05], [0.6, 0.5, 0.4], [0.1, 0.01, 0.001], [0.8, 0.2, 0.1], ] imls = [0.005, 0.007, 0.0098] poe = 0.2 expected = [[0.00847798, 0.00664814, 0.0098, 0, 0.007]] actual = calc.compute_hazard_maps(curves, imls, poe) aaae(expected, actual.T)
def test_compute_hazard_map_multi_poe(self): curves = [ [0.8, 0.5, 0.1], [0.98, 0.15, 0.05], [0.6, 0.5, 0.4], [0.1, 0.01, 0.001], [0.8, 0.2, 0.1], ] imls = [0.005, 0.007, 0.0098] poes = [0.1, 0.2] expected = [ [0.0098, 0.00792555, 0.0098, 0.005, 0.0098], [0.00847798, 0.00664814, 0.0098, 0, 0.007] ] actual = calc.compute_hazard_maps(curves, imls, poes) aaae(expected, actual.T)
def execute(self): """ Compute the conditional spectrum """ oq = self.oqparam self.full_lt = self.datastore['full_lt'] self.trts = list(self.full_lt.gsim_lt.values) self.imts = list(oq.imtls) imti = self.imts.index(oq.imt_ref) self.M = len(self.imts) dstore = (self.datastore.parent if self.datastore.parent else self.datastore) totrups = len(dstore['rup/mag']) logging.info('Reading {:_d} ruptures'.format(totrups)) rdt = [('grp_id', U16), ('nsites', U16), ('idx', U32)] rdata = numpy.zeros(totrups, rdt) rdata['idx'] = numpy.arange(totrups) rdata['grp_id'] = dstore['rup/grp_id'][:] rdata['nsites'] = [len(sids) for sids in dstore['rup/sids_']] totweight = rdata['nsites'].sum() trt_smrs = dstore['trt_smrs'][:] rlzs_by_gsim = self.full_lt.get_rlzs_by_gsim_list(trt_smrs) G_ = sum(len(rbg) for rbg in rlzs_by_gsim) self.periods = [from_string(imt).period for imt in self.imts] if oq.imls_ref: self.imls = oq.imls_ref else: # extract imls from the "mean" hazard map curve = self.datastore.sel('hcurves-stats', stat='mean')[0, 0, imti] [self.imls] = compute_hazard_maps(curve, oq.imtls[oq.imt_ref], oq.poes) # there is 1 site self.P = P = len(self.imls) self.datastore.create_dset('cs-rlzs', float, (P, self.R, 2, self.M)) self.datastore.set_shape_descr('cs-rlzs', poe_id=P, rlz_id=self.R, cs=2, m=self.M) self.datastore.create_dset('cond-spectra', float, (P, 2, self.M)) self.datastore.set_shape_descr('cond-spectra', poe_id=P, cs=['spec', 'std'], period=self.periods) self.datastore.create_dset('_c', float, (G_, P, 2, self.M)) self.datastore.create_dset('_s', float, ( G_, P, )) G = max(len(rbg) for rbg in rlzs_by_gsim) maxw = 2 * 1024**3 / (16 * G * self.M) # at max 2 GB maxweight = min(numpy.ceil(totweight / (oq.concurrent_tasks or 1)), maxw) U = 0 Ta = 0 self.cmakers = read_cmakers(self.datastore) self.datastore.swmr_on() smap = parallel.Starmap(conditional_spectrum, h5=self.datastore.hdf5) # IMPORTANT!! we rely on the fact that the classical part # of the calculation stores the ruptures in chunks of constant # grp_id, therefore it is possible to build (start, stop) slices for block in general.block_splitter(rdata, maxweight, operator.itemgetter('nsites'), operator.itemgetter('grp_id')): Ta += 1 grp_id = block[0]['grp_id'] G = len(rlzs_by_gsim[grp_id]) cmaker = self.cmakers[grp_id] U = max(U, block.weight) slc = slice(block[0]['idx'], block[-1]['idx'] + 1) smap.submit((dstore, slc, cmaker, imti, self.imls)) return smap.reduce()