Exemplo n.º 1
0
 def testBinCenterToEdges(self):
     """Convert a set of bin centers to bin edges"""
     inputs = [[1, 2, 3, 4, 5],
               [12.4, 77, 100],]
     outputs = [[0.5, 1.5, 2.5, 3.5, 4.5, 5.5],
                [-19.9, 44.7, 88.5, 111.5],]
     for i, val in enumerate(inputs):
         numpy.testing.assert_almost_equal(outputs[i], tb.bin_center_to_edges(val))
Exemplo n.º 2
0
 def testBinCenterToEdges(self):
     """Convert a set of bin centers to bin edges"""
     inputs = [[1, 2, 3, 4, 5],
               [12.4, 77, 100],]
     outputs = [[0.5, 1.5, 2.5, 3.5, 4.5, 5.5],
                [-19.9, 44.7, 88.5, 111.5],]
     for i, val in enumerate(inputs):
         numpy.testing.assert_almost_equal(outputs[i], tb.bin_center_to_edges(val))
Exemplo n.º 3
0
    def plot_mult(self,
                  windows,
                  data,
                  min=None,
                  max=None,
                  cbar_label=None,
                  figsize=None,
                  dpi=80,
                  xlabel='Lag',
                  ylabel='Window Size'):
        """Plots a 2D function of window size and lag

        Parameters
        ==========
        windows : list
            list of window sizes (y axis)
        data : list
            list of data, dimensioned (windows x lags)
        min : float, optional
            clip L{data} to this minimum value
        max : float, optional
            clip L{data} to this maximum value
        """
        import matplotlib.pyplot as plt

        x = np.array(tb.bin_center_to_edges(self.lags))
        y = np.array(tb.bin_center_to_edges(windows))

        fig = plt.figure(figsize=figsize, dpi=dpi)
        ax0 = fig.add_subplot(111)
        cax = ax0.pcolormesh(x, y, data, vmin=min, vmax=max, shading='flat')
        ax0.set_xlim((x[0], x[-1]))
        ax0.set_ylim((y[0], y[-1]))
        plt.xlabel(xlabel)
        plt.ylabel(ylabel)
        if cbar_label == None:
            if plt.rcParams['text.usetex']:
                cbar_label = r'\% confident above asymptotic association'
            else:
                cbar_label = r'% confident above asymptotic association'
        plt.colorbar(cax, fraction=0.05).set_label(cbar_label)
        return fig
Exemplo n.º 4
0
    def plot_mult(self, windows, data, min=None, max=None, cbar_label=None,
                  figsize=None, dpi=80,
                  xlabel='Lag', ylabel='Window Size'):
        """Plots a 2D function of window size and lag

        Parameters
        ==========
        windows : list
            list of window sizes (y axis)
        data : list
            list of data, dimensioned (windows x lags)
        min : float, optional
            clip L{data} to this minimum value
        max : float, optional
            clip L{data} to this maximum value
        """
        import matplotlib.pyplot as plt

        x = np.array(tb.bin_center_to_edges(self.lags))
        y = np.array(tb.bin_center_to_edges(windows))

        fig = plt.figure(figsize=figsize, dpi=dpi)
        ax0 = fig.add_subplot(111)
        cax = ax0.pcolormesh(x, y, data, vmin=min, vmax=max,
                             shading='flat')
        ax0.set_xlim((x[0], x[-1]))
        ax0.set_ylim((y[0], y[-1]))
        plt.xlabel(xlabel)
        plt.ylabel(ylabel)
        if cbar_label == None:
            if plt.rcParams['text.usetex']:
                cbar_label = r'\% confident above asymptotic association'
            else:
                cbar_label = r'% confident above asymptotic association'
        plt.colorbar(cax, fraction=0.05).set_label(cbar_label)
        return fig
