# Path: ldif/inference/metrics.py
# OCCNET_FSCORE_EPS = 1e-09
# def get_class(row):
# def print_pivot_table(class_mean_df, metric_name, metric_pretty_print):
# def aggregate_extracted(csv_path_or_df):
# def sample_points_and_face_normals(mesh, sample_count):
# def pointcloud_neighbor_distances_indices(source_points, target_points):
# def dot_product(a, b):
# def point_iou(pred_is_inside, gt_is_inside):
# def point_metrics(element):
# def element_to_example(element):
# def percent_below(dists, thresh):
# def f_score(a_to_b, b_to_a, thresh):
# def fscore(mesh1,
#            mesh2,
#            sample_count=100000,
#            tau=1e-04,
#            points1=None,
#            points2=None):
# def mesh_chamfer_via_points(mesh1,
#                             mesh2,
#                             sample_count=100000,
#                             points1=None,
#                             points2=None):
# def get_points(mesh1, mesh2, points1, points2, sample_count):
# def normal_consistency(mesh1, mesh2, sample_count=100000, return_points=False):
# def print_mesh_metrics(pred_mesh, gt_mesh, sample_count=100000):
# def compute_all(sif_vector, decoder, e, resolution=256, sample_count=100000):
# def print_all(sif_vector, decoder, e, resolution=256, sample_count=100000):
# def all_mesh_metrics(mesh1, mesh2, sample_count=100000):
# def mesh_metrics(element):
# 
# Path: ldif/util/file_util.py
# def readlines(p):
# def readbin(p):
# def writebin(p, s):
# def writetxt(p, s):
# def write_np(path, arr):
# def read_grd(path):
# def read_sif_v1(path, verbose=False):
# def read_lines(p):
# def read_image(p):
# def read_npz(p):
# def read_np(p):
# def read_txt_to_np(p):
# def read_py2_pkl(p):
# def write_mesh(path, mesh):
# def read_mesh(path):
# def read_csv(path):
# def read_normals(path_to_dir, im_count=20, leading='_depth'):
# def write_points(path_ext_optional, points):
# def write_grd(path, volume, world2grid=None):
# def write_depth_image(path, depth_image):
# 
# Path: ldif/util/mesh_util.py
# def serialize(mesh):
# def deserialize(mesh_str):
# def remove_small_components(mesh, min_volume=5e-05):
# 
# Path: ldif/util/file_util.py
# def readlines(p):
# def readbin(p):
# def writebin(p, s):
# def writetxt(p, s):
# def write_np(path, arr):
# def read_grd(path):
# def read_sif_v1(path, verbose=False):
# def read_lines(p):
# def read_image(p):
# def read_npz(p):
# def read_np(p):
# def read_txt_to_np(p):
# def read_py2_pkl(p):
# def write_mesh(path, mesh):
# def read_mesh(path):
# def read_csv(path):
# def read_normals(path_to_dir, im_count=20, leading='_depth'):
# def write_points(path_ext_optional, points):
# def write_grd(path, volume, world2grid=None):
# def write_depth_image(path, depth_image):


from absl import app
from absl import flags
from ldif.inference import metrics
from ldif.results import results_pb2
from ldif.util import file_util
from ldif.util import mesh_util
from ldif.util.file_util import log
import apache_beam as beam
import numpy as np
import pandas as pd

flags.DEFINE_boolean('write_metrics', None,
                     'Whether to write full metrics in the results.')
flags.mark_flag_as_required('write_metrics')

flags.DEFINE_boolean('write_metric_summaries', None,
                     'Whether to write summaries of metrics in the results.')
flags.mark_flag_as_required('write_metric_summaries')

flags.DEFINE_boolean('transform', True,
                     'Whether to transform from the gaps to occnet frame.')


def make_metrics(proto):
  """Builds a dictionary containing proto elements."""
  key, s = proto
  p = results_pb2.Results.FromString(s)
  mesh_path = FLAGS.occnet_dir + key.replace('test/', '') + '.ply'
  log.warning('Mesh path: %s' % mesh_path)
  try:
    mesh = file_util.read_mesh(mesh_path)
    if FLAGS.transform:
      # TODO(ldif-user) Set up the path to the transformation:
      tx_path = 'ROOT_DIR/%s/occnet_to_gaps.txt' % key
      occnet_to_gaps = file_util.read_txt_to_np(tx_path).reshape([4, 4])
      gaps_to_occnet = np.linalg.inv(occnet_to_gaps)
      mesh.apply_transform(gaps_to_occnet)
  # pylint: disable=broad-except
  except Exception as e:
    # pylint: enable=broad-except

