Changeset 12320 for branches/HeuristicLab.VariableInteractionNetworks/HeuristicLab.VariableInteractionNetworks.Views/3.3/VariableInteractionNetworkView.cs
- Timestamp:
- 04/16/15 12:32:26 (10 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/HeuristicLab.VariableInteractionNetworks/HeuristicLab.VariableInteractionNetworks.Views/3.3/VariableInteractionNetworkView.cs
r12263 r12320 36 36 using System.Collections; 37 37 38 namespace HeuristicLab.VariableInteractionNetworks.Views { 39 [View("Variable Interaction Network")] 40 [Content(typeof(RunCollection), IsDefaultView = false)] 41 42 public sealed partial class VariableInteractionNetworkView : AsynchronousContentView { 43 private const string variableImpactResultName = "Variable impacts"; 44 private const string TrainingBestSolutionParameterName = "Best training solution"; 45 public new RunCollection Content { 46 get { return (RunCollection)base.Content; } 47 set { base.Content = value; } 38 namespace HeuristicLab.VariableInteractionNetworks.Views 39 { 40 [View("Variable Interaction Network")] 41 [Content(typeof(RunCollection), IsDefaultView = false)] 42 43 public sealed partial class VariableInteractionNetworkView : AsynchronousContentView 44 { 45 private const string variableImpactResultName = "Variable impacts"; 46 private const string TrainingBestSolutionParameterName = "Best training solution"; 47 48 public new RunCollection Content 49 { 50 get { return (RunCollection)base.Content; } 51 set { base.Content = value; } 52 } 53 54 public VariableInteractionNetworkView() 55 { 56 InitializeComponent(); 57 } 58 59 internal class NoFocusTrackBar : System.Windows.Forms.TrackBar 60 { 61 [System.Runtime.InteropServices.DllImport("user32.dll")] 62 public extern static int SendMessage(IntPtr hWnd, uint msg, int wParam, int lParam); 63 64 private static int MakeParam(int loWord, int hiWord) 65 { 66 return (hiWord << 16) | (loWord & 0xffff); 67 } 68 69 protected override void OnGotFocus(EventArgs e) 70 { 71 base.OnGotFocus(e); 72 SendMessage(this.Handle, 0x0128, MakeParam(1, 0x1), 0); 73 } 74 } 75 76 #region events 77 78 // #region Event Handlers (Content) 79 protected override void OnContentChanged() 80 { 81 base.OnContentChanged(); 82 if (Content == null) 83 { 84 // TODO: Add code when content has been changed and is null 85 } 86 else 87 { 88 // TODO: Add code when content has been changed and is not null 89 viewHost2.Content = CalculateNodeImportance(CalculateAdjacencyMatrix()); 90 var adjMatrix = CalculateAdjacencyMatrix(); 91 viewHost1.Content = adjMatrix; 92 viewHost3.Content = (DoubleMatrix)adjMatrix.Clone(); 93 trackBar1.Minimum = (int)(1000 * GetMinNonNullElement(adjMatrix)); 94 trackBar1.Maximum = (int)(1000 * (adjMatrix.Max())); 95 } 96 } 97 #endregion 98 99 protected override void SetEnabledStateOfControls() 100 { 101 base.SetEnabledStateOfControls(); 102 // TODO: Enable or disable controls based on whether the content is null or the view is set readonly 103 } 104 105 #region Event Handlers (child controls) 106 // TODO: Put event handlers of child controls here. 107 #endregion 108 109 private DoubleMatrix CalculateAdjacencyMatrix() 110 { 111 var runCollection = Content; 112 var groupRunCollection = Content.GroupBy(x => ((IRegressionProblemData)x.Parameters["ProblemData"]).TargetVariable).ToList(); 113 114 var allVariableImpacts = runCollection.Select(run => (DoubleMatrix)run.Results[variableImpactResultName]); 115 var variableNames = (from variableImpact in allVariableImpacts 116 from variableName in variableImpact.RowNames 117 select variableName).Distinct().ToArray(); 118 var adjMatrix = new DoubleMatrix(variableNames.Length, variableNames.Length); 119 120 adjMatrix.RowNames = groupRunCollection.Select(x => x.Key); 121 adjMatrix.ColumnNames = adjMatrix.RowNames; 122 for (int j = 0; j < groupRunCollection.Count; ++j) 123 { 124 var g = groupRunCollection[j]; 125 var matrix = CalculateAdjacencyRows(g); 126 var variables = new List<Tuple<string, double>>(); 127 var columnNames = matrix.ColumnNames.ToList(); 128 129 for (int i = 0; i < matrix.Columns; ++i) 130 { 131 variables.Add(new Tuple<string, double>(columnNames[i], matrix[0, i])); 132 } 133 variables.Add(new Tuple<string, double>(g.Key, 0)); 134 variables.Sort((a, b) => a.Item1.CompareTo(b.Item1)); 135 for (int i = 0; i < variables.Count; ++i) 136 { 137 adjMatrix[j, i] = variables[i].Item2; 138 139 } 140 } 141 return adjMatrix; 142 } 143 144 private DoubleMatrix CalculateAdjacencyRows(IEnumerable<IRun> runs) 145 { 146 IEnumerable<DoubleMatrix> allVariableImpacts = (from run in runs 147 select run.Results[variableImpactResultName]).Cast<DoubleMatrix>(); 148 var variableNames = (from variableImpact in allVariableImpacts 149 from variableName in variableImpact.RowNames 150 select variableName) 151 .Distinct().ToArray(); 152 153 List<string> variableNamesList = (from variableName in variableNames 154 where GetVariableImpacts(variableName, allVariableImpacts).Any(x => !x.IsAlmost(0.0)) 155 select variableName) 156 .ToList(); 157 158 var runNames = runs.Select(x => x.Name).ToArray(); 159 var runsArray = runs.ToArray(); 160 DoubleMatrix varImpactMatrix = CalculateVariableImpactMatrix(runsArray, runNames); 161 var targetMatrix = new DoubleMatrix(1, variableNames.Length); 162 163 for (int i = 0; i < varImpactMatrix.Rows; ++i) 164 { 165 targetMatrix[0, i] = varImpactMatrix[i, runNames.Length]; 166 } 167 168 targetMatrix.RowNames = new[] { "Target" }; 169 targetMatrix.ColumnNames = variableNames; 170 171 return targetMatrix; 172 } 173 174 private DoubleMatrix UpdateAdjacencyMatrixByThresholdAndTargetVariable(double threshold, string targetVariable, DoubleMatrix adjMatrix) 175 { 176 var originalMatrix = (DoubleMatrix)viewHost1.Content; 177 var groupRunCollection = Content.GroupBy(x => ((IRegressionProblemData)x.Parameters["ProblemData"]).TargetVariable).ToList(); 178 string[] targets = adjMatrix.RowNames.ToArray(); 179 var targetIndex = Array.IndexOf(targets, targetVariable); 180 181 for (int j = 0; j < groupRunCollection.Count; ++j) 182 { 183 double originalValue = originalMatrix[targetIndex, j]; 184 adjMatrix[targetIndex, j] = (originalValue <= Math.Max(threshold, Double.Parse(genThreshold.Text))) ? 0 : originalValue; 185 } 186 return adjMatrix; 187 } 188 189 private DoubleMatrix UpdateAdjacencyMatrixByThreshold(double threshold, DoubleMatrix adjMatrix) 190 { 191 // var updatedMatrix = (DoubleMatrix) adjMatrix.Clone(); 192 var originalMatrix = (DoubleMatrix)viewHost1.Content; 193 var groupRunCollection = Content.GroupBy(x => ((IRegressionProblemData)x.Parameters["ProblemData"]).TargetVariable).ToList(); 194 195 for (int i = 0; i < adjMatrix.Rows; ++i) 196 { 197 for (int j = 0; j < adjMatrix.Columns; ++j) 198 { 199 double originalValue = originalMatrix[i, j]; 200 adjMatrix[i, j] = originalValue <= threshold ? 0 : originalValue; 201 } 202 } 203 return adjMatrix; 204 } 205 206 private double GetMinNonNullElement(DoubleMatrix adjMatrix) 207 { 208 double min = adjMatrix.Max(); 209 for (int i = 0; i < adjMatrix.Rows; i++) 210 { 211 for (int j = 0; j < adjMatrix.Columns; j++) 212 { 213 min = (min > adjMatrix[i, j] && adjMatrix[i, j] != 0) ? adjMatrix[i, j] : min; 214 } 215 } 216 return min; 217 } 218 219 private double GetMaxFromRow(int rowIndex, DoubleMatrix adjMatrix) 220 { 221 double max = adjMatrix.Min(); 222 for (int j = 0; j < adjMatrix.Columns; ++j) 223 { 224 max = (max < adjMatrix[rowIndex, j] && adjMatrix[rowIndex, j] != 0) ? adjMatrix[rowIndex, j] : max; 225 } 226 return max; 227 } 228 229 private DoubleMatrix CalculateNodeImportance(DoubleMatrix adjMatrix) 230 { 231 DoubleMatrix nodeImportance = new DoubleMatrix(adjMatrix.Rows, 1); 232 var variables = new List<Tuple<string, double>>(); 233 var rowNames = adjMatrix.RowNames.ToList(); 234 var groupRunCollection = Content.GroupBy(x => ((IRegressionProblemData)x.Parameters["ProblemData"]).TargetVariable).ToList(); 235 double[] meanQuality = new double[groupRunCollection.Count]; 236 237 for (int j = 0; j < groupRunCollection.Count; ++j) 238 { 239 var g = groupRunCollection[j]; 240 meanQuality[j] = g.Average(x => ((IRegressionSolution)x.Results[TrainingBestSolutionParameterName]).TrainingRSquared); 241 } 242 243 for (int i = 0; i < adjMatrix.Columns; ++i) 244 { 245 for (int j = 0; j < adjMatrix.Rows; ++j) 246 { 247 nodeImportance[i, 0] += adjMatrix[j, i] * meanQuality[j]; 248 } 249 nodeImportance[i, 0] /= (adjMatrix.Rows - 1); 250 variables.Add(new Tuple<string, double>(rowNames[i], nodeImportance[i, 0])); 251 } 252 253 variables.Sort((b, a) => a.Item2.CompareTo(b.Item2)); 254 255 for (int i = 0; i < nodeImportance.Rows; ++i) 256 { 257 nodeImportance[i, 0] = variables[i].Item2; 258 rowNames[i] = variables[i].Item1; 259 } 260 261 nodeImportance.RowNames = rowNames; 262 nodeImportance.ColumnNames = new[] { "Node Importance" }; 263 return nodeImportance; 264 } 265 266 //adapted from RunCollectionVariableImpactView 267 private DoubleMatrix CalculateVariableImpactMatrix(IRun[] runs, string[] runNames) 268 { 269 IEnumerable<DoubleMatrix> allVariableImpacts = (from run in runs 270 select run.Results[variableImpactResultName]).Cast<DoubleMatrix>(); 271 IEnumerable<string> variableNames = (from variableImpact in allVariableImpacts 272 from variableName in variableImpact.RowNames 273 select variableName).Distinct(); 274 275 // filter variableNames: only include names that have at least one non-zero value in a run 276 List<string> variableNamesList = (from variableName in variableNames 277 where GetVariableImpacts(variableName, allVariableImpacts).Any(x => !x.IsAlmost(0.0)) 278 select variableName).ToList(); 279 280 List<string> columnNames = new List<string>(runNames); 281 columnNames.Add("Mean"); 282 283 int numberOfRuns = runs.Length; 284 285 DoubleMatrix matrix = new DoubleMatrix(variableNamesList.Count, numberOfRuns + 1); 286 matrix.SortableView = true; 287 matrix.ColumnNames = columnNames; 288 289 List<List<double>> variableImpactsOverRuns = (from variableName in variableNamesList 290 select GetVariableImpacts(variableName, allVariableImpacts).ToList()).ToList(); 291 292 for (int row = 0; row < variableImpactsOverRuns.Count; row++) 293 { 294 matrix[row, numberOfRuns] = Math.Round(variableImpactsOverRuns[row].Average(), 3); 295 } 296 297 // fill matrix with impacts from runs 298 for (int i = 0; i < runs.Length; i++) 299 { 300 IRun run = runs[i]; 301 DoubleMatrix runVariableImpacts = (DoubleMatrix)run.Results[variableImpactResultName]; 302 for (int j = 0; j < runVariableImpacts.Rows; j++) 303 { 304 int rowIndex = variableNamesList.FindIndex(s => s == runVariableImpacts.RowNames.ElementAt(j)); 305 if (rowIndex > -1) 306 { 307 matrix[rowIndex, i] = Math.Round(runVariableImpacts[j, 0], 3); 308 } 309 } 310 } 311 return matrix; 312 } 313 314 //taken from RunCollectionVariableImpactView 315 private IEnumerable<double> GetVariableImpacts(string variableName, IEnumerable<DoubleMatrix> allVariableImpacts) 316 { 317 foreach (DoubleMatrix runVariableImpacts in allVariableImpacts) 318 { 319 int row = 0; 320 foreach (string rowName in runVariableImpacts.RowNames) 321 { 322 if (rowName == variableName) 323 yield return runVariableImpacts[row, 0]; 324 row++; 325 } 326 } 327 } 328 329 private void trackBar1_ValueChanged(object sender, EventArgs e) 330 { 331 genThreshold.Text = (0.001 * trackBar1.Value).ToString(); 332 textBox1.Text = (0.001 * trackBar1.Minimum).ToString(); 333 textBox2.Text = (0.001 * trackBar1.Maximum).ToString(); 334 viewHost3.Content = UpdateAdjacencyMatrixByThreshold(0.001 * trackBar1.Value, (DoubleMatrix)viewHost3.Content); 335 } 336 337 private void mouseDownEvent(TrackBar tb, MouseEventArgs e) 338 { 339 double percentage = (double)e.X / (double)(tb.Width - 6); 340 double clickPos = percentage * (tb.Maximum - tb.Minimum); 341 try 342 { 343 tb.Value = (int)clickPos + tb.Minimum; 344 } 345 catch 346 { 347 MessageBox.Show("Value outside range!"); 348 return; 349 } 350 } 351 352 private void trackBar1_MouseDown(object sender, MouseEventArgs e) 353 { 354 mouseDownEvent(trackBar1, e); 355 } 356 357 private void trackBar2_MouseDown(object sender, MouseEventArgs e) 358 { 359 if (targetVariablesCombo.SelectedIndex < 0) 360 { 361 return; 362 } 363 else 364 { 365 mouseDownEvent(trackBar2, e); 366 } 367 } 368 369 private void trackBar2_ValueChanged(object sender, EventArgs e) 370 { 371 targetThreshold.Text = (0.001 * trackBar2.Value).ToString(); 372 373 if (targetVariablesCombo.SelectedIndex < 0) 374 { 375 MessageBox.Show("Please select target variable!"); 376 return; 377 } 378 else 379 { 380 string selectedItem = (string)targetVariablesCombo.Items[targetVariablesCombo.SelectedIndex]; 381 viewHost3.Content = UpdateAdjacencyMatrixByThresholdAndTargetVariable(0.001 * trackBar2.Value, selectedItem, (DoubleMatrix)viewHost3.Content); 382 } 383 } 384 385 private void genThresholdEvent() 386 { 387 this.errorProvider.SetError(genThreshold, ""); 388 389 if (genThreshold.Text != "") 390 { 391 if (Double.Parse(genThreshold.Text) >= GetMinNonNullElement((DoubleMatrix)viewHost1.Content) && Double.Parse(genThreshold.Text) <= ((DoubleMatrix)viewHost1.Content).Max()) 392 { 393 genThreshold.Select(genThreshold.Text.Length, 0); 394 trackBar1.Value = (int)(1000 * Double.Parse(genThreshold.Text)); 395 viewHost3.Content = UpdateAdjacencyMatrixByThreshold(Double.Parse(genThreshold.Text), (DoubleMatrix)viewHost3.Content); 396 } 397 else 398 { 399 this.errorProvider.SetError(genThreshold, "Value out of range!"); 400 } 401 } 402 else 403 { 404 MessageBox.Show("Please select a threshold!"); 405 this.errorProvider.SetError(genThreshold, ""); 406 return; 407 } 408 } 409 410 private void genThreshold_TextChanged(object sender, EventArgs e) 411 { 412 genThresholdEvent(); 413 } 414 415 private void genThreshold_KeyDown(object sender, KeyEventArgs e) 416 { 417 if (e.KeyCode == Keys.Enter) 418 genThresholdEvent(); 419 } 420 421 private void targetThresholdEvent() 422 { 423 this.errorProvider2.SetError(targetThreshold, ""); 424 425 if (targetVariablesCombo.SelectedIndex < 0) 426 { 427 return; 428 } 429 else 430 { 431 string selectedItem = (string)targetVariablesCombo.Items[targetVariablesCombo.SelectedIndex]; 432 if (Double.Parse(targetThreshold.Text) >= Double.Parse(textBox3.Text) && Double.Parse(targetThreshold.Text) <= Double.Parse(textBox4.Text)) 433 { 434 trackBar2.Value = (int)(1000 * Double.Parse(targetThreshold.Text)); 435 } 436 else 437 { 438 this.errorProvider2.SetError(targetThreshold, "Value out of range!"); 439 return; 440 } 441 } 442 } 443 444 private void targetThreshold_TextChanged(object sender, EventArgs e) 445 { 446 targetThresholdEvent(); 447 } 448 449 private void targetThreshold_KeyDown(object sender, KeyEventArgs e) 450 { 451 if (e.KeyCode == Keys.Enter) 452 { 453 targetThresholdEvent(); 454 } 455 } 456 457 private void targetVariablesCombo_Dropdown(object sender, System.EventArgs e) 458 { 459 targetVariablesCombo.Items.Clear(); 460 string[] targetVariables = ((DoubleMatrix)viewHost3.Content).RowNames.ToArray(); 461 targetVariablesCombo.Items.AddRange(targetVariables); 462 } 463 464 private void targetVariablesCombo_SelectedIndexChanged(object sender, System.EventArgs e) 465 { 466 var targetIndex = targetVariablesCombo.SelectedIndex; 467 string selectedItem = (string)targetVariablesCombo.Items[targetIndex]; 468 trackBar2.Minimum = 0; 469 trackBar2.Maximum = (int)(1000 * GetMaxFromRow(targetIndex, (DoubleMatrix)viewHost1.Content)); 470 textBox3.Text = trackBar2.Minimum.ToString(); 471 textBox4.Text = (0.001 * trackBar2.Maximum).ToString(); 472 UpdateAdjacencyMatrixByThresholdAndTargetVariable(0.001 * trackBar2.Value, selectedItem, (DoubleMatrix)viewHost3.Content); 473 } 48 474 } 49 50 public VariableInteractionNetworkView() {51 InitializeComponent();52 }53 54 #region events55 56 // #region Event Handlers (Content)57 protected override void OnContentChanged() {58 base.OnContentChanged();59 if (Content == null) {60 // TODO: Add code when content has been changed and is null61 } else {62 // TODO: Add code when content has been changed and is not null63 CalculateAdjacencyMatrix();64 }65 }66 #endregion67 68 protected override void SetEnabledStateOfControls() {69 base.SetEnabledStateOfControls();70 // TODO: Enable or disable controls based on whether the content is null or the view is set readonly71 }72 73 #region Event Handlers (child controls)74 // TODO: Put event handlers of child controls here.75 #endregion76 77 private void CalculateAdjacencyMatrix()78 {79 var runCollection = Content;80 var groupRunCollection = Content.GroupBy(x => ((IRegressionProblemData)x.Parameters["ProblemData"]).TargetVariable).ToList();81 82 var allVariableImpacts = runCollection.Select(run => (DoubleMatrix)run.Results[variableImpactResultName]);83 var variableNames = (from variableImpact in allVariableImpacts84 from variableName in variableImpact.RowNames85 select variableName).Distinct().ToArray();86 var adjMatrix = new DoubleMatrix(variableNames.Length, variableNames.Length);87 88 adjMatrix.RowNames = groupRunCollection.Select(x => x.Key);89 adjMatrix.ColumnNames = adjMatrix.RowNames;90 91 for (int j = 0; j < groupRunCollection.Count; ++j)92 {93 var g = groupRunCollection[j];94 var matrix = CalculateAdjacencyRows(g);95 var variables = new List<Tuple<string, double>>();96 var columnNames = matrix.ColumnNames.ToList();97 98 for (int i = 0; i < matrix.Columns; ++i)99 {100 variables.Add(new Tuple<string, double>(columnNames[i], matrix[0, i]));101 }102 variables.Add(new Tuple<string, double>(g.Key, 0));103 variables.Sort((a, b) => a.Item1.CompareTo(b.Item1));104 for (int i = 0; i < variables.Count; ++i)105 {106 adjMatrix[j, i] = variables[i].Item2;107 }108 }109 viewHost2.Content = CalculateNodeImportance(adjMatrix);110 viewHost3.Content = UpdateAdjacencyMatrixByThreshold(0.2, "x1", adjMatrix);111 viewHost1.Content = adjMatrix;112 }113 114 private DoubleMatrix CalculateAdjacencyRows(IEnumerable<IRun> runs)115 {116 IEnumerable<DoubleMatrix> allVariableImpacts = (from run in runs117 select run.Results[variableImpactResultName]).Cast<DoubleMatrix>();118 var variableNames = (from variableImpact in allVariableImpacts119 from variableName in variableImpact.RowNames120 select variableName)121 .Distinct().ToArray();122 123 List<string> variableNamesList = (from variableName in variableNames124 where GetVariableImpacts(variableName, allVariableImpacts).Any(x => !x.IsAlmost(0.0))125 select variableName)126 .ToList();127 128 var runNames = runs.Select(x => x.Name).ToArray();129 var runsArray = runs.ToArray();130 DoubleMatrix varImpactMatrix = CalculateVariableImpactMatrix(runsArray, runNames);131 var targetMatrix = new DoubleMatrix(1, variableNames.Length);132 133 for (int i = 0; i < varImpactMatrix.Rows; ++i)134 {135 targetMatrix[0, i] = varImpactMatrix[i, runNames.Length];136 }137 138 targetMatrix.RowNames = new[] { "Target" };139 targetMatrix.ColumnNames = variableNames;140 141 return targetMatrix;142 }143 144 private DoubleMatrix UpdateAdjacencyMatrixByThreshold(double threshold, string targetVariable, DoubleMatrix adjMatrix)145 {146 var updatedMatrix = (DoubleMatrix) adjMatrix.Clone();147 var groupRunCollection = Content.GroupBy(x => ((IRegressionProblemData)x.Parameters["ProblemData"]).TargetVariable).ToList();148 string[] targets = adjMatrix.RowNames.ToArray();149 var targetIndex = Array.IndexOf(targets, targetVariable);150 151 for (int j = 0; j < groupRunCollection.Count; ++j)152 {153 if (updatedMatrix[targetIndex, j] < threshold)154 {155 updatedMatrix[targetIndex, j] = 0;156 }157 }158 return updatedMatrix;159 }160 161 private DoubleMatrix CalculateNodeImportance(DoubleMatrix adjMatrix)162 {163 DoubleMatrix nodeImportance = new DoubleMatrix(adjMatrix.Rows, 1);164 var variables = new List<Tuple<string, double>>();165 var rowNames = adjMatrix.RowNames.ToList();166 var groupRunCollection = Content.GroupBy(x => ((IRegressionProblemData)x.Parameters["ProblemData"]).TargetVariable).ToList();167 double[] meanQuality = new double[groupRunCollection.Count];168 169 for (int j = 0; j < groupRunCollection.Count; ++j)170 {171 var g = groupRunCollection[j];172 meanQuality[j] = g.Average(x => ((IRegressionSolution)x.Results[TrainingBestSolutionParameterName]).TrainingRSquared);173 }174 175 for (int i = 0; i < adjMatrix.Columns; ++i)176 {177 for (int j = 0; j < adjMatrix.Rows; ++j)178 {179 nodeImportance[i, 0] += adjMatrix[j, i];180 }181 nodeImportance[i, 0] = nodeImportance[i, 0] * meanQuality[i] / (adjMatrix.Rows - 1);182 variables.Add(new Tuple<string, double>(rowNames[i], nodeImportance[i, 0]));183 }184 185 variables.Sort((b,a) => a.Item2.CompareTo(b.Item2));186 187 for (int i = 0; i < nodeImportance.Rows; ++i)188 {189 nodeImportance[i, 0] = variables[i].Item2;190 rowNames[i] = variables[i].Item1;191 }192 193 nodeImportance.RowNames = rowNames;194 nodeImportance.ColumnNames = new[] { "Node Importance" };195 return nodeImportance;196 }197 198 //adapted from RunCollectionVariableImpactView199 private DoubleMatrix CalculateVariableImpactMatrix(IRun[] runs, string[] runNames)200 {201 IEnumerable<DoubleMatrix> allVariableImpacts = (from run in runs202 select run.Results[variableImpactResultName]).Cast<DoubleMatrix>();203 IEnumerable<string> variableNames = (from variableImpact in allVariableImpacts204 from variableName in variableImpact.RowNames205 select variableName).Distinct();206 207 // filter variableNames: only include names that have at least one non-zero value in a run208 List<string> variableNamesList = (from variableName in variableNames209 where GetVariableImpacts(variableName, allVariableImpacts).Any(x => !x.IsAlmost(0.0))210 select variableName).ToList();211 212 List<string> columnNames = new List<string>(runNames);213 columnNames.Add("Mean");214 215 int numberOfRuns = runs.Length;216 217 DoubleMatrix matrix = new DoubleMatrix(variableNamesList.Count, numberOfRuns + 1);218 matrix.SortableView = true;219 matrix.ColumnNames = columnNames;220 221 List<List<double>> variableImpactsOverRuns = (from variableName in variableNamesList222 select GetVariableImpacts(variableName, allVariableImpacts).ToList()).ToList();223 224 for (int row = 0; row < variableImpactsOverRuns.Count; row++)225 {226 matrix[row, numberOfRuns] = Math.Round(variableImpactsOverRuns[row].Average(), 3);227 }228 229 // fill matrix with impacts from runs230 for (int i = 0; i < runs.Length; i++)231 {232 IRun run = runs[i];233 DoubleMatrix runVariableImpacts = (DoubleMatrix)run.Results[variableImpactResultName];234 for (int j = 0; j < runVariableImpacts.Rows; j++)235 {236 int rowIndex = variableNamesList.FindIndex(s => s == runVariableImpacts.RowNames.ElementAt(j));237 if (rowIndex > -1)238 {239 matrix[rowIndex, i] = Math.Round(runVariableImpacts[j, 0], 3);240 }241 }242 }243 return matrix;244 }245 246 //taken from RunCollectionVariableImpactView247 private IEnumerable<double> GetVariableImpacts(string variableName, IEnumerable<DoubleMatrix> allVariableImpacts)248 {249 foreach (DoubleMatrix runVariableImpacts in allVariableImpacts)250 {251 int row = 0;252 foreach (string rowName in runVariableImpacts.RowNames)253 {254 if (rowName == variableName)255 yield return runVariableImpacts[row, 0];256 row++;257 }258 }259 }260 }261 475 }
Note: See TracChangeset
for help on using the changeset viewer.