<commit_before>import numpy as np
import tensorflow as tf
import tensortools as tt


def random_distortion(image, contrast_lower=0.2, contrast_upper=1.8, brightness_max_delta=0.2, seed=None):
    """Distorts a single image for data augmentation, by applying random horizontal flipping,
       contrast and brightness.
    Parameters
    ----------
    image: Tensor with shape [height, width, channels]
        The image to apply distortion.
    contrast_lower: float, optional
        The lower contrast level, relative to the normal contrast of 1.0.
    contrast_upper: float, optional
        The upper contrast level, relative to the normal contrast of 1.0.
    brightness_max_delta: float, optional
        Defines the max brighness delta.
    seed: int or None, optional
        Used to create a random seed. See set_random_seed for behavior.
    Returns
    ----------
    image: Tensor with same shape as input.
        The randomly distorted image.
    """
    with tf.name_scope('random_distort'):
        image = tf.image.random_flip_left_right(image,
                                                seed=seed)
        # Because these operations are not commutative, consider randomizing the order their operation:
        image = tf.image.random_contrast(image,
                                         lower=contrast_lower,
                                         upper=contrast_upper,
                                         seed=seed + 1 if seed else None)
        image = tf.image.random_brightness(image,
                                           max_delta=brightness_max_delta,
                                           seed=seed + 2 if seed else None)
    return image


def equal_random_distortion(images, contrast_lower=0.2, contrast_upper=1.8, brightness_max_delta=0.2, seed=None):
    """Distorts a list of images equally for data augmentation, by applying random horizontal flipping,
       contrast and brightness.
    Parameters
    ----------
    images: Tensor list with shape list([height, width, channels])
        The images to apply an equal distortion to.
    contrast_lower: float, optional
        The lower contrast level, relative to the normal contrast of 1.0.
    contrast_upper: float, optional
        The upper contrast level, relative to the normal contrast of 1.0.
    brightness_max_delta: float, optional
        Defines the max brighness delta.
    seed: int or None, optional
        Used to create a random seed. See set_random_seed for behavior.
    Returns
    ----------
    image: Tensor with same shape as input.
        The randomly distorted image.
    Raises
    ----------
    ValueError: If `contrast_upper <= contrast_lower`, if `contrast_lower < 0`
                or if `brightness_max_delta` is negative.
    """
    if contrast_upper <= contrast_lower:
        raise ValueError('contrast_upper must be > lower.')

    if contrast_lower < 0:
        raise ValueError('contrast_lower must be non-negative.')
        
    if brightness_max_delta < 0:
        raise ValueError('brightness_max_delta must be non-negative.')
    
    with tf.name_scope('eq_random_distort'):
        uniform_random = tf.random_uniform([], 0, 1.0, dtype=tf.float32, seed=seed)
        mirror = tf.less(tf.pack([1.0, uniform_random, 1.0]), 0.5)

        contrast_factor = tf.random_uniform([], contrast_lower, contrast_upper,
                                            seed=seed + 1 if seed else None)
        delta = tf.random_uniform([], -brightness_max_delta, brightness_max_delta,
                                  seed=seed + 2 if seed else None)

        for i in xrange(len(images)):
            images[i] = tf.reverse(images[i], mirror)
            images[i] = tf.image.adjust_contrast(images[i], contrast_factor)
            images[i] = tf.image.adjust_brightness(images[i], delta)
    return images


