# Path: disentanglement_lib/evaluation/metrics/unsupervised_metrics.py
# @gin.configurable(
#     "unsupervised_metrics",
#     blacklist=["ground_truth_data", "representation_function", "random_state",
#                "artifact_dir"])
# def unsupervised_metrics(ground_truth_data,
#                          representation_function,
#                          random_state,
#                          artifact_dir=None,
#                          num_train=gin.REQUIRED,
#                          batch_size=16):
#   """Computes unsupervised scores based on covariance and mutual information.
# 
#   Args:
#     ground_truth_data: GroundTruthData to be sampled from.
#     representation_function: Function that takes observations as input and
#       outputs a dim_representation sized representation for each observation.
#     random_state: Numpy random state used for randomness.
#     artifact_dir: Optional path to directory where artifacts can be saved.
#     num_train: Number of points used for training.
#     batch_size: Batch size for sampling.
# 
#   Returns:
#     Dictionary with scores.
#   """
#   del artifact_dir
#   scores = {}
#   logging.info("Generating training set.")
#   mus_train, _ = utils.generate_batch_factor_code(
#       ground_truth_data, representation_function, num_train, random_state,
#       batch_size)
#   num_codes = mus_train.shape[0]
#   cov_mus = np.cov(mus_train)
#   assert num_codes == cov_mus.shape[0]
# 
#   # Gaussian total correlation.
#   scores["gaussian_total_correlation"] = gaussian_total_correlation(cov_mus)
# 
#   # Gaussian Wasserstein correlation.
#   scores["gaussian_wasserstein_correlation"] = gaussian_wasserstein_correlation(
#       cov_mus)
#   scores["gaussian_wasserstein_correlation_norm"] = (
#       scores["gaussian_wasserstein_correlation"] / np.sum(np.diag(cov_mus)))
# 
#   # Compute average mutual information between different factors.
#   mus_discrete = utils.make_discretizer(mus_train)
#   mutual_info_matrix = utils.discrete_mutual_info(mus_discrete, mus_discrete)
#   np.fill_diagonal(mutual_info_matrix, 0)
#   mutual_info_score = np.sum(mutual_info_matrix) / (num_codes**2 - num_codes)
#   scores["mutual_info_score"] = mutual_info_score
#   return scores


from absl.testing import absltest
from disentanglement_lib.evaluation.metrics import unsupervised_metrics
import numpy as np
import scipy
# 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 unsupervised_metrics.py."""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function


class UnsupervisedMetricsTest(absltest.TestCase):

  def test_gaussian_total_correlation_zero(self):
    score = unsupervised_metrics.gaussian_total_correlation(
        np.diag(np.ones(5, dtype=np.float64)))
    self.assertBetween(score, -0.01, 0.01)

  def test_gaussian_total_correlation_same(self):
    """Check that the results of the both functions are the same."""
    cov = np.array([[1, 0.9], [0.9, 1.0]], dtype=np.float32)
    means = np.array([0.0, 0.0], dtype=np.float32)
    cov_central = np.diag(np.diag(cov))

# Path: disentanglement_lib/evaluation/metrics/unsupervised_metrics.py
# @gin.configurable(
#     "unsupervised_metrics",
#     blacklist=["ground_truth_data", "representation_function", "random_state",
#                "artifact_dir"])
# def unsupervised_metrics(ground_truth_data,
#                          representation_function,
#                          random_state,
#                          artifact_dir=None,
#                          num_train=gin.REQUIRED,
#                          batch_size=16):
#   """Computes unsupervised scores based on covariance and mutual information.
# 
#   Args:
#     ground_truth_data: GroundTruthData to be sampled from.
#     representation_function: Function that takes observations as input and
#       outputs a dim_representation sized representation for each observation.
#     random_state: Numpy random state used for randomness.
#     artifact_dir: Optional path to directory where artifacts can be saved.
#     num_train: Number of points used for training.
#     batch_size: Batch size for sampling.
# 
#   Returns:
#     Dictionary with scores.
#   """
#   del artifact_dir
#   scores = {}
#   logging.info("Generating training set.")
#   mus_train, _ = utils.generate_batch_factor_code(
#       ground_truth_data, representation_function, num_train, random_state,
#       batch_size)
#   num_codes = mus_train.shape[0]
#   cov_mus = np.cov(mus_train)
#   assert num_codes == cov_mus.shape[0]
# 
#   # Gaussian total correlation.
#   scores["gaussian_total_correlation"] = gaussian_total_correlation(cov_mus)
# 
#   # Gaussian Wasserstein correlation.
#   scores["gaussian_wasserstein_correlation"] = gaussian_wasserstein_correlation(
#       cov_mus)
#   scores["gaussian_wasserstein_correlation_norm"] = (
#       scores["gaussian_wasserstein_correlation"] / np.sum(np.diag(cov_mus)))
# 
#   # Compute average mutual information between different factors.
#   mus_discrete = utils.make_discretizer(mus_train)
#   mutual_info_matrix = utils.discrete_mutual_info(mus_discrete, mus_discrete)
#   np.fill_diagonal(mutual_info_matrix, 0)
#   mutual_info_score = np.sum(mutual_info_matrix) / (num_codes**2 - num_codes)
#   scores["mutual_info_score"] = mutual_info_score
#   return scores


# Path: disentanglement_lib/evaluation/metrics/unsupervised_metrics_test.py
from absl.testing import absltest
from disentanglement_lib.evaluation.metrics import unsupervised_metrics
import numpy as np
import scipy

# 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 unsupervised_metrics.py."""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function


class UnsupervisedMetricsTest(absltest.TestCase):

  def test_gaussian_total_correlation_zero(self):
    score = unsupervised_metrics.gaussian_total_correlation(
        np.diag(np.ones(5, dtype=np.float64)))
    self.assertBetween(score, -0.01, 0.01)

  def test_gaussian_total_correlation_same(self):
    """Check that the results of the both functions are the same."""
    cov = np.array([[1, 0.9], [0.9, 1.0]], dtype=np.float32)
    means = np.array([0.0, 0.0], dtype=np.float32)
    cov_central = np.diag(np.diag(cov))

    shouldbe = unsupervised_metrics.kl_gaussians_numerically_unstable(