forked from Kitware/VTK
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vtkReduceTable.cxx
334 lines (294 loc) · 9.91 KB
/
vtkReduceTable.cxx
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
/*=========================================================================
Program: Visualization Toolkit
Module: vtkReduceTable.cxx
Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
All rights reserved.
See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notice for more information.
=========================================================================*/
#include "vtkReduceTable.h"
#include "vtkAbstractArray.h"
#include "vtkInformation.h"
#include "vtkInformationVector.h"
#include "vtkObjectFactory.h"
#include "vtkStreamingDemandDrivenPipeline.h"
#include "vtkTable.h"
#include <algorithm>
vtkStandardNewMacro(vtkReduceTable);
//---------------------------------------------------------------------------
vtkReduceTable::vtkReduceTable()
{
this->SetNumberOfInputPorts(1);
this->SetNumberOfOutputPorts(1);
this->IndexColumn = -1;
this->NumericalReductionMethod = vtkReduceTable::MEAN;
this->NonNumericalReductionMethod = vtkReduceTable::MODE;
}
//---------------------------------------------------------------------------
vtkReduceTable::~vtkReduceTable()
{
}
//---------------------------------------------------------------------------
int vtkReduceTable::RequestData(
vtkInformation*,
vtkInformationVector** inputVector,
vtkInformationVector* outputVector)
{
if (this->IndexColumn == -1)
{
vtkWarningMacro(<< "Index column not set");
return 1;
}
// Get input table
vtkInformation* inputInfo = inputVector[0]->GetInformationObject(0);
vtkTable* input = vtkTable::SafeDownCast(
inputInfo->Get(vtkDataObject::DATA_OBJECT()));
if (this->IndexColumn < 0 ||
this->IndexColumn > input->GetNumberOfColumns() - 1)
{
vtkWarningMacro(<< "Index column exceeds bounds of input table");
return 1;
}
// Get output table
vtkInformation* outInfo = outputVector->GetInformationObject(0);
vtkTable* output = vtkTable::SafeDownCast(
outInfo->Get(vtkDataObject::DATA_OBJECT()));
this->InitializeOutputTable(input, output);
this->AccumulateIndexValues(input);
// set the number of rows in the output table
output->SetNumberOfRows(static_cast<vtkIdType>(this->IndexValues.size()));
this->PopulateIndexColumn(output);
// populate the data columns of the output table
for (vtkIdType col = 0; col < output->GetNumberOfColumns(); ++col)
{
if (col == this->IndexColumn)
{
continue;
}
this->PopulateDataColumn(input, output, col);
}
// Clean up pipeline information
int piece = -1;
int npieces = -1;
if (outInfo->Has(
vtkStreamingDemandDrivenPipeline::UPDATE_PIECE_NUMBER()))
{
piece = outInfo->Get(
vtkStreamingDemandDrivenPipeline::UPDATE_PIECE_NUMBER());
npieces = outInfo->Get(
vtkStreamingDemandDrivenPipeline::UPDATE_NUMBER_OF_PIECES());
}
output->GetInformation()->Set(vtkDataObject::DATA_NUMBER_OF_PIECES(), npieces);
output->GetInformation()->Set(vtkDataObject::DATA_PIECE_NUMBER(), piece);
return 1;
}
//---------------------------------------------------------------------------
void vtkReduceTable::InitializeOutputTable(vtkTable *input, vtkTable *output)
{
output->DeepCopy(input);
for (vtkIdType row = output->GetNumberOfRows() - 1; row > -1; --row)
{
output->RemoveRow(row);
}
}
//---------------------------------------------------------------------------
void vtkReduceTable::AccumulateIndexValues(vtkTable *input)
{
for (vtkIdType row = 0; row < input->GetNumberOfRows(); ++row)
{
vtkVariant value = input->GetValue(row, this->IndexColumn);
this->IndexValues.insert(value);
std::map<vtkVariant, std::vector<vtkIdType> >::iterator itr =
this->NewRowToOldRowsMap.find(value);
if (itr == this->NewRowToOldRowsMap.end())
{
std::vector<vtkIdType> v;
v.push_back(row);
this->NewRowToOldRowsMap[value] = v;
}
else
{
itr->second.push_back(row);
}
}
}
//---------------------------------------------------------------------------
void vtkReduceTable::PopulateIndexColumn(vtkTable *output)
{
vtkIdType row = 0;
for (std::set<vtkVariant>::iterator itr = this->IndexValues.begin();
itr != this->IndexValues.end(); ++itr)
{
output->SetValue(row, this->IndexColumn, *itr);
++row;
}
}
//---------------------------------------------------------------------------
void vtkReduceTable::PopulateDataColumn(vtkTable *input, vtkTable *output,
vtkIdType col)
{
int reductionMethod = 0;
// check if this column has a reduction method
int columnSpecificMethod = this->GetReductionMethodForColumn(col);
if (columnSpecificMethod != -1)
{
reductionMethod = columnSpecificMethod;
}
else
{
// determine whether this column contains numerical or not.
if (input->GetValue(0, col).IsNumeric())
{
reductionMethod = this->NumericalReductionMethod;
}
else
{
reductionMethod = this->NonNumericalReductionMethod;
}
}
for (vtkIdType row = 0; row < output->GetNumberOfRows(); ++row)
{
// look up the cells in the input table that should be represented by
// this cell in the output table
vtkVariant indexValue = output->GetValue(row, this->IndexColumn);
std::vector<vtkIdType> oldRows = this->NewRowToOldRowsMap[indexValue];
// special case: one-to-one mapping between input table and output table
// (no collapse necessary)
if (oldRows.size() == 1)
{
output->SetValue(row, col,
input->GetValue(this->NewRowToOldRowsMap[indexValue].at(0), col));
continue;
}
// otherwise, combine them appropriately & store the value in the
// output table
switch (reductionMethod)
{
case vtkReduceTable::MODE:
this->ReduceValuesToMode(input, output, row, col, oldRows);
break;
case vtkReduceTable::MEDIAN:
this->ReduceValuesToMedian(input, output, row, col, oldRows);
break;
case vtkReduceTable::MEAN:
default:
this->ReduceValuesToMean(input, output, row, col, oldRows);
break;
}
}
}
//---------------------------------------------------------------------------
void vtkReduceTable::ReduceValuesToMean(vtkTable *input, vtkTable *output,
vtkIdType row, vtkIdType col,
std::vector<vtkIdType> oldRows)
{
if (!input->GetValue(0, col).IsNumeric())
{
vtkErrorMacro(<< "Mean is unsupported for non-numerical data");
return;
}
double mean = 0.0;
for (std::vector<vtkIdType>::iterator itr = oldRows.begin();
itr != oldRows.end(); ++itr)
{
mean += input->GetValue(*itr, col).ToDouble();
}
mean /= oldRows.size();
output->SetValue(row, col, vtkVariant(mean));
}
//---------------------------------------------------------------------------
void vtkReduceTable::ReduceValuesToMedian(vtkTable *input, vtkTable *output,
vtkIdType row, vtkIdType col,
std::vector<vtkIdType> oldRows)
{
if (!input->GetValue(0, col).IsNumeric())
{
vtkErrorMacro(<< "Median is unsupported for non-numerical data");
return;
}
// generate a vector of values
std::vector<double> values;
for (std::vector<vtkIdType>::iterator itr = oldRows.begin();
itr != oldRows.end(); ++itr)
{
values.push_back(input->GetValue(*itr, col).ToDouble());
}
// sort it
std::sort(values.begin(), values.end());
// get the median and store it in the output table
if (values.size() % 2 == 1)
{
output->SetValue(row, col,
vtkVariant( values.at( (values.size() - 1) / 2 ) )
);
}
else
{
double d1 = values.at( (values.size() - 1) / 2 );
double d2 = values.at( values.size() / 2 );
output->SetValue(row, col, vtkVariant( (d1 + d2) / 2.0 ));
}
}
//---------------------------------------------------------------------------
void vtkReduceTable::ReduceValuesToMode(vtkTable *input, vtkTable *output,
vtkIdType row, vtkIdType col,
std::vector<vtkIdType> oldRows)
{
// setup a map to determine how frequently each value appears
std::map<vtkVariant, int> modeMap;
std::map<vtkVariant, int>::iterator mapItr;
for (std::vector<vtkIdType>::iterator vectorItr = oldRows.begin();
vectorItr != oldRows.end(); ++vectorItr)
{
vtkVariant v = input->GetValue(*vectorItr, col);
mapItr = modeMap.find(v);
if (mapItr == modeMap.end())
{
modeMap[v] = 1;
}
else
{
mapItr->second += 1;
}
}
// use our map to find the mode & store it in the output table
int maxCount = -1;
vtkVariant mode;
for (mapItr = modeMap.begin(); mapItr != modeMap.end(); ++mapItr)
{
if (mapItr->second > maxCount)
{
mode = mapItr->first;
maxCount = mapItr->second;
}
}
output->SetValue(row, col, mode);
}
//---------------------------------------------------------------------------
int vtkReduceTable::GetReductionMethodForColumn(vtkIdType col)
{
std::map<vtkIdType, int>::iterator itr =
this->ColumnReductionMethods.find(col);
if (itr != this->ColumnReductionMethods.end())
{
return itr->second;
}
return -1;
}
//---------------------------------------------------------------------------
void vtkReduceTable::SetReductionMethodForColumn(vtkIdType col, int method)
{
this->ColumnReductionMethods[col] = method;
}
//---------------------------------------------------------------------------
void vtkReduceTable::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os, indent);
os << indent << "IndexColumn: " << this->IndexColumn << endl;
os << indent << "NumericalReductionMethod: "
<< this->NumericalReductionMethod << endl;
os << indent << "NonNumericalReductionMethod: "
<< this->NonNumericalReductionMethod << endl;
}