Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
07/12/17 16:00:13 (7 years ago)
Author:
mkommend
Message:

#2807: Made partial dependence plot work with constant variable values.

Location:
trunk/sources/HeuristicLab.Problems.DataAnalysis.Views/3.4/Controls
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Problems.DataAnalysis.Views/3.4/Controls/FactorPartialDependencePlot.cs

    r15054 r15211  
    7373        if (value != yAxisTicks) {
    7474          yAxisTicks = value;
    75           SetupAxis(chart.ChartAreas[0].AxisY, yMin, yMax, YAxisTicks, FixedYAxisMin, FixedYAxisMax);
     75          SetupAxis(chart, chart.ChartAreas[0].AxisY, yMin, yMax, YAxisTicks, FixedYAxisMin, FixedYAxisMax);
    7676          RecalculateInternalDataset();
    7777        }
     
    8484        if ((value.HasValue && fixedYAxisMin.HasValue && !value.Value.IsAlmost(fixedYAxisMin.Value)) || (value.HasValue != fixedYAxisMin.HasValue)) {
    8585          fixedYAxisMin = value;
    86           SetupAxis(chart.ChartAreas[0].AxisY, yMin, yMax, YAxisTicks, FixedYAxisMin, FixedYAxisMax);
     86          SetupAxis(chart, chart.ChartAreas[0].AxisY, yMin, yMax, YAxisTicks, FixedYAxisMin, FixedYAxisMax);
    8787        }
    8888      }
     
    9494        if ((value.HasValue && fixedYAxisMax.HasValue && !value.Value.IsAlmost(fixedYAxisMax.Value)) || (value.HasValue != fixedYAxisMax.HasValue)) {
    9595          fixedYAxisMax = value;
    96           SetupAxis(chart.ChartAreas[0].AxisY, yMin, yMax, YAxisTicks, FixedYAxisMin, FixedYAxisMax);
     96          SetupAxis(chart, chart.ChartAreas[0].AxisY, yMin, yMax, YAxisTicks, FixedYAxisMin, FixedYAxisMax);
    9797        }
    9898      }
     
    209209      try {
    210210        var limits = await UpdateAllSeriesDataAsync(cancellationToken);
     211        chart.Invalidate();
    211212
    212213        yMin = limits.Lower;
     
    214215        // Set y-axis
    215216        if (resetYAxis)
    216           SetupAxis(chart.ChartAreas[0].AxisY, yMin, yMax, YAxisTicks, FixedYAxisMin, FixedYAxisMax);
     217          SetupAxis(chart, chart.ChartAreas[0].AxisY, yMin, yMax, YAxisTicks, FixedYAxisMin, FixedYAxisMax);
    217218
    218219        calculationPendingTimer.Stop();
     
    220221        if (updateOnFinish)
    221222          Update();
    222       } catch (OperationCanceledException) { } catch (AggregateException ae) {
     223      }
     224      catch (OperationCanceledException) { }
     225      catch (AggregateException ae) {
    223226        if (!ae.InnerExceptions.Any(e => e is OperationCanceledException))
    224227          throw;
     
    235238    }
    236239
    237     private void SetupAxis(Axis axis, double minValue, double maxValue, int ticks, double? fixedAxisMin, double? fixedAxisMax) {
    238       if (minValue < maxValue) {
    239         double axisMin, axisMax, axisInterval;
    240         ChartUtil.CalculateAxisInterval(minValue, maxValue, ticks, out axisMin, out axisMax, out axisInterval);
    241         axis.Minimum = fixedAxisMin ?? axisMin;
    242         axis.Maximum = fixedAxisMax ?? axisMax;
    243         axis.Interval = (axis.Maximum - axis.Minimum) / ticks;
    244       }
     240    private static void SetupAxis(EnhancedChart chart, Axis axis, double minValue, double maxValue, int ticks, double? fixedAxisMin, double? fixedAxisMax) {
     241      //guard if only one distinct value is present
     242      if (minValue.IsAlmost(maxValue)) {
     243        minValue = minValue - 0.5;
     244        maxValue = minValue + 0.5;
     245      }
     246
     247      double axisMin, axisMax, axisInterval;
     248      ChartUtil.CalculateAxisInterval(minValue, maxValue, ticks, out axisMin, out axisMax, out axisInterval);
     249      axis.Minimum = fixedAxisMin ?? axisMin;
     250      axis.Maximum = fixedAxisMax ?? axisMax;
     251      axis.Interval = (axis.Maximum - axis.Minimum) / ticks;
    245252
    246253      try {
    247254        chart.ChartAreas[0].RecalculateAxesScale();
    248       } catch (InvalidOperationException) {
     255      }
     256      catch (InvalidOperationException) {
    249257        // Can occur if eg. axis min == axis max
    250258      }
     
    343351          if (yvalues[i] > max) max = yvalues[i];
    344352        }
    345         chart.Invalidate();
    346353
    347354        cancellationToken.ThrowIfCancellationRequested();
     
    358365            if (upper > max) max = upper;
    359366          }
    360           chart.Invalidate();
    361367        }
    362368
  • trunk/sources/HeuristicLab.Problems.DataAnalysis.Views/3.4/Controls/PartialDependencePlot.cs

    r14852 r15211  
    8282        if (value != xAxisTicks) {
    8383          xAxisTicks = value;
    84           SetupAxis(chart.ChartAreas[0].AxisX, trainingMin, trainingMax, XAxisTicks, FixedXAxisMin, FixedXAxisMax);
     84          SetupAxis(chart, chart.ChartAreas[0].AxisX, trainingMin, trainingMax, XAxisTicks, FixedXAxisMin, FixedXAxisMax);
    8585          RecalculateInternalDataset();
    8686        }
     
    9393        if ((value.HasValue && fixedXAxisMin.HasValue && !value.Value.IsAlmost(fixedXAxisMin.Value)) || (value.HasValue != fixedXAxisMin.HasValue)) {
    9494          fixedXAxisMin = value;
    95           if (trainingMin < trainingMax) {
    96             SetupAxis(chart.ChartAreas[0].AxisX, trainingMin, trainingMax, XAxisTicks, FixedXAxisMin, FixedXAxisMax);
    97             RecalculateInternalDataset();
    98             // set the vertical line position
    99             if (VerticalLineAnnotation.X <= fixedXAxisMin) {
    100               var axisX = chart.ChartAreas[0].AxisX;
    101               var step = (axisX.Maximum - axisX.Minimum) / drawingSteps;
    102               VerticalLineAnnotation.X = axisX.Minimum + step;
    103             }
     95          SetupAxis(chart, chart.ChartAreas[0].AxisX, trainingMin, trainingMax, XAxisTicks, FixedXAxisMin, FixedXAxisMax);
     96          RecalculateInternalDataset();
     97          // set the vertical line position
     98          if (VerticalLineAnnotation.X <= fixedXAxisMin) {
     99            var axisX = chart.ChartAreas[0].AxisX;
     100            var step = (axisX.Maximum - axisX.Minimum) / drawingSteps;
     101            VerticalLineAnnotation.X = axisX.Minimum + step;
    104102          }
    105103        }
     
    112110        if ((value.HasValue && fixedXAxisMax.HasValue && !value.Value.IsAlmost(fixedXAxisMax.Value)) || (value.HasValue != fixedXAxisMax.HasValue)) {
    113111          fixedXAxisMax = value;
    114           if (trainingMin < trainingMax) {
    115             SetupAxis(chart.ChartAreas[0].AxisX, trainingMin, trainingMax, XAxisTicks, FixedXAxisMin, FixedXAxisMax);
    116             RecalculateInternalDataset();
    117             // set the vertical line position
    118             if (VerticalLineAnnotation.X >= fixedXAxisMax) {
    119               var axisX = chart.ChartAreas[0].AxisX;
    120               var step = (axisX.Maximum - axisX.Minimum) / drawingSteps;
    121               VerticalLineAnnotation.X = axisX.Maximum - step;
    122             }
     112          SetupAxis(chart, chart.ChartAreas[0].AxisX, trainingMin, trainingMax, XAxisTicks, FixedXAxisMin, FixedXAxisMax);
     113          RecalculateInternalDataset();
     114          // set the vertical line position
     115          if (VerticalLineAnnotation.X >= fixedXAxisMax) {
     116            var axisX = chart.ChartAreas[0].AxisX;
     117            var step = (axisX.Maximum - axisX.Minimum) / drawingSteps;
     118            VerticalLineAnnotation.X = axisX.Maximum - step;
    123119          }
    124120        }
     
    132128        if (value != yAxisTicks) {
    133129          yAxisTicks = value;
    134           SetupAxis(chart.ChartAreas[0].AxisY, yMin, yMax, YAxisTicks, FixedYAxisMin, FixedYAxisMax);
     130          SetupAxis(chart, chart.ChartAreas[0].AxisY, yMin, yMax, YAxisTicks, FixedYAxisMin, FixedYAxisMax);
    135131          RecalculateInternalDataset();
    136132        }
     
    143139        if ((value.HasValue && fixedYAxisMin.HasValue && !value.Value.IsAlmost(fixedYAxisMin.Value)) || (value.HasValue != fixedYAxisMin.HasValue)) {
    144140          fixedYAxisMin = value;
    145           SetupAxis(chart.ChartAreas[0].AxisY, yMin, yMax, YAxisTicks, FixedYAxisMin, FixedYAxisMax);
     141          SetupAxis(chart, chart.ChartAreas[0].AxisY, yMin, yMax, YAxisTicks, FixedYAxisMin, FixedYAxisMax);
    146142        }
    147143      }
     
    153149        if ((value.HasValue && fixedYAxisMax.HasValue && !value.Value.IsAlmost(fixedYAxisMax.Value)) || (value.HasValue != fixedYAxisMax.HasValue)) {
    154150          fixedYAxisMax = value;
    155           SetupAxis(chart.ChartAreas[0].AxisY, yMin, yMax, YAxisTicks, FixedYAxisMin, FixedYAxisMax);
     151          SetupAxis(chart, chart.ChartAreas[0].AxisY, yMin, yMax, YAxisTicks, FixedYAxisMin, FixedYAxisMax);
    156152        }
    157153      }
     
    293289      if (IsDisposed
    294290        || sharedFixedVariables == null || !solutions.Any() || string.IsNullOrEmpty(freeVariable)
    295         || trainingMin.IsAlmost(trainingMax) || trainingMin > trainingMax || drawingSteps == 0)
     291        || trainingMin > trainingMax || drawingSteps == 0)
    296292        return;
    297293
     
    307303      try {
    308304        var limits = await UpdateAllSeriesDataAsync(cancellationToken);
     305        chart.Invalidate();
    309306
    310307        yMin = limits.Lower;
     
    312309        // Set y-axis
    313310        if (resetYAxis)
    314           SetupAxis(chart.ChartAreas[0].AxisY, yMin, yMax, YAxisTicks, FixedYAxisMin, FixedYAxisMax);
     311          SetupAxis(chart, chart.ChartAreas[0].AxisY, yMin, yMax, YAxisTicks, FixedYAxisMin, FixedYAxisMax);
    315312
    316313        UpdateOutOfTrainingRangeStripLines();
     
    320317        if (updateOnFinish)
    321318          Update();
    322       } catch (OperationCanceledException) { } catch (AggregateException ae) {
     319      }
     320      catch (OperationCanceledException) { }
     321      catch (AggregateException ae) {
    323322        if (!ae.InnerExceptions.Any(e => e is OperationCanceledException))
    324323          throw;
     
    335334    }
    336335
    337     private void SetupAxis(Axis axis, double minValue, double maxValue, int ticks, double? fixedAxisMin, double? fixedAxisMax) {
    338       if (minValue < maxValue) {
    339         double axisMin, axisMax, axisInterval;
    340         ChartUtil.CalculateAxisInterval(minValue, maxValue, ticks, out axisMin, out axisMax, out axisInterval);
    341         axis.Minimum = fixedAxisMin ?? axisMin;
    342         axis.Maximum = fixedAxisMax ?? axisMax;
    343         axis.Interval = (axis.Maximum - axis.Minimum) / ticks;
    344       }
     336    private static void SetupAxis(EnhancedChart chart, Axis axis, double minValue, double maxValue, int ticks, double? fixedAxisMin, double? fixedAxisMax) {
     337      //guard if only one distinct value is present
     338      if (minValue.IsAlmost(maxValue)) {
     339        minValue = minValue - 0.5;
     340        maxValue = minValue + 0.5;
     341      }
     342
     343      double axisMin, axisMax, axisInterval;
     344      ChartUtil.CalculateAxisInterval(minValue, maxValue, ticks, out axisMin, out axisMax, out axisInterval);
     345      axis.Minimum = fixedAxisMin ?? axisMin;
     346      axis.Maximum = fixedAxisMax ?? axisMax;
     347      axis.Interval = (axis.Maximum - axis.Minimum) / ticks;
    345348
    346349      try {
    347350        chart.ChartAreas[0].RecalculateAxesScale();
    348       } catch (InvalidOperationException) {
     351      }
     352      catch (InvalidOperationException) {
    349353        // Can occur if eg. axis min == axis max
    350354      }
     
    357361      if (initializeAxisRanges) {
    358362        double xmin, xmax, xinterval;
    359         ChartUtil.CalculateAxisInterval(trainingMin, trainingMax, XAxisTicks, out xmin, out xmax, out xinterval);
     363        //guard if only one distinct value is present
     364        if (trainingMin.IsAlmost(trainingMax))
     365          ChartUtil.CalculateAxisInterval(trainingMin - 0.5, trainingMax + 0.5, XAxisTicks, out xmin, out xmax, out xinterval);
     366        else
     367          ChartUtil.CalculateAxisInterval(trainingMin, trainingMax, XAxisTicks, out xmin, out xmax, out xinterval);
     368
    360369        FixedXAxisMin = xmin;
    361370        FixedXAxisMax = xmax;
     
    369378      // we expand the range in order to get nice tick intervals on the x axis
    370379      double xmin, xmax, xinterval;
    371       ChartUtil.CalculateAxisInterval(trainingMin, trainingMax, XAxisTicks, out xmin, out xmax, out xinterval);
     380      //guard if only one distinct value is present
     381      if (trainingMin.IsAlmost(trainingMax))
     382        ChartUtil.CalculateAxisInterval(trainingMin - 0.5, trainingMin + 0.5, XAxisTicks, out xmin, out xmax, out xinterval);
     383      else
     384        ChartUtil.CalculateAxisInterval(trainingMin, trainingMax, XAxisTicks, out xmin, out xmax, out xinterval);
    372385
    373386      if (FixedXAxisMin.HasValue) xmin = FixedXAxisMin.Value;
     
    470483          if (yvalues[i] > max) max = yvalues[i];
    471484        }
    472         chart.Invalidate();
    473485
    474486        cancellationToken.ThrowIfCancellationRequested();
     
    485497            if (upper > max) max = upper;
    486498          }
    487           chart.Invalidate();
    488499        }
    489500
Note: See TracChangeset for help on using the changeset viewer.