import os.path
from exceptions import Exception
import numpy as np
import pandas as pd
from scandir import walk
from fnmatch import fnmatch
from parse_datetime import parse_datetime
from frq_info import IncorrectFormatException
from frq_features import FRQFeatures, FeaturesMissingException


INPUT_POOR_TRAINSET = "..\\training_data\\trainset_v2.csv"
OUTPUT_EXTENDED_TABLE = "..\\training_data\\trainset_v2_extended.csv"

def data_to_csv(input_trainset_filename, output_table_filename):
    """Combines .frq .csv tagged data from trainset to one csv table
    """
    poor_trainset = pd.read_csv(input_trainset_filename, sep = ';')
    n_samples = poor_trainset.shape[0]
    n_fails = 0
    #  make table with id (filename), 
    # .frq, .csv (total_weight and axles count) data and label        
    columns = ('filename', ) + FRQFeatures.names + \
                              ('total_weight', 'axles_count') + \
                              ('class_name', ) 
    data = []
    for idx in range(n_samples):
        if idx % 100 == 0:
            print str(idx) + "\\" + str(n_samples) + " samples read" 
        try:
            filename = poor_trainset.iloc[idx][0]
            label = poor_trainset.iloc[idx][1]
            frq = FRQFeatures(filename + '.frq')
            dfcsv = pd.read_csv(filename + '.csv', sep = ';', decimal = ',')
            row = [filename] + frq.features + \
                  [dfcsv['total_weight'][0]] + \
                  [dfcsv['axles_count'][0]] + \
                  [label]
            data.append(row)

        except Exception as e:
            n_fails += 1
            print e

    print str(n_samples - n_fails) + " samples read"
    print str(n_fails) + " samples failed to read"
    df = pd.DataFrame(data=data, columns=columns)
    df.to_csv(path_or_buf=output_table_filename,
              sep=';',
              index = False,
              float_format='%.3f')

    return df


if __name__ == "__main__":
    data_to_csv(INPUT_POOR_TRAINSET, OUTPUT_EXTENDED_TABLE)
