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 |
|
---|
22 | using System;
|
---|
23 | using System.Collections;
|
---|
24 | using System.Collections.Generic;
|
---|
25 | using System.Globalization;
|
---|
26 | using System.Linq;
|
---|
27 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
28 | using HEAL.Attic;
|
---|
29 |
|
---|
30 | namespace HeuristicLab.DataImporter.Data.Model {
|
---|
31 | [StorableType("1C7B8981-3027-4133-A55F-2B1985568E69")]
|
---|
32 | public class DoubleColumn : ColumnBase {
|
---|
33 | [Storable]
|
---|
34 | private List<double?> values;
|
---|
35 |
|
---|
36 | [StorableConstructor]
|
---|
37 | protected DoubleColumn(StorableConstructorFlag _) : base(_) { }
|
---|
38 |
|
---|
39 | public DoubleColumn(string columnName)
|
---|
40 | : base(columnName) {
|
---|
41 | this.values = new List<double?>();
|
---|
42 | }
|
---|
43 |
|
---|
44 | public DoubleColumn(string columnName, int capacity)
|
---|
45 | : this(columnName) {
|
---|
46 | this.values.Capacity = capacity;
|
---|
47 | }
|
---|
48 |
|
---|
49 | public override Type DataType {
|
---|
50 | get { return typeof(double?); }
|
---|
51 | }
|
---|
52 |
|
---|
53 | public override string ToString() {
|
---|
54 | return base.ToString() + " <Double>";
|
---|
55 | }
|
---|
56 |
|
---|
57 | protected override IList Values {
|
---|
58 | get { return this.values; }
|
---|
59 | }
|
---|
60 |
|
---|
61 | public override void AddValue(IComparable value) {
|
---|
62 | this.values.Add((double?)value);
|
---|
63 | }
|
---|
64 |
|
---|
65 | public override void AddValueOrNull(IComparable value) {
|
---|
66 | this.values.Add(ConvertToDouble(value));
|
---|
67 | }
|
---|
68 |
|
---|
69 | public override void InsertValue(int position, IComparable value) {
|
---|
70 | this.values.Insert(position, (double?)value);
|
---|
71 | }
|
---|
72 |
|
---|
73 | public override void ChangeValue(int position, IComparable value) {
|
---|
74 | this.values[position] = (double?)value;
|
---|
75 | }
|
---|
76 |
|
---|
77 | public override void ChangeValueOrNull(int position, IComparable value) {
|
---|
78 | this.values[position] = ConvertToDouble(value);
|
---|
79 | }
|
---|
80 |
|
---|
81 | public override void ChangeValueOrLeaveOldValue(int position, IComparable value) {
|
---|
82 | if (value == null)
|
---|
83 | this.values[position] = null;
|
---|
84 | else {
|
---|
85 | double? val = ConvertToDouble(value);
|
---|
86 | if (val != null)
|
---|
87 | this.values[position] = val;
|
---|
88 | }
|
---|
89 | }
|
---|
90 |
|
---|
91 | private double? ConvertToDouble(IComparable value) {
|
---|
92 | double? val = null;
|
---|
93 | double test;
|
---|
94 | if (value != null) {
|
---|
95 | if (value is DateTime)
|
---|
96 | val = double.Parse(((DateTime)value).ToString("yyyyMMddHHmmss"));
|
---|
97 | else if (value is string) {
|
---|
98 | string s = value.ToString();
|
---|
99 | if (s.StartsWith("."))
|
---|
100 | s = s.Insert(0, "0");
|
---|
101 | else if (s.StartsWith("-."))
|
---|
102 | s = s.Insert(1, "0");
|
---|
103 | CultureInfo culture = CultureInfo.CurrentCulture;
|
---|
104 | if (s.Contains('.'))
|
---|
105 | culture = CultureInfo.InvariantCulture;
|
---|
106 | if (double.TryParse(s, NumberStyles.Any, culture, out test))
|
---|
107 | val = test;
|
---|
108 | } else if (value is double)
|
---|
109 | val = (double)value;
|
---|
110 | }
|
---|
111 | return val;
|
---|
112 | }
|
---|
113 |
|
---|
114 | public double? Mean {
|
---|
115 | get {
|
---|
116 | double sum = 0;
|
---|
117 | int cnt = 0;
|
---|
118 | for (int i = 0; i < this.values.Count; i++) {
|
---|
119 | if (this.values[i] != null) {
|
---|
120 | sum += (double)this.values[i];
|
---|
121 | cnt++;
|
---|
122 | }
|
---|
123 | }
|
---|
124 | if (cnt != 0)
|
---|
125 | return sum / cnt;
|
---|
126 | else
|
---|
127 | return null;
|
---|
128 | }
|
---|
129 | }
|
---|
130 |
|
---|
131 | public double? StandardDeviation {
|
---|
132 | get {
|
---|
133 | double? mean = Mean;
|
---|
134 | double sum = 0;
|
---|
135 | if (mean == null)
|
---|
136 | return null;
|
---|
137 |
|
---|
138 | for (int i = 0; i < this.values.Count; i++) {
|
---|
139 | if (values[i] == null)
|
---|
140 | continue;
|
---|
141 | sum += Math.Pow((double)(values[i] - mean), 2.0);
|
---|
142 | }
|
---|
143 | return Math.Sqrt(sum / NonNullValuesCount);
|
---|
144 | }
|
---|
145 | }
|
---|
146 |
|
---|
147 | public double? Median {
|
---|
148 | get {
|
---|
149 | return GetMedian(0, this.TotalValuesCount);
|
---|
150 | }
|
---|
151 | }
|
---|
152 |
|
---|
153 | public double? GetMedian(int startIndex, int endIndex) {
|
---|
154 | List<double?> sortedValues = values.GetRange(startIndex, endIndex - startIndex).Where(x => x != null).ToList(); ;
|
---|
155 | if (sortedValues.Count == 0)
|
---|
156 | return null;
|
---|
157 |
|
---|
158 | sortedValues.Sort();
|
---|
159 | if (sortedValues.Count % 2 == 0)
|
---|
160 | return (sortedValues[sortedValues.Count / 2] + sortedValues[sortedValues.Count / 2 - 1]) / 2;
|
---|
161 | else
|
---|
162 | return sortedValues[sortedValues.Count / 2];
|
---|
163 | }
|
---|
164 |
|
---|
165 | public override ColumnBase CreateCopyOfColumnWithoutValues() {
|
---|
166 | return CreateCopyOfColumnWithoutValues(this.values.Capacity);
|
---|
167 | }
|
---|
168 |
|
---|
169 | public override ColumnBase CreateCopyOfColumnWithoutValues(int capacity) {
|
---|
170 | return new DoubleColumn(this.Name, capacity);
|
---|
171 | }
|
---|
172 | }
|
---|
173 | }
|
---|