# -*- coding: utf-8 -*-
# @Author: Aliaksei Halachkin
# @Date:   2016-10-28 22:18:29
# @Last Modified by:   Aliaksei Halachkin
# @Last Modified time: 2016-10-28 22:33:17

import sys
sys.path.append('..\\pyclf\\model')
sys.path.append('..\\pyclf\\validation')

 
import os.path
import json
import numpy as np
import pandas as pd

dataset = "..//training_data//trainset_v3_with_attributes_and_profile_clusters.csv"
train_fname = "..//training_data//matlabtxt//trainset_v3_train.txt"
val_fname = "..//training_data//matlabtxt//trainset_v3_val.txt"
test_fname = "..//training_data//matlabtxt//trainset_v3_test.txt"


with open('..//pyclf//configs/feature_headers.json', 'r') as f:
    feature_headers = json.load(f)

with open('..//pyclf//configs//train_val_test.json', 'r') as f:
    train_val_test = json.load(f)

with open('..//pyclf//configs//class_mapping.json', 'r') as f:
    mapper = json.load(f)
    
mapper = mapper["loop_table"]


#prepare data
df = pd.read_csv(dataset, sep=';')
df.loc[:,'class_name'] = df.loc[:,'class_name'].map( 
            lambda x: mapper[x] if x in mapper.keys() else x)

cols = feature_headers['feature_names'] + ['class_name']

df_train = df.loc[train_val_test["train_ind"]][cols]
df_val = df.loc[train_val_test["val_ind"]][cols]
df_test = df.loc[train_val_test["test_ind"]][cols]




df_train.to_csv(train_fname, sep=' ', index=False, header=False)
df_val.to_csv(val_fname, sep=' ', index=False, header=False)
df_test.to_csv(test_fname, sep=' ', index=False, header=False)