[11589] | 1 | #region License Information
|
---|
| 2 |
|
---|
| 3 | /* HeuristicLab
|
---|
[15583] | 4 | * Copyright (C) 2002-2018 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[11589] | 5 | *
|
---|
| 6 | * This file is part of HeuristicLab.
|
---|
| 7 | *
|
---|
| 8 | * HeuristicLab is free software: you can redistribute it and/or modify
|
---|
| 9 | * it under the terms of the GNU General Public License as published by
|
---|
| 10 | * the Free Software Foundation, either version 3 of the License, or
|
---|
| 11 | * (at your option) any later version.
|
---|
| 12 | *
|
---|
| 13 | * HeuristicLab is distributed in the hope that it will be useful,
|
---|
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 16 | * GNU General Public License for more details.
|
---|
| 17 | *
|
---|
| 18 | * You should have received a copy of the GNU General Public License
|
---|
| 19 | * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
|
---|
| 20 | */
|
---|
| 21 |
|
---|
| 22 | #endregion
|
---|
| 23 |
|
---|
| 24 | using System;
|
---|
| 25 | using System.Collections;
|
---|
| 26 | using System.Collections.Generic;
|
---|
| 27 | using System.Linq;
|
---|
| 28 | using HeuristicLab.Common;
|
---|
| 29 | using HeuristicLab.Core;
|
---|
[12141] | 30 | using HeuristicLab.Data;
|
---|
[11589] | 31 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 32 |
|
---|
| 33 | namespace HeuristicLab.Problems.DataAnalysis {
|
---|
| 34 | [Item("ModifiableDataset", "Represents a dataset containing data that should be analyzed, which can be modified by adding or replacing variables and values.")]
|
---|
| 35 | [StorableClass]
|
---|
[12511] | 36 | public sealed class ModifiableDataset : Dataset, IStringConvertibleMatrix {
|
---|
[11589] | 37 | [StorableConstructor]
|
---|
| 38 | private ModifiableDataset(bool deserializing) : base(deserializing) { }
|
---|
[13027] | 39 |
|
---|
| 40 | private ModifiableDataset(ModifiableDataset original, Cloner cloner) : base(original, cloner) {
|
---|
[16188] | 41 | variableNames = new List<string>(original.variableNames);
|
---|
| 42 | variableValues = CloneValues(original.variableValues);
|
---|
[13027] | 43 | }
|
---|
[16188] | 44 |
|
---|
[11589] | 45 | public override IDeepCloneable Clone(Cloner cloner) { return new ModifiableDataset(this, cloner); }
|
---|
[12508] | 46 |
|
---|
[16188] | 47 | public ModifiableDataset() { }
|
---|
[11589] | 48 |
|
---|
[16188] | 49 | public ModifiableDataset(IEnumerable<string> variableNames, IEnumerable<IList> variableValues, bool cloneValues = false) :
|
---|
| 50 | base(variableNames, variableValues, cloneValues) { }
|
---|
| 51 |
|
---|
| 52 | public Dataset ToDataset() {
|
---|
| 53 | return new Dataset(variableNames, variableNames.Select(v => variableValues[v]));
|
---|
| 54 | }
|
---|
| 55 |
|
---|
| 56 |
|
---|
| 57 | public IEnumerable<object> GetRow(int row) {
|
---|
| 58 | return variableValues.Select(x => x.Value[row]);
|
---|
| 59 | }
|
---|
| 60 |
|
---|
| 61 | public void AddRow(IEnumerable<object> values) {
|
---|
[11589] | 62 | var list = values.ToList();
|
---|
| 63 | if (list.Count != variableNames.Count)
|
---|
| 64 | throw new ArgumentException("The number of values must be equal to the number of variable names.");
|
---|
| 65 | // check if all the values are of the correct type
|
---|
| 66 | for (int i = 0; i < list.Count; ++i) {
|
---|
| 67 | if (list[i].GetType() != GetVariableType(variableNames[i])) {
|
---|
| 68 | throw new ArgumentException("The type of the provided value does not match the variable type.");
|
---|
| 69 | }
|
---|
| 70 | }
|
---|
[16188] | 71 | // add values
|
---|
[11589] | 72 | for (int i = 0; i < list.Count; ++i) {
|
---|
[16188] | 73 | variableValues[variableNames[i]].Add(list[i]);
|
---|
[11589] | 74 | }
|
---|
[16188] | 75 | Rows++;
|
---|
| 76 | OnRowsChanged();
|
---|
[12489] | 77 | OnReset();
|
---|
[11589] | 78 | }
|
---|
| 79 |
|
---|
[16188] | 80 | public void ReplaceRow(int row, IEnumerable<object> values) {
|
---|
[11589] | 81 | var list = values.ToList();
|
---|
| 82 | if (list.Count != variableNames.Count)
|
---|
| 83 | throw new ArgumentException("The number of values must be equal to the number of variable names.");
|
---|
| 84 | // check if all the values are of the correct type
|
---|
| 85 | for (int i = 0; i < list.Count; ++i) {
|
---|
| 86 | if (list[i].GetType() != GetVariableType(variableNames[i])) {
|
---|
| 87 | throw new ArgumentException("The type of the provided value does not match the variable type.");
|
---|
| 88 | }
|
---|
| 89 | }
|
---|
[16188] | 90 | // replace values
|
---|
[11589] | 91 | for (int i = 0; i < list.Count; ++i) {
|
---|
[16188] | 92 | variableValues[variableNames[i]][row] = list[i];
|
---|
[11589] | 93 | }
|
---|
[16188] | 94 | OnReset();
|
---|
| 95 | }
|
---|
| 96 |
|
---|
| 97 | // slow, avoid using this
|
---|
| 98 | public void RemoveRow(int row) {
|
---|
| 99 | foreach (var list in variableValues.Values)
|
---|
| 100 | list.RemoveAt(row);
|
---|
| 101 | Rows--;
|
---|
[12489] | 102 | OnRowsChanged();
|
---|
| 103 | OnReset();
|
---|
[11589] | 104 | }
|
---|
| 105 |
|
---|
[12032] | 106 | // adds a new variable to the dataset
|
---|
[15769] | 107 | public void AddVariable(string variableName, IList values) {
|
---|
[16188] | 108 | InsertVariable(variableName, Columns, values);
|
---|
| 109 | }
|
---|
| 110 |
|
---|
| 111 | public void InsertVariable(string variableName, int position, IList values) {
|
---|
[12032] | 112 | if (variableValues.ContainsKey(variableName))
|
---|
[15769] | 113 | throw new ArgumentException(string.Format("Variable {0} is already present in the dataset.", variableName));
|
---|
| 114 |
|
---|
[16188] | 115 | if (position < 0 || position > Columns)
|
---|
| 116 | throw new ArgumentException(string.Format("Incorrect position {0} specified. The position must be between 0 and {1}.", position, Columns));
|
---|
[15769] | 117 |
|
---|
[16188] | 118 | if (values == null)
|
---|
| 119 | throw new ArgumentNullException("values", "Values must not be null. At least an empty list of values has to be provided.");
|
---|
| 120 |
|
---|
| 121 | if (values.Count != Rows)
|
---|
| 122 | throw new ArgumentException(string.Format("{0} values are provided, but {1} rows are present in the dataset.", values.Count, Rows));
|
---|
| 123 |
|
---|
[15769] | 124 | if (!IsAllowedType(values))
|
---|
| 125 | throw new ArgumentException(string.Format("Unsupported type {0} for variable {1}.", GetElementType(values), variableName));
|
---|
| 126 |
|
---|
[16188] | 127 | variableNames.Insert(position, variableName);
|
---|
[15769] | 128 | variableValues[variableName] = values;
|
---|
| 129 |
|
---|
[12489] | 130 | OnColumnsChanged();
|
---|
| 131 | OnColumnNamesChanged();
|
---|
| 132 | OnReset();
|
---|
[12032] | 133 | }
|
---|
| 134 |
|
---|
[16188] | 135 | public void ReplaceVariable(string variableName, IList values) {
|
---|
| 136 | if (!variableValues.ContainsKey(variableName))
|
---|
| 137 | throw new ArgumentException(string.Format("Variable {0} is not present in the dataset.", variableName));
|
---|
| 138 | if (values.Count != variableValues[variableName].Count)
|
---|
| 139 | throw new ArgumentException("The number of values must coincide with the number of dataset rows.");
|
---|
| 140 | if (GetVariableType(variableName) != values[0].GetType())
|
---|
| 141 | throw new ArgumentException("The type of the provided value does not match the variable type.");
|
---|
| 142 | variableValues[variableName] = values;
|
---|
| 143 | }
|
---|
| 144 |
|
---|
| 145 |
|
---|
[12032] | 146 | public void RemoveVariable(string variableName) {
|
---|
| 147 | if (!variableValues.ContainsKey(variableName))
|
---|
[15769] | 148 | throw new ArgumentException(string.Format("The variable {0} does not exist in the dataset.", variableName));
|
---|
[12032] | 149 | variableValues.Remove(variableName);
|
---|
| 150 | variableNames.Remove(variableName);
|
---|
[12489] | 151 | OnColumnsChanged();
|
---|
| 152 | OnColumnNamesChanged();
|
---|
| 153 | OnReset();
|
---|
[12032] | 154 | }
|
---|
| 155 |
|
---|
[16188] | 156 | public void ClearValues() {
|
---|
| 157 | foreach (var list in variableValues.Values) {
|
---|
| 158 | list.Clear();
|
---|
| 159 | }
|
---|
| 160 | Rows = 0;
|
---|
[12489] | 161 | OnRowsChanged();
|
---|
| 162 | OnReset();
|
---|
[11589] | 163 | }
|
---|
| 164 |
|
---|
[16188] | 165 |
|
---|
[12141] | 166 | public void SetVariableValue(object value, string variableName, int row) {
|
---|
[12032] | 167 | IList list;
|
---|
| 168 | variableValues.TryGetValue(variableName, out list);
|
---|
| 169 | if (list == null)
|
---|
| 170 | throw new ArgumentException("The variable " + variableName + " does not exist in the dataset.");
|
---|
| 171 | if (row < 0 || list.Count < row)
|
---|
| 172 | throw new ArgumentOutOfRangeException("Invalid row value");
|
---|
[12141] | 173 | if (GetVariableType(variableName) != value.GetType())
|
---|
| 174 | throw new ArgumentException("The type of the provided value does not match the variable type.");
|
---|
| 175 |
|
---|
[12032] | 176 | list[row] = value;
|
---|
[12508] | 177 | OnItemChanged(row, variableNames.IndexOf(variableName));
|
---|
[12032] | 178 | }
|
---|
| 179 |
|
---|
[12141] | 180 | bool IStringConvertibleMatrix.SetValue(string value, int rowIndex, int columnIndex) {
|
---|
| 181 | var variableName = variableNames[columnIndex];
|
---|
| 182 | // if value represents a double
|
---|
| 183 | double dv;
|
---|
| 184 | if (double.TryParse(value, out dv)) {
|
---|
| 185 | SetVariableValue(dv, variableName, rowIndex);
|
---|
| 186 | return true;
|
---|
| 187 | }
|
---|
| 188 | // if value represents a DateTime object
|
---|
| 189 | DateTime dt;
|
---|
| 190 | if (DateTime.TryParse(value, out dt)) {
|
---|
| 191 | SetVariableValue(dt, variableName, rowIndex);
|
---|
| 192 | return true;
|
---|
| 193 | }
|
---|
| 194 | // if value is simply a string
|
---|
| 195 | SetVariableValue(value, variableName, rowIndex);
|
---|
| 196 | return true;
|
---|
| 197 | }
|
---|
| 198 |
|
---|
| 199 | bool IStringConvertibleMatrix.Validate(string value, out string errorMessage) {
|
---|
[12508] | 200 | errorMessage = string.Empty;
|
---|
| 201 | return true;
|
---|
[12141] | 202 | }
|
---|
| 203 |
|
---|
[12489] | 204 | #region event handlers
|
---|
| 205 | public override event EventHandler RowsChanged;
|
---|
| 206 | private void OnRowsChanged() {
|
---|
[12508] | 207 | var handler = RowsChanged;
|
---|
| 208 | if (handler != null)
|
---|
| 209 | handler(this, EventArgs.Empty);
|
---|
[12489] | 210 | }
|
---|
| 211 |
|
---|
| 212 | public override event EventHandler ColumnsChanged;
|
---|
| 213 | private void OnColumnsChanged() {
|
---|
[12508] | 214 | var handler = ColumnsChanged;
|
---|
| 215 | if (handler != null)
|
---|
| 216 | handler(this, EventArgs.Empty);
|
---|
[12489] | 217 | }
|
---|
| 218 |
|
---|
| 219 | public override event EventHandler ColumnNamesChanged;
|
---|
| 220 | private void OnColumnNamesChanged() {
|
---|
[12508] | 221 | var handler = ColumnNamesChanged;
|
---|
| 222 | if (handler != null)
|
---|
| 223 | handler(this, EventArgs.Empty);
|
---|
[12489] | 224 | }
|
---|
| 225 |
|
---|
| 226 | public override event EventHandler Reset;
|
---|
| 227 | private void OnReset() {
|
---|
[12508] | 228 | var handler = Reset;
|
---|
| 229 | if (handler != null)
|
---|
| 230 | handler(this, EventArgs.Empty);
|
---|
[12489] | 231 | }
|
---|
| 232 |
|
---|
| 233 | public override event EventHandler<EventArgs<int, int>> ItemChanged;
|
---|
| 234 | private void OnItemChanged(int rowIndex, int columnIndex) {
|
---|
[12508] | 235 | var handler = ItemChanged;
|
---|
| 236 | if (handler != null) {
|
---|
| 237 | handler(this, new EventArgs<int, int>(rowIndex, columnIndex));
|
---|
[12489] | 238 | }
|
---|
| 239 | }
|
---|
| 240 | #endregion
|
---|
[11589] | 241 | }
|
---|
| 242 | }
|
---|