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
|
/*=========================================================================
Program: Visualization Toolkit
Module: vtkDescriptiveStatistics.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 "vtkToolkits.h"
#include "vtkDescriptiveStatistics.h"
#include "vtkStatisticsAlgorithmPrivate.h"
#include "vtkDataObjectCollection.h"
#include "vtkDoubleArray.h"
#include "vtkIdTypeArray.h"
#include "vtkInformation.h"
#include "vtkMath.h"
#include "vtkMultiBlockDataSet.h"
#include "vtkObjectFactory.h"
#include "vtkStringArray.h"
#include "vtkStdString.h"
#include "vtkTable.h"
#include "vtkVariantArray.h"
#include <set>
#include <sstream>
#include <limits>
vtkObjectFactoryNewMacro(vtkDescriptiveStatistics)
// ----------------------------------------------------------------------
vtkDescriptiveStatistics::vtkDescriptiveStatistics()
{
this->AssessNames->SetNumberOfValues( 1 );
this->AssessNames->SetValue( 0, "d" ); // relative deviation, i.e., when unsigned, 1D Mahalanobis distance
this->UnbiasedVariance = 1; // By default, use unbiased estimator of the variance (divided by cardinality-1)
this->G1Skewness = 0; // By default, use g1 estimator of the skewness (G1 otherwise)
this->G2Kurtosis = 0; // By default, use g2 estimator of the kurtosis (G2 otherwise)
this->SignedDeviations = 0; // By default, use unsigned deviation (1D Mahlanobis distance)
}
// ----------------------------------------------------------------------
vtkDescriptiveStatistics::~vtkDescriptiveStatistics()
{
}
// ----------------------------------------------------------------------
void vtkDescriptiveStatistics::PrintSelf( ostream &os, vtkIndent indent )
{
this->Superclass::PrintSelf( os, indent );
os << indent << "UnbiasedVariance: " << this->UnbiasedVariance << "\n";
os << indent << "G1Skewness: " << this->G1Skewness << "\n";
os << indent << "G2Kurtosis: " << this->G2Kurtosis << "\n";
os << indent << "SignedDeviations: " << this->SignedDeviations << "\n";
}
// ----------------------------------------------------------------------
void vtkDescriptiveStatistics::Aggregate( vtkDataObjectCollection* inMetaColl,
vtkMultiBlockDataSet* outMeta )
{
if ( ! outMeta )
{
return;
}
// Get hold of the first model (data object) in the collection
vtkCollectionSimpleIterator it;
inMetaColl->InitTraversal( it );
vtkDataObject *inMetaDO = inMetaColl->GetNextDataObject( it );
// Verify that the first input model is indeed contained in a multiblock data set
vtkMultiBlockDataSet* inMeta = vtkMultiBlockDataSet::SafeDownCast( inMetaDO );
if ( ! inMeta )
{
return;
}
// Verify that the first primary statistics are indeed contained in a table
vtkTable* primaryTab = vtkTable::SafeDownCast( inMeta->GetBlock( 0 ) );
if ( ! primaryTab )
{
return;
}
vtkIdType nRow = primaryTab->GetNumberOfRows();
if ( ! nRow )
{
// No statistics were calculated.
return;
}
// Use this first model to initialize the aggregated one
vtkTable* aggregatedTab = vtkTable::New();
aggregatedTab->DeepCopy( primaryTab );
// Now, loop over all remaining models and update aggregated each time
while ( ( inMetaDO = inMetaColl->GetNextDataObject( it ) ) )
{
// Verify that the current model is indeed contained in a multiblock data set
inMeta = vtkMultiBlockDataSet::SafeDownCast( inMetaDO );
if ( ! inMeta )
{
aggregatedTab->Delete();
return;
}
// Verify that the current primary statistics are indeed contained in a table
primaryTab = vtkTable::SafeDownCast( inMeta->GetBlock( 0 ) );
if ( ! primaryTab )
{
aggregatedTab->Delete();
return;
}
if ( primaryTab->GetNumberOfRows() != nRow )
{
// Models do not match
aggregatedTab->Delete();
return;
}
// Iterate over all model rows
for ( int r = 0; r < nRow; ++ r )
{
// Verify that variable names match each other
if ( primaryTab->GetValueByName( r, "Variable" ) != aggregatedTab->GetValueByName( r, "Variable" ) )
{
// Models do not match
aggregatedTab->Delete();
return;
}
// Get aggregated statistics
int n = aggregatedTab->GetValueByName( r, "Cardinality" ).ToInt();
double min = aggregatedTab->GetValueByName( r, "Minimum" ).ToDouble();
double max = aggregatedTab->GetValueByName( r, "Maximum" ).ToDouble();
double mean = aggregatedTab->GetValueByName( r, "Mean" ).ToDouble();
double M2 = aggregatedTab->GetValueByName( r, "M2" ).ToDouble();
double M3 = aggregatedTab->GetValueByName( r, "M3" ).ToDouble();
double M4 = aggregatedTab->GetValueByName( r, "M4" ).ToDouble();
// Get current model statistics
int n_c = primaryTab->GetValueByName( r, "Cardinality" ).ToInt();
double min_c = primaryTab->GetValueByName( r, "Minimum" ).ToDouble();
double max_c = primaryTab->GetValueByName( r, "Maximum" ).ToDouble();
double mean_c = primaryTab->GetValueByName( r, "Mean" ).ToDouble();
double M2_c = primaryTab->GetValueByName( r, "M2" ).ToDouble();
double M3_c = primaryTab->GetValueByName( r, "M3" ).ToDouble();
double M4_c = primaryTab->GetValueByName( r, "M4" ).ToDouble();
// Update global statics
int N = n + n_c;
if ( min_c < min )
{
aggregatedTab->SetValueByName( r, "Minimum", min_c );
}
if ( max_c > max )
{
aggregatedTab->SetValueByName( r, "Maximum", max_c );
}
double delta = mean_c - mean;
double delta_sur_N = delta / static_cast<double>( N );
double delta2_sur_N2 = delta_sur_N * delta_sur_N;
int n2 = n * n;
int n_c2 = n_c * n_c;
int prod_n = n * n_c;
M4 += M4_c
+ prod_n * ( n2 - prod_n + n_c2 ) * delta * delta_sur_N * delta2_sur_N2
+ 6. * ( n2 * M2_c + n_c2 * M2 ) * delta2_sur_N2
+ 4. * ( n * M3_c - n_c * M3 ) * delta_sur_N;
M3 += M3_c
+ prod_n * ( n - n_c ) * delta * delta2_sur_N2
+ 3. * ( n * M2_c - n_c * M2 ) * delta_sur_N;
M2 += M2_c
+ prod_n * delta * delta_sur_N;
mean += n_c * delta_sur_N;
// Store updated model
aggregatedTab->SetValueByName( r, "Cardinality", N );
aggregatedTab->SetValueByName( r, "Mean", mean );
aggregatedTab->SetValueByName( r, "M2", M2 );
aggregatedTab->SetValueByName( r, "M3", M3 );
aggregatedTab->SetValueByName( r, "M4", M4 );
}
}
// Finally set first block of aggregated model to primary statistics table
outMeta->SetNumberOfBlocks( 1 );
outMeta->GetMetaData( static_cast<unsigned>( 0 ) )->Set( vtkCompositeDataSet::NAME(), "Primary Statistics" );
outMeta->SetBlock( 0, aggregatedTab );
// Clean up
aggregatedTab->Delete();
}
// ----------------------------------------------------------------------
void vtkDescriptiveStatistics::Learn( vtkTable* inData,
vtkTable* vtkNotUsed( inParameters ),
vtkMultiBlockDataSet* outMeta )
{
if ( ! inData )
{
return;
}
if ( ! outMeta )
{
return;
}
// The primary statistics table
vtkTable* primaryTab = vtkTable::New();
vtkStringArray* stringCol = vtkStringArray::New();
stringCol->SetName( "Variable" );
primaryTab->AddColumn( stringCol );
stringCol->Delete();
vtkIdTypeArray* idTypeCol = vtkIdTypeArray::New();
idTypeCol->SetName( "Cardinality" );
primaryTab->AddColumn( idTypeCol );
idTypeCol->Delete();
vtkDoubleArray* doubleCol = vtkDoubleArray::New();
doubleCol->SetName( "Minimum" );
primaryTab->AddColumn( doubleCol );
doubleCol->Delete();
doubleCol = vtkDoubleArray::New();
doubleCol->SetName( "Maximum" );
primaryTab->AddColumn( doubleCol );
doubleCol->Delete();
doubleCol = vtkDoubleArray::New();
doubleCol->SetName( "Mean" );
primaryTab->AddColumn( doubleCol );
doubleCol->Delete();
doubleCol = vtkDoubleArray::New();
doubleCol->SetName( "M2" );
primaryTab->AddColumn( doubleCol );
doubleCol->Delete();
doubleCol = vtkDoubleArray::New();
doubleCol->SetName( "M3" );
primaryTab->AddColumn( doubleCol );
doubleCol->Delete();
doubleCol = vtkDoubleArray::New();
doubleCol->SetName( "M4" );
primaryTab->AddColumn( doubleCol );
doubleCol->Delete();
// Loop over requests
vtkIdType nRow = inData->GetNumberOfRows();
for ( std::set<std::set<vtkStdString> >::const_iterator rit = this->Internals->Requests.begin();
rit != this->Internals->Requests.end(); ++ rit )
{
// Each request contains only one column of interest (if there are others, they are ignored)
std::set<vtkStdString>::const_iterator it = rit->begin();
vtkStdString varName = *it;
if ( ! inData->GetColumnByName( varName ) )
{
vtkWarningMacro( "InData table does not have a column "
<< varName.c_str()
<< ". Ignoring it." );
continue;
}
double minVal = inData->GetValueByName( 0, varName ).ToDouble();
double maxVal = minVal;
double mean = 0.;
double mom2 = 0.;
double mom3 = 0.;
double mom4 = 0.;
double n, inv_n, val, delta, A, B;
for ( vtkIdType r = 0; r < nRow; ++ r )
{
n = r + 1.;
inv_n = 1. / n;
val = inData->GetValueByName( r, varName ).ToDouble();
delta = val - mean;
A = delta * inv_n;
mean += A;
mom4 += A * ( A * A * delta * r * ( n * ( n - 3. ) + 3. ) + 6. * A * mom2 - 4. * mom3 );
B = val - mean;
mom3 += A * ( B * delta * ( n - 2. ) - 3. * mom2 );
mom2 += delta * B;
if ( val < minVal )
{
minVal = val;
}
else if ( val > maxVal )
{
maxVal = val;
}
}
vtkVariantArray* row = vtkVariantArray::New();
row->SetNumberOfValues( 8 );
row->SetValue( 0, varName );
row->SetValue( 1, nRow );
row->SetValue( 2, minVal );
row->SetValue( 3, maxVal );
row->SetValue( 4, mean );
row->SetValue( 5, mom2 );
row->SetValue( 6, mom3 );
row->SetValue( 7, mom4 );
primaryTab->InsertNextRow( row );
row->Delete();
} // rit
// Finally set first block of output meta port to primary statistics table
outMeta->SetNumberOfBlocks( 1 );
outMeta->GetMetaData( static_cast<unsigned>( 0 ) )->Set( vtkCompositeDataSet::NAME(), "Primary Statistics" );
outMeta->SetBlock( 0, primaryTab );
// Clean up
primaryTab->Delete();
}
// ----------------------------------------------------------------------
void vtkDescriptiveStatistics::Derive( vtkMultiBlockDataSet* inMeta )
{
if ( ! inMeta || inMeta->GetNumberOfBlocks() < 1 )
{
return;
}
vtkTable* primaryTab = vtkTable::SafeDownCast( inMeta->GetBlock( 0 ) );
if ( ! primaryTab )
{
return;
}
int numDoubles = 5;
vtkStdString doubleNames[] = { "Standard Deviation",
"Variance",
"Skewness",
"Kurtosis",
"Sum" };
// Create table for derived statistics
vtkIdType nRow = primaryTab->GetNumberOfRows();
vtkTable* derivedTab = vtkTable::New();
vtkDoubleArray* doubleCol;
for ( int j = 0; j < numDoubles; ++ j )
{
if ( ! derivedTab->GetColumnByName( doubleNames[j] ) )
{
doubleCol = vtkDoubleArray::New();
doubleCol->SetName( doubleNames[j] );
doubleCol->SetNumberOfTuples( nRow );
derivedTab->AddColumn( doubleCol );
doubleCol->Delete();
}
}
// Storage for standard deviation, variance, skewness, kurtosis, sum
double* derivedVals = new double[numDoubles];
for ( int i = 0; i < nRow; ++ i )
{
double mom2 = primaryTab->GetValueByName( i, "M2" ).ToDouble();
double mom3 = primaryTab->GetValueByName( i, "M3" ).ToDouble();
double mom4 = primaryTab->GetValueByName( i, "M4" ).ToDouble();
int numSamples = primaryTab->GetValueByName( i, "Cardinality" ).ToInt();
if ( numSamples == 1 || mom2 < 1.e-150 )
{
derivedVals[0] = 0.;
derivedVals[1] = 0.;
derivedVals[2] = 0.;
derivedVals[3] = 0.;
derivedVals[4] = 0.;
}
else
{
double n = static_cast<double>( numSamples );
double inv_n = 1. / n;
double nm1 = n - 1.;
// Variance
if ( this->UnbiasedVariance )
{
derivedVals[1] = mom2 / nm1;
}
else // use population variance
{
derivedVals[1] = mom2 * inv_n;
}
// Standard deviation
derivedVals[0] = sqrt( derivedVals[1] );
// Skeweness and kurtosis
double var_inv = nm1 / mom2;
double nvar_inv = var_inv * inv_n;
derivedVals[2] = nvar_inv * sqrt( var_inv ) * mom3;
derivedVals[3] = nvar_inv * var_inv * mom4 - 3.;
if ( this->G1Skewness && n > 2 )
{
// G1 skewness estimate
derivedVals[2] *= ( n * n ) / ( nm1 * ( nm1 - 1. ) );
}
if ( this->G2Kurtosis && n > 3 )
{
// G2 kurtosis estimate
derivedVals[3] *= ( ( n + 1. ) * derivedVals[4] + 6. ) * nm1 / ( ( nm1 - 1. ) * ( nm1 - 2. ) );
}
}
// Sum
derivedVals[4] = numSamples * primaryTab->GetValueByName( i, "Mean" ).ToDouble();
for ( int j = 0; j < numDoubles; ++ j )
{
derivedTab->SetValueByName( i, doubleNames[j], derivedVals[j] );
}
}
// Finally set second block of output meta port to derived statistics table
inMeta->SetNumberOfBlocks( 2 );
inMeta->GetMetaData( static_cast<unsigned>( 0 ) )->Set( vtkCompositeDataSet::NAME(), "Derived Statistics" );
inMeta->SetBlock( 1, derivedTab );
// Clean up
derivedTab->Delete();
delete [] derivedVals;
}
// ----------------------------------------------------------------------
// Use the invalid value of -1 for p-values if R is absent
vtkDoubleArray* vtkDescriptiveStatistics::CalculatePValues(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 vtkDescriptiveStatistics::Test( vtkTable* inData,
vtkMultiBlockDataSet* inMeta,
vtkTable* outMeta )
{
if ( ! inMeta )
{
return;
}
vtkTable* primaryTab = vtkTable::SafeDownCast( inMeta->GetBlock( 0 ) );
if ( ! primaryTab )
{
return;
}
vtkTable* derivedTab = vtkTable::SafeDownCast( inMeta->GetBlock( 1 ) );
if ( ! derivedTab )
{
return;
}
vtkIdType nRowPrim = primaryTab->GetNumberOfRows();
if ( nRowPrim != derivedTab->GetNumberOfRows() )
{
vtkErrorMacro( "Inconsistent input: primary model has "
<< nRowPrim
<< " rows but derived model has "
<< derivedTab->GetNumberOfRows()
<<". Cannot test." );
return;
}
if ( ! outMeta )
{
return;
}
// Prepare columns for the test:
// 0: variable name
// 1: Jarque-Bera statistic
// 2: Jarque-Bera p-value (calculated only if R is available, filled with -1 otherwise)
// 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.
vtkStringArray* nameCol = vtkStringArray::New();
nameCol->SetName( "Variable" );
vtkDoubleArray* statCol = vtkDoubleArray::New();
statCol->SetName( "Jarque-Bera" );
// Downcast columns to string arrays for efficient data access
vtkStringArray* vars = vtkArrayDownCast<vtkStringArray>( primaryTab->GetColumnByName( "Variable" ) );
// Loop over requests
for ( std::set<std::set<vtkStdString> >::const_iterator rit = this->Internals->Requests.begin();
rit != this->Internals->Requests.end(); ++ rit )
{
// Each request contains only one column of interest (if there are others, they are ignored)
std::set<vtkStdString>::const_iterator it = rit->begin();
vtkStdString varName = *it;
if ( ! inData->GetColumnByName( varName ) )
{
vtkWarningMacro( "InData table does not have a column "
<< varName.c_str()
<< ". Ignoring it." );
continue;
}
// Find the model row that corresponds to the variable of the request
vtkIdType r = 0;
while ( r < nRowPrim && vars->GetValue( r ) != varName )
{
++ r;
}
if ( r >= nRowPrim )
{
vtkWarningMacro( "Incomplete input: model does not have a row "
<< varName.c_str()
<<". Cannot test." );
continue;
}
// Retrieve model statistics necessary for Jarque-Bera testing
double n = primaryTab->GetValueByName( r, "Cardinality" ).ToDouble();
double skew = derivedTab->GetValueByName( r, "Skewness" ).ToDouble();
double kurt = derivedTab->GetValueByName( r, "Kurtosis" ).ToDouble();
// Now calculate Jarque-Bera statistic
double jb = n * ( skew * skew + .25 * kurt * kurt ) / 6.;
// Insert variable name and calculated Jarque-Bera statistic
// NB: R will be invoked only once at the end for efficiency
nameCol->InsertNextValue( varName );
statCol->InsertNextTuple1( jb );
} // rit
// Now, add the already prepared columns to the output table
outMeta->AddColumn( nameCol );
outMeta->AddColumn( statCol );
// Last phase: compute the p-values or assign invalid value if they cannot be computed
// If available, use R to obtain the p-values for the Chi square distribution with 2 DOFs
vtkDoubleArray* testCol = this->CalculatePValues( 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 );
testCol->Delete();
// Clean up
nameCol->Delete();
statCol->Delete();
}
// ----------------------------------------------------------------------
class TableColumnDeviantFunctor : public vtkStatisticsAlgorithm::AssessFunctor
{
public:
vtkDataArray* Data;
double Nominal;
double Deviation;
};
// When the deviation is 0, we can't normalize. Instead, a non-zero value (1)
// is returned only when the nominal value is matched exactly.
class ZedDeviationDeviantFunctor : public TableColumnDeviantFunctor
{
public:
ZedDeviationDeviantFunctor( vtkDataArray* vals,
double nominal )
{
this->Data = vals;
this->Nominal = nominal;
}
~ZedDeviationDeviantFunctor() VTK_OVERRIDE { }
void operator() ( vtkDoubleArray* result,
vtkIdType id ) VTK_OVERRIDE
{
result->SetNumberOfValues( 1 );
result->SetValue( 0, ( this->Data->GetTuple1( id ) == this->Nominal ) ? 0. : 1. );
}
};
class SignedTableColumnDeviantFunctor : public TableColumnDeviantFunctor
{
public:
SignedTableColumnDeviantFunctor( vtkDataArray* vals,
double nominal,
double deviation )
{
this->Data = vals;
this->Nominal = nominal;
this->Deviation = deviation;
}
~SignedTableColumnDeviantFunctor() VTK_OVERRIDE { }
void operator() ( vtkDoubleArray* result,
vtkIdType id ) VTK_OVERRIDE
{
result->SetNumberOfValues( 1 );
result->SetValue( 0, ( this->Data->GetTuple1( id ) - this->Nominal ) / this->Deviation );
}
};
class UnsignedTableColumnDeviantFunctor : public TableColumnDeviantFunctor
{
public:
UnsignedTableColumnDeviantFunctor( vtkDataArray* vals,
double nominal,
double deviation )
{
this->Data = vals;
this->Nominal = nominal;
this->Deviation = deviation;
}
~UnsignedTableColumnDeviantFunctor() VTK_OVERRIDE { }
void operator() ( vtkDoubleArray* result,
vtkIdType id ) VTK_OVERRIDE
{
result->SetNumberOfValues( 1 );
result->SetValue( 0, fabs ( this->Data->GetTuple1( id ) - this->Nominal ) / this->Deviation );
}
};
// ----------------------------------------------------------------------
void vtkDescriptiveStatistics::SelectAssessFunctor( vtkTable* outData,
vtkDataObject* inMetaDO,
vtkStringArray* rowNames,
AssessFunctor*& dfunc )
{
dfunc = 0;
vtkMultiBlockDataSet* inMeta = vtkMultiBlockDataSet::SafeDownCast( inMetaDO );
if ( ! inMeta )
{
return;
}
vtkTable* primaryTab= vtkTable::SafeDownCast( inMeta->GetBlock( 0 ) );
if ( ! primaryTab )
{
return;
}
vtkTable* derivedTab = vtkTable::SafeDownCast( inMeta->GetBlock( 1 ) );
if ( ! derivedTab )
{
return;
}
vtkIdType nRowPrim = primaryTab->GetNumberOfRows();
if ( nRowPrim != derivedTab->GetNumberOfRows() )
{
return;
}
vtkStdString varName = rowNames->GetValue( 0 );
// Downcast meta columns to string arrays for efficient data access
vtkStringArray* vars = vtkArrayDownCast<vtkStringArray>( primaryTab->GetColumnByName( "Variable" ) );
if ( ! vars )
{
return;
}
// Loop over primary statistics table until the requested variable is found
for ( int r = 0; r < nRowPrim; ++ r )
{
if ( vars->GetValue( r ) == varName )
{
// Grab the data for the requested variable
vtkAbstractArray* arr = outData->GetColumnByName( varName );
if ( ! arr )
{
return;
}
// For descriptive statistics, type must be convertible to DataArray
// E.g., StringArrays do not fit here
vtkDataArray* vals = vtkArrayDownCast<vtkDataArray>( arr );
if ( ! vals )
{
return;
}
// Fetch necessary value from primary model
double mean = primaryTab->GetValueByName( r, "Mean" ).ToDouble();
// Fetch necessary value from derived model
double stdv = derivedTab->GetValueByName( r, "Standard Deviation" ).ToDouble();
// NB: If derived values were specified (and not calculated by Derive)
// and are inconsistent, then incorrect assessments will be produced
if ( stdv < VTK_DBL_MIN )
{
dfunc = new ZedDeviationDeviantFunctor( vals, mean );
}
else
{
if ( this->GetSignedDeviations() )
{
dfunc = new SignedTableColumnDeviantFunctor( vals, mean, stdv );
}
else
{
dfunc = new UnsignedTableColumnDeviantFunctor( vals, mean, stdv );
}
}
return;
}
}
// If arrived here it means that the variable of interest was not found in the parameter table
}
|