-
Notifications
You must be signed in to change notification settings - Fork 0
/
datasave.py
138 lines (100 loc) · 3.53 KB
/
datasave.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
# -*- coding: utf-8 -*-
import numpy as np
import torch
import os
import re
import scipy.io as scio
import scipy.signal
from torch.utils import data as da
from sklearn.preprocessing import LabelBinarizer
from sklearn.preprocessing import MinMaxScaler
from sklearn.model_selection import train_test_split
import h5py
import tables
raw_num = 100
time_series_length = 4096
class Data(object):
def __init__(self):
self.data = self.get_data()
self.label = self.get_label()
def file_list(self):
return os.listdir('../data/')
def get_data(self):
file_list = self.file_list()
x = np.empty((time_series_length, 0))
for i in range(len(file_list)):
# file = scio.loadmat('./data/{}'.format(file_list[i]))
# file = scio.loadmat('./data/{}'.format(file_list[i+1]))
# file = h5py.File('./data/{}'.format(file_list[i+1]),"r")
file = tables.open_file('../data/{}'.format(file_list[i]), mode="r")
print(i)
for k in file.root:
# print(k, "k")
file_matched = re.match('Data', k._v_name)
if file_matched:
key = file_matched.group()
# print(key)
# data1 = np.array(file[key][0:102400]) # 0:80624
# data1 = np.array(getattr(k.read(), 'Data')[0:102400])
data1 = np.array(k.read())[:, :(time_series_length * raw_num)].T
# data1 = np.expand_dims(data1, axis=1)
# print(data1.shape)
for j in range(0, len(data1) - (time_series_length - 1), time_series_length):
# print(data1[j:j + 1024].shape)
x = np.concatenate((x, data1[j:j + time_series_length, :]), axis=1)
# print(x.shape)
file.close()
return x.T
def get_label(self):
file_list = self.file_list()
title = np.array([i.replace('.mat', '') for i in file_list])
label = title[:, np.newaxis]
label_copy = np.copy(label)
for _ in range(raw_num - 1):
label = np.hstack((label, label_copy))
# print(label.shape)
return label.flatten()
Data = Data()
data = Data.data
label = Data.label
y = label.astype("int32")
ss = MinMaxScaler()
data = data.T
# print(data.shape)
# print(y.shape)
# print(data)
data = ss.fit_transform(data).T
data = data.reshape(2800, 4, time_series_length)
# print(data.shape, "data")
# print(y.shape, "y")
X_train, X_test, y_train, y_test = train_test_split(data, y, test_size=0.3, random_state=2, stratify=y)
# X_train = torch.from_numpy(X_train).unsqueeze(2)
X_train = torch.from_numpy(X_train)
# print(X_train.shape, "X_train")
# X_test = torch.from_numpy(X_test).unsqueeze(2)
X_test = torch.from_numpy(X_test)
# print(X_test.shape, "X_test")
class TrainDataset(da.Dataset):
def __init__(self):
self.Data = X_train
self.Label = y_train
def __getitem__(self, index):
txt = self.Data[index]
label = self.Label[index]
return txt, label
def __len__(self):
return len(self.Data)
class TestDataset(da.Dataset):
def __init__(self):
self.Data = X_test
self.Label = y_test
def __getitem__(self, index):
txt = self.Data[index]
label = self.Label[index]
return txt, label
def __len__(self):
return len(self.Data)
Train = TrainDataset()
Test = TestDataset()
train_loader = da.DataLoader(Train, batch_size=128, shuffle=True)
test_loader = da.DataLoader(Test, batch_size=10, shuffle=False)