Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
07/29/17 16:03:05 (7 years ago)
Author:
gkronber
Message:

#2695: added a parameter to DataAnalysisProblemData to select an id column and extended RegressionLinechartViewBase to show the id on the xAxis

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/dataset-ids-2695/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/DataAnalysisProblemData.cs

    r14843 r15297  
    2121
    2222using System;
     23using System.Collections;
    2324using System.Collections.Generic;
    2425using System.Linq;
     
    3940    protected const string TestPartitionParameterName = "TestPartition";
    4041    protected const string TransformationsParameterName = "Transformations";
    41 
     42    protected const string IdVariableParameterName = "Id Variable";
     43    protected const string EmptyIdVariableName = "-";
    4244    #region parameter properites
    4345    //mkommend: inserted parameter caching due to performance reasons
     
    5860    }
    5961
     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
    6070    private IFixedValueParameter<IntRange> trainingPartitionParameter;
    6171    public IFixedValueParameter<IntRange> TrainingPartitionParameter {
     
    92102    public IEnumerable<string> AllowedInputVariables {
    93103      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      }
    94138    }
    95139
     
    152196        TransformationsParameter.Hidden = true;
    153197      }
     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      }
    154204      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));
    155225    }
    156226
     
    179249      Parameters.Add(new FixedValueParameter<IntRange>(TestPartitionParameterName, "", new IntRange(testPartitionStart, testPartitionEnd)));
    180250      Parameters.Add(new FixedValueParameter<ReadOnlyItemList<ITransformation>>(TransformationsParameterName, "", transformationsList.AsReadOnly()));
    181 
    182251      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()));
    183254
    184255      ((ValueParameter<Dataset>)DatasetParameter).ReactOnValueToStringChangedAndValueItemImageChanged = false;
     
    199270
    200271    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
    201282      OnChanged();
    202283    }
Note: See TracChangeset for help on using the changeset viewer.