Exemplo n.º 5
0
def simpleSpectrogram(*args, **kwargs):
    """
    Plot a spectrogram given Z or X,Y,Z. This is a wrapper around pcolormesh()
    that can handle Y being a 2d array of time dependent bins. Like in the
    Van Allen Probes HOPE and MagEIS data files.

    Parameters
    ==========
    *args : 1 or 3 arraylike
        Call Signatures::

        simpleSpectrogram(Z, **kwargs)
        simpleSpectrogram(X, Y, Z, **kwargs)

    Other Parameters
    ================
    zlog : bool
        Plot the color with a log colorbar (default: True)
    ylog : bool
        Plot the Y axis with a log scale (default: True) 
    alpha : scalar (0-1)
        The alpha blending value (default: None)
    cmap : string
        The name of the colormap to use (default: system default)
    vmin : float
        Minimum color value (default: Z.min(), if log non-zero min)
    vmax : float
        Maximum color value (default: Z.max())
    ax : matplotlib.axes
        Axes to plot the spectrogram on (default: None - new axes)
    cb : bool
        Plot a colorbar (default: True)
    cbtitle : string
        Label to go on the colorbar (default: None)
    
    Returns
    =======
    ax : matplotlib.axes._subplots.AxesSubplot
        Matplotlib axes object that the plot is on
    """
    if len(args) not in [1,3]:
        raise(TypeError("simpleSpectrogram, takes Z or X, Y, Z"))

    if len(args) == 1: # just Z in, makeup an X and Y
        Z = np.ma.masked_invalid(np.asarray(args[0])) # make sure not a dmarray or VarCopy
        X = np.arange(Z.shape[0]+1)
        Y = np.arange(Z.shape[1]+1)
    else:
        # we really want X and Y to be one larger than Z
        X = np.asarray(args[0]) # make sure not a dmarray or VarCopy
        Y = np.asarray(args[1]) # make sure not a dmarray or VarCopy
        Z = np.ma.masked_invalid(np.asarray(args[2]))
        if X.shape[0] == Z.shape[0]: # same length, expand X
            X = tb.bin_center_to_edges(X) # hopefully evenly spaced
        if len(Y.shape) == 1: # 1d, just use as axis
            Y = tb.bin_center_to_edges(Y) # hopefully evenly spaced
        elif len(Y.shape) == 2:
            Y_orig = Y
            # 2d this is time dependent and thus need to overplot several
            Y = tb.unique_columns(Y, axis=1)
            if Y.shape[1] == Z.shape[1]: # hopefully evenly spaced
                Y_uniq = Y
                Y_tmp = np.empty((Y.shape[0], Y.shape[1]+1), dtype=Y.dtype)
                for ii in range(Y.shape[0]):
                    Y_tmp[ii] = tb.bin_center_to_edges(Y[ii])
                Y = Y_tmp
                
    # deal with all the default keywords 
    zlog    = kwargs.pop('zlog', True)
    ylog    = kwargs.pop('ylog', True)
    alpha   = kwargs.pop('alpha', None)
    cmap    = kwargs.pop('cmap', None)
    vmin    = kwargs.pop('vmin', np.min(Z) if not zlog else np.min(Z[np.nonzero(Z)]))
    vmax    = kwargs.pop('vmax', np.max(Z))
    ax      = kwargs.pop('ax', None)
    cb      = kwargs.pop('cb', True)
    cbtitle = kwargs.pop('cbtitle', None)
    
    # the first case is that X, Y are 1d and Z is 2d, just make the plot
    if ax is None:
        fig = plt.figure()
        ax = fig.add_subplot(111)
    else:
        fig = ax.get_figure()
    Z = Z.filled(0)
    if len(Y.shape) > 1:
        for yy in Y_uniq:
            # which indices in X have these values
            ind = (yy == Y_orig).all(axis=1)
            print(Y_orig[ind][0].min(), Y_orig[ind][0].max())
            Y_tmp = np.zeros_like(Y_orig)
            Y_tmp[ind] = Y_orig[ind][0]
            Z_tmp = np.zeros_like(Z)
            Z_tmp[ind] = Z[ind]
            pc = ax.pcolormesh(X, Y_tmp[ind][0], Z_tmp.T,
                               norm=LogNorm(vmin=vmin, vmax=vmax), cmap=cmap,
                               alpha=alpha)
    else:
        pc = ax.pcolormesh(X, Y, Z.T, norm=LogNorm(vmin=vmin, vmax=vmax),
                           cmap=cmap,
                           alpha=alpha)
    if ylog: ax.set_yscale('log')
    
    if cb: # add a colorbar
        cb_ = fig.colorbar(pc)
        cb_.set_label(cbtitle)
    return ax
