-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathFloatTable.pde
208 lines (171 loc) · 4.8 KB
/
FloatTable.pde
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
// inspired by Ben Fry
// stores data file in a simple table of floats
// expects column and row names
// not intended to be used alone
// input from data file
class FloatTable{
// data to store
int rowCount;
int colCount;
float[][] data;
String[] rowNames;
String[] colNames;
// file delimiter
char delimiter = ',';
// create a table of float values
FloatTable(String file){
// load in data
String[] rows = loadStrings(file);
String[] cols = split(rows[0], delimiter);
// get rid of the top-left corner
colNames = subset(cols, 1);
// clean column names (first row)
removeQuotes(colNames);
// define the column count
colCount = colNames.length;
// initialize before filling row names and data
rowNames = new String[rows.length - 1];
data = new float[rows.length - 1][];
// skip column row, read in data
for(int i = 1; i < rows.length; i++){
// skip empty & comment rows
if(trim(rows[i]).length() == 0)
continue;
if(rows[i].startsWith("#"))
continue;
// split the row on the delimiter & clean it
String[] pieces = split(rows[i], delimiter);
removeQuotes(pieces);
// store the row name
rowNames[rowCount] = pieces[0];
// copy in the float data
data[rowCount] = parseFloat(subset(pieces, 1));
// increase the row count (only if row contained data)
rowCount++;
}
// resize the 'data' array in case of empty rows
data = (float[][]) subset(data, 0, rowCount);
}
// remove quotes from a string array
void removeQuotes(String[] arr){
for(int i = 0; i < arr.length; i++){
// turn " into '
arr[i] = arr[i].replaceAll("\"\"", "\"");
// only if a pair of quotes can exist in a piece of the string array
if(arr[i].length() > 2){
// remove the quotes, if they surround the piece of the string array
if(arr[i].startsWith("\"") && arr[i].endsWith("\""))
arr[i] = arr[i].substring(1, arr[i].length() - 1);
}
}
}
// how many rows in the data
int getRowCount(){
return rowCount;
}
// the name of a specific row
String getRowName(int index){
return rowNames[index];
}
// all row names
String[] getRowNames(){
return rowNames;
}
// find a row number from its name, or else -1
int getRowIndex(String name){
for(int i = 0; i < rowCount; i++){
if(rowNames[i].equals(name))
return i;
}
// no name found
return -1;
}
// how many columns in the data
int getColCount(){
return colCount;
}
// the name of a specific column
String getColName(int index){
return colNames[index];
}
// all column names
String[] getColNames(){
return colNames;
}
// return a data item at the specified location
float getFloat(int row, int col){
return data[row][col];
}
// check if specified location is valid
boolean isValid(int row, int col){
if(row < 0)
return false;
if(row >= rowCount)
return false;
if(col >= data[row].length)
return false;
if(col < 0)
return false;
return !Float.isNaN(data[row][col]);
}
// get the minimum column value
float getColMin(int col){
float min = Float.MAX_VALUE;
for(int i = 0; i < rowCount; i++)
if(!Float.isNaN(data[i][col]))
if(data[i][col] < min)
min = data[i][col];
return min;
}
// get the maximum column value
float getColMax(int col){
float max = -Float.MAX_VALUE;
for(int i = 0; i < rowCount; i++)
if(isValid(i, col))
if (data[i][col] > max)
max = data[i][col];
return max;
}
// get the minimum row value
float getRowMin(int row){
float min = Float.MAX_VALUE;
for(int i = 0; i < colCount; i++)
if(isValid(row, i))
if(data[row][i] < min)
min = data[row][i];
return min;
}
// get the maximum row value
float getRowMax(int row){
float max = -Float.MAX_VALUE;
for(int i = 1; i < colCount; i++)
if(!Float.isNaN(data[row][i]))
if(data[row][i] > max)
max = data[row][i];
return max;
}
// get the smallest value
float getTableMin(){
float min = Float.MAX_VALUE;
for(int i = 0; i < rowCount; i++)
for(int j = 0; j < colCount; j++)
if(isValid(i, j))
if(data[i][j] < min)
min = data[i][j];
return min;
}
// get the maximum value
float getTableMax(){
float max = -Float.MAX_VALUE;
for(int i = 0; i < rowCount; i++)
for(int j = 0; j < colCount; j++)
if(isValid(i, j))
if(data[i][j] > max)
max = data[i][j];
return max;
}
// output the table of floats
float[][] getData(){
return data;
}
}