# python3.7
"""Trains semantic boundary from latent space.

Basically, this file takes a collection of `latent code - attribute score`
pairs, and find the separation boundary by treating it as a bi-classification
problem and training a linear SVM classifier. The well-trained decision boundary
of the SVM classifier will be saved as the boundary corresponding to a
particular semantic from the latent space. The normal direction of the boundary
can be used to manipulate the correpsonding attribute of the synthesis.
"""

import os.path
import argparse
import numpy as np

from utils.logger import setup_logger
from utils.manipulator import train_boundary
from config import config

def parse_args():
  """Parses arguments."""
  parser = argparse.ArgumentParser(
      description='Train semantic boundary with given latent codes and '
                  'attribute scores.')
  # parser.add_argument('-o', '--output_dir', type=str, required=True,
  #                     help='Directory to save the output results. (required)')
  # parser.add_argument('-c', '--latent_codes_path', type=str, required=True,
  #                     help='Path to the input latent codes. (required)')
  # parser.add_argument('-s', '--scores_path', type=str, required=True,
  #                     help='Path to the input attribute scores. (required)')
  parser.add_argument('-n', '--chosen_num_or_ratio', type=float, default=0.02,
                      help='How many samples to choose for training. '
                           '(default: 0.2)')
  parser.add_argument('-r', '--split_ratio', type=float, default=0.7,
                      help='Ratio with which to split training and validation '
                           'sets. (default: 0.7)')
  parser.add_argument('-V', '--invalid_value', type=float, default=None,
                      help='Sample whose attribute score is equal to this '
                           'field will be ignored. (default: None)')
  parser.add_argument('-t', '--latent_space_type', type=str, default='w',
                      choices=['w','z','wp'])

  return parser.parse_args()


def main():
  """Main function."""
  args = parse_args()
  logger = setup_logger(config.OUTPUT_PATH, logger_name='generate_data')

  logger.info('Loading latent codes.')
  latent_codes_path = os.path.join(config.OUTPUT_PATH,"{}.npy".format(args.latent_space_type))

  if not os.path.isfile(latent_codes_path):
    raise ValueError(f'Latent codes `{latent_codes_path}` does not exist!')
  latent_codes = np.load(latent_codes_path)

  logger.info('Loading attribute scores.')
  if not os.path.isfile(config.SCORES_PATH):
    raise ValueError(f'Attribute scores `{config.SCORES_PATH}` does not exist!')
  scores = np.load(config.SCORES_PATH)
  print(scores.shape)

  boundary = train_boundary(latent_codes=latent_codes,
                            scores=scores,
                            chosen_num_or_ratio=args.chosen_num_or_ratio,
                            split_ratio=args.split_ratio,
                            invalid_value=args.invalid_value,
                            logger=logger)
  np.save(config.BOUNDARY_PATH, boundary)


if __name__ == '__main__':
  main()
