Example #1
0
def plot_pixel_mask(h, w, pixels, output_path=None):
    """
    Display a binary image that shows the given pixel mask.

    A black image with `w` x `h` pixels is created and the `pixels` are marked
    with white.

    Parameters
    ----------
    h : int
        The height of the image in pixels.
    w : int
        The width of the image in pixels.
    pixels : ndarray
        The 2D array of pixels that make up the mask. Each row is a coordinate
        pair (x, y), such that `coords` has size len(`pixels`) x 2.
    output_path : str, optional
        Path (including file type extension) under which the plot is saved (the
        default value is None which implies, that the plot is not saved).

    Notes
    -----
    The resulting image is displayed in a figure using
    `magni.imaging.visualisation.imshow`.

    Examples
    --------
    For example,

    >>> from magni.imaging.measurements import plot_pixel_mask
    >>> h = 3
    >>> w = 3
    >>> pixels = np.array([[0, 0], [1, 1], [2, 1]])
    >>> plot_pixel_mask(h, w, pixels)

    """

    _validate_plot_pixel_mask(h, w, pixels, output_path)

    mask = np.zeros((h, w))
    mask[pixels[:, 1], pixels[:, 0]] = 1

    fs = (np.abs([h, w])).min() / 10
    _plotting.setup_matplotlib({'figure': {'figsize': (h / fs, w / fs)}},
                               cmap='gray')

    fig, axes = plt.subplots(1, 1)
    _imshow(mask, ax=axes, show_axis='top')

    axes.set_xlabel('width [pixels]')
    axes.set_ylabel('height [pixels]')

    plt.tight_layout()

    if output_path is not None:
        plt.savefig(output_path)
Example #2
0
def plot_pattern(l, w, coords, mode, output_path=None):
    """
    Display a plot that shows the pattern given by a set of coordinates.

    The pattern given by the `coords` is displayed on an `w` x `l` area. If
    `mode` is 'surface', `l` and `w` are regarded as measured in meters. If
    `mode` is 'image', `l` and `w` are regarded as measured in pixels. The
    `coords` are marked by filled circles and connected by straight dashed
    lines.

    Parameters
    ----------
    l : float or int
        The length/height of the area. If `mode` is 'surface', it must be a
        float. If `mode` is 'image', it must be an integer.
    w : float or int
        The width of the area. If `mode` is 'surface', it must be a
        float. If `mode` is 'image', it must be an integer.
    coords : ndarray
        The 2D array of pixels that make up the mask. Each row is a coordinate
        pair (x, y).
    mode : {'surface', 'image'}
        The display mode that dertermines the axis labeling and the type of `l`
        and `w`.
    output_path : str, optional
        Path (including file type extension) under which the plot is saved (the
        default value is None which implies, that the plot is not saved).

    Notes
    -----
    The resulting plot is displayed in a figure using `matplotlib`'s
    `pyplot.plot`.

    Examples
    --------
    For example,

    >>> import numpy as np
    >>> from magni.imaging.measurements import plot_pattern
    >>> l = 3
    >>> w = 3
    >>> coords = np.array([[0, 0], [1, 1], [2, 1]])
    >>> mode = 'image'
    >>> plot_pattern(l, w, coords, mode)

    """

    @_decorate_validation
    def validate_input():
        _generic('mode', 'string', value_in=('surface', 'image'))

        if mode == 'surface':
            _numeric('l', 'floating', range_='[{};inf)'.format(_min_l))
            _numeric('w', 'floating', range_='[{};inf)'.format(_min_w))
            _numeric('coords', 'floating', shape=(-1, 2))
            _numeric('coords[:, 0]', 'floating', range_='[0;{}]'.format(w),
                     shape=(-1,), var=coords[:, 0])
            _numeric('coords[:, 1]', 'floating', range_='[0;{}]'.format(l),
                     shape=(-1,), var=coords[:, 1])
        elif mode == 'image':
            _numeric('l', 'integer', range_='[2;inf)')
            _numeric('w', 'integer', range_='[2;inf)')
            _numeric('coords', ('integer', 'floating'), shape=(-1, 2))
            _numeric('coords[:, 0]', ('integer', 'floating'),
                     range_='[0;{})'.format(w), shape=(-1,),
                     var=coords[:, 0])
            _numeric('coords[:, 1]', ('integer', 'floating'),
                     range_='[0;{})'.format(l), shape=(-1,),
                     var=coords[:, 1])

        _generic('output_path', 'string', ignore_none=True)

    validate_input()

    figsize = plt.rcParams['figure.figsize']

    if w / l > figsize[0] / figsize[1]:
        figsize_local = [figsize[0], figsize[0] * l / w]
    else:
        figsize_local = [figsize[1] * w / l, figsize[1]]

    _plotting.setup_matplotlib({'figure': {'figsize': figsize_local}})

    fig, axes = plt.subplots(1, 1)
    axes.plot(coords[:, 0], coords[:, 1],
              marker=_plotting.markers[0],
              ls=_plotting.linestyles[1],
              ms=6)

    axes.set_xlim([0, w])
    axes.xaxis.tick_top()
    axes.invert_yaxis()
    axes.set_ylim([l, 0])
    axes.xaxis.set_label_position('top')
    axes.set_aspect('equal')

    if mode == 'surface':
        axes.set_xlabel('width [m]')
        axes.set_ylabel('length [m]')
        axes.ticklabel_format(style='sci', scilimits=(0, 0))

        # Fix position of exponential multipliers in scientific notation
        exp_texts = [axes.xaxis.get_children()[1],
                     axes.yaxis.get_children()[1]]
        vert_aligns = ['bottom', 'center']
        hori_aligns = ['center', 'right']
        xy_texts = [(0, 25), (-35, 0)]
        xys = [(1, 1), (0, 0)]
        annos = [None, None]
        for k, text in enumerate(exp_texts):
            text.set_visible(False)
            annos[k] = axes.annotate('', xys[k], xytext=xy_texts[k],
                                     xycoords='axes fraction',
                                     textcoords='offset points',
                                     va=vert_aligns[k], ha=hori_aligns[k])

        fig.canvas.mpl_connect(
            'draw_event', lambda event: annos[0].set_text(
                exp_texts[0].get_text()))
        fig.canvas.mpl_connect(
            'draw_event', lambda event: annos[1].set_text(
                exp_texts[1].get_text()))

    elif mode == 'image':
        axes.set_xlabel('width [pixels]')
        axes.set_ylabel('height [pixels]')

    plt.tight_layout()

    if output_path is not None:
        plt.savefig(output_path)

    _plotting.setup_matplotlib({'figure': {'figsize': figsize}})
