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 class Dataset : ItemBase {
|
---|
32 |
|
---|
33 | private string name;
|
---|
34 | public string Name {
|
---|
35 | get { return name; }
|
---|
36 | set { name = value; }
|
---|
37 | }
|
---|
38 |
|
---|
39 | private double[] samples;
|
---|
40 | private int rows;
|
---|
41 |
|
---|
42 | public int Rows {
|
---|
43 | get { return rows; }
|
---|
44 | set { rows = value; }
|
---|
45 | }
|
---|
46 | private int columns;
|
---|
47 |
|
---|
48 | public int Columns {
|
---|
49 | get { return columns; }
|
---|
50 | set { columns = value; }
|
---|
51 | }
|
---|
52 | private Dictionary<int, double[]>[] ranges;
|
---|
53 | private Dictionary<int, double[]>[] means;
|
---|
54 |
|
---|
55 | public double GetValue(int i, int j) {
|
---|
56 | return samples[columns * i + j];
|
---|
57 | }
|
---|
58 |
|
---|
59 | public void SetValue(int i, int j, double v) {
|
---|
60 | if(v != samples[columns * i + j]) {
|
---|
61 | samples[columns * i + j] = v;
|
---|
62 | FireChanged();
|
---|
63 | }
|
---|
64 | }
|
---|
65 |
|
---|
66 | public double[] Samples {
|
---|
67 | get { return samples; }
|
---|
68 | set {
|
---|
69 | samples = value;
|
---|
70 | CreateDictionaries();
|
---|
71 | FireChanged();
|
---|
72 | }
|
---|
73 | }
|
---|
74 |
|
---|
75 | private string[] variableNames;
|
---|
76 | public string[] VariableNames {
|
---|
77 | get { return variableNames; }
|
---|
78 | set { variableNames = value; }
|
---|
79 | }
|
---|
80 |
|
---|
81 | public Dataset() {
|
---|
82 | Name = "-";
|
---|
83 | VariableNames = new string[] {"Var0"};
|
---|
84 | Columns = 1;
|
---|
85 | Rows = 1;
|
---|
86 | Samples = new double[1];
|
---|
87 | }
|
---|
88 |
|
---|
89 | void samples_Changed(object sender, EventArgs e) {
|
---|
90 | CreateDictionaries();
|
---|
91 | }
|
---|
92 |
|
---|
93 | private void CreateDictionaries() {
|
---|
94 | // keep a means and ranges dictionary for each column (possible target variable) of the dataset.
|
---|
95 |
|
---|
96 | means = new Dictionary<int, double[]>[columns];
|
---|
97 | ranges = new Dictionary<int, double[]>[columns];
|
---|
98 |
|
---|
99 | for(int i = 0; i < columns; i++) {
|
---|
100 | means[i] = new Dictionary<int, double[]>();
|
---|
101 | ranges[i] = new Dictionary<int, double[]>();
|
---|
102 | }
|
---|
103 | }
|
---|
104 |
|
---|
105 | public override IView CreateView() {
|
---|
106 | return new DatasetView(this);
|
---|
107 | }
|
---|
108 |
|
---|
109 | public override object Clone(IDictionary<Guid, object> clonedObjects) {
|
---|
110 | Dataset clone = new Dataset();
|
---|
111 | clonedObjects.Add(Guid, clone);
|
---|
112 | double[] cloneSamples = new double[rows * columns];
|
---|
113 | Array.Copy(samples, cloneSamples, samples.Length);
|
---|
114 | clone.rows = rows;
|
---|
115 | clone.columns = columns;
|
---|
116 | clone.Samples = cloneSamples;
|
---|
117 | clone.Name = Name;
|
---|
118 | clone.VariableNames = new string[VariableNames.Length];
|
---|
119 | Array.Copy(VariableNames, clone.VariableNames, VariableNames.Length);
|
---|
120 | return clone;
|
---|
121 | }
|
---|
122 |
|
---|
123 | public override XmlNode GetXmlNode(string name, XmlDocument document, IDictionary<Guid, IStorable> persistedObjects) {
|
---|
124 | XmlNode node = base.GetXmlNode(name, document, persistedObjects);
|
---|
125 | XmlAttribute problemName = document.CreateAttribute("Name");
|
---|
126 | problemName.Value = Name;
|
---|
127 | node.Attributes.Append(problemName);
|
---|
128 | XmlAttribute dim1 = document.CreateAttribute("Dimension1");
|
---|
129 | dim1.Value = rows.ToString(CultureInfo.InvariantCulture.NumberFormat);
|
---|
130 | node.Attributes.Append(dim1);
|
---|
131 | XmlAttribute dim2 = document.CreateAttribute("Dimension2");
|
---|
132 | dim2.Value = columns.ToString(CultureInfo.InvariantCulture.NumberFormat);
|
---|
133 | node.Attributes.Append(dim2);
|
---|
134 |
|
---|
135 | XmlAttribute variableNames = document.CreateAttribute("VariableNames");
|
---|
136 | variableNames.Value = GetVariableNamesString();
|
---|
137 | node.Attributes.Append(variableNames);
|
---|
138 |
|
---|
139 | node.InnerText = ToString(CultureInfo.InvariantCulture.NumberFormat);
|
---|
140 | return node;
|
---|
141 | }
|
---|
142 |
|
---|
143 | //public override void Populate(XmlNode node, IDictionary<Guid, IStorable> restoredObjects) {
|
---|
144 | // base.Populate(node, restoredObjects);
|
---|
145 | // Name = node.Attributes["Name"].Value;
|
---|
146 | // rows = int.Parse(node.Attributes["Dimension1"].Value, CultureInfo.InvariantCulture.NumberFormat);
|
---|
147 | // columns = int.Parse(node.Attributes["Dimension2"].Value, CultureInfo.InvariantCulture.NumberFormat);
|
---|
148 |
|
---|
149 | // VariableNames = ParseVariableNamesString(node.Attributes["VariableNames"].Value);
|
---|
150 |
|
---|
151 | // string[] tokens = node.InnerText.Split(';');
|
---|
152 | // if(tokens.Length != rows * columns) throw new FormatException();
|
---|
153 | // samples = new double[rows * columns];
|
---|
154 | // for(int row = 0; row < rows; row++) {
|
---|
155 | // for(int column = 0; column < columns; column++) {
|
---|
156 | // if(double.TryParse(tokens[row * columns + column], NumberStyles.Float, CultureInfo.InvariantCulture.NumberFormat, out samples[row * columns + column]) == false) {
|
---|
157 | // throw new FormatException("Can't parse " + tokens[row * columns + column] + " as double value.");
|
---|
158 | // }
|
---|
159 | // }
|
---|
160 | // }
|
---|
161 | // CreateDictionaries();
|
---|
162 | //}
|
---|
163 | public override void Populate(XmlReader reader, IDictionary<Guid, IStorable> restoredObjects) {
|
---|
164 | base.Populate(reader, restoredObjects);
|
---|
165 | Name = reader["Name"];
|
---|
166 | rows = int.Parse(reader["Dimension1"], CultureInfo.InvariantCulture.NumberFormat);
|
---|
167 | columns = int.Parse(reader["Dimension2"], CultureInfo.InvariantCulture.NumberFormat);
|
---|
168 | VariableNames = ParseVariableNamesString(reader["VariableNames"]);
|
---|
169 | string[] tokens = reader.ReadString().Split(';');
|
---|
170 |
|
---|
171 | if(tokens.Length != rows * columns) throw new FormatException();
|
---|
172 | samples = new double[rows * columns];
|
---|
173 | for(int row = 0; row < rows; row++) {
|
---|
174 | for(int column = 0; column < columns; column++) {
|
---|
175 | if(double.TryParse(tokens[row * columns + column], NumberStyles.Float, CultureInfo.InvariantCulture.NumberFormat, out samples[row * columns + column]) == false) {
|
---|
176 | throw new FormatException("Can't parse " + tokens[row * columns + column] + " as double value.");
|
---|
177 | }
|
---|
178 | }
|
---|
179 | }
|
---|
180 | CreateDictionaries();
|
---|
181 | }
|
---|
182 |
|
---|
183 | public override string ToString() {
|
---|
184 | return ToString(CultureInfo.CurrentCulture.NumberFormat);
|
---|
185 | }
|
---|
186 |
|
---|
187 | private string ToString(NumberFormatInfo format) {
|
---|
188 | StringBuilder builder = new StringBuilder();
|
---|
189 | for(int row = 0; row < rows; row++) {
|
---|
190 | for(int column = 0; column < columns; column++) {
|
---|
191 | builder.Append(";");
|
---|
192 | builder.Append(samples[row*columns+column].ToString(format));
|
---|
193 | }
|
---|
194 | }
|
---|
195 | if(builder.Length > 0) builder.Remove(0, 1);
|
---|
196 | return builder.ToString();
|
---|
197 | }
|
---|
198 |
|
---|
199 | private string GetVariableNamesString() {
|
---|
200 | string s = "";
|
---|
201 | for (int i = 0; i < variableNames.Length; i++) {
|
---|
202 | s += variableNames[i] + "; ";
|
---|
203 | }
|
---|
204 |
|
---|
205 | if (variableNames.Length > 0) {
|
---|
206 | s = s.TrimEnd(';', ' ');
|
---|
207 | }
|
---|
208 | return s;
|
---|
209 | }
|
---|
210 |
|
---|
211 | private string[] ParseVariableNamesString(string p) {
|
---|
212 | p = p.Trim();
|
---|
213 | string[] tokens = p.Split(new char[] {';'}, StringSplitOptions.RemoveEmptyEntries);
|
---|
214 | return tokens;
|
---|
215 | }
|
---|
216 |
|
---|
217 |
|
---|
218 | // return value of GetMean should be memoized because it is called repeatedly in Evaluators
|
---|
219 | public double GetMean(int column, int from, int to) {
|
---|
220 | Dictionary<int, double[]> columnMeans = means[column];
|
---|
221 | if(columnMeans.ContainsKey(from)) {
|
---|
222 | double[] fromMeans = columnMeans[from];
|
---|
223 | if(fromMeans[to-from] >= 0.0) {
|
---|
224 | // already calculated
|
---|
225 | return fromMeans[to-from];
|
---|
226 | } else {
|
---|
227 | // not yet calculated => calculate
|
---|
228 | fromMeans[to-from] = CalculateMean(column, from, to);
|
---|
229 | return fromMeans[to-from];
|
---|
230 | }
|
---|
231 | } else {
|
---|
232 | // never saw this from-index => create a new array, initialize and recalculate for to-index
|
---|
233 | double[] fromMeans = new double[rows - from];
|
---|
234 | // fill with negative values to indicate which means have already been calculated
|
---|
235 | for(int i=0;i<fromMeans.Length;i++) {fromMeans[i] = -1.0;}
|
---|
236 | // store new array in the dictionary
|
---|
237 | columnMeans[from] = fromMeans;
|
---|
238 | // calculate for specific to-index
|
---|
239 | fromMeans[to-from] = CalculateMean(column, from, to);
|
---|
240 | return fromMeans[to-from];
|
---|
241 | }
|
---|
242 | }
|
---|
243 |
|
---|
244 | private double CalculateMean(int column, int from, int to) {
|
---|
245 | double[] values = new double[to - from +1];
|
---|
246 | for(int sample = from; sample <= to; sample++) {
|
---|
247 | values[sample - from] = GetValue(sample, column);
|
---|
248 | }
|
---|
249 |
|
---|
250 | return Statistics.Mean(values);
|
---|
251 | }
|
---|
252 |
|
---|
253 | // return value of GetRange should be memoized because it is called repeatedly in Evaluators
|
---|
254 | public double GetRange(int column, int from, int to) {
|
---|
255 | Dictionary<int, double[]> columnRanges = ranges[column];
|
---|
256 | if(columnRanges.ContainsKey(from)) {
|
---|
257 | double[] fromRanges = columnRanges[from];
|
---|
258 | if(fromRanges[to-from] >= 0.0) {
|
---|
259 | // already calculated
|
---|
260 | return fromRanges[to-from];
|
---|
261 | } else {
|
---|
262 | // not yet calculated => calculate
|
---|
263 | fromRanges[to-from] = CalculateRange(column, from, to);
|
---|
264 | return fromRanges[to-from];
|
---|
265 | }
|
---|
266 | } else {
|
---|
267 | // never saw this from-index => create a new array, initialize and recalculate for to-index
|
---|
268 | double[] fromRanges = new double[rows - from];
|
---|
269 | // fill with negative values to indicate which means have already been calculated
|
---|
270 | for(int i = 0; i < fromRanges.Length; i++) { fromRanges[i] = -1.0; }
|
---|
271 | // store in dictionary
|
---|
272 | columnRanges[from] = fromRanges;
|
---|
273 | // calculate for specific to-index
|
---|
274 | fromRanges[to-from] = CalculateRange(column, from, to);
|
---|
275 | return fromRanges[to-from];
|
---|
276 | }
|
---|
277 | }
|
---|
278 |
|
---|
279 | private double CalculateRange(int column, int from, int to) {
|
---|
280 | double[] values = new double[to - from + 1];
|
---|
281 | for(int sample = from; sample <= to; sample++) {
|
---|
282 | values[sample - from] = GetValue(sample, column);
|
---|
283 | }
|
---|
284 |
|
---|
285 | return Statistics.Range(values);
|
---|
286 | }
|
---|
287 | }
|
---|
288 | }
|
---|