# Copyright 2022 The KerasCV Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import numpy as np
import tensorflow as tf
from absl.testing import parameterized

from keras_cv import layers
from keras_cv.backend import ops
from keras_cv.tests.test_case import TestCase


class RandAugmentTest(TestCase):
    def test_zero_rate_pass_through(self):
        rand_augment = layers.RandAugment(
            value_range=(0, 255),
            rate=0.0,
        )
        xs = np.ones((2, 512, 512, 3))
        ys = rand_augment(xs)
        self.assertAllClose(ys, xs)

    @parameterized.named_parameters(
        ("0", 0),
        ("20", 0.2),
        ("55", 0.55),
        ("10", 1.0),
    )
    def test_runs_with_magnitude(self, magnitude):
        rand_augment = layers.RandAugment(
            value_range=(0, 255), rate=0.5, magnitude=magnitude
        )
        xs = np.ones((2, 512, 512, 3))
        ys = rand_augment(xs)
        self.assertEqual(ys.shape, (2, 512, 512, 3))

    @parameterized.named_parameters(
        ("0_255", 0, 255),
        ("neg_1_1", -1, 1),
        ("0_1", 0, 1),
    )
    def test_runs_with_value_range(self, low, high):
        rand_augment = layers.RandAugment(
            augmentations_per_image=3,
            magnitude=0.5,
            rate=1.0,
            value_range=(low, high),
        )
        xs = tf.random.uniform((2, 512, 512, 3), low, high, dtype=tf.float32)
        ys = ops.convert_to_numpy(rand_augment(xs))
        self.assertTrue(np.all(np.logical_and(ys >= low, ys <= high)))

    @parameterized.named_parameters(
        ("float32", "float32"),
        ("int32", "int32"),
        ("uint8", "uint8"),
    )
    def test_runs_with_dtype_input(self, dtype):
        rand_augment = layers.RandAugment(value_range=(0, 255))
        xs = np.ones((2, 512, 512, 3), dtype=dtype)
        ys = rand_augment(xs)
        self.assertEqual(ys.shape, (2, 512, 512, 3))

    @parameterized.named_parameters(
        ("0_255", 0, 255),
        ("neg1_1", -1, 1),
        ("0_1", 0, 1),
    )
    def test_standard_policy_respects_value_range(self, lower, upper):
        my_layers = layers.RandAugment.get_standard_policy(
            value_range=(lower, upper), magnitude=1.0, magnitude_stddev=0.2
        )
        rand_augment = layers.RandomAugmentationPipeline(
            layers=my_layers, augmentations_per_image=3
        )
        xs = tf.random.uniform((2, 512, 512, 3), lower, upper, dtype=tf.float32)
        ys = ops.convert_to_numpy(rand_augment(xs))
        self.assertLessEqual(np.max(ys), upper)
        self.assertGreaterEqual(np.min(ys), lower)

    def test_runs_unbatched(self):
        rand_augment = layers.RandAugment(
            augmentations_per_image=3,
            magnitude=0.5,
            rate=1.0,
            value_range=(0, 255),
        )
        xs = tf.random.uniform((512, 512, 3), 0, 255, dtype=tf.float32)
        ys = rand_augment(xs)
        self.assertEqual(xs.shape, ys.shape)

    def test_runs_no_geo(self):
        rand_augment = layers.RandAugment(
            augmentations_per_image=2,
            magnitude=0.5,
            rate=1.0,
            geometric=False,
            value_range=(0, 255),
        )
        self.assertFalse(
            any(
                [
                    isinstance(x, layers.RandomTranslation)
                    for x in rand_augment.layers
                ]
            )
        )
        self.assertFalse(
            any(
                [isinstance(x, layers.RandomShear) for x in rand_augment.layers]
            )
        )
