'''
Created on 2017/7/20

@author: ful6ru04
'''
import os
import numpy as np
from sklearn.model_selection import train_test_split

if __name__ == '__main__':
    
    ###################################################################################################
    # Input file
    name_st = 'feature_static.npz'
    name_dy = 'feature_dynamic.npz'
    
    # Output file
    output_dir = './output-1'
    output_np_name = 'data.npz'
    output_f_name = 'feature.csv'

    # Sklearn parameter
    split_rate = .3  # how many percent of test data
    ###################################################################################################
    
    ### 1. Dataset path (please run Main1 and Main2 first if it doesn't exist)
    path_st = os.path.join('./output-1',name_st)
    path_dy = os.path.join('./output-1',name_dy)
    assert( os.path.exists(path_st) and os.path.exists(path_dy) ) # check if dataset exists
    
    ### 2. Read the dataset
    print('Read "'+path_st+'"')
    dataSt = np.load(path_st) # static
    npStMal = dataSt['npStMal']
    npStBen = dataSt['npStBen']
    print('npStMal shape: ' + str(npStMal.shape))
    print('npStBen shape: ' + str(npStBen.shape))
    
    print('Read "'+path_dy+'"')
    dataDy = np.load(path_dy) # dynamic
    npDyMal = dataDy['npDyMal']
    npDyBen = dataDy['npDyBen']
    print('npDyMal shape: ' + str(npDyMal.shape))
    print('npDyBen shape: ' + str(npDyBen.shape))
    
    ### 3. Concatenate the features
    fMal = np.concatenate((npStMal, npDyMal), axis=1) # (118, 182)
    fBen = np.concatenate((npStBen, npDyBen), axis=1) # (100, 182)

    ### 4. Split data
    mtrain_x, mtest_x, mtrain_y, mtest_y = train_test_split(fMal, np.ones((fMal.shape[0], 1)), test_size=split_rate)
    ntrain_x, ntest_x, ntrain_y, ntest_y = train_test_split(fBen,np.zeros((fBen.shape[0], 1)), test_size=split_rate)

    train_x = np.concatenate((mtrain_x, ntrain_x), axis=0)
    train_y = np.concatenate((mtrain_y, ntrain_y), axis=0)
    test_x = np.concatenate((mtest_x, ntest_x), axis=0)
    test_y = np.concatenate((mtest_y, ntest_y), axis=0)

    ### 6. Shuffle the feature array and label array by assign random indices
    indices_train = np.random.permutation(train_x.shape[0])
    train_x = train_x[indices_train]
    train_y = train_y[indices_train]
    indices_test = np.random.permutation(test_x.shape[0])
    test_x = test_x[indices_test]
    test_y = test_y[indices_test]
    print('Feature vector shuffled!')

    ### 7. Write features vector to file
    path = os.path.join(output_dir, output_np_name)
    np.savez(path, train_x=train_x, train_y=train_y, test_x=test_x, test_y=test_y )
    print('Feature concat done! Saved at "' + path + '"')

    ### 8. Write features and label vectors to csv file
    # path = os.path.join(output_dir, output_f_name)
    # data = np.concatenate((label, feature), axis=1)
    # np.savetxt(path, data.astype(int), fmt='%i', delimiter=",")
    # print('Feature csv saved at "' + path + '"')

