from __future__ import print_function
from pkg_resources import resource_filename
import os
import argparse
import logging
from resmico import predict

# functions
def get_desc():
    desc = 'Predict values'
    return desc

def parse_args(test_args=None, subparsers=None):
    desc = get_desc()
    epi = """DESCRIPTION:
    Predicting misassemblies by used a model generated by `DeepMAsED train`
    or the pre-trained model that comes with the DeepMAsED package.
    
    #-- feature_file_table --#
    * See `DeepMAsED train` for a description 
    * Note that the 'assembler' and 'rep' columns are not actually used, 
      so placeholder values can used. Just make sure to include unique 
      `assembler` + `rep` combinations for each row in the table.
    """
    if subparsers:
        parser = subparsers.add_parser('predict', description=desc, epilog=epi,
                                       formatter_class=argparse.RawTextHelpFormatter)
    else:
        parser = argparse.ArgumentParser(description=desc, epilog=epi,
                                         formatter_class=argparse.RawTextHelpFormatter)
    #default trained model
    pkg_model = resource_filename('resmico','model/fl_all_model.h5')
    pkg_mstd = resource_filename('resmico','model/fl_all_mean_std.pkl')
    pkg_path, pkg_model = os.path.split(pkg_model)
    _, pkg_mstd  = os.path.split(pkg_mstd)
    # args
    parser.add_argument('feature_file_table',  metavar='feature_file_table', type=str, 
                        help='Table listing feature table files (see DESCRIPTION)')
    parser.add_argument('--model-path',  default=pkg_path, type=str, 
                        help='Directory containing the model (default: %(default)s)')
    parser.add_argument('--model-name', default=pkg_model, type=str, 
                        help='Model name in the model_path (default: %(default)s)')
    # parser.add_argument('--mstd-name', default=pkg_mstd, type=str,
    #                     help='Data mean and std name in the model_path (default: %(default)s)')
    parser.add_argument('--save-path', default='.', type=str, 
                        help='Directory where to save output (default: %(default)s)')
    parser.add_argument('--save-name', default='deepmased', type=str, 
                        help='Prefix for name in the save_path (default: %(default)s)')        
    # parser.add_argument('--cpu-only', action='store_true', default=False,
    #                     help='Only use CPUs, and no GPUs (default: %(default)s)')
    parser.add_argument('--seed', default=12, type=int, 
                        help='Seed used for numpy.random (default: %(default)s)')
    parser.add_argument('--batch-size', default=64, type=int,
                        help='Batch size (default: %(default)s)')
    parser.add_argument('--n-procs', default=1, type=int, 
                        help='Number of parallel processes; just used for pickling (default: %(default)s)')
    parser.add_argument('--min-len', default=1000, type=int,
                        help='Definition of -long- contig. If want predict for all use(default: %(default)s)')
    parser.add_argument('--max-len', default=10000, type=int,
                        help='Max contig len, fixed input CNN (default: %(default)s)')
    parser.add_argument('--method-pred', default='chunks', type=str,
                        help='How to predict: only chunks supported in a final version')
    parser.add_argument('--mem-lim', default=500000, type=int,
                        help='Max contig that fits in one batch (default: %(default)s)')
    parser.add_argument('--window', default=5000, type=int,
                        help='Window size for chunks method, size of piece for random method (default: %(default)s)')
    # test args
    if test_args:
        args = parser.parse_args(test_args)
        return args

    return parser

def main(args=None):
    logging.basicConfig(format='%(asctime)s - %(message)s', level=logging.DEBUG)
    # Input
    if args is None:
        args = parse_args()
        print()
        print (args)
        print()
    # Main interface
    predict.main(args)
    
# main
if __name__ == '__main__':
    pass


