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
|
using Microsoft.ML;
using Microsoft.ML.Transforms.TimeSeries;
using SciChart.Charting.Model.ChartSeries;
using SciChart.Charting.Model.DataSeries;
using SciChart.Charting.Visuals.Annotations;
using SciChart.Charting.Visuals.RenderableSeries;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Media;
using Tango.BL.Enumerations;
using Tango.Core;
using Tango.FSE.Insights.ML;
namespace Tango.FSE.Insights.SciChart
{
public class InsightsChart : ExtendedObject
{
public event EventHandler IsVisibleChanged;
private ObservableCollection<InsightsChart> ChartsCollection { get; set; }
public String Name { get; set; }
public String Description { get; set; }
public TechMonitors TechMonitor { get; set; }
public PropertyInfo InsightsMonitorsProperty { get; set; }
public IRenderableSeriesViewModel RenderableSeries { get; set; }
public XyDataSeries<DateTime, double> DataSeries { get; set; }
public List<MonitorValue> MonitorValues { get; set; }
public AnnotationCollection Annotations { get; set; }
public List<DetectedAnnomaly> Anomalies { get; set; }
private bool _isVisible;
public bool IsVisible
{
get { return _isVisible; }
set
{
if (_isVisible != value)
{
_isVisible = value;
RaisePropertyChangedAuto();
IsVisibleChanged?.Invoke(this, new EventArgs());
}
}
}
public bool IsLast
{
get
{
return ChartsCollection.Last() == this;
}
}
public InsightsChart(ObservableCollection<InsightsChart> chartsCollection)
{
Anomalies = new List<DetectedAnnomaly>();
MonitorValues = new List<MonitorValue>();
ChartsCollection = chartsCollection;
ChartsCollection.CollectionChanged += ChartsCollection_CollectionChanged;
RenderableSeries = new LineRenderableSeriesViewModel() { DrawNaNAs = LineDrawMode.Gaps };
RenderableSeries.Stroke = Colors.DodgerBlue;
DataSeries = new XyDataSeries<DateTime, double>();
RenderableSeries.DataSeries = DataSeries;
Annotations = new AnnotationCollection();
}
private void ChartsCollection_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
{
RaisePropertyChanged(nameof(IsLast));
}
public List<DetectedAnnomaly> DetectAnnomalies(Action<int> progress = null)
{
List<DetectedAnnomaly> annomalies = new List<DetectedAnnomaly>();
if (MonitorValues.Count > 0 && MonitorValues.Any(x => x.Value != 0))
{
var chunks = MonitorValues.ChunkBy(1000);
double lastValue = 0;
int totalIndex = 0;
foreach (var chunk in chunks)
{
MLContext mlContext = new MLContext();
IDataView view = mlContext.Data.LoadFromEnumerable<MonitorValue>(chunk);
var iidSpikeEstimator = mlContext.Transforms.DetectIidSpike(outputColumnName: nameof(MonitorSeriesPrediction.Prediction), inputColumnName: nameof(MonitorValue.Value), confidence: 95, pvalueHistoryLength: chunk.Count / 4, side: AnomalySide.Positive);
ITransformer iidSpikeTransform = iidSpikeEstimator.Fit(CreateEmptyDataView(mlContext));
IDataView transformedData = iidSpikeTransform.Transform(view);
var predictions = mlContext.Data.CreateEnumerable<MonitorSeriesPrediction>(transformedData, reuseRowObject: false);
int index = 0;
foreach (var p in predictions)
{
if (p.Prediction[2] < (1 - 0.95))
{
p.Prediction[0] = 1;
}
if (p.Prediction[0] == 1)
{
var monitorValue = chunk[index];
if (lastValue != monitorValue.Value)
{
annomalies.Add(new DetectedAnnomaly()
{
Type = AnomalyType.Spike,
Time = monitorValue.Time,
Value = monitorValue.Value,
Score = p.Prediction[1],
});
lastValue = monitorValue.Value;
}
}
index++;
totalIndex++;
progress?.Invoke(totalIndex);
}
}
annomalies = annomalies.OrderByDescending(x => x.Score).Take(10).OrderBy(x => x.Time).ToList();
}
progress?.Invoke(MonitorValues.Count);
Anomalies = annomalies.ToList();
return annomalies;
}
private IDataView CreateEmptyDataView(MLContext context)
{
IEnumerable<MonitorValue> enumerableData = new List<MonitorValue>();
return context.Data.LoadFromEnumerable(enumerableData);
}
public void DrawAnomalies()
{
foreach (var anomaly in Anomalies)
{
CustomAnnotation annotation = new CustomAnnotation();
annotation.CoordinateMode = AnnotationCoordinateMode.Absolute;
annotation.X1 = anomaly.Time.ToLocalTime();
annotation.Y1 = anomaly.Value;
annotation.Content = anomaly;
annotation.ContentTemplate = Application.Current.Resources["InsightAnomalyTemplate"] as DataTemplate;
Annotations.Add(annotation);
}
}
}
}
|