Example #3
0
def plot_pixel_mask(h, w, pixels, output_path=None):
    """
    Display a binary image that shows the given pixel mask.

    A black image with `w` x `h` pixels is created and the `pixels` are marked
    with white.

    Parameters
    ----------
    h : int
        The height of the image in pixels.
    w : int
        The width of the image in pixels.
    pixels : ndarray
        The 2D array of pixels that make up the mask. Each row is a coordinate
        pair (x, y), such that `coords` has size len(`pixels`) x 2.
    output_path : str, optional
        Path (including file type extension) under which the plot is saved (the
        default value is None which implies, that the plot is not saved).

    Notes
    -----
    The resulting image is displayed in a figure using
    `magni.imaging.visualisation.imshow`.

    Examples
    --------
    For example,

    >>> import numpy as np
    >>> from magni.imaging.measurements import plot_pixel_mask
    >>> h = 3
    >>> w = 3
    >>> pixels = np.array([[0, 0], [1, 1], [2, 1]])
    >>> plot_pixel_mask(h, w, pixels)

    """

    @_decorate_validation
    def validate_input():
        _numeric('h', 'integer', range_='[2;inf)')
        _numeric('w', 'integer', range_='[2;inf)')
        _numeric('pixels', 'integer', shape=(-1, 2))
        _numeric('pixels[:, 0]', 'integer', range_='[0;{}]'.format(w - 1),
                 shape=(-1,), var=pixels[:, 0])
        _numeric('pixels[:, 1]', 'integer', range_='[0;{}]'.format(h - 1),
                 shape=(-1,), var=pixels[:, 1])
        _generic('output_path', 'string', ignore_none=True)

    validate_input()

    mask = _util.construct_pixel_mask(h, w, pixels)

    figsize = plt.rcParams['figure.figsize']

    if w / h > figsize[0] / figsize[1]:
        figsize_local = [figsize[0], figsize[0] * h / w]
    else:
        figsize_local = [figsize[1] * w / h, figsize[1]]

    _plotting.setup_matplotlib({'figure': {'figsize': figsize_local}})

    fig, axes = plt.subplots(1, 1)
    _imshow(mask, ax=axes, show_axis='top')

    axes.set_xlabel('width [pixels]')
    axes.set_ylabel('height [pixels]')

    plt.tight_layout()

    if output_path is not None:
        plt.savefig(output_path)

    _plotting.setup_matplotlib({'figure': {'figsize': figsize}})
