<commit_before>from __future__ import print_function, division, absolute_import

import numpy as np
import six.moves as sm

from .. import imgaug as ia
from .. import dtypes as iadt
from ..augmenters import blend as blendlib


@ia.deprecated(alt_func="SegmentationMapsOnImage",
               comment="(Note the plural 'Maps' instead of old 'Map'.)")
def SegmentationMapOnImage(*args, **kwargs):
    return SegmentationMapsOnImage(*args, **kwargs)


class SegmentationMapsOnImage(object):
    """
    Object representing a segmentation map associated with an image.

    Attributes
    ----------
    DEFAULT_SEGMENT_COLORS : list of tuple of int
        Standard RGB colors to use during drawing, ordered by class index.

    Parameters
    ----------
    arr : (H,W) ndarray or (H,W,C) ndarray
        Array representing the segmentation map(s). May have dtypes bool,
        int or uint.

    shape : tuple of int
        Shape of the corresponding image (NOT of the segmentation map array).
        This is expected to be ``(H, W)`` or ``(H, W, C)`` with ``C`` usually
        being 3. If there is no corresponding image, then use the segmentation
        map's shape instead.

    nb_classes : None or int, optional
        Deprecated.

    """

    # TODO replace this by matplotlib colormap
    DEFAULT_SEGMENT_COLORS = [
        (0, 0, 0),  # black
        (230, 25, 75),  # red
        (60, 180, 75),  # green
        (255, 225, 25),  # yellow
        (0, 130, 200),  # blue
        (245, 130, 48),  # orange
        (145, 30, 180),  # purple
        (70, 240, 240),  # cyan
        (240, 50, 230),  # magenta
        (210, 245, 60),  # lime
        (250, 190, 190),  # pink
        (0, 128, 128),  # teal
        (230, 190, 255),  # lavender
        (170, 110, 40),  # brown
        (255, 250, 200),  # beige
        (128, 0, 0),  # maroon
        (170, 255, 195),  # mint
        (128, 128, 0),  # olive
        (255, 215, 180),  # coral
        (0, 0, 128),  # navy
        (128, 128, 128),  # grey
        (255, 255, 255),  # white
        # --
        (115, 12, 37),  # dark red
        (30, 90, 37),  # dark green
        (127, 112, 12),  # dark yellow
        (0, 65, 100),  # dark blue
        (122, 65, 24),  # dark orange
        (72, 15, 90),  # dark purple
        (35, 120, 120),  # dark cyan
        (120, 25, 115),  # dark magenta
        (105, 122, 30),  # dark lime
        (125, 95, 95),  # dark pink
        (0, 64, 64),  # dark teal
        (115, 95, 127),  # dark lavender
        (85, 55, 20),  # dark brown
        (127, 125, 100),  # dark beige
        (64, 0, 0),  # dark maroon
        (85, 127, 97),  # dark mint
        (64, 64, 0),  # dark olive
        (127, 107, 90),  # dark coral
        (0, 0, 64),  # dark navy
        (64, 64, 64),  # dark grey
    ]

    def __init__(self, arr, shape, nb_classes=None):
        ia.do_assert(ia.is_np_array(arr),
                     "Expected to get numpy array, got %s." % (type(arr),))
        assert isinstance(shape, tuple)

        if arr.dtype.kind == "f":
            ia.warn_deprecated(
                "Got a float array as the segmentation map in "
                "SegmentationMapsOnImage. That is deprecated. Please provide "
                "instead a (H,W,[C]) array of dtype bool_, int or uint, where "
                "C denotes the segmentation map index."
            )

            if arr.ndim == 2:
                arr = (arr > 0.5)
            else:
                assert arr.ndim == 3
                arr = np.argmax(arr, axis=2).astype(np.int32)

        if arr.dtype.name == "bool":
            ia.do_assert(arr.ndim in [2, 3])
            self._input_was = (arr.dtype, arr.ndim)
            if arr.ndim == 2:
                arr = arr[..., np.newaxis]
        elif arr.dtype.kind in ["i", "u"]:
            ia.do_assert(arr.ndim in [2, 3])
            ia.do_assert(np.min(arr.flat[0:100]) >= 0)
            if arr.dtype.kind == "u":
                # allow only <=uint16 due to conversion to int32
                assert arr.dtype.itemsize <= 2, (
                    "When using uint arrays as segmentation maps, only uint8 "
                    "and uint16 are allowed. Got dtype %s." % (arr.dtype.name,)
                )

            self._input_was = (arr.dtype, arr.ndim)
            if arr.ndim == 2:
                arr = arr[..., np.newaxis]
        else:
            raise Exception((
                "Input was expected to be an array of dtype 'bool', 'int' "
                "or 'uint'. Got dtype '%s'.") % (arr.dtype.name,))

        if arr.dtype.name != "int32":
            arr = arr.astype(np.int32)

        self.arr = arr

        # don't allow arrays here as an alternative to tuples as input
        # as allowing arrays introduces risk to mix up 'arr' and 'shape' args
        self.shape = shape

        if nb_classes is not None:
            ia.warn_deprecated(
                "Providing nb_classes to SegmentationMapOnImage is no longer "
                "necessary and hence deprecated. The argument is ignored "
                "and can be safely removed.")

    def get_arr(self):
        """
        Return the segmentation map array similar to its input dtype and shape.

        Returns
        -------
        ndarray
            Segmentation map array. Same dtype and number of dimensions as was
            originally used when the instance was created.<commit_msg>Fix typo<commit_after>from __future__ import print_function, division, absolute_import

import numpy as np
import six.moves as sm

from .. import imgaug as ia
from .. import dtypes as iadt
from ..augmenters import blend as blendlib


@ia.deprecated(alt_func="SegmentationMapsOnImage",
               comment="(Note the plural 'Maps' instead of old 'Map'.)")
def SegmentationMapOnImage(*args, **kwargs):
    return SegmentationMapsOnImage(*args, **kwargs)


class SegmentationMapsOnImage(object):
    """
    Object representing a segmentation map associated with an image.

    Attributes
    ----------
    DEFAULT_SEGMENT_COLORS : list of tuple of int
        Standard RGB colors to use during drawing, ordered by class index.

    Parameters
    ----------
    arr : (H,W) ndarray or (H,W,C) ndarray
        Array representing the segmentation map(s). May have dtypes bool,
        int or uint.

    shape : tuple of int
        Shape of the corresponding image (NOT of the segmentation map array).
        This is expected to be ``(H, W)`` or ``(H, W, C)`` with ``C`` usually
        being 3. If there is no corresponding image, then use the segmentation
        map's shape instead.

    nb_classes : None or int, optional
        Deprecated.

    """

    # TODO replace this by matplotlib colormap
    DEFAULT_SEGMENT_COLORS = [
        (0, 0, 0),  # black
        (230, 25, 75),  # red
        (60, 180, 75),  # green
        (255, 225, 25),  # yellow
        (0, 130, 200),  # blue
        (245, 130, 48),  # orange
        (145, 30, 180),  # purple
        (70, 240, 240),  # cyan
        (240, 50, 230),  # magenta
        (210, 245, 60),  # lime
        (250, 190, 190),  # pink
        (0, 128, 128),  # teal
        (230, 190, 255),  # lavender
        (170, 110, 40),  # brown
        (255, 250, 200),  # beige
        (128, 0, 0),  # maroon
        (170, 255, 195),  # mint
        (128, 128, 0),  # olive
        (255, 215, 180),  # coral
        (0, 0, 128),  # navy
        (128, 128, 128),  # grey
        (255, 255, 255),  # white
        # --
        (115, 12, 37),  # dark red
        (30, 90, 37),  # dark green
        (127, 112, 12),  # dark yellow
        (0, 65, 100),  # dark blue
        (122, 65, 24),  # dark orange
        (72, 15, 90),  # dark purple
        (35, 120, 120),  # dark cyan
        (120, 25, 115),  # dark magenta
        (105, 122, 30),  # dark lime
        (125, 95, 95),  # dark pink
        (0, 64, 64),  # dark teal
        (115, 95, 127),  # dark lavender
        (85, 55, 20),  # dark brown
        (127, 125, 100),  # dark beige
        (64, 0, 0),  # dark maroon
        (85, 127, 97),  # dark mint
        (64, 64, 0),  # dark olive
        (127, 107, 90),  # dark coral
        (0, 0, 64),  # dark navy
        (64, 64, 64),  # dark grey
    ]

    def __init__(self, arr, shape, nb_classes=None):
        ia.do_assert(ia.is_np_array(arr),
                     "Expected to get numpy array, got %s." % (type(arr),))
        assert isinstance(shape, tuple)

        if arr.dtype.kind == "f":
            ia.warn_deprecated(
                "Got a float array as the segmentation map in "
                "SegmentationMapsOnImage. That is deprecated. Please provide "
                "instead a (H,W,[C]) array of dtype bool_, int or uint, where "
                "C denotes the segmentation map index."
            )

            if arr.ndim == 2:
                arr = (arr > 0.5)
            else:
                assert arr.ndim == 3
                arr = np.argmax(arr, axis=2).astype(np.int32)

        if arr.dtype.name == "bool":
            ia.do_assert(arr.ndim in [2, 3])
            self._input_was = (arr.dtype, arr.ndim)
            if arr.ndim == 2:
                arr = arr[..., np.newaxis]
        elif arr.dtype.kind in ["i", "u"]:
            ia.do_assert(arr.ndim in [2, 3])
            ia.do_assert(np.min(arr.flat[0:100]) >= 0)
            if arr.dtype.kind == "u":
                # allow only <=uint16 due to conversion to int32
                assert arr.dtype.itemsize <= 2, (
                    "When using uint arrays as segmentation maps, only uint8 "
                    "and uint16 are allowed. Got dtype %s." % (arr.dtype.name,)
                )

            self._input_was = (arr.dtype, arr.ndim)
            if arr.ndim == 2:
                arr = arr[..., np.newaxis]
        else:
            raise Exception((
                "Input was expected to be an array of dtype 'bool', 'int' "
                "or 'uint'. Got dtype '%s'.") % (arr.dtype.name,))

        if arr.dtype.name != "int32":
            arr = arr.astype(np.int32)

        self.arr = arr

        # don't allow arrays here as an alternative to tuples as input
        # as allowing arrays introduces risk to mix up 'arr' and 'shape' args
        self.shape = shape

        if nb_classes is not None:
            ia.warn_deprecated(
                "Providing nb_classes to SegmentationMapsOnImage is no longer "
                "necessary and hence deprecated. The argument is ignored "
                "and can be safely removed.")

    def get_arr(self):
        """
        Return the segmentation map array similar to its input dtype and shape.

        Returns
        -------
        ndarray
            Segmentation map array. Same dtype and number of dimensions as was
            originally used when the instance was created.
{'fraction_non_alphanumeric': 0.11993337034980567, 'fraction_numerical': 0.061262261706459376, 'mean_word_length': 1.675, 'pattern_counts': {'":': 8, '<': 7, '<?xml version=': 0, '>': 7, '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': '5806608', 'n_tokens_mistral': 3911, 'n_tokens_neox': 3244, 'n_words': 1166}