# Path: disentanglement_lib/data/ground_truth/named_data.py
# def get_named_ground_truth_data(name):
# 
# Path: disentanglement_lib/utils/results.py
# def update_result_directory(result_directory,
#                             step_name,
#                             results_dict,
#                             old_result_directory=None):
# def _copy_recursively(path_to_old_dir, path_to_new_dir):
# def copydir(path_to_old_dir, path_to_new_dir):
# def save_gin(config_path):
#   def default(self, obj):
# def save_dict(config_path, dict_with_info):
# def gin_dict(config_path=None):
# def namespaced_dict(base_dict=None, **named_dicts):
# def aggregate_json_results(base_path):
# class Encoder(json.JSONEncoder):
# 
# Path: disentanglement_lib/visualize/visualize_util.py
# def save_image(image, image_path):
# def grid_save_images(images, image_path):
# def padded_grid(images, num_rows=None, padding_px=10, value=None):
# def padded_stack(images, padding_px=10, axis=0, value=None):
# def padding_array(image, padding_px, axis, value=None):
# def best_num_rows(num_elements, max_ratio=4):
# def pad_around(image, padding_px=10, axis=None, value=None):
# def add_below(image, padding_px=10, value=None):
# def save_animation(list_of_animated_images, image_path, fps):
# def cycle_factor(starting_index, num_indices, num_frames):
# def cycle_gaussian(starting_value, num_frames, loc=0., scale=1.):
# def cycle_interval(starting_value, num_frames, min_val, max_val):
# 
# Path: disentanglement_lib/visualize/visualize_irs.py
# def vis_all_interventional_effects(gen_factors, latents, output_dir):
#   """Compute Matrix of all interventional effects."""
#   res = scalable_disentanglement_score(gen_factors, latents)
#   parents = res["parents"]
#   scores = res["disentanglement_scores"]
# 
#   fig_width_inches = 3.0 * gen_factors.shape[1]
#   fig_height_inches = 3.0 * latents.shape[1]
#   fig, axes = plt.subplots(
#       latents.shape[1],
#       gen_factors.shape[1],
#       figsize=(fig_width_inches, fig_height_inches),
#       sharex="col",
#       sharey="row")
# 
#   for j in range(gen_factors.shape[1]):  # Iterate over generative factors.
#     for l in range(latents.shape[1]):
#       ax = axes[l, j]
#       if parents[l] != j:
#         _visualize_interventional_effect(
#             gen_factors, latents, l, parents[l], j, ax=ax, plot_legend=False)
#         ax.set_title("")
#       else:
#         _visualize_interventional_effect(
#             gen_factors, latents, l, parents[l], j, no_conditioning=True, ax=ax)
#         ax.set_title("Parent={}, IRS = {:1.2}".format(parents[l], scores[l]))
# 
#   fig.tight_layout()
#   if not tf.gfile.IsDirectory(output_dir):
#     tf.gfile.MakeDirs(output_dir)
#   output_path = os.path.join(output_dir, "interventional_effect.png")
#   with tf.gfile.Open(output_path, "wb") as path:
#     fig.savefig(path)


import numbers
import os
import numpy as np
import tensorflow.compat.v1 as tf
import tensorflow_hub as hub
import gin.tf
from disentanglement_lib.data.ground_truth import named_data
from disentanglement_lib.utils import results
from disentanglement_lib.visualize import visualize_util
from disentanglement_lib.visualize.visualize_irs import vis_all_interventional_effects
from scipy import stats
from six.moves import range
from tensorflow.compat.v1 import gfile
      images = []
      for j in range(base_code.shape[0]):
        code = np.repeat(np.expand_dims(base_code, 0), num_frames, axis=0)
        loc = np.mean(means[:, j])
        total_variance = np.mean(np.exp(logvars[:, j])) + np.var(means[:, j])
        code[:, j] = visualize_util.cycle_gaussian(
            base_code[j], num_frames, loc=loc, scale=np.sqrt(total_variance))
        images.append(np.array(activation(_decoder(code))))
      filename = os.path.join(results_dir, "fitted_gaussian_cycle%d.gif" % i)
      visualize_util.save_animation(np.array(images), filename, fps)

    # Cycle through [-2, 2] interval.
    for i, base_code in enumerate(means[:num_animations]):
      images = []
      for j in range(base_code.shape[0]):
        code = np.repeat(np.expand_dims(base_code, 0), num_frames, axis=0)
        code[:, j] = visualize_util.cycle_interval(base_code[j], num_frames,
                                                   -2., 2.)
        images.append(np.array(activation(_decoder(code))))
      filename = os.path.join(results_dir, "fixed_interval_cycle%d.gif" % i)
      visualize_util.save_animation(np.array(images), filename, fps)

    # Cycle linearly through +-2 std dev of a fitted Gaussian.
    for i, base_code in enumerate(means[:num_animations]):
      images = []
      for j in range(base_code.shape[0]):
        code = np.repeat(np.expand_dims(base_code, 0), num_frames, axis=0)
        loc = np.mean(means[:, j])
        total_variance = np.mean(np.exp(logvars[:, j])) + np.var(means[:, j])
        scale = np.sqrt(total_variance)

