Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.CEDMA.Charting/3.3/VisualMatrix.cs @ 2387

Last change on this file since 2387 was 2325, checked in by mkommend, 15 years ago

changed SparseMatrix.Matrix and MatrixRow to a generic type (ticket #701)

File size: 5.1 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using HeuristicLab.Core;
6using HeuristicLab.SparseMatrix;
7
8namespace HeuristicLab.CEDMA.Charting {
9  public class VisualMatrix : ItemBase {
10    private Dictionary<string, Dictionary<object, double>> categoricalValueIndices;
11    private Matrix<string,object> matrix;
12
13    public VisualMatrix() {
14      this.rows = new List<VisualMatrixRow>();
15      this.categoricalVariables = new List<string>();
16      this.ordinalVariables = new List<string>();
17      this.multiDimensionalCategoricalVariables = new List<string>();
18      this.multiDimensionalOrdinalVariables = new List<string>();
19      this.categoricalValueIndices = new Dictionary<string, Dictionary<object, double>>();    }
20
21    public VisualMatrix(Matrix<string,object> matrix, IEnumerable<string> categoricalVariables, IEnumerable<string> ordinalVariables,
22      IEnumerable<string> multiDimensionalCategoricalVariables, IEnumerable<string> multiDimensionalOrdinalVariables)
23      : this() {
24      this.matrix = matrix;
25      this.matrix.Changed += new EventHandler(MatrixChanged);
26
27      foreach (MatrixRow<string,object> row in matrix.GetRows())
28        rows.Add(new VisualMatrixRow(row));
29
30      this.categoricalVariables.AddRange(categoricalVariables);
31      this.ordinalVariables.AddRange(ordinalVariables);
32      this.multiDimensionalCategoricalVariables.AddRange(multiDimensionalCategoricalVariables);
33      this.multiDimensionalOrdinalVariables.AddRange(multiDimensionalOrdinalVariables);
34    }
35
36    private List<VisualMatrixRow> rows;
37    public IEnumerable<VisualMatrixRow> Rows {
38      get {
39        return rows;
40      }
41    }
42
43    public void AddRow(VisualMatrixRow row) {
44      this.rows.Add(row);
45    }
46
47    public void RemoveRow(VisualMatrixRow row) {
48      this.rows.Remove(row);
49    }
50
51    public void Clear() {
52      this.rows.Clear();
53      this.categoricalVariables.Clear();
54      this.ordinalVariables.Clear();
55      this.multiDimensionalCategoricalVariables.Clear();
56      this.multiDimensionalOrdinalVariables.Clear();
57    }
58
59    private List<string> categoricalVariables;
60    public IEnumerable<string> CategoricalVariables {
61      get { return categoricalVariables; }
62    }
63
64    public void AddCategoricalVariable(string name) {     
65      this.categoricalVariables.Add(name);
66    }
67
68    public void RemoveCategoricalVariable(string name) {
69      this.categoricalVariables.Remove(name);
70    }
71
72    public void ClearCategoricalVariables() {
73      this.categoricalVariables.Clear();
74    }
75
76    private List<string> ordinalVariables;
77    public IEnumerable<string> OrdinalVariables {
78      get { return ordinalVariables; }
79    }
80
81    public void AddOrdinalVariable(string name) {
82      this.ordinalVariables.Add(name);
83    }
84
85    public void RemoveOrdinalVariable(string name) {
86      this.ordinalVariables.Remove(name);
87    }
88
89    public void ClearOrdinalVariables() {
90      this.ordinalVariables.Clear();
91    }
92
93    private List<string> multiDimensionalOrdinalVariables;
94    public IEnumerable<string> MultiDimensionalOrdinalVariables {
95      get { return multiDimensionalOrdinalVariables; }
96    }
97
98    public void AddMultiDimensionalOrdinalVariable(string name) {
99      this.multiDimensionalOrdinalVariables.Add(name);
100    }
101
102    public void RemoveMultiDimensionalOrdinalVariable(string name) {
103      this.multiDimensionalOrdinalVariables.Remove(name);
104    }
105
106    public void ClearMultiDimensionalOrdinalVariables() {
107      this.multiDimensionalOrdinalVariables.Clear();
108    }
109
110    private List<string> multiDimensionalCategoricalVariables;
111    public IEnumerable<string> MultiDimensionalCategoricalVariables {
112      get { return multiDimensionalCategoricalVariables; }
113    }
114
115    public void AddMultiDimensionalCategoricalVariable(string name) {
116      this.multiDimensionalCategoricalVariables.Add(name);
117    }
118
119    public void RemoveMultiDimensionalCategoricalVariable(string name) {
120      this.multiDimensionalCategoricalVariables.Remove(name);
121    }
122
123    public void ClearMultiDimensionalCategoricalVariables() {
124      this.multiDimensionalCategoricalVariables.Clear();
125    }
126
127    public double IndexOfCategoricalValue(string variable, object value) {
128      if (value == null) return double.NaN;
129      Dictionary<object, double> valueToIndexMap;
130      if (categoricalValueIndices.ContainsKey(variable)) {
131        valueToIndexMap = categoricalValueIndices[variable];
132      } else {
133        valueToIndexMap = new Dictionary<object, double>();
134        categoricalValueIndices[variable] = valueToIndexMap;
135      }
136      if (!valueToIndexMap.ContainsKey(value)) {
137        if (valueToIndexMap.Values.Count == 0) valueToIndexMap[value] = 1.0;
138        else valueToIndexMap[value] = 1.0 + valueToIndexMap.Values.Max();
139      }
140      return valueToIndexMap[value];
141    }
142
143    private void MatrixChanged(object sender, EventArgs e) {
144      this.FireChanged();
145    }
146
147    public IEnumerable<string> Attributes {
148      get { return CategoricalVariables.Concat(OrdinalVariables); }
149    }
150  }
151}
Note: See TracBrowser for help on using the repository browser.