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