code: # Copyright 2017 Google Inc.
  #
  # 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.
  # ==============================================================================
  """DNC util ops and modules."""
  
  from __future__ import absolute_import, division, print_function
  
  import numpy as np
  import tensorflow as tf
  import os, sys, inspect
  
  
  def os_module_path():
      current_dir = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))
      parent_dir = os.path.dirname(current_dir)
      # sys.path.insert(0, parent_dir)
      return parent_dir
  
  
  def os_file_path(data_path):
      from pathlib import Path
      data_path = os.path.join(Path(__file__).parent.parent.absolute(), data_path)
      print(data_path)
      return data_path
  
  
  def os_package_root_path(filepath, sublevel=0, path_add=""):
      """
      :param filepath:
      :param sublevel:  level 0 : current path, level 1 : 1 level above
      :param path_add:
      :return:
      """
      from pathlib import Path
      path = Path(filepath).parent
      for i in range(1, sublevel + 1):
          path = path.parent
      
      path = os.path.join(path.absolute(), path_add)
      return path
  
  
  # print("check", os_package_root_path(__file__, sublevel=1) )
  
  
  def batch_invert_permutation(permutations):
      """Returns batched `tf.invert_permutation` for every row in `permutations`."""
      with tf.name_scope("batch_invert_permutation", values=[permutations]):
          unpacked = tf.unstack(permutations)
          inverses = [tf.invert_permutation(permutation) for permutation in unpacked]
          return tf.stack(inverses)
  
  
  def batch_gather(values, indices):
      """Returns batched `tf.gather` for every row in the input."""
      with tf.name_scope("batch_gather", values=[values, indices]):
          unpacked = zip(tf.unstack(values), tf.unstack(indices))
          result = [tf.gather(value, index) for value, index in unpacked]
          return tf.stack(result)
  
  
  def one_hot(length, index):
      """Return an nd array of given `length` filled with 0s and a 1 at `index`."""
      result = np.zeros(length)
      result[index] = 1
      return result
  
  
  def set_root_dir():
      current_dir = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))
      parent_dir = os.path.dirname(current_dir)
      sys.path.insert(0, parent_dir)
      return parent_dir
  
  
  
apis: tensorflow.invert_permutation tensorflow.unstack tensorflow.stack tensorflow.gather tensorflow.name_scope numpy.zeros
api_extract: [(26, 'os.path.dirname', 'os.path.dirname', (['current_dir'], {}), False, 'import os, sys, inspect\n'), (75, 'numpy.zeros', 'np.zeros', (['length'], {}), True, 'import numpy as np\n'), (82, 'os.path.dirname', 'os.path.dirname', (['current_dir'], {}), False, 'import os, sys, inspect\n'), (83, 'sys.path.insert', 'sys.path.insert', (['(0)', 'parent_dir'], {}), False, 'import os, sys, inspect\n'), (46, 'pathlib.Path', 'Path', (['filepath'], {}), False, 'from pathlib import Path\n'), (59, 'tensorflow.name_scope', 'tf.name_scope', (['"""batch_invert_permutation"""'], {'values': '[permutations]'}), True, 'import tensorflow as tf\n'), (60, 'tensorflow.unstack', 'tf.unstack', (['permutations'], {}), True, 'import tensorflow as tf\n'), (62, 'tensorflow.stack', 'tf.stack', (['inverses'], {}), True, 'import tensorflow as tf\n'), (67, 'tensorflow.name_scope', 'tf.name_scope', (['"""batch_gather"""'], {'values': '[values, indices]'}), True, 'import tensorflow as tf\n'), (70, 'tensorflow.stack', 'tf.stack', (['result'], {}), True, 'import tensorflow as tf\n'), (61, 'tensorflow.invert_permutation', 'tf.invert_permutation', (['permutation'], {}), True, 'import tensorflow as tf\n'), (68, 'tensorflow.unstack', 'tf.unstack', (['values'], {}), True, 'import tensorflow as tf\n'), (68, 'tensorflow.unstack', 'tf.unstack', (['indices'], {}), True, 'import tensorflow as tf\n'), (69, 'tensorflow.gather', 'tf.gather', (['value', 'index'], {}), True, 'import tensorflow as tf\n'), (25, 'inspect.currentframe', 'inspect.currentframe', ([], {}), False, 'import os, sys, inspect\n'), (81, 'inspect.currentframe', 'inspect.currentframe', ([], {}), False, 'import os, sys, inspect\n'), (33, 'pathlib.Path', 'Path', (['__file__'], {}), False, 'from pathlib import Path\n')]