Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
05/13/08 14:36:44 (16 years ago)
Author:
gkronber
Message:

implemented #147

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.DataAnalysis/Dataset.cs

    r232 r237  
    3939    private double[] samples;
    4040    private int rows;
    41     Dictionary<int, Dictionary<int, double>>[] cachedMeans;
    42     Dictionary<int, Dictionary<int, double>>[] cachedRanges;
     41    private Dictionary<int, Dictionary<int, double>>[] cachedMeans;
     42    private Dictionary<int, Dictionary<int, double>>[] cachedRanges;
     43    private double[] scalingFactor;
     44    private double[] scalingOffset;
    4345
    4446    public int Rows {
     
    6769    public double[] Samples {
    6870      get { return samples; }
    69       set {
     71      set {
     72        scalingFactor = new double[columns];
     73        scalingOffset = new double[columns];
     74        for(int i = 0; i < scalingFactor.Length; i++) {
     75          scalingFactor[i] = 1.0;
     76          scalingOffset[i] = 0.0;
     77        }
    7078        samples = value;
    7179        CreateDictionaries();
     
    8290    public Dataset() {
    8391      Name = "-";
    84       VariableNames = new string[] {"Var0"};
     92      VariableNames = new string[] { "Var0" };
    8593      Columns = 1;
    8694      Rows = 1;
    8795      Samples = new double[1];
     96      scalingOffset = new double[] { 0.0 };
     97      scalingFactor = new double[] { 1.0 };
    8898    }
    8999
    90100    private void CreateDictionaries() {
    91101      // keep a means and ranges dictionary for each column (possible target variable) of the dataset.
    92 
    93102      cachedMeans = new Dictionary<int, Dictionary<int, double>>[columns];
    94103      cachedRanges = new Dictionary<int, Dictionary<int, double>>[columns];
    95 
    96104      for(int i = 0; i < columns; i++) {
    97105        cachedMeans[i] = new Dictionary<int, Dictionary<int, double>>();
     
    115123      clone.VariableNames = new string[VariableNames.Length];
    116124      Array.Copy(VariableNames, clone.VariableNames, VariableNames.Length);
     125      Array.Copy(scalingFactor, clone.scalingFactor, columns);
     126      Array.Copy(scalingOffset, clone.scalingOffset, columns);
    117127      return clone;
    118128    }
     
    129139      dim2.Value = columns.ToString(CultureInfo.InvariantCulture.NumberFormat);
    130140      node.Attributes.Append(dim2);
    131 
    132141      XmlAttribute variableNames = document.CreateAttribute("VariableNames");
    133142      variableNames.Value = GetVariableNamesString();
    134143      node.Attributes.Append(variableNames);
    135 
     144      XmlAttribute scalingFactorsAttribute = document.CreateAttribute("ScalingFactors");
     145      scalingFactorsAttribute.Value = GetString(scalingFactor);
     146      node.Attributes.Append(scalingFactorsAttribute);
     147      XmlAttribute scalingOffsetsAttribute = document.CreateAttribute("ScalingOffsets");
     148      scalingOffsetsAttribute.Value = GetString(scalingOffset);
     149      node.Attributes.Append(scalingOffsetsAttribute);
    136150      node.InnerText = ToString(CultureInfo.InvariantCulture.NumberFormat);
    137151      return node;
     
    143157      rows = int.Parse(node.Attributes["Dimension1"].Value, CultureInfo.InvariantCulture.NumberFormat);
    144158      columns = int.Parse(node.Attributes["Dimension2"].Value, CultureInfo.InvariantCulture.NumberFormat);
    145      
     159
    146160      VariableNames = ParseVariableNamesString(node.Attributes["VariableNames"].Value);
     161      if(node.Attributes["ScalingFactors"] != null)
     162        scalingFactor = ParseDoubleString(node.Attributes["ScalingFactors"].Value);
     163      else {
     164        scalingFactor = new double[columns]; // compatibility with old serialization format
     165        for(int i = 0; i < scalingFactor.Length; i++) scalingFactor[i] = 1.0;
     166      }
     167      if(node.Attributes["ScalingOffsets"] != null)
     168        scalingOffset = ParseDoubleString(node.Attributes["ScalingOffsets"].Value);
     169      else {
     170        scalingOffset = new double[columns]; // compatibility with old serialization format
     171        for(int i = 0; i < scalingOffset.Length; i++) scalingOffset[i] = 0.0;
     172      }
    147173
    148174      string[] tokens = node.InnerText.Split(';');
     
    151177      for(int row = 0; row < rows; row++) {
    152178        for(int column = 0; column < columns; column++) {
    153           if(double.TryParse(tokens[row * columns + column], NumberStyles.Float, CultureInfo.InvariantCulture.NumberFormat, out samples[row*columns + column]) == false) {
     179          if(double.TryParse(tokens[row * columns + column], NumberStyles.Float, CultureInfo.InvariantCulture.NumberFormat, out samples[row * columns + column]) == false) {
    154180            throw new FormatException("Can't parse " + tokens[row * columns + column] + " as double value.");
    155181          }
     
    168194        for(int column = 0; column < columns; column++) {
    169195          builder.Append(";");
    170           builder.Append(samples[row*columns+column].ToString(format));
     196          builder.Append(samples[row * columns + column].ToString(format));
    171197        }
    172198      }
     
    177203    private string GetVariableNamesString() {
    178204      string s = "";
    179       for (int i = 0; i < variableNames.Length; i++) {
     205      for(int i = 0; i < variableNames.Length; i++) {
    180206        s += variableNames[i] + "; ";
    181207      }
    182208
    183       if (variableNames.Length > 0) {
     209      if(variableNames.Length > 0) {
     210        s = s.TrimEnd(';', ' ');
     211      }
     212      return s;
     213    }
     214    private string GetString(double[] xs) {
     215      string s = "";
     216      for(int i = 0; i < xs.Length; i++) {
     217        s += xs[i].ToString(CultureInfo.InvariantCulture) + "; ";
     218      }
     219
     220      if(xs.Length > 0) {
    184221        s = s.TrimEnd(';', ' ');
    185222      }
     
    189226    private string[] ParseVariableNamesString(string p) {
    190227      p = p.Trim();
    191       string[] tokens = p.Split(new char[] {';'}, StringSplitOptions.RemoveEmptyEntries);
     228      string[] tokens = p.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
    192229      return tokens;
    193230    }
     231    private double[] ParseDoubleString(string s) {
     232      s = s.Trim();
     233      string[] ss = s.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
     234      double[] xs = new double[ss.Length];
     235      for(int i = 0; i < xs.Length; i++) {
     236        xs[i] = double.Parse(ss[i], CultureInfo.InvariantCulture);
     237      }
     238      return xs;
     239    }
    194240
    195241    public double GetMean(int column) {
    196       return GetMean(column, 0, Rows-1);
     242      return GetMean(column, 0, Rows - 1);
    197243    }
    198244
     
    213259
    214260    public double GetRange(int column) {
    215       return GetRange(column, 0, Rows-1);
     261      return GetRange(column, 0, Rows - 1);
    216262    }
    217263
     
    248294      return min;
    249295    }
     296
     297    internal void ScaleVariable(int column) {
     298      if(scalingFactor[column] == 1.0) {
     299        double min = GetMinimum(column);
     300        double max = GetMaximum(column);
     301        double range = max - min;
     302        scalingFactor[column] = range;
     303        scalingOffset[column] = min;
     304        for(int i = 0; i < Rows; i++) {
     305          double origValue = samples[i * columns + column];
     306          samples[i * columns + column] = (origValue - min) / range;
     307        }
     308      }
     309      CreateDictionaries();
     310      FireChanged();
     311    }
     312
     313    internal void UnscaleVariable(int column) {
     314      if(scalingFactor[column] != 1.0) {
     315        for(int i = 0; i < rows; i++) {
     316          double scaledValue = samples[i * columns + column];
     317          samples[i * columns + column] = scaledValue * scalingFactor[column] + scalingOffset[column];
     318        }
     319        scalingFactor[column] = 1.0;
     320        scalingOffset[column] = 0.0;
     321      }
     322    }
    250323  }
    251324}
Note: See TracChangeset for help on using the changeset viewer.