Exemplo n.º 6
0
def simpleSpectrogram(*args, **kwargs):
    """
    Plot a spectrogram given Z or X,Y,Z. This is a wrapper around pcolormesh()
    that can handle Y being a 2d array of time dependent bins. Like in the
    Van Allen Probes HOPE and MagEIS data files.

    Parameters
    ==========
    *args : 1 or 3 arraylike
        Call Signatures::

        simpleSpectrogram(Z, **kwargs)
        simpleSpectrogram(X, Y, Z, **kwargs)

    Other Parameters
    ================
    zlog : bool
        Plot the color with a log colorbar (default: True)
    ylog : bool
        Plot the Y axis with a log scale (default: True) 
    alpha : scalar (0-1)
        The alpha blending value (default: None)
    cmap : string
        The name of the colormap to use (default: system default)
    vmin : float
        Minimum color value (default: Z.min(), if log non-zero min)
    vmax : float
        Maximum color value (default: Z.max())
    ax : matplotlib.axes
        Axes to plot the spectrogram on (default: None - new axes)
    cb : bool
        Plot a colorbar (default: True)
    cbtitle : string
        Label to go on the colorbar (default: None)
    
    Returns
    =======
    ax : matplotlib.axes._subplots.AxesSubplot
        Matplotlib axes object that the plot is on
    """
    if len(args) not in [1, 3]:
        raise (TypeError("simpleSpectrogram, takes Z or X, Y, Z"))

    if len(args) == 1:  # just Z in, makeup an X and Y
        Z = np.ma.masked_invalid(np.asarray(
            args[0]))  # make sure not a dmarray or VarCopy
        X = np.arange(Z.shape[0] + 1)
        Y = np.arange(Z.shape[1] + 1)
    else:
        # we really want X and Y to be one larger than Z
        X = np.asarray(args[0])  # make sure not a dmarray or VarCopy
        Y = np.asarray(args[1])  # make sure not a dmarray or VarCopy
        Z = np.ma.masked_invalid(np.asarray(args[2]))
        if X.shape[0] == Z.shape[0]:  # same length, expand X
            X = tb.bin_center_to_edges(X)  # hopefully evenly spaced
        if len(Y.shape) == 1:  # 1d, just use as axis
            if Y.shape[0] == Z.shape[1]:  # same length, expand Y
                Y = tb.bin_center_to_edges(Y)  # hopefully evenly spaced
        elif len(Y.shape) == 2:
            Y_orig = Y
            # 2d this is time dependent and thus need to overplot several
            Y = tb.unique_columns(Y, axis=1)
            if Y.shape[1] == Z.shape[1]:  # hopefully evenly spaced
                Y_uniq = Y
                Y_tmp = np.empty((Y.shape[0], Y.shape[1] + 1), dtype=Y.dtype)
                for ii in range(Y.shape[0]):
                    Y_tmp[ii] = tb.bin_center_to_edges(Y[ii])
                Y = Y_tmp

    # deal with all the default keywords
    zlog = kwargs.pop('zlog', True)
    ylog = kwargs.pop('ylog', True)
    alpha = kwargs.pop('alpha', None)
    cmap = kwargs.pop('cmap', None)
    vmin = kwargs.pop('vmin',
                      np.min(Z) if not zlog else np.min(Z[np.nonzero(Z)]))
    vmax = kwargs.pop('vmax', np.max(Z))
    ax = kwargs.pop('ax', None)
    cb = kwargs.pop('cb', True)
    cbtitle = kwargs.pop('cbtitle', None)

    # the first case is that X, Y are 1d and Z is 2d, just make the plot
    if ax is None:
        fig = plt.figure()
        ax = fig.add_subplot(111)
    else:
        fig = ax.get_figure()
    Z = Z.filled(0)
    if len(Y.shape) > 1:
        for yy in Y_uniq:
            # which indices in X have these values
            ind = (yy == Y_orig).all(axis=1)
            print(Y_orig[ind][0].min(), Y_orig[ind][0].max())
            Y_tmp = np.zeros_like(Y_orig)
            Y_tmp[ind] = Y_orig[ind][0]
            Z_tmp = np.zeros_like(Z)
            Z_tmp[ind] = Z[ind]
            pc = ax.pcolormesh(X,
                               Y_tmp[ind][0],
                               Z_tmp.T,
                               norm=LogNorm(vmin=vmin, vmax=vmax),
                               cmap=cmap,
                               alpha=alpha)
    else:
        pc = ax.pcolormesh(X,
                           Y,
                           Z.T,
                           norm=LogNorm(vmin=vmin, vmax=vmax),
                           cmap=cmap,
                           alpha=alpha)
    if ylog: ax.set_yscale('log')

    if cb:  # add a colorbar
        cb_ = fig.colorbar(pc)
        if cbtitle is not None:
            cb_.set_label(cbtitle)
    return ax
