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 | private bool cachedValuesInvalidated = true;
|
---|
42 |
|
---|
43 | private bool fireChangeEvents = true;
|
---|
44 | public bool FireChangeEvents {
|
---|
45 | get { return fireChangeEvents; }
|
---|
46 | set { fireChangeEvents = value; }
|
---|
47 | }
|
---|
48 |
|
---|
49 | public string Name {
|
---|
50 | get { return name; }
|
---|
51 | set { name = value; }
|
---|
52 | }
|
---|
53 |
|
---|
54 | public int Rows {
|
---|
55 | get { return rows; }
|
---|
56 | set { rows = value; }
|
---|
57 | }
|
---|
58 |
|
---|
59 | public int Columns {
|
---|
60 | get { return columns; }
|
---|
61 | set {
|
---|
62 | columns = value;
|
---|
63 | if (variableNames == null || variableNames.Length != columns) {
|
---|
64 | variableNames = new string[columns];
|
---|
65 | }
|
---|
66 | }
|
---|
67 | }
|
---|
68 |
|
---|
69 | public double[] ScalingFactor {
|
---|
70 | get { return scalingFactor; }
|
---|
71 | set {
|
---|
72 | if (value.Length != scalingFactor.Length)
|
---|
73 | throw new ArgumentException("Length of scaling factor array doesn't match number of variables");
|
---|
74 | scalingFactor = value;
|
---|
75 | }
|
---|
76 | }
|
---|
77 | public double[] ScalingOffset {
|
---|
78 | get { return scalingOffset; }
|
---|
79 | set {
|
---|
80 | if (value.Length != scalingOffset.Length)
|
---|
81 | throw new ArgumentException("Length of scaling offset array doesn't match number of variables");
|
---|
82 | scalingOffset = value; }
|
---|
83 | }
|
---|
84 |
|
---|
85 | public double GetValue(int i, int j) {
|
---|
86 | return samples[columns * i + j];
|
---|
87 | }
|
---|
88 |
|
---|
89 | public void SetValue(int i, int j, double v) {
|
---|
90 | if (v != samples[columns * i + j]) {
|
---|
91 | samples[columns * i + j] = v;
|
---|
92 | cachedValuesInvalidated = true;
|
---|
93 | if (fireChangeEvents) FireChanged();
|
---|
94 | }
|
---|
95 | }
|
---|
96 |
|
---|
97 | public double[] Samples {
|
---|
98 | get { return samples; }
|
---|
99 | set {
|
---|
100 | scalingFactor = new double[columns];
|
---|
101 | scalingOffset = new double[columns];
|
---|
102 | for (int i = 0; i < scalingFactor.Length; i++) {
|
---|
103 | scalingFactor[i] = 1.0;
|
---|
104 | scalingOffset[i] = 0.0;
|
---|
105 | }
|
---|
106 | samples = value;
|
---|
107 | cachedValuesInvalidated = true;
|
---|
108 | if (fireChangeEvents) FireChanged();
|
---|
109 | }
|
---|
110 | }
|
---|
111 |
|
---|
112 | private string[] variableNames;
|
---|
113 |
|
---|
114 | public Dataset() {
|
---|
115 | Name = "-";
|
---|
116 | variableNames = new string[] { "Var0" };
|
---|
117 | Columns = 1;
|
---|
118 | Rows = 1;
|
---|
119 | Samples = new double[1];
|
---|
120 | scalingOffset = new double[] { 0.0 };
|
---|
121 | scalingFactor = new double[] { 1.0 };
|
---|
122 | cachedValuesInvalidated = true;
|
---|
123 | fireChangeEvents = true;
|
---|
124 | }
|
---|
125 |
|
---|
126 | public string GetVariableName(int variableIndex) {
|
---|
127 | return variableNames[variableIndex];
|
---|
128 | }
|
---|
129 |
|
---|
130 | public int GetVariableIndex(string variableName) {
|
---|
131 | for (int i = 0; i < variableNames.Length; i++) {
|
---|
132 | if (variableNames[i].Equals(variableName)) return i;
|
---|
133 | }
|
---|
134 | throw new ArgumentException("The variable name " + variableName + " was not found.");
|
---|
135 | }
|
---|
136 |
|
---|
137 | public void SetVariableName(int variableIndex, string name) {
|
---|
138 | variableNames[variableIndex] = name;
|
---|
139 | }
|
---|
140 |
|
---|
141 | public override IView CreateView() {
|
---|
142 | return new DatasetView(this);
|
---|
143 | }
|
---|
144 |
|
---|
145 | #region persistence
|
---|
146 | public override object Clone(IDictionary<Guid, object> clonedObjects) {
|
---|
147 | Dataset clone = new Dataset();
|
---|
148 | clonedObjects.Add(Guid, clone);
|
---|
149 | double[] cloneSamples = new double[rows * columns];
|
---|
150 | Array.Copy(samples, cloneSamples, samples.Length);
|
---|
151 | clone.rows = rows;
|
---|
152 | clone.columns = columns;
|
---|
153 | clone.Samples = cloneSamples;
|
---|
154 | clone.Name = Name;
|
---|
155 | clone.variableNames = new string[variableNames.Length];
|
---|
156 | Array.Copy(variableNames, clone.variableNames, variableNames.Length);
|
---|
157 | Array.Copy(scalingFactor, clone.scalingFactor, columns);
|
---|
158 | Array.Copy(scalingOffset, clone.scalingOffset, columns);
|
---|
159 | return clone;
|
---|
160 | }
|
---|
161 |
|
---|
162 | public override XmlNode GetXmlNode(string name, XmlDocument document, IDictionary<Guid, IStorable> persistedObjects) {
|
---|
163 | XmlNode node = base.GetXmlNode(name, document, persistedObjects);
|
---|
164 | XmlAttribute problemName = document.CreateAttribute("Name");
|
---|
165 | problemName.Value = Name;
|
---|
166 | node.Attributes.Append(problemName);
|
---|
167 | XmlAttribute dim1 = document.CreateAttribute("Dimension1");
|
---|
168 | dim1.Value = rows.ToString(CultureInfo.InvariantCulture.NumberFormat);
|
---|
169 | node.Attributes.Append(dim1);
|
---|
170 | XmlAttribute dim2 = document.CreateAttribute("Dimension2");
|
---|
171 | dim2.Value = columns.ToString(CultureInfo.InvariantCulture.NumberFormat);
|
---|
172 | node.Attributes.Append(dim2);
|
---|
173 | XmlAttribute variableNames = document.CreateAttribute("VariableNames");
|
---|
174 | variableNames.Value = GetVariableNamesString();
|
---|
175 | node.Attributes.Append(variableNames);
|
---|
176 | XmlAttribute scalingFactorsAttribute = document.CreateAttribute("ScalingFactors");
|
---|
177 | scalingFactorsAttribute.Value = GetString(scalingFactor);
|
---|
178 | node.Attributes.Append(scalingFactorsAttribute);
|
---|
179 | XmlAttribute scalingOffsetsAttribute = document.CreateAttribute("ScalingOffsets");
|
---|
180 | scalingOffsetsAttribute.Value = GetString(scalingOffset);
|
---|
181 | node.Attributes.Append(scalingOffsetsAttribute);
|
---|
182 | node.InnerText = ToString(CultureInfo.InvariantCulture.NumberFormat);
|
---|
183 | return node;
|
---|
184 | }
|
---|
185 |
|
---|
186 | public override void Populate(XmlNode node, IDictionary<Guid, IStorable> restoredObjects) {
|
---|
187 | base.Populate(node, restoredObjects);
|
---|
188 | Name = node.Attributes["Name"].Value;
|
---|
189 | rows = int.Parse(node.Attributes["Dimension1"].Value, CultureInfo.InvariantCulture.NumberFormat);
|
---|
190 | columns = int.Parse(node.Attributes["Dimension2"].Value, CultureInfo.InvariantCulture.NumberFormat);
|
---|
191 |
|
---|
192 | variableNames = ParseVariableNamesString(node.Attributes["VariableNames"].Value);
|
---|
193 | if (node.Attributes["ScalingFactors"] != null)
|
---|
194 | scalingFactor = ParseDoubleString(node.Attributes["ScalingFactors"].Value);
|
---|
195 | else {
|
---|
196 | scalingFactor = new double[columns]; // compatibility with old serialization format
|
---|
197 | for (int i = 0; i < scalingFactor.Length; i++) scalingFactor[i] = 1.0;
|
---|
198 | }
|
---|
199 | if (node.Attributes["ScalingOffsets"] != null)
|
---|
200 | scalingOffset = ParseDoubleString(node.Attributes["ScalingOffsets"].Value);
|
---|
201 | else {
|
---|
202 | scalingOffset = new double[columns]; // compatibility with old serialization format
|
---|
203 | for (int i = 0; i < scalingOffset.Length; i++) scalingOffset[i] = 0.0;
|
---|
204 | }
|
---|
205 |
|
---|
206 | string[] tokens = node.InnerText.Split(';');
|
---|
207 | if (tokens.Length != rows * columns) throw new FormatException();
|
---|
208 | samples = new double[rows * columns];
|
---|
209 | for (int row = 0; row < rows; row++) {
|
---|
210 | for (int column = 0; column < columns; column++) {
|
---|
211 | if (double.TryParse(tokens[row * columns + column], NumberStyles.Float, CultureInfo.InvariantCulture.NumberFormat, out samples[row * columns + column]) == false) {
|
---|
212 | throw new FormatException("Can't parse " + tokens[row * columns + column] + " as double value.");
|
---|
213 | }
|
---|
214 | }
|
---|
215 | }
|
---|
216 | }
|
---|
217 |
|
---|
218 | public override string ToString() {
|
---|
219 | return ToString(CultureInfo.CurrentCulture.NumberFormat);
|
---|
220 | }
|
---|
221 |
|
---|
222 | private string ToString(NumberFormatInfo format) {
|
---|
223 | StringBuilder builder = new StringBuilder();
|
---|
224 | for (int row = 0; row < rows; row++) {
|
---|
225 | for (int column = 0; column < columns; column++) {
|
---|
226 | builder.Append(";");
|
---|
227 | builder.Append(samples[row * columns + column].ToString("r", format));
|
---|
228 | }
|
---|
229 | }
|
---|
230 | if (builder.Length > 0) builder.Remove(0, 1);
|
---|
231 | return builder.ToString();
|
---|
232 | }
|
---|
233 |
|
---|
234 | private string GetVariableNamesString() {
|
---|
235 | string s = "";
|
---|
236 | for (int i = 0; i < variableNames.Length; i++) {
|
---|
237 | s += variableNames[i] + "; ";
|
---|
238 | }
|
---|
239 |
|
---|
240 | if (variableNames.Length > 0) {
|
---|
241 | s = s.TrimEnd(';', ' ');
|
---|
242 | }
|
---|
243 | return s;
|
---|
244 | }
|
---|
245 | private string GetString(double[] xs) {
|
---|
246 | string s = "";
|
---|
247 | for (int i = 0; i < xs.Length; i++) {
|
---|
248 | s += xs[i].ToString("r", CultureInfo.InvariantCulture) + "; ";
|
---|
249 | }
|
---|
250 |
|
---|
251 | if (xs.Length > 0) {
|
---|
252 | s = s.TrimEnd(';', ' ');
|
---|
253 | }
|
---|
254 | return s;
|
---|
255 | }
|
---|
256 |
|
---|
257 | private string[] ParseVariableNamesString(string p) {
|
---|
258 | p = p.Trim();
|
---|
259 | string[] tokens = p.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
|
---|
260 | for (int i = 0; i < tokens.Length; i++) tokens[i] = tokens[i].Trim();
|
---|
261 | return tokens;
|
---|
262 | }
|
---|
263 | private double[] ParseDoubleString(string s) {
|
---|
264 | s = s.Trim();
|
---|
265 | string[] ss = s.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
|
---|
266 | double[] xs = new double[ss.Length];
|
---|
267 | for (int i = 0; i < xs.Length; i++) {
|
---|
268 | xs[i] = double.Parse(ss[i], CultureInfo.InvariantCulture);
|
---|
269 | }
|
---|
270 | return xs;
|
---|
271 | }
|
---|
272 | #endregion
|
---|
273 |
|
---|
274 | public double GetMean(int column) {
|
---|
275 | return GetMean(column, 0, Rows);
|
---|
276 | }
|
---|
277 |
|
---|
278 | public double GetMean(int column, int from, int to) {
|
---|
279 | if (cachedValuesInvalidated) CreateDictionaries();
|
---|
280 | if (!cachedMeans[column].ContainsKey(from) || !cachedMeans[column][from].ContainsKey(to)) {
|
---|
281 | double[] values = new double[to - from];
|
---|
282 | for (int sample = from; sample < to; sample++) {
|
---|
283 | values[sample - from] = GetValue(sample, column);
|
---|
284 | }
|
---|
285 | double mean = Statistics.Mean(values);
|
---|
286 | if (!cachedMeans[column].ContainsKey(from)) cachedMeans[column][from] = new Dictionary<int, double>();
|
---|
287 | cachedMeans[column][from][to] = mean;
|
---|
288 | return mean;
|
---|
289 | } else {
|
---|
290 | return cachedMeans[column][from][to];
|
---|
291 | }
|
---|
292 | }
|
---|
293 |
|
---|
294 | public double GetRange(int column) {
|
---|
295 | return GetRange(column, 0, Rows);
|
---|
296 | }
|
---|
297 |
|
---|
298 | public double GetRange(int column, int from, int to) {
|
---|
299 | if (cachedValuesInvalidated) CreateDictionaries();
|
---|
300 | if (!cachedRanges[column].ContainsKey(from) || !cachedRanges[column][from].ContainsKey(to)) {
|
---|
301 | double[] values = new double[to - from];
|
---|
302 | for (int sample = from; sample < to; sample++) {
|
---|
303 | values[sample - from] = GetValue(sample, column);
|
---|
304 | }
|
---|
305 | double range = Statistics.Range(values);
|
---|
306 | if (!cachedRanges[column].ContainsKey(from)) cachedRanges[column][from] = new Dictionary<int, double>();
|
---|
307 | cachedRanges[column][from][to] = range;
|
---|
308 | return range;
|
---|
309 | } else {
|
---|
310 | return cachedRanges[column][from][to];
|
---|
311 | }
|
---|
312 | }
|
---|
313 |
|
---|
314 | public double GetMaximum(int column) {
|
---|
315 | return GetMaximum(column, 0, Rows);
|
---|
316 | }
|
---|
317 |
|
---|
318 | public double GetMaximum(int column, int start, int end) {
|
---|
319 | double max = Double.NegativeInfinity;
|
---|
320 | for (int i = start; i < end; i++) {
|
---|
321 | double val = GetValue(i, column);
|
---|
322 | if (!double.IsNaN(val) && val > max) max = val;
|
---|
323 | }
|
---|
324 | return max;
|
---|
325 | }
|
---|
326 |
|
---|
327 | public double GetMinimum(int column) {
|
---|
328 | return GetMinimum(column, 0, Rows);
|
---|
329 | }
|
---|
330 |
|
---|
331 | public double GetMinimum(int column, int start, int end) {
|
---|
332 | double min = Double.PositiveInfinity;
|
---|
333 | for (int i = start; i < end; i++) {
|
---|
334 | double val = GetValue(i, column);
|
---|
335 | if (!double.IsNaN(val) && val < min) min = val;
|
---|
336 | }
|
---|
337 | return min;
|
---|
338 | }
|
---|
339 |
|
---|
340 | internal void ScaleVariable(int column) {
|
---|
341 | if (scalingFactor[column] == 1.0 && scalingOffset[column] == 0.0) {
|
---|
342 | double min = GetMinimum(column);
|
---|
343 | double max = GetMaximum(column);
|
---|
344 | double range = max - min;
|
---|
345 | if (range == 0) ScaleVariable(column, 1.0, -min);
|
---|
346 | else ScaleVariable(column, 1.0 / range, -min);
|
---|
347 | }
|
---|
348 | cachedValuesInvalidated = true;
|
---|
349 | if (fireChangeEvents) FireChanged();
|
---|
350 | }
|
---|
351 |
|
---|
352 | internal void ScaleVariable(int column, double factor, double offset) {
|
---|
353 | scalingFactor[column] = factor;
|
---|
354 | scalingOffset[column] = offset;
|
---|
355 | for (int i = 0; i < Rows; i++) {
|
---|
356 | double origValue = samples[i * columns + column];
|
---|
357 | samples[i * columns + column] = (origValue + offset) * factor;
|
---|
358 | }
|
---|
359 | cachedValuesInvalidated = true;
|
---|
360 | if (fireChangeEvents) FireChanged();
|
---|
361 | }
|
---|
362 |
|
---|
363 | internal void UnscaleVariable(int column) {
|
---|
364 | if (scalingFactor[column] != 1.0 || scalingOffset[column] != 0.0) {
|
---|
365 | for (int i = 0; i < rows; i++) {
|
---|
366 | double scaledValue = samples[i * columns + column];
|
---|
367 | samples[i * columns + column] = scaledValue / scalingFactor[column] - scalingOffset[column];
|
---|
368 | }
|
---|
369 | scalingFactor[column] = 1.0;
|
---|
370 | scalingOffset[column] = 0.0;
|
---|
371 | }
|
---|
372 | cachedValuesInvalidated = true;
|
---|
373 | if (fireChangeEvents) FireChanged();
|
---|
374 | }
|
---|
375 |
|
---|
376 | private void CreateDictionaries() {
|
---|
377 | // keep a means and ranges dictionary for each column (possible target variable) of the dataset.
|
---|
378 | cachedMeans = new Dictionary<int, Dictionary<int, double>>[columns];
|
---|
379 | cachedRanges = new Dictionary<int, Dictionary<int, double>>[columns];
|
---|
380 | for (int i = 0; i < columns; i++) {
|
---|
381 | cachedMeans[i] = new Dictionary<int, Dictionary<int, double>>();
|
---|
382 | cachedRanges[i] = new Dictionary<int, Dictionary<int, double>>();
|
---|
383 | }
|
---|
384 | cachedValuesInvalidated = false;
|
---|
385 | }
|
---|
386 | }
|
---|
387 | }
|
---|