[13368] | 1 | #region License Information
|
---|
[11589] | 2 |
|
---|
| 3 | /* HeuristicLab
|
---|
[12032] | 4 | * Copyright (C) 2002-2015 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.")]
|
---|
[14711] | 35 | [StorableType("208D817C-4800-4E59-A80F-C77C1562C404")]
|
---|
[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) {
|
---|
| 41 | var variables = variableValues.Keys.ToList();
|
---|
| 42 | foreach (var v in variables) {
|
---|
| 43 | var type = GetVariableType(v);
|
---|
| 44 | if (type == typeof(DateTime)) {
|
---|
| 45 | variableValues[v] = GetDateTimeValues(v).ToList();
|
---|
| 46 | } else if (type == typeof(double)) {
|
---|
| 47 | variableValues[v] = GetDoubleValues(v).ToList();
|
---|
[13040] | 48 | } else if (type == typeof(string)) {
|
---|
| 49 | variableValues[v] = GetStringValues(v).ToList();
|
---|
| 50 | } else {
|
---|
| 51 | throw new ArgumentException("Unsupported type " + type + " for variable " + v);
|
---|
[13027] | 52 | }
|
---|
| 53 | }
|
---|
| 54 | }
|
---|
[11589] | 55 | public override IDeepCloneable Clone(Cloner cloner) { return new ModifiableDataset(this, cloner); }
|
---|
| 56 | public ModifiableDataset() : base() { }
|
---|
[12508] | 57 |
|
---|
| 58 | public ModifiableDataset(Dataset dataset) : base(dataset) { }
|
---|
[11589] | 59 | public ModifiableDataset(IEnumerable<string> variableNames, IEnumerable<IList> variableValues) : base(variableNames, variableValues) { }
|
---|
| 60 |
|
---|
| 61 | public void ReplaceRow(int row, IEnumerable<object> values) {
|
---|
| 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 | }
|
---|
| 71 | // replace values
|
---|
| 72 | for (int i = 0; i < list.Count; ++i) {
|
---|
| 73 | variableValues[variableNames[i]][row] = list[i];
|
---|
| 74 | }
|
---|
[12489] | 75 | OnReset();
|
---|
[11589] | 76 | }
|
---|
| 77 |
|
---|
| 78 | public void AddRow(IEnumerable<object> values) {
|
---|
| 79 | var list = values.ToList();
|
---|
| 80 | if (list.Count != variableNames.Count)
|
---|
| 81 | throw new ArgumentException("The number of values must be equal to the number of variable names.");
|
---|
| 82 | // check if all the values are of the correct type
|
---|
| 83 | for (int i = 0; i < list.Count; ++i) {
|
---|
| 84 | if (list[i].GetType() != GetVariableType(variableNames[i])) {
|
---|
| 85 | throw new ArgumentException("The type of the provided value does not match the variable type.");
|
---|
| 86 | }
|
---|
| 87 | }
|
---|
| 88 | // add values
|
---|
| 89 | for (int i = 0; i < list.Count; ++i) {
|
---|
| 90 | variableValues[variableNames[i]].Add(list[i]);
|
---|
| 91 | }
|
---|
| 92 | rows++;
|
---|
[12489] | 93 | OnRowsChanged();
|
---|
| 94 | OnReset();
|
---|
[11589] | 95 | }
|
---|
| 96 |
|
---|
[12032] | 97 | // adds a new variable to the dataset
|
---|
| 98 | public void AddVariable<T>(string variableName, IEnumerable<T> values) {
|
---|
| 99 | if (variableValues.ContainsKey(variableName))
|
---|
| 100 | throw new ArgumentException("Variable " + variableName + " is already present in the dataset.");
|
---|
| 101 | int count = values.Count();
|
---|
| 102 | if (count != rows)
|
---|
| 103 | throw new ArgumentException("The number of values must exactly match the number of rows in the dataset.");
|
---|
| 104 | variableValues[variableName] = new List<T>(values);
|
---|
| 105 | variableNames.Add(variableName);
|
---|
[12489] | 106 | OnColumnsChanged();
|
---|
| 107 | OnColumnNamesChanged();
|
---|
| 108 | OnReset();
|
---|
[12032] | 109 | }
|
---|
| 110 |
|
---|
| 111 | public void RemoveVariable(string variableName) {
|
---|
| 112 | if (!variableValues.ContainsKey(variableName))
|
---|
| 113 | throw new ArgumentException("The variable " + variableName + " does not exist in the dataset.");
|
---|
| 114 | variableValues.Remove(variableName);
|
---|
| 115 | variableNames.Remove(variableName);
|
---|
[12489] | 116 | OnColumnsChanged();
|
---|
| 117 | OnColumnNamesChanged();
|
---|
| 118 | OnReset();
|
---|
[12032] | 119 | }
|
---|
| 120 |
|
---|
[11589] | 121 | // slow, avoid to use this
|
---|
| 122 | public void RemoveRow(int row) {
|
---|
| 123 | foreach (var list in variableValues.Values)
|
---|
| 124 | list.RemoveAt(row);
|
---|
| 125 | rows--;
|
---|
[12489] | 126 | OnRowsChanged();
|
---|
| 127 | OnReset();
|
---|
[11589] | 128 | }
|
---|
| 129 |
|
---|
[12141] | 130 | public void SetVariableValue(object value, string variableName, int row) {
|
---|
[12032] | 131 | IList list;
|
---|
| 132 | variableValues.TryGetValue(variableName, out list);
|
---|
| 133 | if (list == null)
|
---|
| 134 | throw new ArgumentException("The variable " + variableName + " does not exist in the dataset.");
|
---|
| 135 | if (row < 0 || list.Count < row)
|
---|
| 136 | throw new ArgumentOutOfRangeException("Invalid row value");
|
---|
[12141] | 137 | if (GetVariableType(variableName) != value.GetType())
|
---|
| 138 | throw new ArgumentException("The type of the provided value does not match the variable type.");
|
---|
| 139 |
|
---|
[12032] | 140 | list[row] = value;
|
---|
[12508] | 141 | OnItemChanged(row, variableNames.IndexOf(variableName));
|
---|
[12032] | 142 | }
|
---|
| 143 |
|
---|
[11589] | 144 | private Type GetVariableType(string variableName) {
|
---|
| 145 | IList list;
|
---|
| 146 | variableValues.TryGetValue(variableName, out list);
|
---|
| 147 | if (list == null)
|
---|
| 148 | throw new ArgumentException("The variable " + variableName + " does not exist in the dataset.");
|
---|
[12190] | 149 | return list.GetType().GetGenericArguments()[0];
|
---|
[11589] | 150 | }
|
---|
[12141] | 151 |
|
---|
| 152 | bool IStringConvertibleMatrix.SetValue(string value, int rowIndex, int columnIndex) {
|
---|
| 153 | var variableName = variableNames[columnIndex];
|
---|
| 154 | // if value represents a double
|
---|
| 155 | double dv;
|
---|
| 156 | if (double.TryParse(value, out dv)) {
|
---|
| 157 | SetVariableValue(dv, variableName, rowIndex);
|
---|
| 158 | return true;
|
---|
| 159 | }
|
---|
| 160 | // if value represents a DateTime object
|
---|
| 161 | DateTime dt;
|
---|
| 162 | if (DateTime.TryParse(value, out dt)) {
|
---|
| 163 | SetVariableValue(dt, variableName, rowIndex);
|
---|
| 164 | return true;
|
---|
| 165 | }
|
---|
| 166 | // if value is simply a string
|
---|
| 167 | SetVariableValue(value, variableName, rowIndex);
|
---|
| 168 | return true;
|
---|
| 169 | }
|
---|
| 170 |
|
---|
| 171 | bool IStringConvertibleMatrix.Validate(string value, out string errorMessage) {
|
---|
[12508] | 172 | errorMessage = string.Empty;
|
---|
| 173 | return true;
|
---|
[12141] | 174 | }
|
---|
| 175 |
|
---|
[12489] | 176 | #region event handlers
|
---|
| 177 | public override event EventHandler RowsChanged;
|
---|
| 178 | private void OnRowsChanged() {
|
---|
[12508] | 179 | var handler = RowsChanged;
|
---|
| 180 | if (handler != null)
|
---|
| 181 | handler(this, EventArgs.Empty);
|
---|
[12489] | 182 | }
|
---|
| 183 |
|
---|
| 184 | public override event EventHandler ColumnsChanged;
|
---|
| 185 | private void OnColumnsChanged() {
|
---|
[12508] | 186 | var handler = ColumnsChanged;
|
---|
| 187 | if (handler != null)
|
---|
| 188 | handler(this, EventArgs.Empty);
|
---|
[12489] | 189 | }
|
---|
| 190 |
|
---|
| 191 | public override event EventHandler ColumnNamesChanged;
|
---|
| 192 | private void OnColumnNamesChanged() {
|
---|
[12508] | 193 | var handler = ColumnNamesChanged;
|
---|
| 194 | if (handler != null)
|
---|
| 195 | handler(this, EventArgs.Empty);
|
---|
[12489] | 196 | }
|
---|
| 197 |
|
---|
| 198 | public override event EventHandler Reset;
|
---|
| 199 | private void OnReset() {
|
---|
[12508] | 200 | var handler = Reset;
|
---|
| 201 | if (handler != null)
|
---|
| 202 | handler(this, EventArgs.Empty);
|
---|
[12489] | 203 | }
|
---|
| 204 |
|
---|
| 205 | public override event EventHandler<EventArgs<int, int>> ItemChanged;
|
---|
| 206 | private void OnItemChanged(int rowIndex, int columnIndex) {
|
---|
[12508] | 207 | var handler = ItemChanged;
|
---|
| 208 | if (handler != null) {
|
---|
| 209 | handler(this, new EventArgs<int, int>(rowIndex, columnIndex));
|
---|
[12489] | 210 | }
|
---|
| 211 | }
|
---|
| 212 | #endregion
|
---|
[11589] | 213 | }
|
---|
| 214 | }
|
---|