Exemplo n.º 7
0
 def testBinEdgesToCenterToEdges_datetimeroundtrip(self):
     """Convert a set of datetime bin edges to centers and back to bin edges"""
     inputs = [datetime.datetime(2012,9,3,n) for n in range(10)]
     computedOut = tb.bin_edges_to_center(inputs)
     roundtripResults = tb.bin_center_to_edges(computedOut)
     self.assertTrue((inputs == roundtripResults).all())
Exemplo n.º 8
0
 def testBinEdgesToCenterToEdges_datetimeroundtrip(self):
     """Convert a set of datetime bin edges to centers and back to bin edges"""
     inputs = [datetime.datetime(2012,9,3,n) for n in range(10)]
     computedOut = tb.bin_edges_to_center(inputs)
     roundtripResults = tb.bin_center_to_edges(computedOut)
     self.assertTrue((inputs == roundtripResults).all())
            axes.append(fig.add_subplot(nplots, 1, i + 1))

        # set the titles
        axes[0].set_title(conf['panel']['title'], fontsize='larger')
        for i, ax in enumerate(axes, 1):
            ax.set_ylabel(conf['plot{0}'.format(i)]['ylabel'], fontsize='larger')

            # plot in the color
            ax.imshow(out[i - 1][ind_d][0], cmap=cmap, vmin=0, vmax=3,
                      interpolation='nearest', aspect='auto')

            yticklabels = _get_yticklabels(conf, i)

            ax.set_yticks(np.arange(out[0][ind_d][0].shape[0]))
            ax.set_yticklabels(yticklabels)
            ax.set_ylim(tb.bin_center_to_edges(range(out[0][ind_d][0].shape[0]))[0],
                        tb.bin_center_to_edges(range(out[0][ind_d][0].shape[0]))[-1])
            for i in tb.bin_center_to_edges(range(out[0][ind_d][0].shape[0])):
                ax.axhline(i, color='k')

            steps = np.arange(out[0][ind_d][0].shape[1])

            diff = np.diff(steps)[0] / 2
            for ii, i in enumerate(steps):
                if ii % 3 == 0:
                    ax.axvline(i + diff, color='k', lw=2)
                else:
                    ax.axvline(i + diff, color='k', lw=1)
            ## ax.set_xlim(xlim)
            ax.set_xticks(steps[::3])  # +diff)
            ax.set_xticklabels(out[0][ind_d][1][::3])