# 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/inference/util.py
# def parse_xid_str(xidstr):
# def ensure_split_valid(split):
# def ensure_category_valid(category):
# def ensure_synset_valid(synset):
# def ensure_hash_valid(h):
# def parse_xid_to_ckpt(xid_to_ckpt):
# def get_npz_paths(split, category, modifier=''):
# def get_mesh_identifiers(split, category):
# def get_rgb_paths(split, category, modifier=''):
# def rgb_path_to_synset_and_hash(rgb_path):
# def rgb_path_to_npz_path(rgb_path, split, dataset='shapenet-occnet'):
# def parse_npz_path(path):
# def read_png_to_float_npy_with_reraising(path):
# 
# 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):


import functools
import apache_beam as beam
import pandas as pd
import tqdm
from absl import app
from absl import flags
from ldif.inference import metrics
from ldif.inference import util as inference_util
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
    # pylint: enable=broad-except
    return []


def make_metrics(proto):
  key, s = proto
  p = results_pb2.Results.FromString(s)
  mesh = mesh_util.deserialize(p.mesh)
  gt_mesh = mesh_util.deserialize(p.gt_mesh)
  nc, fst, fs2t, chamfer = metrics.all_mesh_metrics(mesh, gt_mesh)
  return {
      'key': key,
      'Normal Consistency': nc,
      'F-Score (tau)': fst,
      'F-Score (2*tau)': fs2t,
      'Chamfer': chamfer,
      'IoU': p.iou
  }


def save_metrics(elts):
  elts = pd.DataFrame(elts)
  csv_str = elts.to_csv()
  return csv_str


def get_result_path(xid):
  """Generates the result path associated with the requested XID."""
  # TODO(ldif-user) Set up the result path:
  base = FLAGS.input_dir + '/ROOT%i-*00000-*' % xid

# 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/inference/util.py
# def parse_xid_str(xidstr):
# def ensure_split_valid(split):
# def ensure_category_valid(category):
# def ensure_synset_valid(synset):
# def ensure_hash_valid(h):
# def parse_xid_to_ckpt(xid_to_ckpt):
# def get_npz_paths(split, category, modifier=''):
# def get_mesh_identifiers(split, category):
# def get_rgb_paths(split, category, modifier=''):
# def rgb_path_to_synset_and_hash(rgb_path):
# def rgb_path_to_npz_path(rgb_path, split, dataset='shapenet-occnet'):
# def parse_npz_path(path):
# def read_png_to_float_npy_with_reraising(path):
# 
# 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/extract_results.py
import functools
import apache_beam as beam
import pandas as pd
import tqdm
from absl import app
from absl import flags
from ldif.inference import metrics
from ldif.inference import util as inference_util
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

    # pylint: enable=broad-except
    return []


def make_metrics(proto):
  key, s = proto
  p = results_pb2.Results.FromString(s)
  mesh = mesh_util.deserialize(p.mesh)
  gt_mesh = mesh_util.deserialize(p.gt_mesh)
  nc, fst, fs2t, chamfer = metrics.all_mesh_metrics(mesh, gt_mesh)
  return {
      'key': key,
      'Normal Consistency': nc,
      'F-Score (tau)': fst,
      'F-Score (2*tau)': fs2t,
      'Chamfer': chamfer,
      'IoU': p.iou
  }


def save_metrics(elts):
  elts = pd.DataFrame(elts)
  csv_str = elts.to_csv()
  return csv_str


def get_result_path(xid):
  """Generates the result path associated with the requested XID."""
  # TODO(ldif-user) Set up the result path:
  base = FLAGS.input_dir + '/ROOT%i-*00000-*' % xid

  matches = file_util.glob(base)