#!/usr/bin/env python

import argparse

argparser = argparse.ArgumentParser(description='ML Experiment Framework')
argparser.add_argument('configfile', help='Configuration file')
argparser.add_argument('--overridingh5dir', default=None, type=str,
                       help='Overriding directory for intermediate numerical HDF files.')
args = argparser.parse_args()

import os
import json
import configparser
from functools import partial

from mlexpt.data.adding_features import adding_no_features, convert_label_to_str
from mlexpt.experiment import add_multiple_features
from mlexpt.experiment import run_experiment


if __name__ == '__main__':
    # reading config file (.ini or .json)
    file_ext = os.path.splitext(args.configfile)[1]
    if file_ext.lower() == '.json':
        config = json.load(open(args.configfile, 'r'))
    elif file_ext == '.ini':
        config = configparser.ConfigParser()
        config.read(args.configparser)
    else:
        raise ValueError('The configuration file should have an extention ' +
                         'be either ".ini" or ".json", not "{}".')

    # identify label
    label = config['model']['target']

    # defining feature addition (can be configured)
    feature_adder = add_multiple_features([adding_no_features,
                                           partial(convert_label_to_str, label=label)])
    run_experiment(config,
                   feature_adder=feature_adder,
                   overriding_h5dir=args.overridingh5dir)
