Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
03/19/14 10:48:23 (11 years ago)
Author:
rstoll
Message:
  • added InTranscation to ITransactionalPreprocessingData and used it in ManipulationLogic
Location:
branches/DataPreprocessing/HeuristicLab.DataPreprocessing/3.3
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • branches/DataPreprocessing/HeuristicLab.DataPreprocessing/3.3/Implementations/ManipulationLogic.cs

    r10590 r10612  
    4444
    4545    public void ReplaceIndicesByAverageValue(Dictionary<int, List<int>> cells) {
    46       preprocessingData.BeginTransaction();
    47       foreach (var column in cells) {
    48         double average = statisticInfo.GetAverage(column.Key);
    49         ReplaceIndicesByValue<double>(column.Key, column.Value, average);
    50       }
    51       preprocessingData.EndTransaction();
     46      preprocessingData.InTransaction(() => {
     47        foreach (var column in cells) {
     48          double average = statisticInfo.GetAverage(column.Key);
     49          ReplaceIndicesByValue<double>(column.Key, column.Value, average);
     50        }
     51      });
    5252    }
    5353
    5454    public void ReplaceIndicesByMedianValue(Dictionary<int, List<int>> cells) {
    55       preprocessingData.BeginTransaction();
    56       foreach (var column in cells) {
    57         double median = statisticInfo.GetMedian(column.Key);
    58         ReplaceIndicesByValue<double>(column.Key, column.Value, median);
    59       }
    60       preprocessingData.EndTransaction();
     55      preprocessingData.InTransaction(() => {
     56        foreach (var column in cells) {
     57          double median = statisticInfo.GetMedian(column.Key);
     58          ReplaceIndicesByValue<double>(column.Key, column.Value, median);
     59        }
     60      });
    6161    }
    6262
    6363    public void ReplaceIndicesByRandomValue(Dictionary<int, List<int>> cells) {
    64       preprocessingData.BeginTransaction();
    65       Random r = new Random();
    66 
    67       foreach (var column in cells) {
    68         double max = statisticInfo.GetMax<double>(column.Key);
    69         double min = statisticInfo.GetMin<double>(column.Key);
    70         double randMultiplier = (max - min);
    71         foreach (int index in column.Value) {
    72           double rand = r.NextDouble() * randMultiplier + min;
    73           preprocessingData.SetCell<double>(column.Key, index, rand);
    74         }
    75       }
    76       preprocessingData.EndTransaction();
     64      preprocessingData.InTransaction(() => {
     65        Random r = new Random();
     66
     67        foreach (var column in cells) {
     68          double max = statisticInfo.GetMax<double>(column.Key);
     69          double min = statisticInfo.GetMin<double>(column.Key);
     70          double randMultiplier = (max - min);
     71          foreach (int index in column.Value) {
     72            double rand = r.NextDouble() * randMultiplier + min;
     73            preprocessingData.SetCell<double>(column.Key, index, rand);
     74          }
     75        }
     76      });
    7777    }
    7878
    7979    public void ReplaceIndicesByLinearInterpolationOfNeighbours(Dictionary<int, List<int>> cells) {
    80       preprocessingData.BeginTransaction();
    81       foreach (var column in cells) {
    82         int countValues = preprocessingData.GetValues<double>(column.Key).Count();
    83         foreach (int index in column.Value) {
    84           // dont replace first or last values
    85           if (index > 0 && index < countValues) {
    86             int prevIndex = indexOfPrevPresentValue(column.Key, index);
    87             int nextIndex = indexOfNextPresentValue(column.Key, index);
    88 
    89             // no neighbours found
    90             if (prevIndex < 0 && nextIndex >= countValues) {
    91               continue;
     80      preprocessingData.InTransaction(() => {
     81        foreach (var column in cells) {
     82          int countValues = preprocessingData.GetValues<double>(column.Key).Count();
     83          foreach (int index in column.Value) {
     84            // dont replace first or last values
     85            if (index > 0 && index < countValues) {
     86              int prevIndex = indexOfPrevPresentValue(column.Key, index);
     87              int nextIndex = indexOfNextPresentValue(column.Key, index);
     88
     89              // no neighbours found
     90              if (prevIndex < 0 && nextIndex >= countValues) {
     91                continue;
     92              }
     93              double prev = preprocessingData.GetCell<double>(column.Key, prevIndex);
     94              double next = preprocessingData.GetCell<double>(column.Key, nextIndex);
     95
     96              int valuesToInterpolate = nextIndex - prevIndex;
     97
     98              double interpolationStep = (prev + next) / valuesToInterpolate;
     99
     100              for (int i = prevIndex; i < nextIndex; ++i) {
     101                double interpolated = prev + (interpolationStep * (i - prevIndex));
     102                preprocessingData.SetCell<double>(column.Key, i, interpolated);
     103              }
    92104            }
    93             double prev = preprocessingData.GetCell<double>(column.Key, prevIndex);
    94             double next = preprocessingData.GetCell<double>(column.Key, nextIndex);
    95 
    96             int valuesToInterpolate = nextIndex - prevIndex;
    97 
    98             double interpolationStep = (prev + next) / valuesToInterpolate;
    99 
    100             for (int i = prevIndex; i < nextIndex; ++i) {
    101               double interpolated = prev + (interpolationStep * (i - prevIndex));
    102               preprocessingData.SetCell<double>(column.Key, i, interpolated);
    103             }
    104           }
    105         }
    106       }
    107       preprocessingData.EndTransaction();
     105          }
     106        }
     107      });
    108108    }
    109109
     
    127127
    128128    public void ReplaceIndicesByMostCommonValue(Dictionary<int, List<int>> cells) {
    129       preprocessingData.BeginTransaction();
    130       foreach (var column in cells) {
    131         if (preprocessingData.IsType<double>(column.Key)) {
    132           ReplaceIndicesByValue<double>(column.Key, column.Value, statisticInfo.GetMostCommonValue<double>(column.Key));
    133         } else if (preprocessingData.IsType<string>(column.Key)) {
    134           ReplaceIndicesByValue<string>(column.Key, column.Value, statisticInfo.GetMostCommonValue<string>(column.Key));
    135         } else if (preprocessingData.IsType<DateTime>(column.Key)) {
    136           ReplaceIndicesByValue<DateTime>(column.Key, column.Value, statisticInfo.GetMostCommonValue<DateTime>(column.Key));
    137         } else {
    138           throw new ArgumentException("column with index: " + column.Key + " contains a non supported type.");
    139         }
    140       }
    141       preprocessingData.EndTransaction();
     129      preprocessingData.InTransaction(() => {
     130        foreach (var column in cells) {
     131          if (preprocessingData.IsType<double>(column.Key)) {
     132            ReplaceIndicesByValue<double>(column.Key, column.Value, statisticInfo.GetMostCommonValue<double>(column.Key));
     133          } else if (preprocessingData.IsType<string>(column.Key)) {
     134            ReplaceIndicesByValue<string>(column.Key, column.Value, statisticInfo.GetMostCommonValue<string>(column.Key));
     135          } else if (preprocessingData.IsType<DateTime>(column.Key)) {
     136            ReplaceIndicesByValue<DateTime>(column.Key, column.Value, statisticInfo.GetMostCommonValue<DateTime>(column.Key));
     137          } else {
     138            throw new ArgumentException("column with index: " + column.Key + " contains a non supported type.");
     139          }
     140        }
     141      });
    142142    }
    143143
     
    146146      Random random = new Random();
    147147
    148       preprocessingData.BeginTransaction();
    149       // process all given ranges - e.g. TrainingPartition, Trainingpartition
    150       foreach (IntRange range in ranges) {
    151         List<Tuple<int, int>> shuffledIndices = new List<Tuple<int, int>>();
    152 
    153         // generate random indices used for shuffeling each column
    154         for (int i = range.End; i > range.Start; --i) {
    155           int rand = random.Next(range.Start, i);
    156           shuffledIndices.Add(new Tuple<int, int>(i, rand));
    157         }
    158 
    159         ReOrderToIndices(shuffledIndices);
    160       }
    161       preprocessingData.EndTransaction();
     148      preprocessingData.InTransaction(() => {
     149        // process all given ranges - e.g. TrainingPartition, Trainingpartition
     150        foreach (IntRange range in ranges) {
     151          List<Tuple<int, int>> shuffledIndices = new List<Tuple<int, int>>();
     152
     153          // generate random indices used for shuffeling each column
     154          for (int i = range.End; i > range.Start; --i) {
     155            int rand = random.Next(range.Start, i);
     156            shuffledIndices.Add(new Tuple<int, int>(i, rand));
     157          }
     158
     159          ReOrderToIndices(shuffledIndices);
     160        }
     161      });
    162162    }
    163163
     
    173173
    174174    public void ReOrderToIndices(IList<System.Tuple<int, int>> indices) {
    175       preprocessingData.BeginTransaction();
    176       for (int i = 0; i < preprocessingData.Columns; ++i) {
    177         if (preprocessingData.IsType<double>(i)) {
    178           reOrderToIndices<double>(i, indices);
    179         } else if (preprocessingData.IsType<string>(i)) {
    180           reOrderToIndices<string>(i, indices);
    181         } else if (preprocessingData.IsType<DateTime>(i)) {
    182           reOrderToIndices<DateTime>(i, indices);
    183         }
    184       }
    185       preprocessingData.EndTransaction();
     175      preprocessingData.InTransaction(() => {
     176        for (int i = 0; i < preprocessingData.Columns; ++i) {
     177          if (preprocessingData.IsType<double>(i)) {
     178            reOrderToIndices<double>(i, indices);
     179          } else if (preprocessingData.IsType<string>(i)) {
     180            reOrderToIndices<string>(i, indices);
     181          } else if (preprocessingData.IsType<DateTime>(i)) {
     182            reOrderToIndices<DateTime>(i, indices);
     183          }
     184        }
     185      });
    186186    }
    187187
  • branches/DataPreprocessing/HeuristicLab.DataPreprocessing/3.3/Implementations/TransactionalPreprocessingData.cs

    r10609 r10612  
    158158    }
    159159
     160    public void InTransaction(Action action) {
     161      BeginTransaction();
     162      action();
     163      EndTransaction();
     164    }
     165
    160166    public void BeginTransaction() {
    161167      SaveSnapshot(DataPreprocessingChangedEventType.Any, -1, -1);
  • branches/DataPreprocessing/HeuristicLab.DataPreprocessing/3.3/Interfaces/ITransactionalPreprocessingData.cs

    r10586 r10612  
    2121
    2222
     23using System;
    2324namespace HeuristicLab.DataPreprocessing {
    2425
     
    2930    bool IsUndoAvailable { get; }
    3031    void Undo();
     32    void InTransaction(Action action);
    3133    void BeginTransaction();
    3234    void EndTransaction();
Note: See TracChangeset for help on using the changeset viewer.