Beispiel #1
0
    def create_plot(cls, *, data, tiles_params: Dict, points_params: Dict,
                    labels_params: Dict, reverse_y: bool, show_legend: bool,
                    labels_map_size: bool, corr_value_format: str,
                    threshold) -> PlotSpec:

        # Adjust options
        labels_map_size = _OpUtil.adjust_type_color_size(
            tiles_params, points_params, labels_params, labels_map_size)
        _OpUtil.adjust_diag(tiles_params, points_params, labels_params,
                            threshold)
        _OpUtil.flip_type(tiles_params, points_params, labels_params,
                          reverse_y)

        tooltips = (layer_tooltips().format(
            field='@..corr..', format=corr_value_format).line('@..corr..'))

        plot = ggplot(data)

        if tiles_params is not None:
            plot += geom_tile(stat='corr',
                              show_legend=show_legend,
                              size=0.0,
                              width=1.002,
                              height=1.002,
                              tooltips=tooltips,
                              sampling='none',
                              **tiles_params)
            plot += coord_cartesian()
        else:
            plot += coord_fixed()

        if points_params is not None:
            plot += geom_point(stat='corr',
                               show_legend=show_legend,
                               size_unit='x',
                               mapping=aes(size='..corr_abs..'),
                               tooltips=tooltips,
                               sampling='none',
                               **points_params)

        if labels_params is not None:
            m = None
            if labels_map_size:
                m = aes(size='..corr_abs..')
            else:
                labels_params['size'] = 1

            plot += geom_text(stat='corr',
                              show_legend=show_legend,
                              mapping=m,
                              na_text='',
                              label_format=corr_value_format,
                              tooltips=tooltips,
                              size_unit='x',
                              sampling='none',
                              **labels_params)

        return plot
Beispiel #2
0
def image_matrix(image_data_array, *, norm: bool = None, scale=1) -> GGBunch:
    """
    Display images in a grid.
    The grid dimensions are determined by shape of the input 2D ndarray.

    Elements of the input 2D array are images specified by ndarrays with shape (n, m) or (n, m, 3) or (n, m, 4).

    Parameters
    ----------
    image_data_array : 2D numpy.ndarray containing images
        Specifies dimensions of output grid

    norm : bool
        False - disables default scaling of a luminance (grayscale) images to the (0, 255) range.

    scale : scalar, default: 1
        Specifies magnification factor

    Returns
    -------
        Plot bunch object (GGBunch)

    Examples
    --------
    >>> import numpy as np
    >>> from lets_plot.bistro import im
    >>> image = np.random.choice([0.0, 1.0], [64, 64, 3])
    >>> X = np.empty([4, 6], dtype=object)
    >>> X.fill(image)
    >>> im.image_matrix(X)
    """

    if not is_ndarray(image_data_array):
        raise Exception(
            "Invalid image_data_array: 2d ndarray is expacted but was {}".
            format(type(image_data_array)))

    if image_data_array.ndim != 2:
        raise Exception(
            "Invalid image_data_array: 2-dimentional ndarray is expacted but was {}-dimentional"
            .format(image_data_array.ndim))

    rows, cols = image_data_array.shape
    if cols * rows <= 0:
        return

    w_max = 0
    h_max = 0
    for row in range(rows):
        for col in range(cols):
            image_data = image_data_array[row][col]
            if image_data is None:
                continue

            _assert_image_data(image_data)
            h, w = image_data.shape[0:2]
            h, w = _expand_h_w(h, w, scale)
            w_max = max(w_max, w)
            h_max = max(h_max, h)

    # no gaps between image and plot edges
    options = scale_x_continuous(expand=[0, 0])
    options += scale_y_continuous(expand=[0, 0])

    # show no axis
    options += theme(axis_line='blank',
                     axis_title='blank',
                     axis_ticks='blank',
                     axis_text='blank')

    ggbunch = GGBunch()

    for row in range(rows):
        for col in range(cols):
            image_data = image_data_array[row][col]
            if image_data is None:
                continue

            h, w = image_data.shape[0:2]
            h, w = _expand_h_w(h, w, scale)
            p = ggplot() + geom_image(image_data=image_data, norm=norm)
            p += options
            ggbunch.add_plot(p, col * w_max, row * h_max, w, h)

    return ggbunch
Beispiel #3
0
def image_matrix(image_data_array, *, norm: bool = None, scale=1) -> GGBunch:
    """
    Display images in a grid.
    The grid dimensions are determined by shape of the input 2D ndarray.

    Elements of the input 2D array are images specified by ndarrays with shape
    (n, m) or (n, m, 3) or (n, m, 4).

    Parameters
    ----------
    image_data_array : `ndarray`
        2D `numpy.ndarray` containing images. Specifies dimensions of output grid.
    norm : bool, default=True
        False value disables default scaling of a luminance (grayscale) images to the (0, 255) range.
    scale : float, default=1.0
        Specifies magnification factor.

    Returns
    -------
    `GGBunch`
        Plot bunch object.

    Examples
    --------
    .. jupyter-execute::
        :linenos:
        :emphasize-lines: 9

        import numpy as np
        from lets_plot import *
        from lets_plot.bistro.im import *
        LetsPlot.setup_html()
        np.random.seed(42)
        image = np.random.randint(256, size=(64, 64, 3))
        matrix = np.empty((2, 3), dtype=object)
        matrix.fill(image)
        image_matrix(matrix)

    |

    .. jupyter-execute::
        :linenos:
        :emphasize-lines: 12

        import numpy as np
        from lets_plot import *
        from lets_plot.bistro.im import *
        LetsPlot.setup_html()
        rows, cols = 3, 3
        matrix = np.empty((rows, cols), dtype=object)
        for r in range(rows):
            for c in range(cols):
                w, h = 32 + 16 * c, 32 + 16 * r
                matrix[r][c] = 256 * np.linspace(np.linspace(0, .5, w), \\
                                                 np.linspace(.5, .5, w), h)
        image_matrix(matrix, norm=False, scale=1.5)

    """

    if not is_ndarray(image_data_array):
        raise Exception(
            "Invalid image_data_array: 2d ndarray is expacted but was {}".
            format(type(image_data_array)))

    if image_data_array.ndim != 2:
        raise Exception(
            "Invalid image_data_array: 2-dimentional ndarray is expacted but was {}-dimentional"
            .format(image_data_array.ndim))

    rows, cols = image_data_array.shape
    if cols * rows <= 0:
        return

    w_max = 0
    h_max = 0
    for row in range(rows):
        for col in range(cols):
            image_data = image_data_array[row][col]
            if image_data is None:
                continue

            _assert_image_data(image_data)
            h, w = image_data.shape[0:2]
            h, w = _expand_h_w(h, w, scale)
            w_max = max(w_max, w)
            h_max = max(h_max, h)

    # no gaps between image and plot edges
    options = scale_x_continuous(expand=[0, 0])
    options += scale_y_continuous(expand=[0, 0])

    # show no axis
    options += theme(axis_line='blank',
                     axis_title='blank',
                     axis_ticks='blank',
                     axis_text='blank')

    ggbunch = GGBunch()

    for row in range(rows):
        for col in range(cols):
            image_data = image_data_array[row][col]
            if image_data is None:
                continue

            h, w = image_data.shape[0:2]
            h, w = _expand_h_w(h, w, scale)
            p = ggplot() + geom_image(image_data=image_data, norm=norm)
            p += options
            ggbunch.add_plot(p, col * w_max, row * h_max, w, h)

    return ggbunch