Example #1
0
File: smtk.py Project: rizac/eGSIM
def get_residuals(params):
    '''Core method to compute residuals plots data

    :param params: dict with the request parameters

    :return: json serializable dict to be passed into a Response object
    '''
    # params:
    GMDB = 'gmdb'  # pylint: disable=invalid-name
    GSIM = 'gsim'  # pylint: disable=invalid-name
    IMT = 'imt'  # pylint: disable=invalid-name
    PLOTTYPE = 'plot_type'  # pylint: disable=invalid-name
    SEL = 'selexpr'  # pylint: disable=invalid-name

    func, kwargs = params[PLOTTYPE]
    residuals = Residuals(params[GSIM], params[IMT])

    # Compute residuals.
    # params[GMDB] is the tuple (hdf file name, table name):
    gmdb = GroundMotionTable(*params[GMDB], mode='r')
    if params.get(SEL):
        gmdb = gmdb.filter(params[SEL])
    residuals.get_residuals(gmdb)

    # statistics = residuals.get_residual_statistics()
    ret = defaultdict(lambda: defaultdict(lambda: {}))

    # extend keyword arguments:
    kwargs = dict(kwargs, residuals=residuals, as_json=True)
    # linestep = binwidth/10
    for gsim in residuals.residuals:
        for imt in residuals.residuals[gsim]:
            kwargs['gmpe'] = gsim
            kwargs['imt'] = imt
            imt2 = _relabel_sa(imt)
            res_plots = func(**kwargs)
            for res_type, res_plot in res_plots.items():
                for stat in RESIDUALS_STATS:
                    res_plot.setdefault(stat, None)
                if imt2 != imt:
                    res_plot['xlabel'] = _relabel_sa(res_plot['xlabel'])
                    res_plot['ylabel'] = _relabel_sa(res_plot['ylabel'])
                # make also x and y keys consistent with trellis response:
                res_plot['xvalues'] = res_plot.pop('x')
                res_plot['yvalues'] = res_plot.pop('y')
                ret[imt2][res_type][gsim] = res_plot

    return ret
def get_conditional_gmfs(database, rupture, sites, gsims, imts,
        number_simulations, truncation_level,
        correlation_model=DEFAULT_CORRELATION):
    """
    Get a set of random fields conditioned on a set of observations
    :param database:
        Ground motion records for the event as instance of :class:
        smtk.sm_database.GroundMotionDatabase
    :param rupture:
        Event rupture as instance of :class:
        openquake.hazardlib.source.rupture.Rupture
    :param sites:
        Target sites as instance of :class:
        openquake.hazardlib.site.SiteCollection
    :param list gsims:
        List of GMPEs required
    :param list imts:
        List of intensity measures required
    :param int number_simulations:
        Number of simulated fields required
    :param float truncation_level:
        Ground motion truncation level
    """

    # Get known sites mesh
    known_sites = database.get_site_collection()

    # Get Observed Residuals
    residuals = Residuals(gsims, imts)
    residuals.get_residuals(database)
    imt_dict = OrderedDict([
        (imtx,  np.zeros([len(sites.lons), number_simulations]))
        for imtx in imts])
    gmfs = OrderedDict([(gmpe, imt_dict) for gmpe in gsims])
    gmpe_list = [GSIM_LIST[gmpe]() for gmpe in gsims]
    cmaker = ContextMaker(gmpe_list)
    sctx, rctx, dctx = cmaker.make_contexts(sites, rupture)
    for gsim in gmpe_list:
        gmpe = gsim.__class__.__name__
        #gsim = GSIM_LIST[gmpe]()
        #sctx, rctx, dctx = gsim.make_contexts(sites, rupture)
        for imtx in imts:
            if truncation_level == 0:
                gmfs[gmpe][imtx], _ = gsim.get_mean_and_stddevs(sctx, rctx,
                    dctx, from_string(imtx), stddev_types=[])
                continue
            if "Intra event" in gsim.DEFINED_FOR_STANDARD_DEVIATION_TYPES:
                epsilon = conditional_simulation(
                    known_sites,
                    residuals.residuals[gmpe][imtx]["Intra event"],
                    sites,
                    imtx,
                    number_simulations,
                    correlation_model)
                tau = np.unique(residuals.residuals[gmpe][imtx]["Inter event"])
                mean, [stddev_inter, stddev_intra] = gsim.get_mean_and_stddevs(
                    sctx,
                    rctx,
                    dctx, 
                    from_string(imtx), 
                    ["Inter event", "Intra event"])
                for iloc in range(0, number_simulations):
                    gmfs[gmpe][imtx][:, iloc] = np.exp(
                        mean +
                        (tau * stddev_inter) +
                        (epsilon[:, iloc].A1 * stddev_intra))
                        
            else:
                epsilon = conditional_simulation(
                    known_sites,
                    residuals.residuals[gmpe][imtx]["Total"],
                    sites,
                    imtx,
                    number_simulations,
                    correlation_model)
                tau = None
                mean, [stddev_total] = gsim.get_mean_and_stddevs(
                    sctx,
                    rctx,
                    dctx,
                    from_string(imtx),
                    ["Total"])
                for iloc in range(0, number_simulations):
                    gmfs[gmpe][imtx][:, iloc] = np.exp(
                        mean +
                        (epsilon[:, iloc].A1 * stddev_total.flatten()))
    return gmfs