# -*- coding: utf-8 -*-
# @Author: Aliaksei Halachkin
# @Date:   2016-11-01 21:29:17
# @Last Modified by:   Aliaksei Halachkin
# @Last Modified time: 2016-11-01 22:11:53


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


FRQ_FOLDER = "E:\\Camea\\Loops\\data"
INPUT_POOR_TRAINSET = "..\\training_data\\trainset_v3.csv"
OUTPUT_EXTENDED_TABLE = "..\\training_data\\trainset_v3_resampled.csv"

def interpolate(signal, pb, pe, profile_length):
    xp = [i for i in range(pb, pe)]
    yp = signal[pb:pe]
    xnew = np.linspace(pb, pe, profile_length)
    return np.interp(xnew, xp, yp)

def data_to_csv(input_trainset_filename, 
                output_table_filename, 
                profile_length):
    """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', ) + \
              tuple(["P" + str(i) for i in range(profile_length)]) + \
              ('class_name', ) 

    data = []
    for idx in range(n_samples):
        if idx % 100 == 0:
            print str(idx) + "\\" + str(n_samples) + " samples read" 
        try:
            filename = os.path.join(FRQ_FOLDER, poor_trainset.iloc[idx][0])
            label = poor_trainset.iloc[idx][1]
            frq = FRQFeatures(filename + '.frq')
            pb = int(frq.info["Pb"])
            pe = int(frq.info["Pe"])
            signal = frq.signals[0]
            #get profile
            p = interpolate(signal, pb, pe, profile_length)
            # print len(p)
            #normalize profile
            p = (p - p.min()) / (p.max() - p.min())
            row = [filename] + list(p)  + [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, 50)
