Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
01/08/19 15:29:57 (5 years ago)
Author:
pfleck
Message:

#2972 merged to trunk

Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk

  • trunk/HeuristicLab.Problems.DataAnalysis.Views

  • trunk/HeuristicLab.Problems.DataAnalysis.Views/3.4/Regression/RegressionSolutionPartialDependencePlotView.cs

    r15583 r16519  
    9797      var problemData = Content.ProblemData;
    9898
     99      if (sharedFixedVariables != null) {
     100        sharedFixedVariables.ItemChanged -= SharedFixedVariables_ItemChanged;
     101        sharedFixedVariables.Reset -= SharedFixedVariables_Reset;
     102      }
     103
    99104      // Init Y-axis range
    100105      double min = double.MaxValue, max = double.MinValue;
     
    120125        Content.ProblemData.Dataset.VariableNames.Where(v => inputvariables.Contains(v)).ToList();
    121126
    122 
    123127      var doubleVariables = allowedInputVariables.Where(problemData.Dataset.VariableHasType<double>);
    124       var doubleVariableValues = (IEnumerable<IList>)doubleVariables.Select(x => new List<double> { problemData.Dataset.GetDoubleValues(x, problemData.TrainingIndices).Median() });
     128      var doubleVariableValues = (IEnumerable<IList>)doubleVariables.Select(x => new List<double> {
     129        problemData.Dataset.GetDoubleValue(x, 0)
     130      });
    125131
    126132      var factorVariables = allowedInputVariables.Where(problemData.Dataset.VariableHasType<string>);
    127133      var factorVariableValues = (IEnumerable<IList>)factorVariables.Select(x => new List<string> {
    128         problemData.Dataset.GetStringValues(x, problemData.TrainingIndices)
    129         .GroupBy(val => val).OrderByDescending(g => g.Count()).First().Key // most frequent value
     134        problemData.Dataset.GetStringValue(x, 0)
    130135      });
    131136
    132       if (sharedFixedVariables != null)
    133         sharedFixedVariables.ItemChanged -= SharedFixedVariables_ItemChanged;
    134 
    135137      sharedFixedVariables = new ModifiableDataset(doubleVariables.Concat(factorVariables), doubleVariableValues.Concat(factorVariableValues));
    136 
     138      variableValuesModeComboBox.SelectedItem = "Median"; // triggers UpdateVariableValue and changes shardFixedVariables
    137139
    138140      // create controls
     
    166168        };
    167169
    168         // Initially, the inner plot areas are not initialized for hidden charts (scollpanel, ...)
     170        // Initially, the inner plot areas are not initialized for hidden charts (scrollpanel, ...)
    169171        // This event handler listens for the paint event once (where everything is already initialized) to do some manual layouting.
    170172        plot.ChartPostPaint += OnPartialDependencePlotPostPaint;
     
    205207        };
    206208
    207         // Initially, the inner plot areas are not initialized for hidden charts (scollpanel, ...)
     209        // Initially, the inner plot areas are not initialized for hidden charts (scrollpanel, ...)
    208210        // This event handler listens for the paint event once (where everything is already initialized) to do some manual layouting.
    209211        plot.ChartPostPaint += OnFactorPartialDependencePlotPostPaint;
     
    230232
    231233      sharedFixedVariables.ItemChanged += SharedFixedVariables_ItemChanged;
     234      sharedFixedVariables.Reset += SharedFixedVariables_Reset;
     235
     236      rowNrNumericUpDown.Maximum = Content.ProblemData.Dataset.Rows - 1;
    232237
    233238      RecalculateAndRelayoutCharts();
     
    235240
    236241    private void SharedFixedVariables_ItemChanged(object sender, EventArgs<int, int> e) {
     242      SharedFixedVariablesChanged();
     243    }
     244    private void SharedFixedVariables_Reset(object sender, EventArgs e) {
     245      SharedFixedVariablesChanged();
     246    }
     247    private void SharedFixedVariablesChanged() {
     248      if (!setVariableValues) // set mode to "nothing" if change was not initiated from a "mode change"
     249        variableValuesModeComboBox.SelectedIndex = -1;
     250
    237251      double yValue = Content.Model.GetEstimatedValues(sharedFixedVariables, new[] { 0 }).Single();
    238252      string title = Content.ProblemData.TargetVariable + ": " + yValue.ToString("G5", CultureInfo.CurrentCulture);
     
    243257      }
    244258    }
    245 
    246259
    247260    private void OnPartialDependencePlotPostPaint(object o, EventArgs e) {
     
    564577      }
    565578    }
     579
     580    // flag that the current change is not triggered by a manual change from within a single plot
     581    private bool setVariableValues = false;
     582    private void variableValuesComboBox_SelectedValueChanged(object sender, EventArgs e) {
     583      if (variableValuesModeComboBox.SelectedIndex == -1)
     584        return; // changed to "manual" due to manual change of a variable
     585      setVariableValues = true;
     586      UpdateVariableValues();
     587      setVariableValues = false;
     588    }
     589    private void rowNrNumericUpDown_ValueChanged(object sender, EventArgs e) {
     590      if ((string)variableValuesModeComboBox.SelectedItem != "Row") {
     591        variableValuesModeComboBox.SelectedItem = "Row"; // triggers UpdateVariableValues
     592      } else {
     593        setVariableValues = true;
     594        UpdateVariableValues();
     595        setVariableValues = false;
     596      }
     597    }
     598    private void UpdateVariableValues() {
     599      string mode = (string)variableValuesModeComboBox.SelectedItem;
     600
     601      var dataset = Content.ProblemData.Dataset;
     602      object[] newRow;
     603
     604      if (mode == "Row") {
     605        int rowNumber = (int)rowNrNumericUpDown.Value;
     606        newRow = sharedFixedVariables.VariableNames
     607          .Select<string, object>(variableName => {
     608            if (dataset.DoubleVariables.Contains(variableName)) {
     609              return dataset.GetDoubleValue(variableName, rowNumber);
     610            } else if (dataset.StringVariables.Contains(variableName)) {
     611              return dataset.GetStringValue(variableName, rowNumber);
     612            } else {
     613              throw new NotSupportedException("Only double and string(factor) columns are currently supported.");
     614            }
     615          }).ToArray();
     616      } else {
     617        newRow = sharedFixedVariables.VariableNames
     618          .Select<string, object>(variableName => {
     619            if (dataset.DoubleVariables.Contains(variableName)) {
     620              var values = dataset.GetDoubleValues(variableName);
     621              return
     622                mode == "Mean" ? values.Average() :
     623                mode == "Median" ? values.Median() :
     624                mode == "Most Common" ? MostCommon(values) :
     625                throw new NotSupportedException();
     626            } else if (dataset.StringVariables.Contains(variableName)) {
     627              var values = dataset.GetStringValues(variableName);
     628              return
     629                mode == "Mean" ? MostCommon(values) :
     630                mode == "Median" ? MostCommon(values) :
     631                mode == "Most Common" ? MostCommon(values) :
     632                throw new NotSupportedException();
     633            } else {
     634              throw new NotSupportedException("Only double and string(factor) columns are currently supported.");
     635            }
     636          }).ToArray();
     637      }
     638
     639      sharedFixedVariables.ReplaceRow(0, newRow);
     640    }
     641
     642    private static T MostCommon<T>(IEnumerable<T> values) {
     643      return values.GroupBy(x => x).OrderByDescending(g => g.Count()).Select(g => g.Key).First();
     644    }
     645
     646    // ToolTips cannot be shown longer than 5000ms, only by using ToolTip.Show manually
     647    // See: https://stackoverflow.com/questions/8225807/c-sharp-tooltip-doesnt-display-long-enough
     648    private void variableValuesModeComboBox_MouseHover(object sender, EventArgs e) {
     649      string tooltipText = @"Sets each variable to a specific value:
     650    Row - Selects the value based on a specified row of the dataset.
     651    Mean - Sets the value to the arithmetic mean of the variable.
     652    Median - Sets the value to the median of the variable.
     653    Most Common - Sets the value to the most common value of the variable (first if multiple).
     654
     655Note: For categorical values, the most common value is used when selecting Mean, Median or Most Common.";
     656      toolTip.Show(tooltipText, variableValuesModeComboBox, 30000);
     657      toolTip.Active = true;
     658    }
     659    private void variableValuesModeComboBox_MouseLeave(object sender, EventArgs e) {
     660      toolTip.Active = false;
     661    }
    566662  }
    567663}
Note: See TracChangeset for help on using the changeset viewer.