# Copyright 2021, The TensorFlow Federated 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
#
#      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.

from absl.testing import parameterized
import tensorflow as tf

from tensorflow_federated.python.simulation.baselines import task_data
from tensorflow_federated.python.simulation.datasets import client_data


def create_client_data(num_clients):
  client_ids = [str(x) for x in range(num_clients)]

  def create_dataset_fn(client_id):
    num_examples = tf.strings.to_number(client_id, out_type=tf.int64) + 1
    return tf.data.Dataset.range(num_examples)

  return client_data.ClientData.from_clients_and_tf_fn(client_ids,
                                                       create_dataset_fn)


class GetElementSpecTest(tf.test.TestCase, parameterized.TestCase):

  @parameterized.named_parameters(
      ('federated_no_preprocess', 'Federated', False),
      ('federated_preprocess', 'Federated', True),
      ('centralized_no_preprocess', 'Centralized', False),
      ('centralized_preprocess', 'Centralized', True),
  )
  def test_get_element_spec(self, dataset_type, preprocess):
    data = create_client_data(10)
    if dataset_type == 'Centralized':
      data = data.create_tf_dataset_from_all_clients()

    def convert_int64_to_int32(dataset):
      return dataset.map(lambda x: tf.cast(x, tf.int32))

    if preprocess:
      preprocess_fn = convert_int64_to_int32
      expected_type = tf.TensorSpec(shape=(), dtype=tf.int32, name=None)
    else:
      preprocess_fn = None
      expected_type = tf.TensorSpec(shape=(), dtype=tf.int64, name=None)

    actual_type = task_data._get_element_spec(data, preprocess_fn)
    self.assertEqual(actual_type, expected_type)


class BaselineTaskDatasetsTest(tf.test.TestCase, parameterized.TestCase):

  def test_raises_when_train_and_test_types_are_different_no_preprocessing(
      self):
    train_data = create_client_data(10)
    test_data = tf.data.Dataset.range(10, output_type=tf.int32)
    with self.assertRaisesRegex(
        ValueError,
        'train and test element structures after preprocessing must be equal'):
      task_data.BaselineTaskDatasets(train_data=train_data, test_data=test_data)

  def test_raises_when_train_and_test_types_are_different_with_train_preprocessing(
      self):
    train_data = create_client_data(10)
    test_data = tf.data.Dataset.range(10)
    train_preprocess_fn = lambda x: x.map(lambda y: tf.cast(y, dtype=tf.int32))
    with self.assertRaisesRegex(
        ValueError,
        'train and test element structures after preprocessing must be equal'):
      task_data.BaselineTaskDatasets(
          train_data=train_data,
          train_preprocess_fn=train_preprocess_fn,
          test_data=test_data)

  def test_raises_when_train_and_test_types_are_different_with_eval_preprocessing(
      self):
    train_data = create_client_data(10)
    test_data = tf.data.Dataset.range(10)
    eval_preprocess_fn = lambda x: x.map(lambda y: tf.cast(y, dtype=tf.int32))
    with self.assertRaisesRegex(
        ValueError,
        'train and test element structures after preprocessing must be equal'):
      task_data.BaselineTaskDatasets(
          train_data=train_data,
          eval_preprocess_fn=eval_preprocess_fn,
          test_data=test_data)

  def test_raises_when_test_and_validation_types_are_different(self):
    train_data = create_client_data(10)
    test_data = tf.data.Dataset.range(10)
    validation_data = tf.data.Dataset.range(10, output_type=tf.int32)
    with self.assertRaisesRegex(
        ValueError,
        'validation set must be None, or have the same element type structure '
        'as the test data'):
      task_data.BaselineTaskDatasets(
          train_data=train_data,
          test_data=test_data,
          validation_data=validation_data)

  def test_constructs_without_eval_preprocess_fn(self):
    preprocess_fn = lambda x: x.map(lambda y: 2 * y)
    test_task_data = task_data.BaselineTaskDatasets(
        train_data=create_client_data(10),
        train_preprocess_fn=preprocess_fn,
        test_data=create_client_data(2))
    train_preprocess_fn = test_task_data.train_preprocess_fn
    example_dataset = train_preprocess_fn(tf.data.Dataset.range(20))
    for i, x in enumerate(example_dataset):
      self.assertEqual(2 * i, x.numpy())

  def test_constructs_with_eval_preprocess_fn(self):
    train_preprocess_fn = lambda x: x.map(lambda y: 2 * y)
    eval_preprocess_fn = lambda x: x.map(lambda y: 3 * y)
    test_task_data = task_data.BaselineTaskDatasets(
        train_data=create_client_data(10),
        train_preprocess_fn=train_preprocess_fn,
        test_data=create_client_data(2),
        eval_preprocess_fn=eval_preprocess_fn)
    example_dataset = test_task_data.eval_preprocess_fn(
        tf.data.Dataset.range(20))
    for i, x in enumerate(example_dataset):
      self.assertEqual(3 * i, x.numpy())

  def test_sample_train_clients_returns_train_datasets(self):
    train_data = create_client_data(10)
    test_task_data = task_data.BaselineTaskDatasets(
        train_data=train_data, test_data=create_client_data(2))
    all_client_datasets = [
        train_data.create_tf_dataset_for_client(x)
        for x in train_data.client_ids
    ]
    all_client_datasets_as_lists = [
        list(ds.as_numpy_iterator()) for ds in all_client_datasets
    ]
    sampled_client_datasets = test_task_data.sample_train_clients(num_clients=3)
    for ds in sampled_client_datasets:
      ds_as_list = list(ds.as_numpy_iterator())
      self.assertIn(ds_as_list, all_client_datasets_as_lists)

  def test_sample_train_clients_returns_preprocessed_train_datasets(self):
    preprocess_fn = lambda x: x.map(lambda y: 2 * y)
    train_data = create_client_data(10)
    test_task_data = task_data.BaselineTaskDatasets(
        train_data=train_data,
        train_preprocess_fn=preprocess_fn,
        test_data=create_client_data(2))
    preprocess_train_data = train_data.preprocess(preprocess_fn)
    all_client_datasets = [
        preprocess_train_data.create_tf_dataset_for_client(x)
        for x in preprocess_train_data.client_ids
    ]
    all_client_datasets_as_lists = [
        list(ds.as_numpy_iterator()) for ds in all_client_datasets
    ]
    sampled_client_datasets = test_task_data.sample_train_clients(num_clients=5)
    for ds in sampled_client_datasets:
      ds_as_list = list(ds.as_numpy_iterator())
      self.assertIn(ds_as_list, all_client_datasets_as_lists)

  def test_sample_train_clients_random_seed(self):