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