Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
07/30/18 16:59:47 (6 years ago)
Author:
fholzing
Message:

#2904: Removed callback, adapted both view and calculator.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/2904_CalculateImpacts/3.4/Implementation/Regression/RegressionSolutionVariableImpactsCalculator.cs

    r16031 r16034  
    106106      ReplacementMethodEnum replacementMethod = ReplacementMethodEnum.Shuffle,
    107107      FactorReplacementMethodEnum factorReplacementMethod = FactorReplacementMethodEnum.Best,
    108       DataPartitionEnum data = DataPartitionEnum.Training,
    109       Func<double, string, bool> progressCallback = null) {
    110       return CalculateImpacts(solution.Model, solution.ProblemData, solution.EstimatedValues, replacementMethod, factorReplacementMethod, data, progressCallback);
     108      DataPartitionEnum data = DataPartitionEnum.Training) {
     109      return CalculateImpacts(solution.Model, solution.ProblemData, solution.EstimatedValues, replacementMethod, factorReplacementMethod, data);
    111110    }
    112111
     
    117116      ReplacementMethodEnum replacementMethod = ReplacementMethodEnum.Shuffle,
    118117      FactorReplacementMethodEnum factorReplacementMethod = FactorReplacementMethodEnum.Best,
    119       DataPartitionEnum data = DataPartitionEnum.Training,
    120       Func<double, string, bool> progressCallback = null) {
     118      DataPartitionEnum data = DataPartitionEnum.Training) {
    121119      IEnumerable<int> rows;
    122120
     
    135133      }
    136134
    137       return CalculateImpacts(model, problemData, estimatedValues, rows, replacementMethod, factorReplacementMethod, progressCallback);
     135      return CalculateImpacts(model, problemData, estimatedValues, rows, replacementMethod, factorReplacementMethod);
     136    }
     137
     138    public static double CalculateImpact(string variableName, IRegressionModel model, IRegressionProblemData problemData, IEnumerable<double> estimatedValues, DataPartitionEnum dataPartition, ReplacementMethodEnum replMethod, FactorReplacementMethodEnum factorReplMethod) {
     139      double impact = 0;
     140
     141      IEnumerable<int> rows;
     142      switch (dataPartition) {
     143        case DataPartitionEnum.All:
     144          rows = problemData.AllIndices;
     145          break;
     146        case DataPartitionEnum.Test:
     147          rows = problemData.TestIndices;
     148          break;
     149        case DataPartitionEnum.Training:
     150          rows = problemData.TrainingIndices;
     151          break;
     152        default:
     153          throw new NotSupportedException("DataPartition not supported");
     154      }
     155
     156      OnlineCalculatorError error;
     157      IEnumerable<double> targetValuesPartition = rows.Select(v => problemData.TargetVariableValues.ElementAt(v));
     158      IEnumerable<double> estimatedValuesPartition = rows.Select(v => estimatedValues.ElementAt(v));
     159      var originalCalculatorValue = CalculateVariableImpact(targetValuesPartition, estimatedValuesPartition, out error);
     160      if (error != OnlineCalculatorError.None) throw new InvalidOperationException("Error during calculation.");
     161
     162
     163      var modifiableDataset = ((Dataset)(problemData.Dataset).Clone()).ToModifiable();
     164
     165      // calculate impacts for double variables
     166      if (problemData.Dataset.VariableHasType<double>(variableName)) {
     167        impact = CalculateImpactForNumericalVariables(variableName, model, modifiableDataset, rows, targetValuesPartition, originalCalculatorValue, replMethod);
     168      } else if (problemData.Dataset.VariableHasType<string>(variableName)) {
     169        impact = CalculateImpactForFactorVariables(variableName, model, problemData.Dataset, modifiableDataset, rows, targetValuesPartition, originalCalculatorValue, factorReplMethod);
     170      } else {
     171        throw new NotSupportedException("Variable not supported");
     172      }
     173      return impact;
    138174    }
    139175
     
    144180     IEnumerable<int> rows,
    145181     ReplacementMethodEnum replacementMethod = ReplacementMethodEnum.Shuffle,
    146      FactorReplacementMethodEnum factorReplacementMethod = FactorReplacementMethodEnum.Best,
    147      Func<double, string, bool> progressCallback = null) {
     182     FactorReplacementMethodEnum factorReplacementMethod = FactorReplacementMethodEnum.Best) {
    148183      //Calculate original quality-values (via calculator, default is R²)
    149184      OnlineCalculatorError error;
     
    157192      var allowedInputVariables = problemData.Dataset.VariableNames.Where(v => inputvariables.Contains(v)).ToList();
    158193
    159       int curIdx = 0;
    160       int count = allowedInputVariables.Count(v => problemData.Dataset.VariableHasType<double>(v) || problemData.Dataset.VariableHasType<string>(v));
    161 
    162194      foreach (var inputVariable in allowedInputVariables) {
    163         //Report the current progress in percent. If the callback returns true, it means the execution shall be stopped
    164         if (progressCallback != null) {
    165           curIdx++;
    166           if (progressCallback((double)curIdx / count, string.Format("Calculating impact for variable {0} ({1} of {2})", inputVariable, curIdx, count))) { return null; }
    167         }
    168195        impacts[inputVariable] = CalculateImpact(inputVariable, model, problemData.Dataset, rows, targetValuesPartition, originalCalculatorValue, replacementMethod, factorReplacementMethod);
    169196      }
     
    204231      }
    205232      return impact;
    206     }
    207 
    208     private static void PrepareData(IEnumerable<int> rows,
    209       IRegressionProblemData problemData,
    210       IEnumerable<double> estimatedValues,
    211       out IEnumerable<double> targetValues,
    212       out double originalValue) {
    213       OnlineCalculatorError error;
    214 
    215       var targetVariableValueList = problemData.TargetVariableValues.ToList();
    216       targetValues = rows.Select(v => targetVariableValueList.ElementAt(v));
    217       var estimatedValuesPartition = rows.Select(v => estimatedValues.ElementAt(v));
    218       originalValue = CalculateVariableImpact(targetValues, estimatedValuesPartition, out error);
    219 
    220       if (error != OnlineCalculatorError.None) throw new InvalidOperationException("Error during calculation.");
    221233    }
    222234
Note: See TracChangeset for help on using the changeset viewer.