Beispiel #1
0
def plot_results(result,
                 v0=None,
                 fname=None,
                 title=None,
                 quantities=QUANTITIES,
                 mean=None,
                 llim=None,
                 Qlim=None,
                 figsize=None):
    """Plot results"""
    freq = np.array(result['freq'])
    N = len(quantities)
    n = int(np.ceil(np.sqrt(N)))
    fig = plt.figure(figsize=figsize)
    gs = gridspec.GridSpec(n, n)
    share = None
    col = collect_results(result, only=('g0', 'b', 'error', 'v0'))
    v0 = v0 or result['config'].get('v0') or col['v0']
    result = col
    result.pop('v0', None)
    weights = 1 / np.array(result['error']) if mean == 'weighted' else None
    robust = mean == 'robust'
    for i, q in enumerate(quantities):
        ax = plt.subplot(gs[i // n, i % n], sharex=share)
        if q == 'nobs':
            nobs = np.sum(~np.isnan(result['g0']), axis=0)
            ax.bar(freq, nobs, width=0.1 * freq, color='gray')
        else:
            value = result[DEPMAP[q]]
            value = calc_dependent(q, value, freq, v0)
            freqs = np.repeat(freq[np.newaxis, :], value.shape[0], axis=0)
            ax.loglog(freqs, value, 'o', ms=MS, color='gray', mec='gray')
            means, err1, err2 = gerr(value,
                                     axis=0,
                                     weights=weights,
                                     robust=robust)
            errs = (err1, err2)
            ax.errorbar(freq,
                        means,
                        yerr=errs,
                        marker='o',
                        mfc='k',
                        mec='k',
                        color='m',
                        ecolor='m')
        ax.annotate(QLABELS[q], (1, 1), (-5, -5),
                    'axes fraction',
                    'offset points',
                    ha='right',
                    va='top')
        _set_gridlabels(ax, i, n, n, N, ylabel=None)
        if share is None:
            share = ax
        if q in ('Qsc', 'Qi') and Qlim:
            ax.set_ylim(Qlim)
        if q in ('lsc', 'li') and llim:
            ax.set_ylim(llim)
    ax.set_xlim(freqlim(freq))
    _savefig(fig, fname=fname, title=title)
Beispiel #2
0
def plot_sites(result, fname=None, title=None, mean=None,
               xlim=None, ylim=(1e-2, 1e2), nx=None, figsize=None):
    """Plot site amplification factors"""
    freq = np.array(result['freq'])
    col = collect_results(result, only=['R', 'error'])
    R = col['R']
    weights = 1 / np.array(col['error']) if mean == 'weighted' else None
    robust = mean == 'robust'
    max_nobs = np.max([np.sum(~np.isnan(r), axis=0) for r in R.values()])
    N = max_nobs > 1
    for station in sorted(R):
        if not np.all(np.isnan(R[station])):
            N = N + 1
#    N = len(R) + (max_nobs > 1)
#    for i
    fig = plt.figure(figsize=figsize)
    nx, ny, gs = _get_grid(N, nx=nx)
    cmap = plt.get_cmap('hot_r', max_nobs)
    norm = mpl.colors.Normalize(vmin=0.5, vmax=max_nobs + 0.5)
    share = None
    i = 0
    for station in sorted(R):
        if np.all(np.isnan(R[station])):
            continue
        ax = plt.subplot(gs[i // nx, i % nx], sharex=share, sharey=share)
        means, err1, err2 = gerr(R[station], axis=0, weights=weights,
                                 robust=robust)
        nobs = 1. * np.sum(~np.isnan(R[station]), axis=0)
        errs = (err1, err2)
        freqs = np.repeat(freq[np.newaxis, :], R[station].shape[0], axis=0)
#        if not np.all(np.isnan(R[station])):
        if max_nobs == 1:
            kwargs = {'c': 'k'}
        else:
            kwargs = {'c': nobs, 'norm': norm, 'cmap': cmap}
        ax.loglog(freqs, R[station], 'o', ms=MS, color='gray', mec='gray')
        ax.errorbar(freq, means, yerr=errs, marker=None,
                    color='m', ecolor='m')
        sc = ax.scatter(freq, means, s=4 * MS ** 2,
                        marker='o', zorder=10,
                        linewidth=0.5,
                        **kwargs)
        ax.annotate(station, (1, 1), (-5, -5), 'axes fraction',
                    'offset points', ha='right', va='top', size='x-small')
        _set_gridlabels(ax, i, nx, ny, N, ylabel='amplification factor')
        if share is None:
            share = ax
        i += 1
    ax.set_xlim(xlim or freqlim(freq))
    if ylim:
        ax.set_ylim(ylim)
    if max_nobs != 1:
        ax = plt.subplot(gs[(N - 1) // nx, (N - 1) % nx])
        ax.set_axis_off()
        fig.colorbar(sc, ax=ax, shrink=0.9, format='%d', label='nobs',
                     ticks=np.arange(0, max_nobs + 1, max(1, max_nobs // 5)))
    _savefig(fig, fname=fname, title=title)
Beispiel #3
0
def plot_sites(result, fname=None, title=None, mean=None,
               xlim=None, ylim=(1e-2, 1e2), nx=None, figsize=None):
    freq = np.array(result['freq'])
    g0, b, error, R, _, _, _ = collect_results(result)
    weights = 1 / np.array(error) if mean == 'weighted' else None
    robust = mean == 'robust'
    max_nobs = np.max([np.sum(~np.isnan(r), axis=0) for r in R.values()])
    N = max_nobs > 1
    for station in sorted(R):
        if not np.all(np.isnan(R[station])):
            N = N + 1
    #N = len(R) + (max_nobs > 1)
    #for i
    fig = plt.figure(figsize=figsize)
    nx, ny, gs = _get_grid(N, nx=nx)
    cmap = plt.get_cmap('hot_r', max_nobs)
    norm = mpl.colors.Normalize(vmin=0.5, vmax=max_nobs + 0.5)
    share = None
    i = 0
    for station in sorted(R):
        if np.all(np.isnan(R[station])):
            continue
        ax = plt.subplot(gs[i // nx, i % nx], sharex=share, sharey=share)
        means, err1, err2 = gerr(R[station], axis=0, weights=weights,
                                 robust=robust)
        nobs = 1. * np.sum(~np.isnan(R[station]), axis=0)
        errs = (err1, err2)
        freqs = np.repeat(freq[np.newaxis, :], R[station].shape[0], axis=0)
        #if not np.all(np.isnan(R[station])):
        if max_nobs == 1:
            kwargs = {'c': 'k'}
        else:
            kwargs = {'c': nobs, 'norm': norm, 'cmap': cmap}
        ax.loglog(freqs, R[station], 'o', ms=MS, color='gray', mec='gray')
        ax.errorbar(freq, means, yerr=errs, marker=None,
                    color='m', ecolor='m')
        sc = ax.scatter(freq, means, s=4 * MS ** 2,
                        marker='o', zorder=10,
                        linewidth=0.5,
                        **kwargs)
        ax.annotate(station, (1, 1), (-5, -5), 'axes fraction',
                    'offset points', ha='right', va='top', size='x-small')
        _set_gridlabels(ax, i, nx, ny, N, ylabel='amplification factor')
        if share is None:
            share = ax
        i += 1
    ax.set_xlim(xlim or freqlim(freq))
    if ylim:
        ax.set_ylim(ylim)
    if max_nobs != 1:
        ax = plt.subplot(gs[(N - 1) // nx, (N - 1) % nx])
        ax.set_axis_off()
        fig.colorbar(sc, ax=ax, shrink=0.9, format='%d', label='nobs',
                     ticks=np.arange(0, max_nobs + 1, max(1, max_nobs // 5)))
    _savefig(fig, fname=fname, title=title)
Beispiel #4
0
def _collectR(results, freqi=0, only=None):
    from qopen.core import collect_results
    col = collect_results(results, freqi=freqi, only=['R'])
    R = col['R']
    if only is not None:
        R = {sta: R[sta] for sta in only}


#    for evid, evres in results['events'].items():
#        for sta, Rval in evres['R'].items():
#            if use_only and sta not in use_only:
#                continue
#            R[sta].append(Rval[freqi])
    return R
Beispiel #5
0
def plot_results(result, v0=None, fname=None, title=None,
                 quantities=QUANTITIES, mean=None,
                 llim=None, Qlim=None, figsize=None):
    freq = np.array(result['freq'])
    N = len(quantities)
    n = int(np.ceil(np.sqrt(N)))
    fig = plt.figure(figsize=figsize)
    gs = gridspec.GridSpec(n, n)
    share = None
    g0, b, error, R, _, _, v02 = collect_results(result)
    v0 = v0 or result['config'].get('v0') or v02
    result = {'g0': g0, 'b': b, 'error': error, 'R': R}
    weights = 1 / np.array(error) if mean == 'weighted' else None
    robust = mean == 'robust'
    for i, q in enumerate(quantities):
        ax = plt.subplot(gs[i // n, i % n], sharex=share)
        if q == 'nobs':
            nobs = np.sum(~np.isnan(g0), axis=0)
            ax.bar(freq, nobs, width=0.1 * freq, color='gray')
        else:
            value = result[DEPMAP[q]]
            value = calc_dependent(q, value, freq, v0)
            freqs = np.repeat(freq[np.newaxis, :], value.shape[0], axis=0)
            ax.loglog(freqs, value, 'o', ms=MS, color='gray', mec='gray')
            means, err1, err2 = gerr(
                value, axis=0, weights=weights, robust=robust)
            errs = (err1, err2)
            ax.errorbar(freq, means, yerr=errs, marker='o',
                        mfc='k', mec='k', color='m', ecolor='m')
        ax.annotate(QLABELS[q], (1, 1), (-5, -5), 'axes fraction',
                    'offset points', ha='right', va='top')
        _set_gridlabels(ax, i, n, n, N, ylabel=None)
        if share is None:
            share = ax
        if q in ('Qsc', 'Qi') and Qlim:
            ax.set_ylim(Qlim)
        if q in ('lsc', 'li') and llim:
            ax.set_ylim(llim)
    ax.set_xlim(freqlim(freq))
    _savefig(fig, fname=fname, title=title)
Beispiel #6
0
def plot_sites(result,
               mean=None,
               xlim=None,
               ylim=(1e-2, 1e2),
               nx=None,
               cmap='viridis_r',
               vmin=None,
               vmax=None,
               **kwargs):
    """Plot site amplification factors"""
    freq = np.array(result['freq'])
    # True for invert_events_simultaneously
    single_inversion = 'R' not in list(result['events'].values())[0]
    if single_inversion:
        colres = result
        R = copy(colres['R'])
        for sta in R:
            R[sta] = np.array(R[sta], dtype=float)
        max_nobs = 1
    else:
        colres = collect_results(result, only=['R', 'error'])
        R = colres['R']
        max_nobs = np.max([np.sum(~np.isnan(r), axis=0) for r in R.values()])
    weights = 1 / np.array(colres['error']) if mean == 'weighted' else None
    robust = mean == 'robust'
    N = max_nobs > 1
    for station in sorted(R):
        if not np.all(np.isnan(R[station])):
            N = N + 1


#    N = len(R) + (max_nobs > 1)
    fig = plt.figure()
    nx, ny, gs = _get_grid(N, nx=nx)
    if cmap is None:
        cmap = 'black'
    cmap = plt.get_cmap(cmap, max_nobs)
    if vmax is None:
        vmax = max_nobs + 0.5
    if vmin is None:
        vmin = 0.5
    norm = mpl.colors.Normalize(vmin=vmin, vmax=vmax)
    share = None
    i = 0
    for station in sorted(R):
        if np.all(np.isnan(R[station])):
            continue
        ax = plt.subplot(gs[i // nx, i % nx], sharex=share, sharey=share)
        means, err1, err2 = gerr(R[station],
                                 axis=0,
                                 weights=weights,
                                 robust=robust)
        errs = (err1, err2)
        #        if not np.all(np.isnan(R[station])):
        if max_nobs == 1:
            kw = {'c': 'k'}
        else:
            nobs = 1. * np.sum(~np.isnan(R[station]), axis=0)
            kw = {'c': nobs, 'norm': norm, 'cmap': cmap}
        if not single_inversion:
            freqs = np.repeat(freq[np.newaxis, :], R[station].shape[0], axis=0)
            ax.plot(freqs, R[station], 'o', ms=MS, color='gray', mec='gray')
        ax.errorbar(freq, means, yerr=errs, marker=None, color='m', ecolor='m')
        sc = ax.scatter(freq,
                        means,
                        s=4 * MS**2,
                        marker='o',
                        zorder=10,
                        linewidth=0.5,
                        **kw)
        ax.set_xscale('log')
        ax.set_yscale('log')
        ax.annotate(station, (1, 1), (-5, -5),
                    'axes fraction',
                    'offset points',
                    ha='right',
                    va='top',
                    size='x-small')
        _set_gridlabels(ax, i, nx, ny, N, ylabel='amplification factor')
        if share is None:
            share = ax
        i += 1
    ax.set_xlim(xlim or freqlim(freq))
    if ylim:
        ax.set_ylim(ylim)
    if max_nobs != 1:
        ax = plt.subplot(gs[(N - 1) // nx, (N - 1) % nx])
        ax.set_axis_off()
        fig.colorbar(sc,
                     ax=ax,
                     shrink=0.9,
                     format='%d',
                     label='nobs',
                     ticks=np.arange(0, max_nobs + 1, max(1, max_nobs // 5)))
    _savefig(fig, **kwargs)