# Path: disentanglement_lib/data/ground_truth/dummy_data.py
# class IdentityObservationsData(ground_truth_data.GroundTruthData):
# class DummyData(ground_truth_data.GroundTruthData):
#   def num_factors(self):
#   def observation_shape(self):
#   def factors_num_values(self):
#   def sample_factors(self, num, random_state):
#   def sample_observations_from_factors(self, factors, random_state):
#   def factor_names(self):
#   def num_factors(self):
#   def factors_num_values(self):
#   def observation_shape(self):
#   def sample_factors(self, num, random_state):
#   def sample_observations_from_factors(self, factors, random_state):
# 
# Path: disentanglement_lib/evaluation/metrics/modularity_explicitness.py
# def compute_modularity_explicitness(ground_truth_data,
#                                     representation_function,
#                                     random_state,
#                                     artifact_dir=None,
#                                     num_train=gin.REQUIRED,
#                                     num_test=gin.REQUIRED,
#                                     batch_size=16):
# def explicitness_per_factor(mus_train, y_train, mus_test, y_test):
# def modularity(mutual_information):


from absl.testing import absltest
from disentanglement_lib.data.ground_truth import dummy_data
from disentanglement_lib.evaluation.metrics import modularity_explicitness
from six.moves import range
import numpy as np
import gin.tf
    importance_matrix = np.zeros(shape=[10, 10], dtype=np.float64)
    result = modularity_explicitness.modularity(importance_matrix)
    np.testing.assert_allclose(result, .0)

  def test_redundant_codes(self):
    importance_matrix = np.diag(5.*np.ones(5))
    importance_matrix = np.vstack([importance_matrix, importance_matrix])
    result = modularity_explicitness.modularity(importance_matrix)
    np.testing.assert_allclose(result, 1.)

  def test_missed_factors(self):
    importance_matrix = np.diag(5.*np.ones(5))
    result = modularity_explicitness.modularity(importance_matrix[:2, :])
    np.testing.assert_allclose(result, 1.0)

  def test_one_code_two_factors(self):
    importance_matrix = np.diag(5.*np.ones(5))
    importance_matrix = np.hstack([importance_matrix, importance_matrix])
    result = modularity_explicitness.modularity(importance_matrix)
    np.testing.assert_allclose(result, 1. - 1./9)


class ModularityExplicitnessTest(absltest.TestCase):

  def test_metric(self):
    gin.bind_parameter("discretizer.discretizer_fn", _identity_discretizer)
    gin.bind_parameter("discretizer.num_bins", 10)
    ground_truth_data = dummy_data.IdentityObservationsData()
    representation_function = lambda x: np.array(x, dtype=np.float64)
    random_state = np.random.RandomState(0)

# Path: disentanglement_lib/data/ground_truth/dummy_data.py
# class IdentityObservationsData(ground_truth_data.GroundTruthData):
# class DummyData(ground_truth_data.GroundTruthData):
#   def num_factors(self):
#   def observation_shape(self):
#   def factors_num_values(self):
#   def sample_factors(self, num, random_state):
#   def sample_observations_from_factors(self, factors, random_state):
#   def factor_names(self):
#   def num_factors(self):
#   def factors_num_values(self):
#   def observation_shape(self):
#   def sample_factors(self, num, random_state):
#   def sample_observations_from_factors(self, factors, random_state):
# 
# Path: disentanglement_lib/evaluation/metrics/modularity_explicitness.py
# def compute_modularity_explicitness(ground_truth_data,
#                                     representation_function,
#                                     random_state,
#                                     artifact_dir=None,
#                                     num_train=gin.REQUIRED,
#                                     num_test=gin.REQUIRED,
#                                     batch_size=16):
# def explicitness_per_factor(mus_train, y_train, mus_test, y_test):
# def modularity(mutual_information):


# Path: disentanglement_lib/evaluation/metrics/modularity_explicitness_test.py
from absl.testing import absltest
from disentanglement_lib.data.ground_truth import dummy_data
from disentanglement_lib.evaluation.metrics import modularity_explicitness
from six.moves import range
import numpy as np
import gin.tf

    importance_matrix = np.zeros(shape=[10, 10], dtype=np.float64)
    result = modularity_explicitness.modularity(importance_matrix)
    np.testing.assert_allclose(result, .0)

  def test_redundant_codes(self):
    importance_matrix = np.diag(5.*np.ones(5))
    importance_matrix = np.vstack([importance_matrix, importance_matrix])
    result = modularity_explicitness.modularity(importance_matrix)
    np.testing.assert_allclose(result, 1.)

  def test_missed_factors(self):
    importance_matrix = np.diag(5.*np.ones(5))
    result = modularity_explicitness.modularity(importance_matrix[:2, :])
    np.testing.assert_allclose(result, 1.0)

  def test_one_code_two_factors(self):
    importance_matrix = np.diag(5.*np.ones(5))
    importance_matrix = np.hstack([importance_matrix, importance_matrix])
    result = modularity_explicitness.modularity(importance_matrix)
    np.testing.assert_allclose(result, 1. - 1./9)


class ModularityExplicitnessTest(absltest.TestCase):

  def test_metric(self):
    gin.bind_parameter("discretizer.discretizer_fn", _identity_discretizer)
    gin.bind_parameter("discretizer.num_bins", 10)
    ground_truth_data = dummy_data.IdentityObservationsData()
    representation_function = lambda x: np.array(x, dtype=np.float64)
    random_state = np.random.RandomState(0)

    scores = modularity_explicitness.compute_modularity_explicitness(