Example #4
0
def imshow(X, ax=None, intensity_func=None, intensity_args=(),
           show_axis='frame', **kwargs):
    """
    Display an image.

    Wrap `matplotlib.pyplot.imshow` to display a possibly intensity manipulated
    version of the image `X`.

    Parameters
    ----------
    X : ndarray
        The image to be displayed.
    ax : matplotlib.axes.Axes, optional
        The axes on which the image is displayed (the default is None, which
        implies that the current axes is used).
    intensity_func : FunctionType, optional
        The handle to the function used to manipulate the image intensity
        before the image is displayed (the default is None, which implies that
        no intensity manipulation is used).
    intensity_args : list or tuple, optional
        The arguments that are passed to the `intensity_func` (the default is
        (), which implies that no arguments are passed).
    show_axis : {'none', 'top', 'inherit', 'frame'}
        How the x- and y-axis are display. If 'none', no axis are displayed. If
        'top', the x-axis is displayed at the top of the image. If 'inherit',
        the axis display is inherited from `matplotlib.pyplot.imshow`. If
        'frame' only the frame is shown and not the ticks.

    Returns
    -------
    im_out : matplotlib.image.AxesImage
        The AxesImage returned by matplotlibs imshow.

    See Also
    --------
    matplotlib.pyplot.imshow : Matplotlib's imshow function.

    Examples
    --------
    For example,

    >>> import numpy as np
    >>> from magni.imaging.visualisation import imshow
    >>> X = np.arange(4).reshape(2, 2)
    >>> add_k = lambda X, k: X + k
    >>> im_out = imshow(X, intensity_func=add_k, intensity_args=(2,))

    """

    @_decorate_validation
    def validate_input():
        _numeric('X', ('boolean', 'integer', 'floating'), shape=(-1, -1))
        _generic('ax', mpl.axes.Axes, ignore_none=True)
        _generic('intensity_func', 'function', ignore_none=True)
        _generic('intensity_args', 'explicit collection')
        _generic('show_axis', 'string', value_in=('none', 'top', 'inherit',
                                                  'frame'))

    validate_input()

    _plotting.setup_matplotlib()

    # Handle ax keyword argument
    ca = plt.gca()
    if ax is not None:
        plt.sca(ax)

    # Intensity manipulation
    if intensity_func is not None:
        im_out = plt.imshow(intensity_func(X, *intensity_args), **kwargs)
    else:
        im_out = plt.imshow(X, **kwargs)

    # Display of axis
    axes = plt.gca()
    if show_axis == 'none':
        axes.axis('off')
    elif show_axis == 'top':
        axes.xaxis.tick_top()
        axes.xaxis.set_label_position('top')
    elif show_axis == 'frame':
        xlabels = axes.get_xticklabels()
        ylabels = axes.get_yticklabels()
        empty_xlabels = ['']*len(xlabels)
        empty_ylabels = ['']*len(ylabels)
        axes.set_xticklabels(empty_xlabels)
        axes.set_yticklabels(empty_ylabels)
        axes.tick_params(length=0)

    plt.sca(ca)

    return im_out
