forked from Kitware/VTK
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vtkPMultiCorrelativeStatistics.cxx
223 lines (183 loc) · 6.36 KB
/
vtkPMultiCorrelativeStatistics.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
/*=========================================================================
Program: Visualization Toolkit
Module: vtkPMultiCorrelativeStatistics.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.
=========================================================================*/
/*-------------------------------------------------------------------------
Copyright 2011 Sandia Corporation.
Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
the U.S. Government retains certain rights in this software.
-------------------------------------------------------------------------*/
#include "vtkPMultiCorrelativeStatistics.h"
#include "vtkAbstractArray.h"
#include "vtkCommunicator.h"
#include "vtkInformation.h"
#include "vtkInformationVector.h"
#include "vtkMultiBlockDataSet.h"
#include "vtkMultiProcessController.h"
#include "vtkNew.h"
#include "vtkObjectFactory.h"
#include "vtkPOrderStatistics.h"
#include "vtkTable.h"
#include "vtkVariant.h"
#include <map>
vtkStandardNewMacro(vtkPMultiCorrelativeStatistics);
vtkCxxSetObjectMacro(vtkPMultiCorrelativeStatistics, Controller, vtkMultiProcessController);
//------------------------------------------------------------------------------
vtkPMultiCorrelativeStatistics::vtkPMultiCorrelativeStatistics()
{
this->Controller = nullptr;
this->SetController(vtkMultiProcessController::GetGlobalController());
}
//------------------------------------------------------------------------------
vtkPMultiCorrelativeStatistics::~vtkPMultiCorrelativeStatistics()
{
this->SetController(nullptr);
}
//------------------------------------------------------------------------------
void vtkPMultiCorrelativeStatistics::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os, indent);
os << indent << "Controller: " << this->Controller << endl;
}
//------------------------------------------------------------------------------
void vtkPMultiCorrelativeStatistics::Learn(
vtkTable* inData, vtkTable* inParameters, vtkMultiBlockDataSet* outMeta)
{
if (!outMeta)
{
return;
}
// First calculate correlative statistics on local data set
this->Superclass::Learn(inData, inParameters, outMeta);
// Get a hold of the (sparse) covariance matrix
vtkTable* sparseCov = vtkTable::SafeDownCast(outMeta->GetBlock(0));
if (!sparseCov)
{
return;
}
if (!this->MedianAbsoluteDeviation)
{
vtkPMultiCorrelativeStatistics::GatherStatistics(this->Controller, sparseCov);
}
}
//------------------------------------------------------------------------------
void vtkPMultiCorrelativeStatistics::GatherStatistics(
vtkMultiProcessController* curController, vtkTable* sparseCov)
{
vtkIdType nRow = sparseCov->GetNumberOfRows();
if (nRow <= 0)
{
// No statistics were calculated.
return;
}
// Make sure that parallel updates are needed, otherwise leave it at that.
int np = curController->GetNumberOfProcesses();
if (np < 2)
{
return;
}
// Now get ready for parallel calculations
vtkCommunicator* com = curController->GetCommunicator();
if (!com)
{
vtkGenericWarningMacro("No parallel communicator.");
return;
}
// (All) gather all sample sizes
int n_l = sparseCov->GetValueByName(0, "Entries").ToInt(); // Cardinality
int* n_g = new int[np];
com->AllGather(&n_l, n_g, 1);
// Iterate over all mean and MXY entries
// NB: two passes are required as there is no guarantee that all means
// are stored before MXYs
int nM = nRow - 1;
double* M_l = new double[nM];
// First, load all means and create a name-to-index lookup table
std::map<vtkStdString, vtkIdType> meanIndex;
for (vtkIdType r = 1; r < nRow; ++r)
{
if (sparseCov->GetValueByName(r, "Column2").ToString().empty())
{
meanIndex[sparseCov->GetValueByName(r, "Column1").ToString()] = r - 1;
M_l[r - 1] = sparseCov->GetValueByName(r, "Entries").ToDouble();
}
}
vtkIdType nMeans = static_cast<vtkIdType>(meanIndex.size());
// Second, load all MXYs and create an index-to-index-pair lookup table
std::map<vtkIdType, std::pair<vtkIdType, vtkIdType>> covToMeans;
for (vtkIdType r = 1; r < nRow; ++r)
{
vtkStdString col2 = sparseCov->GetValueByName(r, "Column2").ToString();
if (!col2.empty())
{
covToMeans[r - 1] = std::pair<vtkIdType, vtkIdType>(
meanIndex[sparseCov->GetValueByName(r, "Column1").ToString()], meanIndex[col2]);
M_l[r - 1] = sparseCov->GetValueByName(r, "Entries").ToDouble();
}
}
// (All) gather all local means and MXY statistics
double* M_g = new double[nM * np];
com->AllGather(M_l, M_g, nM);
// Aggregate all local nM-tuples of M statistics into global ones
int ns = n_g[0];
for (int i = 0; i < nM; ++i)
{
M_l[i] = M_g[i];
}
for (int i = 1; i < np; ++i)
{
int ns_l = n_g[i];
int N = ns + ns_l;
int prod_ns = ns * ns_l;
double invN = 1. / static_cast<double>(N);
double* M_part = new double[nM];
double* delta = new double[nMeans];
double* delta_sur_N = new double[nMeans];
int o = nM * i;
// First, calculate deltas for all means
for (int j = 0; j < nMeans; ++j)
{
M_part[j] = M_g[o + j];
delta[j] = M_part[j] - M_l[j];
delta_sur_N[j] = delta[j] * invN;
}
// Then, update covariances
for (int j = nMeans; j < nM; ++j)
{
M_part[j] = M_g[o + j];
M_l[j] +=
M_part[j] + prod_ns * delta[covToMeans[j].first] * delta_sur_N[covToMeans[j].second];
}
// Then, update means
for (int j = 0; j < nMeans; ++j)
{
M_l[j] += ns_l * delta_sur_N[j];
}
// Last, update cardinality
ns = N;
// Clean-up
delete[] M_part;
delete[] delta;
delete[] delta_sur_N;
}
for (int i = 0; i < nM; ++i)
{
sparseCov->SetValueByName(i + 1, "Entries", M_l[i]);
}
sparseCov->SetValueByName(0, "Entries", ns);
// Clean-up
delete[] M_l;
delete[] M_g;
delete[] n_g;
}
//------------------------------------------------------------------------------
vtkOrderStatistics* vtkPMultiCorrelativeStatistics::CreateOrderStatisticsInstance()
{
return vtkPOrderStatistics::New();
}