[2] | 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 {
|
---|
[207] | 31 | public sealed class Dataset : ItemBase {
|
---|
[2] | 32 |
|
---|
| 33 | private string name;
|
---|
| 34 | private double[] samples;
|
---|
| 35 | private int rows;
|
---|
[333] | 36 | private int columns;
|
---|
[237] | 37 | private Dictionary<int, Dictionary<int, double>>[] cachedMeans;
|
---|
| 38 | private Dictionary<int, Dictionary<int, double>>[] cachedRanges;
|
---|
| 39 | private double[] scalingFactor;
|
---|
| 40 | private double[] scalingOffset;
|
---|
[2] | 41 |
|
---|
[333] | 42 | public string Name {
|
---|
| 43 | get { return name; }
|
---|
| 44 | set { name = value; }
|
---|
[312] | 45 | }
|
---|
| 46 |
|
---|
[2] | 47 | public int Rows {
|
---|
| 48 | get { return rows; }
|
---|
| 49 | set { rows = value; }
|
---|
| 50 | }
|
---|
| 51 |
|
---|
| 52 | public int Columns {
|
---|
| 53 | get { return columns; }
|
---|
[1786] | 54 | set {
|
---|
[1287] | 55 | columns = value;
|
---|
| 56 | if (variableNames == null || variableNames.Length != columns) {
|
---|
| 57 | variableNames = new string[columns];
|
---|
| 58 | }
|
---|
| 59 | }
|
---|
[2] | 60 | }
|
---|
| 61 |
|
---|
[333] | 62 | public double[] ScalingFactor {
|
---|
| 63 | get { return scalingFactor; }
|
---|
| 64 | }
|
---|
| 65 | public double[] ScalingOffset {
|
---|
| 66 | get { return scalingOffset; }
|
---|
| 67 | }
|
---|
| 68 |
|
---|
[2] | 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) {
|
---|
[1786] | 74 | if (v != samples[columns * i + j]) {
|
---|
[2] | 75 | samples[columns * i + j] = v;
|
---|
[232] | 76 | CreateDictionaries();
|
---|
[2] | 77 | FireChanged();
|
---|
| 78 | }
|
---|
| 79 | }
|
---|
| 80 |
|
---|
| 81 | public double[] Samples {
|
---|
| 82 | get { return samples; }
|
---|
[237] | 83 | set {
|
---|
| 84 | scalingFactor = new double[columns];
|
---|
| 85 | scalingOffset = new double[columns];
|
---|
[1786] | 86 | for (int i = 0; i < scalingFactor.Length; i++) {
|
---|
[237] | 87 | scalingFactor[i] = 1.0;
|
---|
| 88 | scalingOffset[i] = 0.0;
|
---|
| 89 | }
|
---|
[2] | 90 | samples = value;
|
---|
| 91 | CreateDictionaries();
|
---|
| 92 | FireChanged();
|
---|
| 93 | }
|
---|
| 94 | }
|
---|
| 95 |
|
---|
| 96 | private string[] variableNames;
|
---|
| 97 |
|
---|
| 98 | public Dataset() {
|
---|
| 99 | Name = "-";
|
---|
[1287] | 100 | variableNames = new string[] { "Var0" };
|
---|
[2] | 101 | Columns = 1;
|
---|
| 102 | Rows = 1;
|
---|
| 103 | Samples = new double[1];
|
---|
[237] | 104 | scalingOffset = new double[] { 0.0 };
|
---|
| 105 | scalingFactor = new double[] { 1.0 };
|
---|
[2] | 106 | }
|
---|
| 107 |
|
---|
| 108 | private void CreateDictionaries() {
|
---|
| 109 | // keep a means and ranges dictionary for each column (possible target variable) of the dataset.
|
---|
[196] | 110 | cachedMeans = new Dictionary<int, Dictionary<int, double>>[columns];
|
---|
| 111 | cachedRanges = new Dictionary<int, Dictionary<int, double>>[columns];
|
---|
[1786] | 112 | for (int i = 0; i < columns; i++) {
|
---|
[196] | 113 | cachedMeans[i] = new Dictionary<int, Dictionary<int, double>>();
|
---|
| 114 | cachedRanges[i] = new Dictionary<int, Dictionary<int, double>>();
|
---|
[2] | 115 | }
|
---|
| 116 | }
|
---|
| 117 |
|
---|
[1287] | 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 |
|
---|
[2] | 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;
|
---|
[1287] | 140 | clone.variableNames = new string[variableNames.Length];
|
---|
| 141 | Array.Copy(variableNames, clone.variableNames, variableNames.Length);
|
---|
[237] | 142 | Array.Copy(scalingFactor, clone.scalingFactor, columns);
|
---|
| 143 | Array.Copy(scalingOffset, clone.scalingOffset, columns);
|
---|
[2] | 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);
|
---|
[237] | 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);
|
---|
[2] | 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);
|
---|
[237] | 176 |
|
---|
[1287] | 177 | variableNames = ParseVariableNamesString(node.Attributes["VariableNames"].Value);
|
---|
[1786] | 178 | if (node.Attributes["ScalingFactors"] != null)
|
---|
[237] | 179 | scalingFactor = ParseDoubleString(node.Attributes["ScalingFactors"].Value);
|
---|
| 180 | else {
|
---|
| 181 | scalingFactor = new double[columns]; // compatibility with old serialization format
|
---|
[1786] | 182 | for (int i = 0; i < scalingFactor.Length; i++) scalingFactor[i] = 1.0;
|
---|
[237] | 183 | }
|
---|
[1786] | 184 | if (node.Attributes["ScalingOffsets"] != null)
|
---|
[237] | 185 | scalingOffset = ParseDoubleString(node.Attributes["ScalingOffsets"].Value);
|
---|
| 186 | else {
|
---|
| 187 | scalingOffset = new double[columns]; // compatibility with old serialization format
|
---|
[1786] | 188 | for (int i = 0; i < scalingOffset.Length; i++) scalingOffset[i] = 0.0;
|
---|
[237] | 189 | }
|
---|
[2] | 190 |
|
---|
| 191 | string[] tokens = node.InnerText.Split(';');
|
---|
[1786] | 192 | if (tokens.Length != rows * columns) throw new FormatException();
|
---|
[2] | 193 | samples = new double[rows * columns];
|
---|
[1786] | 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) {
|
---|
[2] | 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();
|
---|
[1786] | 210 | for (int row = 0; row < rows; row++) {
|
---|
| 211 | for (int column = 0; column < columns; column++) {
|
---|
[2] | 212 | builder.Append(";");
|
---|
[344] | 213 | builder.Append(samples[row * columns + column].ToString("r", format));
|
---|
[2] | 214 | }
|
---|
| 215 | }
|
---|
[1786] | 216 | if (builder.Length > 0) builder.Remove(0, 1);
|
---|
[2] | 217 | return builder.ToString();
|
---|
| 218 | }
|
---|
| 219 |
|
---|
| 220 | private string GetVariableNamesString() {
|
---|
| 221 | string s = "";
|
---|
[1786] | 222 | for (int i = 0; i < variableNames.Length; i++) {
|
---|
[2] | 223 | s += variableNames[i] + "; ";
|
---|
| 224 | }
|
---|
| 225 |
|
---|
[1786] | 226 | if (variableNames.Length > 0) {
|
---|
[2] | 227 | s = s.TrimEnd(';', ' ');
|
---|
| 228 | }
|
---|
| 229 | return s;
|
---|
| 230 | }
|
---|
[237] | 231 | private string GetString(double[] xs) {
|
---|
| 232 | string s = "";
|
---|
[1786] | 233 | for (int i = 0; i < xs.Length; i++) {
|
---|
[344] | 234 | s += xs[i].ToString("r", CultureInfo.InvariantCulture) + "; ";
|
---|
[237] | 235 | }
|
---|
[2] | 236 |
|
---|
[1786] | 237 | if (xs.Length > 0) {
|
---|
[237] | 238 | s = s.TrimEnd(';', ' ');
|
---|
| 239 | }
|
---|
| 240 | return s;
|
---|
| 241 | }
|
---|
| 242 |
|
---|
[2] | 243 | private string[] ParseVariableNamesString(string p) {
|
---|
| 244 | p = p.Trim();
|
---|
[237] | 245 | string[] tokens = p.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
|
---|
[1786] | 246 | for (int i = 0; i < tokens.Length; i++) tokens[i] = tokens[i].Trim();
|
---|
[2] | 247 | return tokens;
|
---|
| 248 | }
|
---|
[237] | 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];
|
---|
[1786] | 253 | for (int i = 0; i < xs.Length; i++) {
|
---|
[237] | 254 | xs[i] = double.Parse(ss[i], CultureInfo.InvariantCulture);
|
---|
| 255 | }
|
---|
| 256 | return xs;
|
---|
| 257 | }
|
---|
[2] | 258 |
|
---|
[132] | 259 | public double GetMean(int column) {
|
---|
[1784] | 260 | return GetMean(column, 0, Rows);
|
---|
[132] | 261 | }
|
---|
[2] | 262 |
|
---|
| 263 | public double GetMean(int column, int from, int to) {
|
---|
[1786] | 264 | if (!cachedMeans[column].ContainsKey(from) || !cachedMeans[column][from].ContainsKey(to)) {
|
---|
[1784] | 265 | double[] values = new double[to - from];
|
---|
[1786] | 266 | for (int sample = from; sample < to; sample++) {
|
---|
[196] | 267 | values[sample - from] = GetValue(sample, column);
|
---|
| 268 | }
|
---|
| 269 | double mean = Statistics.Mean(values);
|
---|
[1786] | 270 | if (!cachedMeans[column].ContainsKey(from)) cachedMeans[column][from] = new Dictionary<int, double>();
|
---|
[196] | 271 | cachedMeans[column][from][to] = mean;
|
---|
| 272 | return mean;
|
---|
| 273 | } else {
|
---|
| 274 | return cachedMeans[column][from][to];
|
---|
[2] | 275 | }
|
---|
| 276 | }
|
---|
| 277 |
|
---|
[132] | 278 | public double GetRange(int column) {
|
---|
[1784] | 279 | return GetRange(column, 0, Rows);
|
---|
[132] | 280 | }
|
---|
| 281 |
|
---|
[2] | 282 | public double GetRange(int column, int from, int to) {
|
---|
[1786] | 283 | if (!cachedRanges[column].ContainsKey(from) || !cachedRanges[column][from].ContainsKey(to)) {
|
---|
[1784] | 284 | double[] values = new double[to - from];
|
---|
[1786] | 285 | for (int sample = from; sample < to; sample++) {
|
---|
[196] | 286 | values[sample - from] = GetValue(sample, column);
|
---|
| 287 | }
|
---|
| 288 | double range = Statistics.Range(values);
|
---|
[1786] | 289 | if (!cachedRanges[column].ContainsKey(from)) cachedRanges[column][from] = new Dictionary<int, double>();
|
---|
[196] | 290 | cachedRanges[column][from][to] = range;
|
---|
| 291 | return range;
|
---|
| 292 | } else {
|
---|
| 293 | return cachedRanges[column][from][to];
|
---|
[2] | 294 | }
|
---|
| 295 | }
|
---|
[232] | 296 |
|
---|
| 297 | public double GetMaximum(int column) {
|
---|
| 298 | double max = Double.NegativeInfinity;
|
---|
[1786] | 299 | for (int i = 0; i < Rows; i++) {
|
---|
[232] | 300 | double val = GetValue(i, column);
|
---|
[1786] | 301 | if (!double.IsNaN(val) && val > max) max = val;
|
---|
[232] | 302 | }
|
---|
| 303 | return max;
|
---|
| 304 | }
|
---|
| 305 |
|
---|
| 306 | public double GetMinimum(int column) {
|
---|
| 307 | double min = Double.PositiveInfinity;
|
---|
[1786] | 308 | for (int i = 0; i < Rows; i++) {
|
---|
[232] | 309 | double val = GetValue(i, column);
|
---|
[1786] | 310 | if (!double.IsNaN(val) && val < min) min = val;
|
---|
[232] | 311 | }
|
---|
| 312 | return min;
|
---|
| 313 | }
|
---|
[237] | 314 |
|
---|
| 315 | internal void ScaleVariable(int column) {
|
---|
[1786] | 316 | if (scalingFactor[column] == 1.0 && scalingOffset[column] == 0.0) {
|
---|
[237] | 317 | double min = GetMinimum(column);
|
---|
| 318 | double max = GetMaximum(column);
|
---|
| 319 | double range = max - min;
|
---|
[1786] | 320 | if (range == 0) ScaleVariable(column, 1.0, -min);
|
---|
[312] | 321 | else ScaleVariable(column, 1.0 / range, -min);
|
---|
[237] | 322 | }
|
---|
| 323 | CreateDictionaries();
|
---|
| 324 | FireChanged();
|
---|
| 325 | }
|
---|
| 326 |
|
---|
[312] | 327 | internal void ScaleVariable(int column, double factor, double offset) {
|
---|
| 328 | scalingFactor[column] = factor;
|
---|
| 329 | scalingOffset[column] = offset;
|
---|
[1786] | 330 | for (int i = 0; i < Rows; i++) {
|
---|
[312] | 331 | double origValue = samples[i * columns + column];
|
---|
| 332 | samples[i * columns + column] = (origValue + offset) * factor;
|
---|
| 333 | }
|
---|
| 334 | CreateDictionaries();
|
---|
| 335 | FireChanged();
|
---|
| 336 | }
|
---|
| 337 |
|
---|
[237] | 338 | internal void UnscaleVariable(int column) {
|
---|
[1786] | 339 | if (scalingFactor[column] != 1.0 || scalingOffset[column] != 0.0) {
|
---|
| 340 | for (int i = 0; i < rows; i++) {
|
---|
[237] | 341 | double scaledValue = samples[i * columns + column];
|
---|
[312] | 342 | samples[i * columns + column] = scaledValue / scalingFactor[column] - scalingOffset[column];
|
---|
[237] | 343 | }
|
---|
| 344 | scalingFactor[column] = 1.0;
|
---|
| 345 | scalingOffset[column] = 0.0;
|
---|
| 346 | }
|
---|
| 347 | }
|
---|
[2] | 348 | }
|
---|
| 349 | }
|
---|