#!/usr/bin/python

import argparse
import sys

from happyml import datasets
from happyml.tools.data_creator import DataSetCreator


# Parse args.
parser = argparse.ArgumentParser(description="""
    Create simple datasets clicking on a plot.
    Use the left button to create points of class 0 and the right button to create points of class 1.
    Use the wheel button to delete a point.

    You can create points of more classes (maximun 10 classes) if you press the number keys 0-9 and
    after that you click with any mouse button. Onces you press a key number, all the points created
    with the mouse will be of this class unless you press another key.

    If all the points you create are of the same class, it will be considered that it is a regression
    dataset, i.e. one input feature and one output real number. Avoid this behaviour using the
    arguments '-r' or '--no-regression'.

    When you close the window, the dataset is printed to the stdout in a CSV format.
    The first column will be the target/output label.
    Use 'happy_data_creator > dataset.csv' to save the results on disk.
""", formatter_class=argparse.RawTextHelpFormatter)
parser.add_argument('-l', '--limits', dest='limits', nargs=4, type=float, default=(-1, 1, -1, 1),
                    metavar=('XMIN', 'XMAX', 'YMIN', 'YMAX'),
                    help='Axis limits (default: -1 1 -1 1)')
parser.add_argument('-n', '--no-scaled', dest='no_scaled', action='store_true',
                    help='Avoid scaling axes. Useful when the axes scales are quite different. Default: Scaled axes.')
parser.add_argument('-r', '--no-regression', dest='no_regression', action='store_true',
                    help='Avoid creating regression dataset still being all the points of the same class.')
parser.add_argument('-b', '--binary', dest='binary', action='store_true',
                    help='Forces a binary dataset (only points of two classes). If more than '
                         'two classes are found an ValueError will be raised.')
parser.add_argument('-1', '--ones', dest='ones', action='store_true',
                    help='The classes are mapped to -1/+1. Implicitly it forces the creation of a '
                         'binary dataset, hence, if more than two classes are found an ValueError '
                         'will be raised.')
parser.add_argument('-s', '--save-plot', dest='save_plot', default=False, metavar='IMAGE_FILE',
                    help='Save plot on a image file before closing.')
parser.add_argument('-d', '--dataset', dest='dataset', default=False, metavar='DATASET_FILE',
                    help='Load the given dataset.')
args = parser.parse_args()


def printDataSet(dataset):
    """Saves the created dataset to the stdout file, i.e.,
    prints the dataset.

    """
    datasets.save(sys.stdout, dataset)


def main():
    """Open a DataSetCreator and prints the result on the
    standard output.

    """
    dic_args = vars(args)
    creator = DataSetCreator(**dic_args)
    creator.show()
    dataset = creator.getDataSet()
    printDataSet(dataset)


if __name__ == "__main__":
    main()
