# Copyright 2017 The TensorFlow Authors. All Rights Reserved.
#
# 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
#
#     http://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.
# ==============================================================================
"""Tests for `tf.data.Dataset.unbatch()`."""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

from absl.testing import parameterized
import numpy as np

from tensorflow.python.data.kernel_tests import test_base
from tensorflow.python.data.ops import dataset_ops
from tensorflow.python.framework import combinations
from tensorflow.python.framework import constant_op
from tensorflow.python.framework import dtypes
from tensorflow.python.framework import errors
from tensorflow.python.framework import sparse_tensor
from tensorflow.python.ops import array_ops
from tensorflow.python.ops import math_ops
from tensorflow.python.ops import string_ops
from tensorflow.python.ops.ragged import ragged_factory_ops
from tensorflow.python.platform import test
from tensorflow.python.util import compat


class UnbatchTest(test_base.DatasetTestBase, parameterized.TestCase):

  @combinations.generate(test_base.default_test_combinations())
  def testUnbatchWithUnknownRankInput(self):
    dataset = dataset_ops.Dataset.from_tensors([0, 1, 2, 3]).unbatch()
    self.assertDatasetProduces(dataset, range(4))

  @combinations.generate(test_base.default_test_combinations())
  def testUnbatchScalarDataset(self):
    data = tuple([math_ops.range(10) for _ in range(3)])
    data = dataset_ops.Dataset.from_tensor_slices(data)
    expected_types = (dtypes.int32,) * 3
    data = data.batch(2)
    self.assertEqual(expected_types, dataset_ops.get_legacy_output_types(data))
    data = data.unbatch()
    self.assertEqual(expected_types, dataset_ops.get_legacy_output_types(data))

    self.assertDatasetProduces(data, [(i,) * 3 for i in range(10)])

  @combinations.generate(test_base.default_test_combinations())
  def testUnbatchNestedDataset(self):
    data = dataset_ops.Dataset.from_tensors(
        [dataset_ops.Dataset.range(10) for _ in range(10)])
    data = data.unbatch().flat_map(lambda x: x)
    self.assertDatasetProduces(data, list(range(10)) * 10)

  @combinations.generate(test_base.default_test_combinations())
  def testUnbatchDatasetWithStrings(self):
    data = tuple([math_ops.range(10) for _ in range(3)])
    data = dataset_ops.Dataset.from_tensor_slices(data)
    data = data.map(lambda x, y, z: (x, string_ops.as_string(y), z))
    expected_types = (dtypes.int32, dtypes.string, dtypes.int32)
    data = data.batch(2)
    self.assertEqual(expected_types, dataset_ops.get_legacy_output_types(data))
    data = data.unbatch()
    self.assertEqual(expected_types, dataset_ops.get_legacy_output_types(data))

    self.assertDatasetProduces(
        data, [(i, compat.as_bytes(str(i)), i) for i in range(10)])

  @combinations.generate(test_base.default_test_combinations())
  def testUnbatchDatasetWithSparseTensor(self):
    st = sparse_tensor.SparseTensorValue(
        indices=[[i, i] for i in range(10)],
        values=list(range(10)),
        dense_shape=[10, 10])
    data = dataset_ops.Dataset.from_tensors(st)
    data = data.unbatch()
    data = data.batch(5)
    data = data.unbatch()
    expected_output = [
        sparse_tensor.SparseTensorValue([[i]], [i], [10]) for i in range(10)
    ]
    self.assertDatasetProduces(data, expected_output=expected_output)

  @combinations.generate(test_base.default_test_combinations())
  def testUnbatchDatasetWithDenseSparseAndRaggedTensor(self):
    st = sparse_tensor.SparseTensorValue(
        indices=[[i, i] for i in range(10)],
        values=list(range(10)),
        dense_shape=[10, 10])
    rt = ragged_factory_ops.constant_value([[[0]], [[1]], [[2]], [[3]], [[4]],
                                            [[5]], [[6]], [[7]], [[8]], [[9]]])
    data = dataset_ops.Dataset.from_tensors((list(range(10)), st, rt))
    data = data.unbatch()
    data = data.batch(5)
    data = data.unbatch()
    expected_output = [(i, sparse_tensor.SparseTensorValue([[i]], [i], [10]),
                        ragged_factory_ops.constant_value([[i]]))
                       for i in range(10)]
    self.assertDatasetProduces(
        data, expected_output=expected_output)

  @combinations.generate(test_base.default_test_combinations())
  def testUnbatchDatasetWithRaggedTensor(self):
    rt = ragged_factory_ops.constant_value([[[0]], [[1]], [[2]], [[3]], [[4]],
                                            [[5]], [[6]], [[7]], [[8]], [[9]]])
    data = dataset_ops.Dataset.from_tensors(rt)
    data = data.unbatch()
    data = data.batch(5)
    data = data.batch(2)
    data = data.unbatch()
    expected_output = [
        ragged_factory_ops.constant_value([[[0]], [[1]], [[2]], [[3]], [[4]]]),
        ragged_factory_ops.constant_value([[[5]], [[6]], [[7]], [[8]], [[9]]]),
    ]
    self.assertDatasetProduces(
        data, expected_output=expected_output)

  @combinations.generate(test_base.default_test_combinations())
  def testUnbatchSingleElementTupleDataset(self):
    data = tuple([(math_ops.range(10),) for _ in range(3)])
    data = dataset_ops.Dataset.from_tensor_slices(data)
    expected_types = ((dtypes.int32,),) * 3