Changeset 16723 for branches/2521_ProblemRefactoring/HeuristicLab.Problems.DataAnalysis/3.4/ModifiableDataset.cs
- Timestamp:
- 03/28/19 16:54:20 (6 years ago)
- Location:
- branches/2521_ProblemRefactoring
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/2521_ProblemRefactoring
- Property svn:mergeinfo changed
-
branches/2521_ProblemRefactoring/HeuristicLab.Problems.DataAnalysis
- Property svn:mergeinfo changed
-
branches/2521_ProblemRefactoring/HeuristicLab.Problems.DataAnalysis/3.4
-
Property
svn:mergeinfo
set to
(toggle deleted branches)
/branches/2520_PersistenceReintegration/HeuristicLab.Problems.DataAnalysis/3.4 merged eligible /branches/2839_HiveProjectManagement/HeuristicLab.Problems.DataAnalysis/3.4 merged eligible /branches/2947_ConfigurableIndexedDataTable/HeuristicLab.Problems.DataAnalysis/3.4 merged eligible /branches/2965_CancelablePersistence/HeuristicLab.Problems.DataAnalysis/3.4 merged eligible /stable/HeuristicLab.Problems.DataAnalysis/3.4 merged eligible /trunk/HeuristicLab.Problems.DataAnalysis/3.4 merged eligible /branches/2892_LR-prediction-intervals/HeuristicLab.Problems.DataAnalysis/3.4 15743-16388 /branches/2904_CalculateImpacts/3.4 15808-16421 /branches/2915-AbsoluteSymbol/HeuristicLab.Problems.DataAnalysis/3.4 15943-16355 /branches/2966_interval_calculation/HeuristicLab.Problems.DataAnalysis/3.4 16320-16406 /branches/Async/HeuristicLab.Problems.DataAnalysis/3.4 13329-15286 /branches/Classification-Extensions/HeuristicLab.Problems.DataAnalysis/3.4 11606-11761 /branches/ClassificationModelComparison/HeuristicLab.Problems.DataAnalysis/3.4 9073-13099 /branches/CloningRefactoring/HeuristicLab.Problems.DataAnalysis/3.4 4656-4721 /branches/DataAnalysis Refactoring/HeuristicLab.Problems.DataAnalysis/3.4 5471-5808 /branches/DataAnalysis SolutionEnsembles/HeuristicLab.Problems.DataAnalysis/3.4 5815-6180 /branches/DataAnalysis/HeuristicLab.Problems.DataAnalysis/3.4 4220,4226,4236-4238,4389,4458-4459,4462,4464 /branches/DataAnalysisCSVImport/HeuristicLab.Problems.DataAnalysis/3.4 8713-8875 /branches/DataPreprocessing/HeuristicLab.Problems.DataAnalysis/3.4 10085-11101 /branches/DatasetFeatureCorrelation/HeuristicLab.Problems.DataAnalysis/3.4 8035-8538 /branches/GP.Grammar.Editor/HeuristicLab.Problems.DataAnalysis/3.4 6284-6795 /branches/GP.Symbols (TimeLag, Diff, Integral)/HeuristicLab.Problems.DataAnalysis/3.4 5060 /branches/HeuristicLab.DatasetRefactor/sources/HeuristicLab.Problems.DataAnalysis/3.4 11570-12508 /branches/HeuristicLab.Problems.Orienteering/HeuristicLab.Problems.DataAnalysis/3.4 11130-12721 /branches/HeuristicLab.RegressionSolutionGradientView/HeuristicLab.Problems.DataAnalysis/3.4 13819-14091 /branches/HeuristicLab.TimeSeries/HeuristicLab.Problems.DataAnalysis/3.4 7098-8789 /branches/LogResidualEvaluator/HeuristicLab.Problems.DataAnalysis/3.4 10202-10483 /branches/NET40/sources/HeuristicLab.Problems.DataAnalysis/3.4 5138-5162 /branches/ParallelEngine/HeuristicLab.Problems.DataAnalysis/3.4 5175-5192 /branches/ProblemInstancesRegressionAndClassification/HeuristicLab.Problems.DataAnalysis/3.4 7570-7810 /branches/QAPAlgorithms/HeuristicLab.Problems.DataAnalysis/3.4 6350-6627 /branches/Restructure trunk solution/HeuristicLab.Problems.DataAnalysis/3.4 6828 /branches/SimplifierViewsProgress/HeuristicLab.Problems.DataAnalysis/3.4 15318-15370 /branches/SpectralKernelForGaussianProcesses/HeuristicLab.Problems.DataAnalysis/3.4 10204-10479 /branches/Trunk/HeuristicLab.Problems.DataAnalysis/3.4 6829-6865 /branches/histogram/HeuristicLab.Problems.DataAnalysis/3.4 5959-6341 /branches/symbreg-factors-2650/HeuristicLab.Problems.DataAnalysis/3.4 14232-14825
-
Property
svn:mergeinfo
set to
(toggle deleted branches)
-
branches/2521_ProblemRefactoring/HeuristicLab.Problems.DataAnalysis/3.4/ModifiableDataset.cs
r16692 r16723 2 2 3 3 /* HeuristicLab 4 * Copyright (C) 2002-201 8Heuristic and Evolutionary Algorithms Laboratory (HEAL)4 * Copyright (C) 2002-2019 Heuristic and Evolutionary Algorithms Laboratory (HEAL) 5 5 * 6 6 * This file is part of HeuristicLab. … … 29 29 using HeuristicLab.Core; 30 30 using HeuristicLab.Data; 31 using H euristicLab.Persistence.Default.CompositeSerializers.Storable;31 using HEAL.Attic; 32 32 33 33 namespace HeuristicLab.Problems.DataAnalysis { 34 34 [Item("ModifiableDataset", "Represents a dataset containing data that should be analyzed, which can be modified by adding or replacing variables and values.")] 35 [Storable Class]35 [StorableType("4B9DA9DD-10C4-4609-8F87-B35ECD7A7487")] 36 36 public sealed class ModifiableDataset : Dataset, IStringConvertibleMatrix { 37 37 [StorableConstructor] 38 private ModifiableDataset( bool deserializing) : base(deserializing) { }38 private ModifiableDataset(StorableConstructorFlag _) : base(_) { } 39 39 40 40 private ModifiableDataset(ModifiableDataset original, Cloner cloner) : base(original, cloner) { 41 var variables = variableValues.Keys.ToList(); 42 foreach (var v in variables) { 43 var type = GetVariableType(v); 44 if (type == typeof(DateTime)) { 45 variableValues[v] = GetDateTimeValues(v).ToList(); 46 } else if (type == typeof(double)) { 47 variableValues[v] = GetDoubleValues(v).ToList(); 48 } else if (type == typeof(string)) { 49 variableValues[v] = GetStringValues(v).ToList(); 50 } else { 51 throw new ArgumentException("Unsupported type " + type + " for variable " + v); 41 variableNames = new List<string>(original.variableNames); 42 variableValues = CloneValues(original.variableValues); 43 } 44 45 public override IDeepCloneable Clone(Cloner cloner) { return new ModifiableDataset(this, cloner); } 46 47 public ModifiableDataset() { } 48 49 public ModifiableDataset(IEnumerable<string> variableNames, IEnumerable<IList> variableValues, bool cloneValues = false) : 50 base(variableNames, variableValues, cloneValues) { } 51 52 public Dataset ToDataset() { 53 return new Dataset(variableNames, variableNames.Select(v => variableValues[v])); 54 } 55 56 57 public IEnumerable<object> GetRow(int row) { 58 if (row < 0 || row >= Rows) 59 throw new ArgumentException(string.Format("Invalid row {0} specified. The dataset contains {1} row(s).", row, Rows)); 60 61 return variableValues.Select(x => x.Value[row]); 62 } 63 64 public void AddRow(IEnumerable<object> values) { 65 var list = values.ToList(); 66 if (list.Count != variableNames.Count) 67 throw new ArgumentException("The number of values must be equal to the number of variable names."); 68 // check if all the values are of the correct type 69 for (int i = 0; i < list.Count; ++i) { 70 if (list[i].GetType() != GetVariableType(variableNames[i])) { 71 throw new ArgumentException("The type of the provided value does not match the variable type."); 52 72 } 53 73 } 54 } 55 public override IDeepCloneable Clone(Cloner cloner) { return new ModifiableDataset(this, cloner); } 56 public ModifiableDataset() : base() { } 57 58 public ModifiableDataset(IEnumerable<string> variableNames, IEnumerable<IList> variableValues) : base(variableNames, variableValues) { } 74 // add values 75 for (int i = 0; i < list.Count; ++i) { 76 variableValues[variableNames[i]].Add(list[i]); 77 } 78 Rows++; 79 OnRowsChanged(); 80 OnReset(); 81 } 59 82 60 83 public void ReplaceRow(int row, IEnumerable<object> values) { … … 72 95 variableValues[variableNames[i]][row] = list[i]; 73 96 } 97 OnReset(); 98 } 99 100 // slow, avoid using this 101 public void RemoveRow(int row) { 102 foreach (var list in variableValues.Values) 103 list.RemoveAt(row); 104 Rows--; 105 OnRowsChanged(); 106 OnReset(); 107 } 108 109 // adds a new variable to the dataset 110 public void AddVariable(string variableName, IList values) { 111 InsertVariable(variableName, Columns, values); 112 } 113 114 public void InsertVariable(string variableName, int position, IList values) { 115 if (variableValues.ContainsKey(variableName)) 116 throw new ArgumentException(string.Format("Variable {0} is already present in the dataset.", variableName)); 117 118 if (position < 0 || position > Columns) 119 throw new ArgumentException(string.Format("Incorrect position {0} specified. The position must be between 0 and {1}.", position, Columns)); 120 121 if (values == null) 122 throw new ArgumentNullException("values", "Values must not be null. At least an empty list of values has to be provided."); 123 124 if (values.Count != Rows) 125 throw new ArgumentException(string.Format("{0} values are provided, but {1} rows are present in the dataset.", values.Count, Rows)); 126 127 if (!IsAllowedType(values)) 128 throw new ArgumentException(string.Format("Unsupported type {0} for variable {1}.", GetElementType(values), variableName)); 129 130 variableNames.Insert(position, variableName); 131 variableValues[variableName] = values; 132 133 OnColumnsChanged(); 134 OnColumnNamesChanged(); 74 135 OnReset(); 75 136 } … … 85 146 } 86 147 87 public void AddRow(IEnumerable<object> values) {88 var list = values.ToList();89 if (list.Count != variableNames.Count)90 throw new ArgumentException("The number of values must be equal to the number of variable names.");91 // check if all the values are of the correct type92 for (int i = 0; i < list.Count; ++i) {93 if (list[i].GetType() != GetVariableType(variableNames[i])) {94 throw new ArgumentException("The type of the provided value does not match the variable type.");95 }96 }97 // add values98 for (int i = 0; i < list.Count; ++i) {99 variableValues[variableNames[i]].Add(list[i]);100 }101 rows++;102 OnRowsChanged();103 OnReset();104 }105 106 // adds a new variable to the dataset107 public void AddVariable<T>(string variableName, IEnumerable<T> values) {108 if (variableValues.ContainsKey(variableName))109 throw new ArgumentException("Variable " + variableName + " is already present in the dataset.");110 int count = values.Count();111 if (count != rows)112 throw new ArgumentException("The number of values must exactly match the number of rows in the dataset.");113 variableValues[variableName] = new List<T>(values);114 variableNames.Add(variableName);115 OnColumnsChanged();116 OnColumnNamesChanged();117 OnReset();118 }119 148 120 149 public void RemoveVariable(string variableName) { 121 150 if (!variableValues.ContainsKey(variableName)) 122 throw new ArgumentException( "The variable " + variableName + " does not exist in the dataset.");151 throw new ArgumentException(string.Format("The variable {0} does not exist in the dataset.", variableName)); 123 152 variableValues.Remove(variableName); 124 153 variableNames.Remove(variableName); … … 128 157 } 129 158 130 // slow, avoid to use this131 public void RemoveRow(int row) {132 foreach (var list in variableValues.Values)133 list.RemoveAt(row);134 rows--;159 public void ClearValues() { 160 foreach (var list in variableValues.Values) { 161 list.Clear(); 162 } 163 Rows = 0; 135 164 OnRowsChanged(); 136 165 OnReset(); 137 166 } 167 138 168 139 169 public void SetVariableValue(object value, string variableName, int row) { … … 151 181 } 152 182 153 private Type GetVariableType(string variableName) {154 IList list;155 variableValues.TryGetValue(variableName, out list);156 if (list == null)157 throw new ArgumentException("The variable " + variableName + " does not exist in the dataset.");158 return list.GetType().GetGenericArguments()[0];159 }160 161 183 bool IStringConvertibleMatrix.SetValue(string value, int rowIndex, int columnIndex) { 162 184 var variableName = variableNames[columnIndex];
Note: See TracChangeset
for help on using the changeset viewer.