# Path: disentanglement_lib/data/ground_truth/named_data.py
# def get_named_ground_truth_data(name):
# 
# Path: disentanglement_lib/utils/results.py
# def update_result_directory(result_directory,
#                             step_name,
#                             results_dict,
#                             old_result_directory=None):
# def _copy_recursively(path_to_old_dir, path_to_new_dir):
# def copydir(path_to_old_dir, path_to_new_dir):
# def save_gin(config_path):
#   def default(self, obj):
# def save_dict(config_path, dict_with_info):
# def gin_dict(config_path=None):
# def namespaced_dict(base_dict=None, **named_dicts):
# def aggregate_json_results(base_path):
# class Encoder(json.JSONEncoder):
# 
# Path: disentanglement_lib/visualize/visualize_util.py
# def save_image(image, image_path):
# def grid_save_images(images, image_path):
# def padded_grid(images, num_rows=None, padding_px=10, value=None):
# def padded_stack(images, padding_px=10, axis=0, value=None):
# def padding_array(image, padding_px, axis, value=None):
# def best_num_rows(num_elements, max_ratio=4):
# def pad_around(image, padding_px=10, axis=None, value=None):
# def add_below(image, padding_px=10, value=None):
# def save_animation(list_of_animated_images, image_path, fps):
# def cycle_factor(starting_index, num_indices, num_frames):
# def cycle_gaussian(starting_value, num_frames, loc=0., scale=1.):
# def cycle_interval(starting_value, num_frames, min_val, max_val):
# 
# Path: disentanglement_lib/visualize/visualize_irs.py
# def vis_all_interventional_effects(gen_factors, latents, output_dir):
#   """Compute Matrix of all interventional effects."""
#   res = scalable_disentanglement_score(gen_factors, latents)
#   parents = res["parents"]
#   scores = res["disentanglement_scores"]
# 
#   fig_width_inches = 3.0 * gen_factors.shape[1]
#   fig_height_inches = 3.0 * latents.shape[1]
#   fig, axes = plt.subplots(
#       latents.shape[1],
#       gen_factors.shape[1],
#       figsize=(fig_width_inches, fig_height_inches),
#       sharex="col",
#       sharey="row")
# 
#   for j in range(gen_factors.shape[1]):  # Iterate over generative factors.
#     for l in range(latents.shape[1]):
#       ax = axes[l, j]
#       if parents[l] != j:
#         _visualize_interventional_effect(
#             gen_factors, latents, l, parents[l], j, ax=ax, plot_legend=False)
#         ax.set_title("")
#       else:
#         _visualize_interventional_effect(
#             gen_factors, latents, l, parents[l], j, no_conditioning=True, ax=ax)
#         ax.set_title("Parent={}, IRS = {:1.2}".format(parents[l], scores[l]))
# 
#   fig.tight_layout()
#   if not tf.gfile.IsDirectory(output_dir):
#     tf.gfile.MakeDirs(output_dir)
#   output_path = os.path.join(output_dir, "interventional_effect.png")
#   with tf.gfile.Open(output_path, "wb") as path:
#     fig.savefig(path)


# Path: disentanglement_lib/visualize/visualize_model.py
import numbers
import os
import numpy as np
import tensorflow.compat.v1 as tf
import tensorflow_hub as hub
import gin.tf
from disentanglement_lib.data.ground_truth import named_data
from disentanglement_lib.utils import results
from disentanglement_lib.visualize import visualize_util
from disentanglement_lib.visualize.visualize_irs import vis_all_interventional_effects
from scipy import stats
from six.moves import range
from tensorflow.compat.v1 import gfile

      images = []
      for j in range(base_code.shape[0]):
        code = np.repeat(np.expand_dims(base_code, 0), num_frames, axis=0)
        loc = np.mean(means[:, j])
        total_variance = np.mean(np.exp(logvars[:, j])) + np.var(means[:, j])
        code[:, j] = visualize_util.cycle_gaussian(
            base_code[j], num_frames, loc=loc, scale=np.sqrt(total_variance))
        images.append(np.array(activation(_decoder(code))))
      filename = os.path.join(results_dir, "fitted_gaussian_cycle%d.gif" % i)
      visualize_util.save_animation(np.array(images), filename, fps)

    # Cycle through [-2, 2] interval.
    for i, base_code in enumerate(means[:num_animations]):
      images = []
      for j in range(base_code.shape[0]):
        code = np.repeat(np.expand_dims(base_code, 0), num_frames, axis=0)
        code[:, j] = visualize_util.cycle_interval(base_code[j], num_frames,
                                                   -2., 2.)
        images.append(np.array(activation(_decoder(code))))
      filename = os.path.join(results_dir, "fixed_interval_cycle%d.gif" % i)
      visualize_util.save_animation(np.array(images), filename, fps)

    # Cycle linearly through +-2 std dev of a fitted Gaussian.
    for i, base_code in enumerate(means[:num_animations]):
      images = []
      for j in range(base_code.shape[0]):
        code = np.repeat(np.expand_dims(base_code, 0), num_frames, axis=0)
        loc = np.mean(means[:, j])
        total_variance = np.mean(np.exp(logvars[:, j])) + np.var(means[:, j])
        scale = np.sqrt(total_variance)

        code[:, j] = visualize_util.cycle_interval(base_code[j], num_frames,