# Path: morph_net/framework/generic_regularizers.py
# class NetworkRegularizer(object):  # pytype: disable=ignored-metaclass
# class OpRegularizer(object):  # pytype: disable=ignored-metaclass
#   def get_regularization_term(self, ops=None):
#   def get_cost(self, ops=None):
#   def op_regularizer_manager(self):
#   def name(self):
#   def cost_name(self):
#   def regularization_vector(self):
#   def alive_vector(self):
# def dimensions_are_compatible(op_regularizer):
# 
# Path: morph_net/framework/op_regularizer_manager.py
# ITERATION_LIMIT = 1000000
# class OpSlice(collections.namedtuple('OpSlice', ['op', 'slice'])):
# class Slice(collections.namedtuple('Slice', ['start_index', 'size'])):
# class OpRegularizerManager(object):
# class OpGroup(object):
#   def __str__(self):
#   def __str__(self):
#   def __init__(
#       self,
#       output_boundary: List[tf.Operation],
#       op_handler_dict=None,
#       create_grouping_regularizer=grouping_regularizers.MaxGroupingRegularizer,
#       force_group=None,
#       regularizer_blacklist=None,
#       input_boundary: List[tf.Operation] = None,
#       iteration_limit=ITERATION_LIMIT):
#   def ops(self):
#   def get_regularizer(self, op):
#   def create_op_group_for_op_slice(self, op_slice, is_source=True):
#   def group_op_slices(self, op_slices, omit_source_op_slices=None):
#   def slice_op(self, op, sizes):
#   def process_ops(self, ops):
#   def process_ops_last(self, ops):
#   def is_source_op(self, op):
#   def is_passthrough(self, op):
#   def get_op_slices(self, op):
#   def get_op_group(self, op_slice):
#   def _slice_op_slice(self, op_slice, sizes, size_index, size_count,
#                       new_op_slice_group):
#   def _slice_op_with_sizes(self, op, sizes, is_source, is_resliced):
#   def _get_source_slices(self, sizes, op_slices):
#   def _dfs_for_source_ops(self, output_boundary, input_boundary=None):
#   def _force_group_ops(self, force_group):
#   def __init__(self, op_slice=None, op_groups=None, omit_source_op_slices=None):
#   def op_slices(self):
#   def source_op_slices(self):
# 
# Path: morph_net/tools/structure_exporter.py
# SUPPORTED_OPS = ['Conv2D', 'Conv2DBackpropInput', 'Conv3D']
# ALIVE_FILENAME = 'alive'
# class StructureExporter(object):
#   def __init__(self,
#                op_regularizer_manager: orm.OpRegularizerManager,
#                remove_common_prefix: bool = False) -> None:
#   def tensors(self):
#   def populate_tensor_values(self, values: Dict[Text, Sequence[bool]]) -> None:
#   def get_alive_counts(self) -> Dict[Text, int]:
#   def save_alive_counts(self, f: IO[bytes]) -> None:
#   def create_file_and_save_alive_counts(self, base_dir: Text,
#                                         global_step: int) -> None:
# def get_remove_common_prefix_fn(iterable: Iterable[Text]
#                                ) -> Callable[[Text], Text]:
# def _compute_alive_counts(
#     alive_vectors: Dict[Text, Sequence[bool]]) -> Dict[Text, int]:
# def format_structure(structure: Dict[Text, int]) -> Text:


import json
import os
import tensorflow.compat.v1 as tf
from absl import flags
from absl.testing import parameterized
from morph_net.framework import generic_regularizers
from morph_net.framework import op_regularizer_manager as orm
from morph_net.tools import structure_exporter as se
from tensorflow.contrib import layers as contrib_layers
    self.exporter.populate_tensor_values(self.tensor_value_2)
    self.exporter.create_file_and_save_alive_counts(base_dir, 1009)
    self.assertAllEqual(
        _alive_from_file('ee/learned_structure/alive_1009'),
        self.expected_alive_2)
    self.assertAllEqual(
        _alive_from_file('ee/learned_structure/alive_19'),
        self.expected_alive_1)

  @parameterized.parameters(
      ([], []),
      (['', ''], ['', '']),
      (['a', 'a'], ['a', 'a']),  # No / present
      (['/', '/'], ['', '']),
      (['/x', '/x'], ['x', 'x']),
      (['a/x', 'a/x'], ['x', 'x']),
      (['abc/', 'abc/', 'abc/'], ['', '', '']),
      (['abc/x', 'abc/x', 'abd/x'], ['abc/x', 'abc/x', 'abd/x']),
      (['abc/x', 'abc/y', 'abc/z'], ['x', 'y', 'z']),
      (['abc/x/', 'abc/y', 'abc/z/'], ['x/', 'y', 'z/']),
      (['abc/x/', 'abc/y/', 'abc/z/'], ['x/', 'y/', 'z/']),
  )
  def test_find_common_prefix_size(self, iterable, expected_result):
    rename_op = se.get_remove_common_prefix_fn(iterable)
    self.assertEqual(expected_result, list(map(rename_op, iterable)))


class TestStructureExporterRemovePrefix(tf.test.TestCase):

  def test_removes_prefix(self):

