Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
05/27/09 15:42:04 (15 years ago)
Author:
epitzer
Message:

Migration of DataAnalysis, GP, GP.StructureIdentification and Modeling to new Persistence-3.3 (#603)

File:
1 edited

Legend:

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

    r1786 r1914  
    2727using System.Globalization;
    2828using System.Text;
     29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    2930
    3031namespace HeuristicLab.DataAnalysis {
    3132  public sealed class Dataset : ItemBase {
    3233
     34    [Storable]
    3335    private string name;
     36
     37    [Storable]
     38    private int rows;
     39
     40    [Storable]
     41    private int columns;
     42
     43    [Storable]
     44    private string[] variableNames;
     45
     46    [Storable]
     47    private double[] scalingFactor;
     48
     49    [Storable]
     50    private double[] scalingOffset;
     51
     52    [Storable]
    3453    private double[] samples;
    35     private int rows;
    36     private int columns;
     54
    3755    private Dictionary<int, Dictionary<int, double>>[] cachedMeans;
    3856    private Dictionary<int, Dictionary<int, double>>[] cachedRanges;
    39     private double[] scalingFactor;
    40     private double[] scalingOffset;
     57
     58    [Storable]
     59    private object CreateDictionaries_Persistence {
     60      get { return null; }
     61      set { CreateDictionaries(); }
     62    }
    4163
    4264    public string Name {
     
    93115      }
    94116    }
    95 
    96     private string[] variableNames;
    97117
    98118    public Dataset() {
     
    145165    }
    146166
    147     public override XmlNode GetXmlNode(string name, XmlDocument document, IDictionary<Guid, IStorable> persistedObjects) {
    148       XmlNode node = base.GetXmlNode(name, document, persistedObjects);
    149       XmlAttribute problemName = document.CreateAttribute("Name");
    150       problemName.Value = Name;
    151       node.Attributes.Append(problemName);
    152       XmlAttribute dim1 = document.CreateAttribute("Dimension1");
    153       dim1.Value = rows.ToString(CultureInfo.InvariantCulture.NumberFormat);
    154       node.Attributes.Append(dim1);
    155       XmlAttribute dim2 = document.CreateAttribute("Dimension2");
    156       dim2.Value = columns.ToString(CultureInfo.InvariantCulture.NumberFormat);
    157       node.Attributes.Append(dim2);
    158       XmlAttribute variableNames = document.CreateAttribute("VariableNames");
    159       variableNames.Value = GetVariableNamesString();
    160       node.Attributes.Append(variableNames);
    161       XmlAttribute scalingFactorsAttribute = document.CreateAttribute("ScalingFactors");
    162       scalingFactorsAttribute.Value = GetString(scalingFactor);
    163       node.Attributes.Append(scalingFactorsAttribute);
    164       XmlAttribute scalingOffsetsAttribute = document.CreateAttribute("ScalingOffsets");
    165       scalingOffsetsAttribute.Value = GetString(scalingOffset);
    166       node.Attributes.Append(scalingOffsetsAttribute);
    167       node.InnerText = ToString(CultureInfo.InvariantCulture.NumberFormat);
    168       return node;
    169     }
    170 
    171     public override void Populate(XmlNode node, IDictionary<Guid, IStorable> restoredObjects) {
    172       base.Populate(node, restoredObjects);
    173       Name = node.Attributes["Name"].Value;
    174       rows = int.Parse(node.Attributes["Dimension1"].Value, CultureInfo.InvariantCulture.NumberFormat);
    175       columns = int.Parse(node.Attributes["Dimension2"].Value, CultureInfo.InvariantCulture.NumberFormat);
    176 
    177       variableNames = ParseVariableNamesString(node.Attributes["VariableNames"].Value);
    178       if (node.Attributes["ScalingFactors"] != null)
    179         scalingFactor = ParseDoubleString(node.Attributes["ScalingFactors"].Value);
    180       else {
    181         scalingFactor = new double[columns]; // compatibility with old serialization format
    182         for (int i = 0; i < scalingFactor.Length; i++) scalingFactor[i] = 1.0;
    183       }
    184       if (node.Attributes["ScalingOffsets"] != null)
    185         scalingOffset = ParseDoubleString(node.Attributes["ScalingOffsets"].Value);
    186       else {
    187         scalingOffset = new double[columns]; // compatibility with old serialization format
    188         for (int i = 0; i < scalingOffset.Length; i++) scalingOffset[i] = 0.0;
    189       }
    190 
    191       string[] tokens = node.InnerText.Split(';');
    192       if (tokens.Length != rows * columns) throw new FormatException();
    193       samples = new double[rows * columns];
    194       for (int row = 0; row < rows; row++) {
    195         for (int column = 0; column < columns; column++) {
    196           if (double.TryParse(tokens[row * columns + column], NumberStyles.Float, CultureInfo.InvariantCulture.NumberFormat, out samples[row * columns + column]) == false) {
    197             throw new FormatException("Can't parse " + tokens[row * columns + column] + " as double value.");
    198           }
    199         }
    200       }
    201       CreateDictionaries();
    202     }
    203 
    204167    public override string ToString() {
    205168      return ToString(CultureInfo.CurrentCulture.NumberFormat);
     
    216179      if (builder.Length > 0) builder.Remove(0, 1);
    217180      return builder.ToString();
    218     }
    219 
    220     private string GetVariableNamesString() {
    221       string s = "";
    222       for (int i = 0; i < variableNames.Length; i++) {
    223         s += variableNames[i] + "; ";
    224       }
    225 
    226       if (variableNames.Length > 0) {
    227         s = s.TrimEnd(';', ' ');
    228       }
    229       return s;
    230     }
    231     private string GetString(double[] xs) {
    232       string s = "";
    233       for (int i = 0; i < xs.Length; i++) {
    234         s += xs[i].ToString("r", CultureInfo.InvariantCulture) + "; ";
    235       }
    236 
    237       if (xs.Length > 0) {
    238         s = s.TrimEnd(';', ' ');
    239       }
    240       return s;
    241     }
    242 
    243     private string[] ParseVariableNamesString(string p) {
    244       p = p.Trim();
    245       string[] tokens = p.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
    246       for (int i = 0; i < tokens.Length; i++) tokens[i] = tokens[i].Trim();
    247       return tokens;
    248     }
    249     private double[] ParseDoubleString(string s) {
    250       s = s.Trim();
    251       string[] ss = s.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
    252       double[] xs = new double[ss.Length];
    253       for (int i = 0; i < xs.Length; i++) {
    254         xs[i] = double.Parse(ss[i], CultureInfo.InvariantCulture);
    255       }
    256       return xs;
    257181    }
    258182
Note: See TracChangeset for help on using the changeset viewer.