-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathdataset.py
171 lines (124 loc) · 5.49 KB
/
dataset.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#%%
import pandas as pd
import torch
from torch.utils.data import Dataset
import numpy as np
# %%
from torch.utils.data import DataLoader
def load_traffic(root, batch_size):
"""
Load traffic dataset
return train_loader, val_loader, test_loader
"""
df = pd.read_hdf(root)
df = df.reset_index()
df = df.rename(columns={"index":"utc"})
df["utc"] = pd.to_datetime(df["utc"], unit="s")
df = df.set_index("utc")
n_sensor = len(df.columns)
mean = df.values.flatten().mean()
std = df.values.flatten().std()
df = (df - mean)/std
df = df.sort_index()
# split the dataset
train_df = df.iloc[:int(0.75*len(df))]
val_df = df.iloc[int(0.75*len(df)):int(0.875*len(df))]
test_df = df.iloc[int(0.75*len(df)):]
train_loader = DataLoader(Traffic(train_df), batch_size=batch_size, shuffle=True)
val_loader = DataLoader(Traffic(val_df), batch_size=batch_size, shuffle=False)
test_loader = DataLoader(Traffic(test_df), batch_size=batch_size, shuffle=False)
return train_loader, val_loader, test_loader, n_sensor
class Traffic(Dataset):
def __init__(self, df, window_size=12, stride_size=1):
super(Traffic, self).__init__()
self.df = df
self.window_size = window_size
self.stride_size = stride_size
self.data, self.idx, self.time = self.preprocess(df)
def preprocess(self, df):
start_idx = np.arange(0,len(df)-self.window_size,self.stride_size)
end_idx = np.arange(self.window_size, len(df), self.stride_size)
delat_time = df.index[end_idx]-df.index[start_idx]
idx_mask = delat_time==pd.Timedelta(5*self.window_size,unit='min')
return df.values, start_idx[idx_mask], df.index[start_idx[idx_mask]]
def __len__(self):
length = len(self.idx)
return length
def __getitem__(self, index):
# N X K X L X D
start = self.idx[index]
end = start + self.window_size
data = self.data[start:end].reshape([self.window_size,-1, 1])
return torch.FloatTensor(data).transpose(0,1)
def load_water(root, batch_size,label=False):
data = pd.read_csv(root)
data = data.rename(columns={"Normal/Attack":"label"})
data.label[data.label!="Normal"]=1
data.label[data.label=="Normal"]=0
data["Timestamp"] = pd.to_datetime(data["Timestamp"])
data = data.set_index("Timestamp")
#%%
feature = data.iloc[:,:51]
mean_df = feature.mean(axis=0)
std_df = feature.std(axis=0)
norm_feature = (feature-mean_df)/std_df
norm_feature = norm_feature.dropna(axis=1)
n_sensor = len(norm_feature.columns)
train_df = norm_feature.iloc[:int(0.6*len(data))]
train_label = data.label.iloc[:int(0.6*len(data))]
val_df = norm_feature.iloc[int(0.6*len(data)):int(0.8*len(data))]
val_label = data.label.iloc[int(0.6*len(data)):int(0.8*len(data))]
test_df = norm_feature.iloc[int(0.8*len(data)):]
test_label = data.label.iloc[int(0.8*len(data)):]
if label:
train_loader = DataLoader(WaterLabel(train_df,train_label), batch_size=batch_size, shuffle=True)
else:
train_loader = DataLoader(Water(train_df,train_label), batch_size=batch_size, shuffle=True)
val_loader = DataLoader(Water(val_df,val_label), batch_size=batch_size, shuffle=False)
test_loader = DataLoader(Water(test_df,test_label), batch_size=batch_size, shuffle=False)
return train_loader, val_loader, test_loader, n_sensor
class Water(Dataset):
def __init__(self, df, label, window_size=60, stride_size=10):
super(Water, self).__init__()
self.df = df
self.window_size = window_size
self.stride_size = stride_size
self.data, self.idx, self.label = self.preprocess(df,label)
def preprocess(self, df, label):
start_idx = np.arange(0,len(df)-self.window_size,self.stride_size)
end_idx = np.arange(self.window_size, len(df), self.stride_size)
delat_time = df.index[end_idx]-df.index[start_idx]
idx_mask = delat_time==pd.Timedelta(self.window_size,unit='s')
return df.values, start_idx[idx_mask], label[start_idx[idx_mask]]
def __len__(self):
length = len(self.idx)
return length
def __getitem__(self, index):
# N X K X L X D
start = self.idx[index]
end = start + self.window_size
data = self.data[start:end].reshape([self.window_size,-1, 1])
return torch.FloatTensor(data).transpose(0,1)
class WaterLabel(Dataset):
def __init__(self, df, label, window_size=60, stride_size=10):
super(WaterLabel, self).__init__()
self.df = df
self.window_size = window_size
self.stride_size = stride_size
self.data, self.idx, self.label = self.preprocess(df,label)
self.label = 1.0-2*self.label
def preprocess(self, df, label):
start_idx = np.arange(0,len(df)-self.window_size,self.stride_size)
end_idx = np.arange(self.window_size, len(df), self.stride_size)
delat_time = df.index[end_idx]-df.index[start_idx]
idx_mask = delat_time==pd.Timedelta(self.window_size,unit='s')
return df.values, start_idx[idx_mask], label[start_idx[idx_mask]]
def __len__(self):
length = len(self.idx)
return length
def __getitem__(self, index):
# N X K X L X D
start = self.idx[index]
end = start + self.window_size
data = self.data[start:end].reshape([self.window_size,-1, 1])
return torch.FloatTensor(data).transpose(0,1),self.label[index]