예제 #1
0
파일: plotting.py 프로젝트: TCvanLeth/PyHAD
def meshplot(x, y, z, log=False, levels=100, aligned=False, **kwargs):
    labels, kwargs = get_labels(x, y, z, **kwargs)
    if aligned == False:
        x, y = uf.broadcast(x, y)
    subp_kwargs = {
        k: v
        for k, v in kwargs.items() if k in common.get_kwarg_names(pl.subplots)
    }
    cont_kwargs = {k: v for k, v in kwargs.items() if k not in subp_kwargs}

    if x.dtype.kind == 'M':
        x = x.astype(datetime)
    if y.dtype.kind == 'M':
        y = y.astype(datetime)

    fig, ax = pl.subplots(**subp_kwargs)
    levels = np.linspace(0, uf.nanmax(z).values, levels)
    img = ax.pcolormesh(x.values, y.values, z.values, **cont_kwargs)

    # layout
    if x.dtype.kind == 'M':
        fig.autofmt_xdate()
    cbar = fig.colorbar(img, ax=ax)
    cbar.set_label(labels[2])
    ax.set_xlabel(labels[0])
    ax.set_ylabel(labels[1])
    pl.grid = True
    pl.tight_layout()
    return fig, ax
예제 #2
0
파일: analysis.py 프로젝트: TCvanLeth/PyHAD
def semivar(*args, **kwargs):
    """
    semivariance
    """
    args = uf.broadcast(*args)
    X = da.stack([x.task.flatten() for x in args])
    out = 0.5 * da.nanmean((X[0] - X[1])**2)
    return out
예제 #3
0
파일: analysis.py 프로젝트: TCvanLeth/PyHAD
def bootstrap(*args, func='corr', size=100):
    if func == 'corr':
        func = uf.corr

    args = uf.broadcast(*args)
    args = tuple(x.task if hasattr(x, 'task') else x for x in args)
    args = tuple(x.compute() if hasattr(x, 'compute') else x for x in args)

    indices = np.random.randint(len(args[0]), size=(size, len(args[0])))
    return func(*tuple(x[indices] for x in args), axis=1)
예제 #4
0
파일: analysis.py 프로젝트: TCvanLeth/PyHAD
def linregress(x, y, **kwargs):
    """
    linear least-squares regression
    """
    x, y = uf.broadcast(x, y)
    x = x.task.flatten().compute()
    y = y.task.flatten().compute()
    cond = ~(np.isnan(x) | np.isnan(y))
    x = x[cond]
    y = y[cond]
    return stats.linregress(x, y)
예제 #5
0
파일: plotting.py 프로젝트: TCvanLeth/PyHAD
def pixplot(x, y, z, log=False, interp='nearest', aligned=False, **kwargs):
    labels, kwargs = get_labels(x, y, z, **kwargs)
    if aligned == False:
        x, y = uf.broadcast(x, y)
    subp_kwargs = {
        k: v
        for k, v in kwargs.items() if k in common.get_kwarg_names(pl.subplots)
    }
    imsh_kwargs = {k: v for k, v in kwargs.items() if k not in subp_kwargs}

    if x.dtype.kind == 'M':
        xlims = mdates.date2num([
            x.min().values.astype(datetime).item(),
            x.max().values.astype(datetime).item()
        ])
    else:
        xlims = (min(x).values.item(), max(x).values.item())

    if y.dtype.kind == 'M':
        ylims = mdates.date2num([
            y.min().values.astype(datetime).item(),
            y.max().values.astype(datetime).item()
        ])
    else:
        ylims = [y.min().values.item(), y.max().values.item()]

    fig, ax = pl.subplots(**subp_kwargs)

    if log:
        z = uf.log10(z)
    img = ax.imshow(z.values,
                    interpolation=interp,
                    extent=[xlims[0], xlims[1], ylims[0], ylims[1]],
                    origin='lower',
                    aspect='auto',
                    **imsh_kwargs)

    # layout
    if x.dtype.kind == 'M':
        ax.xaxis_date()
        fig.autofmt_xdate()
    cbar = fig.colorbar(img, ax=ax)
    cbar.set_label(labels[2])
    ax.set_xlabel(labels[0])
    ax.set_ylabel(labels[1])
    pl.grid = True
    pl.tight_layout()
    return fig, ax
예제 #6
0
파일: plotting.py 프로젝트: TCvanLeth/PyHAD
def barplot(x, y, width=None, ax=None, fig=None, aligned=False, **kwargs):
    labels, kwargs = get_labels(x, y, **kwargs)
    if aligned == False:
        x, y = uf.broadcast(x, y)
    fig_kwargs = {
        k: v
        for k, v in kwargs.items() if k in common.get_kwarg_names(pl.figure)
    }
    subp_kwargs = {
        k: v
        for k, v in kwargs.items() if k in common.get_kwarg_names(pl.subplot)
    }
    plot_kwargs = {k: v for k, v in kwargs.items() if k not in subp_kwargs}

    if x.dtype.kind == 'M':
        x = x.astype(datetime)
    if y.dtype.kind == 'M':
        y = y.astype(datetime)

    if ax is None:
        if fig is None:
            fig = pl.figure(**fig_kwargs)
        ax = fig.add_subplot(111, **subp_kwargs)

    x = x.values
    if width is None:
        width = 0.5 * x[1] - x[0]
    else:
        try:
            width = width.values
        except AttributeError:
            pass

    ax.bar(x, y.values, align='center', width=width, **plot_kwargs)

    ax.set_xlabel(labels[0])
    ax.set_ylabel(labels[1])
    pl.grid = True
    pl.tight_layout()
    return fig, ax
