Repository navigation
Expand file tree
/
Copy pathPredictionTaskForm.Workflow.cs
More file actions
197 lines (179 loc) · 9.51 KB
/
Copy pathPredictionTaskForm.Workflow.cs
File metadata and controls
197 lines (179 loc) · 9.51 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
using JadeChem.Utils;
using System.Data;
namespace JadeChem;
public partial class PredictionTaskForm
{
internal void PredictDataset()
{
if (predictionDataset == null)
throw new InvalidOperationException("Load prediction data first.");
visualizePredictionButton.Enabled = false;
predictionInputColumns = null;
predictionOutputColumnForRegression = null;
predictionOutputColumnForClassification = null;
// DataGridView rows can move while sorted by the prediction column. Keep
// stable DataRow identities and publish results only after every row succeeds.
DataRow[] rows = predictionDataset.Rows.Cast<DataRow>().ToArray();
int outputColumn = predictionDataset.Columns.Count - 1;
foreach (DataRow row in rows) row[outputColumn] = "(...)";
if (rows.Length == 0) throw new ArgumentException("Prediction data must contain at least one row.");
var processed = new double[rows.Length][];
var outputs = new string[rows.Length];
double[]? numericOutputs = predictionType == PredictionType.Regression ? new double[rows.Length] : null;
for (int i = 0; i < rows.Length; i++)
{
string[] cells = Enumerable.Range(0, outputColumn).Select(column => Convert.ToString(rows[i][column]) ?? "").ToArray();
if (cells.Any(string.IsNullOrWhiteSpace)) throw new ArgumentException($"Input values cannot be empty (row {i + 1}).");
(processed[i], outputs[i]) = PredictRow(cells);
if (numericOutputs != null) numericOutputs[i] = double.Parse(outputs[i]);
predictionProgressBar.Visible = true;
predictionProgressBar.Value = (int)(100L * (i + 1) / rows.Length);
predictionProgressLabel.Text = $"{i + 1}/{rows.Length} rows predicted...";
predictionProgressBar.Update();
predictionProgressLabel.Update();
}
for (int i = 0; i < rows.Length; i++) rows[i][outputColumn] = outputs[i];
predictionInputColumns = processed;
predictionOutputColumnForRegression = numericOutputs;
predictionOutputColumnForClassification = numericOutputs == null ? outputs : null;
predictionProgressLabel.Text = $"{rows.Length} rows predicted!";
visualizePredictionButton.Enabled = true;
}
internal static void AddPredictionColumn(DataTable data, string outputName, Type columnType)
{
string name = outputName + " (Predicted)";
while (data.Columns.Contains(name)) name += " (Predicted)";
data.Columns.Add(name, columnType);
}
internal static DataTable PreparePredictionDataset(DataTable data, string[] inputNames, bool hasHeaders)
{
if (data.Columns.Count != inputNames.Length)
throw new ArgumentException($"Prediction data must contain {inputNames.Length} input columns.");
var result = data.Copy();
if (hasHeaders)
{
foreach (string name in inputNames)
if (!result.Columns.Contains(name))
throw new ArgumentException($"Prediction data is missing the input column '{name}'.");
for (int column = 0; column < inputNames.Length; column++)
result.Columns[inputNames[column]]!.SetOrdinal(column);
}
return result;
}
private void EnsureProcessingConfiguration()
{
string[] expectedOutputs = predictionType == PredictionType.Regression ? [outputColumnName!] : [];
if (!isDataProcessingDialogResetNeeded &&
inputScalersDictionary.Keys.SequenceEqual(inputColumnNames!) &&
outputScalersDictionary.Keys.SequenceEqual(expectedOutputs)) return;
inputScalersDictionary = inputColumnNames!.ToDictionary(name => name, _ => new Dictionary<string, (double, double)>());
outputScalersDictionary = expectedOutputs.ToDictionary(name => name, _ => new Dictionary<string, (double, double)>());
dimensionalityReductionStepsDictionary.Clear();
processingStepsListBox.Items.Clear();
isDataProcessingDialogResetNeeded = false;
}
private void FitProcessing(int[] fittingIndices)
{
if (inputData == null || inputColumnNames == null || outputColumnName == null)
throw new InvalidOperationException("Select the input and output columns first.");
if (inputData.Rows.Count < 2)
throw new ArgumentException("At least two data rows are required for training and testing.");
var rawInputs = inputData.Rows.Cast<DataRow>().Select(row =>
inputColumnNames.Select(name => Convert.ToDouble(row[name])).ToArray()).ToArray();
double[]? rawOutput = predictionType == PredictionType.Regression
? inputData.Rows.Cast<DataRow>().Select(row => Convert.ToDouble(row[outputColumnName])).ToArray()
: null;
outputScalersDictionary.TryGetValue(outputColumnName, out var outputSteps);
var fitted = new FittedPreprocessor(inputColumnNames, rawInputs.Get(fittingIndices),
rawOutput?.Get(fittingIndices), inputScalersDictionary, outputSteps ?? new(), dimensionalityReductionStepsDictionary);
var transformed = fitted.Transform(rawInputs);
var transformedOutput = rawOutput == null ? null : fitted.TransformOutput(rawOutput);
var table = new DataTable();
foreach (string name in fitted.ColumnNames) table.Columns.Add(name, typeof(double));
// PCA component names can coincide with the user's output column name.
string tableOutputName = outputColumnName;
while (table.Columns.Contains(tableOutputName)) tableOutputName += " (Output)";
table.Columns.Add(tableOutputName, rawOutput == null ? typeof(string) : typeof(double));
string[]? labels = rawOutput == null ? inputData.Columns[outputColumnName]!.ToArray<string>() : null;
for (int row = 0; row < transformed.Length; row++)
{
var values = transformed[row].Cast<object>().ToList();
values.Add(transformedOutput == null ? labels![row] : transformedOutput[row]);
table.Rows.Add(values.ToArray());
}
preprocessor = fitted;
processedInputColumnNames = fitted.ColumnNames;
processedInputColumns = transformed;
processedOutputColumnForRegression = transformedOutput;
processedOutputColumnForClassification = labels;
processedClassIndices = labels?.Select(label => Array.IndexOf(classLabels!, label)).ToArray();
processedDataset = table;
}
private static void DisposeControls(Control container)
{
foreach (Control control in container.Controls.Cast<Control>().ToArray()) control.Dispose();
container.Controls.Clear();
}
private void ResetResults()
{
predictedOutputColumnForClassification = null;
predictedOutputColumnForRegression = null;
predictionDataset = null;
predictionInputColumns = null;
predictionOutputColumnForRegression = null;
predictionOutputColumnForClassification = null;
evaluationDataGridView.DataSource = null;
predictionDatasetDataGridView.DataSource = null;
predictionDataGridView.Columns.Clear();
DisposeControls(evaluationMetricsPanel);
evaluationTableLayoutPanel.Enabled = false;
predictionTableLayoutPanel.Enabled = false;
visualizeEvaluationButton.Enabled = false;
visualizePredictionButton.Enabled = false;
predictDatasetButton.Enabled = false;
predictionProgressLabel.Text = "";
predictionProgressBar.Visible = false;
workflowDiagramControl.DrawEvaluationBlockOutlines(false);
}
private void ResetModel()
{
ResetResults();
DiscardAdditionalModel();
model = null;
DisposeControls(modelPanel);
workflowDiagramControl.DrawModelBlockDashedOutlines(false);
workflowDiagramControl.DrawModelBlockOutlines(false);
}
private void ResetSplit()
{
ResetModel();
trainDataset = testDataset = null;
trainInputColumns = testInputColumns = null;
trainOutputColumnForRegression = testOutputColumnForRegression = null;
trainOutputColumnForClassification = testOutputColumnForClassification = null;
trainClassIndices = testClassIndices = null;
trainDatasetDataGridView.DataSource = testDatasetDataGridView.DataSource = null;
trainDatasetTableLayoutPanel.Enabled = testDatasetTableLayoutPanel.Enabled = false;
workflowDiagramControl.DrawTrainDatasetBlockOutlines(false);
workflowDiagramControl.DrawTestDatasetBlockOutlines(false);
}
private void ResetProcessedData()
{
ResetSplit();
preprocessor = null;
processedDataset = null;
processedInputColumns = null;
processedInputColumnNames = null;
processedOutputColumnForRegression = null;
processedOutputColumnForClassification = null;
processedClassIndices = null;
processedDataDataGridView.DataSource = null;
processedDataTableLayoutPanel.Enabled = modelTableLayoutPanel.Enabled = false;
processingProgressLabel.Text = "";
binaryClassificationRadioButton.Enabled = multiclassClassificationRadioButton.Enabled = regressionRadioButton.Enabled = true;
workflowDiagramControl.DrawProcessDataBlockOutlines(false);
workflowDiagramControl.Refresh();
}
private static Dictionary<string, Dictionary<string, T>> CopySteps<T>(Dictionary<string, Dictionary<string, T>> steps) =>
steps.ToDictionary(pair => pair.Key, pair => new Dictionary<string, T>(pair.Value));
}