<commit_before>from abc import ABCMeta
from io import BytesIO
from PIL import Image

import numpy
from six import PY3, add_metaclass

from ._image import window_batch_bchw
from . import SourcewiseTransformer
from .. import config


@add_metaclass(ABCMeta)
class ImagesFromBytes(SourcewiseTransformer):
    """Load from a stream of image-formatted data represented as bytes.

    Parameters
    ----------
    data_stream : instance of :class:`AbstractDataStream`
        The wrapped data stream.
    which_sources : tuple of str
        Which sources to apply the mapping to.
    color_mode : str, optional
        Mode to pass to PIL for color space conversion. Default is RGB.
        If `None`, no coercion is performed.

    Notes
    -----
    Images are returned as NumPy arrays converted from PIL objects.
    If `color_mode` is RGB, then the array is transposed from the
    `(height, width, channel)` dimension layout native to PIL to the
    `(channel, height, width)` layout that is pervasive in the world of
    convolutional networks.

    This SourcewiseTransformer supports streams returning single examples as
    `bytes` objects (`str` on Python 2.x) as well as streams that
    return iterables containing such objects. In the case of an
    iterable, a list of loaded images is returned.
<commit_msg>Fix missing import.<commit_after>from abc import ABCMeta
from io import BytesIO
from PIL import Image

import numpy
from six import PY3, add_metaclass

from ._image import window_batch_bchw
from . import ExpectsAxisLabels, SourcewiseTransformer
from .. import config


@add_metaclass(ABCMeta)
class ImagesFromBytes(SourcewiseTransformer):
    """Load from a stream of image-formatted data represented as bytes.

    Parameters
    ----------
    data_stream : instance of :class:`AbstractDataStream`
        The wrapped data stream.
    which_sources : tuple of str
        Which sources to apply the mapping to.
    color_mode : str, optional
        Mode to pass to PIL for color space conversion. Default is RGB.
        If `None`, no coercion is performed.

    Notes
    -----
    Images are returned as NumPy arrays converted from PIL objects.
    If `color_mode` is RGB, then the array is transposed from the
    `(height, width, channel)` dimension layout native to PIL to the
    `(channel, height, width)` layout that is pervasive in the world of
    convolutional networks.

    This SourcewiseTransformer supports streams returning single examples as
    `bytes` objects (`str` on Python 2.x) as well as streams that
    return iterables containing such objects. In the case of an
    iterable, a list of loaded images is returned.

{'fraction_non_alphanumeric': 0.05858054825384904, 'fraction_numerical': 0.0015020653398422831, 'mean_word_length': 3.2624, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '5869744', 'n_tokens_mistral': 714, 'n_tokens_neox': 691, 'n_words': 333}