1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2008 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.Generic;
|
---|
24 | using System.Xml;
|
---|
25 | using HeuristicLab.Core;
|
---|
26 | using HeuristicLab.Data;
|
---|
27 | using System.Globalization;
|
---|
28 | using System.Text;
|
---|
29 |
|
---|
30 | namespace HeuristicLab.DataAnalysis {
|
---|
31 | public sealed class Dataset : ItemBase {
|
---|
32 |
|
---|
33 | private string name;
|
---|
34 | private double[] samples;
|
---|
35 | private int rows;
|
---|
36 | private int columns;
|
---|
37 | private Dictionary<int, Dictionary<int, double>>[] cachedMeans;
|
---|
38 | private Dictionary<int, Dictionary<int, double>>[] cachedRanges;
|
---|
39 | private double[] scalingFactor;
|
---|
40 | private double[] scalingOffset;
|
---|
41 |
|
---|
42 | public string Name {
|
---|
43 | get { return name; }
|
---|
44 | set { name = value; }
|
---|
45 | }
|
---|
46 |
|
---|
47 | public int Rows {
|
---|
48 | get { return rows; }
|
---|
49 | set { rows = value; }
|
---|
50 | }
|
---|
51 |
|
---|
52 | public int Columns {
|
---|
53 | get { return columns; }
|
---|
54 | set {
|
---|
55 | columns = value;
|
---|
56 | if (variableNames == null || variableNames.Length != columns) {
|
---|
57 | variableNames = new string[columns];
|
---|
58 | }
|
---|
59 | }
|
---|
60 | }
|
---|
61 |
|
---|
62 | public double[] ScalingFactor {
|
---|
63 | get { return scalingFactor; }
|
---|
64 | }
|
---|
65 | public double[] ScalingOffset {
|
---|
66 | get { return scalingOffset; }
|
---|
67 | }
|
---|
68 |
|
---|
69 | public double GetValue(int i, int j) {
|
---|
70 | return samples[columns * i + j];
|
---|
71 | }
|
---|
72 |
|
---|
73 | public void SetValue(int i, int j, double v) {
|
---|
74 | if (v != samples[columns * i + j]) {
|
---|
75 | samples[columns * i + j] = v;
|
---|
76 | CreateDictionaries();
|
---|
77 | FireChanged();
|
---|
78 | }
|
---|
79 | }
|
---|
80 |
|
---|
81 | public double[] Samples {
|
---|
82 | get { return samples; }
|
---|
83 | set {
|
---|
84 | scalingFactor = new double[columns];
|
---|
85 | scalingOffset = new double[columns];
|
---|
86 | for (int i = 0; i < scalingFactor.Length; i++) {
|
---|
87 | scalingFactor[i] = 1.0;
|
---|
88 | scalingOffset[i] = 0.0;
|
---|
89 | }
|
---|
90 | samples = value;
|
---|
91 | CreateDictionaries();
|
---|
92 | FireChanged();
|
---|
93 | }
|
---|
94 | }
|
---|
95 |
|
---|
96 | private string[] variableNames;
|
---|
97 |
|
---|
98 | public Dataset() {
|
---|
99 | Name = "-";
|
---|
100 | variableNames = new string[] { "Var0" };
|
---|
101 | Columns = 1;
|
---|
102 | Rows = 1;
|
---|
103 | Samples = new double[1];
|
---|
104 | scalingOffset = new double[] { 0.0 };
|
---|
105 | scalingFactor = new double[] { 1.0 };
|
---|
106 | }
|
---|
107 |
|
---|
108 | private void CreateDictionaries() {
|
---|
109 | // keep a means and ranges dictionary for each column (possible target variable) of the dataset.
|
---|
110 | cachedMeans = new Dictionary<int, Dictionary<int, double>>[columns];
|
---|
111 | cachedRanges = new Dictionary<int, Dictionary<int, double>>[columns];
|
---|
112 | for (int i = 0; i < columns; i++) {
|
---|
113 | cachedMeans[i] = new Dictionary<int, Dictionary<int, double>>();
|
---|
114 | cachedRanges[i] = new Dictionary<int, Dictionary<int, double>>();
|
---|
115 | }
|
---|
116 | }
|
---|
117 |
|
---|
118 | public string GetVariableName(int variableIndex) {
|
---|
119 | return variableNames[variableIndex];
|
---|
120 | }
|
---|
121 |
|
---|
122 | public void SetVariableName(int variableIndex, string name) {
|
---|
123 | variableNames[variableIndex] = name;
|
---|
124 | }
|
---|
125 |
|
---|
126 |
|
---|
127 | public override IView CreateView() {
|
---|
128 | return new DatasetView(this);
|
---|
129 | }
|
---|
130 |
|
---|
131 | public override object Clone(IDictionary<Guid, object> clonedObjects) {
|
---|
132 | Dataset clone = new Dataset();
|
---|
133 | clonedObjects.Add(Guid, clone);
|
---|
134 | double[] cloneSamples = new double[rows * columns];
|
---|
135 | Array.Copy(samples, cloneSamples, samples.Length);
|
---|
136 | clone.rows = rows;
|
---|
137 | clone.columns = columns;
|
---|
138 | clone.Samples = cloneSamples;
|
---|
139 | clone.Name = Name;
|
---|
140 | clone.variableNames = new string[variableNames.Length];
|
---|
141 | Array.Copy(variableNames, clone.variableNames, variableNames.Length);
|
---|
142 | Array.Copy(scalingFactor, clone.scalingFactor, columns);
|
---|
143 | Array.Copy(scalingOffset, clone.scalingOffset, columns);
|
---|
144 | return clone;
|
---|
145 | }
|
---|
146 |
|
---|
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 |
|
---|
204 | public override string ToString() {
|
---|
205 | return ToString(CultureInfo.CurrentCulture.NumberFormat);
|
---|
206 | }
|
---|
207 |
|
---|
208 | private string ToString(NumberFormatInfo format) {
|
---|
209 | StringBuilder builder = new StringBuilder();
|
---|
210 | for (int row = 0; row < rows; row++) {
|
---|
211 | for (int column = 0; column < columns; column++) {
|
---|
212 | builder.Append(";");
|
---|
213 | builder.Append(samples[row * columns + column].ToString("r", format));
|
---|
214 | }
|
---|
215 | }
|
---|
216 | if (builder.Length > 0) builder.Remove(0, 1);
|
---|
217 | 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;
|
---|
257 | }
|
---|
258 |
|
---|
259 | public double GetMean(int column) {
|
---|
260 | return GetMean(column, 0, Rows);
|
---|
261 | }
|
---|
262 |
|
---|
263 | public double GetMean(int column, int from, int to) {
|
---|
264 | if (!cachedMeans[column].ContainsKey(from) || !cachedMeans[column][from].ContainsKey(to)) {
|
---|
265 | double[] values = new double[to - from];
|
---|
266 | for (int sample = from; sample < to; sample++) {
|
---|
267 | values[sample - from] = GetValue(sample, column);
|
---|
268 | }
|
---|
269 | double mean = Statistics.Mean(values);
|
---|
270 | if (!cachedMeans[column].ContainsKey(from)) cachedMeans[column][from] = new Dictionary<int, double>();
|
---|
271 | cachedMeans[column][from][to] = mean;
|
---|
272 | return mean;
|
---|
273 | } else {
|
---|
274 | return cachedMeans[column][from][to];
|
---|
275 | }
|
---|
276 | }
|
---|
277 |
|
---|
278 | public double GetRange(int column) {
|
---|
279 | return GetRange(column, 0, Rows);
|
---|
280 | }
|
---|
281 |
|
---|
282 | public double GetRange(int column, int from, int to) {
|
---|
283 | if (!cachedRanges[column].ContainsKey(from) || !cachedRanges[column][from].ContainsKey(to)) {
|
---|
284 | double[] values = new double[to - from];
|
---|
285 | for (int sample = from; sample < to; sample++) {
|
---|
286 | values[sample - from] = GetValue(sample, column);
|
---|
287 | }
|
---|
288 | double range = Statistics.Range(values);
|
---|
289 | if (!cachedRanges[column].ContainsKey(from)) cachedRanges[column][from] = new Dictionary<int, double>();
|
---|
290 | cachedRanges[column][from][to] = range;
|
---|
291 | return range;
|
---|
292 | } else {
|
---|
293 | return cachedRanges[column][from][to];
|
---|
294 | }
|
---|
295 | }
|
---|
296 |
|
---|
297 | public double GetMaximum(int column) {
|
---|
298 | double max = Double.NegativeInfinity;
|
---|
299 | for (int i = 0; i < Rows; i++) {
|
---|
300 | double val = GetValue(i, column);
|
---|
301 | if (!double.IsNaN(val) && val > max) max = val;
|
---|
302 | }
|
---|
303 | return max;
|
---|
304 | }
|
---|
305 |
|
---|
306 | public double GetMinimum(int column) {
|
---|
307 | double min = Double.PositiveInfinity;
|
---|
308 | for (int i = 0; i < Rows; i++) {
|
---|
309 | double val = GetValue(i, column);
|
---|
310 | if (!double.IsNaN(val) && val < min) min = val;
|
---|
311 | }
|
---|
312 | return min;
|
---|
313 | }
|
---|
314 |
|
---|
315 | internal void ScaleVariable(int column) {
|
---|
316 | if (scalingFactor[column] == 1.0 && scalingOffset[column] == 0.0) {
|
---|
317 | double min = GetMinimum(column);
|
---|
318 | double max = GetMaximum(column);
|
---|
319 | double range = max - min;
|
---|
320 | if (range == 0) ScaleVariable(column, 1.0, -min);
|
---|
321 | else ScaleVariable(column, 1.0 / range, -min);
|
---|
322 | }
|
---|
323 | CreateDictionaries();
|
---|
324 | FireChanged();
|
---|
325 | }
|
---|
326 |
|
---|
327 | internal void ScaleVariable(int column, double factor, double offset) {
|
---|
328 | scalingFactor[column] = factor;
|
---|
329 | scalingOffset[column] = offset;
|
---|
330 | for (int i = 0; i < Rows; i++) {
|
---|
331 | double origValue = samples[i * columns + column];
|
---|
332 | samples[i * columns + column] = (origValue + offset) * factor;
|
---|
333 | }
|
---|
334 | CreateDictionaries();
|
---|
335 | FireChanged();
|
---|
336 | }
|
---|
337 |
|
---|
338 | internal void UnscaleVariable(int column) {
|
---|
339 | if (scalingFactor[column] != 1.0 || scalingOffset[column] != 0.0) {
|
---|
340 | for (int i = 0; i < rows; i++) {
|
---|
341 | double scaledValue = samples[i * columns + column];
|
---|
342 | samples[i * columns + column] = scaledValue / scalingFactor[column] - scalingOffset[column];
|
---|
343 | }
|
---|
344 | scalingFactor[column] = 1.0;
|
---|
345 | scalingOffset[column] = 0.0;
|
---|
346 | }
|
---|
347 | }
|
---|
348 | }
|
---|
349 | }
|
---|