<commit_before>from __future__ import division, print_function, absolute_import
import numpy as np
import amitgroup as ag

# If skimage is available, the image returned will be wrapped
# in the Image class. This is nice since it will be automatically
# displayed in an IPython notebook.
try:
    from skimage.io import Image
except ImportError:
    def Image(x): x

class ImageGrid:
    """
    An image grid used for combining equally-sized intensity images into a
    single larger image.

    Parameters
    ----------
    rows, cols : int
        Rows and columns of the grid.
    shape : tuple, (height, width)
        Shape of each patch in the grid.
    border_color : array_like, length 3 or int/float
        Specify the color of the border between each entry as RGB values
        between 0 and 1. If a scalar is provided, it is interpreted as a
        grayscale intensity.
    border_width : int
        Pixel width of border.

    Examples
    --------

    >>> import amitgroup as ag
    >>> import numpy as np
    >>> import matplotlib.pylab as plt
    >>> from matplotlib.pylab import cm
    >>> rs = np.random.RandomState(0)

    Let's generate a set of 100 8x8 image patches. 

    >>> shape = (100, 8, 8)
    >>> images = np.arange(np.prod(shape)).reshape(shape)
    >>> images += rs.uniform(0, np.prod(shape), size=shape)

    The most convenient way of using `ImageGrid` is through
    `ImageGrid.fromarray`:

    >>> G = ag.plot.ImageGrid.fromarray(images, cmap=cm.hsv)
    >>> plt.imshow(G.scaled_image(scale=5))
    >>> plt.show()
    
    """
    def __init__(self, rows, cols, shape, border_color=None, border_width=1):
        if border_color is None:
            self._border_color = np.array([0.5, 0.5, 0.5])
        elif isinstance(border_color, (int, float)): 
            self._border_color = np.array([border_color]*3)
        else:
            self._border_color = np.array(border_color)
        self._rows = rows
        self._cols = cols
        self._shape = shape 
        self._border = border_width

        self._fullsize = (self._border + (shape[0] + self._border) * self._rows,
                          self._border + (shape[1] + self._border) * self._cols)

        self._data = np.ones(self._fullsize + (3,), dtype=np.float64)

    @classmethod
    def fromarray(cls, images, rows=None, cols=None, border_color=None,
            border_width=1, cmap=None, vmin=None, vmax=None,
            global_bounds=True):
        """
        Constructs an image grid from an array of images. The images will be
        layed out row by row starting from the top (as English is written).

        Parameters
        ----------
        images : ndarray, ndim=3
            The first dimension should index the image and the rest are spatial
            dimensions.
        rows, cols : int or None
            The number of rows and columns for the grid. If both are None, the
            minimal square grid that holds all images will be used. If one is
            specified, the other will adapt to hold all images. If both are
            specified, then it possible that the grid will be vacuous or that
            some images will be omitted.
        border_color, border_width :
            See `ImageGrid`.
        cmap, vmin, vmax :
            See `ImageGrid.set_image`.
        global_bounds : bool
            If this is set to True and either vmin or vmax is not specified, it
            will infer it globally for the data. If it is set to False, it does
            it per image, which would be the equivalent of calling `set_image`
            manually with `vmin` and `vmax` set as such.

        """
        assert images.ndim == 3
        N = images.shape[0]

        if rows is None and cols is None:
            rows = cols = int(np.ceil(np.sqrt(N)))
        elif rows is None:<commit_msg>DOC: A few adjustments in image_grid.py.<commit_after>from __future__ import division, print_function, absolute_import
import numpy as np
import amitgroup as ag

# If skimage is available, the image returned will be wrapped
# in the Image class. This is nice since it will be automatically
# displayed in an IPython notebook.
try:
    from skimage.io import Image
except ImportError:
    def Image(x): x

class ImageGrid:
    """
    An image grid used for combining equally-sized intensity images into a
    single larger image.

    Parameters
    ----------
    rows, cols : int
        Rows and columns of the grid.
    shape : tuple, (height, width)
        Shape of each patch in the grid.
    border_color : array_like, length 3 or int/float
        Specify the color of the border between each entry as RGB values
        between 0 and 1. If a scalar is provided, it is interpreted as a
        grayscale intensity.
    border_width : int
        Pixel width of border.

    Examples
    --------

    >>> import amitgroup as ag
    >>> import numpy as np
    >>> import matplotlib.pylab as plt
    >>> from matplotlib.pylab import cm
    >>> rs = np.random.RandomState(0)

    Let's generate a set of 100 8x8 image patches. 

    >>> shape = (100, 8, 8)
    >>> images = np.arange(np.prod(shape)).reshape(shape)
    >>> images += rs.uniform(0, np.prod(shape), size=shape)

    The most convenient way of using `ImageGrid` is through
    `ImageGrid.fromarray`:

    >>> grid = ag.plot.ImageGrid.fromarray(images, cmap=cm.hsv)
    >>> img = grid.scaled_image(scale=5)
    >>> plt.imshow(img)
    >>> plt.show()

    If you are working in an IPython notebook, you can display
    ``img`` simply by adding it to the end of a cell.
    """
    def __init__(self, rows, cols, shape, border_color=None, border_width=1):
        if border_color is None:
            self._border_color = np.array([0.5, 0.5, 0.5])
        elif isinstance(border_color, (int, float)): 
            self._border_color = np.array([border_color]*3)
        else:
            self._border_color = np.array(border_color)
        self._rows = rows
        self._cols = cols
        self._shape = shape 
        self._border = border_width

        self._fullsize = (self._border + (shape[0] + self._border) * self._rows,
                          self._border + (shape[1] + self._border) * self._cols)

        self._data = np.ones(self._fullsize + (3,), dtype=np.float64)

    @classmethod
    def fromarray(cls, images, rows=None, cols=None, border_color=None,
            border_width=1, cmap=None, vmin=None, vmax=None,
            global_bounds=True):
        """
        Constructs an image grid from an array of images. The images will be
        layed out row by row starting from the top (as English is written).

        Parameters
        ----------
        images : ndarray, ndim=3
            The first dimension should index the image and the rest are spatial
            dimensions.
        rows, cols : int or None
            The number of rows and columns for the grid. If both are None, the
            minimal square grid that holds all images will be used. If one is
            specified, the other will adapt to hold all images. If both are
            specified, then it possible that the grid will be vacuous or that
            some images will be omitted.
        border_color, border_width :
            See `ImageGrid`.
        cmap, vmin, vmax :
            See `ImageGrid.set_image`.
        global_bounds : bool
            If this is set to True and either `vmin` or `vmax` is not specified, it
            will infer it globally for the data. If it is set to False, it does
            it per image, which would be the equivalent of calling `set_image`
            manually with `vmin` and `vmax` set as such.

        """
        assert images.ndim == 3
        N = images.shape[0]

        if rows is None and cols is None:
            rows = cols = int(np.ceil(np.sqrt(N)))
        elif rows is None:
{'fraction_non_alphanumeric': 0.08895393229829474, 'fraction_numerical': 0.008399083736319674, 'mean_word_length': 2.180493727235937, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 72, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3599082', 'n_tokens_mistral': 2269, 'n_tokens_neox': 2199, 'n_words': 872}