forked from Kitware/VTK
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vtkPCAStatistics.cxx
1084 lines (973 loc) · 32.9 KB
/
vtkPCAStatistics.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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "vtkPCAStatistics.h"
#include "vtkDoubleArray.h"
#include "vtkIdTypeArray.h"
#include "vtkInformation.h"
#include "vtkMultiBlockDataSet.h"
#include "vtkMultiCorrelativeStatisticsAssessFunctor.h"
#include "vtkObjectFactory.h"
#include "vtkSmartPointer.h"
#include "vtkStringArray.h"
#include "vtkTable.h"
#include "vtkVariantArray.h"
#include <map>
#include <vector>
#include <sstream>
#include "alglib/svd.h"
// To Do:
// - Add option to pre-multiply EigenVectors by normalization coeffs
// - In vtkPCAAssessFunctor, pre-multiply EigenVectors by normalization coeffs (if req)
// -
#define VTK_PCA_NORMCOLUMN "PCA Cov Norm"
#define VTK_PCA_COMPCOLUMN "PCA"
vtkObjectFactoryNewMacro(vtkPCAStatistics)
const char* vtkPCAStatistics::NormalizationSchemeEnumNames[NUM_NORMALIZATION_SCHEMES + 1] =
{
"None",
"TriangleSpecified",
"DiagonalSpecified",
"DiagonalVariance",
"InvalidNormalizationScheme"
};
const char* vtkPCAStatistics::BasisSchemeEnumNames[NUM_BASIS_SCHEMES + 1] =
{
"FullBasis",
"FixedBasisSize",
"FixedBasisEnergy",
"InvalidBasisScheme"
};
// ----------------------------------------------------------------------
void vtkPCAStatistics::GetEigenvalues(int request, vtkDoubleArray* eigenvalues)
{
vtkSmartPointer<vtkMultiBlockDataSet> outputMetaDS =
vtkMultiBlockDataSet::SafeDownCast(
this->GetOutputDataObject( vtkStatisticsAlgorithm::OUTPUT_MODEL ) );
if(!outputMetaDS)
{
vtkErrorMacro(<<"NULL dataset pointer!");
}
vtkSmartPointer<vtkTable> outputMeta =
vtkTable::SafeDownCast( outputMetaDS->GetBlock( request + 1 ) );
if(!outputMetaDS)
{
vtkErrorMacro(<<"NULL table pointer!");
}
vtkDoubleArray* meanCol = vtkArrayDownCast<vtkDoubleArray>(outputMeta->GetColumnByName("Mean"));
vtkStringArray* rowNames = vtkArrayDownCast<vtkStringArray>(outputMeta->GetColumnByName("Column"));
eigenvalues->SetNumberOfComponents(1);
// Get values
int eval = 0;
for(vtkIdType i = 0; i < meanCol->GetNumberOfTuples(); i++)
{
std::stringstream ss;
ss << "PCA " << eval;
std::string rowName = rowNames->GetValue(i);
if(rowName.compare(ss.str()) == 0)
{
eigenvalues->InsertNextValue(meanCol->GetValue(i));
eval++;
}
}
}
// ----------------------------------------------------------------------
double vtkPCAStatistics::GetEigenvalue(int request, int i)
{
vtkSmartPointer<vtkDoubleArray> eigenvalues =
vtkSmartPointer<vtkDoubleArray>::New();
this->GetEigenvalues(request, eigenvalues);
return eigenvalues->GetValue(i);
}
// ----------------------------------------------------------------------
void vtkPCAStatistics::GetEigenvalues(vtkDoubleArray* eigenvalues)
{
this->GetEigenvalues(0, eigenvalues);
}
// ----------------------------------------------------------------------
double vtkPCAStatistics::GetEigenvalue(int i)
{
return this->GetEigenvalue(0,i);
}
// ----------------------------------------------------------------------
void vtkPCAStatistics::GetEigenvectors(int request, vtkDoubleArray* eigenvectors)
{
// Count eigenvalues
vtkSmartPointer<vtkDoubleArray> eigenvalues =
vtkSmartPointer<vtkDoubleArray>::New();
this->GetEigenvalues(request, eigenvalues);
vtkIdType numberOfEigenvalues = eigenvalues->GetNumberOfTuples();
vtkSmartPointer<vtkMultiBlockDataSet> outputMetaDS =
vtkMultiBlockDataSet::SafeDownCast(
this->GetOutputDataObject( vtkStatisticsAlgorithm::OUTPUT_MODEL ) );
if(!outputMetaDS)
{
vtkErrorMacro(<<"NULL dataset pointer!");
}
vtkSmartPointer<vtkTable> outputMeta =
vtkTable::SafeDownCast( outputMetaDS->GetBlock( request + 1 ) );
if(!outputMeta)
{
vtkErrorMacro(<<"NULL table pointer!");
}
vtkDoubleArray* meanCol = vtkArrayDownCast<vtkDoubleArray>(outputMeta->GetColumnByName("Mean"));
vtkStringArray* rowNames = vtkArrayDownCast<vtkStringArray>(outputMeta->GetColumnByName("Column"));
eigenvectors->SetNumberOfComponents(numberOfEigenvalues);
// Get vectors
int eval = 0;
for(vtkIdType i = 0; i < meanCol->GetNumberOfTuples(); i++)
{
std::stringstream ss;
ss << "PCA " << eval;
std::string rowName = rowNames->GetValue(i);
if(rowName.compare(ss.str()) == 0)
{
std::vector<double> eigenvector;
for(int val = 0; val < numberOfEigenvalues; val++)
{
// The first two columns will always be "Column" and "Mean", so start with the next one
vtkDoubleArray* currentCol = vtkArrayDownCast<vtkDoubleArray>(outputMeta->GetColumn(val+2));
eigenvector.push_back(currentCol->GetValue(i));
}
eigenvectors->InsertNextTypedTuple(&eigenvector.front());
eval++;
}
}
}
// ----------------------------------------------------------------------
void vtkPCAStatistics::GetEigenvectors(vtkDoubleArray* eigenvectors)
{
this->GetEigenvectors(0, eigenvectors);
}
// ----------------------------------------------------------------------
void vtkPCAStatistics::GetEigenvector(int request, int i, vtkDoubleArray* eigenvector)
{
vtkSmartPointer<vtkDoubleArray> eigenvectors =
vtkSmartPointer<vtkDoubleArray>::New();
this->GetEigenvectors(request, eigenvectors);
double* evec = new double[eigenvectors->GetNumberOfComponents()];
eigenvectors->GetTypedTuple(i, evec);
eigenvector->Reset();
eigenvector->Squeeze();
eigenvector->SetNumberOfComponents(eigenvectors->GetNumberOfComponents());
eigenvector->InsertNextTypedTuple(evec);
delete[] evec;
}
// ----------------------------------------------------------------------
void vtkPCAStatistics::GetEigenvector(int i, vtkDoubleArray* eigenvector)
{
this->GetEigenvector(0, i, eigenvector);
}
// ======================================================== vtkPCAAssessFunctor
class vtkPCAAssessFunctor : public vtkMultiCorrelativeAssessFunctor
{
public:
static vtkPCAAssessFunctor* New();
vtkPCAAssessFunctor() { }
~vtkPCAAssessFunctor() VTK_OVERRIDE { }
virtual bool InitializePCA(
vtkTable* inData, vtkTable* reqModel,
int normScheme, int basisScheme, int basisSize, double basisEnergy );
void operator () ( vtkDoubleArray* result, vtkIdType row ) VTK_OVERRIDE;
std::vector<double> EigenValues;
std::vector<std::vector<double> > EigenVectors;
vtkIdType BasisSize;
};
// ----------------------------------------------------------------------
vtkPCAAssessFunctor* vtkPCAAssessFunctor::New()
{
return new vtkPCAAssessFunctor;
}
// ----------------------------------------------------------------------
bool vtkPCAAssessFunctor::InitializePCA( vtkTable* inData,
vtkTable* reqModel,
int normScheme,
int basisScheme,
int fixedBasisSize,
double fixedBasisEnergy )
{
if ( ! this->vtkMultiCorrelativeAssessFunctor::Initialize( inData,
reqModel,
false /* no Cholesky decomp */ ) )
{
return false;
}
// Put the PCA basis into a matrix form we can use.
vtkIdType m = reqModel->GetNumberOfColumns() - 2;
vtkDoubleArray* evalm = vtkArrayDownCast<vtkDoubleArray>( reqModel->GetColumnByName( VTK_MULTICORRELATIVE_AVERAGECOL ) );
if ( ! evalm )
{
vtkGenericWarningMacro( "No \"" VTK_MULTICORRELATIVE_AVERAGECOL "\" column in request." );
return false;
}
// Check that the derived model includes additional rows specifying the
// normalization as required.
vtkIdType mrmr = reqModel->GetNumberOfRows(); // actual number of rows
vtkIdType ermr; // expected number of rows
switch ( normScheme )
{
case vtkPCAStatistics::NONE:
// m+1 covariance/Cholesky rows, m eigenvector rows, no normalization factors
ermr = 2 * m + 1;
break;
case vtkPCAStatistics::DIAGONAL_SPECIFIED:
case vtkPCAStatistics::DIAGONAL_VARIANCE:
// m+1 covariance/Cholesky rows, m eigenvector rows, 1 normalization factor row
ermr = 2 * m + 2;
break;
case vtkPCAStatistics::TRIANGLE_SPECIFIED:
// m+1 covariance/Cholesky rows, m eigenvector rows, m normalization factor rows
ermr = 3 * m + 1;
break;
case vtkPCAStatistics::NUM_NORMALIZATION_SCHEMES:
default:
{
vtkGenericWarningMacro( "The normalization scheme specified (" << normScheme << ") is invalid." );
return false;
}
}
// Allow derived classes to add rows, but never allow fewer than required.
if ( mrmr < ermr )
{
vtkGenericWarningMacro(
"Expected " << ( 2 * m + 1 ) << " or more columns in request but found "
<< reqModel->GetNumberOfRows() << "." );
return false;
}
// OK, we got this far; we should succeed.
vtkIdType i, j;
double eigSum = 0.;
for ( i = 0; i < m; ++ i )
{
double eigVal = evalm->GetValue( m + 1 + i );
eigSum += eigVal;
this->EigenValues.push_back( eigVal );
}
this->BasisSize = -1;
switch ( basisScheme )
{
case vtkPCAStatistics::NUM_BASIS_SCHEMES:
default:
vtkGenericWarningMacro( "Unknown basis scheme " << basisScheme << ". Using FULL_BASIS." );
VTK_FALLTHROUGH;
case vtkPCAStatistics::FULL_BASIS:
this->BasisSize = m;
break;
case vtkPCAStatistics::FIXED_BASIS_SIZE:
this->BasisSize = fixedBasisSize;
break;
case vtkPCAStatistics::FIXED_BASIS_ENERGY:
{
double frac = 0.;
for ( i = 0; i < m; ++ i )
{
frac += this->EigenValues[i] / eigSum;
if ( frac > fixedBasisEnergy )
{
this->BasisSize = i + 1;
break;
}
}
if ( this->BasisSize < 0 )
{ // OK, it takes all the eigenvectors to approximate that well...
this->BasisSize = m;
}
}
break;
}
// FIXME: Offer mode to include normalization factors (none,diag,triang)?
// Could be done here by pre-multiplying this->EigenVectors by factors.
for ( i = 0; i < this->BasisSize; ++ i )
{
std::vector<double> evec;
for ( j = 0; j < m; ++ j )
{
evec.push_back( reqModel->GetValue( m + 1 + i, j + 2 ).ToDouble() );
}
this->EigenVectors.push_back( evec );
}
return true;
}
// ----------------------------------------------------------------------
void vtkPCAAssessFunctor::operator () ( vtkDoubleArray* result, vtkIdType row )
{
vtkIdType i;
result->SetNumberOfValues( this->BasisSize );
std::vector<std::vector<double> >::iterator it;
vtkIdType m = this->GetNumberOfColumns();
for ( i = 0; i < m; ++ i )
{
this->Tuple[i] = this->Columns[i]->GetTuple( row )[0] - this->Center[i];
}
i = 0;
for ( it = this->EigenVectors.begin(); it != this->EigenVectors.end(); ++ it, ++ i )
{
double cv = 0.;
std::vector<double>::iterator tvit;
std::vector<double>::iterator evit = this->Tuple.begin();
for ( tvit = it->begin(); tvit != it->end(); ++ tvit, ++ evit )
{
cv += (*evit) * (*tvit);
}
result->SetValue( i, cv );
}
}
// ======================================================== vtkPCAStatistics
vtkPCAStatistics::vtkPCAStatistics()
{
this->SetNumberOfInputPorts( 4 ); // last port is for normalization coefficients.
this->NormalizationScheme = NONE;
this->BasisScheme = FULL_BASIS;
this->FixedBasisSize = -1;
this->FixedBasisEnergy = 1.;
}
// ----------------------------------------------------------------------
vtkPCAStatistics::~vtkPCAStatistics()
{
}
// ----------------------------------------------------------------------
void vtkPCAStatistics::PrintSelf( ostream& os, vtkIndent indent )
{
this->Superclass::PrintSelf( os, indent );
os << indent << "NormalizationScheme: " << this->GetNormalizationSchemeName( this->NormalizationScheme ) << "\n";
os << indent << "BasisScheme: " << this->GetBasisSchemeName( this->BasisScheme ) << "\n";
os << indent << "FixedBasisSize: " << this->FixedBasisSize << "\n";
os << indent << "FixedBasisEnergy: " << this->FixedBasisEnergy << "\n";
}
// ----------------------------------------------------------------------
bool vtkPCAStatistics::SetParameter( const char* parameter,
int vtkNotUsed( index ),
vtkVariant value )
{
if ( ! strcmp( parameter, "NormalizationScheme" ) )
{
this->SetNormalizationScheme( value.ToInt() );
return true;
}
if ( ! strcmp( parameter, "BasisScheme" ) )
{
this->SetBasisScheme( value.ToInt() );
return true;
}
if ( ! strcmp( parameter, "FixedBasisSize" ) )
{
this->SetFixedBasisSize( value.ToInt() );
return true;
}
if ( ! strcmp( parameter, "FixedBasisEnergy" ) )
{
this->SetFixedBasisEnergy( value.ToDouble() );
return true;
}
return false;
}
// ----------------------------------------------------------------------
const char* vtkPCAStatistics::GetNormalizationSchemeName( int schemeIndex )
{
if ( schemeIndex < 0 || schemeIndex > NUM_NORMALIZATION_SCHEMES )
{
return vtkPCAStatistics::NormalizationSchemeEnumNames[NUM_NORMALIZATION_SCHEMES];
}
return vtkPCAStatistics::NormalizationSchemeEnumNames[schemeIndex];
}
// ----------------------------------------------------------------------
void vtkPCAStatistics::SetNormalizationSchemeByName( const char* schemeName )
{
for ( int i = 0; i < NUM_NORMALIZATION_SCHEMES; ++ i )
{
if ( ! strcmp( vtkPCAStatistics::NormalizationSchemeEnumNames[i], schemeName ) )
{
this->SetNormalizationScheme( i );
return;
}
}
vtkErrorMacro( "Invalid normalization scheme name \"" << schemeName << "\" provided." );
}
// ----------------------------------------------------------------------
vtkTable* vtkPCAStatistics::GetSpecifiedNormalization()
{
return vtkTable::SafeDownCast( this->GetInputDataObject( 3, 0 ) );
}
void vtkPCAStatistics::SetSpecifiedNormalization( vtkTable* normSpec )
{
this->SetInputData( 3, normSpec );
}
// ----------------------------------------------------------------------
const char* vtkPCAStatistics::GetBasisSchemeName( int schemeIndex )
{
if ( schemeIndex < 0 || schemeIndex > NUM_BASIS_SCHEMES )
{
return vtkPCAStatistics::BasisSchemeEnumNames[NUM_BASIS_SCHEMES];
}
return vtkPCAStatistics::BasisSchemeEnumNames[schemeIndex];
}
// ----------------------------------------------------------------------
void vtkPCAStatistics::SetBasisSchemeByName( const char* schemeName )
{
for ( int i = 0; i < NUM_BASIS_SCHEMES; ++ i )
{
if ( ! strcmp( vtkPCAStatistics::BasisSchemeEnumNames[i], schemeName ) )
{
this->SetBasisScheme( i );
return;
}
}
vtkErrorMacro( "Invalid basis scheme name \"" << schemeName << "\" provided." );
}
// ----------------------------------------------------------------------
int vtkPCAStatistics::FillInputPortInformation( int port, vtkInformation* info )
{
if ( port == 3 )
{
info->Set( vtkAlgorithm::INPUT_REQUIRED_DATA_TYPE(), "vtkTable" );
info->Set( vtkAlgorithm::INPUT_IS_OPTIONAL(), 1 );
return 1;
}
return this->Superclass::FillInputPortInformation( port, info );
}
// ----------------------------------------------------------------------
static void vtkPCAStatisticsNormalizeSpec( vtkVariantArray* normData,
ap::real_2d_array& cov,
vtkTable* normSpec,
vtkTable* reqModel,
bool triangle )
{
vtkIdType i, j;
vtkIdType m = reqModel->GetNumberOfColumns() - 2;
std::map<vtkStdString,vtkIdType> colNames;
// Get a list of columns of interest for this request
for ( i = 0; i < m; ++ i )
{
colNames[ reqModel->GetColumn( i + 2 )->GetName() ] = i;
}
// Turn normSpec into a useful array.
std::map<std::pair<vtkIdType,vtkIdType>,double> factor;
vtkIdType n = normSpec->GetNumberOfRows();
for ( vtkIdType r = 0; r < n; ++ r )
{
std::map<vtkStdString,vtkIdType>::iterator it;
if ( ( it = colNames.find( normSpec->GetValue( r, 0 ).ToString() ) ) == colNames.end() )
{
continue;
}
i = it->second;
if ( ( it = colNames.find( normSpec->GetValue( r, 1 ).ToString() ) ) == colNames.end() )
{
continue;
}
j = it->second;
if ( j < i )
{
vtkIdType tmp = i;
i = j;
j = tmp;
}
factor[std::pair<vtkIdType,vtkIdType>( i, j )] = normSpec->GetValue( r, 2 ).ToDouble();
}
// Now normalize cov, recording any missing factors along the way.
std::ostringstream missing;
bool gotMissing = false;
std::map<std::pair<vtkIdType,vtkIdType>,double>::iterator fit;
if ( triangle )
{ // Normalization factors are provided for the upper triangular portion of the covariance matrix.
for ( i = 0; i < m; ++ i )
{
for ( j = i; j < m; ++ j )
{
double v;
fit = factor.find( std::pair<vtkIdType,vtkIdType>( i, j ) );
if ( fit == factor.end() )
{
v = 1.;
gotMissing = true;
missing
<< "(" << reqModel->GetColumn( i + 2 )->GetName()
<< "," << reqModel->GetColumn( j + 2 )->GetName()
<< ") ";
}
else
{
v = fit->second;
}
normData->InsertNextValue( v );
cov( i, j ) /= v;
if ( i != j )
{ // don't normalize diagonal entries twice
cov( j, i ) /= v;
}
}
}
}
else
{ // Only diagonal normalization factors are supplied. Off-diagonals are the product of diagonals.
for ( i = 0; i < m; ++ i )
{
double v;
double vsq;
fit = factor.find( std::pair<vtkIdType,vtkIdType>( i, i ) );
if ( fit == factor.end() )
{
vsq = v = 1.;
gotMissing = true;
missing
<< "(" << reqModel->GetColumn( i + 2 )->GetName()
<< "," << reqModel->GetColumn( i + 2 )->GetName()
<< ") ";
}
else
{
vsq = fit->second;
v = sqrt( vsq );
}
normData->InsertNextValue( vsq );
// normalization factor applied up and to the left.
for ( j = 0; j < i; ++ j )
{
cov( i, j ) /= v;
cov( j, i ) /= v;
}
// normalization factor applied down and to the right.
for ( j = i + 1; j < m; ++ j )
{
cov( i, j ) /= v;
cov( j, i ) /= v;
}
cov( i, i ) /= vsq;
}
}
if ( gotMissing )
{
vtkGenericWarningMacro(
"The following normalization factors were expected but not provided: "
<< missing.str().c_str() );
}
}
// ----------------------------------------------------------------------
static void vtkPCAStatisticsNormalizeVariance( vtkVariantArray* normData,
ap::real_2d_array& cov )
{
vtkIdType i, j;
vtkIdType m = cov.gethighbound( 0 ) - cov.getlowbound( 0 ) + 1;
for ( i = 0; i < m; ++ i )
{
normData->InsertNextValue( cov( i, i ) );
double norm = sqrt( cov( i, i ) );
// normalization factor applied down and to the right.
for ( j = i + 1; j < m; ++ j )
{
cov( i, j ) /= norm;
cov( j, i ) /= norm;
}
// normalization factor applied up and to the left.
for ( j = 0; j < i; ++ j )
{
cov( i, j ) /= norm;
cov( j, i ) /= norm;
}
cov( i, i ) = 1.;
}
}
// ----------------------------------------------------------------------
void vtkPCAStatistics::Derive( vtkMultiBlockDataSet* inMeta )
{
if ( ! inMeta )
{
return;
}
// Use the parent class to compute a covariance matrix for each request.
this->Superclass::Derive( inMeta );
// Now that we have the covariance matrices, compute the SVD of each.
vtkIdType nb = static_cast<vtkIdType>( inMeta->GetNumberOfBlocks() );
for ( vtkIdType b = 1; b < nb; ++ b )
{
vtkTable* reqModel = vtkTable::SafeDownCast( inMeta->GetBlock( b ) );
if ( ! reqModel )
{
continue;
}
vtkIdType m = reqModel->GetNumberOfColumns() - 2;
ap::real_2d_array cov;
cov.setbounds( 0, m - 1, 0, m - 1 );
// Fill the cov array with values from the vtkTable
vtkIdType i, j;
for ( j = 2; j < 2 + m; ++ j )
{
for ( i = 0; i < j - 1; ++ i )
{
cov( i, j - 2 ) = reqModel->GetValue( i, j ).ToDouble();
}
}
// Fill in the lower triangular portion of the matrix
for ( j = 0; j < m - 1; ++ j )
{
for ( i = j; i < m; ++ i )
{
cov( i, j ) = cov( j, i );
}
}
// If normalization of the covariance array is requested, perform it:
vtkVariantArray* normData = vtkVariantArray::New();
switch ( this->NormalizationScheme )
{
case TRIANGLE_SPECIFIED:
case DIAGONAL_SPECIFIED:
vtkPCAStatisticsNormalizeSpec(
normData, cov, this->GetSpecifiedNormalization(), reqModel,
this->NormalizationScheme == TRIANGLE_SPECIFIED );
break;
case DIAGONAL_VARIANCE:
vtkPCAStatisticsNormalizeVariance( normData, cov );
break;
case NONE:
case NUM_NORMALIZATION_SCHEMES:
default:
// do nothing
break;
}
ap::real_2d_array u;
ap::real_1d_array s;
ap::real_2d_array vt;
// Now that we have the covariance matrix, compute the SVD.
// Note that vt is not computed since the VtNeeded parameter is 0.
bool status = rmatrixsvd( cov, m, m, 2, 0, 2, s, u, vt );
if ( ! status )
{
vtkWarningMacro( "Could not compute PCA for request " << b );
continue;
}
vtkVariantArray* row = vtkVariantArray::New();
//row->SetNumberOfComponents( m + 2 );
//row->SetNumberOfTuples( 1 );
row->SetNumberOfComponents( 1 );
row->SetNumberOfTuples( m + 2 );
for ( i = 0; i < m; ++ i )
{
std::ostringstream pcaCompName;
pcaCompName << VTK_PCA_COMPCOLUMN << " " << i;
row->SetValue( 0, pcaCompName.str().c_str() );
row->SetValue( 1, s( i ) );
for ( j = 0; j < m; ++ j )
{
// transpose the matrix so basis is row vectors (and thus
// eigenvalues are to the left of their eigenvectors):
row->SetValue( j + 2, u( j, i ) );
}
reqModel->InsertNextRow( row );
}
// Now insert the subset of the normalization data we used to
// process this request at the bottom of the results.
switch ( this->NormalizationScheme )
{
case TRIANGLE_SPECIFIED:
for ( i = 0; i < m; ++ i )
{
std::ostringstream normCompName;
normCompName << VTK_PCA_NORMCOLUMN << " " << i;
row->SetValue( 0, normCompName.str().c_str() );
row->SetValue( 1, 0. );
for ( j = 0; j < i; ++ j )
{
row->SetValue( j + 2, 0. );
}
for ( ; j < m; ++ j )
{
row->SetValue( j + 2, normData->GetValue( j ) );
}
reqModel->InsertNextRow( row );
}
break;
case DIAGONAL_SPECIFIED:
case DIAGONAL_VARIANCE:
{
row->SetValue( 0, VTK_PCA_NORMCOLUMN );
row->SetValue( 1, 0. );
for ( j = 0; j < m; ++ j )
{
row->SetValue( j + 2, normData->GetValue( j ) );
}
reqModel->InsertNextRow( row );
}
break;
case NONE:
case NUM_NORMALIZATION_SCHEMES:
default:
// do nothing
break;
}
normData->Delete();
row->Delete();
}
}
// Use the invalid value of -1 for p-values if R is absent
vtkDoubleArray* vtkPCAStatistics::CalculatePValues( vtkIdTypeArray* vtkNotUsed( dimCol ),
vtkDoubleArray* statCol )
{
// A column must be created first
vtkDoubleArray* testCol = vtkDoubleArray::New();
// Fill this column
vtkIdType n = statCol->GetNumberOfTuples();
testCol->SetNumberOfTuples( n );
for ( vtkIdType r = 0; r < n; ++ r )
{
testCol->SetTuple1( r, -1 );
}
return testCol;
}
// ----------------------------------------------------------------------
void vtkPCAStatistics::Test( vtkTable* inData,
vtkMultiBlockDataSet* inMeta,
vtkTable* outMeta )
{
if ( ! inMeta )
{
return;
}
if ( ! outMeta )
{
return;
}
// Prepare columns for the test:
// 0: (derived) model block index
// 1: multivariate Srivastava skewness
// 2: multivariate Srivastava kurtosis
// 3: multivariate Jarque-Bera-Srivastava statistic
// 4: multivariate Jarque-Bera-Srivastava p-value (calculated only if R is available, filled with -1 otherwise)
// 5: number of degrees of freedom of Chi square distribution
// NB: These are not added to the output table yet, for they will be filled individually first
// in order that R be invoked only once.
vtkIdTypeArray* blockCol = vtkIdTypeArray::New();
blockCol->SetName( "Block" );
vtkDoubleArray* bS1Col = vtkDoubleArray::New();
bS1Col->SetName( "Srivastava Skewness" );
vtkDoubleArray* bS2Col = vtkDoubleArray::New();
bS2Col->SetName( "Srivastava Kurtosis" );
vtkDoubleArray* statCol = vtkDoubleArray::New();
statCol->SetName( "Jarque-Bera-Srivastava" );
vtkIdTypeArray* dimCol = vtkIdTypeArray::New();
dimCol->SetName( "d" );
// Retain data cardinality to check that models are applicable
vtkIdType nRowData = inData->GetNumberOfRows();
// Now iterate over model blocks
unsigned int nBlocks = inMeta->GetNumberOfBlocks();
for ( unsigned int b = 1; b < nBlocks; ++ b )
{
vtkTable* derivedTab = vtkTable::SafeDownCast( inMeta->GetBlock( b ) );
// Silenty ignore empty blocks
if ( ! derivedTab )
{
continue;
}
// Figure out dimensionality; it is assumed that the 2 first columns
// are what they should be: namely, Column and Mean.
int p = derivedTab->GetNumberOfColumns() - 2;
// Return informative message when cardinalities do not match.
if ( derivedTab->GetValueByName( p, "Mean" ).ToInt() != nRowData )
{
vtkWarningMacro( "Inconsistent input: input data has "
<< nRowData
<< " rows but primary model has cardinality "
<< derivedTab->GetValueByName( p, "Mean" ).ToInt()
<< " for block "
<< b
<<". Cannot test." );
continue;
}
// Create and fill entries of name and mean vectors
vtkStdString *varNameX = new vtkStdString[p];
double *mX = new double[p];
for ( int i = 0; i < p; ++ i )
{
varNameX[i] = derivedTab->GetValueByName( i, "Column" ).ToString();
mX[i] = derivedTab->GetValueByName( i, "Mean" ).ToDouble();
}
// Create and fill entries of eigenvalue vector and change of basis matrix
double *wX = new double[p];
double *P = new double[p * p];
for ( int i = 0; i < p; ++ i )
{
// Skip p + 1 (Means and Cholesky) rows and 1 column (Column)
wX[i] = derivedTab->GetValue( i + p + 1, 1).ToDouble();
for ( int j = 0; j < p; ++ j )
{
// Skip p + 1 (Means and Cholesky) rows and 2 columns (Column and Mean)
P[p * i + j] = derivedTab->GetValue( i + p + 1, j + 2).ToDouble();
}
}
// Now iterate over all observations
double tmp, t;
double *x = new double[p];
double *sum3 = new double[p];
double *sum4 = new double[p];
for ( int i = 0; i < p; ++ i )
{
sum3[i] = 0.;
sum4[i] = 0.;
}
for ( vtkIdType r = 0; r < nRowData; ++ r )
{
// Read and center observation
for ( int i = 0; i < p; ++ i )
{
x[i] = inData->GetValueByName( r, varNameX[i] ).ToDouble() - mX[i];
}
// Now calculate skewness and kurtosis per component
for ( int i = 0; i < p; ++ i )
{
// Transform coordinate into eigencoordinates
t = 0.;
for ( int j = 0; j < p; ++ j )
{
// Pij = P[p*i+j]
t += P[p * i + j] * x[j];
}
// Update third and fourth order sums for each eigencoordinate
tmp = t * t;
sum3[i] += tmp * t;
sum4[i] += tmp * tmp;
}
}
// Finally calculate moments by normalizing sums with corresponding eigenvalues and powers
double bS1 = 0.;
double bS2 = 0.;
for ( int i = 0; i < p; ++ i )
{
tmp = wX[i] * wX[i];
bS1 += sum3[i] * sum3[i] / ( tmp * wX[i] );
bS2 += sum4[i] / tmp;
}
bS1 /= ( nRowData * nRowData * p );
bS2 /= ( nRowData * p );
// Finally, calculate Jarque-Bera-Srivastava statistic
tmp = bS2 - 3.;
double jbs = static_cast<double>( nRowData * p ) * ( bS1 / 6. + ( tmp * tmp ) / 24. );
// Insert variable name and calculated Jarque-Bera-Srivastava statistic
blockCol->InsertNextValue( b );
bS1Col->InsertNextTuple1( bS1 );
bS2Col->InsertNextTuple1( bS2 );
statCol->InsertNextTuple1( jbs );
dimCol->InsertNextTuple1( p + 1 );
// Clean up
delete [] sum3;
delete [] sum4;
delete [] x;
delete [] P;
delete [] wX;
delete [] mX;
delete [] varNameX;
} // b
// Now, add the already prepared columns to the output table
outMeta->AddColumn( blockCol );
outMeta->AddColumn( bS1Col );
outMeta->AddColumn( bS2Col );
outMeta->AddColumn( statCol );
outMeta->AddColumn( dimCol );
// Last phase: compute the p-values or assign invalid value if they cannot be computed
vtkDoubleArray* testCol = this->CalculatePValues( dimCol, statCol );
// The test column name can only be set after the column has been obtained from R
testCol->SetName( "P" );
// Now add the column of invalid values to the output table
outMeta->AddColumn( testCol );
// Clean up
testCol->Delete();
// Clean up
blockCol->Delete();
bS1Col->Delete();
bS2Col->Delete();
statCol->Delete();
dimCol->Delete();
}
// ----------------------------------------------------------------------
void vtkPCAStatistics::Assess( vtkTable* inData,
vtkMultiBlockDataSet* inMeta,
vtkTable* outData )
{
if ( ! inData )
{
return;
}
if ( ! inMeta )
{
return;
}
// For each request, add a column to the output data related to the likelihood of each input datum wrt the model in the request.
// Column names of the metadata and input data are assumed to match.
// The output columns will be named "RelDevSq(A,B,C)" where "A", "B", and "C" are the column names specified in the
// per-request metadata tables.
vtkIdType nRow = inData->GetNumberOfRows();
int nb = static_cast<int>( inMeta->GetNumberOfBlocks() );
AssessFunctor* dfunc = 0;
for ( int req = 1; req < nb; ++ req )