- Timestamp:
- 07/29/17 16:03:05 (7 years ago)
- Location:
- branches/dataset-ids-2695/HeuristicLab.Problems.DataAnalysis/3.4
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/dataset-ids-2695/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/DataAnalysisProblemData.cs
r14843 r15297 21 21 22 22 using System; 23 using System.Collections; 23 24 using System.Collections.Generic; 24 25 using System.Linq; … … 39 40 protected const string TestPartitionParameterName = "TestPartition"; 40 41 protected const string TransformationsParameterName = "Transformations"; 41 42 protected const string IdVariableParameterName = "Id Variable"; 43 protected const string EmptyIdVariableName = "-"; 42 44 #region parameter properites 43 45 //mkommend: inserted parameter caching due to performance reasons … … 58 60 } 59 61 62 private IConstrainedValueParameter<StringValue> idVariableParameter; 63 public IConstrainedValueParameter<StringValue> IdVariableParameter { 64 get { 65 if (idVariableParameter == null) idVariableParameter = (IConstrainedValueParameter<StringValue>)Parameters[IdVariableParameterName]; 66 return idVariableParameter; 67 } 68 } 69 60 70 private IFixedValueParameter<IntRange> trainingPartitionParameter; 61 71 public IFixedValueParameter<IntRange> TrainingPartitionParameter { … … 92 102 public IEnumerable<string> AllowedInputVariables { 93 103 get { return InputVariables.CheckedItems.Select(x => x.Value.Value); } 104 } 105 public string IdVariable { 106 get { return IdVariableParameter.Value.Value; } 107 // set only possible via parameter 108 } 109 public IEnumerable AllIds { 110 get { 111 var idVar = IdVariable; 112 if (idVar == EmptyIdVariableName) return Enumerable.Range(0, Dataset.Rows); 113 else if (Dataset.VariableHasType<double>(idVar)) return Dataset.GetDoubleValues(idVar); 114 else if (Dataset.VariableHasType<DateTime>(idVar)) return Dataset.GetDateTimeValues(idVar); 115 else if (Dataset.VariableHasType<string>(idVar)) return Dataset.GetStringValues(idVar); 116 else throw new NotSupportedException("The data type of the id variable is not supported"); 117 } 118 } 119 public IEnumerable TrainingIds { 120 get { 121 var idVar = IdVariable; 122 if (idVar == EmptyIdVariableName) return TrainingIndices; 123 else if (Dataset.VariableHasType<double>(idVar)) return Dataset.GetDoubleValues(idVar, TrainingIndices); 124 else if (Dataset.VariableHasType<DateTime>(idVar)) return Dataset.GetDateTimeValues(idVar, TrainingIndices); 125 else if (Dataset.VariableHasType<string>(idVar)) return Dataset.GetStringValues(idVar, TrainingIndices); 126 else throw new NotSupportedException("The data type of the id variable is not supported"); 127 } 128 } 129 public IEnumerable TestIds { 130 get { 131 var idVar = IdVariable; 132 if (idVar == EmptyIdVariableName) return TestIndices; 133 else if (Dataset.VariableHasType<double>(idVar)) return Dataset.GetDoubleValues(idVar, TestIndices); 134 else if (Dataset.VariableHasType<DateTime>(idVar)) return Dataset.GetDateTimeValues(idVar, TestIndices); 135 else if (Dataset.VariableHasType<string>(idVar)) return Dataset.GetStringValues(idVar, TestIndices); 136 else throw new NotSupportedException("The data type of the id variable is not supported"); 137 } 94 138 } 95 139 … … 152 196 TransformationsParameter.Hidden = true; 153 197 } 198 if (!Parameters.ContainsKey(IdVariableParameterName)) { 199 var allIdVars = new ItemSet<StringValue>(GetIdVariableNames()); 200 var idVar = allIdVars.First(); 201 Parameters.Add(new ConstrainedValueParameter<StringValue>(IdVariableParameterName, "", allIdVars, idVar)); 202 TransformationsParameter.Hidden = true; 203 } 154 204 RegisterEventHandlers(); 205 } 206 207 private IEnumerable<StringValue> GetIdVariableNames() { 208 // all variables with number of different values == number of rows 209 // + a separate entry for row id 210 // select separate entry for row id as default 211 // use event to update when the dataset is changed 212 var nRows = Dataset.Rows; 213 var potentialDoubleIdVars = Dataset.DoubleVariables 214 .Where(vn => Dataset.GetReadOnlyDoubleValues(vn).Distinct().Count() == nRows).ToArray(); 215 var potentialStringIdVars = Dataset.StringVariables 216 .Where(vn => Dataset.GetReadOnlyStringValues(vn).Distinct().Count() == nRows).ToArray(); 217 var potentialDateTimeIdVars = Dataset.DateTimeVariables 218 .Where(vn => Dataset.GetReadOnlyDateTimeValues(vn).Distinct().Count() == nRows).ToArray(); 219 return 220 new string[] { EmptyIdVariableName } 221 .Concat(potentialStringIdVars) 222 .Concat(potentialDateTimeIdVars) 223 .Concat(potentialDoubleIdVars) 224 .Select(s => new StringValue(s)); 155 225 } 156 226 … … 179 249 Parameters.Add(new FixedValueParameter<IntRange>(TestPartitionParameterName, "", new IntRange(testPartitionStart, testPartitionEnd))); 180 250 Parameters.Add(new FixedValueParameter<ReadOnlyItemList<ITransformation>>(TransformationsParameterName, "", transformationsList.AsReadOnly())); 181 182 251 TransformationsParameter.Hidden = true; 252 var allIdVars = new ItemSet<StringValue>(GetIdVariableNames()); 253 Parameters.Add(new ConstrainedValueParameter<StringValue>(IdVariableParameterName, "The variable to use as an ID column", allIdVars, allIdVars.First())); 183 254 184 255 ((ValueParameter<Dataset>)DatasetParameter).ReactOnValueToStringChangedAndValueItemImageChanged = false; … … 199 270 200 271 private void Parameter_ValueChanged(object sender, EventArgs e) { 272 // update list of possible id variables and set currently set id variable if possible 273 var curId = IdVariable; 274 var newIdVars = GetIdVariableNames().ToArray(); 275 var matchingIdVar = newIdVars.FirstOrDefault(var => var.Value == curId); 276 if (matchingIdVar == null) matchingIdVar = newIdVars.First(); 277 IdVariableParameter.ValidValues.Clear(); 278 var validValues = IdVariableParameter.ValidValues; 279 foreach(var entry in newIdVars) validValues.Add(entry); 280 IdVariableParameter.Value = matchingIdVar; 281 201 282 OnChanged(); 202 283 } -
branches/dataset-ids-2695/HeuristicLab.Problems.DataAnalysis/3.4/Interfaces/IDataAnalysisProblemData.cs
r14843 r15297 21 21 22 22 using System; 23 using System.Collections; 23 24 using System.Collections.Generic; 24 25 using HeuristicLab.Core; … … 32 33 ICheckedItemList<StringValue> InputVariables { get; } 33 34 IEnumerable<string> AllowedInputVariables { get; } 35 string IdVariable { get; } 34 36 35 37 double[,] AllowedInputsTrainingValues { get; } … … 39 41 IntRange TestPartition { get; } 40 42 43 IEnumerable AllIds { get; } 44 IEnumerable TrainingIds { get; } 45 IEnumerable TestIds { get; } 41 46 IEnumerable<int> AllIndices { get; } 42 47 IEnumerable<int> TrainingIndices { get; }
Note: See TracChangeset
for help on using the changeset viewer.