# 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 WALSMatrixFactorization."""

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import itertools
import json
import numpy as np

from tensorflow.contrib.factorization.python.ops import factorization_ops_test_utils
from tensorflow.contrib.factorization.python.ops import wals as wals_lib
from tensorflow.contrib.learn.python.learn import run_config
from tensorflow.contrib.learn.python.learn.estimators import model_fn
from tensorflow.contrib.learn.python.learn.estimators import run_config as run_config_lib
from tensorflow.python.framework import constant_op
from tensorflow.python.framework import dtypes
from tensorflow.python.framework import sparse_tensor
from tensorflow.python.ops import array_ops
from tensorflow.python.ops import control_flow_ops
from tensorflow.python.ops import embedding_ops
from tensorflow.python.ops import math_ops
from tensorflow.python.ops import sparse_ops
from tensorflow.python.ops import state_ops
from tensorflow.python.ops import variables
from tensorflow.python.platform import test
from tensorflow.python.training import input as input_lib
from tensorflow.python.training import monitored_session


class WALSMatrixFactorizationTest(test.TestCase):
  INPUT_MATRIX = factorization_ops_test_utils.INPUT_MATRIX

  def np_array_to_sparse(self, np_array):
    """Transforms an np.array to a tf.SparseTensor."""
    return factorization_ops_test_utils.np_matrix_to_tf_sparse(np_array)

  def calculate_loss(self):
    """Calculates the loss of the current (trained) model."""
    current_rows = embedding_ops.embedding_lookup(
        self._model.get_row_factors(), math_ops.range(self._num_rows),
        partition_strategy='div')
    current_cols = embedding_ops.embedding_lookup(
        self._model.get_col_factors(), math_ops.range(self._num_cols),
        partition_strategy='div')
    row_wts = embedding_ops.embedding_lookup(
        self._row_weights, math_ops.range(self._num_rows),
        partition_strategy='div')
    col_wts = embedding_ops.embedding_lookup(
        self._col_weights, math_ops.range(self._num_cols),
        partition_strategy='div')
    sp_inputs = self.np_array_to_sparse(self.INPUT_MATRIX)
    return factorization_ops_test_utils.calculate_loss(
        sp_inputs, current_rows, current_cols, self._regularization_coeff,
        self._unobserved_weight, row_wts, col_wts)

  # TODO(walidk): Replace with input_reader_utils functions once open sourced.
  def remap_sparse_tensor_rows(self, sp_x, row_ids, shape):
    """Remaps the row ids of a tf.SparseTensor."""
    old_row_ids, old_col_ids = array_ops.split(
        value=sp_x.indices, num_or_size_splits=2, axis=1)
    new_row_ids = array_ops.gather(row_ids, old_row_ids)
    new_indices = array_ops.concat([new_row_ids, old_col_ids], 1)
    return sparse_tensor.SparseTensor(
        indices=new_indices, values=sp_x.values, dense_shape=shape)

  # TODO(walidk): Add an option to shuffle inputs.
  def input_fn(self, np_matrix, batch_size, mode,
               project_row=None, projection_weights=None,
               remove_empty_rows_columns=False):
    """Returns an input_fn that selects row and col batches from np_matrix.

    This simple utility creates an input function from a numpy_array. The
    following transformations are performed:
    * The empty rows and columns in np_matrix are removed (if
      remove_empty_rows_columns is true)
    * np_matrix is converted to a SparseTensor.
    * The rows of the sparse matrix (and the rows of its transpose) are batched.
    * A features dictionary is created, which contains the row / column batches.

    In TRAIN mode, one only needs to specify the np_matrix and the batch_size.
    In INFER and EVAL modes, one must also provide project_row, a boolean which
    specifies whether we are projecting rows or columns.

    Args:
      np_matrix: A numpy array. The input matrix to use.
      batch_size: Integer.
      mode: Can be one of model_fn.ModeKeys.{TRAIN, INFER, EVAL}.
      project_row: A boolean. Used in INFER and EVAL modes. Specifies whether
        to project rows or columns.
      projection_weights: A float numpy array. Used in INFER mode. Specifies
        the weights to use in the projection (the weights are optional, and
        default to 1.).
      remove_empty_rows_columns: A boolean. When true, this will remove empty
        rows and columns in the np_matrix. Note that this will result in
        modifying the indices of the input matrix. The mapping from new indices
        to old indices is returned in the form of two numpy arrays.

    Returns:
      A tuple consisting of:
      _fn: A callable. Calling _fn returns a features dict.
      nz_row_ids: A numpy array of the ids of non-empty rows, such that
        nz_row_ids[i] is the old row index corresponding to new index i.
      nz_col_ids: A numpy array of the ids of non-empty columns, such that
        nz_col_ids[j] is the old column index corresponding to new index j.
    """
    if remove_empty_rows_columns:
      np_matrix, nz_row_ids, nz_col_ids = (
          factorization_ops_test_utils.remove_empty_rows_columns(np_matrix))
    else:
      nz_row_ids = np.arange(np.shape(np_matrix)[0])
      nz_col_ids = np.arange(np.shape(np_matrix)[1])

    def extract_features(row_batch, col_batch, num_rows, num_cols):
      row_ids = row_batch[0]
      col_ids = col_batch[0]
      rows = self.remap_sparse_tensor_rows(
          row_batch[1], row_ids, shape=[num_rows, num_cols])
      cols = self.remap_sparse_tensor_rows(
          col_batch[1], col_ids, shape=[num_cols, num_rows])
      features = {
          wals_lib.WALSMatrixFactorization.INPUT_ROWS: rows,
          wals_lib.WALSMatrixFactorization.INPUT_COLS: cols,
      }
      return features

    def _fn():
      num_rows = np.shape(np_matrix)[0]
      num_cols = np.shape(np_matrix)[1]
      row_ids = math_ops.range(num_rows, dtype=dtypes.int64)
      col_ids = math_ops.range(num_cols, dtype=dtypes.int64)
      sp_mat = self.np_array_to_sparse(np_matrix)
      sp_mat_t = sparse_ops.sparse_transpose(sp_mat)
      row_batch = input_lib.batch(
          [row_ids, sp_mat],
          batch_size=min(batch_size, num_rows),
          capacity=10,
          enqueue_many=True)
      col_batch = input_lib.batch(
          [col_ids, sp_mat_t],
          batch_size=min(batch_size, num_cols),
          capacity=10,
          enqueue_many=True)

      features = extract_features(row_batch, col_batch, num_rows, num_cols)

      if mode == model_fn.ModeKeys.INFER or mode == model_fn.ModeKeys.EVAL:
        self.assertTrue(
            project_row is not None,
            msg='project_row must be specified in INFER or EVAL mode.')
        features[wals_lib.WALSMatrixFactorization.PROJECT_ROW] = (
            constant_op.constant(project_row))

      if mode == model_fn.ModeKeys.INFER and projection_weights is not None:
        weights_batch = input_lib.batch(
            projection_weights,
            batch_size=batch_size,
            capacity=10,
            enqueue_many=True)
        features[wals_lib.WALSMatrixFactorization.PROJECTION_WEIGHTS] = (
            weights_batch)

      labels = None
      return features, labels

    return _fn, nz_row_ids, nz_col_ids