Changeset 11541
- Timestamp:
- 11/12/14 15:58:32 (10 years ago)
- Location:
- trunk/sources
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic.Classification.Views/3.4/SymbolicDiscriminantFunctionClassificationSolutionExcelExporter.cs
r11523 r11541 6 6 using HeuristicLab.Problems.DataAnalysis.Symbolic.Views; 7 7 using OfficeOpenXml; 8 using OfficeOpenXml.Drawing.Chart; 8 9 9 10 namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Classification.Views { 10 11 public class SymbolicDiscriminantFunctionClassificationSolutionExcelExporter : SymbolicSolutionExcelExporter { 11 protected override void ExportChart(string fileName, ISymbolicDataAnalysisSolution solution, string formula) { 12 ExportChart(fileName, solution as ISymbolicClassificationSolution, formula); 13 } 14 15 private void ExportChart(string fileName, ISymbolicClassificationSolution solution, string formula) { 12 public override void Export(IDataAnalysisSolution solution, string fileName) { 13 var symbDiscriminantSolution = solution as SymbolicDiscriminantFunctionClassificationSolution; 14 if (symbDiscriminantSolution == null) throw new NotSupportedException("This solution cannot be exported to Excel"); 15 var formatter = new SymbolicDataAnalysisExpressionExcelFormatter(); 16 var formula = formatter.Format(symbDiscriminantSolution.Model.SymbolicExpressionTree, solution.ProblemData.Dataset); 17 ExportChart(fileName, symbDiscriminantSolution, formula); 18 } 19 20 private void ExportChart(string fileName, SymbolicDiscriminantFunctionClassificationSolution solution, string formula) { 16 21 FileInfo newFile = new FileInfo(fileName); 17 22 if (newFile.Exists) { … … 35 40 36 41 ExcelWorksheet chartsWorksheet = package.Workbook.Worksheets.Add("Charts"); 37 AddCharts(chartsWorksheet );42 AddCharts(chartsWorksheet, solution); 38 43 package.Workbook.Properties.Title = "Excel Export"; 39 44 package.Workbook.Properties.Author = "HEAL"; … … 42 47 package.Save(); 43 48 } 44 }45 46 protected void WriteEstimatedWorksheet(ExcelWorksheet estimatedWorksheet, ExcelWorksheet datasetWorksheet, string[] formulaParts, ISymbolicDataAnalysisSolution solution) {47 WriteEstimatedWorksheet(estimatedWorksheet, datasetWorksheet, formulaParts, solution as SymbolicDiscriminantFunctionClassificationSolution);48 }49 50 protected override void FormatModelSheet(ExcelWorksheet modelWorksheet, ISymbolicDataAnalysisSolution solution, IEnumerable<string> formulaParts) {51 FormatModelSheet(modelWorksheet, solution as SymbolicDiscriminantFunctionClassificationSolution, formulaParts);52 49 } 53 50 … … 180 177 estimatedWorksheet.Cells[1, 3].Value = "Estimated Values"; // C 181 178 estimatedWorksheet.Cells[1, 4].Value = "Estimated Class Values"; // D 182 estimatedWorksheet.Cells[1, 6].Value = "Error"; // E179 estimatedWorksheet.Cells[1, 6].Value = "Error"; // F 183 180 estimatedWorksheet.Cells[1, 7].Value = "Squared Error"; // G 184 181 estimatedWorksheet.Cells[1, 9].Value = "Unbounded Estimated Values"; // I 185 182 estimatedWorksheet.Cells[1, 10].Value = "Bounded Estimated Values"; // J 186 187 estimatedWorksheet.Cells[1, 1, 1, 10].AutoFitColumns(); 183 estimatedWorksheet.Cells[1, 11].Value = "Random Key"; // K 188 184 189 185 var thresholds = solution.Model.Thresholds.Where(x => !double.IsInfinity(x)).ToList(); 190 186 var thresholdsFormula = GenerateThresholdsFormula(thresholds); 187 188 const int columnIndex = 13; // index of beginning columns for class values 189 for (int i = 0; i <= thresholds.Count; ++i) { 190 estimatedWorksheet.Cells[1, i + columnIndex].Value = "Class " + i; 191 if (i < thresholds.Count) 192 estimatedWorksheet.Cells[1, i + columnIndex + thresholds.Count + 1].Value = "Threshold " + i; 193 } 194 estimatedWorksheet.Cells[1, 1, 1, 10].AutoFitColumns(); 191 195 192 196 int targetIndex = solution.ProblemData.Dataset.VariableNames.ToList().FindIndex(x => x.Equals(solution.ProblemData.TargetVariable)) + 1; … … 195 199 estimatedWorksheet.Cells[i + 2, 2].Formula = datasetWorksheet.Cells[i + 2, targetIndex].FullAddress; // target values 196 200 estimatedWorksheet.Cells[i + 2, 9].Formula = string.Format(preparedFormula, i + 2); // formula (estimated) values 201 202 string condition = string.Empty; 203 string rowRef = "C" + (i + 2); 204 205 int nClasses = thresholds.Count + 1; 206 for (int j = columnIndex; j < columnIndex + nClasses; ++j) { 207 int idx = j - columnIndex + 5; // row index for the threshold values 208 if (j == columnIndex) { 209 condition = rowRef + " < Model!$B$" + idx; 210 } else if (j > columnIndex && j < columnIndex + thresholds.Count) { 211 condition = "AND(" + rowRef + "> Model!$B$" + (idx - 1) + ", " + rowRef + " < Model!$B$" + idx + ")"; 212 } else if (j == columnIndex + thresholds.Count) { 213 condition = rowRef + " > Model!$B$" + (idx - 1); 214 } 215 estimatedWorksheet.Cells[i + 2, j].Formula = "IF(" + condition + ", " + rowRef + ", #N/A)"; 216 217 if (j < columnIndex + thresholds.Count) 218 estimatedWorksheet.Cells[i + 2, j + nClasses].Formula = "Model!$B$" + idx; 219 } 197 220 } 198 221 estimatedWorksheet.Cells["B2:B" + (rows + 1)].Style.Numberformat.Format = "0.000"; … … 213 236 estimatedWorksheet.Cells["J2:J" + (rows + 1)].Formula = "IFERROR(IF(I2 > Model!EstimationLimitUpper, Model!EstimationLimitUpper, IF(I2 < Model!EstimationLimitLower, Model!EstimationLimitLower, I2)), AVERAGE(Model!EstimationLimitLower, Model!EstimationLimitUpper))"; 214 237 estimatedWorksheet.Cells["J2:J" + (rows + 1)].Style.Numberformat.Format = "0.000"; 238 239 estimatedWorksheet.Cells["K2:K" + (rows + 1)].Formula = "RAND()"; 240 estimatedWorksheet.Cells["K2:K" + (rows + 1)].Style.Numberformat.Format = "0.000"; 241 } 242 243 private void AddCharts(ExcelWorksheet chartsWorksheet, DiscriminantFunctionClassificationSolution solution) { 244 List<char> columns = new List<char> { 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' }; 245 var scatterPlot = chartsWorksheet.Drawings.AddChart("scatterPlot", eChartType.XYScatter); 246 scatterPlot.SetSize(800, 400); 247 scatterPlot.SetPosition(0, 0); 248 scatterPlot.Title.Text = "Scatter Plot"; 249 var thresholds = solution.Model.Thresholds.Where(x => !double.IsInfinity(x)).ToList(); 250 chartsWorksheet.Names.AddFormula("XKey", "OFFSET('Estimated Values'!$K$1,1,0, COUNTA('Estimated Values'!$K:$K)-1)"); 251 252 for (int i = 0; i <= thresholds.Count; ++i) { 253 string header = "Class" + i; 254 chartsWorksheet.Names.AddFormula(header, String.Format("OFFSET('Estimated Values'!${0}$1, 1, 0, COUNTA('Estimated Values'!${0}:${0})-1)", columns[i])); 255 var series = scatterPlot.Series.Add(header, "XKey"); 256 series.Header = header; 257 258 if (i < thresholds.Count) { 259 chartsWorksheet.Names.AddFormula("Threshold" + i, String.Format("OFFSET('Estimated Values'!${0}$1, 1, 0, COUNTA('Estimated Values'!${0}:${0})-1)", columns[i + thresholds.Count + 1])); 260 var s = scatterPlot.Series.Add("Threshold" + i, "XKey"); 261 s.Header = "Threshold" + i; 262 } 263 } 215 264 } 216 265 217 266 // this method assumes that the thresholds list is sorted in ascending order 218 private st ring GenerateThresholdsFormula(List<double> thresholds) {267 private static string GenerateThresholdsFormula(List<double> thresholds) { 219 268 if (thresholds.Count == 1) { 220 269 return String.Format("IF(J2 < {0}, 0, 1)", thresholds[0]); -
trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic.Views/3.4/Exporters/SymbolicSolutionExcelExporter.cs
r11523 r11541 44 44 } 45 45 46 public v oid Export(IDataAnalysisSolution solution, string fileName) {46 public virtual void Export(IDataAnalysisSolution solution, string fileName) { 47 47 var symbSolution = solution as ISymbolicDataAnalysisSolution; 48 48 if (symbSolution == null) throw new NotSupportedException("This solution cannot be exported to Excel"); … … 52 52 } 53 53 54 pr otected virtualvoid ExportChart(string fileName, ISymbolicDataAnalysisSolution solution, string formula) {54 private void ExportChart(string fileName, ISymbolicDataAnalysisSolution solution, string formula) { 55 55 FileInfo newFile = new FileInfo(fileName); 56 56 if (newFile.Exists) { … … 85 85 } 86 86 87 pr otected virtualvoid FormatModelSheet(ExcelWorksheet modelWorksheet, ISymbolicDataAnalysisSolution solution, IEnumerable<string> formulaParts) {87 private void FormatModelSheet(ExcelWorksheet modelWorksheet, ISymbolicDataAnalysisSolution solution, IEnumerable<string> formulaParts) { 88 88 int row = 1; 89 89 modelWorksheet.Cells[row, 1].Value = "Model"; … … 224 224 } 225 225 226 pr otected virtualvoid AddCharts(ExcelWorksheet chartsWorksheet) {226 private void AddCharts(ExcelWorksheet chartsWorksheet) { 227 227 chartsWorksheet.Names.AddFormula("AllId", "OFFSET('Estimated Values'!$A$1,1,0, COUNTA('Estimated Values'!$A:$A)-1)"); 228 228 chartsWorksheet.Names.AddFormula("AllTarget", "OFFSET('Estimated Values'!$B$1,1,0, COUNTA('Estimated Values'!$B:$B)-1)"); … … 260 260 } 261 261 262 protected v irtual void AddModelTreePicture(ExcelWorksheet modelWorksheet, ISymbolicDataAnalysisModel model) {262 protected void AddModelTreePicture(ExcelWorksheet modelWorksheet, ISymbolicDataAnalysisModel model) { 263 263 SymbolicExpressionTreeChart modelTreePicture = new SymbolicExpressionTreeChart(); 264 264 modelTreePicture.Tree = model.SymbolicExpressionTree;
Note: See TracChangeset
for help on using the changeset viewer.