# Creation du dataset SMD

import pandas as pd
import os
import sys
import json 
train_df=pd.DataFrame()
test_df=pd.DataFrame()

data_root_path="/home/shared/shared_results/Demonstrators_results/comp_511_7/SMD_dataset/"

for file in os.listdir(data_root_path+"ServerMachineDataset/train/"):
    print("fichier traité", file)

    train_tmp_df=pd.read_csv(data_root_path+"ServerMachineDataset/train/"+file,sep=",",header=None)
    train_tmp_df.columns=list(range(1,39))
    train_tmp_df["machine_label"]=file.split(".")[0]
    train_df=pd.concat([train_df,train_tmp_df])

train_df=train_df.set_index("machine_label")        
for file in os.listdir(data_root_path+"ServerMachineDataset/test/"):
    print("fichier traité", file)

    with open(data_root_path+"ServerMachineDataset/interpretation_label/"+file) as f:
        d={}
        for line in f:
           (key, val) = line.split(":")
           d[key] = val.replace("\n","")
        
    
    
    test_tmp_df=pd.read_csv(data_root_path+"ServerMachineDataset/test/"+file,sep=",",header=None)
    test_tmp_df.columns=list(range(1,39))
    test_tmp_df["machine_label"]=file.split(".")[0] 
    label_tmp_df=pd.read_csv(data_root_path+"ServerMachineDataset/test_label/"+file,sep=",",header=None)
    label_tmp_df.columns=["label_anomalie"]
    test_tmp_df=pd.concat([test_tmp_df,label_tmp_df],axis=1)
    test_df=pd.concat([test_df,test_tmp_df])


test_df["num_index"]=test_df.index
test_df=test_df.set_index(["machine_label"])
 
train_df.to_pickle("train_df.pkl")
test_df.to_pickle("test_df.pkl")