예제 #7
0
파일: plotting.py 프로젝트: TCvanLeth/PyHAD
def plot(x,
         y,
         style='o',
         dim=None,
         log=False,
         semilogx=False,
         semilogy=False,
         ax=None,
         aligned=False,
         fig=None,
         lloc='best',
         **kwargs):
    xdim = x.dims
    ydim = y.dims
    if xdim != ydim:
        err = 'unequal dimensions: %s and %s' % (str(xdim), str(ydim))
        raise Exception(err)
    if len(xdim) > 1 and dim is None:
        err = 'more than 1 dimension: %s' % str(xdim)
        raise Exception(err)

    labels, kwargs = get_labels(x, y, **kwargs)
    if x.attrs['quantity'] == y.attrs['quantity']:
        kwargs['figsize'] = pl.figaspect(1)
    elif x.attrs['quantity'] == 'time':
        kwargs['figsize'] = (15, 5)

    if aligned == False:
        x, y = uf.broadcast(x, y)
    fig_kwargs = {
        k: v
        for k, v in kwargs.items() if k in common.get_kwarg_names(pl.figure)
    }
    subp_kwargs = {
        k: v
        for k, v in kwargs.items() if k in common.get_kwarg_names(pl.subplot)
    }
    plot_kwargs = {
        k: v
        for k, v in kwargs.items() if k not in {
            **subp_kwargs,
            **fig_kwargs
        }
    }

    if x.dtype.kind == 'M':
        x = x.astype(datetime)
    if y.dtype.kind == 'M':
        y = y.astype(datetime)

    if ax is None:
        if fig is None:
            fig = pl.figure(**fig_kwargs)
        ax = fig.add_subplot(111, **subp_kwargs)

    if log or semilogx:
        x = uf.log(x)

    if log or semilogy:
        y = uf.log(y)

    if dim is not None:
        x = x.split(dim)
        y = y.split(dim)
        for ix, iy in zip(x, y):
            line, = ax.plot(ix[1].values,
                            iy[1].values,
                            style,
                            label=ix[0],
                            **plot_kwargs)
    else:
        line, = ax.plot(x.values, y.values, style, **plot_kwargs)

    # layout
    ax.set_xlabel(labels[0])
    ax.set_ylabel(labels[1])
    if log:

        def f(x, pos):
            return '{:0.2e}'.format(uf.exp(x))

        ax.xaxis.set_major_formatter(mpl.ticker.FuncFormatter(f))
        ax.yaxis.set_major_formatter(mpl.ticker.FuncFormatter(f))

    ax.legend(loc=lloc)
    pl.tight_layout()
    return fig, ax
예제 #8
0
파일: plotting.py 프로젝트: TCvanLeth/PyHAD
def contourplot(x,
                y,
                z,
                log=False,
                levels=100,
                aligned=False,
                maxz=None,
                minz=None,
                zlog=False,
                ax=None,
                fig=None,
                **kwargs):
    labels, kwargs = get_labels(x, y, z, **kwargs)

    if aligned == False:
        x, y = uf.broadcast(x, y)
    fig_kwargs = {
        k: v
        for k, v in kwargs.items() if k in common.get_kwarg_names(pl.figure)
    }
    subp_kwargs = {
        k: v
        for k, v in kwargs.items() if k in common.get_kwarg_names(pl.subplots)
    }

    if x.dtype.kind == 'M':
        x = x.astype(datetime)
    if y.dtype.kind == 'M':
        y = y.astype(datetime)

    if ax is None:
        if fig is None:
            fig = pl.figure(**fig_kwargs)
        ax = fig.add_subplot(111, **subp_kwargs)
    else:
        fig = ax.figure
        if ax.get_xlabel() == labels[1] or ax.get_ylabel() == labels[0]:
            tmp = x.copy()
            x = y.copy()
            y = tmp
            tmp = labels[0]
            labels[0] = labels[1]
            labels[1] = tmp
            z = z.T

    if maxz is None:
        minz = 0
        maxz = uf.nanmax(z).values
    if zlog:
        levels = np.logspace(minz, maxz, levels)
    else:
        levels = np.linspace(minz, maxz, levels)

    z = z.values
    z = np.ma.masked_array(z, mask=np.isnan(z))
    img = ax.contour(x.values, y.values, z, levels=levels, **kwargs)

    # layout
    if log:

        def f(x, pos):
            return '{:0.2e}'.format(uf.exp(x))

        ax.xaxis.set_major_formatter(mpl.ticker.FuncFormatter(f))
        ax.yaxis.set_major_formatter(mpl.ticker.FuncFormatter(f))
    if x.dtype.kind == 'M':
        fig.autofmt_xdate()
    cbar = fig.colorbar(img, ax=ax, format='%.1e')
    cbar.set_label(labels[2])
    ax.set_xlabel(labels[0])
    ax.set_ylabel(labels[1])
    pl.grid = True
    pl.tight_layout()
    return fig, ax