-
Notifications
You must be signed in to change notification settings - Fork 0
/
droneML.py
400 lines (289 loc) · 14.3 KB
/
droneML.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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
import click
import numpy as np
import pandas as pd
import time
import datetime
import matplotlib
#matplotlib.use('jpeg')
import matplotlib.image as mpimg
import matplotlib.pyplot as plt
from kNNDTW import KnnDtw
from utils import load_labelled, load_test, get_distances, dtw_plots, label_dtws, load_csvs, get_windows, window_to_lists
from utils import save_data_object, load_data_object, load_labelled_csvs
from trainer import Trainer
from argparse import Namespace as ns
from dtw import dtw_distance
@click.group()
def cli():
pass
@cli.command('findbestk')
@click.option('--kmin', default=1, help='Minimum value for k Nearest Neighbours')
@click.option('--kmax', default=5, help='Maximum value for k Nearest Neighbours')
@click.option('-w', default=4000, help='Maximum Warping Window')
@click.option('--seed', default=108, help='Seed for random shuffle')
@click.option('--train', help='Training Data as CSV file path')
def find_best_k(kmin, kmax, w, seed, train):
click.echo('--- Find best k ---')
kmin = int(min(kmin, kmax))
kmax = int(max(kmin, kmax))
ks = range(kmin, kmax + 1)
train_data, train_label = load_labelled(train)
click.echo(' - ks : %s ' % str(ks))
click.echo(' - w : %d ' % w)
click.echo(' - seed : %d ' % seed)
click.echo(' - train : %s ' % train)
click.echo(' - Training data size: %d' % len(train_data))
click.echo('\nRunning...')
trainer = Trainer(seed=seed, data=train_data, data_labels=train_label)
trainer.find_best_k(ks, w)
click.echo('\nDone.')
@cli.command('findbestw')
@click.option('--wmin', default=100, help='Minimum value for Warping Window')
@click.option('--wmax', default=4000, help='Maximum value for Warping Window')
@click.option('--step', default=100, help='Step for Warping Window')
@click.option('-k', default=3, help='k Nearest Neighbours')
@click.option('--seed', default=108, help='Seed for random shuffle')
@click.option('--train', help='Training Data as CSV file path')
def find_best_w(wmin, wmax, step, k, seed, train):
click.echo('--- Find best w ---')
wmin = int(min(wmin, wmax))
wmax = int(max(wmin, wmax))
ws = range(wmin, wmax, step)
train_data, train_label = load_labelled(train)
click.echo(' - ws : %s ' % str(ws))
click.echo(' - k : %d ' % k)
click.echo(' - seed : %d ' % seed)
click.echo(' - train : %s ' % train)
click.echo(' - Training data size: %d' % len(train_data))
click.echo('\nRunning...')
trainer = Trainer(seed=seed, data=train_data, data_labels=train_label)
trainer.find_best_w(k, ws)
click.echo('\nDone.')
@cli.command('predict')
@click.option('-k', default=3, help='k Nearest Neighbours')
@click.option('-w', default=200, help='Maximum Warping Window')
@click.option('--train', help='Training Data as CSV file path')
@click.option('--test', help='Test Data as CSV file path')
def predict(k, w, train, test):
click.echo('--- Predicting a label ---')
#click.echo('Predicting with k=%d and w=%d.' % (k,w))
train_data, train_label = load_labelled(train)
test_data = load_test(test)
click.echo(' - k : %d ' % k)
click.echo(' - w : %d ' % w)
click.echo(' - train : %s ' % train)
click.echo(' - test : %s ' % test)
click.echo('\nRunning...')
model = KnnDtw(k_neighbours = k, max_warping_window = w)
model.fit(train_data, train_label)
predicted_label, probability = model.predict(test_data, parallel=False)
click.echo('\nPredicted label : %s ' % str(predicted_label))
click.echo('\nDone.')
@cli.command('dtw')
@click.option('--data', help='Single timeseries data as CSV file path')
@click.option('--dataarray', help='List of timeseries data as CSV file path')
@click.option('-w', default=200, help='Maximum Warping Window')
def compute_dtw(data, dataarray, w):
click.echo('--- Compute DTW ---')
timeseries, timeseries_label = load_labelled(dataarray)
timeserie_1 = load_test(data)
click.echo(' - data : %s ' % data)
click.echo(' - dataarray : %s ' % dataarray)
click.echo(' - w : %d ' % w)
click.echo('\nRunning...')
unsorted_dtws = get_distances(timeserie_1[0], data_array=timeseries, max_warping_window=w)
# Save plots
dtw_plots(unsorted_dtws)
click.echo('Done. Plots have been saved.')
click.echo('Choose a maximum number for labelling good and bad data based on DTW values.')
click.echo('Check the plots to take better decsision.')
click.echo(' Example: If value for Good is 150, all data with DTW 0-150 will be labelled "Good".')
# Enter limit for 'Good'
good_value = raw_input(' > Enter a value for "Good" (Ex: 150) : ')
# Enter limit for 'Bad'
bad_value = raw_input(' > Enter a value for "Bad" (Ex: 350) : ')
# Print and save results to CSV
fileName = raw_input(' > Enter a file name (add .csv at the end) : ')
label_dtws(unsorted_dtws, int(good_value), int(bad_value), fileName)
click.echo('\nDone.')
# Online anomaly detection
@cli.command('online')
@click.option('-k', default=3, help='k Nearest Neighbours')
@click.option('-w', default=200, help='Maximum Warping Window')
@click.option('--window-size', default=2, help='Maximum Warping Window')
@click.option('--train', help='Training Data as CSV file path')
@click.option('--test', help='Test Data as CSV file path')
def online_identification(k, w, window_size, train, test):
click.echo('\n--- Online flight degradation identification ---')
train_csv = load_csvs(train, should_preprocess=False)
test_csv = load_csvs(test, should_preprocess=False)
click.echo(' - k : %d ' % k)
click.echo(' - w : %d ' % w)
click.echo(' - train : %s ' % train)
click.echo(' - test : %s ' % test)
click.echo('\nRunning...\n')
# For Test
last_second = test_csv[0]['data']['seconds'].iloc[-1]
#print('[TEST] Last second: {}'.format(last_second))
for time_window in range(0, last_second, window_size):
click.echo('Time window is: %d ' % time_window)
train_window = get_windows(train_csv, time_window, time_window + window_size - 1)
test_window = get_windows(test_csv, time_window, time_window + window_size - 1)
train_data, train_label = window_to_lists(train_window, 'pose_position_z')
test_data, test_label = window_to_lists(test_window, 'pose_position_z')
model = KnnDtw(k_neighbours = k, max_warping_window = w)
model.fit(np.array(train_data), np.array(train_label))
#click.echo(train_label)
predicted_label, probability = model.predict(test_data, parallel=True)
click.echo('Predicted label : %s ' % str(predicted_label))
click.echo('\n')
click.echo('\nDone.')
@cli.command('findbestk_online')
@click.option('--kmin', default=1, help='Minimum value for k Nearest Neighbours')
@click.option('--kmax', default=5, help='Maximum value for k Nearest Neighbours')
@click.option('-w', default=4000, help='Maximum Warping Window')
@click.option('--window-size', default=2, help='Window size')
@click.option('--seed', default=108, help='Seed for random shuffle')
@click.option('--train', help='Training Data as CSV file path')
@click.option('--feature', help='The feature to use (e.g. pose_position_z)')
def find_best_k_online(kmin, kmax, w, window_size, seed, train, feature):
click.echo('--- Find best k ---')
kmin = int(min(kmin, kmax))
kmax = int(max(kmin, kmax))
ks = range(kmin, kmax + 1)
train_csv = load_labelled_csvs(train, should_preprocess=False) # [{labal;..., data},...]
#train_label = [flight['label'][0] for flight in train_csv] # Wrong, dont need
train_data = [flight['data'] for flight in train_csv]
#print(train_label)
click.echo(' - ks : %s ' % str(ks))
click.echo(' - w : %d ' % w)
click.echo(' - seed : %d ' % seed)
click.echo(' - train : %s ' % train)
click.echo(' - Training data size: %d' % len(train_data))
click.echo('\nRunning...')
trainer = Trainer(seed=seed, data=train_data, data_labels=None)
trainer.find_best_k_online(ks, w, window_size, feature)
click.echo('\nDone.')
@cli.command('findbestw_online')
@click.option('--wmin', default=100, help='Minimum value for Warping Window')
@click.option('--wmax', default=4000, help='Maximum value for Warping Window')
@click.option('--step', default=100, help='Step for Warping Window')
@click.option('-k', default=3, help='k Nearest Neighbours')
@click.option('--window-size', default=2, help='Window size')
@click.option('--seed', default=108, help='Seed for random shuffle')
@click.option('--train', help='Training Data as CSV file path')
@click.option('--feature', help='The feature to use (e.g. pose_position_z)')
def find_best_w_online(wmin, wmax, step, window_size, k, seed, train, feature):
click.echo('--- Find best w ---')
wmin = int(min(wmin, wmax))
wmax = int(max(wmin, wmax))
ws = range(wmin, wmax, step)
train_csv = load_labelled_csvs(train, should_preprocess=False)
train_data = [flight['data'] for flight in train_csv]
click.echo(' - ws : %s ' % str(ws))
click.echo(' - k : %d ' % k)
click.echo(' - seed : %d ' % seed)
click.echo(' - train : %s ' % train)
click.echo(' - Training data size: %d' % len(train_data))
click.echo('\nRunning...')
trainer = Trainer(seed=seed, data=train_data, data_labels=None)
trainer.find_best_w_online(k, ws, window_size, feature)
click.echo('\nDone.')
@cli.command('dtw_time_window')
@click.option('--data', help='Single timeseries data as CSV file path')
@click.option('--dataarray', help='List of timeseries data as CSV file path')
@click.option('-w', default=200, help='Maximum Warping Window')
@click.option('--window-size', default=2, help='Window size')
@click.option('--coordinate', default='pose_position_z', help='X/Y/Z coordinate')
@click.option('--should-plot', default=False)
def compute_labels(data, dataarray, w, window_size, coordinate, should_plot):
click.echo('--- Compute labels ---')
desired_csv = load_csvs(data, should_preprocess=False)
data_csv = load_csvs(dataarray, should_preprocess=False)
for index, flight_data_number in enumerate(data_csv):
flight_data_number['data']['flight_number'] = index
# For Test
last_second = desired_csv[0]['data']['seconds'].iloc[-1]
#click.echo(' - data : %s ' % data)
click.echo(' - dataarray : %s ' % dataarray)
click.echo(' - w : %d ' % w)
click.echo('\nRunning...')
# For plot-pdfs title
ts = time.time()
st = datetime.datetime.fromtimestamp(ts).strftime('%Y-%m-%d_%H%M%S')
flight_dict = {}
#flight_full_data = pd.DataFrame()
flight_dataframe = pd.DataFrame()
for time_window in range(0, last_second, window_size):
click.echo('Time window is: %d ' % time_window)
desired_window = get_windows(desired_csv, time_window, time_window + window_size - 1)
data_window = get_windows(data_csv, time_window, time_window + window_size - 1)
#print(data_window)
# Compute all DTWs
desired_flight = desired_window[0]['window'][coordinate].tolist()
dtws = []
for index, flight_window in enumerate(data_window):
# Compute DTW of window with desired_window ; window[coordinate].tolist()
pos_data = flight_window['window'][coordinate].tolist()
flight_seconds = flight_window['window']['seconds'].tolist()
dtw = dtw_distance(desired_flight, pos_data, w)
# Add key DTW to window ; window['dtw']
flight_window['dtw'] = dtw
# Track all DTWS ; append dtw value
dtws.append( dtw )
#click.echo('Done. Plots have been saved.')
print(dtw)
if should_plot:
# plotting desired data with each flight for each window
plt.plot(desired_flight, label='Desired')
plt.plot(pos_data, label='Unlabelled')
#plt.xlim(0,15)
plt.ylim(0,2.1)
plt.xlabel('Seconds')
plt.ylabel('Meters')
plt.title('Desired and unlabelled data. DTW = {}'.format(dtw))
plt.legend()
plt.grid(True)
plt.savefig('plots/z/desired_each_flight/dtwValues_{}_{}_{}.jpeg'.format(st, time_window, index))
plt.cla()
print(dtws)
if should_plot:
print(dtws)
dtws = [550 if x==9.223372036854776e+18 else x for x in dtws]
# plotting dtw values of all flights for each window
plt.figure(figsize=(20, 10))
plt.plot(dtws, color='#006600', alpha=.9) # marker='o', linestyle='--',
plt.ylim([0, 600])
flights_x = [i for i in range(0, 53)]
for flight_x, dtw in zip(flights_x, dtws):
plt.text(flight_x, dtw, str(dtw))
plt.grid(True)
#plt.show()
plt.savefig('plots/z/dtw_values/dtwValues_{}_{}.pdf'.format(st, time_window))
plt.cla()
click.echo('Done. Plots have been saved.')
# Loop again and update labels
for flight_window in data_window:
if flight_window['dtw'] < 100:
flight_window['label'] = 1
elif flight_window['dtw'] < 299:
flight_window['label'] = 2
else:
flight_window['label'] = 3
# Save to flight_dict
fn = flight_window['window']['flight_number'].iloc[0]
flight_full_data = flight_window['window']
flight_full_data.is_copy = False
flight_full_data['label'] = flight_window['label']
flight_full_data['dtw'] = flight_window['dtw']
if fn not in flight_dict.keys():
flight_dict[fn] = flight_full_data # flight_window['window']
else:
flight_dict[fn] = pd.concat([flight_dict[fn], flight_full_data]) # flight_window['window']
for flight_index in flight_dict:
flight_dataframe = pd.DataFrame(flight_dict[flight_index])
flight_dataframe.to_csv('data/labelled/z/f_{}.csv'.format(flight_index), sep=';', encoding='utf-8')
print('f_{}.csv saved!'.format(flight_index))
click.echo('\nDone.')
if __name__ == '__main__':
cli()