Ejemplo n.º 1
0
 def test_gmean_gerr3(self):
     """Difference between biased and unbiased error is small for large N"""
     x = np.linspace(0.5, 1.5, 100)
     w = np.linspace(0.5, 1.5, 100)
     error = (0.01, 0.01)
     gerrwbias = np.array(gerr(x, weights=w, unbiased=False)[1:])
     gerrwunbias = np.array(gerr(x, weights=w, unbiased=True)[1:])
     gerrbias = np.array(gerr(x, unbiased=False)[1:])
     gerrunbias = np.array(gerr(x, unbiased=True)[1:])
     np.testing.assert_array_less(gerrwunbias - gerrwbias, error)
     np.testing.assert_array_less(gerrunbias - gerrbias, error)
Ejemplo n.º 2
0
 def test_gmean_gerr1(self):
     """Results are same for no weight and equal weights"""
     x = np.array([3, 5, 8, 10, np.nan, 3, 4, -np.inf])
     w = np.ones(len(x))
     self.assertEqual(gmean(x), gmean(x, weights=w))
     self.assertEqual(gerr(x), gerr(x, weights=w))
     x = x.reshape((2, 4))
     w = np.ones(x.shape)
     self.assertEqual(len(gmean(x, axis=0)), x.shape[1])
     np.testing.assert_array_equal(gmean(x, axis=0),
                                   gmean(x, axis=0, weights=w))
     np.testing.assert_array_equal(gerr(x, axis=0),
                                   gerr(x, axis=0, weights=w))
Ejemplo n.º 3
0
    def test_robust(self):
        x = [1, 5, 6, 6, 6, 10000]
        m, err1, err2 = gerr(x, robust=True)
        m_, err1_, err2_ = gerr(x)
        self.assertLess(m, m_)
        self.assertLess(err1, err1_)
        self.assertLess(err2, err2_)

        x = np.reshape([3, 4, 5, 4, 5, 100, 1, 5, 6, 6, 6, 10000], (2, 6))
        m, err1, err2 = gerr(x, axis=1, robust=True)
        m_, err1_, err2_ = gerr(x, axis=1)
        np.testing.assert_array_less(m, m_)
        np.testing.assert_array_less(err1, err1_)
        np.testing.assert_array_less(err2, err2_)
Ejemplo n.º 4
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)
Ejemplo n.º 5
0
    def test_gmean_gerr2(self):
        """Results are same for repeated elements and adapted weights"""
        x1 = np.array([1, 2, 2, 5, 5, 5, 5, 5, np.inf])
        x2 = np.array([1, 2, 5, np.inf])
        w = np.array([1, 2, 5, 10])
        self.assertEqual(gmean(x1), gmean(x2, weights=w))

        # errors can only be compared for biased std
        np.testing.assert_array_equal(gerr(x1, unbiased=False)[1:],
                                      gerr(x2, weights=w, unbiased=False)[1:])
        # weighted errors are bigger than unweighted
        np.testing.assert_array_less(gerr(x1)[1:], gerr(x2, weights=w)[1:])
        # biased errors are smaller than unbiased
        np.testing.assert_array_less(gerr(x2, weights=w, unbiased=False)[1:],
                                     gerr(x2, weights=w)[1:])
Ejemplo n.º 6
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)