-
Notifications
You must be signed in to change notification settings - Fork 3
/
AnomalyData.cs
627 lines (544 loc) · 19.1 KB
/
AnomalyData.cs
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
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
using OxyPlot;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
namespace DesktopApp
{
public class AnomalyData : INotifyPropertyChanged
{
private TimeSeries _test; //the test TimeSeries fight file to run.
private readonly SimpleAnomalyDetector _detector; //the AnomalyDetector that learn the flight.
private List<DataPoint> _currentDataPoints; //list of the current column Points.
public List<DataPoint> _CurrentDataPoints
{
get => _currentDataPoints;
private set
{
_currentDataPoints = value;
NotifyPropertyChanged("_CurrentDataPoints");
}
}
private List<DataPoint> _correlatedDataPoints; //list of the most correlated column Points.
public List<DataPoint> _CorrelatedDataPoints
{
get => _correlatedDataPoints;
private set
{
_correlatedDataPoints = value;
NotifyPropertyChanged("_CorrelatedDataPoints");
}
}
private List<DataPoint> _multiDataPoints; //list of the current column and the most correlative column Points.
public List<DataPoint> _MultiDataPoints
{
get => _multiDataPoints;
private set
{
_multiDataPoints = value;
NotifyPropertyChanged("_MultiDataPoints");
}
}
private List<DataPoint>
_lastMultiDataPoints; //list of the 10 last current column and the most correlative column Points.
public List<DataPoint> _LastMultiDataPoints
{
get => _lastMultiDataPoints;
private set
{
_lastMultiDataPoints = value;
NotifyPropertyChanged("_LastMultiDataPoints");
}
}
private List<DataPoint> _linearReg; //list of 2 points declaring the regression line.
public List<DataPoint> _LinearReg
{
get => _linearReg;
private set
{
_linearReg = value;
NotifyPropertyChanged("_LinearReg");
}
}
private string _currColumn; //the current selected column.
public string _CurrColumn
{
get => _currColumn;
set
{
_currColumn = value;
_CorrelatedColumn = MostCorrelative(value);
UpdateCurrList();
UpdateMultiList();
UpdateLine();
if (_dllPath != "")
Draw();
NotifyPropertyChanged("_CurrColumn");
}
}
private string _correlatedColumn; //the current correlated column.
public string _CorrelatedColumn
{
get => _correlatedColumn;
set
{
_correlatedColumn = value;
UpdateCorrelatedList();
NotifyPropertyChanged("_CorrelatedColumn");
}
}
private int _currIndex; //the line number that currently being sent.
public int _NumLine
{
get => _currIndex;
set
{
_currIndex = value;
UpdateCurrList();
UpdateCorrelatedList();
UpdateMultiList();
UpdateLine();
UpdateJoystickCurrFeatures();
NotifyPropertyChanged("_NumLine");
}
}
private List<string> _allFeatures; //list of all the XML features.
public List<string> _AllFeatures
{
get => _allFeatures;
private set
{
_allFeatures = new List<string>(value);
NotifyPropertyChanged("_AllFeatures");
}
}
private float _minMulti; //the minimum X-value between all the _multiDataPoints.
private float _maxMulti; //the maximum X-value between all the _multiDataPoints.
public event PropertyChangedEventHandler PropertyChanged;
//Constructor
public AnomalyData(SimpleAnomalyDetector detector)
{
_detector = detector;
_currentDataPoints = new List<DataPoint>();
_currColumn = "";
_correlatedDataPoints = new List<DataPoint>();
_correlatedColumn = "";
_multiDataPoints = new List<DataPoint>();
_lastMultiDataPoints = new List<DataPoint>();
_linearReg = new List<DataPoint>();
_dllPath = "";
}
private void NotifyPropertyChanged(string propName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propName));
}
//puts all points up to the last point sent in _CurrentDataPoints.
private void UpdateCurrList()
{
List<DataPoint> tmp = new List<DataPoint>();
for (int i = 0; i <= _currIndex; i++)
{
tmp.Add(new DataPoint(i, GetTestAt(_currColumn, i)));
}
_CurrentDataPoints = tmp;
}
//puts all points up to the last point sent in _CorrelatedDataPoints.
private void UpdateCorrelatedList()
{
var tmp = new List<DataPoint>();
for (var i = 0; i < _currIndex; i++)
{
tmp.Add(new DataPoint(i, GetTestAt(_correlatedColumn, i)));
}
_CorrelatedDataPoints = tmp;
}
/*
* puts all points up to the last point sent in _MultiDataPoints.
* put the last 10 Points in _LastMultiDataPoints.
* initializes the minMulti and the maxMulti values.
*/
private void UpdateMultiList()
{
var tmp = new List<DataPoint>();
var tmp2 = new List<DataPoint>();
double minX = float.MaxValue;
double maxX = float.MinValue;
for (var i = Math.Max(0, _currIndex - 300); i < _currIndex; i++)
{
tmp.Add(new DataPoint(GetTestAt(_currColumn, i), GetTestAt(_correlatedColumn, i)));
if (GetTestAt(_currColumn, i) > maxX)
maxX = GetTestAt(_currColumn, i);
if (GetTestAt(_currColumn, i) < minX)
minX = GetTestAt(_currColumn, i);
}
for (var i = Math.Max(0, _currIndex - 10); i <= _currIndex; i++)
{
tmp2.Add(new DataPoint(GetTestAt(_currColumn, i), GetTestAt(_correlatedColumn, i)));
}
_minMulti = (float) minX;
_maxMulti = (float) maxX;
_MultiDataPoints = tmp;
_LastMultiDataPoints = tmp2;
}
//update the _LinearReg.
private void UpdateLine()
{
var tmp = new List<DataPoint>
{
new DataPoint(_minMulti, LinearReg(_currColumn).F(_minMulti)),
new DataPoint(_maxMulti, LinearReg(_currColumn).F(_maxMulti))
};
_LinearReg = tmp;
}
//learn the _test TimeSeries flight file.
public void Learn()
{
_detector.LearnNormal(_test);
if (_dllPath == "") return;
Start();
Draw();
}
//return the _test TimeSeries column value at index.
private float GetTestAt(string column, int index)
{
if (_test?.GetColumn(column) == null) return 0;
try
{
return _test.GetColumn(column)[index];
}
catch (Exception)
{
// ignored
}
return 0;
}
//return the most correlative feature to the column.
private string MostCorrelative(string column)
{
var cf = _detector.GetNormalModel();
foreach (var corr in cf.Where(corr => corr.Feature1 == column))
{
return corr.Feature2;
}
return "";
}
//return the regression line of the column (with the most correlative column).
private Line LinearReg(string column)
{
var cf = _detector.GetNormalModel();
foreach (var c in cf.Where(c => c.Feature1 == column))
{
return c.LinReg;
}
return new Line();
}
//return list of all the Points of the column (with the most correlative column).
public List<Point> GetAllPoints(string column)
{
var cf = _detector.GetNormalModel();
foreach (var c in cf.Where(c => c.Feature1 == column))
{
return c.AllPoints;
}
return new List<Point>();
}
//analyzes the XML file and initializes the _AllFeatures
public void AnalyzeXml(string path)
{
_AllFeatures = XmlAnalyzer.Analyzer(path);
}
//Joystick:
// Joystick features
private float _aileron;
private float _elevator;
private float _rudder;
private float _throttle;
private float _yaw;
private float _roll;
private float _pitch;
private float _airSpeed;
private float _alimeter;
private float _direction;
public float _Throttle
{
get => _throttle;
set
{
_throttle = value;
NotifyPropertyChanged("_Throttle");
}
}
public float _Rudder
{
get => _rudder;
set
{
_rudder = value;
NotifyPropertyChanged("_Rudder");
}
}
public float _Elevator
{
get => _elevator;
set
{
_elevator = value;
NotifyPropertyChanged("_Elevator");
}
}
public float _Aileron
{
get => _aileron;
set
{
_aileron = value;
NotifyPropertyChanged("_Aileron");
}
}
public float _AirSpeed
{
get => _airSpeed;
set
{
_airSpeed = value;
NotifyPropertyChanged("_AirSpeed");
}
}
public float _Roll
{
get => _roll;
set
{
_roll = value;
NotifyPropertyChanged("_Roll");
}
}
public float _Pitch
{
get => _pitch;
set
{
_pitch = value;
NotifyPropertyChanged("_Pitch");
}
}
public float _Alimeter
{
get => _alimeter;
set
{
_alimeter = value;
NotifyPropertyChanged("_Alimeter");
}
}
public float _Yaw
{
get => _yaw;
set
{
_yaw = value;
NotifyPropertyChanged("_Yaw");
}
}
public float _Direction
{
get => this._direction;
set
{
this._direction = value;
NotifyPropertyChanged("_Direction");
}
}
// lists for Joystick feature
private List<float> _aileronList;
private List<float> _elevatorList;
private List<float> _rudderList;
private List<float> _throttleList;
private List<float> _yawList;
private List<float> _rollList;
private List<float> _pitchList;
private List<float> _airSpeedList;
private List<float> _alimeterList;
private void UpdateJoystickCurrFeatures()
{
try
{
if (_aileronList != null)
_Aileron = _aileronList[_NumLine];
else
{
_Aileron = 125;
}
if (_elevatorList != null)
_Elevator = _elevatorList[_NumLine];
else
{
_Elevator = 125;
}
if (_rudderList != null)
_Rudder = _rudderList[_NumLine];
else
{
_Rudder = -1;
}
if (_throttleList != null)
_Throttle = _throttleList[_NumLine];
else
{
_Throttle = 0;
}
if (_airSpeedList != null)
_AirSpeed = _airSpeedList[_NumLine];
else
{
_AirSpeed = 0;
}
if (_yawList != null)
{
_Yaw = _yawList[_NumLine];
_Direction = _yawList[_NumLine];
}
else
{
_Yaw = 0;
_Direction = 0;
}
if (_pitchList != null)
_Pitch = _pitchList[_NumLine];
else
{
_Pitch = 0;
}
if (_rollList != null)
_Roll = _rollList[_NumLine];
else
{
_Roll = 0;
}
if (_alimeterList != null)
_Alimeter = _alimeterList[_NumLine];
else
{
_Alimeter = 0;
}
}
catch (Exception)
{
// ignored
}
}
private void UpdateJoystickFeatures()
{
_aileronList = GetCol("aileron");
_elevatorList = GetCol("elevator");
_rudderList = GetCol("rudder");
_throttleList = GetCol("throttle1");
_airSpeedList = GetCol("airspeed-indicator_indicated-speed-kt");
_alimeterList = GetCol("altimeter_indicated-altitude-ft");
//_directionList = GetCol("heading-deg");
_yawList = GetCol("heading-deg");
_pitchList = GetCol("pitch-deg");
_rollList = GetCol("roll-deg");
}
private List<float> GetCol(string s)
{
return _test?.GetColumn(s);
}
//DLL part:
private string _dllPath; //the dll path.
private string _trainFile; //the train csv file.
private string _testFile; //the test csv file.
private object _dllAnomalyManager; //the DLL's AnomalyManager Object.
private MethodInfo _shapeMethod; //the method that returns the PlotModel
private MethodInfo _corrMethod; //the method that returns the most correlative according to the DLL.
private MethodInfo _anomaliesMethod; //the method that returns List of the anomalies.
private string _dllTitle; //the Title above the DLL's graphs.
public string _DllTitle
{
get => _dllTitle;
private set
{
_dllTitle = value;
NotifyPropertyChanged("_DllTitle");
}
}
private PlotModel _dllPlotModel; //the DLL's PlotModel.
public PlotModel _DllPlotModel
{
get => _dllPlotModel;
private set
{
_dllPlotModel = value;
NotifyPropertyChanged("_DllPlotModel");
}
}
private List<int> _dllAnomaliesLines; //list of the lines of the anomalies.
private List<string> _dllAnomaliesList; //list of the strings of the anomalies.
public List<string> _DllAnomaliesList
{
get => _dllAnomaliesList;
private set
{
_dllAnomaliesList = value;
NotifyPropertyChanged("_DllAnomaliesList");
}
}
//set the DLL's path.
public void PathSetter(string path)
{
_dllPath = path;
}
//upload the train file.
public void UploadTrain(string file)
{
_trainFile = file;
}
//upload the test file and create the TimeSeries test.
public void UploadTest(string file)
{
_test = new TimeSeries(file);
_testFile = file;
UpdateJoystickFeatures();
}
//load th DLL, learn and detect the train and test files.
public void Start()
{
var assembly = Assembly.LoadFile(_dllPath);
var @namespace = _dllPath.Split('\\')[^1].Split('.')[0];
var type = assembly.GetType(@namespace + ".AnomalyManager");
_dllAnomalyManager = Activator.CreateInstance(type!);
var uploadTrain = type.GetMethod("UploadTrain");
uploadTrain?.Invoke(_dllAnomalyManager, new object[] {_trainFile});
var uploadTest = type.GetMethod("UploadTest");
uploadTest?.Invoke(_dllAnomalyManager, new object[] {_testFile});
var learn = type.GetMethod("Learn");
learn?.Invoke(_dllAnomalyManager, new object[] { });
var detect = type.GetMethod("Detect");
detect?.Invoke(_dllAnomalyManager, new object[] { });
_shapeMethod = type.GetMethod("GetShape");
_corrMethod = type.GetMethod("GetCorrelated");
_anomaliesMethod = type.GetMethod("GetAnomalies");
}
//set the _DllPlotModel, the _DllTitle and the _DllAnomaliesList.
public void Draw()
{
//PlotModel tmp = (PlotModel)_shapeMethod.Invoke(_detector, new object[] { _currColumn });
_DllPlotModel = null;
GC.Collect();
_DllPlotModel = (PlotModel) _shapeMethod.Invoke(_dllAnomalyManager, new object[] {_currColumn});
_DllTitle = "\"" + _currColumn + "\" is most correlative to: \"" +
(string) _corrMethod.Invoke(_dllAnomalyManager, new object[] {_currColumn}) +
"\"\n according to the train flight";
var tuple = ((Tuple<List<string>, List<int>>) _anomaliesMethod.Invoke(_dllAnomalyManager,
new object[] {_currColumn}));
_DllAnomaliesList = tuple?.Item1;
_dllAnomaliesLines = tuple?.Item2;
}
//return the line of the anomaly string.
public int GetTimeStepFromString(string anomaly)
{
return _dllAnomaliesLines[_dllAnomaliesList.IndexOf(anomaly)];
}
}
}