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 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028
|
/*=========================================================================
Program: Visualization Toolkit
Module: vtkOrderStatistics.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 "vtkOrderStatistics.h"
#include "vtkStatisticsAlgorithmPrivate.h"
#include "vtkDoubleArray.h"
#include "vtkIdTypeArray.h"
#include "vtkInformation.h"
#include "vtkIntArray.h"
#include "vtkObjectFactory.h"
#include "vtkMath.h"
#include "vtkMultiBlockDataSet.h"
#include "vtkStringArray.h"
#include "vtkTable.h"
#include "vtkVariantArray.h"
#include <cstdlib>
#include <vector>
#include <map>
#include <set>
vtkStandardNewMacro(vtkOrderStatistics);
// ----------------------------------------------------------------------
vtkOrderStatistics::vtkOrderStatistics()
{
this->QuantileDefinition = vtkOrderStatistics::InverseCDFAveragedSteps;
this->NumberOfIntervals = 4; // By default, calculate 5-points statistics
this->Quantize = false; // By default, do not force quantization
this->MaximumHistogramSize = 1000; // A large value by default
// Number of primary tables is variable
this->NumberOfPrimaryTables = -1;
this->AssessNames->SetNumberOfValues( 1 );
this->AssessNames->SetValue( 0, "Quantile" );
}
// ----------------------------------------------------------------------
vtkOrderStatistics::~vtkOrderStatistics()
{
}
// ----------------------------------------------------------------------
void vtkOrderStatistics::PrintSelf( ostream &os, vtkIndent indent )
{
this->Superclass::PrintSelf( os, indent );
os << indent << "NumberOfIntervals: " << this->NumberOfIntervals << endl;
os << indent << "QuantileDefinition: " << this->QuantileDefinition << endl;
os << indent << "Quantize: " << this->Quantize << endl;
os << indent << "MaximumHistogramSize: " << this->MaximumHistogramSize << endl;
}
// ----------------------------------------------------------------------
void vtkOrderStatistics::SetQuantileDefinition( int qd )
{
switch ( qd )
{
case vtkOrderStatistics::InverseCDF:
break;
case vtkOrderStatistics::InverseCDFAveragedSteps:
break;
default:
vtkWarningMacro( "Incorrect type of quantile definition: "
<<qd
<<". Ignoring it." );
return;
}
this->QuantileDefinition = static_cast<vtkOrderStatistics::QuantileDefinitionType>( qd );
this->Modified();
return;
}
// ----------------------------------------------------------------------
bool vtkOrderStatistics::SetParameter( const char* parameter,
int vtkNotUsed( index ),
vtkVariant value )
{
if ( ! strcmp( parameter, "NumberOfIntervals" ) )
{
this->SetNumberOfIntervals( value.ToInt() );
return true;
}
if ( ! strcmp( parameter, "QuantileDefinition" ) )
{
this->SetQuantileDefinition( value.ToInt() );
return true;
}
return false;
}
// ----------------------------------------------------------------------
void vtkOrderStatistics::Learn( vtkTable* inData,
vtkTable* vtkNotUsed( inParameters ),
vtkMultiBlockDataSet* outMeta )
{
if ( ! inData )
{
return;
}
if ( ! outMeta )
{
return;
}
// Loop over requests
vtkIdType nRow = inData->GetNumberOfRows();
for ( std::set<std::set<vtkStdString> >::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 col = *it;
if ( ! inData->GetColumnByName( col ) )
{
vtkWarningMacro( "InData table does not have a column "
<< col.c_str()
<< ". Ignoring it." );
continue;
}
// Get hold of data for this variable
vtkAbstractArray* vals = inData->GetColumnByName( col );
// Create histogram table for this variable
vtkTable* histogramTab = vtkTable::New();
// Row to be used to insert into histogram table
vtkVariantArray* row = vtkVariantArray::New();
row->SetNumberOfValues( 2 );
// Switch depending on data type
if ( vals->IsA("vtkDataArray") )
{
vtkDoubleArray* doubleCol = vtkDoubleArray::New();
doubleCol->SetName( "Value" );
histogramTab->AddColumn( doubleCol );
doubleCol->Delete();
}
else if ( vals->IsA("vtkStringArray") )
{
vtkStringArray* stringCol = vtkStringArray::New();
stringCol->SetName( "Value" );
histogramTab->AddColumn( stringCol );
stringCol->Delete();
}
else if ( vals->IsA("vtkVariantArray") )
{
vtkVariantArray* variantCol = vtkVariantArray::New();
variantCol->SetName( "Value" );
histogramTab->AddColumn( variantCol );
variantCol->Delete();
}
else
{
vtkWarningMacro( "Unsupported data type for column "
<< col.c_str()
<< ". Ignoring it." );
continue;
}
vtkIdTypeArray* idTypeCol = vtkIdTypeArray::New();
idTypeCol->SetName( "Cardinality" );
histogramTab->AddColumn( idTypeCol );
idTypeCol->Delete();
// Switch depending on data type
if ( vals->IsA("vtkDataArray") )
{
// Downcast column to data array for efficient data access
vtkDataArray* dvals = vtkArrayDownCast<vtkDataArray>( vals );
// Calculate histogram
std::map<double,vtkIdType> histogram;
for ( vtkIdType r = 0; r < nRow; ++ r )
{
++ histogram[dvals->GetTuple1( r )];
}
// If maximum size was requested, make sure it is satisfied
if ( this->Quantize )
{
// Retrieve achieved histogram size
vtkIdType Nq = histogram.size();
// If histogram is too big, quantization will have to occur
while ( Nq > this->MaximumHistogramSize )
{
// Retrieve extremal values
double mini = histogram.begin()->first;
double maxi = histogram.rbegin()->first;
// Create bucket width based on target histogram size
// FIXME: .5 is arbitrary at this point
double width = ( maxi - mini ) / vtkMath::Round( Nq / 2. );
// Now re-calculate histogram by quantizing values
histogram.clear();
double reading;
double quantum;
for ( vtkIdType r = 0; r < nRow; ++ r )
{
reading = dvals->GetTuple1( r );
quantum = mini + vtkMath::Round( ( reading - mini ) / width ) * width;
++ histogram[quantum];
}
// Update histogram size for conditional clause
Nq = histogram.size();
}
}
// Store histogram
for ( std::map<double,vtkIdType>::iterator mit = histogram.begin();
mit != histogram.end(); ++ mit )
{
row->SetValue( 0, mit->first );
row->SetValue( 1, mit->second );
histogramTab->InsertNextRow( row );
}
} // if ( vals->IsA("vtkDataArray") )
else if ( vals->IsA("vtkStringArray") )
{
// Downcast column to string array for efficient data access
vtkStringArray* svals = vtkArrayDownCast<vtkStringArray>( vals );
// Calculate histogram
std::map<vtkStdString,vtkIdType> histogram;
for ( vtkIdType r = 0; r < nRow; ++ r )
{
++ histogram[svals->GetValue( r )];
}
// Store histogram
for ( std::map<vtkStdString,vtkIdType>::iterator mit = histogram.begin();
mit != histogram.end(); ++ mit )
{
row->SetValue( 0, mit->first );
row->SetValue( 1, mit->second );
histogramTab->InsertNextRow( row );
}
} // else if ( vals->IsA("vtkStringArray") )
else if ( vals->IsA("vtkVariantArray") )
{
// Downcast column to variant array for efficient data access
vtkVariantArray* vvals = vtkArrayDownCast<vtkVariantArray>( vals );
// Calculate histogram
std::map<vtkVariant,vtkIdType> histogram;
for ( vtkIdType r = 0; r < nRow; ++ r )
{
++ histogram[vvals->GetVariantValue( r )];
}
// Store histogram
for ( std::map<vtkVariant,vtkIdType>::iterator mit = histogram.begin();
mit != histogram.end(); ++ mit )
{
row->SetValue( 0, mit->first );
row->SetValue( 1, mit->second );
histogramTab->InsertNextRow( row );
}
} // else if ( vals->IsA("vtkVariantArray") )
else
{
vtkWarningMacro( "Unsupported data type for column "
<< col.c_str()
<< ". Ignoring it." );
continue;
} // else
// Resize output meta so histogram table can be appended
unsigned int nBlocks = outMeta->GetNumberOfBlocks();
outMeta->SetNumberOfBlocks( nBlocks + 1 );
outMeta->GetMetaData( nBlocks )->Set( vtkCompositeDataSet::NAME(), col );
outMeta->SetBlock( nBlocks, histogramTab );
// Clean up
histogramTab->Delete();
row->Delete();
} // rit
return;
}
// ----------------------------------------------------------------------
void vtkOrderStatistics::Derive( vtkMultiBlockDataSet* inMeta )
{
if ( ! inMeta || inMeta->GetNumberOfBlocks() < 1 )
{
return;
}
// Create cardinality table
vtkTable* cardinalityTab = vtkTable::New();
vtkStringArray* stringCol = vtkStringArray::New();
stringCol->SetName( "Variable" );
cardinalityTab->AddColumn( stringCol );
stringCol->Delete();
vtkIdTypeArray* idTypeCol = vtkIdTypeArray::New();
idTypeCol->SetName( "Cardinality" );
cardinalityTab->AddColumn( idTypeCol );
idTypeCol->Delete();
// Create quantile table
vtkTable* quantileTab = vtkTable::New();
stringCol = vtkStringArray::New();
stringCol->SetName( "Quantile" );
quantileTab->AddColumn( stringCol );
stringCol->Delete();
double dq = 1. / static_cast<double>( this->NumberOfIntervals );
for ( vtkIdType i = 0; i <= this->NumberOfIntervals; ++ i )
{
// Handle special case of quartiles and median for convenience
ldiv_t q = ldiv( i << 2, this->NumberOfIntervals );
if ( q.rem )
{
// General case
stringCol->InsertNextValue( vtkStdString( vtkVariant( i * dq ).ToString() + "-quantile" ).c_str() );
}
else
{
// Case where q is a multiple of 4
switch ( q.quot )
{
case 0:
stringCol->InsertNextValue( "Minimum" );
break;
case 1:
stringCol->InsertNextValue( "First Quartile" );
break;
case 2:
stringCol->InsertNextValue( "Median" );
break;
case 3:
stringCol->InsertNextValue( "Third Quartile" );
break;
case 4:
stringCol->InsertNextValue( "Maximum" );
break;
default:
stringCol->InsertNextValue( vtkStdString( vtkVariant( i * dq ).ToString() + "-quantile" ).c_str() );
break;
}
}
}
// Prepare row for insertion into cardinality table
vtkVariantArray* row = vtkVariantArray::New();
row->SetNumberOfValues( 2 );
// Iterate over primary tables
unsigned int nBlocks = inMeta->GetNumberOfBlocks();
for ( unsigned int b = 0; b < nBlocks; ++ b )
{
vtkTable* histogramTab = vtkTable::SafeDownCast( inMeta->GetBlock( b ) );
if ( ! histogramTab )
{
continue;
}
// Downcast columns to typed arrays for efficient data access
vtkAbstractArray* vals = histogramTab->GetColumnByName( "Value" );
vtkIdTypeArray* card = vtkArrayDownCast<vtkIdTypeArray>( histogramTab->GetColumnByName( "Cardinality" ) );
// The CDF will be used for quantiles calculation (effectively as a reverse look-up table)
vtkIdType nRowHist = histogramTab->GetNumberOfRows();
vtkIdType* cdf = new vtkIdType[nRowHist];
// Calculate variable cardinality and CDF
vtkIdType c;
vtkIdType n = 0;
for ( vtkIdType r = 0; r < nRowHist; ++ r )
{
// Update cardinality and CDF
c = card->GetValue( r );
n += c;
cdf[r] = n;
}
// Get block variable name
vtkStdString varName = inMeta->GetMetaData( b )->Get( vtkCompositeDataSet::NAME() );
// Store cardinality
row->SetValue( 0, varName );
row->SetValue( 1, n );
cardinalityTab->InsertNextRow( row );
// Find or create column of probability mass function of histogram table
vtkStdString probaName( "P" );
vtkDoubleArray* probaCol;
vtkAbstractArray* abstrCol = histogramTab->GetColumnByName( probaName );
if ( ! abstrCol )
{
probaCol = vtkDoubleArray::New();
probaCol->SetName( probaName );
probaCol->SetNumberOfTuples( nRowHist );
histogramTab->AddColumn( probaCol );
probaCol->Delete();
}
else
{
probaCol = vtkArrayDownCast<vtkDoubleArray>( abstrCol );
}
// Finally calculate and store probabilities
double inv_n = 1. / n;
double p;
for ( vtkIdType r = 0; r < nRowHist; ++ r )
{
c = card->GetValue( r );
p = inv_n * c;
probaCol->SetValue( r, p );
}
// Storage for quantile indices
std::vector<std::pair<vtkIdType,vtkIdType> > quantileIndices;
std::pair<vtkIdType,vtkIdType> qIdxPair;
// First quantile index is always 0 with no jump (corresponding to the first and the smallest value)
qIdxPair.first = 0;
qIdxPair.second = 0;
quantileIndices.push_back( qIdxPair );
// Calculate all interior quantiles (i.e. for 0 < k < q)
vtkIdType rank = 0;
double dh = n / static_cast<double>( this->NumberOfIntervals );
for ( vtkIdType k = 1; k < this->NumberOfIntervals; ++ k )
{
// Calculate np value
double np = k * dh;
// Calculate first quantile index
vtkIdType qIdx1;
if ( this->QuantileDefinition == vtkOrderStatistics::InverseCDFAveragedSteps )
{
qIdx1 = static_cast<vtkIdType>( vtkMath::Round( np ) );
}
else
{
qIdx1 = static_cast<vtkIdType>( ceil( np ) );
}
// Find rank of the entry where first quantile index is reached using the CDF
while ( qIdx1 > cdf[rank] )
{
++ rank;
if ( rank >= nRowHist )
{
vtkErrorMacro( "Inconsistent quantile table: at last rank "
<< rank
<< " the CDF is "
<< cdf[rank-1]
<<" < "
<< qIdx1
<< " the quantile index. Cannot derive model." );
return;
}
}
// Store rank in histogram of first quantile index
qIdxPair.first = rank;
// Decide whether midpoint interpolation will be used for this numeric type input
if ( this->QuantileDefinition == vtkOrderStatistics::InverseCDFAveragedSteps )
{
// Calculate second quantile index for mid-point interpolation
vtkIdType qIdx2 = static_cast<vtkIdType>( floor( np + 1. ) );
// If the two quantile indices differ find rank where second is reached
if ( qIdx1 != qIdx2 )
{
while ( qIdx2 > cdf[rank] )
{
++ rank;
if ( rank >= nRowHist )
{
vtkErrorMacro( "Inconsistent quantile table: at last rank "
<< rank
<< " the CDF is "
<< cdf[rank-1]
<<" < "
<< qIdx2
<< " the quantile index. Cannot derive model." );
return;
}
}
}
} // if ( this->QuantileDefinition == vtkOrderStatistics::InverseCDFAveragedSteps )
// Store rank in histogram of second quantile index
qIdxPair.second = rank;
// Store pair of ranks
quantileIndices.push_back( qIdxPair );
}
// Last quantile index is always cardinality with no jump (corresponding to the last and thus largest value)
qIdxPair.first = nRowHist - 1;
qIdxPair.second = nRowHist - 1;;
quantileIndices.push_back( qIdxPair );
// Finally prepare quantile values column depending on data type
if ( vals->IsA("vtkDataArray") )
{
// Downcast column to data array for efficient data access
vtkDataArray* dvals = vtkArrayDownCast<vtkDataArray>( vals );
// Create column for quantiles of the same type as the values
vtkDataArray* quantCol = vtkDataArray::CreateDataArray( dvals->GetDataType() );
quantCol->SetName( varName );
quantCol->SetNumberOfTuples( this->NumberOfIntervals + 1 );
quantileTab->AddColumn( quantCol );
quantCol->Delete();
// Decide whether midpoint interpolation will be used for this numeric type input
if ( this->QuantileDefinition == vtkOrderStatistics::InverseCDFAveragedSteps )
{
// Compute and store quantile values
vtkIdType k = 0;
for ( std::vector<std::pair<vtkIdType,vtkIdType> >::iterator qit = quantileIndices.begin();
qit != quantileIndices.end(); ++ qit, ++ k )
{
// Retrieve data values from rank into histogram and interpolate
double Qp = .5 * ( dvals->GetTuple1( qit->first )
+ dvals->GetTuple1( qit->second ) );
// Store quantile value
quantCol->SetTuple1( k, Qp );
} // qit
}
else // if ( this->QuantileDefinition == vtkOrderStatistics::InverseCDFAveragedSteps )
{
// Compute and store quantile values
vtkIdType k = 0;
for ( std::vector<std::pair<vtkIdType,vtkIdType> >::iterator qit = quantileIndices.begin();
qit != quantileIndices.end(); ++ qit, ++ k )
{
// Retrieve data value from rank into histogram
double Qp = dvals->GetTuple1( qit->first );
// Store quantile value
quantCol->SetTuple1( k, Qp );
} // qit
} // else ( this->QuantileDefinition == vtkOrderStatistics::InverseCDFAveragedSteps )
} // if ( vals->IsA("vtkDataArray") )
else if ( vals->IsA("vtkStringArray") )
{
// Downcast column to string array for efficient data access
vtkStringArray* svals = vtkArrayDownCast<vtkStringArray>( vals );
// Create column for quantiles of the same type as the values
vtkStringArray* quantCol = vtkStringArray::New();
quantCol->SetName( varName );
quantCol->SetNumberOfTuples( this->NumberOfIntervals + 1 );
quantileTab->AddColumn( quantCol );
quantCol->Delete();
// Compute and store quantile values
vtkIdType k = 0;
for ( std::vector<std::pair<vtkIdType,vtkIdType> >::iterator qit = quantileIndices.begin();
qit != quantileIndices.end(); ++ qit, ++ k )
{
// Retrieve data value from rank into histogram
vtkStdString Qp = svals->GetValue( qit->first );
// Store quantile value
quantCol->SetValue( k, Qp );
}
} // else if ( vals->IsA("vtkStringArray") )
else if ( vals->IsA("vtkVariantArray") )
{
// Downcast column to variant array for efficient data access
vtkVariantArray* vvals = vtkArrayDownCast<vtkVariantArray>( vals );
// Create column for quantiles of the same type as the values
vtkVariantArray* quantCol = vtkVariantArray::New();
quantCol->SetName( varName );
quantCol->SetNumberOfTuples( this->NumberOfIntervals + 1 );
quantileTab->AddColumn( quantCol );
quantCol->Delete();
// Compute and store quantile values
vtkIdType k = 0;
for ( std::vector<std::pair<vtkIdType,vtkIdType> >::iterator qit = quantileIndices.begin();
qit != quantileIndices.end(); ++ qit, ++ k )
{
// Retrieve data value from rank into histogram
vtkVariant Qp = vvals->GetValue( qit->first );
// Store quantile value
quantCol->SetValue( k, Qp );
}
} // else if ( vals->IsA("vtkVariantArray") )
else
{
vtkWarningMacro( "Unsupported data type for column "
<< varName.c_str()
<< ". Cannot calculate quantiles for it." );
continue;
} // else
// Clean up
delete [] cdf;
} // for ( unsigned int b = 0; b < nBlocks; ++ b )
// Resize output meta so cardinality and quantile tables can be appended
nBlocks = inMeta->GetNumberOfBlocks();
inMeta->SetNumberOfBlocks( nBlocks + 2 );
// Append cardinality table at block nBlocks
inMeta->GetMetaData( nBlocks )->Set( vtkCompositeDataSet::NAME(), "Cardinalities" );
inMeta->SetBlock( nBlocks, cardinalityTab );
// Increment number of blocks and append quantile table at the end
++ nBlocks;
inMeta->GetMetaData( nBlocks )->Set( vtkCompositeDataSet::NAME(), "Quantiles" );
inMeta->SetBlock( nBlocks, quantileTab );
// Clean up
row->Delete();
cardinalityTab->Delete();
quantileTab->Delete();
}
// ----------------------------------------------------------------------
void vtkOrderStatistics::Test( vtkTable* inData,
vtkMultiBlockDataSet* inMeta,
vtkTable* outMeta )
{
if ( ! inMeta )
{
return;
}
unsigned nBlocks = inMeta->GetNumberOfBlocks();
if ( nBlocks < 1 )
{
return;
}
vtkTable* quantileTab = vtkTable::SafeDownCast( inMeta->GetBlock( nBlocks - 1 ) );
if ( ! quantileTab
|| inMeta->GetMetaData( nBlocks - 1 )->Get( vtkCompositeDataSet::NAME() ) != vtkStdString( "Quantiles" ) )
{
return;
}
if ( ! outMeta )
{
return;
}
// Prepare columns for the test:
// 0: variable name
// 1: Maximum vertical distance between CDFs
// 2: Kolmogorov-Smirnov test statistic (the above times the square root of the cardinality)
// 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* distCol = vtkDoubleArray::New();
distCol->SetName( "Maximum Distance" );
vtkDoubleArray* statCol = vtkDoubleArray::New();
statCol->SetName( "Kolmogorov-Smirnov" );
// Prepare storage for quantiles and model CDFs
vtkIdType nQuant = quantileTab->GetNumberOfRows();
vtkStdString* quantiles = new vtkStdString[nQuant];
// Loop over requests
vtkIdType nRowData = inData->GetNumberOfRows();
double inv_nq = 1. / nQuant;
double inv_card = 1. / nRowData;
double sqrt_card = sqrt( static_cast<double>( nRowData ) );
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 quantile column that corresponds to the variable of the request
vtkAbstractArray* quantCol = quantileTab->GetColumnByName( varName );
if ( ! quantCol )
{
vtkWarningMacro( "Quantile table table does not have a column "
<< varName.c_str()
<< ". Ignoring it." );
continue;
}
// First iterate over all observations to calculate empirical PDF
typedef std::map<vtkStdString,double> CDF;
CDF cdfEmpirical;
for ( vtkIdType j = 0; j < nRowData; ++ j )
{
// Read observation and update PDF
cdfEmpirical
[inData->GetValueByName( j, varName ).ToString()] += inv_card;
}
// Now integrate to obtain empirical CDF
double sum = 0.;
for ( CDF::iterator cit = cdfEmpirical.begin(); cit != cdfEmpirical.end(); ++ cit )
{
sum += cit->second;
cit->second = sum;
}
// Sanity check: verify that empirical CDF = 1
if ( fabs( sum - 1. ) > 1.e-6 )
{
vtkWarningMacro( "Incorrect empirical CDF for variable:"
<< varName.c_str()
<< ". Ignoring it." );
continue;
}
// Retrieve quantiles to calculate model CDF and insert value into empirical CDF
for ( vtkIdType i = 0; i < nQuant; ++ i )
{
// Read quantile and update CDF
quantiles[i] = quantileTab->GetValueByName( i, varName ).ToString();
// Update empirical CDF if new value found (with unknown ECDF)
std::pair<CDF::iterator,bool> result
= cdfEmpirical.insert( std::pair<vtkStdString,double>( quantiles[i], -1 ) );
if ( result.second == true )
{
CDF::iterator eit = result.first;
// Check if new value has no predecessor, in which case CDF = 0
if ( eit == cdfEmpirical.begin() )
{
result.first->second = 0.;
}
else
{
-- eit;
result.first->second = eit->second;
}
}
}
// Iterate over all CDF jump values
int currentQ = 0;
double mcdf = 0.;
double Dmn = 0.;
for ( CDF::iterator cit = cdfEmpirical.begin(); cit != cdfEmpirical.end(); ++ cit )
{
// If observation is smaller than minimum then there is nothing to do
if ( cit->first >= quantiles[0] )
{
while ( currentQ < nQuant && cit->first >= quantiles[currentQ] )
{
++ currentQ;
}
// Calculate model CDF at observation
mcdf = currentQ * inv_nq;
}
// Calculate vertical distance between CDFs and update maximum if needed
double d = fabs( cit->second - mcdf );
if ( d > Dmn )
{
Dmn = d;
}
}
// Insert variable name and calculated Kolmogorov-Smirnov statistic
// NB: R will be invoked only once at the end for efficiency
nameCol->InsertNextValue( varName );
distCol->InsertNextTuple1( Dmn );
statCol->InsertNextTuple1( sqrt_card * Dmn );
} // rit
// Now, add the already prepared columns to the output table
outMeta->AddColumn( nameCol );
outMeta->AddColumn( distCol );
outMeta->AddColumn( statCol );
// Clean up
delete [] quantiles;
nameCol->Delete();
distCol->Delete();
statCol->Delete();
}
// ----------------------------------------------------------------------
class DataArrayQuantizer : public vtkStatisticsAlgorithm::AssessFunctor
{
public:
vtkDataArray* Data;
vtkDataArray* Quantiles;
DataArrayQuantizer( vtkAbstractArray* vals,
vtkAbstractArray* quantiles )
{
this->Data = vtkArrayDownCast<vtkDataArray>( vals );
this->Quantiles = vtkArrayDownCast<vtkDataArray>( quantiles );
}
~DataArrayQuantizer() VTK_OVERRIDE
{
}
void operator() ( vtkDoubleArray* result,
vtkIdType id ) VTK_OVERRIDE
{
result->SetNumberOfValues( 1 );
double dval = this->Data->GetTuple1( id );
if ( dval < this->Quantiles->GetTuple1( 0 ) )
{
// dval is smaller than lower bound
result->SetValue( 0, 0 );
return;
}
vtkIdType q = 1;
vtkIdType n = this->Quantiles->GetNumberOfTuples();
while ( q < n && dval > this->Quantiles->GetTuple1( q ) )
{
++ q;
}
result->SetValue( 0, q );
}
};
// ----------------------------------------------------------------------
class StringArrayQuantizer : public vtkStatisticsAlgorithm::AssessFunctor
{
public:
vtkStringArray* Data;
vtkStringArray* Quantiles;
StringArrayQuantizer( vtkAbstractArray* vals,
vtkAbstractArray* quantiles )
{
this->Data = vtkArrayDownCast<vtkStringArray>( vals );
this->Quantiles = vtkArrayDownCast<vtkStringArray>( quantiles );
}
~StringArrayQuantizer() VTK_OVERRIDE
{
}
void operator() ( vtkDoubleArray* result,
vtkIdType id ) VTK_OVERRIDE
{
result->SetNumberOfValues( 1 );
vtkStdString sval = this->Data->GetValue( id );
if ( sval < this->Quantiles->GetValue( 0 ) )
{
// sval is smaller than lower bound
result->SetValue( 0, 0 );
return;
}
vtkIdType q = 1;
vtkIdType n = this->Quantiles->GetNumberOfValues();
while ( q < n && sval > this->Quantiles->GetValue( q ) )
{
++ q;
}
result->SetValue( 0, q );
}
};
// ----------------------------------------------------------------------
class VariantArrayQuantizer : public vtkStatisticsAlgorithm::AssessFunctor
{
public:
vtkVariantArray* Data;
vtkVariantArray* Quantiles;
VariantArrayQuantizer( vtkAbstractArray* vals,
vtkAbstractArray* quantiles )
{
this->Data = vtkArrayDownCast<vtkVariantArray>( vals );
this->Quantiles = vtkArrayDownCast<vtkVariantArray>( quantiles );
}
~VariantArrayQuantizer() VTK_OVERRIDE
{
}
void operator() ( vtkDoubleArray* result,
vtkIdType id ) VTK_OVERRIDE
{
result->SetNumberOfValues( 1 );
vtkVariant vval = this->Data->GetValue( id );
if ( vval < this->Quantiles->GetValue( 0 ) )
{
// vval is smaller than lower bound
result->SetValue( 0, 0 );
return;
}
vtkIdType q = 1;
vtkIdType n = this->Quantiles->GetNumberOfValues();
while ( q < n && vval > this->Quantiles->GetValue( q ) )
{
++ q;
}
result->SetValue( 0, q );
}
};
// ----------------------------------------------------------------------
void vtkOrderStatistics::SelectAssessFunctor( vtkTable* outData,
vtkDataObject* inMetaDO,
vtkStringArray* rowNames,
AssessFunctor*& dfunc )
{
dfunc = 0;
vtkMultiBlockDataSet* inMeta = vtkMultiBlockDataSet::SafeDownCast( inMetaDO );
if ( ! inMeta )
{
return;
}
unsigned nBlocks = inMeta->GetNumberOfBlocks();
if ( nBlocks < 1 )
{
return;
}
vtkTable* quantileTab = vtkTable::SafeDownCast( inMeta->GetBlock( nBlocks - 1 ) );
if ( ! quantileTab
|| inMeta->GetMetaData( nBlocks - 1 )->Get( vtkCompositeDataSet::NAME() ) != vtkStdString( "Quantiles" ) )
{
return;
}
// Retrieve name of variable of the request
vtkStdString varName = rowNames->GetValue( 0 );
// Grab the data for the requested variable
vtkAbstractArray* vals = outData->GetColumnByName( varName );
if ( ! vals )
{
return;
}
// Find the quantile column that corresponds to the variable of the request
vtkAbstractArray* quantiles = quantileTab->GetColumnByName( varName );
if ( ! quantiles )
{
vtkWarningMacro( "Quantile table table does not have a column "
<< varName.c_str()
<< ". Ignoring it." );
return;
}
// Select assess functor depending on data and quantile type
if ( vals->IsA("vtkDataArray") && quantiles->IsA("vtkDataArray") )
{
dfunc = new DataArrayQuantizer( vals, quantiles );
}
else if ( vals->IsA("vtkStringArray") && quantiles->IsA("vtkStringArray") )
{
dfunc = new StringArrayQuantizer( vals, quantiles );
}
else if ( vals->IsA("vtkVariantArray") && quantiles->IsA("vtkVariantArray") )
{
dfunc = new VariantArrayQuantizer( vals, quantiles );
}
else
{
vtkWarningMacro( "Unsupported (data,quantiles) type for column "
<< varName.c_str()
<< ": data type is "
<< vals->GetClassName()
<< " and quantiles type is "
<< quantiles->GetClassName()
<< ". Ignoring it." );
}
}
|