-
Notifications
You must be signed in to change notification settings - Fork 5
/
sample_datasets.py
244 lines (222 loc) · 9.68 KB
/
sample_datasets.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
import argparse
import os
import matplotlib.pyplot as plt
import pandas as pd
from scipy.signal import resample
from datasets.mobi_act_data import (MOBI_ACT_COLUMNS_TO_IGNORE,
SCENARIOS_TO_IGNORE, MobiActDataset,
MobiActInstance)
from datasets.motion_sense_data import (MOTION_SENSE_COLUMNS_TO_IGNORE,
MotionSenseDataset,
MotionSenseInstance)
from datasets.pamap_data import PamapDataset, PamapInstance
from datasets.ucihar_data import (UCI_ACTIVITIES_TO_IGNORE,
SmartphoneRawDataset, SmartphoneRawInstance)
from datasets.uschad_data import USCDataset, USCInstance
DATASETS_DICT = {
'uci_smartphones': {
'dataset_class': SmartphoneRawDataset,
'instance_class': SmartphoneRawInstance,
'type': 'raw',
'frequency': 50},
'pamap2': {
'dataset_class': PamapDataset,
'instance_class': PamapInstance,
'type': 'raw',
'frequency': 100,
'optional_labels': [0, 9, 10, 11, 18, 19, 20]},
'usc_had': {
'dataset_class': USCDataset,
'instance_class': USCInstance,
'type': 'predefined',
'frequency': 100},
'motion_sense': {
'dataset_class': MotionSenseDataset,
'instance_class': MotionSenseInstance,
'type': 'predefined',
'frequency': 50},
'mobi_act': {
'dataset_class': MobiActDataset,
'instance_class': MobiActInstance,
'type': 'raw',
'frequency': 200}
}
def downsample_dataframe(df, downsample_freq, sample_dur):
downsampling_factor = int(downsample_freq * sample_dur)
arr = df.values
new_values = resample(arr, downsampling_factor)
return pd.DataFrame(new_values, columns=df.columns)
def sample_raw_dataframe(data_instance, sample_dur, overlap_pct, frequency, ignore_labels=[], downsample_freq=None, num_frames=None):
""" Function for sampling raw signals
Parameters
----------
data_instance : DatasetInstance
instance of the dataset to be sampled
sample_dur : int
duration of the output frame in seconds
overlap_pct : float
overlap percentage between frames
frequency : int
sensors' sampling frequency in Hz
"""
df_list = []
df = data_instance.data
df['timestep'] = df['timestep'].apply(lambda x: round(x, 4))
if not num_frames:
num_frames = int(frequency * sample_dur)
for _, row in data_instance.labels_summary.iterrows():
tmp_label = row['label']
if int(tmp_label) not in ignore_labels:
try:
tmp_start = max(int(df.loc[df['timestep'] == round(row['start_timestep'], 4)].index.to_list()[0] - num_frames / 2), 0)
except:
print(row['start_timestep'])
print('start_passed')
try:
end = min(int(df.loc[df['timestep'] == round(row['end_timestep'], 4)].index.to_list()[0] + num_frames / 2), max(df.index))
except:
print('end passed')
print(row['end_timestep'])
while end - tmp_start >= num_frames:
tmp_end = int(tmp_start + num_frames)
tmp_df = df.iloc[tmp_start: tmp_end]
if downsample_freq:
tmp_df = downsample_dataframe(tmp_df, downsample_freq, sample_dur)
tmp_df = tmp_df.fillna(0.0)
df_list.append((tmp_df.iloc[:, 1:], int(tmp_label)))
tmp_start += int((1 - overlap_pct) * num_frames)
return df_list
def sample_predefined_dataframe(data_instance, sample_dur, overlap_pct, frequency, downsample_freq=None, num_frames=None):
""" Function for sampling samples with a single activity
Parameters
----------
data_instance : DatasetInstance
instance of the dataset to be sampled
sample_dur : int
duration of the output frame in seconds
overlap_pct : float
overlap percentage between frames
frequency : float
initial frequency
downsample_freq : float
desired frequency after downsampling
"""
df = data_instance.data
label = data_instance.label
tmp_start = df.index[0]
end = df.index[-1]
df_list = []
if not num_frames:
num_frames = int(sample_dur * frequency)
while end - tmp_start >= num_frames:
tmp_end = tmp_start + num_frames
tmp_df = df.iloc[tmp_start: tmp_end]
if downsample_freq:
tmp_df = downsample_dataframe(tmp_df, downsample_freq, sample_dur)
tmp_df = tmp_df.fillna(0.0)
df_list.append((tmp_df, label))
tmp_start += int((1 - overlap_pct) * num_frames)
return df_list
def parse_arguments():
parser = argparse.ArgumentParser()
parser.add_argument("--datasets", help="specify datasets to be sampled", required=True, nargs="+")
parser.add_argument("--paths", help="path to the datasets", required=True, nargs="+")
parser.add_argument("--destination", help="path to store sampled datasets", default='./sampled_data/')
# parser.add_argument("--opportunity_locomotion", help='specify opportunity mode', action="store_true", default=False)
parser.add_argument("--num_frames", help='number of frames in the time-window', type=int, default=None)
parser.add_argument("--sample_dur", help='sample duration in seconds', type=float, default=1)
parser.add_argument("--overlap_pct", help='overlap percentage between samples', type=float, default=0.5)
parser.add_argument("--pamap_include_optional", help='include optional labels', action='store_true')
parser.add_argument("--downsample_freq", help='frequency for downsampling', default=None)
parser.add_argument("--pamap_columns_ignore", help='substrings of columns to ignore pamap', default=[], nargs='+')
args = parser.parse_args()
return args
def main():
args = parse_arguments()
if not args.pamap_include_optional:
ignore_labels = DATASETS_DICT['pamap2']['optional_labels']
else:
ignore_labels = []
for i, dataset in enumerate(args.datasets):
print('Sampling {} dataset..'.format(dataset))
tmp_dataset = DATASETS_DICT[dataset]['dataset_class'](args.paths[i])
# if dataset == 'opportunity':
# if not args.opportunity_locomotion:
# dataset_destination_path = os.path.join(args.destination, dataset + '_gesture')
# mode = 'gesture'
# else:
# dataset_destination_path = os.path.join(args.destination, dataset + '_locomotion')
# mode = 'locomotion'
# else:
dataset_destination_path = os.path.join(args.destination, dataset)
if not os.path.exists(dataset_destination_path):
os.makedirs(dataset_destination_path)
if DATASETS_DICT[dataset]['type'] == 'raw':
users = set(tmp_dataset.datafiles_dict.keys())
exps = []
for user in users:
exps.extend(tmp_dataset.datafiles_dict[user].keys())
exps = set(exps)
for user in users:
for exp in exps:
try:
if dataset == 'mobi_act':
scenarios = [scenario for scenario in os.listdir(args.paths[i]) if scenario not in SCENARIOS_TO_IGNORE]
for scenario in scenarios:
# print(user, exp)
files = tmp_dataset.get_files(user, exp, scenario)
if not files:
continue
tmp_instance = DATASETS_DICT[dataset]['instance_class'](files, columns_to_ignore=MOBI_ACT_COLUMNS_TO_IGNORE)
tmp_sample_list = sample_raw_dataframe(tmp_instance, args.sample_dur, args.overlap_pct, DATASETS_DICT[dataset]['frequency'], num_frames=args.num_frames, ignore_labels=[-1], downsample_freq=args.downsample_freq)
user_name = 'subject' + ''.join(str(user))
for j, sample in enumerate(tmp_sample_list):
sample[0].to_csv(os.path.join(dataset_destination_path, '{}_{}_a{}_{}.csv'.format(user_name, exp, sample[1], j)), index=None)
else:
files = tmp_dataset.get_files(user, exp)
# if dataset == 'opportunity':
# tmp_instance = DATASETS_DICT[dataset]['instance_class'](files, mode=mode, include_columns=OPP_COLUMNS)
# elif dataset == 'pamap2':
# tmp_instance = DATASETS_DICT[dataset]['instance_class'](files, substring_eliminate=args.pamap_columns_ignore)
# else:
tmp_instance = DATASETS_DICT[dataset]['instance_class'](files)
if dataset == 'uci_smartphones':
ignore_labels = UCI_ACTIVITIES_TO_IGNORE
elif dataset not in ['pamap2', 'daphnet']:
ignore_labels = []
tmp_sample_list = sample_raw_dataframe(tmp_instance, args.sample_dur, args.overlap_pct, DATASETS_DICT[dataset]['frequency'], ignore_labels=ignore_labels, num_frames=args.num_frames, downsample_freq=args.downsample_freq)
user_name = 'subject' + ''.join(list(filter(str.isdigit, user)))
for j, sample in enumerate(tmp_sample_list):
sample[0].to_csv(os.path.join(dataset_destination_path, '{}_{}_a{}_{}.csv'.format(user_name, exp, sample[1], j)), index=None)
except KeyError:
pass
elif DATASETS_DICT[dataset]['type'] == 'predefined':
users = set(tmp_dataset.datafiles_dict.keys())
actions = []
for user in users:
actions.extend(tmp_dataset.datafiles_dict[user].keys())
actions = set(actions)
exps = []
for user in users:
for action in actions:
exps.extend(tmp_dataset.datafiles_dict[user][action].keys())
exps = set(exps)
for user in users:
for action in actions:
for exp in exps:
try:
files = tmp_dataset.get_file(user, action, exp)
if dataset == 'motion_sense':
tmp_instance = DATASETS_DICT[dataset]['instance_class'](files, columns_to_ignore=MOTION_SENSE_COLUMNS_TO_IGNORE)
else:
tmp_instance = DATASETS_DICT[dataset]['instance_class'](files)
tmp_sample_list = sample_predefined_dataframe(tmp_instance, args.sample_dur, args.overlap_pct, DATASETS_DICT[dataset]['frequency'], num_frames=args.num_frames, downsample_freq=args.downsample_freq)
user_name = 'subject' + ''.join(list(filter(str.isdigit, user)))
for i, sample in enumerate(tmp_sample_list):
if str(action)[0] != 'a':
action = 'a' + str(action)
sample[0].to_csv(os.path.join(dataset_destination_path, '{}_{}_{}_{}.csv'.format(user_name, exp, action, i)), index=None)
except KeyError:
pass
if __name__ == '__main__':
main()