Example #5
0
def plot_phase_transitions(curves, plot_l1=True, output_path=None):
    r"""
    Plot of a set of phase transition boundary curves.

    The set of phase transition boundary curves are plotted an saved under the
    `output_path`, if specified. The `curves` must be a list of dictionaries
    each having keys *delta*, *rho*, and *label*. *delta* must be an `ndarray`
    of :math:`\delta` values in the phase space. *rho* must be an `ndarray` of
    the corresponding :math:`\rho` values in the phase space. *label* must be a
    `str` describing the curve.

    Parameters
    ----------
    curves : list
        A list of dicts describing the curves to plot.
    plot_l1 : bool, optional
        Whether or not to plot the theoretical :math:`\ell_1` curve (the
        default is True).
    output_path : str, optional
        Path (including file type extension) under which the plot is saved (the
        default value is None which implies, that the plot is not saved).

    Notes
    -----
    The plotting is done using `matplotlib`, which implies that an open figure
    containing the plot will result from using this function.

    Tabulated values of the theoretical :math:`\ell_1` phase transition
    boundary is available at
    http://people.maths.ox.ac.uk/tanner/polytopes.shtml

    Examples
    --------
    For example,

    >>> from magni.cs.phase_transition.plotting import plot_phase_transitions
    >>> delta = np.array([0.1, 0.2, 0.9])
    >>> rho = np.array([0.1, 0.3, 0.8])
    >>> curves = [{'delta': delta, 'rho': rho, 'label': 'data1'}]
    >>> output_path = 'phase_transitions.pdf'
    >>> plot_phase_transitions(curves, output_path=output_path)

    """

    _validate_plot_phase_transitions(curves, plot_l1, output_path)

    _plotting.setup_matplotlib()
    fig, axes = plt.subplots(1, 1)

    for curve in curves:
        axes.plot(curve['delta'], curve['rho'], label=curve['label'])

    if plot_l1:
        _plot_theoretical_l1(axes)

    axes.legend(loc='upper left')
    axes.set_xlabel(r'$\delta = m/n$')
    axes.set_ylabel(r'$\rho = k/m$')

    plt.tight_layout()

    if output_path is not None:
        plt.savefig(output_path)
Example #6
0
def plot_phase_transition_colormap(dist, delta, rho, plot_l1=True,
                                   output_path=None):
    r"""
    Create a colormap of the phase space reconstruction probabilities.

    The `delta` and `rho` values span a 2D grid in the phase space.
    Reconstruction probabilities are then calculated from the `dist` 3D array
    of reconstruction error distances. The resulting 2D grid of reconstruction
    probabilites is visualised over the square centers in this 2D grid using a
    colormap. Values in this grid at lower indicies correspond to lower values
    of :math:`\delta` and :math:`\rho`. If `plot_l1` is True, then the
    theoretical l1 curve is overlayed the colormap. The colormap is saved under
    the `output_path`, if specified.

    Parameters
    ----------
    dist : ndarray
        A 3D array of reconstruction error distances.
    delta : ndarray
        :math:`\delta` values used in the 2D grid.
    rho : ndarray
        :math:`\rho` values used in the 2D grid.
    plot_l1 : bool
        Whether or not to plot the theoretical :math:`\ell_1` curve. (the
        default is True)
    output_path : str, optional
        Path (including file type extension) under which the plot is saved (the
        default value is None which implies, that the plot is not saved).

    See Also
    --------
    magni.cs.phase_transition.io.load_phase_transition : Loading phase
        transitions from an HDF database.

    Notes
    -----
    The plotting is done using `matplotlib`, which implies that an open figure
    containing the plot will result from using this function.

    The values in `delta` and `rho` are assumed to be equally spaced.

    Due to the *centering* of the color coded rectangles, they are not
    necessarily square towards the ends of the intervals defined by `delta` and
    `rho`.

    Tabulated values of the theoretical :math:`\ell_1` phase transition
    boundary is available at
    http://people.maths.ox.ac.uk/tanner/polytopes.shtml

    Examples
    --------
    For example,

    >>> from magni.cs.phase_transition.plotting import (
    ...     plot_phase_transition_colormap)
    >>> delta = np.array([0.2, 0.5, 0.8])
    >>> rho = np.array([0.3, 0.6])
    >>> dist = np.array([[[1.35e-08, 1.80e-08], [1.08, 1.11]],
    ... [[1.40e-12, 8.32e-12], [8.57e-01, 7.28e-01]], [[1.92e-13, 1.17e-13],
    ... [2.10e-10,   1.12e-10]]])
    >>> out_path = 'phase_transition_cmap.pdf'
    >>> plot_phase_transition_colormap(dist, delta, rho, output_path=out_path)

    """

    _validate_plot_phase_transition_colormap(dist, delta, rho, plot_l1,
                                             output_path)

    probs = np.sum(dist < 1e-4, axis=-1, dtype=float) / np.size(dist, -1)

    d_delta = (delta[1] - delta[0]) / 2
    d_rho = (rho[1] - rho[0]) / 2

    x = np.hstack((np.array([0]), delta[1:] - d_delta, np.array([1])))
    y = np.hstack((np.array([0]), rho[1:] - d_rho, np.array([1])))

    X, Y = np.meshgrid(x, y)

    _plotting.setup_matplotlib()
    fig, axes = plt.subplots(1, 1)
    p_mesh = axes.pcolormesh(X, Y, probs.T, vmin=0, vmax=1, edgecolor='face')
    c_bar = plt.colorbar(p_mesh)
    c_bar.set_label('Estimated probability of reconstruction')

    # Apply vector graphics render workaround to avoid white gaps in colorbar
    # See: http://matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.colorbar
    c_bar.solids.set_edgecolor("face")

    if plot_l1:
        _plot_theoretical_l1(axes)

    axes.legend(loc='upper left')
    axes.set_xticks(np.arange(11) / 10)
    axes.set_yticks(np.arange(11) / 10)
    axes.set_xlabel(r'$\delta = m/n$')
    axes.set_ylabel(r'$\rho = k/m$')

    plt.tight_layout()

    if output_path is not None:
        plt.savefig(output_path)
