forked from LukasKG/SHL_GAN
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdata_source.py
194 lines (160 loc) · 6.33 KB
/
data_source.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# -*- coding: utf-8 -*-
import pandas as pd
import numpy as np
import os
from log import log
from sliding_window import extract_features, FXdict
DATA_FILES = [
'Acc_x.txt','Acc_y.txt','Acc_z.txt',
'Gra_x.txt','Gra_y.txt','Gra_z.txt',
'Gyr_x.txt','Gyr_y.txt','Gyr_z.txt',
'LAcc_x.txt','LAcc_y.txt','LAcc_z.txt',
'Mag_x.txt','Mag_y.txt','Mag_z.txt',
'Ori_w.txt','Ori_x.txt','Ori_y.txt','Ori_z.txt',
'Pressure.txt'
]
LABELS_SHL = {
1: "Still",
2: "Walking",
3: "Run",
4: "Bike",
5: "Car",
6: "Bus",
7: "Train",
8: "Subway",
}
PATHS = {
'Challenge_test': 'D:\\data\\SHL_Challenge_2020\\test\\',
'Challenge_train': 'D:\\data\\SHL_Challenge_2020\\train\\',
'Challenge_validation': 'D:\\data\\SHL_Challenge_2020\\validation\\',
'Data_test': 'D:\\data\\SHL_2020_prepared\\test\\',
'Data_train': 'D:\\data\\SHL_2020_prepared\\train\\',
'Data_validation': 'D:\\data\\SHL_2020_prepared\\validation\\',
}
FEATURES = {
'basic': ['mean','std','mcr','kurtosis','skew'],
'all': FXdict.keys(),
}
def load_data(params,dataset):
'''
Checks if the selected dataset-location combination is already extracted.
If not, the according data is loaded, features extracted, and the result stored.
Then the selected data and - if available - according labels are loaded and returned.
Parameters
----------
dataset : name of the dataset
location : location of the sensor
FX_sel : selection of features
Returns
-------
Data X
Labels Y (optional, otherwise None)
'''
if dataset == 'test':
location = 'test'
else:
location = params['location']
FX_sel = params['FX_sel']
assert dataset in ['test','train','validation']
assert location in ['bag','hand','hips','torso','all','test']
assert (dataset == 'test') == (location == 'test')
assert FX_sel in ['basic','all']
log("Loading dataset %s.. (Location: %s | FX: %s)"%(dataset,location,FX_sel),name=params['log_name'])
if location == 'all':
params_tmp = params.copy()
params_tmp['location'] = 'bag'
X1, Y1 = load_data(params_tmp,dataset)
params_tmp['location'] = 'hand'
X2, Y2 = load_data(params_tmp,dataset)
params_tmp['location'] = 'hips'
X3, Y3 = load_data(params_tmp,dataset)
params_tmp['location'] = 'torso'
X4, Y4 = load_data(params_tmp,dataset)
data = np.concatenate((X1, X2, X3, X4),axis=0)
label = np.concatenate((Y1, Y2, Y3, Y4),axis=0)
else:
path = PATHS['Data_'+dataset] + location + '\\' + FX_sel + '\\'
if not os.path.isfile(path+'data.txt'):
log("Generating dataset..",name=params['log_name'])
generate_data(dataset,location,FX_sel)
data = pd.read_csv(path+'data.txt',header=None).to_numpy()
if os.path.isfile(path+'label.txt'):
label = pd.read_csv(path+'label.txt',header=None).to_numpy()
else:
label = None
log("Dataset %s (%s) loaded."%(dataset,location),name=params['log_name'])
return data, label
def read_data(dataset,location,channel_selection=range(len(DATA_FILES))):
src_path = PATHS['Challenge_'+dataset] + location + '\\'
stack = []
for i,filename in enumerate(DATA_FILES):
if i in channel_selection:
X = pd.read_csv(src_path+filename,sep=' ',header=None).to_numpy()
stack.append(X)
return stack
def generate_data(dataset,location,FX_sel):
src_path = PATHS['Challenge_'+dataset] + location + '\\'
tar_path = PATHS['Data_'+dataset] + location + '\\' + FX_sel + '\\'
os.makedirs(tar_path, exist_ok=True)
stack = read_data(dataset,location)
data = extract_features(stack,FEATURES[FX_sel])
df = pd.DataFrame(data=data, index=None, columns=None)
df.to_csv(tar_path+'data.txt',header=False,index=False)
if os.path.isfile(src_path+'Label.txt'):
labels = pd.read_csv(src_path+'Label.txt',sep=' ',header=None).to_numpy()
labels = labels[:,0]
df = pd.DataFrame(data=labels, index=None, columns=None)
df.to_csv(tar_path+'label.txt',header=False,index=False)
def read_prediction(params,src_path):
if os.path.isfile(src_path):
return pd.read_csv(src_path,sep=' ',header=None).to_numpy()[:,0]
else:
log("Can't find predictions for model %s."%(params['name']),name=params['log_name'])
return None
def load_FX(FX_sel):
from GAN import get_params
params = get_params(FX_sel=FX_sel)
params['location'] = 'all'
data, label = load_data(params,'validation')
print('Loaded!')
print(" Data:",data.shape)
print(" NaN:",np.count_nonzero(np.isnan(data)))
print("Label:",label.shape)
print("----------------------")
params['location'] = 'bag'
data, label = load_data(params,'train')
print('Loaded!')
print(" Data:",data.shape)
print(" NaN:",np.count_nonzero(np.isnan(data)))
print("Label:",label.shape)
print("----------------------")
params['location'] = 'hand'
data, label = load_data(params,'train')
print('Loaded!')
print(" Data:",data.shape)
print(" NaN:",np.count_nonzero(np.isnan(data)))
print("Label:",label.shape)
print("----------------------")
params['location'] = 'hips'
data, label = load_data(params,'train')
print('Loaded!')
print(" Data:",data.shape)
print(" NaN:",np.count_nonzero(np.isnan(data)))
print("Label:",label.shape)
print("----------------------")
params['location'] = 'torso'
data, label = load_data(params,'train')
print('Loaded!')
print(" Data:",data.shape)
print(" NaN:",np.count_nonzero(np.isnan(data)))
print("Label:",label.shape)
print("----------------------")
params['location'] = 'test'
data, label = load_data(params,'test')
print('Loaded!')
print(" Data:",data.shape)
print(" NaN:",np.count_nonzero(np.isnan(data)))
print("----------------------")
if __name__ == "__main__":
load_FX('basic')
load_FX('all')