Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.DataImporter/HeuristicLab.DataImporter.Data/Model/DoubleColumn.cs @ 9708

Last change on this file since 9708 was 9615, checked in by mkommend, 11 years ago

#1734: Updated copyright information in all DataImporter classes.

File size: 5.2 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2013 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Collections;
24using System.Collections.Generic;
25using System.Globalization;
26using System.Linq;
27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
28
29namespace HeuristicLab.DataImporter.Data.Model {
30  [StorableClass]
31  public class DoubleColumn : ColumnBase {
32    [Storable]
33    private List<double?> values;
34
35    [StorableConstructor]
36    protected DoubleColumn(bool deserializing) : base(deserializing) { }
37
38    public DoubleColumn(string columnName)
39      : base(columnName) {
40      this.values = new List<double?>();
41    }
42
43    public DoubleColumn(string columnName, int capacity)
44      : this(columnName) {
45      this.values.Capacity = capacity;
46    }
47
48    public override Type DataType {
49      get { return typeof(double?); }
50    }
51
52    public override string ToString() {
53      return base.ToString() + " <Double>";
54    }
55
56    protected override IList Values {
57      get { return this.values; }
58    }
59
60    public override void AddValue(IComparable value) {
61      this.values.Add((double?)value);
62    }
63
64    public override void AddValueOrNull(IComparable value) {
65      this.values.Add(ConvertToDouble(value));
66    }
67
68    public override void InsertValue(int position, IComparable value) {
69      this.values.Insert(position, (double?)value);
70    }
71
72    public override void ChangeValue(int position, IComparable value) {
73      this.values[position] = (double?)value;
74    }
75
76    public override void ChangeValueOrNull(int position, IComparable value) {
77      this.values[position] = ConvertToDouble(value);
78    }
79
80    public override void ChangeValueOrLeaveOldValue(int position, IComparable value) {
81      if (value == null)
82        this.values[position] = null;
83      else {
84        double? val = ConvertToDouble(value);
85        if (val != null)
86          this.values[position] = val;
87      }
88    }
89
90    private double? ConvertToDouble(IComparable value) {
91      double? val = null;
92      double test;
93      if (value != null) {
94        if (value is DateTime)
95          val = double.Parse(((DateTime)value).ToString("yyyyMMddHHmmss"));
96        else if (value is string) {
97          string s = value.ToString();
98          if (s.StartsWith("."))
99            s = s.Insert(0, "0");
100          else if (s.StartsWith("-."))
101            s = s.Insert(1, "0");
102          CultureInfo culture = CultureInfo.CurrentCulture;
103          if (s.Contains('.'))
104            culture = CultureInfo.InvariantCulture;
105          if (double.TryParse(s, NumberStyles.Any, culture, out test))
106            val = test;
107        } else if (value is double)
108          val = (double)value;
109      }
110      return val;
111    }
112
113    public double? Mean {
114      get {
115        double sum = 0;
116        int cnt = 0;
117        for (int i = 0; i < this.values.Count; i++) {
118          if (this.values[i] != null) {
119            sum += (double)this.values[i];
120            cnt++;
121          }
122        }
123        if (cnt != 0)
124          return sum / cnt;
125        else
126          return null;
127      }
128    }
129
130    public double? StandardDeviation {
131      get {
132        double? mean = Mean;
133        double sum = 0;
134        if (mean == null)
135          return null;
136
137        for (int i = 0; i < this.values.Count; i++) {
138          if (values[i] == null)
139            continue;
140          sum += Math.Pow((double)(values[i] - mean), 2.0);
141        }
142        return Math.Sqrt(sum / NonNullValuesCount);
143      }
144    }
145
146    public double? Median {
147      get {
148        return GetMedian(0, this.TotalValuesCount);
149      }
150    }
151
152    public double? GetMedian(int startIndex, int endIndex) {
153      List<double?> sortedValues = values.GetRange(startIndex, endIndex - startIndex).Where(x => x != null).ToList(); ;
154      if (sortedValues.Count == 0)
155        return null;
156
157      sortedValues.Sort();
158      if (sortedValues.Count % 2 == 0)
159        return (sortedValues[sortedValues.Count / 2] + sortedValues[sortedValues.Count / 2 - 1]) / 2;
160      else
161        return sortedValues[sortedValues.Count / 2];
162    }
163
164    public override ColumnBase CreateCopyOfColumnWithoutValues() {
165      return CreateCopyOfColumnWithoutValues(this.values.Capacity);
166    }
167
168    public override ColumnBase CreateCopyOfColumnWithoutValues(int capacity) {
169      return new DoubleColumn(this.Name, capacity);
170    }
171  }
172}
Note: See TracBrowser for help on using the repository browser.