Example #1
0
    def mask_invalid(self) -> 'DataDictBase':
        """
        Mask all invalid data in all values.
        :return: copy of the dataset with invalid entries (nan/None) masked.
        """
        ret = self.copy()
        for d, _ in self.data_items():
            arr = self.data_vals(d)
            vals = np.ma.masked_where(num.is_invalid(arr), arr, copy=True)
            vals.fill_value = np.nan
            ret[d]['values'] = vals

        return ret
Example #2
0
    def mask_invalid(self: T) -> T:
        """
        Mask all invalid data in all values.
        :return: copy of the dataset with invalid entries (nan/None) masked.
        """
        ret = self.copy()
        for d, _ in self.data_items():
            arr = self.data_vals(d)
            vals = np.ma.masked_where(num.is_invalid(arr), arr, copy=True)
            try:
                vals.fill_value = np.nan
            except TypeError:
                vals.fill_value = -9999
            ret[d]['values'] = vals

        return ret
Example #3
0
def colorplot2d(ax: Axes,
                x: Union[np.ndarray, np.ma.MaskedArray],
                y: Union[np.ndarray, np.ma.MaskedArray],
                z: Union[np.ndarray, np.ma.MaskedArray],
                plotType: PlotType = PlotType.image,
                axLabels: Tuple[Optional[str], Optional[str],
                                Optional[str]] = ('', '', ''),
                **kw: Any) -> Optional[AxesImage]:
    """make a 2d colorplot. what plot is made, depends on `plotType`.
    Any of the 2d plot types in :class:`PlotType` works.

    :param ax: matplotlib subPlots to plot in
    :param x: x coordinates (meshgrid)
    :param y: y coordinates (meshgrid)
    :param z: z data
    :param plotType: the plot type
    :param axLabels: labels for the x, y subPlots, and the colorbar.

    all keywords are passed to the actual plotting functions, depending on the ``plotType``:

    - :attr:`PlotType.image` --
        :func:`plotImage`
    - :attr:`PlotType.colormesh` --
        :func:`ppcolormesh_from_meshgrid`
    - :attr:`PlotType.scatter2d` --
        matplotlib's `scatter`
    """
    cmap = kw.pop('cmap', rcParams['image.cmap'])

    # first we need to check if our grid can be plotted nicely.
    if plotType in [PlotType.image, PlotType.colormesh]:
        x = x.astype(float)
        y = y.astype(float)
        z = z.astype(float)

        # first check if we need to fill some masked values in
        if isinstance(x, np.ma.MaskedArray) and np.ma.is_masked(x):
            x = x.filled(np.nan)
        if isinstance(y, np.ma.MaskedArray) and np.ma.is_masked(y):
            y = y.filled(np.nan)
        if isinstance(z, np.ma.MaskedArray) and np.ma.is_masked(z):
            z = z.filled(np.nan)

        # next: try some surgery, if possible
        if np.all(num.is_invalid(x)) or np.all(num.is_invalid(y)):
            return None
        if np.any(np.isnan(x)) or np.any(np.isnan(y)):
            x, y = interp_meshgrid_2d(x, y)
        if np.any(num.is_invalid(x)) or np.any(num.is_invalid(y)):
            x, y, z = num.crop2d(x, y, z)

        # next, check if the resulting grids are even still plottable
        for g in x, y, z:
            if g.size == 0:
                return None
            elif len(g.shape) < 2:
                return None

            # special case: if we have a single line, a pcolor-type plot won't work.
            elif min(g.shape) < 2:
                plotType = PlotType.scatter2d

    if plotType is PlotType.image:
        im = plotImage(ax, x, y, z, cmap=cmap, **kw)
    elif plotType is PlotType.colormesh:
        im = ppcolormesh_from_meshgrid(ax, x, y, z, cmap=cmap, **kw)
    elif plotType is PlotType.scatter2d:
        im = ax.scatter(x.ravel(), y.ravel(), c=z.ravel(), cmap=cmap, **kw)
    else:
        im = None

    if im is None:
        return None

    ax.set_xlabel(axLabels[0])
    ax.set_ylabel(axLabels[1])
    return im