Example #7
0
def plot_pattern(l, w, coords, mode, output_path=None):
    """
    Display a plot that shows the pattern given by a set of coordinates.

    The pattern given by the `coords` is displayed on an `w` x `l` area. If
    `mode` is 'surface', `l` and `w` are regarded as measured in meters. If
    `mode` is 'image', `l` and `w` are regarded as measured in pixels. The
    `coords` are marked by filled circles and connected by straight dashed
    lines.

    Parameters
    ----------
    l : float or int
        The length/height of the area. If `mode` is 'surface', it must be a
        float. If `mode` is 'image', it must be an integer.
    w : float or int
        The width of the area. If `mode` is 'surface', it must be a
        float. If `mode` is 'image', it must be an integer.
    coords : ndarray
        The 2D array of pixels that make up the mask. Each row is a coordinate
        pair (x, y).
    mode : {'surface', 'image'}
        The display mode that dertermines the axis labeling and the type of `l`
        and `w`.
    output_path : str, optional
        Path (including file type extension) under which the plot is saved (the
        default value is None which implies, that the plot is not saved).

    Notes
    -----
    The resulting plot is displayed in a figure using `matplotlib`'s
    `pyplot.plot`.

    Examples
    --------
    For example,

    >>> from magni.imaging.measurements import plot_pattern
    >>> l = 3
    >>> w = 3
    >>> coords = np.array([[0, 0], [1, 1], [2, 1]], dtype=np.float)
    >>> mode = 'image'
    >>> plot_pattern(l, w, coords, mode)

    """

    _validate_plot_pattern(l, w, coords, mode, output_path)

    fs = (np.abs([l, w])).min() / 10
    _plotting.setup_matplotlib({'figure': {'figsize': (l / fs, w / fs)}})

    fig, axes = plt.subplots(1, 1)
    axes.plot(coords[:, 0], coords[:, 1],
              marker=_plotting.markers[0],
              ls=_plotting.linestyles[1],
              ms=6)

    axes.set_xlim([0, w])
    axes.xaxis.tick_top()
    axes.invert_yaxis()
    axes.set_ylim([l, 0])
    axes.xaxis.set_label_position('top')
    axes.set_aspect('equal')

    if mode == 'surface':
        axes.set_xlabel('width [m]')
        axes.set_ylabel('length [m]')
        axes.ticklabel_format(style='sci', scilimits=(0, 0))

        # Fix position of exponential multipliers in scientific notation
        exp_texts = [axes.xaxis.get_children()[1],
                     axes.yaxis.get_children()[1]]
        vert_aligns = ['bottom', 'center']
        hori_aligns = ['center', 'right']
        xy_texts = [(0, 25), (-35, 0)]
        xys = [(1, 1), (0, 0)]
        annos = [None, None]
        for k, text in enumerate(exp_texts):
            text.set_visible(False)
            annos[k] = axes.annotate('', xys[k], xytext=xy_texts[k],
                                     xycoords='axes fraction',
                                     textcoords='offset points',
                                     va=vert_aligns[k], ha=hori_aligns[k])

        fig.canvas.mpl_connect(
            'draw_event', lambda event: annos[0].set_text(
                exp_texts[0].get_text()))
        fig.canvas.mpl_connect(
            'draw_event', lambda event: annos[1].set_text(
                exp_texts[1].get_text()))

    elif mode == 'image':
        axes.set_xlabel('width [pixels]')
        axes.set_ylabel('height [pixels]')

    plt.tight_layout()

    if output_path is not None:
        plt.savefig(output_path)
