from torch.utils.data import Dataset, DataLoader
import pandas as pd
import torch
import numpy as np
from utils.Arg import Arg
arg = Arg()
time_split = arg.time_split
powermax = arg.power_max
powermin = arg.power_min

class TimeSeriesDataset(Dataset):
    def __init__(self, file_inputs_2, file_inputs_3):
        self.inputs_3 = pd.read_csv(file_inputs_3)#18维度
        self.inputs_3 = self.inputs_3.iloc[:,1:-1]
        self.target = pd.read_csv(file_inputs_2 ,usecols=[1])#1维
        self.time = pd.read_csv(file_inputs_2,usecols=[0])
        self.inputs_3.iloc[:,4:12] = np.sin(self.inputs_3.iloc[:,4:12] / 180 * np.pi)
        self.inputs_3.iloc[:,:4]  = (self.inputs_3.iloc[:,:4]  - self.inputs_3.iloc[:,:4].min()) / (self.inputs_3.iloc[:,:4].max() - self.inputs_3.iloc[:,:4] .min())
        self.inputs_3.iloc[:, 12:] = (self.inputs_3.iloc[:, 12:] - self.inputs_3.iloc[:, 12:].min()) / (
                    self.inputs_3.iloc[:, 12:].max() - self.inputs_3.iloc[:, 12:].min())
        # print(self.inputs_3.iloc[:, 13].max())
        # print(self.inputs_3.iloc[:, 13].min())
        self.target.iloc[:, 0] = (self.target.iloc[:, 0] - powermin) / (powermax)
        assert len(self.inputs_3) == len(self.target) == len(self.time)

    def __len__(self):
        return len(self.target) - time_split

    def __getitem__(self, idx):
        inputs_3 = self.inputs_3.iloc[idx:idx + time_split, :]
        #print(inputs_3)
        target = self.target.iloc[idx + time_split - 1, :]
        inputs_3 = torch.tensor(inputs_3.values, dtype=torch.float32).unsqueeze(1)
        inputs_3 = (inputs_3.view(inputs_3.size()[0],inputs_3.size()[2]))
        target = torch.tensor(target.values, dtype=torch.float32)
        return inputs_3, target

# class TimeSeriesDataset(Dataset):
#     def __init__(self,  file_inputs_2,file_inputs_3):
#         self.inputs_3 = pd.read_csv(file_inputs_3,usecols=[2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19])#18维度
#         self.target = pd.read_csv(file_inputs_2,usecols=[2])#1维
#         self.time = pd.read_csv(file_inputs_2,usecols=[0])
#         self.inputs_3.iloc[:,0:6] = np.sin(self.inputs_3.iloc[:,0:6] / 180 * np.pi)
#         self.inputs_3.iloc[:,7:]  = (self.inputs_3.iloc[:,7:]  - self.inputs_3.iloc[:,7:].min()) / (self.inputs_3.iloc[:,7:].max() - self.inputs_3.iloc[:,7:] .min())
#         self.target.iloc[:, 0] = (self.target.iloc[:, 0] - powermin) / (powermax - powermin)
#         assert len(self.inputs_3) == len(self.target) == len(self.time)
#
#     def __len__(self):
#         return len(self.target) - 16
#
#     def __getitem__(self, idx):
#         inputs_3 = self.inputs_3.iloc[idx:idx+16,:]
#         target=self.target.iloc[idx+7,:]
#         inputs_3 = torch.tensor(inputs_3.values, dtype=torch.float32)
#         target = torch.tensor(target.values, dtype=torch.float32)
#         # print(inputs_3.size())
#         # print(target.size())
#         return inputs_3, target