[11589] | 1 | #region License Information
|
---|
| 2 |
|
---|
| 3 | /* HeuristicLab
|
---|
[17097] | 4 | * Copyright (C) 2002-2019 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;
|
---|
[17097] | 31 | using HEAL.Attic;
|
---|
[11589] | 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.")]
|
---|
[17097] | 35 | [StorableType("4B9DA9DD-10C4-4609-8F87-B35ECD7A7487")]
|
---|
[12511] | 36 | public sealed class ModifiableDataset : Dataset, IStringConvertibleMatrix {
|
---|
[11589] | 37 | [StorableConstructor]
|
---|
[17097] | 38 | private ModifiableDataset(StorableConstructorFlag _) : base(_) { }
|
---|
[13179] | 39 |
|
---|
| 40 | private ModifiableDataset(ModifiableDataset original, Cloner cloner) : base(original, cloner) {
|
---|
[16124] | 41 | variableNames = new List<string>(original.variableNames);
|
---|
| 42 | variableValues = CloneValues(original.variableValues);
|
---|
[13179] | 43 | }
|
---|
[16124] | 44 |
|
---|
[11589] | 45 | public override IDeepCloneable Clone(Cloner cloner) { return new ModifiableDataset(this, cloner); }
|
---|
[12508] | 46 |
|
---|
[16124] | 47 | public ModifiableDataset() { }
|
---|
[11589] | 48 |
|
---|
[16125] | 49 | public ModifiableDataset(IEnumerable<string> variableNames, IEnumerable<IList> variableValues, bool cloneValues = false) :
|
---|
| 50 | base(variableNames, variableValues, cloneValues) { }
|
---|
[16124] | 51 |
|
---|
[16125] | 52 | public Dataset ToDataset() {
|
---|
| 53 | return new Dataset(variableNames, variableNames.Select(v => variableValues[v]));
|
---|
| 54 | }
|
---|
[16247] | 55 |
|
---|
| 56 |
|
---|
| 57 | public IEnumerable<object> GetRow(int row) {
|
---|
| 58 | if (row < 0 || row >= Rows)
|
---|
| 59 | throw new ArgumentException(string.Format("Invalid row {0} specified. The dataset contains {1} row(s).", row, Rows));
|
---|
| 60 |
|
---|
| 61 | return variableValues.Select(x => x.Value[row]);
|
---|
| 62 | }
|
---|
| 63 |
|
---|
| 64 | public void AddRow(IEnumerable<object> values) {
|
---|
[11589] | 65 | var list = values.ToList();
|
---|
| 66 | if (list.Count != variableNames.Count)
|
---|
| 67 | throw new ArgumentException("The number of values must be equal to the number of variable names.");
|
---|
| 68 | // check if all the values are of the correct type
|
---|
| 69 | for (int i = 0; i < list.Count; ++i) {
|
---|
| 70 | if (list[i].GetType() != GetVariableType(variableNames[i])) {
|
---|
| 71 | throw new ArgumentException("The type of the provided value does not match the variable type.");
|
---|
| 72 | }
|
---|
| 73 | }
|
---|
[16247] | 74 | // add values
|
---|
[11589] | 75 | for (int i = 0; i < list.Count; ++i) {
|
---|
[16247] | 76 | variableValues[variableNames[i]].Add(list[i]);
|
---|
[11589] | 77 | }
|
---|
[16247] | 78 | Rows++;
|
---|
| 79 | OnRowsChanged();
|
---|
[12489] | 80 | OnReset();
|
---|
[11589] | 81 | }
|
---|
| 82 |
|
---|
[16247] | 83 | public void ReplaceRow(int row, IEnumerable<object> values) {
|
---|
[11589] | 84 | var list = values.ToList();
|
---|
| 85 | if (list.Count != variableNames.Count)
|
---|
| 86 | throw new ArgumentException("The number of values must be equal to the number of variable names.");
|
---|
| 87 | // check if all the values are of the correct type
|
---|
| 88 | for (int i = 0; i < list.Count; ++i) {
|
---|
| 89 | if (list[i].GetType() != GetVariableType(variableNames[i])) {
|
---|
| 90 | throw new ArgumentException("The type of the provided value does not match the variable type.");
|
---|
| 91 | }
|
---|
| 92 | }
|
---|
[16247] | 93 | // replace values
|
---|
[11589] | 94 | for (int i = 0; i < list.Count; ++i) {
|
---|
[16247] | 95 | variableValues[variableNames[i]][row] = list[i];
|
---|
[11589] | 96 | }
|
---|
[16247] | 97 | OnReset();
|
---|
| 98 | }
|
---|
| 99 |
|
---|
| 100 | // slow, avoid using this
|
---|
| 101 | public void RemoveRow(int row) {
|
---|
| 102 | foreach (var list in variableValues.Values)
|
---|
| 103 | list.RemoveAt(row);
|
---|
| 104 | Rows--;
|
---|
[12489] | 105 | OnRowsChanged();
|
---|
| 106 | OnReset();
|
---|
[11589] | 107 | }
|
---|
| 108 |
|
---|
[12032] | 109 | // adds a new variable to the dataset
|
---|
[16124] | 110 | public void AddVariable(string variableName, IList values) {
|
---|
[16247] | 111 | InsertVariable(variableName, Columns, values);
|
---|
[12032] | 112 | }
|
---|
| 113 |
|
---|
[16125] | 114 | public void InsertVariable(string variableName, int position, IList values) {
|
---|
| 115 | if (variableValues.ContainsKey(variableName))
|
---|
| 116 | throw new ArgumentException(string.Format("Variable {0} is already present in the dataset.", variableName));
|
---|
| 117 |
|
---|
| 118 | if (position < 0 || position > Columns)
|
---|
| 119 | throw new ArgumentException(string.Format("Incorrect position {0} specified. The position must be between 0 and {1}.", position, Columns));
|
---|
| 120 |
|
---|
[16247] | 121 | if (values == null)
|
---|
| 122 | throw new ArgumentNullException("values", "Values must not be null. At least an empty list of values has to be provided.");
|
---|
[16125] | 123 |
|
---|
| 124 | if (values.Count != Rows)
|
---|
| 125 | throw new ArgumentException(string.Format("{0} values are provided, but {1} rows are present in the dataset.", values.Count, Rows));
|
---|
| 126 |
|
---|
| 127 | if (!IsAllowedType(values))
|
---|
| 128 | throw new ArgumentException(string.Format("Unsupported type {0} for variable {1}.", GetElementType(values), variableName));
|
---|
| 129 |
|
---|
| 130 | variableNames.Insert(position, variableName);
|
---|
| 131 | variableValues[variableName] = values;
|
---|
| 132 |
|
---|
| 133 | OnColumnsChanged();
|
---|
| 134 | OnColumnNamesChanged();
|
---|
| 135 | OnReset();
|
---|
| 136 | }
|
---|
| 137 |
|
---|
[16247] | 138 | public void ReplaceVariable(string variableName, IList values) {
|
---|
| 139 | if (!variableValues.ContainsKey(variableName))
|
---|
| 140 | throw new ArgumentException(string.Format("Variable {0} is not present in the dataset.", variableName));
|
---|
| 141 | if (values.Count != variableValues[variableName].Count)
|
---|
| 142 | throw new ArgumentException("The number of values must coincide with the number of dataset rows.");
|
---|
| 143 | if (GetVariableType(variableName) != values[0].GetType())
|
---|
| 144 | throw new ArgumentException("The type of the provided value does not match the variable type.");
|
---|
| 145 | variableValues[variableName] = values;
|
---|
| 146 | }
|
---|
| 147 |
|
---|
| 148 |
|
---|
[12032] | 149 | public void RemoveVariable(string variableName) {
|
---|
| 150 | if (!variableValues.ContainsKey(variableName))
|
---|
[16124] | 151 | throw new ArgumentException(string.Format("The variable {0} does not exist in the dataset.", variableName));
|
---|
[12032] | 152 | variableValues.Remove(variableName);
|
---|
| 153 | variableNames.Remove(variableName);
|
---|
[12489] | 154 | OnColumnsChanged();
|
---|
| 155 | OnColumnNamesChanged();
|
---|
| 156 | OnReset();
|
---|
[12032] | 157 | }
|
---|
| 158 |
|
---|
[16247] | 159 | public void ClearValues() {
|
---|
| 160 | foreach (var list in variableValues.Values) {
|
---|
| 161 | list.Clear();
|
---|
| 162 | }
|
---|
| 163 | Rows = 0;
|
---|
[12489] | 164 | OnRowsChanged();
|
---|
| 165 | OnReset();
|
---|
[11589] | 166 | }
|
---|
| 167 |
|
---|
[16247] | 168 |
|
---|
[12141] | 169 | public void SetVariableValue(object value, string variableName, int row) {
|
---|
[12032] | 170 | IList list;
|
---|
| 171 | variableValues.TryGetValue(variableName, out list);
|
---|
| 172 | if (list == null)
|
---|
| 173 | throw new ArgumentException("The variable " + variableName + " does not exist in the dataset.");
|
---|
| 174 | if (row < 0 || list.Count < row)
|
---|
| 175 | throw new ArgumentOutOfRangeException("Invalid row value");
|
---|
[12141] | 176 | if (GetVariableType(variableName) != value.GetType())
|
---|
| 177 | throw new ArgumentException("The type of the provided value does not match the variable type.");
|
---|
| 178 |
|
---|
[12032] | 179 | list[row] = value;
|
---|
[12508] | 180 | OnItemChanged(row, variableNames.IndexOf(variableName));
|
---|
[12032] | 181 | }
|
---|
| 182 |
|
---|
[12141] | 183 | bool IStringConvertibleMatrix.SetValue(string value, int rowIndex, int columnIndex) {
|
---|
| 184 | var variableName = variableNames[columnIndex];
|
---|
| 185 | // if value represents a double
|
---|
| 186 | double dv;
|
---|
| 187 | if (double.TryParse(value, out dv)) {
|
---|
| 188 | SetVariableValue(dv, variableName, rowIndex);
|
---|
| 189 | return true;
|
---|
| 190 | }
|
---|
| 191 | // if value represents a DateTime object
|
---|
| 192 | DateTime dt;
|
---|
| 193 | if (DateTime.TryParse(value, out dt)) {
|
---|
| 194 | SetVariableValue(dt, variableName, rowIndex);
|
---|
| 195 | return true;
|
---|
| 196 | }
|
---|
| 197 | // if value is simply a string
|
---|
| 198 | SetVariableValue(value, variableName, rowIndex);
|
---|
| 199 | return true;
|
---|
| 200 | }
|
---|
| 201 |
|
---|
| 202 | bool IStringConvertibleMatrix.Validate(string value, out string errorMessage) {
|
---|
[12508] | 203 | errorMessage = string.Empty;
|
---|
| 204 | return true;
|
---|
[12141] | 205 | }
|
---|
| 206 |
|
---|
[12489] | 207 | #region event handlers
|
---|
| 208 | public override event EventHandler RowsChanged;
|
---|
| 209 | private void OnRowsChanged() {
|
---|
[12508] | 210 | var handler = RowsChanged;
|
---|
| 211 | if (handler != null)
|
---|
| 212 | handler(this, EventArgs.Empty);
|
---|
[12489] | 213 | }
|
---|
| 214 |
|
---|
| 215 | public override event EventHandler ColumnsChanged;
|
---|
| 216 | private void OnColumnsChanged() {
|
---|
[12508] | 217 | var handler = ColumnsChanged;
|
---|
| 218 | if (handler != null)
|
---|
| 219 | handler(this, EventArgs.Empty);
|
---|
[12489] | 220 | }
|
---|
| 221 |
|
---|
| 222 | public override event EventHandler ColumnNamesChanged;
|
---|
| 223 | private void OnColumnNamesChanged() {
|
---|
[12508] | 224 | var handler = ColumnNamesChanged;
|
---|
| 225 | if (handler != null)
|
---|
| 226 | handler(this, EventArgs.Empty);
|
---|
[12489] | 227 | }
|
---|
| 228 |
|
---|
| 229 | public override event EventHandler Reset;
|
---|
| 230 | private void OnReset() {
|
---|
[12508] | 231 | var handler = Reset;
|
---|
| 232 | if (handler != null)
|
---|
| 233 | handler(this, EventArgs.Empty);
|
---|
[12489] | 234 | }
|
---|
| 235 |
|
---|
| 236 | public override event EventHandler<EventArgs<int, int>> ItemChanged;
|
---|
| 237 | private void OnItemChanged(int rowIndex, int columnIndex) {
|
---|
[12508] | 238 | var handler = ItemChanged;
|
---|
| 239 | if (handler != null) {
|
---|
| 240 | handler(this, new EventArgs<int, int>(rowIndex, columnIndex));
|
---|
[12489] | 241 | }
|
---|
| 242 | }
|
---|
| 243 | #endregion
|
---|
[11589] | 244 | }
|
---|
| 245 | }
|
---|