def _fspecial_gauss(size, sigma):
    """Function to mimic the 'fspecial' gaussian MATLAB function
    Parameters
    ----------
    size: int
        The size (widht, height) of the filter kernel
    sigma: float
        The sigma of the Guassian distribution.
    Returns
    ----------
    A gaussian filter kernel.
    """
    with tf.name_scope('fspecial_gauss'):
        x_data, y_data = np.mgrid[-size//2 + 1:size//2 + 1,
                                  -size//2 + 1:size//2 + 1]

        # expand each dims twice:<commit_msg>Fixed image value scale after distortion.<commit_after>import numpy as np
import tensorflow as tf
import tensortools as tt


def random_distortion(image, contrast_lower=0.8, contrast_upper=1.2, brightness_max_delta=0.2, seed=None):
    """Distorts a single image for data augmentation, by applying random horizontal flipping,
       contrast and brightness.
    Parameters
    ----------
    image: Tensor with shape [height, width, channels]
        The image to apply distortion.
    contrast_lower: float, optional
        The lower contrast level, relative to the normal contrast of 1.0.
    contrast_upper: float, optional
        The upper contrast level, relative to the normal contrast of 1.0.
    brightness_max_delta: float, optional
        Defines the max brighness delta.
    seed: int or None, optional
        Used to create a random seed. See set_random_seed for behavior.
    Returns
    ----------
    image: Tensor with same shape as input.
        The randomly distorted image.
    """
    with tf.name_scope('random_distort'):
        image = tf.image.random_flip_left_right(image,
                                                seed=seed)
        # Because these operations are not commutative, consider randomizing the order their operation:
        image = tf.image.random_contrast(image,
                                         lower=contrast_lower,
                                         upper=contrast_upper,
                                         seed=seed + 1 if seed else None)
        image = tf.image.random_brightness(image,
                                           max_delta=brightness_max_delta,
                                           seed=seed + 2 if seed else None)
        
        # limit to scale [0, 1]
        image = tf.minimum(image, 1.0)
        image = tf.maximum(image, 0.0)
        return image


def equal_random_distortion(images, contrast_lower=0.8, contrast_upper=1.2, brightness_max_delta=0.2, seed=None):
    """Distorts a list of images equally for data augmentation, by applying random horizontal flipping,
       contrast and brightness.
    Parameters
    ----------
    images: Tensor list with shape list([height, width, channels])
        The images to apply an equal distortion to in value scale [0, 1].
    contrast_lower: float, optional
        The lower contrast level, relative to the normal contrast of 1.0.
    contrast_upper: float, optional
        The upper contrast level, relative to the normal contrast of 1.0.
    brightness_max_delta: float, optional
        Defines the max brighness delta.
    seed: int or None, optional
        Used to create a random seed. See set_random_seed for behavior.
    Returns
    ----------
    image: Tensor with same shape as input.
        The randomly distorted image.
    Raises
    ----------
    ValueError: If `contrast_upper <= contrast_lower`, if `contrast_lower < 0`
                or if `brightness_max_delta` is negative.
    """
    if contrast_upper <= contrast_lower:
        raise ValueError('contrast_upper must be > lower.')

    if contrast_lower < 0:
        raise ValueError('contrast_lower must be non-negative.')
        
    if brightness_max_delta < 0:
        raise ValueError('brightness_max_delta must be non-negative.')
    
    with tf.name_scope('eq_random_distort'):
        uniform_random = tf.random_uniform([], 0, 1.0, dtype=tf.float32, seed=seed)
        mirror = tf.less(tf.pack([1.0, uniform_random, 1.0]), 0.5)

        contrast_factor = tf.random_uniform([], contrast_lower, contrast_upper,
                                            seed=seed + 1 if seed else None)
        delta = tf.random_uniform([], -brightness_max_delta, brightness_max_delta,
                                  seed=seed + 2 if seed else None)

        for i in xrange(len(images)):
            adjusted_image = tf.reverse(images[i], mirror)
            adjusted_image = tf.image.adjust_contrast(adjusted_image, contrast_factor)
            adjusted_image = tf.image.adjust_brightness(adjusted_image, delta)
            
            # limit to scale [0, 1]
            adjusted_image = tf.minimum(adjusted_image, 1.0)
            adjusted_image = tf.maximum(adjusted_image, 0.0)
            
            images[i] = adjusted_image
        return images


def _fspecial_gauss(size, sigma):
    """Function to mimic the 'fspecial' gaussian MATLAB function
    Parameters
    ----------
    size: int
        The size (widht, height) of the filter kernel
    sigma: float
        The sigma of the Guassian distribution.
    Returns
    ----------
    A gaussian filter kernel.
    """
    with tf.name_scope('fspecial_gauss'):
        x_data, y_data = np.mgrid[-size//2 + 1:size//2 + 1,
                                  -size//2 + 1:size//2 + 1]

        # expand each dims twice:
{'fraction_non_alphanumeric': 0.08766974470396524, 'fraction_numerical': 0.01151548071700163, 'mean_word_length': 2.060505319148936, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 32, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3531774', 'n_tokens_mistral': 2494, 'n_tokens_neox': 2357, 'n_words': 801}