Example #8
0
def imshow(X, ax=None, intensity_func=None, intensity_args=(),
           show_axis='none', **kwargs):
    """
    Display an image.

    Wrap `matplotlib.pyplot.imshow` to display a possibly intensity manipulated
    version of the image `X`.

    Parameters
    ----------
    X : ndarray
        The image to be displayed.
    ax : matplotlib.axes.Axes, optional
        The axes on which the image is displayed (the default is None, which
        implies that the current axes is used).
    intensity_func : FunctionType, optional
        The handle to the function used to manipulate the image intensity
        before the image is displayed (the default is None, which implies that
        no intensity manipulation is used).
    intensity_args : list or tuple, optional
        The arguments that are passed to the `intensity_func` (the default is
        (), which implies that no arguments are passed).
    show_axis : {'none', 'top', 'inherit'}
        How the x- and y-axis are display. If 'none', no axis are displayed. If
        'top', the x-axis is displayed at the top of the image. If 'inherit',
        the axis display is inherited from `matplotlib.pyplot.imshow`.

    Returns
    -------
    im_out : matplotlib.image.AxesImage
        The AxesImage returned by matplotlibs imshow.

    See Also
    --------
    matplotlib.pyplot.imshow : Matplotlib's imshow function.

    Examples
    --------
    For example,

    >>> from magni.imaging.visualisation import imshow
    >>> X = np.arange(4).reshape(2, 2)
    >>> add_k = lambda X, k: X + k
    >>> im_out = imshow(X, intensity_func=add_k, intensity_args=(2,))

    """

    _validate_imshow(X, ax, intensity_func, intensity_args, show_axis)

    _plotting.setup_matplotlib()

    # Handle ax keyword argument
    ca = plt.gca()
    if ax is not None:
        plt.sca(ax)

    # Intensity manipulation
    if intensity_func is not None:
        im_out = plt.imshow(intensity_func(X, *intensity_args), **kwargs)
    else:
        im_out = plt.imshow(X, **kwargs)

    # Display of axis
    axes = plt.gca()
    if show_axis == 'none':
        xlabels = axes.get_xticklabels()
        ylabels = axes.get_yticklabels()
        empty_xlabels = ['']*len(xlabels)
        empty_ylabels = ['']*len(ylabels)
        axes.set_xticklabels(empty_xlabels)
        axes.set_yticklabels(empty_ylabels)
        axes.tick_params(length=0)
    elif show_axis == 'top':
        axes.xaxis.tick_top()
        axes.xaxis.set_label_position('top')

    plt.sca(ca)

    return im_out
