import numpy as np
import pandas as pd
import pickle
import os
import matplotlib.pyplot as plt
feature_title = ['F1', 'F2', 'F3', 'F4', 'F5', 'F6', 'F7', 'F8', 'F9', 'F10',
                'F11', 'F12', 'F13', 'F14', 'F15', 'F16', 'F17', 'F18', 'F19', 'F20',
                'F21', 'F22', 'F23', 'F24']

kn_num = 500
uk_num = 500
tail_num = kn_num + uk_num
move_value = 2

df0_1 = pd.read_excel('PRONTO_data/Fault_0_1_Data.xlsx', header=None, names=feature_title).rolling(move_value).mean()
df0_1 = df0_1.tail(tail_num)
df0_1_Y = np.ones([df0_1.shape[0], 1]) * 0
df0_2 = pd.read_excel('PRONTO_data/Fault_0_2_Data.xlsx', header=None, names=feature_title).rolling(move_value).mean()
df0_2 = df0_2.tail(tail_num)
df0_2_Y = np.ones([df0_2.shape[0], 1]) * 0
df0_3 = pd.read_excel('PRONTO_data/Fault_0_3_Data.xlsx', header=None, names=feature_title).rolling(move_value).mean()
df0_3 = df0_3.tail(tail_num)
df0_3_Y = np.ones([df0_3.shape[0], 1]) * 0

df1_1 = pd.read_excel('PRONTO_data/Fault_1_1_Data.xlsx', header=None, names=feature_title).rolling(move_value).mean()
df1_1 = df1_1.tail(tail_num)
df1_1_Y = np.ones([df1_1.shape[0], 1]) * 1
df1_2 = pd.read_excel('PRONTO_data/Fault_1_2_Data.xlsx', header=None, names=feature_title).rolling(move_value).mean()
df1_2 = df1_2.tail(tail_num)
df1_2_Y = np.ones([df1_2.shape[0], 1]) * 1
df1_3 = pd.read_excel('PRONTO_data/Fault_1_3_Data.xlsx', header=None, names=feature_title).rolling(move_value).mean()
df1_3 = df1_3.tail(tail_num)
df1_3_Y = np.ones([df1_3.shape[0], 1]) * 1

df2_1 = pd.read_excel('PRONTO_data/Fault_2_1_Data.xlsx', header=None, names=feature_title).rolling(move_value).mean()
df2_1 = df2_1.tail(tail_num)
df2_1_Y = np.ones([df2_1.shape[0], 1]) * 2
df2_2 = pd.read_excel('PRONTO_data/Fault_2_2_Data.xlsx', header=None, names=feature_title).rolling(move_value).mean()
df2_2 = df2_2.tail(tail_num)
df2_2_Y = np.ones([df2_2.shape[0], 1]) * 2
df2_3 = pd.read_excel('PRONTO_data/Fault_2_3_Data.xlsx', header=None, names=feature_title).rolling(move_value).mean()
df2_3 = df2_3.tail(tail_num)
df2_3_Y = np.ones([df2_3.shape[0], 1]) * 2

df3_1 = pd.read_excel('PRONTO_data/Fault_3_1_Data.xlsx', header=None, names=feature_title).rolling(move_value).mean()
df3_1 = df3_1.tail(tail_num)
df3_1_Y = np.ones([df3_1.shape[0], 1]) * 3
df3_2 = pd.read_excel('PRONTO_data/Fault_3_2_Data.xlsx', header=None, names=feature_title).rolling(move_value).mean()
df3_2 = df3_2.tail(tail_num)# 3001
df3_2_Y = np.ones([df3_2.shape[0], 1]) * 3
df3_3 = pd.read_excel('PRONTO_data/Fault_3_3_Data.xlsx', header=None, names=feature_title).rolling(move_value).mean()
df3_3 = df3_3.tail(tail_num)# 3001
df3_3_Y = np.ones([df3_3.shape[0], 1]) * 3

df4_1 = pd.read_excel('PRONTO_data/Fault_4_1_Data.xlsx', header=None, names=feature_title).rolling(move_value).mean()
df4_1 = df4_1.tail(tail_num)
df4_1_Y = np.ones([df4_1.shape[0], 1]) * 4
df4_2 = pd.read_excel('PRONTO_data/Fault_4_2_Data.xlsx', header=None, names=feature_title).rolling(move_value).mean()
df4_2 = df4_2.iloc[100:tail_num + 100]
df4_2_Y = np.ones([df4_2.shape[0], 1]) * 4
df4_3 = pd.read_excel('PRONTO_data/Fault_4_3_Data.xlsx', header=None, names=feature_title).rolling(move_value).mean()
df4_3 = df4_3.iloc[100:tail_num + 100]
df4_3_Y = np.ones([df4_3.shape[0], 1]) * 4

