1 | #region License Information
|
---|
2 |
|
---|
3 | /* HeuristicLab
|
---|
4 | * Copyright (C) 2002-2019 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
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;
|
---|
30 | using HeuristicLab.Data;
|
---|
31 | using HEAL.Fossil;
|
---|
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 | [StorableType("4B9DA9DD-10C4-4609-8F87-B35ECD7A7487")]
|
---|
36 | public sealed class ModifiableDataset : Dataset, IStringConvertibleMatrix {
|
---|
37 | [StorableConstructor]
|
---|
38 | private ModifiableDataset(StorableConstructorFlag _) : base(_) { }
|
---|
39 |
|
---|
40 | private ModifiableDataset(ModifiableDataset original, Cloner cloner) : base(original, cloner) {
|
---|
41 | variableNames = new List<string>(original.variableNames);
|
---|
42 | variableValues = CloneValues(original.variableValues);
|
---|
43 | }
|
---|
44 |
|
---|
45 | public override IDeepCloneable Clone(Cloner cloner) { return new ModifiableDataset(this, cloner); }
|
---|
46 |
|
---|
47 | public ModifiableDataset() { }
|
---|
48 |
|
---|
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 | 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) {
|
---|
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 | }
|
---|
74 | // add values
|
---|
75 | for (int i = 0; i < list.Count; ++i) {
|
---|
76 | variableValues[variableNames[i]].Add(list[i]);
|
---|
77 | }
|
---|
78 | Rows++;
|
---|
79 | OnRowsChanged();
|
---|
80 | OnReset();
|
---|
81 | }
|
---|
82 |
|
---|
83 | public void ReplaceRow(int row, IEnumerable<object> values) {
|
---|
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 | }
|
---|
93 | // replace values
|
---|
94 | for (int i = 0; i < list.Count; ++i) {
|
---|
95 | variableValues[variableNames[i]][row] = list[i];
|
---|
96 | }
|
---|
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--;
|
---|
105 | OnRowsChanged();
|
---|
106 | OnReset();
|
---|
107 | }
|
---|
108 |
|
---|
109 | // adds a new variable to the dataset
|
---|
110 | public void AddVariable(string variableName, IList values) {
|
---|
111 | InsertVariable(variableName, Columns, values);
|
---|
112 | }
|
---|
113 |
|
---|
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 |
|
---|
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.");
|
---|
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 |
|
---|
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 |
|
---|
149 | public void RemoveVariable(string variableName) {
|
---|
150 | if (!variableValues.ContainsKey(variableName))
|
---|
151 | throw new ArgumentException(string.Format("The variable {0} does not exist in the dataset.", variableName));
|
---|
152 | variableValues.Remove(variableName);
|
---|
153 | variableNames.Remove(variableName);
|
---|
154 | OnColumnsChanged();
|
---|
155 | OnColumnNamesChanged();
|
---|
156 | OnReset();
|
---|
157 | }
|
---|
158 |
|
---|
159 | public void ClearValues() {
|
---|
160 | foreach (var list in variableValues.Values) {
|
---|
161 | list.Clear();
|
---|
162 | }
|
---|
163 | Rows = 0;
|
---|
164 | OnRowsChanged();
|
---|
165 | OnReset();
|
---|
166 | }
|
---|
167 |
|
---|
168 |
|
---|
169 | public void SetVariableValue(object value, string variableName, int row) {
|
---|
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");
|
---|
176 | if (GetVariableType(variableName) != value.GetType())
|
---|
177 | throw new ArgumentException("The type of the provided value does not match the variable type.");
|
---|
178 |
|
---|
179 | list[row] = value;
|
---|
180 | OnItemChanged(row, variableNames.IndexOf(variableName));
|
---|
181 | }
|
---|
182 |
|
---|
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) {
|
---|
203 | errorMessage = string.Empty;
|
---|
204 | return true;
|
---|
205 | }
|
---|
206 |
|
---|
207 | #region event handlers
|
---|
208 | public override event EventHandler RowsChanged;
|
---|
209 | private void OnRowsChanged() {
|
---|
210 | var handler = RowsChanged;
|
---|
211 | if (handler != null)
|
---|
212 | handler(this, EventArgs.Empty);
|
---|
213 | }
|
---|
214 |
|
---|
215 | public override event EventHandler ColumnsChanged;
|
---|
216 | private void OnColumnsChanged() {
|
---|
217 | var handler = ColumnsChanged;
|
---|
218 | if (handler != null)
|
---|
219 | handler(this, EventArgs.Empty);
|
---|
220 | }
|
---|
221 |
|
---|
222 | public override event EventHandler ColumnNamesChanged;
|
---|
223 | private void OnColumnNamesChanged() {
|
---|
224 | var handler = ColumnNamesChanged;
|
---|
225 | if (handler != null)
|
---|
226 | handler(this, EventArgs.Empty);
|
---|
227 | }
|
---|
228 |
|
---|
229 | public override event EventHandler Reset;
|
---|
230 | private void OnReset() {
|
---|
231 | var handler = Reset;
|
---|
232 | if (handler != null)
|
---|
233 | handler(this, EventArgs.Empty);
|
---|
234 | }
|
---|
235 |
|
---|
236 | public override event EventHandler<EventArgs<int, int>> ItemChanged;
|
---|
237 | private void OnItemChanged(int rowIndex, int columnIndex) {
|
---|
238 | var handler = ItemChanged;
|
---|
239 | if (handler != null) {
|
---|
240 | handler(this, new EventArgs<int, int>(rowIndex, columnIndex));
|
---|
241 | }
|
---|
242 | }
|
---|
243 | #endregion
|
---|
244 | }
|
---|
245 | }
|
---|