Example #9
0
def imshow(X,
           ax=None,
           intensity_func=None,
           intensity_args=(),
           show_axis='frame',
           **kwargs):
    """
    Display an image.

    Wrap `matplotlib.pyplot.imshow` to display a possibly intensity manipulated
    version of the image `X`.

    Parameters
    ----------
    X : ndarray
        The image to be displayed.
    ax : matplotlib.axes.Axes, optional
        The axes on which the image is displayed (the default is None, which
        implies that the current axes is used).
    intensity_func : FunctionType, optional
        The handle to the function used to manipulate the image intensity
        before the image is displayed (the default is None, which implies that
        no intensity manipulation is used).
    intensity_args : list or tuple, optional
        The arguments that are passed to the `intensity_func` (the default is
        (), which implies that no arguments are passed).
    show_axis : {'none', 'top', 'inherit', 'frame'}
        How the x- and y-axis are display. If 'none', no axis are displayed. If
        'top', the x-axis is displayed at the top of the image. If 'inherit',
        the axis display is inherited from `matplotlib.pyplot.imshow`. If
        'frame' only the frame is shown and not the ticks.

    Returns
    -------
    im_out : matplotlib.image.AxesImage
        The AxesImage returned by matplotlibs imshow.

    See Also
    --------
    matplotlib.pyplot.imshow : Matplotlib's imshow function.

    Examples
    --------
    For example,

    >>> import numpy as np
    >>> from magni.imaging.visualisation import imshow
    >>> X = np.arange(4).reshape(2, 2)
    >>> add_k = lambda X, k: X + k
    >>> im_out = imshow(X, intensity_func=add_k, intensity_args=(2,))

    """
    @_decorate_validation
    def validate_input():
        _numeric('X', ('boolean', 'integer', 'floating'), shape=(-1, -1))
        _generic('ax', mpl.axes.Axes, ignore_none=True)
        _generic('intensity_func', 'function', ignore_none=True)
        _generic('intensity_args', 'explicit collection')
        _generic('show_axis',
                 'string',
                 value_in=('none', 'top', 'inherit', 'frame'))

    validate_input()

    _plotting.setup_matplotlib()

    # Handle ax keyword argument
    ca = plt.gca()
    if ax is not None:
        plt.sca(ax)

    # Intensity manipulation
    if intensity_func is not None:
        im_out = plt.imshow(intensity_func(X, *intensity_args), **kwargs)
    else:
        im_out = plt.imshow(X, **kwargs)

    # Display of axis
    axes = plt.gca()
    if show_axis == 'none':
        axes.axis('off')
    elif show_axis == 'top':
        axes.xaxis.tick_top()
        axes.xaxis.set_label_position('top')
    elif show_axis == 'frame':
        xlabels = axes.get_xticklabels()
        ylabels = axes.get_yticklabels()
        empty_xlabels = [''] * len(xlabels)
        empty_ylabels = [''] * len(ylabels)
        axes.set_xticklabels(empty_xlabels)
        axes.set_yticklabels(empty_ylabels)
        axes.tick_params(length=0)

    plt.sca(ca)

    return im_out
Example #10
0
def plot_pixel_mask(h, w, pixels, output_path=None):
    """
    Display a binary image that shows the given pixel mask.

    A black image with `w` x `h` pixels is created and the `pixels` are marked
    with white.

    Parameters
    ----------
    h : int
        The height of the image in pixels.
    w : int
        The width of the image in pixels.
    pixels : ndarray
        The 2D array of pixels that make up the mask. Each row is a coordinate
        pair (x, y), such that `coords` has size len(`pixels`) x 2.
    output_path : str, optional
        Path (including file type extension) under which the plot is saved (the
        default value is None which implies, that the plot is not saved).

    Notes
    -----
    The resulting image is displayed in a figure using
    `magni.imaging.visualisation.imshow`.

    Examples
    --------
    For example,

    >>> import numpy as np
    >>> from magni.imaging.measurements import plot_pixel_mask
    >>> h = 3
    >>> w = 3
    >>> pixels = np.array([[0, 0], [1, 1], [2, 1]])
    >>> plot_pixel_mask(h, w, pixels)

    """

    @_decorate_validation
    def validate_input():
        _numeric('h', 'integer', range_='[2;inf)')
        _numeric('w', 'integer', range_='[2;inf)')
        _numeric('pixels', 'integer', shape=(-1, 2))
        _numeric('pixels[:, 0]', 'integer', range_='[0;{}]'.format(w - 1),
                 shape=(-1,), var=pixels[:, 0])
        _numeric('pixels[:, 1]', 'integer', range_='[0;{}]'.format(h - 1),
                 shape=(-1,), var=pixels[:, 1])
        _generic('output_path', 'string', ignore_none=True)

    validate_input()

    mask = np.zeros((h, w))
    mask[pixels[:, 1], pixels[:, 0]] = 1

    figsize = plt.rcParams['figure.figsize']

    if w / h > figsize[0] / figsize[1]:
        figsize_local = [figsize[0], figsize[0] * h / w]
    else:
        figsize_local = [figsize[1] * w / h, figsize[1]]

    _plotting.setup_matplotlib({'figure': {'figsize': figsize_local}})

    fig, axes = plt.subplots(1, 1)
    _imshow(mask, ax=axes, show_axis='top')

    axes.set_xlabel('width [pixels]')
    axes.set_ylabel('height [pixels]')

    plt.tight_layout()

    if output_path is not None:
        plt.savefig(output_path)

    _plotting.setup_matplotlib({'figure': {'figsize': figsize}})