# Path: ldif/inference/metrics.py
# OCCNET_FSCORE_EPS = 1e-09
# def get_class(row):
# def print_pivot_table(class_mean_df, metric_name, metric_pretty_print):
# def aggregate_extracted(csv_path_or_df):
# def sample_points_and_face_normals(mesh, sample_count):
# def pointcloud_neighbor_distances_indices(source_points, target_points):
# def dot_product(a, b):
# def point_iou(pred_is_inside, gt_is_inside):
# def point_metrics(element):
# def element_to_example(element):
# def percent_below(dists, thresh):
# def f_score(a_to_b, b_to_a, thresh):
# def fscore(mesh1,
#            mesh2,
#            sample_count=100000,
#            tau=1e-04,
#            points1=None,
#            points2=None):
# def mesh_chamfer_via_points(mesh1,
#                             mesh2,
#                             sample_count=100000,
#                             points1=None,
#                             points2=None):
# def get_points(mesh1, mesh2, points1, points2, sample_count):
# def normal_consistency(mesh1, mesh2, sample_count=100000, return_points=False):
# def print_mesh_metrics(pred_mesh, gt_mesh, sample_count=100000):
# def compute_all(sif_vector, decoder, e, resolution=256, sample_count=100000):
# def print_all(sif_vector, decoder, e, resolution=256, sample_count=100000):
# def all_mesh_metrics(mesh1, mesh2, sample_count=100000):
# def mesh_metrics(element):
# 
# Path: ldif/util/file_util.py
# def readlines(p):
# def readbin(p):
# def writebin(p, s):
# def writetxt(p, s):
# def write_np(path, arr):
# def read_grd(path):
# def read_sif_v1(path, verbose=False):
# def read_lines(p):
# def read_image(p):
# def read_npz(p):
# def read_np(p):
# def read_txt_to_np(p):
# def read_py2_pkl(p):
# def write_mesh(path, mesh):
# def read_mesh(path):
# def read_csv(path):
# def read_normals(path_to_dir, im_count=20, leading='_depth'):
# def write_points(path_ext_optional, points):
# def write_grd(path, volume, world2grid=None):
# def write_depth_image(path, depth_image):
# 
# Path: ldif/util/mesh_util.py
# def serialize(mesh):
# def deserialize(mesh_str):
# def remove_small_components(mesh, min_volume=5e-05):
# 
# Path: ldif/util/file_util.py
# def readlines(p):
# def readbin(p):
# def writebin(p, s):
# def writetxt(p, s):
# def write_np(path, arr):
# def read_grd(path):
# def read_sif_v1(path, verbose=False):
# def read_lines(p):
# def read_image(p):
# def read_npz(p):
# def read_np(p):
# def read_txt_to_np(p):
# def read_py2_pkl(p):
# def write_mesh(path, mesh):
# def read_mesh(path):
# def read_csv(path):
# def read_normals(path_to_dir, im_count=20, leading='_depth'):
# def write_points(path_ext_optional, points):
# def write_grd(path, volume, world2grid=None):
# def write_depth_image(path, depth_image):


# Path: ldif/results/eval_unseen_occnet.py
from absl import app
from absl import flags
from ldif.inference import metrics
from ldif.results import results_pb2
from ldif.util import file_util
from ldif.util import mesh_util
from ldif.util.file_util import log
import apache_beam as beam
import numpy as np
import pandas as pd


flags.DEFINE_boolean('write_metrics', None,
                     'Whether to write full metrics in the results.')
flags.mark_flag_as_required('write_metrics')

flags.DEFINE_boolean('write_metric_summaries', None,
                     'Whether to write summaries of metrics in the results.')
flags.mark_flag_as_required('write_metric_summaries')

flags.DEFINE_boolean('transform', True,
                     'Whether to transform from the gaps to occnet frame.')


def make_metrics(proto):
  """Builds a dictionary containing proto elements."""
  key, s = proto
  p = results_pb2.Results.FromString(s)
  mesh_path = FLAGS.occnet_dir + key.replace('test/', '') + '.ply'
  log.warning('Mesh path: %s' % mesh_path)
  try:
    mesh = file_util.read_mesh(mesh_path)
    if FLAGS.transform:
      # TODO(ldif-user) Set up the path to the transformation:
      tx_path = 'ROOT_DIR/%s/occnet_to_gaps.txt' % key
      occnet_to_gaps = file_util.read_txt_to_np(tx_path).reshape([4, 4])
      gaps_to_occnet = np.linalg.inv(occnet_to_gaps)
      mesh.apply_transform(gaps_to_occnet)
  # pylint: disable=broad-except
  except Exception as e:
    # pylint: enable=broad-except

    log.error("Couldn't load %s, skipping due to %s." % (mesh_path, repr(e)))