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