-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Expand file tree
/
Copy pathForm1.cs
More file actions
238 lines (190 loc) · 8.68 KB
/
Form1.cs
File metadata and controls
238 lines (190 loc) · 8.68 KB
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
using Microsoft.ML;
using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Windows.Forms;
using System.Windows.Forms.DataVisualization.Charting;
namespace SpikeDetection.WinForms
{
public partial class Form1 : Form
{
private DataTable dataTable = null;
private string filePath = "";
Tuple<string, string> tup = null;
Dictionary<int, Tuple<string, string>> dict = new Dictionary<int, Tuple<string, string>>();
private static string BaseModelsRelativePath = @"../../../MLModels";
private static string ModelRelativePath1 = $"{BaseModelsRelativePath}/ProductSalesSpikeModel.zip";
private static string ModelRelativePath2 = $"{BaseModelsRelativePath}/ProductSalesChangePointModel.zip";
private static string spikeModelPath = GetAbsolutePath(ModelRelativePath1);
private static string changePointModelPath = GetAbsolutePath(ModelRelativePath2);
public Form1()
{
InitializeComponent();
}
// Find file button.
private void button1_Click(object sender, EventArgs e)
{
// Open File Explorer.
DialogResult result = openFileExplorer.ShowDialog();
// Set text in file path textbox to file path from file explorer.
if (result == DialogResult.OK)
{
filePathTextbox.Text = openFileExplorer.FileName;
}
}
// Go button.
private void button2_Click(object sender, EventArgs e)
{
// Set filepath from text from filepath textbox.
filePath = filePathTextbox.Text;
// Check if file exists.
if (File.Exists(filePath))
{
dict = new Dictionary<int, Tuple<string, string>>();
if (filePath != "")
{
// Reset text in anomaly textbox.
anomalyText.Text = "";
// Display preview of dataset and graph.
displayDataTableAndGraph();
// Load a trained model to detect anomalies and then mark them on the graph.
detectAnomalies();
}
// If file path textbox is empty, prompt user to input file path.
else
{
MessageBox.Show("Please input file path.");
}
}
else
{
MessageBox.Show("File does not exist. Try finding the file again.");
}
}
private void displayDataTableAndGraph()
{
dataTable = new DataTable();
string[] dataCol = null;
int a = 0;
string xAxis = "";
string yAxis = "";
string[] dataset = File.ReadAllLines(filePath);
dataCol = commaSeparatedRadio.Checked ? dataset[0].Split(',') : dataset[0].Split('\t');
dataTable.Columns.Add(dataCol[0]);
dataTable.Columns.Add(dataCol[1]);
xAxis = dataCol[0];
yAxis = dataCol[1];
foreach (string line in dataset.Skip(1))
{
// Add next row of data.
dataCol = commaSeparatedRadio.Checked ? line.Split(',') : line.Split('\t');
dataTable.Rows.Add(dataCol);
tup = new Tuple<string, string>(dataCol[0], dataCol[1]);
dict.Add(a, tup);
a++;
}
// Set data view preview source.
dataGridView1.DataSource = dataTable;
// Update y axis min and max values.
double yMax = Convert.ToDouble(dataTable.Compute($"max([{yAxis}])", string.Empty));
//double yMin = Convert.ToDouble(dataTable.Compute($"min([{yAxis}])", string.Empty));
double yMin = Convert.ToDouble(0);
// Set graph source.
graph.DataSource = dataTable;
// Set graph options.
graph.Series["Series1"].ChartType = SeriesChartType.Line;
graph.Series["Series1"].XValueMember = xAxis;
graph.Series["Series1"].YValueMembers = yAxis;
graph.Legends["Legend1"].Enabled = true;
graph.ChartAreas["ChartArea1"].AxisX.MajorGrid.LineWidth = 0;
graph.ChartAreas["ChartArea1"].AxisX.Interval = a / 10;
graph.ChartAreas["ChartArea1"].AxisY.Maximum = yMax;
graph.ChartAreas["ChartArea1"].AxisY.Minimum = yMin;
graph.ChartAreas["ChartArea1"].AxisY.Interval = yMax / 10;
graph.DataBind();
}
private void detectAnomalies()
{
// Create MLContext to be shared across the model creation workflow objects.
var mlcontext = new MLContext();
// STEP 1: Load the data into IDataView.
IDataView dataView = mlcontext.Data.LoadFromTextFile<ProductSalesData>(path: filePath, hasHeader: true, separatorChar: commaSeparatedRadio.Checked ? ',' : '\t');
// Step 2: Load & use model.
// Note -- The model is trained with the product-sales dataset in a separate console app (see AnomalyDetectionConsoleApp).
if (spikeDet.Checked)
{
if (File.Exists(spikeModelPath))
{
loadAndUseModel(mlcontext, dataView, spikeModelPath, "Spike", Color.DarkRed);
}
else
{
MessageBox.Show("Spike detection model does not exist. Please run model training console app first.");
}
}
if (changePointDet.Checked)
{
if (File.Exists(changePointModelPath))
{
loadAndUseModel(mlcontext, dataView, changePointModelPath, "Change point", Color.DarkBlue);
}
else
{
MessageBox.Show("Change point detection model does not exist. Please run model training console app first.");
}
}
}
public static string GetAbsolutePath(string relativePath)
{
var _dataRoot = new FileInfo(typeof(Program).Assembly.Location);
string assemblyFolderPath = _dataRoot.Directory.FullName;
string fullPath = Path.Combine(assemblyFolderPath, relativePath);
return fullPath;
}
private void loadAndUseModel(MLContext mlcontext, IDataView dataView, String modelPath, String type, Color color)
{
ITransformer tansformedModel = mlcontext.Model.Load(modelPath, out var modelInputSchema);
// Step 3: Apply data transformation to create predictions.
IDataView transformedData = tansformedModel.Transform(dataView);
var predictions = mlcontext.Data.CreateEnumerable<ProductSalesPrediction>(transformedData, reuseRowObject: false);
// Index key for dictionary (date, sales).
int a = 0;
foreach (var prediction in predictions)
{
// Check if anomaly is predicted (indicated by an alert).
if (prediction.Prediction[0] == 1)
{
// Get the date (year-month) where spike is detected.
var xAxisDate = dict[a].Item1;
// Get the number of sales which was detected to be a spike.
var yAxisSalesNum = dict[a].Item2;
// Add anomaly points to graph
// and set point/marker options.
graph.Series["Series1"].Points[a].SetValueXY(a, yAxisSalesNum);
graph.Series["Series1"].Points[a].MarkerStyle = MarkerStyle.Star4;
graph.Series["Series1"].Points[a].MarkerSize = 10;
graph.Series["Series1"].Points[a].MarkerColor = color;
// Print out anomalies as text for user &
// change color of text accordingly.
string text = type + " detected in " + xAxisDate + ": " + yAxisSalesNum + "\n";
anomalyText.SelectionColor = color;
anomalyText.AppendText(text);
// Change row color in table where anomalies occur.
DataGridViewRow row = dataGridView1.Rows[a];
row.DefaultCellStyle.BackColor = color;
row.DefaultCellStyle.ForeColor = Color.White;
}
a++;
}
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void RadioButton1_CheckedChanged(object sender, EventArgs e)
{
}
}
}