Set_DomainA = pd.concat([df1_2, df2_2, df3_2, df0_2], ignore_index=True)
Set_DomainB = pd.concat([df1_3, df2_3, df3_3, df0_3], ignore_index=True)
Set_DomainC = pd.concat([df1_1, df2_1, df3_1, df0_1], ignore_index=True)

Set_DomainA_Label = np.concatenate((df1_2_Y, df2_2_Y, df3_2_Y, df0_2_Y), axis=0)
Set_DomainB_Label = np.concatenate((df1_3_Y, df2_3_Y, df3_3_Y, df0_3_Y), axis=0)
Set_DomainC_Label = np.concatenate((df1_1_Y, df2_1_Y, df3_1_Y, df0_1_Y), axis=0)

Known_array = np.r_[0:0.5*kn_num,
                    tail_num - 0.5*kn_num:tail_num + 0.5 * kn_num,
                    2*tail_num - 0.5*kn_num:2*tail_num + 0.5*kn_num,
                    3*tail_num - 0.5*kn_num:3*tail_num + 0.5*kn_num,
                    4*tail_num-0.5*kn_num:4*tail_num]
UnKwn_array = np.r_[0.5*kn_num:tail_num - 0.5*kn_num, tail_num + 0.5 * kn_num:2*tail_num - 0.5*kn_num,
                    2*tail_num + 0.5*kn_num:3*tail_num- 0.5*kn_num, 3*tail_num + 0.5*kn_num:4*tail_num-0.5*kn_num]
Known_array = Known_array.astype(int)
UnKwn_array = UnKwn_array.astype(int)

Set_DomainA_Known = Set_DomainA.iloc[Known_array,:].reset_index(drop=True)
Set_DomainA_UnKwn = Set_DomainA.iloc[UnKwn_array,:].reset_index(drop=True)
Set_DomainA_Label_Known = Set_DomainA_Label[Known_array,:]
Set_DomainA_Label_UnKwn = Set_DomainA_Label[UnKwn_array,:]

Set_DomainA_Label_Known = pd.DataFrame(Set_DomainA_Label_Known, columns=['Label'], dtype=int).reset_index(drop=True)
df_set = pd.concat([Set_DomainA_Known, Set_DomainA_Label_Known], axis=1)
df_set = df_set.reset_index(drop=True)
df_set.to_pickle('Datasets_A2.pkl')

Set_DomainA_Label_UnKwn = pd.DataFrame(Set_DomainA_Label_UnKwn, columns=['Label'], dtype=int).reset_index(drop=True)
df_set = pd.concat([Set_DomainA_UnKwn, Set_DomainA_Label_UnKwn], axis=1)
df_set = df_set.reset_index(drop=True)
df_set.to_pickle('Datasets_A1.pkl')

Set_DomainB_Known = Set_DomainB.iloc[Known_array,:].reset_index(drop=True)
Set_DomainB_UnKwn = Set_DomainB.iloc[UnKwn_array,:].reset_index(drop=True)
Set_DomainB_Label_Known = Set_DomainB_Label[Known_array,:]
Set_DomainB_Label_UnKwn = Set_DomainB_Label[UnKwn_array,:]

Set_DomainB_Label_Known = pd.DataFrame(Set_DomainB_Label_Known, columns=['Label'], dtype=int).reset_index(drop=True)
df_set = pd.concat([Set_DomainB_Known, Set_DomainB_Label_Known], axis=1)
df_set = df_set.reset_index(drop=True)
df_set.to_pickle('Datasets_B2.pkl')

Set_DomainB_Label_UnKwn = pd.DataFrame(Set_DomainB_Label_UnKwn, columns=['Label'], dtype=int).reset_index(drop=True)
df_set = pd.concat([Set_DomainB_UnKwn, Set_DomainB_Label_UnKwn], axis=1)
df_set = df_set.reset_index(drop=True)
df_set.to_pickle('Datasets_B1.pkl')

Set_DomainC_Known = Set_DomainC.iloc[Known_array,:].reset_index(drop=True)
Set_DomainC_UnKwn = Set_DomainC.iloc[UnKwn_array,:].reset_index(drop=True)
Set_DomainC_Label_Known = Set_DomainC_Label[Known_array,:]
Set_DomainC_Label_UnKwn = Set_DomainC_Label[UnKwn_array,:]

Set_DomainC_Label_Known = pd.DataFrame(Set_DomainC_Label_Known, columns=['Label'], dtype=int).reset_index(drop=True)
df_set = pd.concat([Set_DomainC_Known, Set_DomainC_Label_Known], axis=1)
df_set = df_set.reset_index(drop=True)
df_set.to_pickle('Datasets_C2.pkl')

Set_DomainC_Label_UnKwn = pd.DataFrame(Set_DomainC_Label_UnKwn, columns=['Label'], dtype=int).reset_index(drop=True)
df_set = pd.concat([Set_DomainC_UnKwn, Set_DomainC_Label_UnKwn], axis=1)
df_set = df_set.reset_index(drop=True)
df_set.to_pickle('Datasets_C1.pkl')
