[2] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[3253] | 3 | * Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[2] | 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 System.Globalization;
|
---|
| 26 | using System.Text;
|
---|
[2285] | 27 | using System.Linq;
|
---|
[3376] | 28 | using HeuristicLab.Common;
|
---|
[3253] | 29 | using HeuristicLab.Core;
|
---|
| 30 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 31 | using HeuristicLab.Data;
|
---|
[2] | 32 |
|
---|
[3253] | 33 | namespace HeuristicLab.Problems.DataAnalysis {
|
---|
| 34 | [Item("Dataset", "Represents a dataset containing data that should be analyzed.")]
|
---|
| 35 | [StorableClass]
|
---|
| 36 | public sealed class Dataset : NamedItem, IStringConvertibleMatrix {
|
---|
[3981] | 37 | // empty constructor for cloning
|
---|
[3994] | 38 | private Dataset()
|
---|
| 39 | : base() {
|
---|
[3981] | 40 | }
|
---|
[3994] | 41 |
|
---|
[3933] | 42 | [StorableConstructor]
|
---|
| 43 | protected Dataset(bool deserializing)
|
---|
| 44 | : base(deserializing) {
|
---|
[2319] | 45 | }
|
---|
[2] | 46 |
|
---|
[3264] | 47 | public Dataset(IEnumerable<string> variableNames, double[,] data)
|
---|
[3933] | 48 | : base() {
|
---|
[2319] | 49 | Name = "-";
|
---|
[3264] | 50 | if (variableNames.Count() != data.GetLength(1)) {
|
---|
| 51 | throw new ArgumentException("Number of variable names doesn't match the number of columns of data");
|
---|
| 52 | }
|
---|
[3933] | 53 | this.data = (double[,])data.Clone();
|
---|
[3442] | 54 | this.variableNames = variableNames.ToArray();
|
---|
[2038] | 55 | }
|
---|
| 56 |
|
---|
[3294] | 57 | [Storable]
|
---|
[3308] | 58 | private string[] variableNames;
|
---|
[2319] | 59 | public IEnumerable<string> VariableNames {
|
---|
| 60 | get { return variableNames; }
|
---|
[333] | 61 | }
|
---|
| 62 |
|
---|
[3294] | 63 | [Storable]
|
---|
[3308] | 64 | private double[,] data;
|
---|
| 65 | private double[,] Data {
|
---|
[3253] | 66 | get { return data; }
|
---|
[2] | 67 | }
|
---|
| 68 |
|
---|
[3253] | 69 | // elementwise access
|
---|
| 70 | public double this[int rowIndex, int columnIndex] {
|
---|
| 71 | get { return data[rowIndex, columnIndex]; }
|
---|
[2] | 72 | }
|
---|
[3839] | 73 | public double this[string variableName, int rowIndex] {
|
---|
| 74 | get {
|
---|
| 75 | int columnIndex = GetVariableIndex(variableName);
|
---|
| 76 | return data[rowIndex, columnIndex];
|
---|
| 77 | }
|
---|
| 78 | }
|
---|
[3933] | 79 |
|
---|
| 80 | public double[] GetVariableValues(int variableIndex) {
|
---|
| 81 | return GetVariableValues(variableIndex, 0, Rows);
|
---|
[1287] | 82 | }
|
---|
[3294] | 83 | public double[] GetVariableValues(int variableIndex, int start, int end) {
|
---|
[2311] | 84 | if (start < 0 || !(start <= end))
|
---|
| 85 | throw new ArgumentException("Start must be between 0 and end (" + end + ").");
|
---|
[3308] | 86 | if (end > Rows || end < start)
|
---|
| 87 | throw new ArgumentException("End must be between start (" + start + ") and dataset rows (" + Rows + ").");
|
---|
[2311] | 88 |
|
---|
| 89 | double[] values = new double[end - start];
|
---|
| 90 | for (int i = 0; i < end - start; i++)
|
---|
[3253] | 91 | values[i] = data[i + start, variableIndex];
|
---|
[2311] | 92 | return values;
|
---|
| 93 | }
|
---|
[3933] | 94 | public double[] GetVariableValues(string variableName) {
|
---|
| 95 | return GetVariableValues(GetVariableIndex(variableName), 0, Rows);
|
---|
| 96 | }
|
---|
[3294] | 97 | public double[] GetVariableValues(string variableName, int start, int end) {
|
---|
| 98 | return GetVariableValues(GetVariableIndex(variableName), start, end);
|
---|
[2311] | 99 | }
|
---|
| 100 |
|
---|
[3994] | 101 | public IEnumerable<double> GetEnumeratedVariableValues(int variableIndex) {
|
---|
| 102 | return GetEnumeratedVariableValues(variableIndex, 0, Rows);
|
---|
| 103 | }
|
---|
| 104 | public IEnumerable<double> GetEnumeratedVariableValues(int variableIndex, int start, int end) {
|
---|
| 105 | if (start < 0 || !(start <= end))
|
---|
| 106 | throw new ArgumentException("Start must be between 0 and end (" + end + ").");
|
---|
| 107 | if (end > Rows || end < start)
|
---|
| 108 | throw new ArgumentException("End must be between start (" + start + ") and dataset rows (" + Rows + ").");
|
---|
| 109 | for (int i = 0; i < end - start; i++)
|
---|
| 110 | yield return data[i + start, variableIndex];
|
---|
| 111 | }
|
---|
| 112 | public IEnumerable<double> GetEnumeratedVariableValues(string variableName) {
|
---|
| 113 | return GetEnumeratedVariableValues(GetVariableIndex(variableName), 0, Rows);
|
---|
| 114 | }
|
---|
| 115 | public IEnumerable<double> GetEnumeratedVariableValues(string variableName, int start, int end) {
|
---|
| 116 | return GetEnumeratedVariableValues(GetVariableIndex(variableName), start, end);
|
---|
| 117 | }
|
---|
| 118 |
|
---|
| 119 |
|
---|
| 120 |
|
---|
[3294] | 121 | public string GetVariableName(int variableIndex) {
|
---|
[2319] | 122 | return variableNames[variableIndex];
|
---|
| 123 | }
|
---|
| 124 |
|
---|
[3294] | 125 | public int GetVariableIndex(string variableName) {
|
---|
[2319] | 126 | for (int i = 0; i < variableNames.Length; i++) {
|
---|
| 127 | if (variableNames[i].Equals(variableName)) return i;
|
---|
| 128 | }
|
---|
| 129 | throw new ArgumentException("The variable name " + variableName + " was not found.");
|
---|
| 130 | }
|
---|
[2310] | 131 |
|
---|
[3933] | 132 | public double[,] GetClonedData() {
|
---|
| 133 | return (double[,])data.Clone();
|
---|
[2] | 134 | }
|
---|
| 135 |
|
---|
[3253] | 136 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
[3981] | 137 | Dataset clone = (Dataset)base.Clone(cloner);
|
---|
| 138 | clone.variableNames = variableNames;
|
---|
| 139 | clone.data = data;
|
---|
| 140 | return clone;
|
---|
[2319] | 141 | }
|
---|
| 142 |
|
---|
[3253] | 143 | public event EventHandler Reset;
|
---|
| 144 | private void OnReset(EventArgs e) {
|
---|
| 145 | var listeners = Reset;
|
---|
| 146 | if (listeners != null) listeners(this, e);
|
---|
[2319] | 147 | }
|
---|
[2] | 148 |
|
---|
[3253] | 149 | #region IStringConvertibleMatrix Members
|
---|
[237] | 150 |
|
---|
[3253] | 151 | public int Rows {
|
---|
[3933] | 152 | get { return data.GetLength(0); }
|
---|
| 153 | set { throw new NotSupportedException(); }
|
---|
[2] | 154 | }
|
---|
| 155 |
|
---|
[3253] | 156 | public int Columns {
|
---|
[3933] | 157 | get { return data.GetLength(1); }
|
---|
| 158 | set { throw new NotSupportedException(); }
|
---|
[2] | 159 | }
|
---|
| 160 |
|
---|
[3321] | 161 | public bool SortableView {
|
---|
[3933] | 162 | get { return false; }
|
---|
| 163 | set { throw new NotSupportedException(); }
|
---|
[3321] | 164 | }
|
---|
| 165 |
|
---|
[3430] | 166 | public bool ReadOnly {
|
---|
[3933] | 167 | get { return true; }
|
---|
[3430] | 168 | }
|
---|
| 169 |
|
---|
[3308] | 170 | IEnumerable<string> IStringConvertibleMatrix.ColumnNames {
|
---|
| 171 | get { return this.VariableNames; }
|
---|
[3933] | 172 | set { throw new NotSupportedException(); }
|
---|
[3308] | 173 | }
|
---|
| 174 |
|
---|
[3311] | 175 | IEnumerable<string> IStringConvertibleMatrix.RowNames {
|
---|
| 176 | get { return new List<string>(); }
|
---|
[3933] | 177 | set { throw new NotSupportedException(); }
|
---|
[3311] | 178 | }
|
---|
| 179 |
|
---|
[3253] | 180 | public bool Validate(string value, out string errorMessage) {
|
---|
[3933] | 181 | throw new NotSupportedException();
|
---|
[3253] | 182 | }
|
---|
[2] | 183 |
|
---|
[3253] | 184 | public string GetValue(int rowIndex, int columnIndex) {
|
---|
[3308] | 185 | return data[rowIndex, columnIndex].ToString();
|
---|
[2] | 186 | }
|
---|
| 187 |
|
---|
[3253] | 188 | public bool SetValue(string value, int rowIndex, int columnIndex) {
|
---|
[3933] | 189 | throw new NotSupportedException();
|
---|
[237] | 190 | }
|
---|
| 191 |
|
---|
[3321] | 192 | public event EventHandler ColumnNamesChanged;
|
---|
| 193 | private void OnColumnNamesChanged() {
|
---|
| 194 | EventHandler handler = ColumnNamesChanged;
|
---|
| 195 | if (handler != null)
|
---|
| 196 | handler(this, EventArgs.Empty);
|
---|
| 197 | }
|
---|
| 198 | public event EventHandler RowNamesChanged;
|
---|
| 199 | private void OnRowNamesChanged() {
|
---|
| 200 | EventHandler handler = RowNamesChanged;
|
---|
| 201 | if (handler != null)
|
---|
| 202 | handler(this, EventArgs.Empty);
|
---|
| 203 | }
|
---|
| 204 | public event EventHandler SortableViewChanged;
|
---|
| 205 | private void OnSortableViewChanged() {
|
---|
| 206 | EventHandler handler = SortableViewChanged;
|
---|
| 207 | if (handler != null)
|
---|
| 208 | handler(this, EventArgs.Empty);
|
---|
| 209 | }
|
---|
[3253] | 210 | public event EventHandler<EventArgs<int, int>> ItemChanged;
|
---|
[3321] | 211 | private void OnItemChanged(int rowIndex, int columnIndex) {
|
---|
| 212 | if (ItemChanged != null)
|
---|
| 213 | ItemChanged(this, new EventArgs<int, int>(rowIndex, columnIndex));
|
---|
| 214 | OnToStringChanged();
|
---|
| 215 | }
|
---|
[2012] | 216 | #endregion
|
---|
[2] | 217 | }
|
---|
| 218 | }
|
---|