# Path: morph_net/framework/generic_regularizers.py
# class NetworkRegularizer(object):  # pytype: disable=ignored-metaclass
# class OpRegularizer(object):  # pytype: disable=ignored-metaclass
#   def get_regularization_term(self, ops=None):
#   def get_cost(self, ops=None):
#   def op_regularizer_manager(self):
#   def name(self):
#   def cost_name(self):
#   def regularization_vector(self):
#   def alive_vector(self):
# def dimensions_are_compatible(op_regularizer):
# 
# Path: morph_net/framework/op_regularizer_manager.py
# ITERATION_LIMIT = 1000000
# class OpSlice(collections.namedtuple('OpSlice', ['op', 'slice'])):
# class Slice(collections.namedtuple('Slice', ['start_index', 'size'])):
# class OpRegularizerManager(object):
# class OpGroup(object):
#   def __str__(self):
#   def __str__(self):
#   def __init__(
#       self,
#       output_boundary: List[tf.Operation],
#       op_handler_dict=None,
#       create_grouping_regularizer=grouping_regularizers.MaxGroupingRegularizer,
#       force_group=None,
#       regularizer_blacklist=None,
#       input_boundary: List[tf.Operation] = None,
#       iteration_limit=ITERATION_LIMIT):
#   def ops(self):
#   def get_regularizer(self, op):
#   def create_op_group_for_op_slice(self, op_slice, is_source=True):
#   def group_op_slices(self, op_slices, omit_source_op_slices=None):
#   def slice_op(self, op, sizes):
#   def process_ops(self, ops):
#   def process_ops_last(self, ops):
#   def is_source_op(self, op):
#   def is_passthrough(self, op):
#   def get_op_slices(self, op):
#   def get_op_group(self, op_slice):
#   def _slice_op_slice(self, op_slice, sizes, size_index, size_count,
#                       new_op_slice_group):
#   def _slice_op_with_sizes(self, op, sizes, is_source, is_resliced):
#   def _get_source_slices(self, sizes, op_slices):
#   def _dfs_for_source_ops(self, output_boundary, input_boundary=None):
#   def _force_group_ops(self, force_group):
#   def __init__(self, op_slice=None, op_groups=None, omit_source_op_slices=None):
#   def op_slices(self):
#   def source_op_slices(self):
# 
# Path: morph_net/tools/structure_exporter.py
# SUPPORTED_OPS = ['Conv2D', 'Conv2DBackpropInput', 'Conv3D']
# ALIVE_FILENAME = 'alive'
# class StructureExporter(object):
#   def __init__(self,
#                op_regularizer_manager: orm.OpRegularizerManager,
#                remove_common_prefix: bool = False) -> None:
#   def tensors(self):
#   def populate_tensor_values(self, values: Dict[Text, Sequence[bool]]) -> None:
#   def get_alive_counts(self) -> Dict[Text, int]:
#   def save_alive_counts(self, f: IO[bytes]) -> None:
#   def create_file_and_save_alive_counts(self, base_dir: Text,
#                                         global_step: int) -> None:
# def get_remove_common_prefix_fn(iterable: Iterable[Text]
#                                ) -> Callable[[Text], Text]:
# def _compute_alive_counts(
#     alive_vectors: Dict[Text, Sequence[bool]]) -> Dict[Text, int]:
# def format_structure(structure: Dict[Text, int]) -> Text:


# Path: morph_net/tools/structure_exporter_test.py
import json
import os
import tensorflow.compat.v1 as tf
from absl import flags
from absl.testing import parameterized
from morph_net.framework import generic_regularizers
from morph_net.framework import op_regularizer_manager as orm
from morph_net.tools import structure_exporter as se
from tensorflow.contrib import layers as contrib_layers

    self.exporter.populate_tensor_values(self.tensor_value_2)
    self.exporter.create_file_and_save_alive_counts(base_dir, 1009)
    self.assertAllEqual(
        _alive_from_file('ee/learned_structure/alive_1009'),
        self.expected_alive_2)
    self.assertAllEqual(
        _alive_from_file('ee/learned_structure/alive_19'),
        self.expected_alive_1)

  @parameterized.parameters(
      ([], []),
      (['', ''], ['', '']),
      (['a', 'a'], ['a', 'a']),  # No / present
      (['/', '/'], ['', '']),
      (['/x', '/x'], ['x', 'x']),
      (['a/x', 'a/x'], ['x', 'x']),
      (['abc/', 'abc/', 'abc/'], ['', '', '']),
      (['abc/x', 'abc/x', 'abd/x'], ['abc/x', 'abc/x', 'abd/x']),
      (['abc/x', 'abc/y', 'abc/z'], ['x', 'y', 'z']),
      (['abc/x/', 'abc/y', 'abc/z/'], ['x/', 'y', 'z/']),
      (['abc/x/', 'abc/y/', 'abc/z/'], ['x/', 'y/', 'z/']),
  )
  def test_find_common_prefix_size(self, iterable, expected_result):
    rename_op = se.get_remove_common_prefix_fn(iterable)
    self.assertEqual(expected_result, list(map(rename_op, iterable)))


class TestStructureExporterRemovePrefix(tf.test.TestCase):

  def test_removes_prefix(self):

    exporter = se.StructureExporter(