Changeset 14330
- Timestamp:
- 10/13/16 19:47:41 (8 years ago)
- Location:
- branches/symbreg-factors-2650
- Files:
-
- 53 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/symbreg-factors-2650
- Property svn:mergeinfo changed
/trunk/sources merged: 14282,14284-14300,14307,14314-14316,14319,14322
- Property svn:mergeinfo changed
-
branches/symbreg-factors-2650/HeuristicLab.Algorithms.DataAnalysis
- Property svn:mergeinfo changed
/trunk/sources/HeuristicLab.Algorithms.DataAnalysis merged: 14314-14316,14319,14322
- Property svn:mergeinfo changed
-
branches/symbreg-factors-2650/HeuristicLab.Algorithms.DataAnalysis/3.4/GradientBoostedTrees/GradientBoostedTreesModelSurrogate.cs
r14239 r14330 21 21 #endregion 22 22 23 using System; 23 24 using System.Collections.Generic; 25 using System.Diagnostics.Eventing.Reader; 24 26 using System.Linq; 25 27 using HeuristicLab.Common; … … 36 38 public sealed class GradientBoostedTreesModelSurrogate : RegressionModel, IGradientBoostedTreesModel { 37 39 // don't store the actual model! 38 private IGradientBoostedTreesModel actualModel; // the actual model is only recalculated when necessary 40 // the actual model is only recalculated when necessary 41 private readonly Lazy<IGradientBoostedTreesModel> actualModel; 42 private IGradientBoostedTreesModel ActualModel { 43 get { return actualModel.Value; } 44 } 39 45 40 46 [Storable] … … 57 63 58 64 public override IEnumerable<string> VariablesUsedForPrediction { 59 get 60 { 61 lock (actualModel) { if (actualModel == null) actualModel = RecalculateModel(); } 62 return actualModel.Models.SelectMany(x => x.VariablesUsedForPrediction).Distinct().OrderBy(x => x); 65 get { 66 return ActualModel.Models.SelectMany(x => x.VariablesUsedForPrediction).Distinct().OrderBy(x => x); 63 67 } 64 68 } 65 69 66 70 [StorableConstructor] 67 private GradientBoostedTreesModelSurrogate(bool deserializing) : base(deserializing) { } 71 private GradientBoostedTreesModelSurrogate(bool deserializing) 72 : base(deserializing) { 73 actualModel = new Lazy<IGradientBoostedTreesModel>(() => RecalculateModel()); 74 } 68 75 69 76 private GradientBoostedTreesModelSurrogate(GradientBoostedTreesModelSurrogate original, Cloner cloner) 70 77 : base(original, cloner) { 71 if (original.actualModel != null) this.actualModel = cloner.Clone(original.actualModel); 78 IGradientBoostedTreesModel clonedModel = null; 79 if (original.ActualModel != null) clonedModel = cloner.Clone(original.ActualModel); 80 actualModel = new Lazy<IGradientBoostedTreesModel>(CreateLazyInitFunc(clonedModel)); // only capture clonedModel in the closure 72 81 73 82 this.trainingProblemData = cloner.Clone(original.trainingProblemData); … … 79 88 this.m = original.m; 80 89 this.nu = original.nu; 90 } 91 92 private Func<IGradientBoostedTreesModel> CreateLazyInitFunc(IGradientBoostedTreesModel clonedModel) { 93 return () => { 94 return clonedModel == null ? RecalculateModel() : clonedModel; 95 }; 81 96 } 82 97 … … 100 115 IGradientBoostedTreesModel model) 101 116 : this(trainingProblemData, seed, lossFunction, iterations, maxSize, r, m, nu) { 102 this.actualModel = model;117 actualModel = new Lazy<IGradientBoostedTreesModel>(() => model); 103 118 } 104 119 … … 109 124 // forward message to actual model (recalculate model first if necessary) 110 125 public override IEnumerable<double> GetEstimatedValues(IDataset dataset, IEnumerable<int> rows) { 111 lock (actualModel) { if (actualModel == null) actualModel = RecalculateModel(); } 112 return actualModel.GetEstimatedValues(dataset, rows); 126 return ActualModel.GetEstimatedValues(dataset, rows); 113 127 } 114 128 … … 123 137 public IEnumerable<IRegressionModel> Models { 124 138 get { 125 lock(actualModel) { if (actualModel == null) actualModel = RecalculateModel();} 126 return actualModel.Models; 139 return ActualModel.Models; 127 140 } 128 141 } … … 130 143 public IEnumerable<double> Weights { 131 144 get { 132 lock(actualModel) { if (actualModel == null) actualModel = RecalculateModel();} 133 return actualModel.Weights; 145 return ActualModel.Weights; 134 146 } 135 147 } -
branches/symbreg-factors-2650/HeuristicLab.Algorithms.DataAnalysis/3.4/NearestNeighbour/NearestNeighbourModel.cs
r14239 r14330 36 36 public sealed class NearestNeighbourModel : ClassificationModel, INearestNeighbourModel { 37 37 38 private readonly object kdTreeLockObject = new object(); 38 39 private alglib.nearestneighbor.kdtree kdTree; 39 40 public alglib.nearestneighbor.kdtree KDTree { … … 47 48 } 48 49 } 50 49 51 50 52 public override IEnumerable<string> VariablesUsedForPrediction { … … 201 203 } 202 204 int numNeighbours; 203 lock (kdTree ) { // gkronber: the following calls change the kdTree data structure205 lock (kdTreeLockObject) { // gkronber: the following calls change the kdTree data structure 204 206 numNeighbours = alglib.nearestneighbor.kdtreequeryknn(kdTree, x, k, false); 205 207 alglib.nearestneighbor.kdtreequeryresultsdistances(kdTree, ref dists); … … 237 239 } 238 240 int numNeighbours; 239 lock (kdTree ) {241 lock (kdTreeLockObject) { 240 242 // gkronber: the following calls change the kdTree data structure 241 243 numNeighbours = alglib.nearestneighbor.kdtreequeryknn(kdTree, x, k, false); -
branches/symbreg-factors-2650/HeuristicLab.Algorithms.DataAnalysis/3.4/NonlinearRegression/NonlinearRegression.cs
r14277 r14330 21 21 22 22 using System; 23 using System.Collections.Generic;24 23 using System.Linq; 24 using HeuristicLab.Analysis; 25 25 using HeuristicLab.Common; 26 26 using HeuristicLab.Core; 27 27 using HeuristicLab.Data; 28 using HeuristicLab.Optimization; 28 29 using HeuristicLab.Parameters; 29 30 using HeuristicLab.Optimization; … … 48 49 private const string SetSeedRandomlyParameterName = "SetSeedRandomly"; 49 50 private const string SeedParameterName = "Seed"; 51 private const string InitParamsRandomlyParameterName = "InitializeParametersRandomly"; 50 52 51 53 public IFixedValueParameter<StringValue> ModelStructureParameter { … … 66 68 public IFixedValueParameter<IntValue> RestartsParameter { 67 69 get { return (IFixedValueParameter<IntValue>)Parameters[RestartsParameterName]; } 70 } 71 72 public IFixedValueParameter<BoolValue> InitParametersRandomlyParameter { 73 get { return (IFixedValueParameter<BoolValue>)Parameters[InitParamsRandomlyParameterName]; } 68 74 } 69 75 … … 91 97 get { return SetSeedRandomlyParameter.Value.Value; } 92 98 set { SetSeedRandomlyParameter.Value.Value = value; } 99 } 100 101 public bool InitializeParametersRandomly { 102 get { return InitParametersRandomlyParameter.Value.Value; } 103 set { InitParametersRandomlyParameter.Value.Value = value; } 93 104 } 94 105 … … 103 114 Parameters.Add(new FixedValueParameter<StringValue>(ModelStructureParameterName, "The function for which the parameters must be fit (only numeric constants are tuned).", new StringValue("1.0 * x*x + 0.0"))); 104 115 Parameters.Add(new FixedValueParameter<IntValue>(IterationsParameterName, "The maximum number of iterations for constants optimization.", new IntValue(200))); 105 Parameters.Add(new FixedValueParameter<IntValue>(RestartsParameterName, "The number of independent random restarts ", new IntValue(10)));116 Parameters.Add(new FixedValueParameter<IntValue>(RestartsParameterName, "The number of independent random restarts (>0)", new IntValue(10))); 106 117 Parameters.Add(new FixedValueParameter<IntValue>(SeedParameterName, "The PRNG seed value.", new IntValue())); 107 118 Parameters.Add(new FixedValueParameter<BoolValue>(SetSeedRandomlyParameterName, "Switch to determine if the random number seed should be initialized randomly.", new BoolValue(true))); 119 Parameters.Add(new FixedValueParameter<BoolValue>(InitParamsRandomlyParameterName, "Switch to determine if the real-valued model parameters should be initialized randomly in each restart.", new BoolValue(false))); 120 121 SetParameterHiddenState(); 122 123 InitParametersRandomlyParameter.Value.ValueChanged += (sender, args) => { 124 SetParameterHiddenState(); 125 }; 126 } 127 128 private void SetParameterHiddenState() { 129 var hide = !InitializeParametersRandomly; 130 RestartsParameter.Hidden = hide; 131 SeedParameter.Hidden = hide; 132 SetSeedRandomlyParameter.Hidden = hide; 108 133 } 109 134 … … 118 143 if (!Parameters.ContainsKey(SetSeedRandomlyParameterName)) 119 144 Parameters.Add(new FixedValueParameter<BoolValue>(SetSeedRandomlyParameterName, "Switch to determine if the random number seed should be initialized randomly.", new BoolValue(true))); 145 if (!Parameters.ContainsKey(InitParamsRandomlyParameterName)) 146 Parameters.Add(new FixedValueParameter<BoolValue>(InitParamsRandomlyParameterName, "Switch to determine if the numeric parameters of the model should be initialized randomly.", new BoolValue(false))); 147 148 SetParameterHiddenState(); 149 InitParametersRandomlyParameter.Value.ValueChanged += (sender, args) => { 150 SetParameterHiddenState(); 151 }; 120 152 #endregion 121 153 } … … 127 159 #region nonlinear regression 128 160 protected override void Run() { 129 if (SetSeedRandomly) Seed = (new System.Random()).Next();130 var rand = new MersenneTwister((uint)Seed);131 161 IRegressionSolution bestSolution = null; 132 for (int r = 0; r < Restarts; r++) { 133 var solution = CreateRegressionSolution(Problem.ProblemData, ModelStructure, Iterations, rand); 134 if (bestSolution == null || solution.TrainingRootMeanSquaredError < bestSolution.TrainingRootMeanSquaredError) { 135 bestSolution = solution; 162 if (InitializeParametersRandomly) { 163 var qualityTable = new DataTable("RMSE table"); 164 qualityTable.VisualProperties.YAxisLogScale = true; 165 var trainRMSERow = new DataRow("RMSE (train)"); 166 trainRMSERow.VisualProperties.ChartType = DataRowVisualProperties.DataRowChartType.Points; 167 var testRMSERow = new DataRow("RMSE test"); 168 testRMSERow.VisualProperties.ChartType = DataRowVisualProperties.DataRowChartType.Points; 169 170 qualityTable.Rows.Add(trainRMSERow); 171 qualityTable.Rows.Add(testRMSERow); 172 Results.Add(new Result(qualityTable.Name, qualityTable.Name + " for all restarts", qualityTable)); 173 if (SetSeedRandomly) Seed = (new System.Random()).Next(); 174 var rand = new MersenneTwister((uint)Seed); 175 bestSolution = CreateRegressionSolution(Problem.ProblemData, ModelStructure, Iterations, rand); 176 trainRMSERow.Values.Add(bestSolution.TrainingRootMeanSquaredError); 177 testRMSERow.Values.Add(bestSolution.TestRootMeanSquaredError); 178 for (int r = 0; r < Restarts; r++) { 179 var solution = CreateRegressionSolution(Problem.ProblemData, ModelStructure, Iterations, rand); 180 trainRMSERow.Values.Add(solution.TrainingRootMeanSquaredError); 181 testRMSERow.Values.Add(solution.TestRootMeanSquaredError); 182 if (solution.TrainingRootMeanSquaredError < bestSolution.TrainingRootMeanSquaredError) { 183 bestSolution = solution; 184 } 136 185 } 186 } else { 187 bestSolution = CreateRegressionSolution(Problem.ProblemData, ModelStructure, Iterations); 137 188 } 138 189 … … 148 199 /// The starting point for the numeric constants is initialized randomly if a random number generator is specified (~N(0,1)). Otherwise the user specified constants are 149 200 /// used as a starting point. 150 /// </summary> 201 /// </summary>- 151 202 /// <param name="problemData">Training and test data</param> 152 203 /// <param name="modelStructure">The function as infix expression</param> … … 154 205 /// <param name="random">Optional random number generator for random initialization of numeric constants.</param> 155 206 /// <returns></returns> 156 public static ISymbolicRegressionSolution CreateRegressionSolution(IRegressionProblemData problemData, string modelStructure, int maxIterations, IRandom rand om= null) {207 public static ISymbolicRegressionSolution CreateRegressionSolution(IRegressionProblemData problemData, string modelStructure, int maxIterations, IRandom rand = null) { 157 208 var parser = new InfixExpressionParser(); 158 209 var tree = parser.Parse(modelStructure); … … 183 234 184 235 // initialize constants randomly 185 if (rand om!= null) {236 if (rand != null) { 186 237 foreach (var node in tree.IterateNodesPrefix().OfType<ConstantTreeNode>()) { 187 node.Value = NormalDistributedRandom.NextDouble(random, 0, 1); 238 double f = Math.Exp(NormalDistributedRandom.NextDouble(rand, 0, 1)); 239 double s = rand.NextDouble() < 0.5 ? -1 : 1; 240 node.Value = s * node.Value * f; 188 241 } 189 242 } -
branches/symbreg-factors-2650/HeuristicLab.Data.Views
- Property svn:mergeinfo changed
/trunk/sources/HeuristicLab.Data.Views (added) merged: 14299
- Property svn:mergeinfo changed
-
branches/symbreg-factors-2650/HeuristicLab.Data.Views/3.3
- Property svn:mergeinfo changed
/trunk/sources/HeuristicLab.Data.Views/3.3 (added) merged: 14299
- Property svn:mergeinfo changed
-
branches/symbreg-factors-2650/HeuristicLab.Data.Views/3.3/StringConvertibleArrayView.Designer.cs
r14185 r14330 65 65 // lengthTextBox 66 66 // 67 this.lengthTextBox.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 68 67 this.lengthTextBox.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 68 | System.Windows.Forms.AnchorStyles.Right))); 69 69 this.lengthTextBox.Location = new System.Drawing.Point(52, 0); 70 70 this.lengthTextBox.Name = "lengthTextBox"; … … 79 79 this.dataGridView.AllowUserToAddRows = false; 80 80 this.dataGridView.AllowUserToDeleteRows = false; 81 this.dataGridView.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 82 | System.Windows.Forms.AnchorStyles.Left)83 84 this.dataGridView.ClipboardCopyMode = System.Windows.Forms.DataGridViewClipboardCopyMode. EnableWithoutHeaderText;81 this.dataGridView.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 82 | System.Windows.Forms.AnchorStyles.Left) 83 | System.Windows.Forms.AnchorStyles.Right))); 84 this.dataGridView.ClipboardCopyMode = System.Windows.Forms.DataGridViewClipboardCopyMode.Disable; 85 85 this.dataGridView.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize; 86 86 this.dataGridView.ColumnHeadersVisible = false; … … 101 101 // StringConvertibleArrayView 102 102 // 103 this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);104 103 this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Inherit; 105 104 this.Controls.Add(this.dataGridView); -
branches/symbreg-factors-2650/HeuristicLab.Data.Views/3.3/StringConvertibleArrayView.cs
r14185 r14330 23 23 using System.ComponentModel; 24 24 using System.Drawing; 25 using System.Linq; 25 26 using System.Text; 26 27 using System.Windows.Forms; … … 203 204 DataGridViewCell cell = dataGridView[column.Index, i]; 204 205 if (cell.Selected) { 206 s.Append(Content.ElementNames.ElementAt(i)); 207 s.Append("\t"); 205 208 s.Append(Content.GetValue(i)); 206 209 s.Append(Environment.NewLine); -
branches/symbreg-factors-2650/HeuristicLab.MainForm.WindowsForms/3.3/Controls/ProgressView.cs
r14185 r14330 141 141 142 142 private void UpdateProgressValue() { 143 if (InvokeRequired) Invoke((Action)UpdateProgressValue); 144 else { 143 // prevent problems with object disposal and invoke as suggested by http://stackoverflow.com/a/18647091 144 if (!IsHandleCreated) return; 145 if (InvokeRequired) { 146 try { 147 Invoke((Action)UpdateProgressValue); 148 } 149 catch (InvalidOperationException) { 150 // swallow ObjectDisposedException 151 // which might occur if the invoke call is executed after or while the control is disposing 152 } 153 } else { 145 154 if (content != null) { 146 155 double progressValue = content.ProgressValue; … … 149 158 } else { 150 159 progressBar.Style = ProgressBarStyle.Blocks; 151 progressBar.Value = (int)Math.Round(progressBar.Minimum + progressValue * (progressBar.Maximum - progressBar.Minimum)); 160 progressBar.Value = 161 (int)Math.Round(progressBar.Minimum + progressValue * (progressBar.Maximum - progressBar.Minimum)); 152 162 } 153 163 } -
branches/symbreg-factors-2650/HeuristicLab.Problems.DataAnalysis
- Property svn:mergeinfo changed
/trunk/sources/HeuristicLab.Problems.DataAnalysis merged: 14289-14290,14292-14295
- Property svn:mergeinfo changed
-
branches/symbreg-factors-2650/HeuristicLab.Problems.DataAnalysis.Symbolic
- Property svn:mergeinfo changed
/trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic (added) merged: 14282,14319
- Property svn:mergeinfo changed
-
branches/symbreg-factors-2650/HeuristicLab.Problems.DataAnalysis.Symbolic.Classification
- Property svn:mergeinfo changed
/trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic.Classification (added) merged: 14289-14290
- Property svn:mergeinfo changed
-
branches/symbreg-factors-2650/HeuristicLab.Problems.DataAnalysis.Symbolic.Classification/3.4/SymbolicClassificationModel.cs
r14185 r14330 20 20 #endregion 21 21 22 using System; 22 23 using System.Collections.Generic; 23 24 using HeuristicLab.Common; … … 34 35 public abstract class SymbolicClassificationModel : SymbolicDataAnalysisModel, ISymbolicClassificationModel { 35 36 [Storable] 36 private readonlystring targetVariable;37 private string targetVariable; 37 38 public string TargetVariable { 38 39 get { return targetVariable; } 40 set { 41 if (string.IsNullOrEmpty(value) || targetVariable == value) return; 42 targetVariable = value; 43 OnTargetVariableChanged(this, EventArgs.Empty); 44 } 39 45 } 40 46 41 47 [StorableConstructor] 42 protected SymbolicClassificationModel(bool deserializing) : base(deserializing) { } 48 protected SymbolicClassificationModel(bool deserializing) 49 : base(deserializing) { 50 targetVariable = string.Empty; 51 } 43 52 44 53 protected SymbolicClassificationModel(SymbolicClassificationModel original, Cloner cloner) … … 64 73 Scale(problemData, problemData.TargetVariable); 65 74 } 75 76 #region events 77 public event EventHandler TargetVariableChanged; 78 private void OnTargetVariableChanged(object sender, EventArgs args) { 79 var changed = TargetVariableChanged; 80 if (changed != null) 81 changed(sender, args); 82 } 83 #endregion 66 84 } 67 85 } -
branches/symbreg-factors-2650/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression
- Property svn:mergeinfo changed
/trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression (added) merged: 14289-14290
- Property svn:mergeinfo changed
-
branches/symbreg-factors-2650/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4
- Property svn:mergeinfo changed
/trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4 (added) merged: 14289-14290
- Property svn:mergeinfo changed
-
branches/symbreg-factors-2650/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/SymbolicRegressionModel.cs
r14185 r14330 35 35 public class SymbolicRegressionModel : SymbolicDataAnalysisModel, ISymbolicRegressionModel { 36 36 [Storable] 37 private readonlystring targetVariable;37 private string targetVariable; 38 38 public string TargetVariable { 39 39 get { return targetVariable; } 40 set { 41 if (string.IsNullOrEmpty(value) || targetVariable == value) return; 42 targetVariable = value; 43 OnTargetVariableChanged(this, EventArgs.Empty); 44 } 40 45 } 41 46 42 47 [StorableConstructor] 43 protected SymbolicRegressionModel(bool deserializing) : base(deserializing) { } 48 protected SymbolicRegressionModel(bool deserializing) 49 : base(deserializing) { 50 targetVariable = string.Empty; 51 } 44 52 45 53 protected SymbolicRegressionModel(SymbolicRegressionModel original, Cloner cloner) … … 74 82 Scale(problemData, problemData.TargetVariable); 75 83 } 84 85 #region events 86 public event EventHandler TargetVariableChanged; 87 private void OnTargetVariableChanged(object sender, EventArgs args) { 88 var changed = TargetVariableChanged; 89 if (changed != null) 90 changed(sender, args); 91 } 92 #endregion 76 93 } 77 94 } -
branches/symbreg-factors-2650/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Importer/InfixExpressionParser.cs
r14251 r14330 226 226 pos++; 227 227 yield return new Token { TokenType = TokenType.Eq, strVal = "=" }; 228 } else { 229 throw new ArgumentException("Invalid character: " + str[pos]); 228 230 } 229 231 } -
branches/symbreg-factors-2650/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Interpreter/SymbolicDataAnalysisExpressionTreeLinearInterpreter.cs
r14249 r14330 38 38 private const string EvaluatedSolutionsParameterName = "EvaluatedSolutions"; 39 39 40 private SymbolicDataAnalysisExpressionTreeInterpreter interpreter;40 private readonly SymbolicDataAnalysisExpressionTreeInterpreter interpreter; 41 41 42 42 public override bool CanChangeName { … … 72 72 private SymbolicDataAnalysisExpressionTreeLinearInterpreter(bool deserializing) 73 73 : base(deserializing) { 74 interpreter = new SymbolicDataAnalysisExpressionTreeInterpreter(); 74 75 } 75 76 -
branches/symbreg-factors-2650/HeuristicLab.Problems.DataAnalysis.Views
- Property svn:mergeinfo changed
/trunk/sources/HeuristicLab.Problems.DataAnalysis.Views (added) merged: 14300,14307
- Property svn:mergeinfo changed
-
branches/symbreg-factors-2650/HeuristicLab.Problems.DataAnalysis.Views/3.4/FeatureCorrelation/TimeframeFeatureCorrelationCalculator.cs
r13938 r14330 72 72 } 73 73 74 var var1 = dataset.GetDoubleValues(variable, indices).ToArray(); 75 74 76 for (int i = 0; i < length; i++) { 75 77 for (int j = start; j <= frames; j++) { … … 80 82 } 81 83 82 IEnumerable<double> var1 = dataset.GetDoubleValues(variable, indices);83 84 IEnumerable<double> var2 = dataset.GetDoubleValues(doubleVariableNames[i], indices); 84 85 85 var valuesInFrame = var1.Take(j);86 var help = var1.Skip(j).ToList();87 help.AddRange(valuesInFrame);88 var1 = help;89 90 86 var error = OnlineCalculatorError.None; 91 elements[i, j] = calc.Calculate(var1 , var2, out error);87 elements[i, j] = calc.Calculate(var1.Skip(j), var2.Take(var1.Length-j), out error); 92 88 93 89 if (!error.Equals(OnlineCalculatorError.None)) { -
branches/symbreg-factors-2650/HeuristicLab.Problems.DataAnalysis.Views/3.4/Regression/RegressionSolutionTargetResponseGradientView.cs
r14277 r14330 125 125 126 126 if (sharedFixedVariables != null) 127 sharedFixedVariables.ItemChanged += SharedFixedVariables_ItemChanged;127 sharedFixedVariables.ItemChanged -= SharedFixedVariables_ItemChanged; 128 128 129 129 sharedFixedVariables = new ModifiableDataset(doubleVariables.Concat(factorVariables), doubleVariableValues.Concat(factorVariableValues)); -
branches/symbreg-factors-2650/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/Classification/ClassificationModel.cs
r13992 r14330 20 20 #endregion 21 21 22 using System; 22 23 using System.Collections.Generic; 23 24 using HeuristicLab.Common; … … 33 34 public string TargetVariable { 34 35 get { return targetVariable; } 35 protected set { targetVariable = value; } 36 set { 37 if (string.IsNullOrEmpty(value) || targetVariable == value) return; 38 targetVariable = value; 39 OnTargetVariableChanged(this, EventArgs.Empty); 40 } 36 41 } 37 42 38 protected ClassificationModel(bool deserializing) : base(deserializing) { } 43 protected ClassificationModel(bool deserializing) 44 : base(deserializing) { 45 targetVariable = string.Empty; 46 } 39 47 protected ClassificationModel(ClassificationModel original, Cloner cloner) 40 48 : base(original, cloner) { … … 55 63 } 56 64 57 [StorableHook(HookType.AfterDeserialization)]58 private void AfterDeserialization() {59 // BackwardsCompatibility3.360 #region Backwards compatible code, remove with 3.461 targetVariable = string.Empty;62 #endregion63 }64 65 65 public abstract IEnumerable<double> GetEstimatedClassValues(IDataset dataset, IEnumerable<int> rows); 66 66 public abstract IClassificationSolution CreateClassificationSolution(IClassificationProblemData problemData); 67 68 #region events 69 public event EventHandler TargetVariableChanged; 70 private void OnTargetVariableChanged(object sender, EventArgs args) { 71 var changed = TargetVariableChanged; 72 if (changed != null) 73 changed(sender, args); 74 } 75 #endregion 67 76 } 68 77 } -
branches/symbreg-factors-2650/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/Classification/ClassificationSolutionBase.cs
r14185 r14330 88 88 [StorableHook(HookType.AfterDeserialization)] 89 89 private void AfterDeserialization() { 90 if (string.IsNullOrEmpty(Model.TargetVariable)) 91 Model.TargetVariable = this.ProblemData.TargetVariable; 92 90 93 if (!this.ContainsKey(TrainingNormalizedGiniCoefficientResultName)) 91 94 Add(new Result(TrainingNormalizedGiniCoefficientResultName, "Normalized Gini coefficient of the model on the training partition.", new DoubleValue())); -
branches/symbreg-factors-2650/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/Regression/RegressionModel.cs
r13992 r14330 20 20 #endregion 21 21 22 using System; 22 23 using System.Collections.Generic; 23 24 using HeuristicLab.Common; … … 33 34 public string TargetVariable { 34 35 get { return targetVariable; } 35 protected set { targetVariable = value; } 36 set { 37 if (string.IsNullOrEmpty(value) || targetVariable == value) return; 38 targetVariable = value; 39 OnTargetVariableChanged(this, EventArgs.Empty); 40 } 36 41 } 37 42 38 protected RegressionModel(bool deserializing) : base(deserializing) { } 43 protected RegressionModel(bool deserializing) 44 : base(deserializing) { 45 targetVariable = string.Empty; 46 } 39 47 40 48 protected RegressionModel(RegressionModel original, Cloner cloner) … … 56 64 } 57 65 58 [StorableHook(HookType.AfterDeserialization)]59 private void AfterDeserialization() {60 // BackwardsCompatibility3.361 #region Backwards compatible code, remove with 3.462 targetVariable = string.Empty;63 #endregion64 }65 66 public abstract IEnumerable<double> GetEstimatedValues(IDataset dataset, IEnumerable<int> rows); 66 67 public abstract IRegressionSolution CreateRegressionSolution(IRegressionProblemData problemData); 68 69 #region events 70 public event EventHandler TargetVariableChanged; 71 private void OnTargetVariableChanged(object sender, EventArgs args) { 72 var changed = TargetVariableChanged; 73 if (changed != null) 74 changed(sender, args); 75 } 76 #endregion 67 77 } 68 78 } -
branches/symbreg-factors-2650/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/Regression/RegressionSolutionBase.cs
r14185 r14330 176 176 [StorableHook(HookType.AfterDeserialization)] 177 177 private void AfterDeserialization() { 178 if (string.IsNullOrEmpty(Model.TargetVariable)) 179 Model.TargetVariable = this.ProblemData.TargetVariable; 180 178 181 // BackwardsCompatibility3.4 179 182 #region Backwards compatible code, remove with 3.5 … … 230 233 231 234 double trainingR = OnlinePearsonsRCalculator.Calculate(originalTrainingValues, estimatedTrainingValues, out errorState); 232 TrainingRSquared = errorState == OnlineCalculatorError.None ? trainingR *trainingR : double.NaN;235 TrainingRSquared = errorState == OnlineCalculatorError.None ? trainingR * trainingR : double.NaN; 233 236 double testR = OnlinePearsonsRCalculator.Calculate(originalTestValues, estimatedTestValues, out errorState); 234 TestRSquared = errorState == OnlineCalculatorError.None ? testR *testR : double.NaN;237 TestRSquared = errorState == OnlineCalculatorError.None ? testR * testR : double.NaN; 235 238 236 239 double trainingRelError = OnlineMeanAbsolutePercentageErrorCalculator.Calculate(originalTrainingValues, estimatedTrainingValues, out errorState); -
branches/symbreg-factors-2650/HeuristicLab.Problems.DataAnalysis/3.4/Interfaces/Classification/IClassificationModel.cs
r14239 r14330 20 20 #endregion 21 21 22 using System; 22 23 using System.Collections.Generic; 24 23 25 namespace HeuristicLab.Problems.DataAnalysis { 24 26 /// <summary> … … 29 31 IEnumerable<double> GetEstimatedClassValues(IDataset dataset, IEnumerable<int> rows); 30 32 IClassificationSolution CreateClassificationSolution(IClassificationProblemData problemData); 31 string TargetVariable { get; } 33 string TargetVariable { get; set; } 34 event EventHandler TargetVariableChanged; 32 35 } 33 36 } -
branches/symbreg-factors-2650/HeuristicLab.Problems.DataAnalysis/3.4/Interfaces/Regression/IRegressionModel.cs
r14239 r14330 20 20 #endregion 21 21 22 using System; 22 23 using System.Collections.Generic; 23 24 … … 30 31 IEnumerable<double> GetEstimatedValues(IDataset dataset, IEnumerable<int> rows); 31 32 IRegressionSolution CreateRegressionSolution(IRegressionProblemData problemData); 32 string TargetVariable { get; } 33 string TargetVariable { get; set; } 34 event EventHandler TargetVariableChanged; 33 35 } 34 36 } -
branches/symbreg-factors-2650/HeuristicLab.Problems.DataAnalysis/3.4/OnlineCalculators/ClassificationPerformanceMeasuresCalculator.cs
r14185 r14330 23 23 using System.Collections.Generic; 24 24 using HeuristicLab.Common; 25 using HeuristicLab.Problems.DataAnalysis.OnlineCalculators;26 25 27 26 namespace HeuristicLab.Problems.DataAnalysis { 28 public class ClassificationPerformanceMeasuresCalculator {27 public class ClassificationPerformanceMeasuresCalculator : IDeepCloneable { 29 28 30 29 public ClassificationPerformanceMeasuresCalculator(string positiveClassName, double positiveClassValue) { … … 32 31 this.positiveClassValue = positiveClassValue; 33 32 Reset(); 33 } 34 35 protected ClassificationPerformanceMeasuresCalculator(ClassificationPerformanceMeasuresCalculator original, Cloner cloner = null) { 36 positiveClassName = original.positiveClassName; 37 positiveClassValue = original.positiveClassValue; 38 truePositiveCount = original.truePositiveCount; 39 falsePositiveCount = original.falsePositiveCount; 40 trueNegativeCount = original.trueNegativeCount; 41 falseNegativeCount = original.falseNegativeCount; 42 errorState = original.errorState; 34 43 } 35 44 … … 138 147 errorState = ErrorState; 139 148 } 149 150 // IDeepCloneable interface members 151 public object Clone() { 152 return new ClassificationPerformanceMeasuresCalculator(this); 153 } 154 155 public IDeepCloneable Clone(Cloner cloner) { 156 var clone = cloner.GetClone(this); 157 if (clone == null) { 158 clone = new ClassificationPerformanceMeasuresCalculator(this); 159 cloner.RegisterClonedObject(this, clone); 160 } 161 return clone; 162 } 140 163 } 141 164 } -
branches/symbreg-factors-2650/HeuristicLab.Problems.DataAnalysis/3.4/OnlineCalculators/OnlineAccuracyCalculator.cs
r14185 r14330 25 25 26 26 namespace HeuristicLab.Problems.DataAnalysis { 27 public class OnlineAccuracyCalculator : IOnlineCalculator {27 public class OnlineAccuracyCalculator : IOnlineCalculator, IDeepCloneable { 28 28 29 29 private int correctlyClassified; … … 37 37 public OnlineAccuracyCalculator() { 38 38 Reset(); 39 } 40 41 // private constructor used internally by the Clone() method 42 protected OnlineAccuracyCalculator(OnlineAccuracyCalculator other, Cloner cloner = null) { 43 correctlyClassified = other.correctlyClassified; 44 n = other.n; 45 errorState = other.errorState; 39 46 } 40 47 … … 90 97 } 91 98 } 99 100 // IDeepCloneable interface members 101 public object Clone() { 102 return new OnlineAccuracyCalculator(this); 103 } 104 105 public IDeepCloneable Clone(Cloner cloner) { 106 var clone = cloner.GetClone(this); 107 if (clone == null) { 108 clone = new OnlineAccuracyCalculator(this); 109 cloner.RegisterClonedObject(this, clone); 110 } 111 return clone; 112 } 92 113 } 93 114 } -
branches/symbreg-factors-2650/HeuristicLab.Problems.DataAnalysis/3.4/OnlineCalculators/OnlineBoundedMeanSquaredErrorCalculator.cs
r14185 r14330 22 22 using System; 23 23 using System.Collections.Generic; 24 using HeuristicLab.Common; 24 25 25 26 namespace HeuristicLab.Problems.DataAnalysis { 26 public class OnlineBoundedMeanSquaredErrorCalculator : IOnlineCalculator {27 public class OnlineBoundedMeanSquaredErrorCalculator : IOnlineCalculator, IDeepCloneable { 27 28 28 29 private double errorSum; … … 38 39 39 40 40 public OnlineBoundedMeanSquaredErrorCalculator(double lowerBound, double upper bound) {41 public OnlineBoundedMeanSquaredErrorCalculator(double lowerBound, double upperBound) { 41 42 LowerBound = lowerBound; 42 UpperBound = upper bound;43 UpperBound = upperBound; 43 44 Reset(); 45 } 46 47 protected OnlineBoundedMeanSquaredErrorCalculator(OnlineBoundedMeanSquaredErrorCalculator original, Cloner cloner = null) { 48 LowerBound = original.LowerBound; 49 UpperBound = original.UpperBound; 50 n = original.n; 51 errorSum = original.errorSum; 52 errorState = original.ErrorState; 44 53 } 45 54 … … 96 105 } 97 106 } 107 108 // IDeepCloneable interface members 109 public object Clone() { 110 return new OnlineBoundedMeanSquaredErrorCalculator(this); 111 } 112 113 public IDeepCloneable Clone(Cloner cloner) { 114 var clone = cloner.GetClone(this); 115 if (clone == null) { 116 clone = new OnlineBoundedMeanSquaredErrorCalculator(this); 117 cloner.RegisterClonedObject(this, clone); 118 } 119 return clone; 120 } 98 121 } 99 122 } -
branches/symbreg-factors-2650/HeuristicLab.Problems.DataAnalysis/3.4/OnlineCalculators/OnlineCovarianceCalculator.cs
r14185 r14330 22 22 using System; 23 23 using System.Collections.Generic; 24 using HeuristicLab.Common; 24 25 25 26 namespace HeuristicLab.Problems.DataAnalysis { 26 public class OnlineCovarianceCalculator : IOnlineCalculator {27 public class OnlineCovarianceCalculator : IOnlineCalculator, IDeepCloneable { 27 28 28 29 private double xMean, yMean, Cn; … … 36 37 public OnlineCovarianceCalculator() { 37 38 Reset(); 39 } 40 41 protected OnlineCovarianceCalculator(OnlineCovarianceCalculator other, Cloner cloner = null) { 42 Cn = other.Cn; 43 xMean = other.xMean; 44 yMean = other.yMean; 45 n = other.n; 46 errorState = other.errorState; 38 47 } 39 48 … … 94 103 } 95 104 } 105 106 // IDeepCloneable interface members 107 public object Clone() { 108 return new OnlineCovarianceCalculator(this); 109 } 110 111 public IDeepCloneable Clone(Cloner cloner) { 112 var clone = cloner.GetClone(this); 113 if (clone == null) { 114 clone = new OnlineCovarianceCalculator(this); 115 cloner.RegisterClonedObject(this, clone); 116 } 117 return clone; 118 } 96 119 } 97 120 } -
branches/symbreg-factors-2650/HeuristicLab.Problems.DataAnalysis/3.4/OnlineCalculators/OnlineDirectionalSymmetryCalculator.cs
r14185 r14330 26 26 27 27 namespace HeuristicLab.Problems.DataAnalysis { 28 public class OnlineDirectionalSymmetryCalculator : IOnlineTimeSeriesCalculator {28 public class OnlineDirectionalSymmetryCalculator : IOnlineTimeSeriesCalculator, IDeepCloneable { 29 29 private int n; 30 30 private int nCorrect; … … 39 39 public OnlineDirectionalSymmetryCalculator() { 40 40 Reset(); 41 } 42 43 protected OnlineDirectionalSymmetryCalculator(OnlineDirectionalSymmetryCalculator other, Cloner cloner = null) { 44 n = other.n; 45 nCorrect = other.nCorrect; 46 errorState = other.errorState; 41 47 } 42 48 … … 94 100 return dsCalculator.DirectionalSymmetry; 95 101 } 96 102 97 103 public static double Calculate(IEnumerable<double> startValues, IEnumerable<IEnumerable<double>> actualContinuations, IEnumerable<IEnumerable<double>> predictedContinuations, out OnlineCalculatorError errorState) { 98 104 IEnumerator<double> startValueEnumerator = startValues.GetEnumerator(); … … 116 122 } 117 123 } 124 125 // IDeepCloneable interface members 126 public object Clone() { 127 return new OnlineDirectionalSymmetryCalculator(this); 128 } 129 130 public IDeepCloneable Clone(Cloner cloner) { 131 var clone = cloner.GetClone(this); 132 if (clone == null) { 133 clone = new OnlineDirectionalSymmetryCalculator(this); 134 cloner.RegisterClonedObject(this, clone); 135 } 136 return clone; 137 } 118 138 } 119 139 } -
branches/symbreg-factors-2650/HeuristicLab.Problems.DataAnalysis/3.4/OnlineCalculators/OnlineLinearScalingParameterCalculator.cs
r14185 r14330 25 25 26 26 namespace HeuristicLab.Problems.DataAnalysis { 27 public class OnlineLinearScalingParameterCalculator {27 public class OnlineLinearScalingParameterCalculator : IDeepCloneable { 28 28 29 29 /// <summary> … … 65 65 Reset(); 66 66 } 67 68 protected OnlineLinearScalingParameterCalculator(OnlineLinearScalingParameterCalculator other, Cloner cloner) { 69 targetMeanCalculator = (OnlineMeanAndVarianceCalculator)other.targetMeanCalculator.Clone(cloner); 70 originalMeanAndVarianceCalculator = (OnlineMeanAndVarianceCalculator)other.originalMeanAndVarianceCalculator.Clone(cloner); 71 originalTargetCovarianceCalculator = (OnlineCovarianceCalculator)other.originalTargetCovarianceCalculator.Clone(cloner); 72 // do not reset the calculators here 73 } 74 67 75 68 76 public void Reset() { … … 117 125 } 118 126 } 127 128 // IDeepCloneable interface members 129 public object Clone() { 130 var cloner = new Cloner(); 131 return new OnlineLinearScalingParameterCalculator(this, cloner); 132 } 133 134 public IDeepCloneable Clone(Cloner cloner) { 135 var clone = cloner.GetClone(this); 136 if (clone == null) { 137 clone = new OnlineLinearScalingParameterCalculator(this, cloner); 138 cloner.RegisterClonedObject(this, clone); 139 } 140 return clone; 141 } 119 142 } 120 143 } -
branches/symbreg-factors-2650/HeuristicLab.Problems.DataAnalysis/3.4/OnlineCalculators/OnlineMaxAbsoluteErrorCalculator.cs
r14185 r14330 22 22 using System; 23 23 using System.Collections.Generic; 24 using HeuristicLab.Common; 24 25 25 26 namespace HeuristicLab.Problems.DataAnalysis { 26 public class OnlineMaxAbsoluteErrorCalculator : IOnlineCalculator {27 public class OnlineMaxAbsoluteErrorCalculator : IOnlineCalculator, IDeepCloneable { 27 28 28 29 private double mae; … … 36 37 public OnlineMaxAbsoluteErrorCalculator() { 37 38 Reset(); 39 } 40 41 protected OnlineMaxAbsoluteErrorCalculator(OnlineMaxAbsoluteErrorCalculator other, Cloner cloner = null) { 42 mae = other.mae; 43 n = other.n; 44 errorState = other.errorState; 38 45 } 39 46 … … 88 95 } 89 96 } 97 98 // IDeepCloneable interface members 99 public object Clone() { 100 return new OnlineMaxAbsoluteErrorCalculator(this); 101 } 102 103 public IDeepCloneable Clone(Cloner cloner) { 104 var clone = cloner.GetClone(this); 105 if (clone == null) { 106 clone = new OnlineMaxAbsoluteErrorCalculator(this); 107 cloner.RegisterClonedObject(this, clone); 108 } 109 return clone; 110 } 90 111 } 91 112 } -
branches/symbreg-factors-2650/HeuristicLab.Problems.DataAnalysis/3.4/OnlineCalculators/OnlineMeanAbsoluteErrorCalculator.cs
r14185 r14330 22 22 using System; 23 23 using System.Collections.Generic; 24 using HeuristicLab.Common; 24 25 25 26 namespace HeuristicLab.Problems.DataAnalysis { 26 public class OnlineMeanAbsoluteErrorCalculator : IOnlineCalculator {27 public class OnlineMeanAbsoluteErrorCalculator : IOnlineCalculator, IDeepCloneable { 27 28 28 29 private double sae; … … 36 37 public OnlineMeanAbsoluteErrorCalculator() { 37 38 Reset(); 39 } 40 41 protected OnlineMeanAbsoluteErrorCalculator(OnlineMeanAbsoluteErrorCalculator other, Cloner cloner = null) { 42 sae = other.sae; 43 n = other.n; 44 errorState = other.errorState; 38 45 } 39 46 … … 87 94 } 88 95 } 96 97 // IDeepCloneable interface members 98 public object Clone() { 99 return new OnlineMeanAbsoluteErrorCalculator(this); 100 } 101 102 public IDeepCloneable Clone(Cloner cloner) { 103 var clone = cloner.GetClone(this); 104 if (clone == null) { 105 clone = new OnlineMeanAbsoluteErrorCalculator(this); 106 cloner.RegisterClonedObject(this, clone); 107 } 108 return clone; 109 } 89 110 } 90 111 } -
branches/symbreg-factors-2650/HeuristicLab.Problems.DataAnalysis/3.4/OnlineCalculators/OnlineMeanAbsolutePercentageErrorCalculator.cs
r14185 r14330 25 25 26 26 namespace HeuristicLab.Problems.DataAnalysis { 27 public class OnlineMeanAbsolutePercentageErrorCalculator : IOnlineCalculator {27 public class OnlineMeanAbsolutePercentageErrorCalculator : IOnlineCalculator, IDeepCloneable { 28 28 29 29 private double sre; … … 37 37 public OnlineMeanAbsolutePercentageErrorCalculator() { 38 38 Reset(); 39 } 40 41 protected OnlineMeanAbsolutePercentageErrorCalculator(OnlineMeanAbsolutePercentageErrorCalculator other, Cloner cloner = null) { 42 sre = other.sre; 43 n = other.n; 44 errorState = other.errorState; 39 45 } 40 46 … … 89 95 } 90 96 } 97 98 // IDeepCloneable interface members 99 public object Clone() { 100 return new OnlineMeanAbsolutePercentageErrorCalculator(this); 101 } 102 103 public IDeepCloneable Clone(Cloner cloner) { 104 var clone = cloner.GetClone(this); 105 if (clone == null) { 106 clone = new OnlineMeanAbsolutePercentageErrorCalculator(this); 107 cloner.RegisterClonedObject(this, clone); 108 } 109 return clone; 110 } 91 111 } 92 112 } -
branches/symbreg-factors-2650/HeuristicLab.Problems.DataAnalysis/3.4/OnlineCalculators/OnlineMeanAndVarianceCalculator.cs
r14185 r14330 21 21 22 22 using System.Collections.Generic; 23 using HeuristicLab.Common; 23 24 24 25 namespace HeuristicLab.Problems.DataAnalysis { 25 public class OnlineMeanAndVarianceCalculator {26 public class OnlineMeanAndVarianceCalculator : IDeepCloneable { 26 27 27 28 private double m_oldM, m_newM, m_oldS, m_newS; … … 66 67 } 67 68 69 protected OnlineMeanAndVarianceCalculator(OnlineMeanAndVarianceCalculator other, Cloner cloner = null) { 70 m_oldS = other.m_oldS; 71 m_oldM = other.m_oldM; 72 m_newS = other.m_newS; 73 m_newM = other.m_newM; 74 n = other.n; 75 errorState = other.errorState; 76 varianceErrorState = other.varianceErrorState; 77 } 78 68 79 public void Reset() { 69 80 n = 0; … … 75 86 if (double.IsNaN(x) || double.IsInfinity(x) || x > 1E13 || x < -1E13 || (errorState & OnlineCalculatorError.InvalidValueAdded) > 0) { 76 87 errorState = errorState | OnlineCalculatorError.InvalidValueAdded; 77 varianceErrorState = errorState | OnlineCalculatorError.InvalidValueAdded;88 varianceErrorState = varianceErrorState | OnlineCalculatorError.InvalidValueAdded; 78 89 } else { 79 90 n++; … … 106 117 varianceErrorState = meanAndVarianceCalculator.VarianceErrorState; 107 118 } 119 120 // IDeepCloneable members 121 public object Clone() { 122 return new OnlineMeanAndVarianceCalculator(this); 123 } 124 125 public IDeepCloneable Clone(Cloner cloner) { 126 var clone = cloner.GetClone(this); 127 if (clone == null) { 128 clone = new OnlineMeanAndVarianceCalculator(this); 129 cloner.RegisterClonedObject(this, clone); 130 } 131 return clone; 132 } 108 133 } 109 134 } -
branches/symbreg-factors-2650/HeuristicLab.Problems.DataAnalysis/3.4/OnlineCalculators/OnlineMeanErrorCalculator.cs
r14185 r14330 22 22 using System; 23 23 using System.Collections.Generic; 24 using HeuristicLab.Common; 24 25 25 26 namespace HeuristicLab.Problems.DataAnalysis { 26 public class OnlineMeanErrorCalculator : IOnlineCalculator {27 public class OnlineMeanErrorCalculator : IOnlineCalculator, IDeepCloneable { 27 28 28 29 private readonly OnlineMeanAndVarianceCalculator meanAndVarianceCalculator; … … 34 35 meanAndVarianceCalculator = new OnlineMeanAndVarianceCalculator(); 35 36 Reset(); 37 } 38 39 protected OnlineMeanErrorCalculator(OnlineMeanErrorCalculator other, Cloner cloner) { 40 meanAndVarianceCalculator = (OnlineMeanAndVarianceCalculator)other.meanAndVarianceCalculator.Clone(cloner); 36 41 } 37 42 … … 74 79 } 75 80 } 81 82 // IDeepCloneable members 83 public object Clone() { 84 var cloner = new Cloner(); 85 return new OnlineMeanErrorCalculator(this, cloner); 86 } 87 88 public IDeepCloneable Clone(Cloner cloner) { 89 var clone = cloner.GetClone(this); 90 if (clone == null) { 91 clone = new OnlineMeanErrorCalculator(this, cloner); 92 cloner.RegisterClonedObject(this, clone); 93 } 94 return clone; 95 } 76 96 } 77 97 } -
branches/symbreg-factors-2650/HeuristicLab.Problems.DataAnalysis/3.4/OnlineCalculators/OnlineMeanSquaredErrorCalculator.cs
r14185 r14330 22 22 using System; 23 23 using System.Collections.Generic; 24 using HeuristicLab.Common; 24 25 25 26 namespace HeuristicLab.Problems.DataAnalysis { 26 public class OnlineMeanSquaredErrorCalculator : IOnlineCalculator {27 public class OnlineMeanSquaredErrorCalculator : IOnlineCalculator, IDeepCloneable { 27 28 28 29 private double sse; … … 36 37 public OnlineMeanSquaredErrorCalculator() { 37 38 Reset(); 39 } 40 41 protected OnlineMeanSquaredErrorCalculator(OnlineMeanSquaredErrorCalculator other, Cloner cloner = null) { 42 sse = other.sse; 43 n = other.n; 44 errorState = other.errorState; 38 45 } 39 46 … … 87 94 } 88 95 } 96 97 // IDeepCloneable members 98 public object Clone() { 99 return new OnlineMeanSquaredErrorCalculator(this); 100 } 101 102 public IDeepCloneable Clone(Cloner cloner) { 103 var clone = cloner.GetClone(this); 104 if (clone == null) { 105 clone = new OnlineMeanSquaredErrorCalculator(this); 106 cloner.RegisterClonedObject(this, clone); 107 } 108 return clone; 109 } 89 110 } 90 111 } -
branches/symbreg-factors-2650/HeuristicLab.Problems.DataAnalysis/3.4/OnlineCalculators/OnlineNormalizedMeanSquaredErrorCalculator.cs
r14226 r14330 22 22 using System; 23 23 using System.Collections.Generic; 24 using HeuristicLab.Common; 24 25 25 26 namespace HeuristicLab.Problems.DataAnalysis { 26 public class OnlineNormalizedMeanSquaredErrorCalculator : IOnlineCalculator {27 public class OnlineNormalizedMeanSquaredErrorCalculator : IOnlineCalculator, IDeepCloneable { 27 28 private OnlineMeanAndVarianceCalculator meanSquaredErrorCalculator; 28 29 private OnlineMeanAndVarianceCalculator originalVarianceCalculator; … … 40 41 originalVarianceCalculator = new OnlineMeanAndVarianceCalculator(); 41 42 Reset(); 43 } 44 45 protected OnlineNormalizedMeanSquaredErrorCalculator(OnlineNormalizedMeanSquaredErrorCalculator other, Cloner cloner) { 46 meanSquaredErrorCalculator = (OnlineMeanAndVarianceCalculator)other.meanSquaredErrorCalculator.Clone(cloner); 47 originalVarianceCalculator = (OnlineMeanAndVarianceCalculator)other.originalVarianceCalculator.Clone(cloner); 42 48 } 43 49 … … 92 98 } 93 99 } 100 101 // IDeepCloneable members 102 public object Clone() { 103 var cloner = new Cloner(); 104 return new OnlineNormalizedMeanSquaredErrorCalculator(this, cloner); 105 } 106 107 public IDeepCloneable Clone(Cloner cloner) { 108 var clone = cloner.GetClone(this); 109 if (clone == null) { 110 clone = new OnlineNormalizedMeanSquaredErrorCalculator(this, cloner); 111 cloner.RegisterClonedObject(this, clone); 112 } 113 return clone; 114 } 94 115 } 95 116 } -
branches/symbreg-factors-2650/HeuristicLab.Problems.DataAnalysis/3.4/OnlineCalculators/OnlinePearsonsRCalculator.cs
r14185 r14330 25 25 26 26 namespace HeuristicLab.Problems.DataAnalysis { 27 public class OnlinePearsonsRCalculator : IOnlineCalculator {27 public class OnlinePearsonsRCalculator : IOnlineCalculator, IDeepCloneable { 28 28 private OnlineCovarianceCalculator covCalculator = new OnlineCovarianceCalculator(); 29 29 private OnlineMeanAndVarianceCalculator sxCalculator = new OnlineMeanAndVarianceCalculator(); … … 46 46 47 47 public OnlinePearsonsRCalculator() { } 48 49 protected OnlinePearsonsRCalculator(OnlinePearsonsRCalculator other, Cloner cloner) { 50 covCalculator = (OnlineCovarianceCalculator)other.covCalculator.Clone(cloner); 51 sxCalculator = (OnlineMeanAndVarianceCalculator)other.sxCalculator.Clone(cloner); 52 syCalculator = (OnlineMeanAndVarianceCalculator)other.syCalculator.Clone(cloner); 53 } 48 54 49 55 #region IOnlineCalculator Members … … 91 97 } 92 98 } 99 100 // IDeepCloneable members 101 public object Clone() { 102 var cloner = new Cloner(); 103 return new OnlinePearsonsRCalculator(this, cloner); 104 } 105 106 public IDeepCloneable Clone(Cloner cloner) { 107 var clone = cloner.GetClone(this); 108 if (clone == null) { 109 clone = new OnlinePearsonsRCalculator(this, cloner); 110 cloner.RegisterClonedObject(this, clone); 111 } 112 return clone; 113 } 93 114 } 94 115 } -
branches/symbreg-factors-2650/HeuristicLab.Problems.DataAnalysis/3.4/OnlineCalculators/OnlinePearsonsRSquaredCalculator.cs
r14185 r14330 26 26 namespace HeuristicLab.Problems.DataAnalysis { 27 27 [Obsolete("Use OnlinePearsonsRCalculator directly")] 28 public class OnlinePearsonsRSquaredCalculator : IOnlineCalculator {28 public class OnlinePearsonsRSquaredCalculator : IOnlineCalculator, IDeepCloneable { 29 29 private readonly OnlinePearsonsRCalculator rCalculator = new OnlinePearsonsRCalculator(); 30 30 … … 37 37 38 38 public OnlinePearsonsRSquaredCalculator() { } 39 40 protected OnlinePearsonsRSquaredCalculator(OnlinePearsonsRSquaredCalculator other, Cloner cloner) { 41 this.rCalculator = (OnlinePearsonsRCalculator)other.rCalculator.Clone(cloner); 42 } 39 43 40 44 #region IOnlineCalculator Members … … 59 63 return r * r; 60 64 } 65 66 // IDeepCloneable members 67 public object Clone() { 68 var cloner = new Cloner(); 69 return new OnlinePearsonsRSquaredCalculator(this, cloner); 70 } 71 72 public IDeepCloneable Clone(Cloner cloner) { 73 var clone = cloner.GetClone(this); 74 if (clone == null) { 75 clone = new OnlinePearsonsRSquaredCalculator(this, cloner); 76 cloner.RegisterClonedObject(this, clone); 77 } 78 return clone; 79 } 61 80 } 62 81 } -
branches/symbreg-factors-2650/HeuristicLab.Problems.DataAnalysis/3.4/OnlineCalculators/OnlineTheilsUStatisticCalculator.cs
r14185 r14330 22 22 using System; 23 23 using System.Collections.Generic; 24 using HeuristicLab.Common; 24 25 25 26 namespace HeuristicLab.Problems.DataAnalysis { 26 public class OnlineTheilsUStatisticCalculator : IOnlineTimeSeriesCalculator {27 public class OnlineTheilsUStatisticCalculator : IOnlineTimeSeriesCalculator, IDeepCloneable { 27 28 private OnlineMeanAndVarianceCalculator squaredErrorMeanCalculator; 28 29 private OnlineMeanAndVarianceCalculator unbiasedEstimatorMeanCalculator; … … 43 44 unbiasedEstimatorMeanCalculator = new OnlineMeanAndVarianceCalculator(); 44 45 Reset(); 46 } 47 48 protected OnlineTheilsUStatisticCalculator(OnlineTheilsUStatisticCalculator other, Cloner cloner) { 49 squaredErrorMeanCalculator = (OnlineMeanAndVarianceCalculator)other.squaredErrorMeanCalculator.Clone(cloner); 50 unbiasedEstimatorMeanCalculator = (OnlineMeanAndVarianceCalculator)other.unbiasedEstimatorMeanCalculator.Clone(cloner); 45 51 } 46 52 … … 125 131 } 126 132 } 133 134 // IDeepCloneable members 135 public object Clone() { 136 var cloner = new Cloner(); 137 return new OnlineTheilsUStatisticCalculator(this, cloner); 138 } 139 140 public IDeepCloneable Clone(Cloner cloner) { 141 var clone = cloner.GetClone(this); 142 if (clone == null) { 143 clone = new OnlineTheilsUStatisticCalculator(this, cloner); 144 cloner.RegisterClonedObject(this, clone); 145 } 146 return clone; 147 } 127 148 } 128 149 } -
branches/symbreg-factors-2650/HeuristicLab.Problems.DataAnalysis/3.4/OnlineCalculators/OnlineWeightedDirectionalSymmetryCalculator.cs
r14185 r14330 26 26 27 27 namespace HeuristicLab.Problems.DataAnalysis { 28 public class OnlineWeightedDirectionalSymmetryCalculator : IOnlineTimeSeriesCalculator {28 public class OnlineWeightedDirectionalSymmetryCalculator : IOnlineTimeSeriesCalculator, IDeepCloneable { 29 29 private int n; 30 30 private double correctSum; … … 40 40 public OnlineWeightedDirectionalSymmetryCalculator() { 41 41 Reset(); 42 } 43 44 protected OnlineWeightedDirectionalSymmetryCalculator(OnlineWeightedDirectionalSymmetryCalculator other, Cloner cloner = null) { 45 n = other.n; 46 correctSum = other.correctSum; 47 incorrectSum = other.incorrectSum; 48 errorState = other.errorState; 42 49 } 43 50 … … 118 125 } 119 126 } 127 128 // IDeepCloneable members 129 public object Clone() { 130 return new OnlineWeightedDirectionalSymmetryCalculator(this); 131 } 132 133 public IDeepCloneable Clone(Cloner cloner) { 134 var clone = cloner.GetClone(this); 135 if (clone == null) { 136 clone = new OnlineWeightedDirectionalSymmetryCalculator(this); 137 cloner.RegisterClonedObject(this, clone); 138 } 139 return clone; 140 } 120 141 } 121 142 } -
branches/symbreg-factors-2650/HeuristicLab.Problems.Instances.DataAnalysis
- Property svn:mergeinfo changed
/trunk/sources/HeuristicLab.Problems.Instances.DataAnalysis (added) merged: 14285,14291,14296
- Property svn:mergeinfo changed
-
branches/symbreg-factors-2650/HeuristicLab.Problems.Instances.DataAnalysis.Views
- Property svn:mergeinfo changed
/trunk/sources/HeuristicLab.Problems.Instances.DataAnalysis.Views (added) merged: 14285-14286,14298
- Property svn:mergeinfo changed
-
branches/symbreg-factors-2650/HeuristicLab.Problems.Instances.DataAnalysis.Views/3.3/DataAnalysisImportTypeDialog.cs
r14185 r14330 158 158 } 159 159 catch (Exception ex) { 160 if (ex is IOException || ex is InvalidOperationException || ex is ArgumentException || ex is TableFileParser.DataFormatException) {160 if (ex is IOException || ex is InvalidOperationException || ex is ArgumentException) { 161 161 OkButton.Enabled = false; 162 162 ErrorTextBox.Text = ex.Message; -
branches/symbreg-factors-2650/HeuristicLab.Problems.Instances.DataAnalysis.Views/3.3/RegressionInstanceProviderView.cs
r14185 r14330 52 52 53 53 try { 54 var progress = mainForm.AddOperationProgressToContent(activeView.Content, "Loading problem instance."); 54 var progress = mainForm.AddOperationProgressToContent(activeView.Content, 55 "Loading problem instance."); 55 56 56 Content.ProgressChanged += (o, args) => { progress.ProgressValue = args.ProgressPercentage / 100.0; }; 57 Content.ProgressChanged += 58 (o, args) => { progress.ProgressValue = args.ProgressPercentage / 100.0; }; 57 59 58 instance = Content.ImportData(importTypeDialog.Path, importTypeDialog.ImportType, importTypeDialog.CSVFormat); 59 } catch (IOException ex) { 60 instance = Content.ImportData(importTypeDialog.Path, importTypeDialog.ImportType, 61 importTypeDialog.CSVFormat); 62 } catch (Exception ex) { 60 63 ErrorWhileParsing(ex); 64 return; 65 } finally { 61 66 mainForm.RemoveOperationProgressFromContent(activeView.Content); 62 return;63 67 } 68 64 69 try { 65 70 GenericConsumer.Load(instance); 66 } catch ( IOException ex) {71 } catch (Exception ex) { 67 72 ErrorWhileLoading(ex, importTypeDialog.Path); 68 73 } finally { -
branches/symbreg-factors-2650/HeuristicLab.Problems.Instances.DataAnalysis/3.3/Regression/VariableNetworks/VariableNetwork.cs
r14277 r14330 26 26 using HeuristicLab.Common; 27 27 using HeuristicLab.Core; 28 using HeuristicLab.Problems.DataAnalysis; 28 29 using HeuristicLab.Random; 29 30 … … 195 196 int nl = xs.Length; 196 197 int nRows = xs.First().Count; 197 double[,] K = new double[nRows, nRows]; 198 199 // sample length-scales 198 199 // sample u iid ~ N(0, 1) 200 var u = Enumerable.Range(0, nRows).Select(_ => NormalDistributedRandom.NextDouble(random, 0, 1)).ToArray(); 201 202 // sample actual length-scales 200 203 var l = Enumerable.Range(0, nl) 201 204 .Select(_ => random.NextDouble() * 2 + 0.5) 202 205 .ToArray(); 203 // calculate covariance matrix 206 207 double[,] K = CalculateCovariance(xs, l); 208 209 // decompose 210 alglib.trfac.spdmatrixcholesky(ref K, nRows, false); 211 212 213 // calc y = Lu 214 var y = new double[u.Length]; 215 alglib.ablas.rmatrixmv(nRows, nRows, K, 0, 0, 0, u, 0, ref y, 0); 216 217 // calculate relevance by removing dimensions 218 relevance = CalculateRelevance(y, u, xs, l); 219 220 221 // calculate variable relevance 222 // as per Rasmussen and Williams "Gaussian Processes for Machine Learning" page 106: 223 // ,,For the squared exponential covariance function [...] the l1, ..., lD hyperparameters 224 // play the role of characteristic length scales [...]. Such a covariance function implements 225 // automatic relevance determination (ARD) [Neal, 1996], since the inverse of the length-scale 226 // determines how relevant an input is: if the length-scale has a very large value, the covariance 227 // will become almost independent of that input, effectively removing it from inference.'' 228 // relevance = l.Select(li => 1.0 / li).ToArray(); 229 230 return y; 231 } 232 233 // calculate variable relevance based on removal of variables 234 // 1) to remove a variable we set it's length scale to infinity (no relation of the variable value to the target) 235 // 2) calculate MSE of the original target values (y) to the updated targes y' (after variable removal) 236 // 3) relevance is larger if MSE(y,y') is large 237 // 4) scale impacts so that the most important variable has impact = 1 238 private double[] CalculateRelevance(double[] y, double[] u, List<double>[] xs, double[] l) { 239 int nRows = xs.First().Count; 240 var changedL = new double[l.Length]; 241 var relevance = new double[l.Length]; 242 for (int i = 0; i < l.Length; i++) { 243 Array.Copy(l, changedL, changedL.Length); 244 changedL[i] = double.MaxValue; 245 var changedK = CalculateCovariance(xs, changedL); 246 247 var yChanged = new double[u.Length]; 248 alglib.ablas.rmatrixmv(nRows, nRows, changedK, 0, 0, 0, u, 0, ref yChanged, 0); 249 250 OnlineCalculatorError error; 251 var mse = OnlineMeanSquaredErrorCalculator.Calculate(y, yChanged, out error); 252 if (error != OnlineCalculatorError.None) mse = double.MaxValue; 253 relevance[i] = mse; 254 } 255 // scale so that max relevance is 1.0 256 var maxRel = relevance.Max(); 257 for (int i = 0; i < relevance.Length; i++) relevance[i] /= maxRel; 258 return relevance; 259 } 260 261 private double[,] CalculateCovariance(List<double>[] xs, double[] l) { 262 int nRows = xs.First().Count; 263 double[,] K = new double[nRows, nRows]; 204 264 for (int r = 0; r < nRows; r++) { 205 265 double[] xi = xs.Select(x => x[r]).ToArray(); … … 213 273 } 214 274 } 215 216 275 // add a small diagonal matrix for numeric stability 217 276 for (int i = 0; i < nRows; i++) { … … 219 278 } 220 279 221 // decompose 222 alglib.trfac.spdmatrixcholesky(ref K, nRows, false); 223 224 // sample u iid ~ N(0, 1) 225 var u = Enumerable.Range(0, nRows).Select(_ => NormalDistributedRandom.NextDouble(random, 0, 1)).ToArray(); 226 227 // calc y = Lu 228 var y = new double[u.Length]; 229 alglib.ablas.rmatrixmv(nRows, nRows, K, 0, 0, 0, u, 0, ref y, 0); 230 231 // calculate variable relevance 232 // as per Rasmussen and Williams "Gaussian Processes for Machine Learning" page 106: 233 // ,,For the squared exponential covariance function [...] the l1, ..., lD hyperparameters 234 // play the role of characteristic length scales [...]. Such a covariance function implements 235 // automatic relevance determination (ARD) [Neal, 1996], since the inverse of the length-scale 236 // determines how relevant an input is: if the length-scale has a very large value, the covariance 237 // will become almost independent of that input, effectively removing it from inference.'' 238 relevance = l.Select(li => 1.0 / li).ToArray(); 239 240 return y; 280 return K; 241 281 } 242 282 } -
branches/symbreg-factors-2650/HeuristicLab.Problems.Instances.DataAnalysis/3.3/TableFileParser.cs
r14185 r14330 198 198 /// <param name="columnNamesInFirstLine"></param> 199 199 public void Parse(Stream stream, NumberFormatInfo numberFormat, DateTimeFormatInfo dateTimeFormatInfo, char separator, bool columnNamesInFirstLine, int lineLimit = -1) { 200 using (StreamReader reader = new StreamReader(stream, Encoding)) { 200 if (lineLimit > 0) estimatedNumberOfLines = lineLimit; 201 202 using (var reader = new StreamReader(stream)) { 201 203 tokenizer = new Tokenizer(reader, numberFormat, dateTimeFormatInfo, separator); 204 var strValues = new List<List<string>>(); 202 205 values = new List<IList>(); 203 if (lineLimit > 0) estimatedNumberOfLines = lineLimit; 204 205 if (columnNamesInFirstLine) { 206 ParseVariableNames(); 207 if (!tokenizer.HasNext()) 208 Error( 209 "Couldn't parse data values. Probably because of incorrect number format (the parser expects english number format with a '.' as decimal separator).", 210 "", tokenizer.CurrentLineNumber); 211 } 212 213 214 // read values... start in first row 206 Prepare(columnNamesInFirstLine, strValues); 207 215 208 int nLinesParsed = 0; 216 209 int colIdx = 0; 217 int numValuesInFirstRow = columnNamesInFirstLine ? variableNames.Count : -1; // number of variables or inizialize based on first row of values (-1)218 210 while (tokenizer.HasNext() && (lineLimit < 0 || nLinesParsed < lineLimit)) { 219 211 if (tokenizer.PeekType() == TokenTypeEnum.NewLine) { … … 221 213 222 214 // all rows have to have the same number of values 223 // the first row defines how many samples are needed224 if ( numValuesInFirstRow < 0) numValuesInFirstRow = values.Count; // set to number of colums in the first row225 else if (colIdx > 0 && numValuesInFirstRow != colIdx) {// read at least one value in the row (support for skipping empty lines)226 Error("The first row of the dataset has " + numValuesInFirstRow+ " columns." + Environment.NewLine +215 // the first row defines how many elements are needed 216 if (colIdx > 0 && values.Count != colIdx) { 217 // read at least one value in the row (support for skipping empty lines) 218 Error("The first row of the dataset has " + values.Count + " columns." + Environment.NewLine + 227 219 "Line " + tokenizer.CurrentLineNumber + " has " + colIdx + " columns.", "", 228 220 tokenizer.CurrentLineNumber); 229 221 } 230 222 OnReport(tokenizer.BytesRead); … … 234 226 } else { 235 227 // read one value 236 TokenTypeEnum type; string strVal; double dblVal; DateTime dateTimeVal; 228 TokenTypeEnum type; 229 string strVal; 230 double dblVal; 231 DateTime dateTimeVal; 237 232 tokenizer.Next(out type, out strVal, out dblVal, out dateTimeVal); 238 233 239 // initialize columns on the first row (fixing data types as presented in the first row...) 240 if (nLinesParsed == 0) { 241 values.Add(CreateList(type, estimatedNumberOfLines)); 242 } else if (colIdx == values.Count) { 243 Error("The first row of the dataset has " + numValuesInFirstRow + " columns." + Environment.NewLine + 234 if (colIdx == values.Count) { 235 Error("The first row of the dataset has " + values.Count + " columns." + Environment.NewLine + 244 236 "Line " + tokenizer.CurrentLineNumber + " has more columns.", "", 245 237 tokenizer.CurrentLineNumber); 246 238 } 247 239 if (!IsColumnTypeCompatible(values[colIdx], type)) { 248 values[colIdx] = ConvertToStringColumn(values[colIdx]);240 values[colIdx] = strValues[colIdx]; 249 241 } 242 250 243 // add the value to the column 251 AddValue(type, values[colIdx++], strVal, dblVal, dateTimeVal); 244 AddValue(type, values[colIdx], strVal, dblVal, dateTimeVal); 245 if (!(values[colIdx] is List<string>)) { // optimization: don't store the string values in another list if the column is list<string> 246 strValues[colIdx].Add(strVal); 247 } 248 colIdx++; 252 249 } 253 250 } 254 255 if (!values.Any() || values.First().Count == 0) 256 Error("Couldn't parse data values. Probably because of incorrect number format " +257 "(the parser expects english number format with a '.' as decimal separator).", "", tokenizer.CurrentLineNumber);258 }251 } 252 253 if (!values.Any() || values.First().Count == 0) 254 Error("Couldn't parse data values. Probably because of incorrect number format " + 255 "(the parser expects english number format with a '.' as decimal separator).", "", tokenizer.CurrentLineNumber); 259 256 260 257 this.rows = values.First().Count; … … 277 274 // for large files we created a lot of memory pressure, cannot hurt to run GC.Collect here (TableFileParser is called seldomly on user interaction) 278 275 GC.Collect(2, GCCollectionMode.Forced); 276 } 277 278 private void Prepare(bool columnNamesInFirstLine, List<List<string>> strValues) { 279 if (columnNamesInFirstLine) { 280 ParseVariableNames(); 281 if (!tokenizer.HasNext()) 282 Error( 283 "Couldn't parse data values. Probably because of incorrect number format (the parser expects english number format with a '.' as decimal separator).", 284 "", tokenizer.CurrentLineNumber); 285 } 286 // read first line to determine types and allocate specific lists 287 // read values... start in first row 288 int colIdx = 0; 289 while (tokenizer.PeekType() != TokenTypeEnum.NewLine) { 290 // read one value 291 TokenTypeEnum type; string strVal; double dblVal; DateTime dateTimeVal; 292 tokenizer.Next(out type, out strVal, out dblVal, out dateTimeVal); 293 294 // initialize column 295 values.Add(CreateList(type, estimatedNumberOfLines)); 296 if (type == TokenTypeEnum.String) 297 strValues.Add(new List<string>(0)); // optimization: don't store the string values in another list if the column is list<string> 298 else 299 strValues.Add(new List<string>(estimatedNumberOfLines)); 300 301 AddValue(type, values[colIdx], strVal, dblVal, dateTimeVal); 302 if (type != TokenTypeEnum.String) 303 strValues[colIdx].Add(strVal); 304 colIdx++; 305 } 306 tokenizer.Skip(); // skip newline 279 307 } 280 308 … … 530 558 type = TokenTypeEnum.Double; 531 559 doubleVals[i] = doubleVal; 532 } else if (DateTime.TryParse(tok, dateTimeFormatInfo, DateTimeStyles.None, out dateTimeValue)) { 560 } else if (DateTime.TryParse(tok, dateTimeFormatInfo, DateTimeStyles.NoCurrentDateDefault, out dateTimeValue) 561 && dateTimeValue.Year > 1 && dateTimeValue.Month > 1 && dateTimeValue.Day > 1 // if no date is given it is returned as 1.1.0001 -> don't allow this 562 ) { 533 563 type = TokenTypeEnum.DateTime; 534 564 dateTimeVals[i] = dateTimeValue; … … 606 636 607 637 private void Error(string message, string token, int lineNumber) { 608 throw new DataFormatException("Error while parsing.\n" + message, token, lineNumber);638 throw new IOException(string.Format("Error while parsing. {0} (token: {1} lineNumber: {2}).", message, token, lineNumber)); 609 639 } 610 640 #endregion 611 612 [Serializable]613 public class DataFormatException : Exception {614 private int line;615 public int Line {616 get { return line; }617 }618 private string token;619 public string Token {620 get { return token; }621 }622 public DataFormatException(string message, string token, int line)623 : base(message + "\nToken: " + token + " (line: " + line + ")") {624 this.token = token;625 this.line = line;626 }627 628 public DataFormatException(SerializationInfo info, StreamingContext context) : base(info, context) { }629 }630 641 } 631 642 } -
branches/symbreg-factors-2650/HeuristicLab.Tests
- Property svn:mergeinfo changed
/trunk/sources/HeuristicLab.Tests (added) merged: 14284,14288
- Property svn:mergeinfo changed
-
branches/symbreg-factors-2650/HeuristicLab.Tests/HeuristicLab.Problems.Instances.DataAnalysis-3.3/TableFileParserTest.cs
r14185 r14330 24 24 using System.Globalization; 25 25 using System.IO; 26 using System.Text; 26 27 using Microsoft.VisualStudio.TestTools.UnitTesting; 27 28 … … 47 48 Assert.AreEqual(4, parser.Columns); 48 49 Assert.AreEqual(parser.Values[3][0], 3.14); 49 } finally { 50 } 51 finally { 50 52 File.Delete(tempFileName); 51 53 } … … 71 73 Assert.AreEqual(4, parser.Columns); 72 74 Assert.AreEqual(parser.Values[3][0], 3.14); 73 } finally { 75 } 76 finally { 74 77 File.Delete(tempFileName); 75 78 } … … 94 97 Assert.AreEqual(4, parser.Columns); 95 98 Assert.AreEqual(parser.Values[3][0], 3.14); 96 } finally { 99 } 100 finally { 97 101 File.Delete(tempFileName); 98 102 } … … 118 122 Assert.AreEqual(4, parser.Columns); 119 123 Assert.AreEqual(parser.Values[3][0], 3.14); 120 } finally { 124 } 125 finally { 121 126 File.Delete(tempFileName); 122 127 } … … 141 146 Assert.AreEqual(4, parser.Columns); 142 147 Assert.AreEqual((double)parser.Values[3][0], 3); 143 } finally { 148 } 149 finally { 144 150 File.Delete(tempFileName); 145 151 } … … 165 171 Assert.AreEqual(4, parser.Columns); 166 172 Assert.AreEqual((double)parser.Values[3][0], 3); 167 } finally { 173 } 174 finally { 168 175 File.Delete(tempFileName); 169 176 } … … 188 195 Assert.AreEqual(4, parser.Columns); 189 196 Assert.AreEqual((double)parser.Values[3][0], 3); 190 } finally { 197 } 198 finally { 191 199 File.Delete(tempFileName); 192 200 } … … 211 219 Assert.AreEqual(4, parser.Columns); 212 220 Assert.AreEqual((double)parser.Values[3][0], 3); 213 } finally { 221 } 222 finally { 214 223 File.Delete(tempFileName); 215 224 } … … 235 244 Assert.AreEqual(4, parser.Columns); 236 245 Assert.AreEqual((double)parser.Values[3][0], 3); 237 } finally { 246 } 247 finally { 238 248 File.Delete(tempFileName); 239 249 } … … 259 269 Assert.AreEqual(4, parser.Columns); 260 270 Assert.AreEqual((double)parser.Values[3][0], 3); 261 } finally { 271 } 272 finally { 262 273 File.Delete(tempFileName); 263 274 } … … 283 294 Assert.AreEqual(4, parser.Columns); 284 295 Assert.AreEqual((double)parser.Values[3][0], 3.14); 285 } finally { 296 } 297 finally { 286 298 File.Delete(tempFileName); 287 299 } … … 307 319 Assert.AreEqual(4, parser.Columns); 308 320 Assert.AreEqual((double)parser.Values[3][0], 3.14); 309 } finally { 321 } 322 finally { 310 323 File.Delete(tempFileName); 311 324 } … … 330 343 Assert.AreEqual(4, parser.Columns); 331 344 Assert.AreEqual((double)parser.Values[3][0], 3.14); 332 } finally { 345 } 346 finally { 333 347 File.Delete(tempFileName); 334 348 } … … 354 368 Assert.AreEqual(4, parser.Columns); 355 369 Assert.AreEqual((double)parser.Values[3][0], 3.14); 356 } finally { 370 } 371 finally { 357 372 File.Delete(tempFileName); 358 373 } … … 377 392 Assert.AreEqual(4, parser.Columns); 378 393 Assert.AreEqual((double)parser.Values[3][0], 3); 379 } finally { 394 } 395 finally { 380 396 File.Delete(tempFileName); 381 397 } … … 401 417 Assert.AreEqual(4, parser.Columns); 402 418 Assert.AreEqual((double)parser.Values[3][0], 3); 403 } finally { 419 } 420 finally { 404 421 File.Delete(tempFileName); 405 422 } … … 424 441 Assert.AreEqual(4, parser.Rows); 425 442 Assert.AreEqual(4, parser.Columns); 426 } finally { 443 } 444 finally { 427 445 File.Delete(tempFileName); 428 446 } … … 447 465 Assert.AreEqual(4, parser.Columns); 448 466 Assert.AreEqual((double)parser.Values[3][0], 3.14); 449 } finally { 467 } 468 finally { 450 469 File.Delete(tempFileName); 451 470 } … … 471 490 Assert.AreEqual(4, parser.Columns); 472 491 Assert.AreEqual((double)parser.Values[3][0], 3.14); 473 } finally { 492 } 493 finally { 474 494 File.Delete(tempFileName); 475 495 } … … 494 514 Assert.AreEqual(4, parser.Columns); 495 515 Assert.AreEqual((double)parser.Values[3][0], 3.14); 496 } finally { 516 } 517 finally { 497 518 File.Delete(tempFileName); 498 519 } … … 518 539 Assert.AreEqual(4, parser.Columns); 519 540 Assert.AreEqual((double)parser.Values[3][0], 3.14); 520 } finally { 541 } 542 finally { 521 543 File.Delete(tempFileName); 522 544 } … … 539 561 Assert.AreEqual(3, parser.Rows); 540 562 Assert.AreEqual(4507, parser.Columns); 541 } finally { 563 } 564 finally { 542 565 File.Delete(tempFileName); 543 566 } … … 562 585 Assert.AreEqual(4, parser.Columns); 563 586 Assert.AreEqual((double)parser.Values[3][0], 3); 564 } finally { 587 } 588 finally { 565 589 File.Delete(tempFileName); 566 590 } … … 586 610 Assert.AreEqual(4, parser.Columns); 587 611 Assert.AreEqual((double)parser.Values[3][0], 3); 588 } finally { 612 } 613 finally { 589 614 File.Delete(tempFileName); 590 615 } … … 627 652 Assert.IsTrue(double.IsPositiveInfinity((double)parser.Values[1][3])); // NOTE: in DE-DE NumberFormat just "unendlich" is not allowed (compare with InvariantCulture) 628 653 Assert.IsTrue(double.IsNegativeInfinity((double)parser.Values[1][4])); 629 } finally { 654 } 655 finally { 630 656 File.Delete(tempFileName); 631 657 } … … 664 690 Assert.IsTrue(double.IsPositiveInfinity((double)parser.Values[1][3])); // NOTE: in InvariantCulture +Infinity is not allowed (compare with DE-DE) 665 691 Assert.IsTrue(double.IsNegativeInfinity((double)parser.Values[1][4])); 666 } finally { 667 File.Delete(tempFileName); 692 } 693 finally { 694 File.Delete(tempFileName); 695 } 696 } 697 698 699 [TestMethod] 700 [TestCategory("Problems.Instances")] 701 [TestProperty("Time", "short")] 702 public void ParseWithTypeConversion() { 703 // the parser tries to determine the column type (double, datetime, string) by looking at the values in the first few rows 704 // if the values are of a different type then the type of the column is converted 705 { 706 // case 1 707 // default for values is double and therefore a column with all missing values should be List<double> and contain NaN 708 var tmpFileName = Path.GetTempFileName(); 709 WriteToFile(tmpFileName, 710 @"stringCol,note 711 ,missing val 712 3.14,double 713 "); 714 715 TableFileParser parser = new TableFileParser(); 716 try { 717 parser.Parse(tmpFileName, 718 CultureInfo.InvariantCulture.NumberFormat, 719 CultureInfo.InvariantCulture.DateTimeFormat, 720 separator: ',', columnNamesInFirstLine: true); 721 Assert.IsTrue(parser.Values[0] is List<double>); 722 Assert.AreEqual(double.NaN, parser.Values[0][0]); 723 Assert.AreEqual(3.14, parser.Values[0][1]); 724 } 725 finally { 726 File.Delete(tmpFileName); 727 } 728 729 } 730 731 { 732 // case 2 733 // 'The first missing values are replaced with double.NaN while the last ones with string.Empty.' 734 735 var tmpFileName = Path.GetTempFileName(); 736 WriteToFile(tmpFileName, 737 @"stringCol,note 738 ,missing val 739 3.14, 740 ,missing val 741 str,a string --> column is converted to List<string> 742 ,missing val 743 "); 744 745 TableFileParser parser = new TableFileParser(); 746 try { 747 parser.Parse(tmpFileName, 748 CultureInfo.InvariantCulture.NumberFormat, 749 CultureInfo.InvariantCulture.DateTimeFormat, 750 separator: ',', columnNamesInFirstLine: true); 751 Assert.IsTrue(parser.Values[0] is List<string>); 752 Assert.AreEqual(string.Empty, parser.Values[0][0]); 753 Assert.AreEqual("3.14", parser.Values[0][1]); 754 Assert.AreEqual(string.Empty, parser.Values[0][2]); 755 Assert.AreEqual("str", parser.Values[0][3]); 756 Assert.AreEqual(string.Empty, parser.Values[0][4]); 757 } 758 finally { 759 File.Delete(tmpFileName); 760 } 761 } 762 763 { 764 // case 3 765 // DateTime conversion to strings 766 var tmpFileName = Path.GetTempFileName(); 767 WriteToFile(tmpFileName, 768 @"stringCol,note 769 ,missing val 770 3.1.2016, 771 ,missing val 772 str,a string --> column is converted to List<string> 773 ,missing val 774 "); 775 776 TableFileParser parser = new TableFileParser(); 777 try { 778 parser.Parse(tmpFileName, 779 CultureInfo.InvariantCulture.NumberFormat, 780 CultureInfo.InvariantCulture.DateTimeFormat, 781 separator: ',', columnNamesInFirstLine: true); 782 Assert.IsTrue(parser.Values[0] is List<string>); 783 Assert.AreEqual(string.Empty, parser.Values[0][0]); 784 Assert.AreEqual("3.1.2016", parser.Values[0][1]); 785 Assert.AreEqual(string.Empty, parser.Values[0][2]); 786 Assert.AreEqual("str", parser.Values[0][3]); 787 Assert.AreEqual(string.Empty, parser.Values[0][4]); 788 } 789 finally { 790 File.Delete(tmpFileName); 791 } 792 } 793 } 794 795 [TestMethod] 796 [TestCategory("Problems.Instances")] 797 [TestProperty("Time", "short")] 798 public void ParseDateTime() { 799 { 800 // case 1 dates and datetimes should be parsed as datetime column 801 var tmpFileName = Path.GetTempFileName(); 802 WriteToFile(tmpFileName, 803 @"stringCol,note 804 19.6.2016,date 805 19.6.2016 8:15,datetime 806 "); 807 808 TableFileParser parser = new TableFileParser(); 809 try { 810 parser.Parse(tmpFileName, 811 CultureInfo.GetCultureInfo("de-de").NumberFormat, 812 CultureInfo.GetCultureInfo("de-de").DateTimeFormat, 813 separator: ',', columnNamesInFirstLine: true); 814 Assert.IsTrue(parser.Values[0] is List<DateTime>); 815 Assert.AreEqual(new DateTime(2016, 6, 19), parser.Values[0][0]); 816 Assert.AreEqual(new DateTime(2016, 6, 19, 8, 15, 0), parser.Values[0][1]); 817 818 WriteToFile(tmpFileName, 819 @"stringCol,note 820 2016/6/19,date 821 2016/6/19 8:15,datetime 822 "); 823 824 parser.Parse(tmpFileName, 825 CultureInfo.InvariantCulture.NumberFormat, 826 CultureInfo.InvariantCulture.DateTimeFormat, 827 separator: ',', columnNamesInFirstLine: true); 828 Assert.IsTrue(parser.Values[0] is List<DateTime>); 829 Assert.AreEqual(new DateTime(2016, 6, 19), parser.Values[0][0]); 830 Assert.AreEqual(new DateTime(2016, 6, 19, 8, 15, 0), parser.Values[0][1]); 831 } 832 833 finally { 834 File.Delete(tmpFileName); 835 } 836 } 837 838 { 839 // case 2 never parse time values as datetimes 840 var tmpFileName = Path.GetTempFileName(); 841 WriteToFile(tmpFileName, 842 @"stringCol,note 843 8:15,time value 844 9:40,time value 845 "); 846 847 TableFileParser parser = new TableFileParser(); 848 try { 849 parser.Parse(tmpFileName, 850 CultureInfo.InvariantCulture.NumberFormat, 851 CultureInfo.InvariantCulture.DateTimeFormat, 852 separator: ',', columnNamesInFirstLine: true); 853 Assert.IsTrue(parser.Values[0] is List<string>); // time values should be parsed as strings 854 Assert.AreEqual("8:15", parser.Values[0][0]); 855 Assert.AreEqual("9:40", parser.Values[0][1]); 856 } 857 finally { 858 File.Delete(tmpFileName); 859 } 860 } 861 } 862 863 864 865 [TestMethod] 866 [TestCategory("Problems.Instances")] 867 [TestProperty("Time", "short")] 868 public void CheckTypeConversionAndLongFiles() { 869 { 870 // case 1 incorrect input after 500 rows should lead to exceptions 871 var tmpFileName = Path.GetTempFileName(); 872 // create input data 873 var sb = new StringBuilder(); 874 sb.AppendLine("col1,col2"); 875 for (int r = 0; r < 2000; r++) { 876 sb.AppendLine("3.15, 3.15"); 877 } 878 // add a row with only one input value 879 sb.AppendLine("3.15"); 880 881 WriteToFile(tmpFileName, sb.ToString()); 882 883 TableFileParser parser = new TableFileParser(); 884 try { 885 parser.Parse(tmpFileName, 886 CultureInfo.InvariantCulture.NumberFormat, 887 CultureInfo.InvariantCulture.DateTimeFormat, 888 separator: ',', columnNamesInFirstLine: true); 889 // Parse should fail with an exception 890 Assert.Fail("expected exception TableFileParser.DataFormatException"); 891 } 892 catch (IOException) { 893 // ignore the expected exception 894 } 895 896 finally { 897 File.Delete(tmpFileName); 898 } 899 } 900 { 901 // case 2 902 var tmpFileName = Path.GetTempFileName(); 903 // create input data 904 var sb = new StringBuilder(); 905 sb.AppendLine("doubleCol,stringCol"); 906 for (int r = 0; r < 2000; r++) { 907 sb.AppendLine("3.15, 3.15"); 908 } 909 // add a row with a string value --> the column should be converted to string 910 sb.AppendLine("3.15,str"); 911 912 WriteToFile(tmpFileName, sb.ToString()); 913 914 TableFileParser parser = new TableFileParser(); 915 try { 916 parser.Parse(tmpFileName, 917 CultureInfo.InvariantCulture.NumberFormat, 918 CultureInfo.InvariantCulture.DateTimeFormat, 919 separator: ',', columnNamesInFirstLine: true); 920 Assert.IsTrue(parser.Values[0] is List<double>); 921 Assert.IsTrue(parser.Values[1] is List<string>); 922 Assert.AreEqual(parser.Values[1][0], "3.15"); 923 Assert.AreEqual(parser.Values[1][2000], "str"); 924 } 925 926 finally { 927 File.Delete(tmpFileName); 928 } 668 929 } 669 930 }
Note: See TracChangeset
for help on using the changeset viewer.