Ejemplo n.º 1
0
    def start_tasks(self, sm_id, ruptures_by_grp, sitecol, assetcol, riskmodel,
                    imts, trunc_level, correl_model, min_iml, monitor):
        """
        :param sm_id: source model ordinal
        :param ruptures_by_grp: dictionary of ruptures by src_group_id
        :param sitecol: a SiteCollection instance
        :param assetcol: an AssetCollection instance
        :param riskmodel: a RiskModel instance
        :param imts: a list of Intensity Measure Types
        :param trunc_level: truncation level
        :param correl_model: correlation model
        :param min_iml: vector of minimum intensities, one per IMT
        :param monitor: a Monitor instance
        :returns: an IterResult instance
        """
        csm_info = self.csm_info.get_info(sm_id)
        grp_ids = sorted(csm_info.get_sm_by_grp())
        rlzs_assoc = csm_info.get_rlzs_assoc(
            count_ruptures=lambda grp: len(ruptures_by_grp.get(grp.id, [])))
        num_events = sum(ebr.multiplicity for grp in ruptures_by_grp
                         for ebr in ruptures_by_grp[grp])
        seeds = self.oqparam.random_seed + numpy.arange(num_events)

        allargs = []
        # prepare the risk inputs
        ruptures_per_block = self.oqparam.ruptures_per_block
        start = 0
        ignore_covs = self.oqparam.ignore_covs
        for grp_id in grp_ids:
            rlzs_by_gsim = rlzs_assoc.get_rlzs_by_gsim(grp_id)
            samples = rlzs_assoc.samples[grp_id]
            for rupts in block_splitter(ruptures_by_grp.get(grp_id, []),
                                        ruptures_per_block):
                if ignore_covs or not self.riskmodel.covs:
                    eps = None
                elif self.oqparam.asset_correlation:
                    eps = EpsilonMatrix1(num_events, self.oqparam.master_seed)
                else:
                    n_events = sum(ebr.multiplicity for ebr in rupts)
                    eps = EpsilonMatrix0(len(self.assetcol),
                                         seeds[start:start + n_events])
                    start += n_events
                getter = riskinput.GmfGetter(grp_id, rlzs_by_gsim, rupts,
                                             sitecol, imts, min_iml,
                                             trunc_level, correl_model,
                                             samples)
                ri = riskinput.RiskInputFromRuptures(getter, eps)
                allargs.append((ri, riskmodel, assetcol, monitor))

        self.vals = self.assetcol.values()
        taskname = '%s#%d' % (event_based_risk.__name__, sm_id + 1)
        ires = Starmap(event_based_risk, allargs, name=taskname).submit_all()
        ires.num_ruptures = {
            sg_id: len(rupts)
            for sg_id, rupts in ruptures_by_grp.items()
        }
        ires.num_events = num_events
        ires.num_rlzs = len(rlzs_assoc.realizations)
        ires.sm_id = sm_id
        return ires
Ejemplo n.º 2
0
    def start_tasks(self, sm_id, ruptures_by_grp, sitecol, assetcol, riskmodel,
                    imtls, trunc_level, correl_model, min_iml, monitor):
        """
        :param sm_id: source model ordinal
        :param ruptures_by_grp: dictionary of ruptures by src_group_id
        :param sitecol: a SiteCollection instance
        :param assetcol: an AssetCollection instance
        :param riskmodel: a RiskModel instance
        :param imtls: Intensity Measure Types and Levels
        :param trunc_level: truncation level
        :param correl_model: correlation model
        :param min_iml: vector of minimum intensities, one per IMT
        :param monitor: a Monitor instance
        :returns: an IterResult instance
        """
        csm_info = self.csm_info.get_info(sm_id)
        grp_ids = sorted(csm_info.get_sm_by_grp())
        rlzs_assoc = csm_info.get_rlzs_assoc()
        # prepare the risk inputs
        allargs = []
        ruptures_per_block = self.oqparam.ruptures_per_block
        try:
            csm_info = self.csm.info
        except AttributeError:  # there is no .csm if --hc was given
            csm_info = self.datastore['csm_info']
        samples_by_grp = csm_info.get_samples_by_grp()
        num_events = 0
        for grp_id in grp_ids:
            rlzs_by_gsim = rlzs_assoc.get_rlzs_by_gsim(grp_id)
            samples = samples_by_grp[grp_id]
            for rupts in block_splitter(ruptures_by_grp.get(grp_id, []),
                                        ruptures_per_block):
                n_events = sum(ebr.multiplicity for ebr in rupts)
                eps = self.get_eps(self.start, self.start + n_events)
                num_events += n_events
                self.start += n_events
                getter = riskinput.GmfGetter(rlzs_by_gsim, rupts, sitecol,
                                             imtls, min_iml,
                                             self.oqparam.maximum_distance,
                                             trunc_level, correl_model,
                                             samples)
                ri = riskinput.RiskInput(getter, self.assets_by_site, eps)
                allargs.append((ri, riskmodel, assetcol, monitor))

        self.vals = self.assetcol.values()
        taskname = '%s#%d' % (event_based_risk.__name__, sm_id + 1)
        ires = parallel.Starmap(event_based_risk, allargs,
                                name=taskname).submit_all()
        ires.num_ruptures = {
            sg_id: len(rupts)
            for sg_id, rupts in ruptures_by_grp.items()
        }
        ires.num_events = num_events
        ires.num_rlzs = len(rlzs_assoc.realizations)
        ires.sm_id = sm_id
        return ires
Ejemplo n.º 3
0
def compute_losses(ssm, src_filter, param, riskmodel, imts, trunc_level,
                   correl_model, min_iml, monitor):
    """
    Compute the losses for a single source model. Returns the ruptures
    as an attribute `.ruptures_by_grp` of the list of losses.

    :param ssm: CompositeSourceModel containing a single source model
    :param sitecol: a SiteCollection instance
    :param param: a dictionary of parameters
    :param riskmodel: a RiskModel instance
    :param imts: a list of Intensity Measure Types
    :param trunc_level: truncation level
    :param correl_model: correlation model
    :param min_iml: vector of minimum intensities, one per IMT
    :param monitor: a Monitor instance
    :returns: a List containing the losses by taxonomy and some attributes
    """
    [grp] = ssm.src_groups
    res = List()
    gsims = ssm.gsim_lt.values[DEFAULT_TRT]
    ruptures_by_grp = compute_ruptures(grp, src_filter, gsims, param, monitor)
    [(grp_id, ebruptures)] = ruptures_by_grp.items()
    rlzs_assoc = ssm.info.get_rlzs_assoc()
    samples = ssm.info.get_samples_by_grp()
    num_rlzs = len(rlzs_assoc.realizations)
    rlzs_by_gsim = rlzs_assoc.get_rlzs_by_gsim(DEFAULT_TRT)
    getter = riskinput.GmfGetter(rlzs_by_gsim, ebruptures, src_filter.sitecol,
                                 imts, min_iml,
                                 src_filter.integration_distance, trunc_level,
                                 correl_model, samples[grp_id])
    ri = riskinput.RiskInput(getter, param['assetcol'].assets_by_site())
    res.append(event_based_risk(ri, riskmodel, param, monitor))
    res.sm_id = ssm.sm_id
    res.num_events = len(ri.hazard_getter.eids)
    start = res.sm_id * num_rlzs
    res.rlz_slice = slice(start, start + num_rlzs)
    res.events_by_grp = ruptures_by_grp.events_by_grp
    res.eff_ruptures = ruptures_by_grp.eff_ruptures
    return res