| 1 |
/*========================================================================= |
| 2 |
|
| 3 |
|
| 4 |
Program: Advanced Normalization Tools |
| 5 |
|
| 6 |
Copyright (c) ConsortiumOfANTS. All rights reserved. |
| 7 |
See accompanying COPYING.txt or |
| 8 |
https://github.com/stnava/ANTs/blob/master/ANTSCopyright.txt |
| 9 |
for details. |
| 10 |
|
| 11 |
This software is distributed WITHOUT ANY WARRANTY; without even |
| 12 |
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 13 |
PURPOSE. See the above copyright notices for more information. |
| 14 |
|
| 15 |
=========================================================================*/ |
| 16 |
#include "itkMinimumMaximumImageFilter.h" |
| 17 |
#include "itkConnectedComponentImageFilter.h" |
| 18 |
#include "itkRelabelComponentImageFilter.h" |
| 19 |
#include "itkExtractImageFilter.h" |
| 20 |
#include <vnl/vnl_random.h> |
| 21 |
#include <vnl/vnl_trace.h> |
| 22 |
#include <vnl/algo/vnl_ldl_cholesky.h> |
| 23 |
#include <vnl/algo/vnl_qr.h> |
| 24 |
#include <vnl/algo/vnl_matrix_inverse.h> |
| 25 |
#include <vnl/algo/vnl_generalized_eigensystem.h> |
| 26 |
#include <ctime> |
| 27 |
#include "itkCSVNumericObjectFileWriter.h" |
| 28 |
#include "itkCSVArray2DDataObject.h" |
| 29 |
#include "itkCSVArray2DFileReader.h" |
| 30 |
#include "itkGradientMagnitudeRecursiveGaussianImageFilter.h" |
| 31 |
#include "itkLaplacianRecursiveGaussianImageFilter.h" |
| 32 |
#include "itkDiscreteGaussianImageFilter.h" |
| 33 |
#include "itkSurfaceImageCurvature.h" |
| 34 |
#include "itkImageFileWriter.h" |
| 35 |
#include "itkGradientAnisotropicDiffusionImageFilter.h" |
| 36 |
#include "ReadWriteData.h" |
| 37 |
|
| 38 |
namespace itk |
| 39 |
{ |
| 40 |
namespace ants |
| 41 |
{ |
| 42 |
template <typename TInputImage, typename TRealType> |
| 43 |
antsSCCANObject<TInputImage, TRealType>::antsSCCANObject() |
| 44 |
{ |
| 45 |
this->m_UseL1 = true; |
| 46 |
this->m_VecToMaskSize = 1; |
| 47 |
this->m_MinClusterSizeP = 1; |
| 48 |
this->m_MinClusterSizeQ = 1; |
| 49 |
this->m_KeptClusterSize = 0; |
| 50 |
this->m_Debug = false; |
| 51 |
this->m_MaxBasedThresholding = false; |
| 52 |
this->m_Silent = true; |
| 53 |
this->m_CorrelationForSignificanceTest = 0; |
| 54 |
this->m_SpecializationForHBM2011 = false; |
| 55 |
this->m_AlreadyWhitened = false; |
| 56 |
this->m_PinvTolerance = 1.e-6; |
| 57 |
this->m_PercentVarianceForPseudoInverse = 0.9; |
| 58 |
this->m_MaximumNumberOfIterations = 20; |
| 59 |
this->m_MaskImageP = nullptr; |
| 60 |
this->m_MaskImageQ = nullptr; |
| 61 |
this->m_MaskImageR = nullptr; |
| 62 |
this->m_KeepPositiveP = true; |
| 63 |
this->m_KeepPositiveQ = true; |
| 64 |
this->m_KeepPositiveR = true; |
| 65 |
this->m_FractionNonZeroP = 0.5; |
| 66 |
this->m_FractionNonZeroQ = 0.5; |
| 67 |
this->m_FractionNonZeroR = 0.5; |
| 68 |
this->m_ConvergenceThreshold = 1.e-6; |
| 69 |
this->m_Epsilon = 1.e-12; |
| 70 |
this->m_GetSmall = true; |
| 71 |
this->m_GradStep = 0.1; |
| 72 |
this->m_UseLongitudinalFormulation = 0; |
| 73 |
this->m_RowSparseness = 0; |
| 74 |
this->m_Smoother = 0; |
| 75 |
this->m_Covering = 0; |
| 76 |
this->m_PriorWeight = 0; |
| 77 |
} |
| 78 |
|
| 79 |
template <typename TInputImage, typename TRealType> |
| 80 |
typename TInputImage::Pointer |
| 81 |
antsSCCANObject<TInputImage, TRealType>::ConvertVariateToSpatialImage( |
| 82 |
typename antsSCCANObject<TInputImage, TRealType>::VectorType w_p, |
| 83 |
typename TInputImage::Pointer mask, |
| 84 |
bool threshold_at_zero) |
| 85 |
{ |
| 86 |
if (ImageDimension == 4) |
| 87 |
return this->ConvertVariateToSpatialImage4D(w_p, mask, threshold_at_zero); |
| 88 |
typename TInputImage::Pointer weights = TInputImage::New(); |
| 89 |
weights->SetOrigin(mask->GetOrigin()); |
| 90 |
weights->SetSpacing(mask->GetSpacing()); |
| 91 |
weights->SetRegions(mask->GetLargestPossibleRegion()); |
| 92 |
weights->SetDirection(mask->GetDirection()); |
| 93 |
weights->AllocateInitialized(); |
| 94 |
|
| 95 |
// overwrite weights with vector values; |
| 96 |
unsigned long vecind = 0; |
| 97 |
typedef itk::ImageRegionIteratorWithIndex<TInputImage> Iterator; |
| 98 |
Iterator mIter(mask, mask->GetLargestPossibleRegion()); |
| 99 |
for (mIter.GoToBegin(); !mIter.IsAtEnd(); ++mIter) |
| 100 |
{ |
| 101 |
if (mIter.Get() >= static_cast<typename TInputImage::PixelType>(0.5)) |
| 102 |
{ |
| 103 |
vecind++; |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
this->m_VecToMaskSize = |
| 108 |
static_cast<unsigned int>(static_cast<double>(w_p.size()) / static_cast<double>(vecind) + 0.5); |
| 109 |
RealType avgwt = 1.0 / static_cast<double>(this->m_VecToMaskSize); |
| 110 |
vecind = 0; |
| 111 |
for (unsigned int k = 0; k < this->m_VecToMaskSize; k++) // loop begin |
| 112 |
{ |
| 113 |
// unsigned long maskct = 0; |
| 114 |
for (mIter.GoToBegin(); !mIter.IsAtEnd(); ++mIter) |
| 115 |
{ |
| 116 |
if (mIter.Get() >= static_cast<typename TInputImage::PixelType>(0.5)) |
| 117 |
{ |
| 118 |
TRealType val = 0; |
| 119 |
if (vecind < w_p.size()) |
| 120 |
{ |
| 121 |
val = |
| 122 |
static_cast<TRealType>(w_p(vecind)) * avgwt + static_cast<RealType>(weights->GetPixel(mIter.GetIndex())); |
| 123 |
} |
| 124 |
else |
| 125 |
{ |
| 126 |
if (!this->m_Silent) |
| 127 |
std::cout << "vecind too large " << vecind << " vs " << w_p.size() << std::endl; |
| 128 |
if (!this->m_Silent) |
| 129 |
std::cout << " this is likely a mask problem --- exiting! " << std::endl; |
| 130 |
std::exception(); |
| 131 |
} |
| 132 |
if (threshold_at_zero && (std::fabs(val) > this->m_Epsilon)) |
| 133 |
{ |
| 134 |
weights->SetPixel(mIter.GetIndex(), 1); |
| 135 |
} |
| 136 |
else |
| 137 |
{ |
| 138 |
weights->SetPixel(mIter.GetIndex(), val); |
| 139 |
} |
| 140 |
vecind++; |
| 141 |
// if ( maskct % 289 == 0 ) if ( ! this->m_Silent ) std::cout << "k "<< k << " mval " << val << " w " << w_p( |
| 142 |
// vecind ) << std::endl; |
| 143 |
// maskct++; |
| 144 |
} |
| 145 |
else |
| 146 |
{ |
| 147 |
mIter.Set(0); |
| 148 |
} |
| 149 |
} |
| 150 |
} // loop end |
| 151 |
return weights; |
| 152 |
} |
| 153 |
|
| 154 |
|
| 155 |
template <typename TInputImage, typename TRealType> |
| 156 |
typename TInputImage::Pointer |
| 157 |
antsSCCANObject<TInputImage, TRealType>::ConvertVariateToSpatialImage4D( |
| 158 |
typename antsSCCANObject<TInputImage, TRealType>::VectorType w_p, |
| 159 |
typename TInputImage::Pointer mask, |
| 160 |
bool threshold_at_zero) |
| 161 |
{ |
| 162 |
typename TInputImage::Pointer weights = TInputImage::New(); |
| 163 |
weights->SetOrigin(mask->GetOrigin()); |
| 164 |
weights->SetSpacing(mask->GetSpacing()); |
| 165 |
weights->SetRegions(mask->GetLargestPossibleRegion()); |
| 166 |
weights->SetDirection(mask->GetDirection()); |
| 167 |
weights->AllocateInitialized(); |
| 168 |
|
| 169 |
typename RealImageTypeDminus1::Pointer maskdm1; |
| 170 |
typedef itk::ExtractImageFilter<TInputImage, RealImageTypeDminus1> ExtractFilterType; |
| 171 |
typename ExtractFilterType::Pointer extractFilter = ExtractFilterType::New(); |
| 172 |
extractFilter->SetInput(mask); |
| 173 |
extractFilter->SetDirectionCollapseToIdentity(); |
| 174 |
extractFilter->SetDirectionCollapseToSubmatrix(); |
| 175 |
typename ImageType::RegionType extractRegion = mask->GetLargestPossibleRegion(); |
| 176 |
extractRegion.SetSize(ImageDimension - 1, 0); |
| 177 |
extractRegion.SetIndex(ImageDimension - 1, 0); |
| 178 |
extractFilter->SetExtractionRegion(extractRegion); |
| 179 |
extractFilter->Update(); |
| 180 |
maskdm1 = extractFilter->GetOutput(); |
| 181 |
// ANTs::WriteImage<RealImageTypeDminus1>( maskdm1, "maskdm1.nii.gz" ); |
| 182 |
|
| 183 |
// overwrite weights with vector values; |
| 184 |
unsigned long vecind = 0; |
| 185 |
typedef itk::ImageRegionIteratorWithIndex<RealImageTypeDminus1> Iterator; |
| 186 |
Iterator mIter(maskdm1, maskdm1->GetLargestPossibleRegion()); |
| 187 |
for (mIter.GoToBegin(); !mIter.IsAtEnd(); ++mIter) |
| 188 |
{ |
| 189 |
if (mIter.Get() >= 0.5) |
| 190 |
{ |
| 191 |
vecind++; |
| 192 |
} |
| 193 |
} |
| 194 |
|
| 195 |
this->m_VecToMaskSize = |
| 196 |
static_cast<unsigned int>(static_cast<double>(w_p.size()) / static_cast<double>(vecind) + 0.5); |
| 197 |
// if ( ! this->m_Silent ) std::cout << " this->m_VecToMaskSize " << this->m_VecToMaskSize << " mask size " << |
| 198 |
// vecind << std::endl; |
| 199 |
vecind = 0; |
| 200 |
for (unsigned int k = 0; k < this->m_VecToMaskSize; k++) |
| 201 |
{ // loop begin |
| 202 |
for (mIter.GoToBegin(); !mIter.IsAtEnd(); ++mIter) |
| 203 |
{ |
| 204 |
if (mIter.Get() >= 0.5) |
| 205 |
{ |
| 206 |
typename RealImageTypeDminus1::IndexType mind = mIter.GetIndex(); |
| 207 |
typename ImageType::IndexType ind4d; |
| 208 |
for (unsigned int dim = 0; dim < ImageDimension - 1; dim++) |
| 209 |
ind4d[dim] = mind[dim]; |
| 210 |
ind4d[ImageDimension - 1] = k; |
| 211 |
TRealType val = 0; |
| 212 |
if (vecind < w_p.size()) |
| 213 |
{ |
| 214 |
val = w_p(vecind); |
| 215 |
} |
| 216 |
else |
| 217 |
{ |
| 218 |
if (!this->m_Silent) |
| 219 |
std::cout << "vecind too large " << vecind << " vs " << w_p.size() << std::endl; |
| 220 |
if (!this->m_Silent) |
| 221 |
std::cout << " this is likely a mask problem --- exiting! " << std::endl; |
| 222 |
std::exception(); |
| 223 |
} |
| 224 |
if (threshold_at_zero && (fabs(val) > this->m_Epsilon)) |
| 225 |
{ |
| 226 |
weights->SetPixel(ind4d, 1); |
| 227 |
} |
| 228 |
else |
| 229 |
{ |
| 230 |
weights->SetPixel(ind4d, val); |
| 231 |
} |
| 232 |
vecind++; |
| 233 |
} |
| 234 |
} |
| 235 |
} // loop end |
| 236 |
return weights; |
| 237 |
} |
| 238 |
|
| 239 |
|
| 240 |
template <typename TInputImage, typename TRealType> |
| 241 |
TRealType |
| 242 |
antsSCCANObject<TInputImage, TRealType>::CurvatureSparseness( |
| 243 |
typename antsSCCANObject<TInputImage, TRealType>::VectorType & x, |
| 244 |
TRealType sparsenessgoal, |
| 245 |
unsigned int maxit, |
| 246 |
typename TInputImage::Pointer mask) |
| 247 |
// , typename antsSCCANObject<TInputImage,TRealType>::MatrixType& A, |
| 248 |
// typename antsSCCANObject<TInputImage,TRealType>::VectorType& b, |
| 249 |
{ |
| 250 |
if (mask.IsNull()) |
| 251 |
{ |
| 252 |
return 0; |
| 253 |
} |
| 254 |
/** penalize by curvature */ |
| 255 |
VectorType signvec(x); |
| 256 |
RealType kappa = 20; |
| 257 |
RealType lastkappa = 20; |
| 258 |
unsigned int kkk = 0; |
| 259 |
// unsigned int zct = 0; |
| 260 |
RealType dkappa = 1; |
| 261 |
RealType sp = 0; |
| 262 |
bool notdone = true; |
| 263 |
unsigned int nzct = 0; |
| 264 |
|
| 265 |
while (notdone) |
| 266 |
{ |
| 267 |
kappa = 0; |
| 268 |
VectorType gradvec = this->ComputeVectorLaplacian(x, mask); |
| 269 |
nzct = 0; |
| 270 |
for (unsigned int kk = 0; kk < x.size(); kk++) |
| 271 |
{ |
| 272 |
RealType grad = (gradvec[kk]) * 0.5; |
| 273 |
if (((x[kk] > 0) && (signvec[kk] < 0)) || ((x[kk] < 0) && (signvec[kk] > 0))) |
| 274 |
{ |
| 275 |
x[kk] = 0; |
| 276 |
// zct++; |
| 277 |
} |
| 278 |
else if (itk::Math::abs(x[kk]) > 1.e-6) |
| 279 |
{ |
| 280 |
kappa += itk::Math::abs(grad); |
| 281 |
x[kk] = x[kk] + grad; |
| 282 |
nzct++; |
| 283 |
} |
| 284 |
else |
| 285 |
{ |
| 286 |
x[kk] = 0; |
| 287 |
// zct++; |
| 288 |
} |
| 289 |
} |
| 290 |
if (nzct > 0) |
| 291 |
{ |
| 292 |
kappa /= (RealType)nzct; |
| 293 |
} |
| 294 |
sp = (RealType)(x.size() - nzct) / x.size() * 100.0; |
| 295 |
// VectorType pp = A.transpose() * ( A * x ); |
| 296 |
// RealType err = ( pp - b ).one_norm() / x.size(); |
| 297 |
// kappa = err + this->m_FractionNonZeroP * ( 100 - sp); |
| 298 |
dkappa = lastkappa - kappa; |
| 299 |
lastkappa = kappa; |
| 300 |
if (kkk > maxit || sp > sparsenessgoal) |
| 301 |
{ |
| 302 |
notdone = false; |
| 303 |
} |
| 304 |
if (kkk < 2) |
| 305 |
{ |
| 306 |
notdone = true; |
| 307 |
} |
| 308 |
kkk++; |
| 309 |
// if ( ! notdone ) |
| 310 |
// if ( ! this->m_Silent ) std::cout << " Kappa " << kappa << " " << kkk << " sparseness " << sp << " dkap " << |
| 311 |
// dkappa << " nzct " |
| 312 |
// << nzct << std::endl; |
| 313 |
} |
| 314 |
|
| 315 |
VectorType gradvec = this->ComputeVectorGradMag(x, mask); |
| 316 |
// if ( ! this->m_Silent ) std::cout << " Kappa " << kappa << " " << kkk << " sparseness " << sp << " dkap " << |
| 317 |
// dkappa << " nzct " |
| 318 |
// << nzct << " GradNorm " << gradvec.two_norm() << std::endl; |
| 319 |
return gradvec.two_norm(); |
| 320 |
} |
| 321 |
|
| 322 |
template <typename TInputImage, typename TRealType> |
| 323 |
typename antsSCCANObject<TInputImage, TRealType>::VectorType |
| 324 |
antsSCCANObject<TInputImage, TRealType>::ClusterThresholdVariate( |
| 325 |
typename antsSCCANObject<TInputImage, TRealType>::VectorType & w_p, |
| 326 |
typename TInputImage::Pointer mask, |
| 327 |
unsigned int minclust) |
| 328 |
{ |
| 329 |
if (minclust <= 1 || mask.IsNull()) |
| 330 |
{ |
| 331 |
return w_p; |
| 332 |
} |
| 333 |
if (ImageDimension == 4) |
| 334 |
return this->ClusterThresholdVariate4D(w_p, mask, minclust); |
| 335 |
typedef unsigned long ULPixelType; |
| 336 |
typedef itk::Image<ULPixelType, ImageDimension> labelimagetype; |
| 337 |
typedef itk::ImageRegionIteratorWithIndex<ImageType> fIterator; |
| 338 |
typedef itk::ImageRegionIteratorWithIndex<labelimagetype> Iterator; |
| 339 |
typedef itk::ConnectedComponentImageFilter<TInputImage, labelimagetype> FilterType; |
| 340 |
typedef itk::RelabelComponentImageFilter<labelimagetype, labelimagetype> RelabelType; |
| 341 |
|
| 342 |
// we assume w_p has been thresholded by another function |
| 343 |
bool threshold_at_zero = true; |
| 344 |
typename TInputImage::Pointer image = this->ConvertVariateToSpatialImage(w_p, mask, threshold_at_zero); |
| 345 |
typename FilterType::Pointer filter = FilterType::New(); |
| 346 |
typename RelabelType::Pointer relabel = RelabelType::New(); |
| 347 |
filter->SetInput(image); |
| 348 |
filter->SetFullyConnected(0); |
| 349 |
relabel->SetInput(filter->GetOutput()); |
| 350 |
relabel->SetMinimumObjectSize(1); |
| 351 |
try |
| 352 |
{ |
| 353 |
relabel->Update(); |
| 354 |
} |
| 355 |
catch (const itk::ExceptionObject & excep) |
| 356 |
{ |
| 357 |
if (!this->m_Silent) |
| 358 |
std::cout << "Relabel: exception caught !" << std::endl; |
| 359 |
if (!this->m_Silent) |
| 360 |
std::cout << excep << std::endl; |
| 361 |
} |
| 362 |
|
| 363 |
Iterator vfIter(relabel->GetOutput(), relabel->GetOutput()->GetLargestPossibleRegion()); |
| 364 |
float maximum = relabel->GetNumberOfObjects(); |
| 365 |
std::vector<unsigned long> histogram((int)maximum + 1); |
| 366 |
for (int i = 0; i <= maximum; i++) |
| 367 |
{ |
| 368 |
histogram[i] = 0; |
| 369 |
} |
| 370 |
for (vfIter.GoToBegin(); !vfIter.IsAtEnd(); ++vfIter) |
| 371 |
{ |
| 372 |
float vox = vfIter.Get(); |
| 373 |
if (vox > 0) |
| 374 |
{ |
| 375 |
if (vox > 0) |
| 376 |
{ |
| 377 |
histogram[(unsigned long)vox] = histogram[(unsigned long)vox] + 1; |
| 378 |
} |
| 379 |
} |
| 380 |
} |
| 381 |
|
| 382 |
// get the largest component's size |
| 383 |
unsigned long largest_component_size = 0; |
| 384 |
for (int i = 0; i <= maximum; i++) |
| 385 |
{ |
| 386 |
if (largest_component_size < histogram[i]) |
| 387 |
{ |
| 388 |
largest_component_size = histogram[i]; |
| 389 |
} |
| 390 |
} |
| 391 |
|
| 392 |
if (largest_component_size < minclust) |
| 393 |
{ |
| 394 |
minclust = largest_component_size - 1; |
| 395 |
} |
| 396 |
// now create the output vector |
| 397 |
// iterate through the image and set the voxels where countinlabel[(unsigned |
| 398 |
// long)(labelimage->GetPixel(vfIter.GetIndex()) - min)] |
| 399 |
// is < MinClusterSize |
| 400 |
unsigned long vecind = 0; // , keepct = 0; |
| 401 |
for (unsigned int k = 0; k < this->m_VecToMaskSize; k++) |
| 402 |
{ |
| 403 |
fIterator mIter(mask, mask->GetLargestPossibleRegion()); |
| 404 |
for (mIter.GoToBegin(); !mIter.IsAtEnd(); ++mIter) |
| 405 |
{ |
| 406 |
if (mIter.Get() > 0) |
| 407 |
{ |
| 408 |
float vox = mask->GetPixel(vfIter.GetIndex()); |
| 409 |
unsigned long clustersize = 0; |
| 410 |
if (vox >= 0) |
| 411 |
{ |
| 412 |
clustersize = histogram[(unsigned long)(relabel->GetOutput()->GetPixel(mIter.GetIndex()))]; |
| 413 |
if (clustersize > minclust) |
| 414 |
{ |
| 415 |
// keepct += 1; |
| 416 |
} // get clusters > minclust |
| 417 |
// if ( clustersize == largest_component_size ) { keepct++; } // get largest cluster |
| 418 |
else |
| 419 |
{ |
| 420 |
w_p(vecind) = 0; |
| 421 |
} |
| 422 |
vecind++; |
| 423 |
} |
| 424 |
} |
| 425 |
} |
| 426 |
} |
| 427 |
this->m_KeptClusterSize = histogram[1]; // only records the size of the largest cluster in the variate |
| 428 |
// for (unsigned int i=0; i<histogram.size(); i++) |
| 429 |
// if ( histogram[i] > minclust ) this->m_KeptClusterSize+=histogram[i]; |
| 430 |
// if ( ! this->m_Silent ) std::cout << " Cluster Threshold Kept % of sparseness " << ( |
| 431 |
// (float)keepct/(float)w_p.size() ) / |
| 432 |
// this->m_FractionNonZeroP << " kept clust size " << keepct << std::endl; |
| 433 |
return w_p; |
| 434 |
} |
| 435 |
|
| 436 |
|
| 437 |
template <typename TInputImage, typename TRealType> |
| 438 |
typename antsSCCANObject<TInputImage, TRealType>::VectorType |
| 439 |
antsSCCANObject<TInputImage, TRealType>::ClusterThresholdVariate4D( |
| 440 |
typename antsSCCANObject<TInputImage, TRealType>::VectorType & w_p, |
| 441 |
typename TInputImage::Pointer mask, |
| 442 |
unsigned int minclust) |
| 443 |
{ |
| 444 |
if (minclust <= 1 || mask.IsNull()) |
| 445 |
{ |
| 446 |
return w_p; |
| 447 |
} |
| 448 |
typedef unsigned long ULPixelType; |
| 449 |
typedef itk::Image<ULPixelType, ImageDimension> labelimagetype; |
| 450 |
typedef itk::ImageRegionIteratorWithIndex<labelimagetype> Iterator; |
| 451 |
typedef itk::ConnectedComponentImageFilter<TInputImage, labelimagetype> FilterType; |
| 452 |
typedef itk::RelabelComponentImageFilter<labelimagetype, labelimagetype> RelabelType; |
| 453 |
|
| 454 |
typedef itk::ImageRegionIteratorWithIndex<RealImageTypeDminus1> fIterator; |
| 455 |
typename RealImageTypeDminus1::Pointer maskdm1; |
| 456 |
typedef itk::ExtractImageFilter<TInputImage, RealImageTypeDminus1> ExtractFilterType; |
| 457 |
typename ExtractFilterType::Pointer extractFilter = ExtractFilterType::New(); |
| 458 |
extractFilter->SetInput(mask); |
| 459 |
extractFilter->SetDirectionCollapseToIdentity(); |
| 460 |
extractFilter->SetDirectionCollapseToSubmatrix(); |
| 461 |
typename ImageType::RegionType extractRegion = mask->GetLargestPossibleRegion(); |
| 462 |
extractRegion.SetSize(ImageDimension - 1, 0); |
| 463 |
extractRegion.SetIndex(ImageDimension - 1, 0); |
| 464 |
extractFilter->SetExtractionRegion(extractRegion); |
| 465 |
extractFilter->Update(); |
| 466 |
maskdm1 = extractFilter->GetOutput(); |
| 467 |
|
| 468 |
// we assume w_p has been thresholded by another function |
| 469 |
bool threshold_at_zero = true; |
| 470 |
typename TInputImage::Pointer image = this->ConvertVariateToSpatialImage(w_p, mask, threshold_at_zero); |
| 471 |
typename FilterType::Pointer filter = FilterType::New(); |
| 472 |
typename RelabelType::Pointer relabel = RelabelType::New(); |
| 473 |
filter->SetInput(image); |
| 474 |
filter->SetFullyConnected(0); |
| 475 |
relabel->SetInput(filter->GetOutput()); |
| 476 |
relabel->SetMinimumObjectSize(1); |
| 477 |
try |
| 478 |
{ |
| 479 |
relabel->Update(); |
| 480 |
} |
| 481 |
catch (const itk::ExceptionObject & excep) |
| 482 |
{ |
| 483 |
if (!this->m_Silent) |
| 484 |
std::cout << "Relabel: exception caught !" << std::endl; |
| 485 |
if (!this->m_Silent) |
| 486 |
std::cout << excep << std::endl; |
| 487 |
} |
| 488 |
|
| 489 |
Iterator vfIter(relabel->GetOutput(), relabel->GetOutput()->GetLargestPossibleRegion()); |
| 490 |
float maximum = relabel->GetNumberOfObjects(); |
| 491 |
std::vector<unsigned long> histogram((int)maximum + 1); |
| 492 |
for (int i = 0; i <= maximum; i++) |
| 493 |
{ |
| 494 |
histogram[i] = 0; |
| 495 |
} |
| 496 |
for (vfIter.GoToBegin(); !vfIter.IsAtEnd(); ++vfIter) |
| 497 |
{ |
| 498 |
float vox = vfIter.Get(); |
| 499 |
if (vox > 0) |
| 500 |
{ |
| 501 |
if (vox > 0) |
| 502 |
{ |
| 503 |
histogram[(unsigned long)vox] = histogram[(unsigned long)vox] + 1; |
| 504 |
} |
| 505 |
} |
| 506 |
} |
| 507 |
|
| 508 |
// get the largest component's size |
| 509 |
unsigned long largest_component_size = 0; |
| 510 |
for (int i = 0; i <= maximum; i++) |
| 511 |
{ |
| 512 |
if (largest_component_size < histogram[i]) |
| 513 |
{ |
| 514 |
largest_component_size = histogram[i]; |
| 515 |
} |
| 516 |
} |
| 517 |
|
| 518 |
if (largest_component_size < minclust) |
| 519 |
{ |
| 520 |
minclust = largest_component_size - 1; |
| 521 |
} |
| 522 |
|
| 523 |
// now create the output vector |
| 524 |
// iterate through the image and set the voxels where countinlabel[(unsigned |
| 525 |
// long)(labelimage->GetPixel(vfIter.GetIndex()) - min)] |
| 526 |
// is < MinClusterSize |
| 527 |
unsigned long vecind = 0; |
| 528 |
for (unsigned int k = 0; k < this->m_VecToMaskSize; k++) |
| 529 |
{ |
| 530 |
fIterator mIter(maskdm1, maskdm1->GetLargestPossibleRegion()); |
| 531 |
for (mIter.GoToBegin(); !mIter.IsAtEnd(); ++mIter) |
| 532 |
{ |
| 533 |
if (mIter.Get() >= 0.5) |
| 534 |
{ |
| 535 |
typename RealImageTypeDminus1::IndexType mind = mIter.GetIndex(); |
| 536 |
typename ImageType::IndexType ind4d; |
| 537 |
for (unsigned int dim = 0; dim < ImageDimension - 1; dim++) |
| 538 |
ind4d[dim] = mind[dim]; |
| 539 |
ind4d[ImageDimension - 1] = k; |
| 540 |
unsigned long clustersize = 0; |
| 541 |
clustersize = histogram[(unsigned long)(relabel->GetOutput()->GetPixel(ind4d))]; |
| 542 |
if (clustersize < minclust) |
| 543 |
{ |
| 544 |
w_p(vecind) = 0; |
| 545 |
} |
| 546 |
vecind++; |
| 547 |
} |
| 548 |
} |
| 549 |
} // loop |
| 550 |
this->m_KeptClusterSize = histogram[1]; // only records the size of the largest cluster in the variate |
| 551 |
// if ( ! this->m_Silent ) std::cout << " w_p out " << w_p.two_norm() << " hist " << histogram[1] << " nz " << |
| 552 |
// this->CountNonZero( w_p ) << std::endl; |
| 553 |
return w_p; |
| 554 |
} |
| 555 |
|
| 556 |
|
| 557 |
template <typename TInputImage, typename TRealType> |
| 558 |
typename antsSCCANObject<TInputImage, TRealType>::VectorType |
| 559 |
antsSCCANObject<TInputImage, TRealType>::ConvertImageToVariate(typename TInputImage::Pointer image, |
| 560 |
typename TInputImage::Pointer mask) |
| 561 |
{ |
| 562 |
typedef unsigned long ULPixelType; |
| 563 |
typedef itk::ImageRegionIteratorWithIndex<ImageType> Iterator; |
| 564 |
if (ImageDimension == 4) |
| 565 |
return this->ConvertImageToVariate4D(image, mask); |
| 566 |
|
| 567 |
ULPixelType maskct = 0; |
| 568 |
Iterator vfIter(mask, mask->GetLargestPossibleRegion()); |
| 569 |
for (vfIter.GoToBegin(); !vfIter.IsAtEnd(); ++vfIter) |
| 570 |
{ |
| 571 |
RealType maskval = vfIter.Get(); |
| 572 |
if (maskval >= 0.5) |
| 573 |
{ |
| 574 |
maskct++; |
| 575 |
} |
| 576 |
} |
| 577 |
|
| 578 |
VectorType vec(maskct * this->m_VecToMaskSize); |
| 579 |
vec.fill(0); |
| 580 |
ULPixelType maskct2 = 0; |
| 581 |
for (unsigned int k = 0; k < this->m_VecToMaskSize; k++) // loop begin |
| 582 |
{ |
| 583 |
// ULPixelType maskctbase = 0; |
| 584 |
for (vfIter.GoToBegin(); !vfIter.IsAtEnd(); ++vfIter) |
| 585 |
{ |
| 586 |
RealType maskval = vfIter.Get(); |
| 587 |
RealType imageval = image->GetPixel(vfIter.GetIndex()); |
| 588 |
if (maskval >= 0.5) |
| 589 |
{ |
| 590 |
vec[maskct2] = imageval; |
| 591 |
maskct2++; |
| 592 |
// maskctbase++; |
| 593 |
} |
| 594 |
} |
| 595 |
} |
| 596 |
return vec; |
| 597 |
} |
| 598 |
|
| 599 |
template <typename TInputImage, typename TRealType> |
| 600 |
typename antsSCCANObject<TInputImage, TRealType>::VectorType |
| 601 |
antsSCCANObject<TInputImage, TRealType>::ConvertImageToVariate4D(typename TInputImage::Pointer image, |
| 602 |
typename TInputImage::Pointer mask) |
| 603 |
{ |
| 604 |
typedef unsigned long ULPixelType; |
| 605 |
typename RealImageTypeDminus1::Pointer maskdm1; |
| 606 |
typedef itk::ExtractImageFilter<TInputImage, RealImageTypeDminus1> ExtractFilterType; |
| 607 |
typename ExtractFilterType::Pointer extractFilter = ExtractFilterType::New(); |
| 608 |
extractFilter->SetInput(mask); |
| 609 |
extractFilter->SetDirectionCollapseToIdentity(); |
| 610 |
extractFilter->SetDirectionCollapseToSubmatrix(); |
| 611 |
typename ImageType::RegionType extractRegion = mask->GetLargestPossibleRegion(); |
| 612 |
extractRegion.SetSize(ImageDimension - 1, 0); |
| 613 |
extractRegion.SetIndex(ImageDimension - 1, 0); |
| 614 |
extractFilter->SetExtractionRegion(extractRegion); |
| 615 |
extractFilter->Update(); |
| 616 |
maskdm1 = extractFilter->GetOutput(); |
| 617 |
|
| 618 |
ULPixelType maskct = 0; |
| 619 |
typedef itk::ImageRegionIteratorWithIndex<RealImageTypeDminus1> Iterator; |
| 620 |
Iterator vfIter(maskdm1, maskdm1->GetLargestPossibleRegion()); |
| 621 |
for (vfIter.GoToBegin(); !vfIter.IsAtEnd(); ++vfIter) |
| 622 |
{ |
| 623 |
RealType maskval = vfIter.Get(); |
| 624 |
if (maskval >= 0.5) |
| 625 |
{ |
| 626 |
maskct++; |
| 627 |
} |
| 628 |
} |
| 629 |
|
| 630 |
VectorType vec(maskct * this->m_VecToMaskSize); |
| 631 |
if (this->m_Debug) |
| 632 |
if (!this->m_Silent) |
| 633 |
std::cout << "I2V maskct " << maskct << " VecToMaskSize " << this->m_VecToMaskSize << std::endl; |
| 634 |
vec.fill(0); |
| 635 |
ULPixelType maskct2 = 0; |
| 636 |
for (unsigned int k = 0; k < this->m_VecToMaskSize; k++) // loop begin |
| 637 |
{ |
| 638 |
// ULPixelType maskctbase = 0; |
| 639 |
for (vfIter.GoToBegin(); !vfIter.IsAtEnd(); ++vfIter) |
| 640 |
{ |
| 641 |
RealType maskval = vfIter.Get(); |
| 642 |
if (maskval >= 0.5) |
| 643 |
{ |
| 644 |
typename RealImageTypeDminus1::IndexType mind = vfIter.GetIndex(); |
| 645 |
typename ImageType::IndexType ind4d; |
| 646 |
for (unsigned int dim = 0; dim < ImageDimension - 1; dim++) |
| 647 |
ind4d[dim] = mind[dim]; |
| 648 |
ind4d[ImageDimension - 1] = k; |
| 649 |
vec[maskct2] = image->GetPixel(ind4d); |
| 650 |
maskct2++; |
| 651 |
// maskctbase++; |
| 652 |
} |
| 653 |
} |
| 654 |
} |
| 655 |
return vec; |
| 656 |
} |
| 657 |
|
| 658 |
|
| 659 |
template <typename TInputImage, typename TRealType> |
| 660 |
typename antsSCCANObject<TInputImage, TRealType>::VectorType |
| 661 |
antsSCCANObject<TInputImage, TRealType>::InitializeV(typename antsSCCANObject<TInputImage, TRealType>::MatrixType p, |
| 662 |
unsigned long seed) |
| 663 |
{ |
| 664 |
VectorType w_p(p.columns()); |
| 665 |
|
| 666 |
w_p.fill(0); |
| 667 |
for (unsigned int its = 0; its < 1; its++) |
| 668 |
{ |
| 669 |
vnl_random randgen(seed); /* use constant seed to prevent weirdness */ |
| 670 |
for (unsigned long i = 0; i < p.columns(); i++) |
| 671 |
{ |
| 672 |
if (seed > 0) |
| 673 |
{ |
| 674 |
w_p(i) = randgen.normal(); |
| 675 |
// w_p(i)=randgen.drand32(); |
| 676 |
} |
| 677 |
else |
| 678 |
{ |
| 679 |
w_p(i) = 1.0; |
| 680 |
} |
| 681 |
} |
| 682 |
} |
| 683 |
w_p = w_p / p.columns(); |
| 684 |
return w_p; |
| 685 |
} |
| 686 |
|
| 687 |
template <typename TInputImage, typename TRealType> |
| 688 |
typename antsSCCANObject<TInputImage, TRealType>::VectorType |
| 689 |
antsSCCANObject<TInputImage, TRealType>::SpatiallySmoothVector( |
| 690 |
typename antsSCCANObject<TInputImage, TRealType>::VectorType vec, |
| 691 |
typename TInputImage::Pointer mask, |
| 692 |
bool surface) |
| 693 |
{ |
| 694 |
if (mask.IsNull() || itk::Math::abs(this->m_Smoother) < 1.e-9) |
| 695 |
{ |
| 696 |
return vec; |
| 697 |
} |
| 698 |
RealType vecnorm = vec.two_norm(); |
| 699 |
ImagePointer image = this->ConvertVariateToSpatialImage(vec, mask, false); |
| 700 |
RealType spacingsize = 0; |
| 701 |
for (unsigned int d = 0; d < ImageDimension; d++) |
| 702 |
{ |
| 703 |
RealType sp = mask->GetSpacing()[d]; |
| 704 |
spacingsize += sp * sp; |
| 705 |
} |
| 706 |
spacingsize = sqrt(spacingsize); |
| 707 |
if (this->m_Smoother < 0.0) |
| 708 |
{ |
| 709 |
typedef itk::GradientAnisotropicDiffusionImageFilter<TInputImage, TInputImage> FilterType; |
| 710 |
typename FilterType::Pointer filter = FilterType::New(); |
| 711 |
filter->SetInput(image); |
| 712 |
filter->SetNumberOfIterations(itk::Math::abs(this->m_Smoother)); |
| 713 |
TRealType mytimestep = spacingsize / std::pow(2.0, static_cast<double>(ImageDimension + 1)); |
| 714 |
TRealType reftimestep = 0.5 / std::pow(2.0, static_cast<double>(ImageDimension + 1)); |
| 715 |
if (mytimestep > reftimestep) |
| 716 |
mytimestep = reftimestep; |
| 717 |
filter->SetTimeStep(mytimestep); |
| 718 |
filter->SetConductanceParameter(0.25); // might need to change this |
| 719 |
filter->Update(); |
| 720 |
VectorType gradvec = this->ConvertImageToVariate(filter->GetOutput(), mask); |
| 721 |
return gradvec; |
| 722 |
} |
| 723 |
if ((surface) && (ImageDimension == 3) && (false)) |
| 724 |
{ |
| 725 |
unsigned int sigma = (unsigned int)this->m_Smoother; |
| 726 |
if (this->m_Smoother > 0.0001) |
| 727 |
if (sigma < 1) |
| 728 |
sigma = 1; |
| 729 |
typedef itk::SurfaceImageCurvature<TInputImage> ParamType; |
| 730 |
typename ParamType::Pointer Parameterizer = ParamType::New(); |
| 731 |
Parameterizer->SetInputImage(mask); |
| 732 |
Parameterizer->SetFunctionImage(image); |
| 733 |
Parameterizer->SetNeighborhoodRadius(1); |
| 734 |
Parameterizer->SetSigma(1.0); |
| 735 |
Parameterizer->SetUseGeodesicNeighborhood(false); |
| 736 |
Parameterizer->SetUseLabel(false); |
| 737 |
Parameterizer->SetThreshold(0.5); |
| 738 |
Parameterizer->IntegrateFunctionOverSurface(true); |
| 739 |
for (unsigned int i = 0; i < sigma; i++) |
| 740 |
{ |
| 741 |
Parameterizer->IntegrateFunctionOverSurface(true); |
| 742 |
} |
| 743 |
VectorType svec = this->ConvertImageToVariate(Parameterizer->GetFunctionImage(), mask); |
| 744 |
return svec; |
| 745 |
} |
| 746 |
else |
| 747 |
{ |
| 748 |
typedef itk::DiscreteGaussianImageFilter<ImageType, ImageType> dgf; |
| 749 |
typename dgf::Pointer filter = dgf::New(); |
| 750 |
filter->SetUseImageSpacing(true); |
| 751 |
filter->SetVariance(this->m_Smoother * spacingsize); |
| 752 |
filter->SetMaximumError(.01f); |
| 753 |
filter->SetInput(image); |
| 754 |
filter->Update(); |
| 755 |
typename ImageType::Pointer imgout = filter->GetOutput(); |
| 756 |
// ANTs::WriteImage<ImageType>( imgout , "ccaout_s.nii.gz" ); |
| 757 |
VectorType gradvec = this->ConvertImageToVariate(imgout, mask); |
| 758 |
gradvec = gradvec * vecnorm / gradvec.two_norm(); |
| 759 |
// if ( ! this->m_Silent ) std::cout << ImageDimension << " gvec " << gradvec[1] << " " << gradvec[100] << " " |
| 760 |
// << gradvec[1000] << std::endl; |
| 761 |
return gradvec; |
| 762 |
} |
| 763 |
} |
| 764 |
|
| 765 |
template <typename TInputImage, typename TRealType> |
| 766 |
typename antsSCCANObject<TInputImage, TRealType>::VectorType |
| 767 |
antsSCCANObject<TInputImage, TRealType>::ComputeVectorLaplacian( |
| 768 |
typename antsSCCANObject<TInputImage, TRealType>::VectorType vec, |
| 769 |
typename TInputImage::Pointer mask) |
| 770 |
{ |
| 771 |
if (mask.IsNull()) |
| 772 |
{ |
| 773 |
return vec; |
| 774 |
} |
| 775 |
ImagePointer image = this->ConvertVariateToSpatialImage(vec, mask, false); |
| 776 |
|
| 777 |
typedef itk::LaplacianRecursiveGaussianImageFilter<ImageType, ImageType> dgf; |
| 778 |
typename dgf::Pointer filter = dgf::New(); |
| 779 |
RealType spacingsize = 0; |
| 780 |
for (unsigned int d = 0; d < ImageDimension; d++) |
| 781 |
{ |
| 782 |
RealType sp = mask->GetSpacing()[d]; |
| 783 |
spacingsize += sp * sp; |
| 784 |
} |
| 785 |
spacingsize = sqrt(spacingsize); |
| 786 |
filter->SetSigma(0.333 * spacingsize); |
| 787 |
filter->SetInput(image); |
| 788 |
filter->Update(); |
| 789 |
image = filter->GetOutput(); |
| 790 |
VectorType gradvec = this->ConvertImageToVariate(image, this->m_MaskImageP); |
| 791 |
return gradvec; |
| 792 |
} |
| 793 |
|
| 794 |
template <typename TInputImage, typename TRealType> |
| 795 |
typename antsSCCANObject<TInputImage, TRealType>::VectorType |
| 796 |
antsSCCANObject<TInputImage, TRealType>::ComputeVectorGradMag( |
| 797 |
typename antsSCCANObject<TInputImage, TRealType>::VectorType vec, |
| 798 |
typename TInputImage::Pointer mask) |
| 799 |
{ |
| 800 |
ImagePointer image = this->ConvertVariateToSpatialImage(vec, mask, false); |
| 801 |
|
| 802 |
typedef itk::GradientMagnitudeRecursiveGaussianImageFilter<ImageType, ImageType> dgf; |
| 803 |
typename dgf::Pointer filter = dgf::New(); |
| 804 |
RealType spacingsize = 0; |
| 805 |
for (unsigned int d = 0; d < ImageDimension; d++) |
| 806 |
{ |
| 807 |
RealType sp = mask->GetSpacing()[d]; |
| 808 |
spacingsize += sp * sp; |
| 809 |
} |
| 810 |
spacingsize = sqrt(spacingsize); |
| 811 |
filter->SetSigma(0.333 * spacingsize); |
| 812 |
filter->SetInput(image); |
| 813 |
filter->Update(); |
| 814 |
image = filter->GetOutput(); |
| 815 |
VectorType gradvec = this->ConvertImageToVariate(image, this->m_MaskImageP); |
| 816 |
return gradvec; |
| 817 |
} |
| 818 |
|
| 819 |
template <typename TInputImage, typename TRealType> |
| 820 |
typename antsSCCANObject<TInputImage, TRealType>::MatrixType |
| 821 |
antsSCCANObject<TInputImage, TRealType>::NormalizeMatrix(typename antsSCCANObject<TInputImage, TRealType>::MatrixType p, |
| 822 |
bool makepositive) |
| 823 |
{ |
| 824 |
return (p); |
| 825 |
MatrixType np(p.rows(), p.columns()); |
| 826 |
|
| 827 |
for (unsigned long i = 0; i < p.columns(); i++) |
| 828 |
{ |
| 829 |
VectorType wpcol = p.get_column(i); |
| 830 |
VectorType wpcol2 = wpcol - wpcol.mean(); |
| 831 |
double sd = wpcol2.squared_magnitude(); |
| 832 |
sd = sqrt(sd / (p.rows() - 1)); |
| 833 |
if (sd <= 0.0 && i == static_cast<unsigned long>(0)) |
| 834 |
{ |
| 835 |
if (!this->m_Silent) |
| 836 |
std::cout << " row " << i << " has zero variance --- exiting " << std::endl; |
| 837 |
std::exception(); |
| 838 |
} |
| 839 |
if (sd <= 0 && i > 0) |
| 840 |
{ |
| 841 |
if (!this->m_Silent) |
| 842 |
std::cout << " row " << i << " has zero variance --- copying the previous row " << std::endl; |
| 843 |
if (!this->m_Silent) |
| 844 |
std::cout << " the row is " << wpcol << std::endl; |
| 845 |
np.set_column(i, np.get_column(i - 1)); |
| 846 |
} |
| 847 |
else |
| 848 |
{ |
| 849 |
wpcol = wpcol2 / sd; |
| 850 |
np.set_column(i, wpcol); |
| 851 |
} |
| 852 |
} |
| 853 |
/** cast to a non-negative space */ |
| 854 |
if (makepositive) |
| 855 |
{ |
| 856 |
np = np - np.min_value(); |
| 857 |
} |
| 858 |
return np; |
| 859 |
} |
| 860 |
|
| 861 |
template <typename TInputImage, typename TRealType> |
| 862 |
typename antsSCCANObject<TInputImage, TRealType>::MatrixType |
| 863 |
antsSCCANObject<TInputImage, TRealType>::VNLPseudoInverse( |
| 864 |
typename antsSCCANObject<TInputImage, TRealType>::MatrixType rin, |
| 865 |
bool take_sqrt) |
| 866 |
{ |
| 867 |
double pinvTolerance = this->m_PinvTolerance; |
| 868 |
MatrixType dd = rin; |
| 869 |
unsigned int ss = dd.rows(); |
| 870 |
|
| 871 |
if (dd.rows() > dd.columns()) |
| 872 |
{ |
| 873 |
ss = dd.columns(); |
| 874 |
} |
| 875 |
vnl_svd<RealType> eig(dd, pinvTolerance); |
| 876 |
if (!take_sqrt) |
| 877 |
{ |
| 878 |
return eig.pinverse(ss); |
| 879 |
} |
| 880 |
else |
| 881 |
{ |
| 882 |
for (unsigned int j = 0; j < ss; j++) |
| 883 |
{ |
| 884 |
RealType eval = eig.W(j, j); |
| 885 |
if (eval > pinvTolerance) |
| 886 |
{ // FIXME -- check tolerances against matlab pinv |
| 887 |
eig.W(j, j) = 1 / (eval); // need eval for inv cov |
| 888 |
if (take_sqrt) |
| 889 |
{ |
| 890 |
eig.W(j, j) = 1 / sqrt(eval); |
| 891 |
} |
| 892 |
} |
| 893 |
else |
| 894 |
{ |
| 895 |
eig.W(j, j) = 0; |
| 896 |
} |
| 897 |
} |
| 898 |
/** there is a scaling problem with the pseudoinverse --- this is a cheap fix!! |
| 899 |
it is based on the theoretical frobenious norm of the inverse matrix */ |
| 900 |
MatrixType pinv = (eig.recompose()).transpose(); |
| 901 |
double a = sqrt(static_cast<double>(dd.rows())); |
| 902 |
double b = (pinv * dd).frobenius_norm(); |
| 903 |
pinv = pinv * a / b; |
| 904 |
return pinv; |
| 905 |
} |
| 906 |
} |
| 907 |
|
| 908 |
|
| 909 |
template <typename TInputImage, typename TRealType> |
| 910 |
void |
| 911 |
antsSCCANObject<TInputImage, TRealType>::SoftClustThreshold( |
| 912 |
typename antsSCCANObject<TInputImage, TRealType>::VectorType & v_in, |
| 913 |
TRealType soft_thresh, |
| 914 |
bool keep_positive, |
| 915 |
unsigned int clust, |
| 916 |
typename TInputImage::Pointer mask) |
| 917 |
{ |
| 918 |
// here , we apply the minimum threshold to the data. |
| 919 |
for (unsigned int i = 0; i < v_in.size(); i++) |
| 920 |
{ |
| 921 |
RealType val = v_in(i); |
| 922 |
if (keep_positive && val < 0) |
| 923 |
val = 0; |
| 924 |
// if ( keep_positive && val < 0 ) val *= ( -1 ); |
| 925 |
if (itk::Math::abs(val) < soft_thresh) |
| 926 |
{ |
| 927 |
v_in(i) = 0; |
| 928 |
} |
| 929 |
else |
| 930 |
{ |
| 931 |
v_in(i) = val; |
| 932 |
if (this->m_UseL1) |
| 933 |
{ |
| 934 |
if (v_in(i) > 0) |
| 935 |
v_in(i) = v_in(i) - soft_thresh; |
| 936 |
if (v_in(i) < 0) |
| 937 |
v_in(i) = v_in(i) + soft_thresh; |
| 938 |
} |
| 939 |
} |
| 940 |
} |
| 941 |
this->ClusterThresholdVariate(v_in, mask, clust); |
| 942 |
// if ( ! this->m_Silent ) std::cout <<" resoft " << this->CountNonZero( v_in ) << std::endl; |
| 943 |
return; |
| 944 |
} |
| 945 |
|
| 946 |
|
| 947 |
template <typename TInputImage, typename TRealType> |
| 948 |
void |
| 949 |
antsSCCANObject<TInputImage, TRealType>::ReSoftThreshold( |
| 950 |
typename antsSCCANObject<TInputImage, TRealType>::VectorType & v_in, |
| 951 |
TRealType fractional_goal, |
| 952 |
bool keep_positive) |
| 953 |
{ |
| 954 |
TRealType abs_fractional_goal = static_cast<TRealType>(std::fabs(fractional_goal)); |
| 955 |
if (abs_fractional_goal >= itk::NumericTraits<TRealType>::OneValue() || |
| 956 |
static_cast<TRealType>(v_in.size()) * abs_fractional_goal <= itk::NumericTraits<TRealType>::OneValue()) |
| 957 |
{ |
| 958 |
return; |
| 959 |
} |
| 960 |
RealType maxv = v_in.max_value(); |
| 961 |
if (static_cast<RealType>(std::fabs(v_in.min_value())) > maxv) |
| 962 |
{ |
| 963 |
maxv = static_cast<RealType>(std::fabs(v_in.min_value())); |
| 964 |
} |
| 965 |
RealType lambg = this->m_Epsilon; |
| 966 |
RealType frac = 0; |
| 967 |
unsigned int its = 0, ct = 0; |
| 968 |
RealType soft_thresh = lambg; |
| 969 |
for (unsigned int i = 0; i < v_in.size(); i++) |
| 970 |
{ |
| 971 |
if (keep_positive && v_in(i) < 0) |
| 972 |
{ |
| 973 |
v_in(i) = 0; |
| 974 |
} |
| 975 |
} |
| 976 |
|
| 977 |
RealType minthresh = 0, minfdiff = 1; |
| 978 |
unsigned int maxits = 1000; |
| 979 |
for (its = 0; its < maxits; its++) |
| 980 |
{ |
| 981 |
soft_thresh = static_cast<RealType>(its) / static_cast<RealType>(maxits) * maxv; |
| 982 |
ct = 0; |
| 983 |
for (unsigned int i = 0; i < v_in.size(); i++) |
| 984 |
{ |
| 985 |
RealType val = v_in(i); |
| 986 |
if (!keep_positive) |
| 987 |
{ |
| 988 |
val = fabs(val); |
| 989 |
} |
| 990 |
else if (val < 0) |
| 991 |
{ |
| 992 |
val = 0; |
| 993 |
} |
| 994 |
if (val < soft_thresh) |
| 995 |
{ |
| 996 |
ct++; |
| 997 |
} |
| 998 |
} |
| 999 |
frac = static_cast<RealType>(v_in.size() - ct) / static_cast<RealType>(v_in.size()); |
| 1000 |
// if ( ! this->m_Silent ) std::cout << " cur " << frac << " goal " << fractional_goal << " st " << soft_thresh |
| 1001 |
// << " th " << |
| 1002 |
// minthresh |
| 1003 |
// << std::endl; |
| 1004 |
if (fabs(frac - fractional_goal) < minfdiff) |
| 1005 |
{ |
| 1006 |
minthresh = soft_thresh; |
| 1007 |
minfdiff = fabs(frac - fractional_goal); |
| 1008 |
} |
| 1009 |
} |
| 1010 |
// if ( ! this->m_Silent ) std::cout << " goal " << fractional_goal << " st " << soft_thresh << " th " << minthresh |
| 1011 |
// << " minfdiff " |
| 1012 |
// << |
| 1013 |
// minfdiff << std::endl; |
| 1014 |
|
| 1015 |
// here , we apply the minimum threshold to the data. |
| 1016 |
ct = 0; |
| 1017 |
for (unsigned int i = 0; i < v_in.size(); i++) |
| 1018 |
{ |
| 1019 |
RealType val = v_in(i); |
| 1020 |
if (!keep_positive) |
| 1021 |
{ |
| 1022 |
val = fabs(val); |
| 1023 |
} |
| 1024 |
else if (val < 0) |
| 1025 |
{ |
| 1026 |
val = 0; |
| 1027 |
} |
| 1028 |
if (val < minthresh) |
| 1029 |
{ |
| 1030 |
v_in(i) = 0; |
| 1031 |
ct++; |
| 1032 |
} |
| 1033 |
else |
| 1034 |
{ |
| 1035 |
if (this->m_UseL1) |
| 1036 |
{ |
| 1037 |
v_in(i) = v_in(i) - minthresh; |
| 1038 |
} |
| 1039 |
} |
| 1040 |
} |
| 1041 |
// if ( ! this->m_Silent ) std::cout << " post minv " << tminv << " post maxv " << tmaxv << std::endl; |
| 1042 |
frac = (float)(v_in.size() - ct) / (float)v_in.size(); |
| 1043 |
// if ( ! this->m_Silent ) std::cout << " frac non-zero " << frac << " wanted " << fractional_goal << std::endl; |
| 1044 |
// if ( v_in.two_norm() > this->m_Epsilon ) v_in=v_in/v_in.two_norm(); |
| 1045 |
// if ( ! this->m_Silent ) std::cout << v_in <<std::endl; |
| 1046 |
return; |
| 1047 |
} |
| 1048 |
|
| 1049 |
template <typename TInputImage, typename TRealType> |
| 1050 |
void |
| 1051 |
antsSCCANObject<TInputImage, TRealType>::ConstantProbabilityThreshold( |
| 1052 |
typename antsSCCANObject<TInputImage, TRealType>::VectorType & v_in, |
| 1053 |
TRealType probability_goal, |
| 1054 |
bool keep_positive) |
| 1055 |
{ |
| 1056 |
bool debug = false; |
| 1057 |
|
| 1058 |
v_in = v_in / v_in.two_norm(); |
| 1059 |
VectorType v_out(v_in); |
| 1060 |
RealType maxv = v_in.max_value(); |
| 1061 |
if (fabs(v_in.min_value()) > maxv) |
| 1062 |
{ |
| 1063 |
maxv = fabs(v_in.min_value()); |
| 1064 |
} |
| 1065 |
RealType lambg = this->m_Epsilon; |
| 1066 |
RealType frac = 0; |
| 1067 |
unsigned int its = 0; |
| 1068 |
RealType probability = 0; |
| 1069 |
RealType probability_sum = 0; |
| 1070 |
RealType soft_thresh = lambg; |
| 1071 |
unsigned int nzct = 0; |
| 1072 |
for (unsigned int i = 0; i < v_in.size(); i++) |
| 1073 |
{ |
| 1074 |
if (keep_positive && v_in(i) < 0) |
| 1075 |
{ |
| 1076 |
v_in(i) = 0; |
| 1077 |
} |
| 1078 |
v_out(i) = v_in(i); |
| 1079 |
probability_sum += fabs(v_out(i)); |
| 1080 |
if (fabs(v_in(i)) > 0) |
| 1081 |
{ |
| 1082 |
nzct++; |
| 1083 |
} |
| 1084 |
} |
| 1085 |
if (debug) |
| 1086 |
{ |
| 1087 |
if (!this->m_Silent) |
| 1088 |
std::cout << " prob sum " << probability_sum << std::endl; |
| 1089 |
} |
| 1090 |
if (debug) |
| 1091 |
{ |
| 1092 |
if (!this->m_Silent) |
| 1093 |
std::cout << " nzct " << nzct << std::endl; |
| 1094 |
} |
| 1095 |
RealType minthresh = 0, minfdiff = 1; |
| 1096 |
unsigned int maxits = 1000; |
| 1097 |
for (its = 0; its < maxits; its++) |
| 1098 |
{ |
| 1099 |
soft_thresh = static_cast<RealType>(its) / static_cast<RealType>(maxits) * maxv; |
| 1100 |
probability = 0; |
| 1101 |
for (unsigned int i = 0; i < v_in.size(); i++) |
| 1102 |
{ |
| 1103 |
RealType val = v_in(i); |
| 1104 |
if (!keep_positive) |
| 1105 |
{ |
| 1106 |
val = fabs(val); |
| 1107 |
} |
| 1108 |
else if (val < 0) |
| 1109 |
{ |
| 1110 |
val = 0; |
| 1111 |
} |
| 1112 |
if (val < soft_thresh) |
| 1113 |
{ |
| 1114 |
v_out(i) = 0; |
| 1115 |
} |
| 1116 |
else |
| 1117 |
{ |
| 1118 |
v_out(i) = v_in(i); |
| 1119 |
probability += fabs(v_out(i)); |
| 1120 |
} |
| 1121 |
} |
| 1122 |
probability /= probability_sum; |
| 1123 |
if (debug) |
| 1124 |
{ |
| 1125 |
if (!this->m_Silent) |
| 1126 |
std::cout << " cur " << probability << " goal " << probability_goal << " st " << soft_thresh << " th " |
| 1127 |
<< minthresh << std::endl; |
| 1128 |
} |
| 1129 |
if (fabs(probability - probability_goal) < minfdiff) |
| 1130 |
{ |
| 1131 |
minthresh = soft_thresh; |
| 1132 |
minfdiff = fabs(probability - probability_goal); |
| 1133 |
} |
| 1134 |
} |
| 1135 |
|
| 1136 |
// here , we apply the minimum threshold to the data. |
| 1137 |
probability = 0; |
| 1138 |
unsigned long ct = 0; |
| 1139 |
for (unsigned int i = 0; i < v_in.size(); i++) |
| 1140 |
{ |
| 1141 |
RealType val = v_in(i); |
| 1142 |
if (!keep_positive) |
| 1143 |
{ |
| 1144 |
val = fabs(val); |
| 1145 |
} |
| 1146 |
else if (val < 0) |
| 1147 |
{ |
| 1148 |
val = 0; |
| 1149 |
} |
| 1150 |
if (val < minthresh) |
| 1151 |
{ |
| 1152 |
v_in(i) = 0; |
| 1153 |
ct++; |
| 1154 |
} |
| 1155 |
else |
| 1156 |
{ |
| 1157 |
// v_in(i)-=minthresh; |
| 1158 |
probability += fabs(v_in(i)); |
| 1159 |
} |
| 1160 |
} |
| 1161 |
if (debug) |
| 1162 |
{ |
| 1163 |
if (!this->m_Silent) |
| 1164 |
std::cout << " frac non-zero " << probability << " wanted " << probability_goal << " Keep+ " << keep_positive |
| 1165 |
<< std::endl; |
| 1166 |
} |
| 1167 |
frac = (float)(v_in.size() - ct) / (float)v_in.size(); |
| 1168 |
if (frac < 1) |
| 1169 |
{ |
| 1170 |
if (!this->m_Silent) |
| 1171 |
std::cout << " const prob " << probability / probability_sum << " sparseness " << frac << std::endl; |
| 1172 |
} |
| 1173 |
// if ( v_in.two_norm() > this->m_Epsilon ) v_in=v_in/v_in.sum(); |
| 1174 |
|
| 1175 |
return; |
| 1176 |
} |
| 1177 |
|
| 1178 |
template <typename TInputImage, typename TRealType> |
| 1179 |
typename antsSCCANObject<TInputImage, TRealType>::VectorType |
| 1180 |
antsSCCANObject<TInputImage, TRealType>::TrueCCAPowerUpdate( |
| 1181 |
TRealType penalty1, |
| 1182 |
typename antsSCCANObject<TInputImage, TRealType>::MatrixType p, |
| 1183 |
typename antsSCCANObject<TInputImage, TRealType>::VectorType w_q, |
| 1184 |
typename antsSCCANObject<TInputImage, TRealType>::MatrixType q, |
| 1185 |
bool keep_pos, |
| 1186 |
bool factorOutR) |
| 1187 |
{ |
| 1188 |
RealType norm = 0; |
| 1189 |
// recall that the matrices below have already be whitened .... |
| 1190 |
// we bracket the computation and use associativity to make sure its done efficiently |
| 1191 |
// vVector wpnew=( (CppInv.transpose()*p.transpose())*(CqqInv*q) )*w_q; |
| 1192 |
VectorType wpnew; |
| 1193 |
|
| 1194 |
if (factorOutR) |
| 1195 |
{ |
| 1196 |
VectorType temp = q * w_q; |
| 1197 |
wpnew = p.transpose() * (temp - this->m_MatrixRRt * temp); |
| 1198 |
} |
| 1199 |
else |
| 1200 |
{ |
| 1201 |
VectorType temp = q * w_q; |
| 1202 |
wpnew = p.transpose() * temp; |
| 1203 |
} |
| 1204 |
this->ReSoftThreshold(wpnew, penalty1, keep_pos); |
| 1205 |
norm = wpnew.two_norm(); |
| 1206 |
if (norm > this->m_Epsilon) |
| 1207 |
{ |
| 1208 |
wpnew = wpnew / (norm); |
| 1209 |
} |
| 1210 |
return wpnew; |
| 1211 |
} |
| 1212 |
|
| 1213 |
template <typename TInputImage, typename TRealType> |
| 1214 |
void |
| 1215 |
antsSCCANObject<TInputImage, TRealType>::UpdatePandQbyR() |
| 1216 |
{ |
| 1217 |
// R is already whitened |
| 1218 |
switch (this->m_SCCANFormulation) |
| 1219 |
{ |
| 1220 |
case PQ: |
| 1221 |
{ |
| 1222 |
// do nothing |
| 1223 |
} |
| 1224 |
break; |
| 1225 |
case PminusRQ: |
| 1226 |
{ |
| 1227 |
this->m_MatrixP = (this->m_MatrixP - this->m_MatrixRRt * this->m_MatrixP); |
| 1228 |
} |
| 1229 |
break; |
| 1230 |
case PQminusR: |
| 1231 |
{ |
| 1232 |
this->m_MatrixQ = (this->m_MatrixQ - this->m_MatrixRRt * this->m_MatrixQ); |
| 1233 |
} |
| 1234 |
break; |
| 1235 |
case PminusRQminusR: |
| 1236 |
{ |
| 1237 |
/** P_R = P - R_w R_w^T P */ |
| 1238 |
/** Q_R = Q - R_w R_w^T Q */ |
| 1239 |
this->m_MatrixP = (this->m_MatrixP - this->m_MatrixRRt * this->m_MatrixP); |
| 1240 |
this->m_MatrixQ = (this->m_MatrixQ - this->m_MatrixRRt * this->m_MatrixQ); |
| 1241 |
} |
| 1242 |
break; |
| 1243 |
case PQR: |
| 1244 |
{ |
| 1245 |
if (!this->m_Silent) |
| 1246 |
std::cout << " You should call mscca not pscca " << std::endl; |
| 1247 |
} |
| 1248 |
break; |
| 1249 |
} |
| 1250 |
} |
| 1251 |
|
| 1252 |
template <typename TInputImage, typename TRealType> |
| 1253 |
void |
| 1254 |
antsSCCANObject<TInputImage, TRealType>::RunDiagnostics(unsigned int n_vecs) |
| 1255 |
{ |
| 1256 |
if (!this->m_Silent) |
| 1257 |
std::cout << "Quantitative diagnostics: " << std::endl; |
| 1258 |
if (!this->m_Silent) |
| 1259 |
std::cout << "Type 1: correlation from canonical variate to confounding vector " << std::endl; |
| 1260 |
if (!this->m_Silent) |
| 1261 |
std::cout << "Type 2: correlation from canonical variate to canonical variate " << std::endl; |
| 1262 |
RealType corrthresh = 0.3; |
| 1263 |
MatrixType omatP = this->NormalizeMatrix(this->m_OriginalMatrixP); |
| 1264 |
MatrixType omatQ; |
| 1265 |
bool doq = true; |
| 1266 |
|
| 1267 |
if (this->m_OriginalMatrixQ.size() > 0) |
| 1268 |
{ |
| 1269 |
omatQ = this->NormalizeMatrix(this->m_OriginalMatrixQ); |
| 1270 |
} |
| 1271 |
else |
| 1272 |
{ |
| 1273 |
doq = false; |
| 1274 |
omatQ = omatP; |
| 1275 |
} |
| 1276 |
if (this->m_OriginalMatrixR.size() > 0) |
| 1277 |
{ |
| 1278 |
for (unsigned int wv = 0; wv < n_vecs; wv++) |
| 1279 |
{ |
| 1280 |
for (unsigned int col = 0; col < this->m_MatrixR.columns(); col++) |
| 1281 |
{ |
| 1282 |
RealType a = |
| 1283 |
this->PearsonCorr(omatP * this->m_VariatesP.get_column(wv), this->m_OriginalMatrixR.get_column(col)); |
| 1284 |
RealType b = |
| 1285 |
this->PearsonCorr(omatQ * this->m_VariatesQ.get_column(wv), this->m_OriginalMatrixR.get_column(col)); |
| 1286 |
// if ( ! this->m_Silent ) std::cout << "Pvec " << wv << " confound " << col << " : " << a <<std::endl; |
| 1287 |
// if ( ! this->m_Silent ) std::cout << "Qvec " << wv << " confound " << col << " : " << b <<std::endl; |
| 1288 |
if (fabs(a) > corrthresh && fabs(b) > corrthresh) |
| 1289 |
{ |
| 1290 |
if (!this->m_Silent) |
| 1291 |
std::cout << " correlation with confound too high for variate " << wv << " corrs " << a << " and " << b |
| 1292 |
<< std::endl; |
| 1293 |
// this->m_CanonicalCorrelations[wv]=0; |
| 1294 |
} |
| 1295 |
} |
| 1296 |
} |
| 1297 |
} |
| 1298 |
for (unsigned int wv = 0; wv < n_vecs; wv++) |
| 1299 |
{ |
| 1300 |
for (unsigned int yv = wv + 1; yv < n_vecs; yv++) |
| 1301 |
{ |
| 1302 |
RealType a = |
| 1303 |
this->PearsonCorr(omatP * this->m_VariatesP.get_column(wv), omatP * this->m_VariatesP.get_column(yv)); |
| 1304 |
if (fabs(a) > corrthresh) |
| 1305 |
{ |
| 1306 |
if (!this->m_Silent) |
| 1307 |
std::cout << " not orthogonal p " << a << std::endl; |
| 1308 |
// this->m_CanonicalCorrelations[yv]=0; |
| 1309 |
} |
| 1310 |
if (!this->m_Silent) |
| 1311 |
std::cout << "Pvec " << wv << " Pvec " << yv << " : " << a << std::endl; |
| 1312 |
if (doq) |
| 1313 |
{ |
| 1314 |
RealType b = |
| 1315 |
this->PearsonCorr(omatQ * this->m_VariatesQ.get_column(wv), omatQ * this->m_VariatesQ.get_column(yv)); |
| 1316 |
if (fabs(b) > corrthresh) |
| 1317 |
{ |
| 1318 |
if (!this->m_Silent) |
| 1319 |
std::cout << " not orthogonal q " << a << std::endl; |
| 1320 |
// this->m_CanonicalCorrelations[yv]=0; |
| 1321 |
} |
| 1322 |
if (!this->m_Silent) |
| 1323 |
std::cout << "Qvec " << wv << " Qvec " << yv << " : " << b << std::endl; |
| 1324 |
} // doq |
| 1325 |
} |
| 1326 |
} |
| 1327 |
} |
| 1328 |
|
| 1329 |
template <typename TInputImage, typename TRealType> |
| 1330 |
TRealType |
| 1331 |
antsSCCANObject<TInputImage, TRealType>::SparseCCA(unsigned int /* nvecs */) |
| 1332 |
{ |
| 1333 |
if (!this->m_Silent) |
| 1334 |
std::cout << " ed sparse cca " << std::endl; |
| 1335 |
unsigned int nsubj = this->m_MatrixP.rows(); |
| 1336 |
|
| 1337 |
this->m_MatrixP = this->NormalizeMatrix(this->m_OriginalMatrixP); |
| 1338 |
this->m_MatrixQ = this->NormalizeMatrix(this->m_OriginalMatrixQ); |
| 1339 |
|
| 1340 |
RealType tau = 0.01; |
| 1341 |
if (this->m_Debug) |
| 1342 |
{ |
| 1343 |
if (!this->m_Silent) |
| 1344 |
std::cout << " inv view mats " << std::endl; |
| 1345 |
} |
| 1346 |
MatrixType inviewcovmatP = |
| 1347 |
((this->m_MatrixP * this->m_MatrixP.transpose()) * (this->m_MatrixP * this->m_MatrixP.transpose())) * (1 - tau) + |
| 1348 |
(this->m_MatrixP * this->m_MatrixP.transpose()) * tau * (RealType)nsubj; |
| 1349 |
MatrixType inviewcovmatQ = |
| 1350 |
((this->m_MatrixQ * this->m_MatrixQ.transpose()) * (this->m_MatrixQ * this->m_MatrixQ.transpose())) * (1 - tau) + |
| 1351 |
(this->m_MatrixQ * this->m_MatrixQ.transpose()) * tau * (RealType)nsubj; |
| 1352 |
|
| 1353 |
/** standard cca */ |
| 1354 |
// MatrixType CppInv=this->PseudoInverseCovMat(this->m_MatrixP); |
| 1355 |
// MatrixType CqqInv=this->PseudoInverseCovMat(this->m_MatrixQ); |
| 1356 |
// MatrixType cov=(CppInv*this->m_MatrixP).transpose()*(CqqInv*this->m_MatrixQ); |
| 1357 |
// MatrixType |
| 1358 |
// TT=(this->m_MatrixP.transpose()*this->m_MatrixQ)*(this->m_MatrixP.transpose()*this->m_MatrixQ).transpose(); |
| 1359 |
// MatrixType TT=( (CppInv*this->m_MatrixP).transpose()*(CqqInv*this->m_MatrixQ))* |
| 1360 |
// (this->m_MatrixP.transpose()*this->m_MatrixQ).transpose(); |
| 1361 |
|
| 1362 |
/** dual cca */ |
| 1363 |
MatrixType CppInv = this->PseudoInverse(inviewcovmatP); |
| 1364 |
MatrixType CqqInv = this->PseudoInverse(inviewcovmatQ); |
| 1365 |
|
| 1366 |
/** need the eigenvectors of this reduced matrix */ |
| 1367 |
// eMatrix pccain=CppInv*Cpq*CqqInv*Cqp; |
| 1368 |
MatrixType ccap = |
| 1369 |
((CppInv * (this->m_MatrixP * this->m_MatrixP.transpose())).transpose() * |
| 1370 |
(CqqInv * (this->m_MatrixQ * this->m_MatrixQ.transpose())) * |
| 1371 |
((this->m_MatrixP * this->m_MatrixP.transpose()).transpose() * (this->m_MatrixQ * this->m_MatrixQ.transpose())) |
| 1372 |
.transpose()); |
| 1373 |
// convert to eigen3 format |
| 1374 |
/* |
| 1375 |
eMatrix pccain=this->mVtoE(ccap); |
| 1376 |
|
| 1377 |
typedef Eigen::EigenSolver<eMatrix> eigsolver; |
| 1378 |
eigsolver pEG( pccain ); |
| 1379 |
// eigsolver qEG( qccain ); |
| 1380 |
eMatrix pccaVecs = pEG.pseudoEigenvectors(); |
| 1381 |
eMatrix pccaSquaredCorrs=pEG.pseudoEigenvalueMatrix(); |
| 1382 |
|
| 1383 |
// call this function to check we are doing conversions correctly , matrix-wise |
| 1384 |
this->mEtoV(pccaVecs); |
| 1385 |
// map the variates back to P, Q space and sort them |
| 1386 |
this->m_CanonicalCorrelations.set_size(nvecs); |
| 1387 |
this->m_CanonicalCorrelations.fill(0); |
| 1388 |
// copy to stl vector so we can sort the results |
| 1389 |
MatrixType projToQ=( CqqInv*( (this->m_MatrixQ*this->m_MatrixQ.transpose() )* |
| 1390 |
(this->m_MatrixP*this->m_MatrixP.transpose() ) )); std::vector<TRealType> evals(pccaSquaredCorrs.cols(),0); |
| 1391 |
std::vector<TRealType> oevals(pccaSquaredCorrs.cols(),0); |
| 1392 |
for ( long j=0; j<pccaSquaredCorrs.cols(); ++j){ |
| 1393 |
RealType val=pccaSquaredCorrs(j,j); |
| 1394 |
if ( val > 0.05 ){ |
| 1395 |
VectorType temp=this->vEtoV( pccaVecs.col( j ) ); |
| 1396 |
VectorType tempq=projToQ*temp; |
| 1397 |
VectorType pvar=temp*this->m_MatrixP; |
| 1398 |
this->ReSoftThreshold( pvar , this->m_FractionNonZeroP , this->m_KeepPositiveP ); |
| 1399 |
VectorType qvar= tempq*this->m_MatrixQ; |
| 1400 |
this->ReSoftThreshold( qvar , this->m_FractionNonZeroQ , this->m_KeepPositiveQ ); |
| 1401 |
evals[j]=fabs(this->PearsonCorr(this->m_MatrixP*pvar,this->m_MatrixQ*qvar)); |
| 1402 |
oevals[j]=evals[j]; |
| 1403 |
} |
| 1404 |
} |
| 1405 |
|
| 1406 |
// sort and reindex the eigenvectors/values |
| 1407 |
sort (evals.begin(), evals.end(), my_sccan_sort_object); |
| 1408 |
std::vector<int> sorted_indices(nvecs,-1); |
| 1409 |
for (unsigned int i=0; i<evals.size(); i++) { |
| 1410 |
for (unsigned int j=0; j<evals.size(); j++) { |
| 1411 |
if ( evals[i] == oevals[j] && sorted_indices[i] == -1 ) { |
| 1412 |
sorted_indices[i]=j; |
| 1413 |
oevals[j]=0; |
| 1414 |
} |
| 1415 |
}} |
| 1416 |
|
| 1417 |
this->m_VariatesP.set_size(this->m_MatrixP.cols(),nvecs); |
| 1418 |
this->m_VariatesQ.set_size(this->m_MatrixQ.cols(),nvecs); |
| 1419 |
for (unsigned int i=0; i<nvecs; i++) { |
| 1420 |
VectorType temp=this->vEtoV( pccaVecs.col( sorted_indices[i] ) ); |
| 1421 |
VectorType tempq=projToQ*temp; |
| 1422 |
VectorType pvar= temp*this->m_MatrixP; |
| 1423 |
this->ReSoftThreshold(pvar , this->m_FractionNonZeroP , this->m_KeepPositiveP ); |
| 1424 |
VectorType qvar= tempq*this->m_MatrixQ ; |
| 1425 |
this->ReSoftThreshold(qvar , this->m_FractionNonZeroQ , this->m_KeepPositiveQ ); |
| 1426 |
this->m_VariatesP.set_column( i, pvar ); |
| 1427 |
this->m_VariatesQ.set_column( i, qvar ); |
| 1428 |
} |
| 1429 |
|
| 1430 |
for (unsigned int i=0; i<nvecs; i++) { |
| 1431 |
this->m_CanonicalCorrelations[i]= |
| 1432 |
this->PearsonCorr(this->m_MatrixP*this->GetVariateP(i),this->m_MatrixQ*this->GetVariateQ(i) ); |
| 1433 |
if ( ! this->m_Silent ) std::cout << "correlation of mapped back data " << this->m_CanonicalCorrelations[i] << |
| 1434 |
" eval " << pccaSquaredCorrs(sorted_indices[i],sorted_indices[i]) << std::endl; |
| 1435 |
} |
| 1436 |
for (unsigned int i=0; i<nvecs; i++) { |
| 1437 |
if ( ! this->m_Silent ) std::cout << "inner prod of projections 0 vs i " << this->PearsonCorr( |
| 1438 |
this->m_MatrixP*this->GetVariateP(0) , this->m_MatrixP*this->GetVariateP(i) ) << std::endl; |
| 1439 |
} |
| 1440 |
*/ |
| 1441 |
// this->RunDiagnostics(nvecs); |
| 1442 |
return this->m_CanonicalCorrelations[0]; |
| 1443 |
} |
| 1444 |
|
| 1445 |
template <typename TInputImage, typename TRealType> |
| 1446 |
TRealType |
| 1447 |
antsSCCANObject<TInputImage, TRealType>::SparsePartialCCA(unsigned int /* nvecs */) |
| 1448 |
{ |
| 1449 |
/* |
| 1450 |
if ( ! this->m_Silent ) std::cout <<" ed sparse partial cca " << std::endl; |
| 1451 |
unsigned int nsubj=this->m_MatrixP.rows(); |
| 1452 |
this->m_MatrixP=this->NormalizeMatrix(this->m_OriginalMatrixP); |
| 1453 |
this->m_MatrixQ=this->NormalizeMatrix(this->m_OriginalMatrixQ); |
| 1454 |
this->m_MatrixR=this->NormalizeMatrix(this->m_OriginalMatrixR); |
| 1455 |
|
| 1456 |
RealType tau=0.01; |
| 1457 |
if (this->m_Debug) if ( ! this->m_Silent ) std::cout << " inv view mats " << std::endl; |
| 1458 |
this->m_MatrixRRt=this->ProjectionMatrix(this->m_OriginalMatrixR); |
| 1459 |
MatrixType PslashR=this->m_MatrixP; |
| 1460 |
if ( this->m_SCCANFormulation == PminusRQ || this->m_SCCANFormulation == PminusRQminusR ) |
| 1461 |
PslashR=this->m_MatrixP-(this->m_MatrixRRt*this->m_MatrixP); |
| 1462 |
MatrixType QslashR=this->m_MatrixQ; |
| 1463 |
if ( this->m_SCCANFormulation == PQminusR || this->m_SCCANFormulation == PminusRQminusR ) |
| 1464 |
QslashR=this->m_MatrixQ-this->m_MatrixRRt*this->m_MatrixQ; |
| 1465 |
|
| 1466 |
if (this->m_Debug) { |
| 1467 |
if ( ! this->m_Silent ) std::cout <<" corr-pre " << this->PearsonCorr( this->m_MatrixP.get_column(0) , |
| 1468 |
this->m_OriginalMatrixR.get_column(0) ) << std::endl ;if ( ! this->m_Silent ) std::cout <<" corr-post " << |
| 1469 |
this->PearsonCorr( PslashR.get_column(0) , this->m_OriginalMatrixR.get_column(0) ) << std::endl; if ( ! this->m_Silent |
| 1470 |
) std::cout <<" corr-pre " << this->PearsonCorr( this->m_MatrixQ.get_column(0) , this->m_OriginalMatrixR.get_column(0) |
| 1471 |
) << std::endl ;if ( ! this->m_Silent ) std::cout <<" corr-post " << this->PearsonCorr( QslashR.get_column(0) , |
| 1472 |
this->m_OriginalMatrixR.get_column(0) ) << std::endl; |
| 1473 |
} |
| 1474 |
MatrixType inviewcovmatP=( (PslashR*PslashR.transpose())*(PslashR*PslashR.transpose()) )*(1-tau)+ ( |
| 1475 |
PslashR*PslashR.transpose() )*tau*(RealType)nsubj; MatrixType inviewcovmatQ=( |
| 1476 |
(QslashR*QslashR.transpose())*(QslashR*QslashR.transpose()) )*(1-tau)+( QslashR*QslashR.transpose() |
| 1477 |
)*tau*(RealType)nsubj; |
| 1478 |
|
| 1479 |
// dual cca |
| 1480 |
if (this->m_Debug) if ( ! this->m_Silent ) std::cout << " inv view mats pinv " << std::endl; |
| 1481 |
MatrixType CppInv=this->PseudoInverse(inviewcovmatP); |
| 1482 |
MatrixType CqqInv=this->PseudoInverse(inviewcovmatQ); |
| 1483 |
if (this->m_Debug) if ( ! this->m_Silent ) std::cout << " inv view mats pinv done " << std::endl; |
| 1484 |
|
| 1485 |
// need the eigenvectors of this reduced matrix |
| 1486 |
MatrixType ccap=( (CppInv*(PslashR*PslashR.transpose())).transpose() * |
| 1487 |
(CqqInv*(QslashR*QslashR.transpose()))* |
| 1488 |
( (PslashR*PslashR.transpose() ).transpose()* |
| 1489 |
(QslashR*QslashR.transpose() ) ).transpose() ); |
| 1490 |
// convert to eigen3 format |
| 1491 |
eMatrix pccain=this->mVtoE(ccap); |
| 1492 |
|
| 1493 |
typedef Eigen::EigenSolver<eMatrix> eigsolver; |
| 1494 |
eigsolver pEG( pccain ); |
| 1495 |
eMatrix pccaVecs = pEG.pseudoEigenvectors(); |
| 1496 |
eMatrix pccaSquaredCorrs=pEG.pseudoEigenvalueMatrix(); |
| 1497 |
// call this function to check we are doing conversions correctly , matrix-wise |
| 1498 |
this->mEtoV(pccaVecs); |
| 1499 |
// map the variates back to P, Q space and sort them |
| 1500 |
this->m_CanonicalCorrelations.set_size(nvecs); |
| 1501 |
this->m_CanonicalCorrelations.fill(0); |
| 1502 |
MatrixType projToQ=( CqqInv*( (QslashR*QslashR.transpose() )* (PslashR*PslashR.transpose() ) )); |
| 1503 |
// copy to stl vector so we can sort the results |
| 1504 |
std::vector<TRealType> evals(pccaSquaredCorrs.cols(),0); |
| 1505 |
std::vector<TRealType> oevals(pccaSquaredCorrs.cols(),0); |
| 1506 |
for ( long j=0; j<pccaSquaredCorrs.cols(); ++j){ |
| 1507 |
RealType val=pccaSquaredCorrs(j,j); |
| 1508 |
if ( val > 0.05 ){ |
| 1509 |
VectorType temp=this->vEtoV( pccaVecs.col( j ) ); |
| 1510 |
VectorType tempq=projToQ*temp; |
| 1511 |
VectorType pvar= temp*PslashR; |
| 1512 |
this->ReSoftThreshold(pvar , this->m_FractionNonZeroP , this->m_KeepPositiveP ); |
| 1513 |
VectorType qvar=tempq*QslashR; |
| 1514 |
this->ReSoftThreshold(qvar , this->m_FractionNonZeroQ , this->m_KeepPositiveQ ); |
| 1515 |
evals[j]=fabs(this->PearsonCorr(PslashR*pvar,QslashR*qvar)); |
| 1516 |
oevals[j]=evals[j]; |
| 1517 |
} |
| 1518 |
} |
| 1519 |
|
| 1520 |
// sort and reindex the eigenvectors/values |
| 1521 |
sort (evals.begin(), evals.end(), my_sccan_sort_object); |
| 1522 |
std::vector<int> sorted_indices(nvecs,-1); |
| 1523 |
for (unsigned int i=0; i<evals.size(); i++) { |
| 1524 |
for (unsigned int j=0; j<evals.size(); j++) { |
| 1525 |
if ( evals[i] == oevals[j] && sorted_indices[i] == -1 ) { |
| 1526 |
sorted_indices[i]=j; |
| 1527 |
oevals[j]=0; |
| 1528 |
} |
| 1529 |
}} |
| 1530 |
|
| 1531 |
this->m_VariatesP.set_size(PslashR.cols(),nvecs); |
| 1532 |
this->m_VariatesQ.set_size(QslashR.cols(),nvecs); |
| 1533 |
for (unsigned int i=0; i<nvecs; i++) { |
| 1534 |
VectorType temp=this->vEtoV( pccaVecs.col( sorted_indices[i] ) ); |
| 1535 |
VectorType tempq=projToQ*temp; |
| 1536 |
VectorType pvar=temp*PslashR ; |
| 1537 |
this->ReSoftThreshold(pvar , this->m_FractionNonZeroP , this->m_KeepPositiveP ); |
| 1538 |
VectorType qvar=tempq*QslashR; |
| 1539 |
this->ReSoftThreshold(qvar , this->m_FractionNonZeroQ , this->m_KeepPositiveQ ); |
| 1540 |
this->m_VariatesP.set_column( i, pvar ); |
| 1541 |
this->m_VariatesQ.set_column( i, qvar ); |
| 1542 |
} |
| 1543 |
|
| 1544 |
for (unsigned int i=0; i<nvecs; i++) { |
| 1545 |
this->m_CanonicalCorrelations[i]= |
| 1546 |
this->PearsonCorr(PslashR*this->GetVariateP(i),QslashR*this->GetVariateQ(i) ); |
| 1547 |
if ( ! this->m_Silent ) std::cout << "correlation of mapped back data " << this->m_CanonicalCorrelations[i] << " |
| 1548 |
eval " << pccaSquaredCorrs(sorted_indices[i],sorted_indices[i]) << std::endl; |
| 1549 |
} |
| 1550 |
for (unsigned int i=0; i<nvecs; i++) { |
| 1551 |
if ( ! this->m_Silent ) std::cout << "inner prod of projections 0 vs i " << this->PearsonCorr( |
| 1552 |
PslashR*this->GetVariateP(0) , PslashR*this->GetVariateP(i) ) << std::endl; |
| 1553 |
} |
| 1554 |
|
| 1555 |
this->RunDiagnostics(nvecs); |
| 1556 |
*/ |
| 1557 |
return this->m_CanonicalCorrelations[0]; |
| 1558 |
} |
| 1559 |
|
| 1560 |
template <typename TInputImage, typename TRealType> |
| 1561 |
void |
| 1562 |
antsSCCANObject<TInputImage, TRealType>::SortResults(unsigned int n_vecs) |
| 1563 |
{ |
| 1564 |
bool debug = false; |
| 1565 |
|
| 1566 |
// sort and reindex the eigenvectors/values |
| 1567 |
std::vector<TRealType> evals(n_vecs, 0); |
| 1568 |
std::vector<TRealType> oevals(n_vecs, 0); |
| 1569 |
|
| 1570 |
if (debug) |
| 1571 |
{ |
| 1572 |
if (!this->m_Silent) |
| 1573 |
std::cout << "sort-a" << std::endl; |
| 1574 |
} |
| 1575 |
for (unsigned long j = 0; j < n_vecs; ++j) |
| 1576 |
{ |
| 1577 |
RealType val = fabs(this->m_CanonicalCorrelations[j]); |
| 1578 |
evals[j] = val; |
| 1579 |
oevals[j] = val; |
| 1580 |
} |
| 1581 |
if (debug) |
| 1582 |
{ |
| 1583 |
if (!this->m_Silent) |
| 1584 |
std::cout << "sort-b" << std::endl; |
| 1585 |
} |
| 1586 |
sort(evals.begin(), evals.end(), std::greater<TRealType>()); |
| 1587 |
std::vector<int> sorted_indices(n_vecs, n_vecs - 1); |
| 1588 |
for (unsigned int i = 0; i < evals.size(); i++) |
| 1589 |
{ |
| 1590 |
for (unsigned int j = 0; j < evals.size(); j++) |
| 1591 |
{ |
| 1592 |
if (fabs(evals[i] - oevals[j]) < 1.e-9 && sorted_indices[i] == static_cast<int>(n_vecs - 1)) |
| 1593 |
{ |
| 1594 |
sorted_indices[i] = j; |
| 1595 |
oevals[j] = 0; |
| 1596 |
} |
| 1597 |
} |
| 1598 |
} |
| 1599 |
if (debug) |
| 1600 |
{ |
| 1601 |
for (unsigned int i = 0; i < evals.size(); i++) |
| 1602 |
{ |
| 1603 |
if (!this->m_Silent) |
| 1604 |
std::cout << " sorted " << i << " is " << sorted_indices[i] << " ev " << evals[i] << " oev " << oevals[i] |
| 1605 |
<< std::endl; |
| 1606 |
} |
| 1607 |
} |
| 1608 |
if (debug) |
| 1609 |
{ |
| 1610 |
if (!this->m_Silent) |
| 1611 |
std::cout << "sort-c" << std::endl; |
| 1612 |
} |
| 1613 |
VectorType newcorrs(n_vecs, 0); |
| 1614 |
MatrixType varp(this->m_VariatesP.rows(), this->m_VariatesP.columns(), 0); |
| 1615 |
MatrixType varq(this->m_VariatesQ.rows(), this->m_VariatesQ.columns(), 0); |
| 1616 |
MatrixType varu(this->m_MatrixU.rows(), this->m_MatrixU.columns(), 0); |
| 1617 |
if (debug) |
| 1618 |
{ |
| 1619 |
if (!this->m_Silent) |
| 1620 |
std::cout << "sort-d" << std::endl; |
| 1621 |
} |
| 1622 |
for (unsigned int i = 0; i < n_vecs; i++) |
| 1623 |
{ |
| 1624 |
// if ( sorted_indices[i] > 0 ) { |
| 1625 |
varp.set_column(i, this->m_VariatesP.get_column(sorted_indices[i])); |
| 1626 |
if (varq.columns() > i) |
| 1627 |
{ |
| 1628 |
varq.set_column(i, this->m_VariatesQ.get_column(sorted_indices[i])); |
| 1629 |
} |
| 1630 |
if (varu.columns() > i) |
| 1631 |
{ |
| 1632 |
varu.set_column(i, this->m_MatrixU.get_column(sorted_indices[i])); |
| 1633 |
} |
| 1634 |
newcorrs[i] = (this->m_CanonicalCorrelations[sorted_indices[i]]); |
| 1635 |
// } |
| 1636 |
} |
| 1637 |
if (debug) |
| 1638 |
{ |
| 1639 |
if (!this->m_Silent) |
| 1640 |
std::cout << "sort-e" << std::endl; |
| 1641 |
} |
| 1642 |
for (unsigned int i = 0; i < n_vecs; i++) |
| 1643 |
{ |
| 1644 |
if (debug) |
| 1645 |
{ |
| 1646 |
if (!this->m_Silent) |
| 1647 |
std::cout << "sort-e-a" << std::endl; |
| 1648 |
} |
| 1649 |
this->m_VariatesP.set_column(i, varp.get_column(i)); |
| 1650 |
if (debug) |
| 1651 |
{ |
| 1652 |
if (!this->m_Silent) |
| 1653 |
std::cout << "sort-e-b" << std::endl; |
| 1654 |
} |
| 1655 |
if (this->m_VariatesQ.columns() > i) |
| 1656 |
{ |
| 1657 |
if (debug) |
| 1658 |
{ |
| 1659 |
if (!this->m_Silent) |
| 1660 |
std::cout << "sort-e-c" << i << " varqcol " << varq.columns() << " VarQcol " << this->m_VariatesQ.columns() |
| 1661 |
<< std::endl; |
| 1662 |
} |
| 1663 |
this->m_VariatesQ.set_column(i, varq.get_column(i)); |
| 1664 |
} |
| 1665 |
if (this->m_MatrixU.columns() > i) |
| 1666 |
{ |
| 1667 |
this->m_MatrixU.set_column(i, varu.get_column(i)); |
| 1668 |
} |
| 1669 |
} |
| 1670 |
if (debug) |
| 1671 |
{ |
| 1672 |
if (!this->m_Silent) |
| 1673 |
std::cout << "sort-f" << std::endl; |
| 1674 |
} |
| 1675 |
this->m_CanonicalCorrelations = newcorrs; |
| 1676 |
} |
| 1677 |
|
| 1678 |
template <typename TInputImage, typename TRealType> |
| 1679 |
TRealType |
| 1680 |
antsSCCANObject<TInputImage, TRealType>::SparseArnoldiSVD_z(unsigned int n_vecs) |
| 1681 |
{ |
| 1682 |
this->m_CanonicalCorrelations.set_size(n_vecs); |
| 1683 |
this->m_CanonicalCorrelations.fill(0); |
| 1684 |
if (!this->m_Silent) |
| 1685 |
std::cout << " arnoldi sparse svd : cg " << std::endl; |
| 1686 |
std::vector<RealType> vexlist; |
| 1687 |
this->m_MatrixP = this->NormalizeMatrix(this->m_OriginalMatrixP); |
| 1688 |
this->m_MatrixQ = this->m_MatrixP; |
| 1689 |
if (this->m_OriginalMatrixR.size() > 0) |
| 1690 |
{ |
| 1691 |
this->m_MatrixRRt = this->ProjectionMatrix(this->m_OriginalMatrixR); |
| 1692 |
this->m_MatrixP = this->m_MatrixP - (this->m_MatrixRRt * this->m_MatrixP); |
| 1693 |
} |
| 1694 |
this->m_ClusterSizes.set_size(n_vecs); |
| 1695 |
this->m_ClusterSizes.fill(0); |
| 1696 |
double trace = 0; |
| 1697 |
for (unsigned int i = 0; i < this->m_MatrixP.cols(); i++) |
| 1698 |
{ |
| 1699 |
trace += inner_product(this->m_MatrixP.get_column(i), this->m_MatrixP.get_column(i)); |
| 1700 |
} |
| 1701 |
this->m_VariatesP.set_size(this->m_MatrixP.cols(), n_vecs); |
| 1702 |
VariateType myGradients; |
| 1703 |
this->m_SparseVariatesP.set_size(this->m_MatrixP.cols(), n_vecs); |
| 1704 |
this->m_SparseVariatesP.fill(0); |
| 1705 |
myGradients.set_size(this->m_MatrixP.cols(), n_vecs); |
| 1706 |
myGradients.fill(0); |
| 1707 |
MatrixType init = this->GetCovMatEigenvectors(this->m_MatrixP); |
| 1708 |
for (unsigned int kk = 0; kk < n_vecs; kk++) |
| 1709 |
{ |
| 1710 |
this->m_VariatesP.set_column(kk, this->InitializeV(this->m_MatrixP)); |
| 1711 |
if (kk < init.columns()) |
| 1712 |
{ |
| 1713 |
VectorType initv = init.get_column(kk) * this->m_MatrixP; |
| 1714 |
this->m_VariatesP.set_column(kk, initv); |
| 1715 |
} |
| 1716 |
} |
| 1717 |
const unsigned int maxloop = this->m_MaximumNumberOfIterations; |
| 1718 |
// Arnoldi Iteration SVD/SPCA |
| 1719 |
unsigned int loop = 0; |
| 1720 |
bool debug = false; |
| 1721 |
double convcrit = 1; |
| 1722 |
RealType fnp = 1; |
| 1723 |
while (loop < maxloop && convcrit > 1.e-8) |
| 1724 |
{ |
| 1725 |
fnp = this->m_FractionNonZeroP; |
| 1726 |
for (unsigned int k = 0; k < n_vecs; k++) |
| 1727 |
{ |
| 1728 |
VectorType ptemp = this->m_SparseVariatesP.get_column(k); |
| 1729 |
// if ( loop == 0 ) |
| 1730 |
ptemp = this->m_VariatesP.get_column(k); |
| 1731 |
MatrixType pmod = this->m_MatrixP; |
| 1732 |
VectorType pveck = pmod.transpose() * (pmod * ptemp); // classic |
| 1733 |
RealType hkkm1 = pveck.two_norm(); |
| 1734 |
if (hkkm1 > this->m_Epsilon) |
| 1735 |
{ |
| 1736 |
pveck = pveck / hkkm1; |
| 1737 |
} |
| 1738 |
for (unsigned int j = 0; j < k; j++) // \forall j \ne i x_j \perp x_i |
| 1739 |
{ |
| 1740 |
VectorType qj = this->m_VariatesP.get_column(j); |
| 1741 |
RealType ip = inner_product(qj, qj); |
| 1742 |
if (ip < this->m_Epsilon) |
| 1743 |
{ |
| 1744 |
ip = 1; |
| 1745 |
} |
| 1746 |
RealType hjk = inner_product(qj, pveck) / ip; |
| 1747 |
pveck = pveck - qj * hjk; |
| 1748 |
} |
| 1749 |
VectorType pvecknew(pveck); |
| 1750 |
// x_i is sparse |
| 1751 |
if (fnp < 1) |
| 1752 |
{ |
| 1753 |
if (this->m_KeepPositiveP) |
| 1754 |
{ |
| 1755 |
this->ConstantProbabilityThreshold(pvecknew, fnp, this->m_KeepPositiveP); |
| 1756 |
} |
| 1757 |
else |
| 1758 |
{ |
| 1759 |
this->ReSoftThreshold(pvecknew, fnp, !this->m_KeepPositiveP); |
| 1760 |
} |
| 1761 |
this->ClusterThresholdVariate(pvecknew, this->m_MaskImageP, this->m_MinClusterSizeP); |
| 1762 |
pvecknew = pvecknew / pvecknew.two_norm(); |
| 1763 |
} |
| 1764 |
/** this is the difference between the updated & orthogonalized (not sparse) pveck and the sparse pveck */ |
| 1765 |
/** put another way - minimize the difference between the sparse solution and the pca solution */ |
| 1766 |
// myGradients.set_column(k,pvecknew*this->m_CanonicalCorrelations[k]-pveck); |
| 1767 |
myGradients.set_column(k, pvecknew - pveck / pveck.two_norm()); |
| 1768 |
// residual between sparse and pca solution |
| 1769 |
if (loop == 0) |
| 1770 |
{ |
| 1771 |
this->m_VariatesP.set_column(k, pvecknew); |
| 1772 |
} |
| 1773 |
} // kloop |
| 1774 |
|
| 1775 |
/** Now update the solution by this gradient */ |
| 1776 |
double recuravg = 0.5; // 0.99; //1/(double)(loop+1); |
| 1777 |
double recuravg2 = 1 - recuravg; |
| 1778 |
for (unsigned int k = 0; k < n_vecs; k++) |
| 1779 |
{ |
| 1780 |
VectorType proj = this->m_MatrixP * this->m_VariatesP.get_column(k); |
| 1781 |
double denom = inner_product(proj, proj); |
| 1782 |
if (denom < this->m_Epsilon) |
| 1783 |
{ |
| 1784 |
denom = 1.e9; |
| 1785 |
} |
| 1786 |
VectorType pveck = myGradients.get_column(k); |
| 1787 |
VectorType newsol = this->m_SparseVariatesP.get_column(k) * recuravg2 + pveck * recuravg; |
| 1788 |
// VectorType newsol = this->m_SparseVariatesP.get_column(k) - pveck * alphak; |
| 1789 |
this->m_SparseVariatesP.set_column(k, newsol); |
| 1790 |
} |
| 1791 |
/** Project the solution space to the sub-space */ |
| 1792 |
for (unsigned int k = 0; k < n_vecs; k++) |
| 1793 |
{ |
| 1794 |
VectorType ptemp = this->m_SparseVariatesP.get_column(k); |
| 1795 |
if (fnp < 1) |
| 1796 |
{ |
| 1797 |
if (this->m_KeepPositiveP) |
| 1798 |
{ |
| 1799 |
this->ConstantProbabilityThreshold(ptemp, fnp, this->m_KeepPositiveP); |
| 1800 |
} |
| 1801 |
else |
| 1802 |
{ |
| 1803 |
this->ReSoftThreshold(ptemp, fnp, !this->m_KeepPositiveP); |
| 1804 |
} |
| 1805 |
this->ClusterThresholdVariate(ptemp, this->m_MaskImageP, this->m_MinClusterSizeP); |
| 1806 |
ptemp = ptemp / ptemp.two_norm(); |
| 1807 |
} |
| 1808 |
MatrixType pmod = this->m_MatrixP; |
| 1809 |
VectorType pveck = pmod.transpose() * (pmod * ptemp); // classic |
| 1810 |
RealType hkkm1 = pveck.two_norm(); |
| 1811 |
if (hkkm1 > this->m_Epsilon) |
| 1812 |
{ |
| 1813 |
pveck = pveck / hkkm1; |
| 1814 |
} |
| 1815 |
for (unsigned int j = 0; j < k; j++) |
| 1816 |
{ |
| 1817 |
VectorType qj = this->m_VariatesP.get_column(j); |
| 1818 |
RealType ip = inner_product(qj, qj); |
| 1819 |
if (ip < this->m_Epsilon) |
| 1820 |
{ |
| 1821 |
ip = 1; |
| 1822 |
} |
| 1823 |
RealType hjk = inner_product(qj, pveck) / ip; |
| 1824 |
pveck = pveck - qj * hjk; |
| 1825 |
} |
| 1826 |
if (fnp < 1) |
| 1827 |
{ |
| 1828 |
if (this->m_KeepPositiveP) |
| 1829 |
{ |
| 1830 |
this->ConstantProbabilityThreshold(pveck, fnp, this->m_KeepPositiveP); |
| 1831 |
} |
| 1832 |
else |
| 1833 |
{ |
| 1834 |
this->ReSoftThreshold(pveck, fnp, !this->m_KeepPositiveP); |
| 1835 |
} |
| 1836 |
this->ClusterThresholdVariate(pveck, this->m_MaskImageP, this->m_MinClusterSizeP); |
| 1837 |
pveck = pveck / pveck.two_norm(); |
| 1838 |
} |
| 1839 |
this->m_VariatesP.set_column(k, pveck); |
| 1840 |
} // kloop |
| 1841 |
|
| 1842 |
this->m_VariatesQ = this->m_VariatesP; |
| 1843 |
if (debug) |
| 1844 |
{ |
| 1845 |
if (!this->m_Silent) |
| 1846 |
std::cout << " get evecs " << std::endl; |
| 1847 |
} |
| 1848 |
RealType vex = this->ComputeSPCAEigenvalues(n_vecs, trace, true); |
| 1849 |
vexlist.push_back(vex); |
| 1850 |
this->SortResults(n_vecs); |
| 1851 |
convcrit = (this->ComputeEnergySlope(vexlist, 5)); |
| 1852 |
if (!this->m_Silent) |
| 1853 |
std::cout << "Iteration: " << loop << " Eigenval_0: " << this->m_CanonicalCorrelations[0] |
| 1854 |
<< " Eigenval_N: " << this->m_CanonicalCorrelations[n_vecs - 1] << " Sparseness: " << fnp |
| 1855 |
<< " convergence-criterion: " << convcrit << " vex " << vex << std::endl; |
| 1856 |
loop++; |
| 1857 |
if (debug) |
| 1858 |
{ |
| 1859 |
if (!this->m_Silent) |
| 1860 |
std::cout << "wloopdone" << std::endl; |
| 1861 |
} |
| 1862 |
} // opt-loop |
| 1863 |
// if ( ! this->m_Silent ) std::cout << " cluster-sizes " << this->m_ClusterSizes << std::endl; |
| 1864 |
for (unsigned int i = 0; i < vexlist.size(); i++) |
| 1865 |
{ |
| 1866 |
if (!this->m_Silent) |
| 1867 |
std::cout << vexlist[i] << ","; |
| 1868 |
} |
| 1869 |
if (!this->m_Silent) |
| 1870 |
std::cout << std::endl; |
| 1871 |
return fabs(this->m_CanonicalCorrelations[0]); |
| 1872 |
} |
| 1873 |
|
| 1874 |
template <typename TInputImage, typename TRealType> |
| 1875 |
TRealType |
| 1876 |
antsSCCANObject<TInputImage, TRealType>::ReconstructionError( |
| 1877 |
typename antsSCCANObject<TInputImage, TRealType>::MatrixType mymat, |
| 1878 |
typename antsSCCANObject<TInputImage, TRealType>::MatrixType myvariate) |
| 1879 |
{ |
| 1880 |
this->m_CanonicalCorrelations.set_size(myvariate.cols()); |
| 1881 |
this->m_CanonicalCorrelations.fill(0); |
| 1882 |
RealType reconerr = 0; |
| 1883 |
RealType onenorm = 0; |
| 1884 |
unsigned int n_vecs = myvariate.cols(); |
| 1885 |
RealType clnum = (RealType)this->m_MatrixP.cols(); |
| 1886 |
for (unsigned int a = 0; a < mymat.rows(); a++) |
| 1887 |
{ |
| 1888 |
VectorType x_i = mymat.get_row(a); |
| 1889 |
onenorm += x_i.one_norm() / clnum; |
| 1890 |
VectorType lmsolv(n_vecs, 1); // row for B |
| 1891 |
(void)this->ConjGrad(myvariate, lmsolv, x_i, 0, 10000); |
| 1892 |
VectorType x_recon = (this->m_VariatesP * lmsolv + this->m_Intercept); |
| 1893 |
reconerr += (x_i - x_recon).one_norm() / clnum; |
| 1894 |
for (unsigned int i = 0; i < n_vecs; i++) |
| 1895 |
{ |
| 1896 |
this->m_CanonicalCorrelations[i] = this->m_CanonicalCorrelations[i] + fabs(lmsolv[i]); |
| 1897 |
} |
| 1898 |
} |
| 1899 |
return (onenorm - reconerr) / onenorm; |
| 1900 |
} |
| 1901 |
|
| 1902 |
template <typename TInputImage, typename TRealType> |
| 1903 |
TRealType |
| 1904 |
antsSCCANObject<TInputImage, TRealType>::SparseReconPrior(unsigned int n_vecs, bool prior) |
| 1905 |
{ |
| 1906 |
VectorType sparsenessparams(n_vecs, this->m_FractionNonZeroP); |
| 1907 |
double lambda = this->GetPriorWeight(); // Make it a parameter |
| 1908 |
if (!this->m_Silent) |
| 1909 |
std::cout << "lambda" << lambda << " GradStep " << this->m_GradStep << std::endl; |
| 1910 |
if (prior) |
| 1911 |
{ |
| 1912 |
this->m_MatrixPriorROI = this->m_OriginalMatrixPriorROI; |
| 1913 |
n_vecs = this->m_MatrixPriorROI.rows(); |
| 1914 |
for (unsigned int x = 0; x < this->m_OriginalMatrixPriorROI.rows(); x++) |
| 1915 |
{ |
| 1916 |
VectorType priorrow = this->m_OriginalMatrixPriorROI.get_row(x); |
| 1917 |
RealType fnz = 0; |
| 1918 |
for (unsigned int y = 0; y < this->m_OriginalMatrixPriorROI.cols(); y++) |
| 1919 |
{ |
| 1920 |
if (itk::Math::abs(priorrow(y)) > 1.e-10) |
| 1921 |
{ |
| 1922 |
fnz += 1; |
| 1923 |
} |
| 1924 |
} |
| 1925 |
if (this->m_FractionNonZeroP < 1.e-10) |
| 1926 |
{ |
| 1927 |
sparsenessparams(x) = (RealType)fnz / (RealType)this->m_OriginalMatrixPriorROI.cols(); |
| 1928 |
} |
| 1929 |
priorrow = priorrow / priorrow.two_norm(); |
| 1930 |
this->m_MatrixPriorROI.set_row(x, priorrow); |
| 1931 |
} |
| 1932 |
} |
| 1933 |
RealType reconerr = 0; |
| 1934 |
this->m_CanonicalCorrelations.set_size(n_vecs); |
| 1935 |
this->m_CanonicalCorrelations.fill(0); |
| 1936 |
if (!this->m_Silent) |
| 1937 |
std::cout << " sparse recon prior " << this->m_MinClusterSizeP << std::endl; |
| 1938 |
MatrixType matrixB(this->m_OriginalMatrixP.rows(), n_vecs); |
| 1939 |
matrixB.fill(0); |
| 1940 |
this->m_MatrixP = this->NormalizeMatrix(this->m_OriginalMatrixP); |
| 1941 |
this->m_VariatesP.set_size(this->m_MatrixP.cols(), n_vecs); |
| 1942 |
this->m_VariatesP.fill(0); |
| 1943 |
VectorType icept(this->m_MatrixP.rows(), 0); |
| 1944 |
if (prior) |
| 1945 |
{ |
| 1946 |
for (unsigned int i = 0; i < n_vecs; i++) |
| 1947 |
{ |
| 1948 |
VectorType initvec = this->m_MatrixPriorROI.get_row(i); |
| 1949 |
initvec = initvec / initvec.two_norm(); |
| 1950 |
this->SparsifyP(initvec); |
| 1951 |
this->m_VariatesP.set_column(i, initvec); |
| 1952 |
} |
| 1953 |
} |
| 1954 |
else |
| 1955 |
{ |
| 1956 |
for (unsigned int i = 0; i < n_vecs; i++) |
| 1957 |
{ |
| 1958 |
VectorType initvec = this->InitializeV(this->m_MatrixP, i + 1); |
| 1959 |
initvec = initvec / initvec.two_norm(); |
| 1960 |
this->m_VariatesP.set_column(i, initvec); |
| 1961 |
} |
| 1962 |
} |
| 1963 |
|
| 1964 |
/** now initialize B */ |
| 1965 |
reconerr = this->SparseReconB(matrixB, icept); |
| 1966 |
for (unsigned int overit = 0; overit < this->m_MaximumNumberOfIterations; overit++) |
| 1967 |
{ |
| 1968 |
// update V matrix |
| 1969 |
/** a power iteration method --- depends on the following |
| 1970 |
|
| 1971 |
given any nonzero $z \in \mathbb{R}^n$, the Rayleigh quotient |
| 1972 |
$x^T X x / x^T x $ minimizes the function $\| \lambda x - X x \|^2 $ |
| 1973 |
wrt $\lambda$. |
| 1974 |
|
| 1975 |
so, if we find the vector x ( by sparse power iteration ) then we have a vector |
| 1976 |
that is a close approximation to the first eigenvector of X. If X is a residual |
| 1977 |
matrix then x is a good approximation of the $n^th$ eigenvector. |
| 1978 |
**/ |
| 1979 |
VectorType zero(this->m_MatrixP.cols(), 0); |
| 1980 |
VectorType zerob(this->m_MatrixP.rows(), 0); |
| 1981 |
for (unsigned int a = 0; a < n_vecs; a++) |
| 1982 |
{ |
| 1983 |
this->m_FractionNonZeroP = sparsenessparams(a); |
| 1984 |
VectorType bvec = matrixB.get_column(a); |
| 1985 |
matrixB.set_column(a, zerob); // if X = U V^T + Error then matrixB = U |
| 1986 |
MatrixType tempMatrix = this->m_VariatesP; |
| 1987 |
tempMatrix.set_column(a, zero); |
| 1988 |
MatrixType partialmatrix = matrixB * tempMatrix.transpose(); |
| 1989 |
for (unsigned int interc = 0; interc < this->m_MatrixP.rows(); interc++) |
| 1990 |
{ |
| 1991 |
partialmatrix.set_row(interc, partialmatrix.get_row(interc) + icept(interc)); |
| 1992 |
} |
| 1993 |
partialmatrix = this->m_MatrixP - partialmatrix; |
| 1994 |
VectorType priorVec = this->m_MatrixPriorROI.get_row(a); |
| 1995 |
if (priorVec.one_norm() < 1.e-12) |
| 1996 |
{ |
| 1997 |
if (!this->m_Silent) |
| 1998 |
std::cout << "Prior Norm too small, could be a bug: " << priorVec.one_norm() << std::endl; |
| 1999 |
} |
| 2000 |
VectorType evec = this->m_VariatesP.get_column(a); |
| 2001 |
priorVec = priorVec / priorVec.two_norm(); |
| 2002 |
this->m_CanonicalCorrelations[a] = this->IHTPowerIterationPrior(partialmatrix, evec, priorVec, 10, a, lambda); |
| 2003 |
this->m_VariatesP.set_column(a, evec); |
| 2004 |
matrixB.set_column(a, bvec); |
| 2005 |
// ///////////////////// |
| 2006 |
// update B matrix by linear regression |
| 2007 |
reconerr = this->SparseReconB(matrixB, icept); |
| 2008 |
} |
| 2009 |
if (!this->m_Silent) |
| 2010 |
std::cout << overit << ": %var " << reconerr << std::endl; |
| 2011 |
} |
| 2012 |
this->m_VariatesQ = matrixB; |
| 2013 |
return 1.0 / reconerr; |
| 2014 |
} |
| 2015 |
|
| 2016 |
template <typename TInputImage, typename TRealType> |
| 2017 |
TRealType |
| 2018 |
antsSCCANObject<TInputImage, TRealType>::IHTPowerIterationPrior( |
| 2019 |
typename antsSCCANObject<TInputImage, TRealType>::MatrixType & A, |
| 2020 |
typename antsSCCANObject<TInputImage, TRealType>::VectorType & evec, |
| 2021 |
typename antsSCCANObject<TInputImage, TRealType>::VectorType & priorin, |
| 2022 |
unsigned int maxits, |
| 2023 |
unsigned int maxorth, |
| 2024 |
double lambda) |
| 2025 |
{ |
| 2026 |
/* |
| 2027 |
// d/dv \| X_p - u_a v^t \| = u_a^t ( X_p - u_a v^t ) |
| 2028 |
// u_a^t X_p - u_a^t u_a v^t |
| 2029 |
// d/dv \| M - v v^t \| = v^t ( M - v v^t ) |
| 2030 |
// v^t M - ip( v, v ) v^t |
| 2031 |
VectorType bvec = this->m_MatrixU.get_column( maxorth ); |
| 2032 |
VectorType mygrad1 = ( bvec * A - evec * inner_product( bvec, bvec ) ) * lam1; |
| 2033 |
VectorType mygrad2 = ( this->FastOuterProductVectorMultiplication( priorin, evec ) |
| 2034 |
- evec * inner_product( evec, evec ) ) * lam2; |
| 2035 |
VectorType mygrad = mygrad1 + mygrad2 ; |
| 2036 |
evec = evec + mygrad * this->m_GradStep; |
| 2037 |
this->SparsifyP( evec ); |
| 2038 |
return 1; |
| 2039 |
*/ |
| 2040 |
/** This computes a hard-thresholded gradient descent on the eigenvector criterion. |
| 2041 |
max x over x^t A^t A x s.t. x^t x = 1 |
| 2042 |
success of the optimization is measured by rayleigh quotient. derivative is: |
| 2043 |
d.dx ( x^t A^t A x ) = A^t A x , x \leftarrow x / \| x \| |
| 2044 |
we use a conjugate gradient version of this optimization. |
| 2045 |
*/ |
| 2046 |
VectorType prior(priorin); |
| 2047 |
if (itk::Math::FloatAlmostEqual(evec.two_norm(), itk::NumericTraits<typename VectorType::abs_t>::ZeroValue())) |
| 2048 |
{ |
| 2049 |
evec = this->InitializeV(this->m_MatrixP, false); |
| 2050 |
} |
| 2051 |
evec = evec / evec.two_norm(); |
| 2052 |
if (itk::Math::FloatAlmostEqual(inner_product(prior, evec), itk::NumericTraits<RealType>::ZeroValue())) |
| 2053 |
{ |
| 2054 |
evec.update(prior, 0); |
| 2055 |
this->SparsifyP(evec); |
| 2056 |
} |
| 2057 |
VectorType mgrad = this->FastOuterProductVectorMultiplication(prior, evec); |
| 2058 |
VectorType proj = (A * prior); |
| 2059 |
VectorType lastgrad = evec; |
| 2060 |
VectorType mlastgrad = mgrad; |
| 2061 |
RealType rayquo = 0, rayquold = -1; |
| 2062 |
RealType denom = inner_product(evec, evec); |
| 2063 |
if (denom > 0) |
| 2064 |
{ |
| 2065 |
rayquo = inner_product(proj, proj) / denom; |
| 2066 |
} |
| 2067 |
MatrixType At = A.transpose(); |
| 2068 |
unsigned int powerits = 0; |
| 2069 |
VectorType bestevec = evec; |
| 2070 |
RealType basevscale = 1; |
| 2071 |
RealType basepscale = 1; |
| 2072 |
VectorType di; |
| 2073 |
while (((powerits < maxits) && (rayquo > rayquold)) || (powerits < 4)) |
| 2074 |
{ |
| 2075 |
VectorType pvec = this->FastOuterProductVectorMultiplication(prior, evec); |
| 2076 |
VectorType nvec = (At * (A * evec)); |
| 2077 |
if (powerits == 0) |
| 2078 |
{ |
| 2079 |
basevscale = pvec.two_norm() / nvec.two_norm() * this->m_GradStep; |
| 2080 |
basepscale = this->m_GradStep * lambda; |
| 2081 |
} |
| 2082 |
VectorType g1 = nvec * basevscale; |
| 2083 |
VectorType g2 = pvec * basepscale; |
| 2084 |
RealType gamma = 0.1; |
| 2085 |
nvec = g1 + g2; |
| 2086 |
if (powerits == 0) |
| 2087 |
di = nvec; |
| 2088 |
if ((lastgrad.two_norm() > 0)) |
| 2089 |
{ |
| 2090 |
gamma = inner_product(nvec, nvec) / inner_product(lastgrad, lastgrad); |
| 2091 |
} |
| 2092 |
lastgrad = nvec; |
| 2093 |
VectorType diplus1 = nvec + di * gamma; |
| 2094 |
evec = evec + diplus1; |
| 2095 |
di = diplus1; |
| 2096 |
for (unsigned int orth = 0; orth < maxorth; orth++) |
| 2097 |
{ |
| 2098 |
VectorType zv = this->m_VariatesP.get_column(orth); |
| 2099 |
evec = this->Orthogonalize(evec, zv); |
| 2100 |
} |
| 2101 |
this->SparsifyP(evec); |
| 2102 |
if (evec.two_norm() > 0) |
| 2103 |
{ |
| 2104 |
evec = evec / evec.two_norm(); |
| 2105 |
} |
| 2106 |
rayquold = rayquo; |
| 2107 |
denom = inner_product(evec, evec); |
| 2108 |
if (denom > 0) |
| 2109 |
{ |
| 2110 |
// fast way to compute evec^T * ( A^T A + M M^T ) evec |
| 2111 |
proj = (At * (A * evec)) * basevscale + (this->FastOuterProductVectorMultiplication(prior, evec)) * basepscale; |
| 2112 |
rayquo = inner_product(evec, proj) / denom; |
| 2113 |
} |
| 2114 |
powerits++; |
| 2115 |
if (rayquo > rayquold || powerits == 1) |
| 2116 |
{ |
| 2117 |
bestevec = evec; |
| 2118 |
} |
| 2119 |
} |
| 2120 |
// if ( ! this->m_Silent ) std::cout << "rayleigh-quotient: " << rayquo << " in " << powerits << " num " << maxorth; |
| 2121 |
evec = bestevec; |
| 2122 |
if (itk::Math::FloatAlmostEqual(inner_product(prior, evec), itk::NumericTraits<RealType>::ZeroValue())) |
| 2123 |
{ |
| 2124 |
evec.update(prior, 0); |
| 2125 |
this->SparsifyP(evec); |
| 2126 |
if (!this->m_Silent) |
| 2127 |
std::cout << " recompute " << maxorth << " new ip " << inner_product(prior, evec); |
| 2128 |
} |
| 2129 |
return rayquo; |
| 2130 |
} |
| 2131 |
|
| 2132 |
template <typename TInputImage, typename TRealType> |
| 2133 |
TRealType |
| 2134 |
antsSCCANObject<TInputImage, TRealType>::SparseReconB( |
| 2135 |
typename antsSCCANObject<TInputImage, TRealType>::MatrixType & matrixB, |
| 2136 |
typename antsSCCANObject<TInputImage, TRealType>::VectorType & icept) |
| 2137 |
{ |
| 2138 |
MatrixType temp(this->m_MatrixP); |
| 2139 |
icept.fill(0); |
| 2140 |
RealType meancorr = 0; |
| 2141 |
matrixB.set_size(this->m_MatrixP.rows(), this->m_VariatesP.cols()); |
| 2142 |
matrixB.fill(0); |
| 2143 |
for (unsigned int a = 0; a < this->m_MatrixP.rows(); a++) |
| 2144 |
{ |
| 2145 |
VectorType x_i = this->m_MatrixP.get_row(a); // 1 by p |
| 2146 |
VectorType lmsolv = matrixB.get_row(a); // 1 by k .... variatesp is p by k ... |
| 2147 |
// solve ( p by k ) ( k by 1 ) = ( p by 1 ) |
| 2148 |
// which is \| V U_j - X_j \|^2 where j is the j^th subject |
| 2149 |
(void)this->ConjGrad(this->m_VariatesP, lmsolv, x_i, 0, 100); // A x = b |
| 2150 |
icept(a) = this->m_Intercept; |
| 2151 |
VectorType x_recon = (this->m_VariatesP * lmsolv + icept(a)); |
| 2152 |
matrixB.set_row(a, lmsolv); |
| 2153 |
RealType localcorr = this->PearsonCorr(x_recon, x_i); |
| 2154 |
temp.set_row(a, x_recon); |
| 2155 |
meancorr += localcorr; |
| 2156 |
} |
| 2157 |
if (false) |
| 2158 |
{ |
| 2159 |
std::vector<std::string> ColumnHeaders; |
| 2160 |
for (unsigned int nv = 0; nv < this->m_MatrixP.cols(); nv++) |
| 2161 |
{ |
| 2162 |
std::stringstream ss; |
| 2163 |
ss << (nv + 1); |
| 2164 |
std::string stringout = ss.str(); |
| 2165 |
std::string colname = std::string("X") + stringout; |
| 2166 |
ColumnHeaders.push_back(colname); |
| 2167 |
} |
| 2168 |
MatrixType recon = (matrixB * this->m_VariatesP.transpose()); |
| 2169 |
typedef itk::CSVNumericObjectFileWriter<double, 1, 1> WriterType; |
| 2170 |
WriterType::Pointer writer = WriterType::New(); |
| 2171 |
writer->SetFileName("Zrecon.csv"); |
| 2172 |
writer->SetInput(&recon); |
| 2173 |
writer->Write(); |
| 2174 |
} |
| 2175 |
this->m_MatrixU = matrixB; |
| 2176 |
RealType matpfrobnorm = this->m_MatrixP.frobenius_norm(); |
| 2177 |
RealType rr = (temp - this->m_MatrixP).frobenius_norm(); |
| 2178 |
return (1.0 - rr * rr / (matpfrobnorm * matpfrobnorm)); |
| 2179 |
} |
| 2180 |
|
| 2181 |
|
| 2182 |
template <typename TInputImage, typename TRealType> |
| 2183 |
TRealType |
| 2184 |
antsSCCANObject<TInputImage, TRealType>::SparseRecon(unsigned int n_vecs) |
| 2185 |
{ |
| 2186 |
bool prior = false; |
| 2187 |
RealType reconerr = 0; |
| 2188 |
VectorType sparsenessparams(n_vecs, this->m_FractionNonZeroP); |
| 2189 |
if ((this->m_OriginalMatrixPriorROI.rows() > 0) && (this->m_OriginalMatrixPriorROI.cols() > 0)) |
| 2190 |
{ |
| 2191 |
prior = true; |
| 2192 |
if (!this->m_Silent) |
| 2193 |
std::cout << " image-driven initialization " << std::endl; |
| 2194 |
this->m_MatrixPriorROI = this->m_OriginalMatrixPriorROI; |
| 2195 |
n_vecs = this->m_OriginalMatrixPriorROI.rows(); |
| 2196 |
for (unsigned int x = 0; x < this->m_OriginalMatrixPriorROI.rows(); x++) |
| 2197 |
{ |
| 2198 |
VectorType priorrow = this->m_OriginalMatrixPriorROI.get_row(x); |
| 2199 |
RealType fnz = 0; |
| 2200 |
for (unsigned int y = 0; y < this->m_OriginalMatrixPriorROI.cols(); y++) |
| 2201 |
{ |
| 2202 |
if (itk::Math::abs(priorrow(y)) > 1.e-8) |
| 2203 |
{ |
| 2204 |
fnz += 1; |
| 2205 |
} |
| 2206 |
} |
| 2207 |
if (itk::Math::abs(this->m_FractionNonZeroP) < 1.e-11) |
| 2208 |
sparsenessparams(x) = 1.0 * (RealType)fnz / (RealType)this->m_OriginalMatrixPriorROI.cols(); |
| 2209 |
priorrow = priorrow / priorrow.two_norm(); |
| 2210 |
this->m_MatrixPriorROI.set_row(x, priorrow); |
| 2211 |
} |
| 2212 |
this->m_FractionNonZeroP = sparsenessparams.mean(); |
| 2213 |
if (!this->m_Silent) |
| 2214 |
std::cout << "sparseness: " << sparsenessparams << std::endl; |
| 2215 |
this->m_VariatesP = this->m_MatrixPriorROI.transpose(); |
| 2216 |
} |
| 2217 |
this->m_CanonicalCorrelations.set_size(n_vecs); |
| 2218 |
this->m_CanonicalCorrelations.fill(0); |
| 2219 |
if (!this->m_Silent) |
| 2220 |
std::cout << " sparse recon : clust " << this->m_MinClusterSizeP << " UseL1 " << this->m_UseL1 << " Keep+ " |
| 2221 |
<< this->m_KeepPositiveP << " covering " << this->m_Covering << " smooth " << this->m_Smoother |
| 2222 |
<< std::endl; |
| 2223 |
MatrixType matrixB(this->m_OriginalMatrixP.rows(), n_vecs); |
| 2224 |
matrixB.fill(0); |
| 2225 |
this->m_MatrixP = this->NormalizeMatrix(this->m_OriginalMatrixP); |
| 2226 |
VectorType icept(this->m_MatrixP.rows(), 0); |
| 2227 |
if (this->m_FractionNonZeroP < 1.e-11) |
| 2228 |
{ // estimate sparseness from PCA |
| 2229 |
if (!this->m_Silent) |
| 2230 |
std::cout << " data-driven initialization from PCA " << std::endl; |
| 2231 |
VectorType maxvals(this->m_MatrixP.cols(), 0); |
| 2232 |
MatrixType cov = this->m_MatrixP * this->m_MatrixP.transpose(); |
| 2233 |
vnl_svd<RealType> eig(cov, 1.e-6); |
| 2234 |
this->m_VariatesP.set_size(this->m_MatrixP.cols(), n_vecs); |
| 2235 |
this->m_VariatesP.fill(0); |
| 2236 |
for (unsigned int i = 0; i < n_vecs; i++) |
| 2237 |
{ |
| 2238 |
if (i < this->m_MatrixP.rows()) |
| 2239 |
{ |
| 2240 |
VectorType u = eig.U().get_column(i); |
| 2241 |
VectorType up = u * this->m_MatrixP; |
| 2242 |
up = up / up.two_norm(); |
| 2243 |
this->m_VariatesP.set_column(i, up); |
| 2244 |
} |
| 2245 |
} |
| 2246 |
for (unsigned int i = 0; i < maxvals.size(); i++) |
| 2247 |
{ |
| 2248 |
RealType maxval = 0; |
| 2249 |
for (unsigned int j = 0; j < n_vecs; j++) |
| 2250 |
{ |
| 2251 |
RealType myval = itk::Math::abs(this->m_VariatesP(i, j)); |
| 2252 |
if (myval > maxval) |
| 2253 |
{ |
| 2254 |
maxvals(i) = j; |
| 2255 |
maxval = myval; |
| 2256 |
} |
| 2257 |
} |
| 2258 |
} |
| 2259 |
sparsenessparams.fill(0); |
| 2260 |
for (unsigned int i = 0; i < maxvals.size(); i++) |
| 2261 |
{ |
| 2262 |
unsigned int index = (unsigned int)(maxvals(i) + 0.5); |
| 2263 |
sparsenessparams(index) = sparsenessparams(index) + 1; |
| 2264 |
} |
| 2265 |
for (unsigned int i = 0; i < n_vecs; i++) |
| 2266 |
{ |
| 2267 |
sparsenessparams(i) = 1.01 * (sparsenessparams(i) / this->m_MatrixP.cols()); |
| 2268 |
this->m_FractionNonZeroP = sparsenessparams(i); |
| 2269 |
VectorType vec = this->m_VariatesP.get_column(i); |
| 2270 |
this->SparsifyP(vec); |
| 2271 |
this->m_VariatesP.set_column(i, vec); |
| 2272 |
if (!this->m_Silent) |
| 2273 |
std::cout << " Estimated-Sparseness " << i << " is " << sparsenessparams(i) << std::endl; |
| 2274 |
} |
| 2275 |
if (!this->m_Silent) |
| 2276 |
std::cout << " Estimated-Sparseness Sum " << sparsenessparams.sum() << std::endl; |
| 2277 |
} // done estimate sparseness |
| 2278 |
else if (this->m_VariatesP.rows() < 1) |
| 2279 |
{ |
| 2280 |
if (!this->m_Silent) |
| 2281 |
std::cout << " data-driven initialization " << std::endl; |
| 2282 |
this->m_VariatesP.set_size(this->m_MatrixP.cols(), n_vecs); |
| 2283 |
this->m_VariatesP.fill(0); |
| 2284 |
for (unsigned int i = 0; i < n_vecs; i++) |
| 2285 |
{ |
| 2286 |
VectorType initvec = this->InitializeV(this->m_MatrixP, i + 1); |
| 2287 |
for (unsigned int j = 0; j < i; j++) |
| 2288 |
{ |
| 2289 |
initvec = this->Orthogonalize(initvec, this->m_VariatesP.get_column(j)); |
| 2290 |
} |
| 2291 |
initvec = this->SpatiallySmoothVector(initvec, this->m_MaskImageP); |
| 2292 |
initvec = initvec / initvec.two_norm(); |
| 2293 |
this->m_VariatesP.set_column(i, initvec); |
| 2294 |
} |
| 2295 |
} |
| 2296 |
/** now initialize B */ |
| 2297 |
reconerr = this->SparseReconB(matrixB, icept); |
| 2298 |
this->SparseArnoldiSVD_Other(matrixB); |
| 2299 |
if (!this->m_Silent) |
| 2300 |
std::cout << "begin : %var " << reconerr << std::endl; |
| 2301 |
RealType matpfrobnorm = this->m_MatrixP.frobenius_norm(); |
| 2302 |
if (false) |
| 2303 |
for (unsigned int overit = 0; overit < this->m_MaximumNumberOfIterations; overit++) |
| 2304 |
{ |
| 2305 |
MatrixType vgrad = |
| 2306 |
matrixB.transpose() * this->m_MatrixP - (matrixB.transpose() * matrixB) * this->m_VariatesP.transpose(); |
| 2307 |
this->m_VariatesP = this->m_VariatesP + vgrad.transpose(); |
| 2308 |
for (unsigned int a = 0; a < n_vecs; a++) |
| 2309 |
{ |
| 2310 |
VectorType evec = this->m_VariatesP.get_column(a); |
| 2311 |
this->SparsifyP(evec); |
| 2312 |
this->m_VariatesP.set_column(a, evec); |
| 2313 |
} |
| 2314 |
reconerr = this->SparseReconB(matrixB, icept); |
| 2315 |
this->SparseArnoldiSVD_Other(matrixB); |
| 2316 |
this->m_MatrixU = matrixB; |
| 2317 |
if (!this->m_Silent) |
| 2318 |
std::cout << overit << ": %var " << reconerr << std::endl; |
| 2319 |
} |
| 2320 |
if (true) |
| 2321 |
for (unsigned int overit = 0; overit < this->m_MaximumNumberOfIterations; overit++) |
| 2322 |
{ |
| 2323 |
/** a power iteration method --- depends on the following |
| 2324 |
given any nonzero $z \in \mathbb{R}^n$, the Rayleigh quotient |
| 2325 |
$x^T X x / x^T x $ minimizes the function $\| \lambda x - X x \|^2 $ |
| 2326 |
wrt $\lambda$. |
| 2327 |
so, if we find the vector x ( by sparse power iteration ) then we have a vector |
| 2328 |
that is a close approximation to the first eigenvector of X. If X is a residual |
| 2329 |
matrix then x is a good approximation of the $n^th$ eigenvector. |
| 2330 |
*/ |
| 2331 |
VectorType zero(this->m_MatrixP.cols(), 0); |
| 2332 |
VectorType zerob(this->m_MatrixP.rows(), 0); |
| 2333 |
unsigned int a = 0; |
| 2334 |
while (a < n_vecs) |
| 2335 |
{ |
| 2336 |
this->m_FractionNonZeroP = sparsenessparams(a); |
| 2337 |
VectorType bvec = matrixB.get_column(a); |
| 2338 |
matrixB.set_column(a, zerob); |
| 2339 |
MatrixType tempMatrix = this->m_VariatesP; |
| 2340 |
tempMatrix.set_column(a, zero); |
| 2341 |
MatrixType partialmatrix = matrixB * tempMatrix.transpose(); |
| 2342 |
for (unsigned int interc = 0; interc < this->m_MatrixP.rows(); interc++) |
| 2343 |
{ |
| 2344 |
partialmatrix.set_row(interc, partialmatrix.get_row(interc) + icept(interc)); |
| 2345 |
} |
| 2346 |
partialmatrix = this->m_MatrixP - partialmatrix; |
| 2347 |
this->m_CanonicalCorrelations[a] = 1 - (partialmatrix.frobenius_norm()) / matpfrobnorm; |
| 2348 |
VectorType evec = this->m_VariatesP.get_column(a); |
| 2349 |
this->m_VariatesP.set_column(a, zero); |
| 2350 |
this->m_CanonicalCorrelations[a] = this->IHTPowerIteration(partialmatrix, evec, 5, a); // 0 => a |
| 2351 |
this->m_VariatesP.set_column(a, evec); |
| 2352 |
matrixB.set_column(a, bvec); |
| 2353 |
// update B matrix by linear regression |
| 2354 |
reconerr = this->SparseReconB(matrixB, icept); |
| 2355 |
this->SparseArnoldiSVD_Other(matrixB); |
| 2356 |
a++; |
| 2357 |
} // while |
| 2358 |
// update B matrix by linear regression |
| 2359 |
reconerr = this->SparseReconB(matrixB, icept); |
| 2360 |
this->SparseArnoldiSVD_Other(matrixB); |
| 2361 |
this->m_MatrixU = matrixB; |
| 2362 |
if (!this->m_Silent) |
| 2363 |
std::cout << overit << ": %var " << reconerr << std::endl; |
| 2364 |
if ((!prior) && (overit == (this->m_MaximumNumberOfIterations - 1))) |
| 2365 |
this->SortResults(n_vecs); |
| 2366 |
} |
| 2367 |
this->m_VariatesQ = matrixB; |
| 2368 |
for (unsigned int i = 0; i < n_vecs; i++) |
| 2369 |
{ |
| 2370 |
VectorType v = this->m_VariatesP.get_column(i); |
| 2371 |
if (v.min_value() < 0) |
| 2372 |
{ |
| 2373 |
this->m_VariatesP.set_column(i, v * (-1)); |
| 2374 |
} |
| 2375 |
} |
| 2376 |
return 1.0 / reconerr; |
| 2377 |
/** a regression-based method |
| 2378 |
for( unsigned int a = 0; a < this->m_MatrixP.cols(); a++ ) |
| 2379 |
{ |
| 2380 |
VectorType x_i = this->m_MatrixP.get_column( a ); |
| 2381 |
VectorType lmsolv = this->m_VariatesP.get_row( a ); // good initialization should increase convergence speed |
| 2382 |
(void) this->ConjGrad( matrixB, lmsolv, x_i, 0, 10000 ); // A x = b |
| 2383 |
VectorType x_recon = ( matrixB * lmsolv + this->m_Intercept ); |
| 2384 |
onenorm += x_i.one_norm() / this->m_MatrixP.rows(); |
| 2385 |
reconerr += ( x_i - x_recon ).one_norm() / this->m_MatrixP.rows(); |
| 2386 |
this->m_VariatesP.set_row( a , lmsolv ); |
| 2387 |
} |
| 2388 |
*/ |
| 2389 |
|
| 2390 |
/* a gradient method |
| 2391 |
E & = \langle X - U V^T , X - U V^T \rangle \\ |
| 2392 |
\partial E/\partial V & = \langle -U , X - U V^T \rangle \\ |
| 2393 |
& = -U^T X + U^T U V^T \rangle |
| 2394 |
if ( overit == 0 ) this->m_VariatesP.fill( 0 ); |
| 2395 |
MatrixType vgrad = matrixB.transpose() * this->m_MatrixP - |
| 2396 |
( matrixB.transpose() * matrixB ) * this->m_VariatesP.transpose(); |
| 2397 |
// this->m_VariatesP = this->m_VariatesP + vgrad.transpose() * 0.01; |
| 2398 |
this->m_VariatesP = this->m_VariatesP + vgrad.transpose() ; |
| 2399 |
for( unsigned int a = 0; a < this->m_MatrixP.rows(); a++ ) |
| 2400 |
{ |
| 2401 |
VectorType evec = this->m_VariatesP.get_column( a ); |
| 2402 |
this->SparsifyP( evec ); |
| 2403 |
this->m_VariatesP.set_column( a, evec ); |
| 2404 |
} |
| 2405 |
*/ |
| 2406 |
} |
| 2407 |
|
| 2408 |
|
| 2409 |
template <typename TInputImage, typename TRealType> |
| 2410 |
TRealType |
| 2411 |
antsSCCANObject<TInputImage, TRealType>::IHTPowerIterationHome( |
| 2412 |
typename antsSCCANObject<TInputImage, TRealType>::MatrixType & A, |
| 2413 |
typename antsSCCANObject<TInputImage, TRealType>::VectorType & evec, |
| 2414 |
// typename antsSCCANObject<TInputImage, TRealType>::VectorType& y, |
| 2415 |
unsigned int maxits, |
| 2416 |
unsigned int maxorth) |
| 2417 |
{ |
| 2418 |
/** This computes a hard-thresholded gradient descent on the eigenvector criterion. |
| 2419 |
max x over x^t A^t A x s.t. x^t x = 1 |
| 2420 |
success of the optimization is measured by rayleigh quotient. derivative is: |
| 2421 |
d.dx ( x^t A^t A x ) = A^t A x , x \leftarrow x / \| x \| |
| 2422 |
we use a conjugate gradient version of this optimization. |
| 2423 |
*/ |
| 2424 |
if (itk::Math::FloatAlmostEqual(evec.two_norm(), itk::NumericTraits<typename VectorType::abs_t>::ZeroValue())) |
| 2425 |
{ |
| 2426 |
evec = this->InitializeV(this->m_MatrixP, true); |
| 2427 |
} |
| 2428 |
VectorType proj = (A * evec); |
| 2429 |
VectorType lastgrad = evec; |
| 2430 |
RealType rayquo = 0, rayquold = -1; |
| 2431 |
RealType denom = inner_product(evec, evec); |
| 2432 |
if (denom > 0) |
| 2433 |
{ |
| 2434 |
rayquo = inner_product(proj, proj) / denom; |
| 2435 |
} |
| 2436 |
MatrixType At = A.transpose(); |
| 2437 |
unsigned int powerits = 0; |
| 2438 |
bool conjgrad = true; |
| 2439 |
VectorType bestevec = evec; |
| 2440 |
while (((rayquo > rayquold) && (powerits < maxits))) |
| 2441 |
{ |
| 2442 |
VectorType nvec = At * proj; |
| 2443 |
for (unsigned int orth = 0; orth < maxorth; orth++) |
| 2444 |
{ |
| 2445 |
nvec = this->Orthogonalize(nvec, this->m_VariatesP.get_column(orth)); |
| 2446 |
} |
| 2447 |
RealType gamma = 0.1; |
| 2448 |
nvec = this->SpatiallySmoothVector(nvec, this->m_MaskImageP); |
| 2449 |
if ((lastgrad.two_norm() > 0) && (conjgrad)) |
| 2450 |
{ |
| 2451 |
gamma = inner_product(nvec, nvec) / inner_product(lastgrad, lastgrad); |
| 2452 |
} |
| 2453 |
lastgrad = nvec; |
| 2454 |
evec = evec * gamma + nvec; |
| 2455 |
evec = this->SpatiallySmoothVector(evec, this->m_MaskImageP); |
| 2456 |
// this->CurvatureSparseness( evec, ( 1 - this->m_FractionNonZeroP ) * 100, 5 , this->m_MaskImageP ); |
| 2457 |
|
| 2458 |
this->SparsifyP(evec); |
| 2459 |
// VectorType gradvec = this->ComputeVectorGradMag( evec, this->m_MaskImageP ); |
| 2460 |
if (evec.two_norm() > 0) |
| 2461 |
{ |
| 2462 |
evec = evec / evec.two_norm(); |
| 2463 |
} |
| 2464 |
proj = (A * evec); |
| 2465 |
rayquold = rayquo; |
| 2466 |
denom = inner_product(evec, evec); |
| 2467 |
if (denom > 0) |
| 2468 |
{ |
| 2469 |
rayquo = inner_product(proj, proj) / denom; // - gradvec.two_norm() / gradvec.size() * 1.e2 ; |
| 2470 |
} |
| 2471 |
powerits++; |
| 2472 |
if (rayquo > rayquold) |
| 2473 |
{ |
| 2474 |
bestevec = evec; |
| 2475 |
} |
| 2476 |
} |
| 2477 |
|
| 2478 |
evec = bestevec; |
| 2479 |
if (!this->m_Silent) |
| 2480 |
std::cout << "rayleigh-quotient: " << rayquo << " in " << powerits << " num " << maxorth << std::endl; |
| 2481 |
return rayquo; |
| 2482 |
} |
| 2483 |
|
| 2484 |
template <typename TInputImage, typename TRealType> |
| 2485 |
TRealType |
| 2486 |
antsSCCANObject<TInputImage, TRealType>::SparseReconHome(unsigned int n_vecs) |
| 2487 |
{ |
| 2488 |
// this->m_UseL1 = false; |
| 2489 |
RealType reconerr = 0; |
| 2490 |
RealType onenorm = 0; |
| 2491 |
|
| 2492 |
this->m_CanonicalCorrelations.set_size(n_vecs); |
| 2493 |
this->m_CanonicalCorrelations.fill(0); |
| 2494 |
if (!this->m_Silent) |
| 2495 |
std::cout << " sparse recon " << this->m_MinClusterSizeP << " useL1 " << this->m_UseL1 << std::endl; |
| 2496 |
MatrixType matrixB(this->m_OriginalMatrixP.rows(), n_vecs); |
| 2497 |
matrixB.fill(0); |
| 2498 |
this->m_MatrixP = this->NormalizeMatrix(this->m_OriginalMatrixP); |
| 2499 |
MatrixType cov = this->m_MatrixP * this->m_MatrixP.transpose(); |
| 2500 |
vnl_svd<RealType> eig(cov, 1.e-6); |
| 2501 |
this->m_VariatesP.set_size(this->m_MatrixP.cols(), n_vecs); |
| 2502 |
this->m_VariatesP.fill(0); |
| 2503 |
for (unsigned int i = 0; i < n_vecs; i++) |
| 2504 |
{ |
| 2505 |
if (i < this->m_MatrixP.rows()) |
| 2506 |
{ |
| 2507 |
VectorType u = eig.U().get_column(i); |
| 2508 |
VectorType up = u * this->m_MatrixP; |
| 2509 |
this->SparsifyP(up); |
| 2510 |
this->m_VariatesP.set_column(i, up); |
| 2511 |
matrixB.set_column(i, u); |
| 2512 |
} |
| 2513 |
else |
| 2514 |
{ |
| 2515 |
this->m_VariatesP.set_column(i, this->InitializeV(this->m_MatrixP, false)); |
| 2516 |
} |
| 2517 |
} |
| 2518 |
VectorType icept(this->m_MatrixP.rows(), 0); |
| 2519 |
RealType matpfrobnorm = this->m_MatrixP.frobenius_norm(); |
| 2520 |
for (unsigned int overit = 0; overit < this->m_MaximumNumberOfIterations; overit++) |
| 2521 |
{ |
| 2522 |
// cov = this->m_VariatesP.transpose() * this->m_VariatesP; |
| 2523 |
// vnl_svd<double> qr( cov ); |
| 2524 |
// this->m_VariatesP = this->m_VariatesP * qr.U(); |
| 2525 |
// update V matrix |
| 2526 |
/** a power iteration method --- depends on the following |
| 2527 |
|
| 2528 |
given any nonzero $z \in \mathbb{R}^n$, the Rayleigh quotient |
| 2529 |
$x^T X x / x^T x $ minimizes the function $\| \lambda x - X x \|^2 $ |
| 2530 |
wrt $\lambda$. |
| 2531 |
|
| 2532 |
so, if we find the vector x ( by sparse power iteration ) then we have a vector |
| 2533 |
that is a close approximation to the first eigenvector of X. If X is a residual |
| 2534 |
matrix then x is a good approximation of the $n^th$ eigenvector. |
| 2535 |
|
| 2536 |
**/ |
| 2537 |
VectorType zero(this->m_MatrixP.cols(), 0); |
| 2538 |
VectorType zerob(this->m_MatrixP.rows(), 0); |
| 2539 |
for (unsigned int a = 0; a < n_vecs; a++) |
| 2540 |
{ |
| 2541 |
VectorType bvec = matrixB.get_column(a); |
| 2542 |
matrixB.set_column(a, zerob); |
| 2543 |
MatrixType tempMatrix = this->m_VariatesP; |
| 2544 |
tempMatrix.set_column(a, zero); |
| 2545 |
MatrixType partialmatrix = matrixB * tempMatrix.transpose(); |
| 2546 |
for (unsigned int interc = 0; interc < this->m_MatrixP.rows(); interc++) |
| 2547 |
{ |
| 2548 |
partialmatrix.set_row(interc, partialmatrix.get_row(interc) + icept(interc)); |
| 2549 |
} |
| 2550 |
this->m_CanonicalCorrelations[a] = (partialmatrix.frobenius_norm()) / matpfrobnorm; |
| 2551 |
partialmatrix = this->m_MatrixP - partialmatrix; |
| 2552 |
VectorType evec = this->m_VariatesP.get_column(a); |
| 2553 |
this->m_VariatesP.set_column(a, zero); |
| 2554 |
if (evec.two_norm() > 0) |
| 2555 |
{ |
| 2556 |
evec = evec / evec.two_norm(); |
| 2557 |
} |
| 2558 |
|
| 2559 |
// get 1st eigenvector ... how should this be done? how about svd? |
| 2560 |
if ((a >= this->m_MatrixP.rows()) && (overit == 0)) |
| 2561 |
{ |
| 2562 |
(void)this->PowerIteration(partialmatrix, evec, 10, false); |
| 2563 |
} |
| 2564 |
this->m_CanonicalCorrelations[a] = this->IHTPowerIterationHome(partialmatrix, evec, 20, 0); // 0 => a |
| 2565 |
this->m_VariatesP.set_column(a, evec); |
| 2566 |
matrixB.set_column(a, bvec); |
| 2567 |
} |
| 2568 |
|
| 2569 |
// update B matrix by linear regression |
| 2570 |
reconerr = onenorm = 0; |
| 2571 |
icept.fill(0); |
| 2572 |
for (unsigned int a = 0; a < this->m_MatrixP.rows(); a++) |
| 2573 |
{ |
| 2574 |
VectorType x_i = this->m_MatrixP.get_row(a); |
| 2575 |
VectorType lmsolv = matrixB.get_row(a); // good initialization should increase |
| 2576 |
// convergence speed |
| 2577 |
(void)this->ConjGrad(this->m_VariatesP, lmsolv, x_i, 0, 10000); // A x = b |
| 2578 |
VectorType x_recon = (this->m_VariatesP * lmsolv + this->m_Intercept); |
| 2579 |
icept(a) = this->m_Intercept; |
| 2580 |
onenorm += x_i.one_norm() / this->m_MatrixP.cols(); |
| 2581 |
reconerr += (x_i - x_recon).one_norm() / this->m_MatrixP.cols(); |
| 2582 |
matrixB.set_row(a, lmsolv); |
| 2583 |
} |
| 2584 |
RealType rr = (onenorm - reconerr) / onenorm; |
| 2585 |
if (!this->m_Silent) |
| 2586 |
std::cout << overit << ": %var " << rr << " raw-reconerr " << reconerr << std::endl; |
| 2587 |
} |
| 2588 |
this->m_VariatesQ = matrixB; |
| 2589 |
this->SortResults(n_vecs); |
| 2590 |
// this->m_UseL1 = false; |
| 2591 |
return 1.0 / reconerr; |
| 2592 |
} |
| 2593 |
|
| 2594 |
template <typename TInputImage, typename TRealType> |
| 2595 |
TRealType |
| 2596 |
antsSCCANObject<TInputImage, TRealType>::SparseArnoldiSVDGreedy(unsigned int n_vecs) |
| 2597 |
{ |
| 2598 |
if (!this->m_Silent) |
| 2599 |
std::cout << " arnoldi sparse svd : greedy " << this->m_MinClusterSizeP << " sparseness " |
| 2600 |
<< this->m_FractionNonZeroP << std::endl; |
| 2601 |
std::vector<RealType> vexlist; |
| 2602 |
|
| 2603 |
vexlist.push_back(0); |
| 2604 |
this->m_MatrixP = this->NormalizeMatrix(this->m_OriginalMatrixP); |
| 2605 |
this->m_MatrixQ = this->m_MatrixP; |
| 2606 |
if (this->m_OriginalMatrixR.size() > 0) |
| 2607 |
{ |
| 2608 |
this->m_MatrixRRt = this->ProjectionMatrix(this->m_OriginalMatrixR); |
| 2609 |
if (!this->m_Silent) |
| 2610 |
std::cout << " Subtracting nuisance matrix from P matrix " << std::endl; |
| 2611 |
this->m_MatrixP = this->m_MatrixP - (this->m_MatrixRRt * this->m_MatrixP); |
| 2612 |
} |
| 2613 |
this->m_VariatesP.set_size(this->m_MatrixP.cols(), n_vecs); |
| 2614 |
MatrixType bmatrix = this->GetCovMatEigenvectors(this->m_MatrixP); |
| 2615 |
MatrixType bmatrix_big; |
| 2616 |
bmatrix_big.set_size(this->m_MatrixP.cols(), n_vecs); |
| 2617 |
// double trace = vnl_trace<double>( this->m_MatrixP * this->m_MatrixP.transpose() ); |
| 2618 |
for (unsigned int kk = 0; kk < n_vecs; kk++) |
| 2619 |
{ |
| 2620 |
this->m_VariatesP.set_column(kk, this->InitializeV(this->m_MatrixP)); |
| 2621 |
if (kk < bmatrix.columns() && true) |
| 2622 |
{ |
| 2623 |
VectorType initv = bmatrix.get_column(kk) * this->m_MatrixP; |
| 2624 |
this->SparsifyP(initv); |
| 2625 |
this->m_VariatesP.set_column(kk, initv); |
| 2626 |
} |
| 2627 |
} |
| 2628 |
const unsigned int maxloop = this->m_MaximumNumberOfIterations; |
| 2629 |
// Arnoldi Iteration SVD/SPCA |
| 2630 |
unsigned int loop = 0; |
| 2631 |
bool debug = false; |
| 2632 |
double convcrit = 1; |
| 2633 |
RealType fnp = 1; |
| 2634 |
bool negate = false; |
| 2635 |
MatrixType lastV = this->m_VariatesP; |
| 2636 |
lastV.fill(0); |
| 2637 |
while (loop < maxloop && (convcrit) > 1.e-8) |
| 2638 |
{ |
| 2639 |
/** Compute the gradient estimate according to standard Arnoldi */ |
| 2640 |
fnp = this->m_FractionNonZeroP; |
| 2641 |
// MatrixType cov = this->m_VariatesP.transpose() * this->m_VariatesP; |
| 2642 |
// vnl_svd<double> qr( cov ); |
| 2643 |
// this->m_VariatesP = this->m_VariatesP * qr.U(); |
| 2644 |
for (unsigned int k = 0; k < n_vecs; k++) |
| 2645 |
{ |
| 2646 |
VectorType pveck = this->m_VariatesP.get_column(k); |
| 2647 |
MatrixType pmod = this->NormalizeMatrix(this->m_OriginalMatrixP); |
| 2648 |
if (k > 1 && loop > 1 && k < this->m_MatrixP.rows() - 1 && false) |
| 2649 |
{ |
| 2650 |
MatrixType m(this->m_MatrixP.rows(), k, 0); |
| 2651 |
for (unsigned int mm = 0; mm < k; mm++) |
| 2652 |
{ |
| 2653 |
m.set_column(mm, this->m_MatrixP * this->m_VariatesP.get_column(mm)); |
| 2654 |
} |
| 2655 |
MatrixType projmat = this->ProjectionMatrix(m, 1.e-8); |
| 2656 |
pmod = pmod - projmat * pmod; |
| 2657 |
} |
| 2658 |
for (unsigned int powerit = 0; powerit < 5; powerit++) |
| 2659 |
{ |
| 2660 |
pveck = (pmod * pveck); // classic |
| 2661 |
pveck = pmod.transpose() * (pveck); |
| 2662 |
RealType hkkm1 = pveck.two_norm(); |
| 2663 |
if (hkkm1 > 0) |
| 2664 |
{ |
| 2665 |
pveck = pveck / hkkm1; |
| 2666 |
} |
| 2667 |
for (unsigned int j = 0; j < k; j++) |
| 2668 |
{ |
| 2669 |
VectorType qj = this->m_VariatesP.get_column(j); |
| 2670 |
pveck = this->Orthogonalize(pveck, qj); |
| 2671 |
} |
| 2672 |
} // powerit |
| 2673 |
/** penalize by gradient |
| 2674 |
RealType spgoal = ( 1 - fnp ) * 100 ; |
| 2675 |
this->CurvatureSparseness( pveck , spgoal );*/ |
| 2676 |
|
| 2677 |
/** Project to the feasible sub-space */ |
| 2678 |
if (negate) |
| 2679 |
{ |
| 2680 |
pveck = pveck * (-1); |
| 2681 |
} |
| 2682 |
if (fnp < 1) |
| 2683 |
{ |
| 2684 |
if (this->m_KeepPositiveP) |
| 2685 |
{ |
| 2686 |
this->ConstantProbabilityThreshold(pveck, fnp, this->m_KeepPositiveP); |
| 2687 |
} |
| 2688 |
else |
| 2689 |
{ |
| 2690 |
this->ReSoftThreshold(pveck, fnp, !this->m_KeepPositiveP); |
| 2691 |
} |
| 2692 |
this->ClusterThresholdVariate(pveck, this->m_MaskImageP, this->m_MinClusterSizeP); |
| 2693 |
if (pveck.two_norm() > 0) |
| 2694 |
{ |
| 2695 |
pveck = pveck / pveck.two_norm(); |
| 2696 |
} |
| 2697 |
} |
| 2698 |
if (negate) |
| 2699 |
{ |
| 2700 |
pveck = pveck * (-1); |
| 2701 |
} |
| 2702 |
/******************************** |
| 2703 |
unsigned int bcolind = 0; |
| 2704 |
RealType maxcorr = 0; |
| 2705 |
for ( unsigned int cc = 0 ; cc < bmatrix_big.cols(); cc ++ ) |
| 2706 |
{ |
| 2707 |
RealType corr = fabs( this->PearsonCorr( this->m_MatrixP * pveck , bmatrix.get_column( cc ) ) ); |
| 2708 |
if ( corr > maxcorr ) bcolind = cc; |
| 2709 |
} |
| 2710 |
VectorType b = bmatrix_big.get_column( bcolind ) ; |
| 2711 |
RealType cgerr = this->SparseConjGrad( pveck , b , 1.e-3 ); |
| 2712 |
********************************/ |
| 2713 |
/** Update the solution */ |
| 2714 |
this->m_VariatesP.set_column(k, pveck); |
| 2715 |
} // kloop |
| 2716 |
// compute the reconstruction error for each voxel |
| 2717 |
/* Now get the LSQ regression solution |
| 2718 |
X - n \times p matrix of subjects by voxels thickness measures |
| 2719 |
|
| 2720 |
B - k \times p matrix of roi measures --- some row has p voxels and that row is binary i.e. non-zero in the voxels |
| 2721 |
where that label is non-zero ( out of e.g. the whole cortex) |
| 2722 |
|
| 2723 |
\beta - 1 \times k vector of beta values computed from the regression solution |
| 2724 |
|
| 2725 |
X_i --- 1 \times n some voxel for each subject |
| 2726 |
|
| 2727 |
then solve , for each voxel , |
| 2728 |
|
| 2729 |
\| X_i - \beta B X \| |
| 2730 |
1xn 1xk kxp pxn |
| 2731 |
|
| 2732 |
\| X - \Beta B X \| |
| 2733 |
pxn pxk kxp pxn |
| 2734 |
|
| 2735 |
|
| 2736 |
voxels - |
| 2737 |
|
| 2738 |
requires you to have n > k --- which you should have .... and easily done in R b.c you precomputed B X => a k |
| 2739 |
\times n matrix |
| 2740 |
|
| 2741 |
this means that you can reconstruct all voxels for a subject given the measures of his ROI values --- so it's basically |
| 2742 |
a regression across all voxels given the ROI ( or PCA ) values from that subject. the reconstruction error is very |
| 2743 |
descriptive about the information gained by the dimensionality reduction used for that data. RealType totalerr = 0; |
| 2744 |
MatrixType A = ( this->m_MatrixP * this->m_VariatesP ); |
| 2745 |
MatrixType reconmat( this->m_MatrixP.rows(), this->m_MatrixP.cols(), 0 ); |
| 2746 |
for ( unsigned int vox = 0; vox < this->m_MatrixP.cols(); vox++ ) |
| 2747 |
{ |
| 2748 |
VectorType lmsol( n_vecs, 0); |
| 2749 |
VectorType voxels = this->m_MatrixP.get_column( vox ); |
| 2750 |
RealType lmerror = this->ConjGrad( A , lmsol , voxels , 0 , 100 ); |
| 2751 |
VectorType proj = A * lmsol; |
| 2752 |
if ( std::isnan( lmerror ) ) { lmerror = 0 ; proj.fill(0); } |
| 2753 |
reconmat.set_column( vox , proj ); |
| 2754 |
totalerr += lmerror; |
| 2755 |
} |
| 2756 |
totalerr /= this->m_MatrixP.cols() ; |
| 2757 |
if ( ! this->m_Silent ) std::cout << " totalerr " << totalerr << std::endl; |
| 2758 |
{ |
| 2759 |
VectorType vvv = reconmat.get_row( 10 ); |
| 2760 |
typename TInputImage::Pointer image=this->ConvertVariateToSpatialImage( vvv , this->m_MaskImageP , false ); |
| 2761 |
typedef itk::ImageFileWriter<TInputImage> WriterType; |
| 2762 |
typename WriterType::Pointer writer = WriterType::New(); |
| 2763 |
writer->SetFileName( "temp.nii.gz" ); |
| 2764 |
writer->SetInput( image ); |
| 2765 |
writer->Update(); |
| 2766 |
} |
| 2767 |
{ |
| 2768 |
VectorType vvv = this->m_MatrixP.get_row( 10 ); |
| 2769 |
typename TInputImage::Pointer image=this->ConvertVariateToSpatialImage( vvv , this->m_MaskImageP , false ); |
| 2770 |
typedef itk::ImageFileWriter<TInputImage> WriterType; |
| 2771 |
typename WriterType::Pointer writer = WriterType::New(); |
| 2772 |
writer->SetFileName( "temp2.nii.gz" ); |
| 2773 |
writer->SetInput( image ); |
| 2774 |
writer->Update(); |
| 2775 |
} |
| 2776 |
*/ |
| 2777 |
this->m_VariatesQ = this->m_VariatesP; |
| 2778 |
/** Estimate eigenvalues , then sort */ |
| 2779 |
|
| 2780 |
RealType bestvex = // this->ComputeSPCAEigenvalues( n_vecs, trace, false ); |
| 2781 |
this->ReconstructionError(this->m_MatrixP, this->m_VariatesP); |
| 2782 |
/* begin the crude line search */ |
| 2783 |
if (bestvex < vexlist[loop]) |
| 2784 |
{ |
| 2785 |
RealType f = -0.5, enew = 0, eold = -1, delt = 0.05; |
| 2786 |
bestvex = 0; |
| 2787 |
MatrixType bestV = this->m_VariatesP; |
| 2788 |
while ((enew - eold) > 0 && f <= 0.5 && loop > 0 && true) |
| 2789 |
{ |
| 2790 |
this->m_VariatesP = (this->m_VariatesQ * f + lastV * (1 - f)); |
| 2791 |
for (unsigned int m = 0; m < this->m_VariatesP.cols(); m++) |
| 2792 |
{ |
| 2793 |
VectorType mm = this->m_VariatesP.get_column(m); |
| 2794 |
this->SparsifyP(mm); |
| 2795 |
if (mm.two_norm() > 0) |
| 2796 |
{ |
| 2797 |
mm = mm / mm.two_norm(); |
| 2798 |
} |
| 2799 |
this->m_VariatesP.set_column(m, mm); |
| 2800 |
} |
| 2801 |
eold = enew; |
| 2802 |
enew = this->ReconstructionError(this->m_MatrixP, this->m_VariatesP); |
| 2803 |
if (enew > bestvex) |
| 2804 |
{ |
| 2805 |
bestvex = enew; |
| 2806 |
bestV = this->m_VariatesP; |
| 2807 |
} |
| 2808 |
// if ( ! this->m_Silent ) std::cout <<" vex " << enew << " f " << f << std::endl; |
| 2809 |
f = f + delt; |
| 2810 |
} |
| 2811 |
|
| 2812 |
this->m_VariatesP = bestV; |
| 2813 |
} |
| 2814 |
if (bestvex < vexlist[loop]) |
| 2815 |
{ |
| 2816 |
this->m_VariatesP = lastV; |
| 2817 |
bestvex = vexlist[loop]; |
| 2818 |
} |
| 2819 |
|
| 2820 |
lastV = this->m_VariatesP; |
| 2821 |
RealType vex = bestvex; |
| 2822 |
vexlist.push_back(vex); |
| 2823 |
this->SortResults(n_vecs); |
| 2824 |
convcrit = (this->ComputeEnergySlope(vexlist, 10)); |
| 2825 |
if (itk::Math::FloatAlmostEqual(bestvex, vexlist[loop])) |
| 2826 |
{ |
| 2827 |
convcrit = 0; |
| 2828 |
} |
| 2829 |
if (!this->m_Silent) |
| 2830 |
std::cout << "Iteration: " << loop << " Eval_0: " << this->m_CanonicalCorrelations[0] |
| 2831 |
<< " Eval_N: " << this->m_CanonicalCorrelations[n_vecs - 1] << " Sp: " << fnp |
| 2832 |
<< " conv-crit: " << convcrit << " %var " << bestvex << std::endl; |
| 2833 |
loop++; |
| 2834 |
if (debug) |
| 2835 |
{ |
| 2836 |
if (!this->m_Silent) |
| 2837 |
std::cout << "wloopdone" << std::endl; |
| 2838 |
} |
| 2839 |
} // opt-loop |
| 2840 |
for (unsigned int i = 0; i < vexlist.size(); i++) |
| 2841 |
{ |
| 2842 |
if (!this->m_Silent) |
| 2843 |
std::cout << vexlist[i] << ","; |
| 2844 |
} |
| 2845 |
if (!this->m_Silent) |
| 2846 |
std::cout << std::endl; |
| 2847 |
return fabs(this->m_CanonicalCorrelations[0]); |
| 2848 |
} |
| 2849 |
|
| 2850 |
template <typename TInputImage, typename TRealType> |
| 2851 |
TRealType |
| 2852 |
antsSCCANObject<TInputImage, TRealType>::SparseArnoldiSVD_x(unsigned int n_vecs) |
| 2853 |
{ |
| 2854 |
this->m_CanonicalCorrelations.set_size(n_vecs); |
| 2855 |
this->m_CanonicalCorrelations.fill(0); |
| 2856 |
if (!this->m_Silent) |
| 2857 |
std::cout << " arnoldi sparse svd " << std::endl; |
| 2858 |
std::vector<RealType> vexlist; |
| 2859 |
this->m_MatrixP = this->NormalizeMatrix(this->m_OriginalMatrixP); |
| 2860 |
this->m_MatrixQ = this->m_MatrixP; |
| 2861 |
if (this->m_OriginalMatrixR.size() > 0) |
| 2862 |
{ |
| 2863 |
this->m_MatrixRRt = this->ProjectionMatrix(this->m_OriginalMatrixR); |
| 2864 |
if (this->m_SCCANFormulation == PminusRQ || this->m_SCCANFormulation == PminusRQminusR) |
| 2865 |
{ |
| 2866 |
if (!this->m_Silent) |
| 2867 |
std::cout << " Subtracting nuisance matrix from P matrix " << std::endl; |
| 2868 |
this->m_MatrixP = this->m_MatrixP - (this->m_MatrixRRt * this->m_MatrixP); |
| 2869 |
} |
| 2870 |
} |
| 2871 |
this->m_ClusterSizes.set_size(n_vecs); |
| 2872 |
this->m_ClusterSizes.fill(0); |
| 2873 |
double trace = 0; |
| 2874 |
for (unsigned int i = 0; i < this->m_MatrixP.cols(); i++) |
| 2875 |
{ |
| 2876 |
trace += inner_product(this->m_MatrixP.get_column(i), this->m_MatrixP.get_column(i)); |
| 2877 |
} |
| 2878 |
this->m_VariatesP.set_size(this->m_MatrixP.cols(), n_vecs); |
| 2879 |
VariateType myGradients; |
| 2880 |
this->m_SparseVariatesP.set_size(this->m_MatrixP.cols(), n_vecs); |
| 2881 |
this->m_SparseVariatesP.fill(0); |
| 2882 |
myGradients.set_size(this->m_MatrixP.cols(), n_vecs); |
| 2883 |
myGradients.fill(0); |
| 2884 |
for (unsigned int kk = 0; kk < n_vecs; kk++) |
| 2885 |
{ |
| 2886 |
this->m_VariatesP.set_column(kk, this->InitializeV(this->m_MatrixP)); |
| 2887 |
} |
| 2888 |
const unsigned int maxloop = this->m_MaximumNumberOfIterations; |
| 2889 |
// Arnoldi Iteration SVD/SPCA |
| 2890 |
unsigned int loop = 0; |
| 2891 |
bool debug = false; |
| 2892 |
double convcrit = 1; |
| 2893 |
RealType fnp = 1; |
| 2894 |
while (loop < maxloop && (convcrit) > 1.e-8) |
| 2895 |
{ |
| 2896 |
/** Compute the gradient estimate */ |
| 2897 |
fnp = this->m_FractionNonZeroP; |
| 2898 |
for (unsigned int k = 0; k < n_vecs; k++) |
| 2899 |
{ |
| 2900 |
// the old solution |
| 2901 |
VectorType pveck = this->m_VariatesP.get_column(k); |
| 2902 |
pveck = this->m_MatrixP.transpose() * (this->m_MatrixP * pveck); |
| 2903 |
RealType hkkm1 = pveck.two_norm(); |
| 2904 |
if (hkkm1 > 0) |
| 2905 |
{ |
| 2906 |
pveck = pveck / hkkm1; |
| 2907 |
} |
| 2908 |
for (unsigned int j = 0; j < k; j++) |
| 2909 |
{ |
| 2910 |
VectorType qj = this->m_VariatesP.get_column(j); |
| 2911 |
RealType ip = inner_product(qj, qj); |
| 2912 |
if (ip < this->m_Epsilon) |
| 2913 |
{ |
| 2914 |
ip = 1; |
| 2915 |
} |
| 2916 |
RealType hjk = inner_product(qj, pveck) / ip; |
| 2917 |
pveck = pveck - qj * hjk; |
| 2918 |
} |
| 2919 |
// pveck is now the standard arnoldi pca update to the solution |
| 2920 |
VectorType ppp(pveck); |
| 2921 |
if (fnp < 1) |
| 2922 |
{ |
| 2923 |
if (this->m_KeepPositiveP) |
| 2924 |
{ |
| 2925 |
this->ConstantProbabilityThreshold(ppp, fnp, this->m_KeepPositiveP); |
| 2926 |
} |
| 2927 |
else |
| 2928 |
{ |
| 2929 |
this->ReSoftThreshold(ppp, fnp, !this->m_KeepPositiveP); |
| 2930 |
} |
| 2931 |
this->ClusterThresholdVariate(ppp, this->m_MaskImageP, this->m_MinClusterSizeP); |
| 2932 |
ppp = ppp / ppp.two_norm(); |
| 2933 |
} |
| 2934 |
myGradients.set_column(k, ppp - pveck); |
| 2935 |
myGradients.set_column(k, ppp); |
| 2936 |
if (loop == 0) |
| 2937 |
{ |
| 2938 |
this->m_VariatesP.set_column(k, pveck); |
| 2939 |
} |
| 2940 |
if (loop == 0) |
| 2941 |
{ |
| 2942 |
myGradients.set_column(k, pveck); |
| 2943 |
} |
| 2944 |
// this->m_VariatesP.set_column(k, this->m_SparseVariatesP.get_column(k) ); |
| 2945 |
// this->m_SparseVariatesP.set_column(k,pveck); |
| 2946 |
} // kloop |
| 2947 |
|
| 2948 |
/** Update the full solution space */ |
| 2949 |
double recuravg = .9; // /sqrt(sqrt((double)(loop+1))); |
| 2950 |
double recuravg2 = 1 - recuravg; |
| 2951 |
for (unsigned int k = 0; k < n_vecs; k++) |
| 2952 |
{ |
| 2953 |
VectorType pveck = myGradients.get_column(k); |
| 2954 |
VectorType newsol = this->m_SparseVariatesP.get_column(k) * recuravg2 + pveck * recuravg; |
| 2955 |
this->m_SparseVariatesP.set_column(k, newsol); // /newsol.two_norm() |
| 2956 |
} |
| 2957 |
/** Update the projected solution in sub-space */ |
| 2958 |
for (unsigned int k = 0; k < n_vecs; k++) |
| 2959 |
{ |
| 2960 |
VectorType pveck = this->m_SparseVariatesP.get_column(k); |
| 2961 |
pveck = this->m_MatrixP.transpose() * (this->m_MatrixP * pveck); |
| 2962 |
RealType hkkm1 = pveck.two_norm(); |
| 2963 |
if (hkkm1 > 0) |
| 2964 |
{ |
| 2965 |
pveck = pveck / hkkm1; |
| 2966 |
} |
| 2967 |
for (unsigned int j = 0; j < k; j++) |
| 2968 |
{ |
| 2969 |
VectorType qj = this->m_VariatesP.get_column(j); |
| 2970 |
RealType ip = inner_product(qj, qj); |
| 2971 |
if (ip < this->m_Epsilon) |
| 2972 |
{ |
| 2973 |
ip = 1; |
| 2974 |
} |
| 2975 |
RealType hjk = inner_product(qj, pveck) / ip; |
| 2976 |
pveck = pveck - qj * hjk; |
| 2977 |
} |
| 2978 |
if (fnp < 1) |
| 2979 |
{ |
| 2980 |
if (this->m_KeepPositiveP) |
| 2981 |
{ |
| 2982 |
this->ConstantProbabilityThreshold(pveck, fnp, this->m_KeepPositiveP); |
| 2983 |
} |
| 2984 |
else |
| 2985 |
{ |
| 2986 |
this->ReSoftThreshold(pveck, fnp, !this->m_KeepPositiveP); |
| 2987 |
} |
| 2988 |
this->ClusterThresholdVariate(pveck, this->m_MaskImageP, this->m_MinClusterSizeP); |
| 2989 |
pveck = pveck / pveck.two_norm(); |
| 2990 |
} |
| 2991 |
this->m_VariatesP.set_column(k, pveck); |
| 2992 |
} // kloop for update |
| 2993 |
|
| 2994 |
this->m_VariatesQ = this->m_VariatesP; |
| 2995 |
if (debug) |
| 2996 |
{ |
| 2997 |
if (!this->m_Silent) |
| 2998 |
std::cout << " get evecs " << std::endl; |
| 2999 |
} |
| 3000 |
RealType vex = this->ComputeSPCAEigenvalues(n_vecs, trace, true); |
| 3001 |
vexlist.push_back(vex); |
| 3002 |
this->SortResults(n_vecs); |
| 3003 |
convcrit = (this->ComputeEnergySlope(vexlist, 5)); |
| 3004 |
if (!this->m_Silent) |
| 3005 |
std::cout << "Iteration: " << loop << " Eigenval_0: " << this->m_CanonicalCorrelations[0] |
| 3006 |
<< " Eigenval_N: " << this->m_CanonicalCorrelations[n_vecs - 1] << " Sparseness: " << fnp |
| 3007 |
<< " convergence-criterion: " << convcrit << " vex " << vex << std::endl; |
| 3008 |
loop++; |
| 3009 |
if (debug) |
| 3010 |
{ |
| 3011 |
if (!this->m_Silent) |
| 3012 |
std::cout << "wloopdone" << std::endl; |
| 3013 |
} |
| 3014 |
} // opt-loop |
| 3015 |
for (unsigned int i = 0; i < vexlist.size(); i++) |
| 3016 |
{ |
| 3017 |
if (!this->m_Silent) |
| 3018 |
std::cout << vexlist[i] << ","; |
| 3019 |
} |
| 3020 |
if (!this->m_Silent) |
| 3021 |
std::cout << std::endl; |
| 3022 |
return fabs(this->m_CanonicalCorrelations[0]); |
| 3023 |
} |
| 3024 |
|
| 3025 |
template <typename TInputImage, typename TRealType> |
| 3026 |
TRealType |
| 3027 |
antsSCCANObject<TInputImage, TRealType>::ConjGrad(typename antsSCCANObject<TInputImage, TRealType>::MatrixType & A, |
| 3028 |
typename antsSCCANObject<TInputImage, TRealType>::VectorType & x_k, |
| 3029 |
typename antsSCCANObject<TInputImage, TRealType>::VectorType b_in, |
| 3030 |
TRealType convcrit, |
| 3031 |
unsigned int maxits) |
| 3032 |
{ |
| 3033 |
/** This will use the normal equations */ |
| 3034 |
/** Based on Golub CONJUGATE G R A D I E N T A N D LANCZOS HISTORY |
| 3035 |
* http://www.matematicas.unam.mx/gfgf/cg2010/HISTORY-conjugategradient.pdf |
| 3036 |
*/ |
| 3037 |
bool debug = false; |
| 3038 |
// minimize the following error : \| A^T*A * vec_i - b \| + sparseness_penalty |
| 3039 |
MatrixType At = A.transpose(); |
| 3040 |
VectorType b = b_in - b_in.mean(); |
| 3041 |
b = b * A; |
| 3042 |
VectorType r_k = At * (A * x_k); |
| 3043 |
r_k = b - r_k; |
| 3044 |
VectorType p_k = r_k; |
| 3045 |
double approxerr = 1.e9; |
| 3046 |
unsigned int ct = 0; |
| 3047 |
VectorType bestsol = x_k; |
| 3048 |
RealType starterr = b.two_norm(); |
| 3049 |
RealType minerr = starterr, deltaminerr = 1, lasterr = starterr * 2; |
| 3050 |
while (deltaminerr > 0 && approxerr > convcrit && ct < maxits) |
| 3051 |
{ |
| 3052 |
RealType alpha_denom = inner_product(p_k, At * (A * p_k)); |
| 3053 |
RealType iprk = inner_product(r_k, r_k); |
| 3054 |
if (debug) |
| 3055 |
{ |
| 3056 |
if (!this->m_Silent) |
| 3057 |
std::cout << " iprk " << iprk << std::endl; |
| 3058 |
} |
| 3059 |
if (alpha_denom < 1.e-12) |
| 3060 |
{ |
| 3061 |
alpha_denom = 1; |
| 3062 |
} |
| 3063 |
RealType alpha_k = iprk / alpha_denom; |
| 3064 |
if (debug) |
| 3065 |
{ |
| 3066 |
if (!this->m_Silent) |
| 3067 |
std::cout << " alpha_k " << alpha_k << std::endl; |
| 3068 |
} |
| 3069 |
VectorType x_k1 = x_k + alpha_k * p_k; // this adds the scaled residual to the current solution |
| 3070 |
// this->SparsifyOther( x_k1 ); // this can be used to approximate NMF |
| 3071 |
VectorType r_k1 = b - At * (A * x_k1); |
| 3072 |
approxerr = r_k1.two_norm(); |
| 3073 |
if (approxerr < minerr) |
| 3074 |
{ |
| 3075 |
minerr = approxerr; |
| 3076 |
bestsol = (x_k1); |
| 3077 |
} |
| 3078 |
deltaminerr = (lasterr - approxerr); |
| 3079 |
if (debug) |
| 3080 |
{ |
| 3081 |
if (!this->m_Silent) |
| 3082 |
std::cout << " ConjGrad " << approxerr << " minerr " << minerr << std::endl; |
| 3083 |
} |
| 3084 |
lasterr = approxerr; |
| 3085 |
// measures the change in the residual --- this is the Fletcher-Reeves form for nonlinear CG |
| 3086 |
// see Table 1.1 \Beta^FR in A SURVEY OF NONLINEAR CONJUGATE GRADIENT METHODS |
| 3087 |
// in this paper's notation d => p, g => r , alpha, beta, x the same , so y = rk1 - rk |
| 3088 |
// measures the change in the residual |
| 3089 |
VectorType yk = r_k1 - r_k; |
| 3090 |
// RealType bknd = inner_product( p_k, yk ); |
| 3091 |
// RealType beta_k = inner_product( ( yk - p_k * 2 * yk.two_norm() / bknd ) , r_k1 / bknd ); // Hager and Zhang |
| 3092 |
RealType beta_k = inner_product(r_k1, r_k1) / inner_product(r_k, r_k); // classic cg |
| 3093 |
VectorType p_k1 = r_k1 + beta_k * p_k; |
| 3094 |
if (debug) |
| 3095 |
{ |
| 3096 |
if (!this->m_Silent) |
| 3097 |
std::cout << " p_k1 " << p_k1.two_norm() << std::endl; |
| 3098 |
} |
| 3099 |
r_k = r_k1; |
| 3100 |
p_k = p_k1; |
| 3101 |
x_k = x_k1; |
| 3102 |
ct++; |
| 3103 |
} |
| 3104 |
x_k = bestsol; |
| 3105 |
// this->SparsifyOther( x_k ); |
| 3106 |
this->m_Intercept = this->ComputeIntercept(A, x_k, b_in); |
| 3107 |
VectorType soln = A * x_k + this->m_Intercept; |
| 3108 |
return (soln - b_in).one_norm() / b_in.size(); |
| 3109 |
} |
| 3110 |
|
| 3111 |
template <typename TInputImage, typename TRealType> |
| 3112 |
TRealType |
| 3113 |
antsSCCANObject<TInputImage, TRealType>::EvaluateEnergy( |
| 3114 |
typename antsSCCANObject<TInputImage, TRealType>::MatrixType & A, |
| 3115 |
typename antsSCCANObject<TInputImage, TRealType>::VectorType & x_k, |
| 3116 |
typename antsSCCANObject<TInputImage, TRealType>::VectorType & p_k, |
| 3117 |
typename antsSCCANObject<TInputImage, TRealType>::VectorType & b, |
| 3118 |
TRealType minalph, |
| 3119 |
TRealType lambda) |
| 3120 |
{ |
| 3121 |
VectorType x_k1 = x_k + minalph * p_k; |
| 3122 |
|
| 3123 |
this->SparsifyP(x_k1); |
| 3124 |
VectorType r_k1 = (b - A.transpose() * (A * x_k1)); |
| 3125 |
RealType e = r_k1.two_norm() + x_k1.two_norm() * lambda; |
| 3126 |
return e; |
| 3127 |
} |
| 3128 |
|
| 3129 |
template <typename TInputImage, typename TRealType> |
| 3130 |
TRealType |
| 3131 |
antsSCCANObject<TInputImage, TRealType>::GoldenSection( |
| 3132 |
typename antsSCCANObject<TInputImage, TRealType>::MatrixType & A, |
| 3133 |
typename antsSCCANObject<TInputImage, TRealType>::VectorType & x_k, |
| 3134 |
typename antsSCCANObject<TInputImage, TRealType>::VectorType & p_k, |
| 3135 |
typename antsSCCANObject<TInputImage, TRealType>::VectorType & bsol, |
| 3136 |
TRealType a, |
| 3137 |
TRealType b, |
| 3138 |
TRealType c, |
| 3139 |
TRealType tau, |
| 3140 |
TRealType lambda) |
| 3141 |
{ |
| 3142 |
if (this->m_GoldenSectionCounter == 0) |
| 3143 |
{ |
| 3144 |
this->m_GSBestSol = a; |
| 3145 |
} |
| 3146 |
this->m_GoldenSectionCounter++; |
| 3147 |
if (this->m_GoldenSectionCounter > 20) |
| 3148 |
{ |
| 3149 |
return this->m_GSBestSol; |
| 3150 |
} |
| 3151 |
double phi = (1 + sqrt(5.0)) / 2; |
| 3152 |
double resphi = 2 - phi; |
| 3153 |
double x; |
| 3154 |
if (c - b > b - a) |
| 3155 |
{ |
| 3156 |
x = b + resphi * (c - b); |
| 3157 |
} |
| 3158 |
else |
| 3159 |
{ |
| 3160 |
x = b - resphi * (b - a); |
| 3161 |
} |
| 3162 |
if (fabs(c - a) < tau * (fabs(b) + fabs(x))) |
| 3163 |
{ |
| 3164 |
return (c + a) / 2; |
| 3165 |
} |
| 3166 |
if (this->EvaluateEnergy(A, x_k, p_k, bsol, x, lambda) < this->EvaluateEnergy(A, x_k, p_k, bsol, b, lambda)) |
| 3167 |
{ |
| 3168 |
this->m_GSBestSol = x; |
| 3169 |
if (c - b > b - a) |
| 3170 |
{ |
| 3171 |
return GoldenSection(A, x_k, p_k, bsol, b, x, c, tau, lambda); |
| 3172 |
} |
| 3173 |
else |
| 3174 |
{ |
| 3175 |
this->m_GSBestSol = b; |
| 3176 |
return GoldenSection(A, x_k, p_k, bsol, a, x, b, tau, lambda); |
| 3177 |
} |
| 3178 |
} |
| 3179 |
else |
| 3180 |
{ |
| 3181 |
if (c - b > b - a) |
| 3182 |
{ |
| 3183 |
return GoldenSection(A, x_k, p_k, bsol, a, b, x, tau, lambda); |
| 3184 |
} |
| 3185 |
else |
| 3186 |
{ |
| 3187 |
return GoldenSection(A, x_k, p_k, bsol, x, b, c, tau, lambda); |
| 3188 |
} |
| 3189 |
} |
| 3190 |
} |
| 3191 |
|
| 3192 |
template <typename TInputImage, typename TRealType> |
| 3193 |
TRealType |
| 3194 |
antsSCCANObject<TInputImage, TRealType>::LineSearch(typename antsSCCANObject<TInputImage, TRealType>::MatrixType & A, |
| 3195 |
typename antsSCCANObject<TInputImage, TRealType>::VectorType & x_k, |
| 3196 |
typename antsSCCANObject<TInputImage, TRealType>::VectorType & p_k, |
| 3197 |
typename antsSCCANObject<TInputImage, TRealType>::VectorType & b, |
| 3198 |
TRealType minalph, |
| 3199 |
TRealType maxalph, |
| 3200 |
TRealType lambda) |
| 3201 |
{ |
| 3202 |
bool dogs = true; |
| 3203 |
|
| 3204 |
if (dogs) |
| 3205 |
{ |
| 3206 |
RealType big = maxalph; |
| 3207 |
this->m_GoldenSectionCounter = 0; |
| 3208 |
RealType tau = 0.00001; // sets accuracy , lower => more accurate |
| 3209 |
return this->GoldenSection(A, x_k, p_k, b, 0, big / 2, big, tau, lambda); |
| 3210 |
} |
| 3211 |
|
| 3212 |
// otherwise just search across a line ... |
| 3213 |
RealType minerr = 1.e99; |
| 3214 |
RealType bestalph = 0; |
| 3215 |
RealType step; |
| 3216 |
step = (maxalph - minalph) / 10; |
| 3217 |
for (RealType eb = minalph; eb <= maxalph; eb = eb + step) |
| 3218 |
{ |
| 3219 |
RealType e = this->EvaluateEnergy(A, x_k, p_k, b, eb, lambda); |
| 3220 |
if (e < minerr) |
| 3221 |
{ |
| 3222 |
minerr = e; |
| 3223 |
bestalph = eb; |
| 3224 |
} |
| 3225 |
} |
| 3226 |
return bestalph; |
| 3227 |
} |
| 3228 |
|
| 3229 |
template <typename TInputImage, typename TRealType> |
| 3230 |
TRealType |
| 3231 |
antsSCCANObject<TInputImage, TRealType>::SparseNLConjGrad( |
| 3232 |
typename antsSCCANObject<TInputImage, TRealType>::MatrixType & A, |
| 3233 |
typename antsSCCANObject<TInputImage, TRealType>::VectorType & x_k, |
| 3234 |
typename antsSCCANObject<TInputImage, TRealType>::VectorType b, |
| 3235 |
TRealType convcrit, |
| 3236 |
unsigned int maxits, |
| 3237 |
bool makeprojsparse, |
| 3238 |
unsigned int loorth, |
| 3239 |
unsigned int hiorth) |
| 3240 |
{ |
| 3241 |
bool negate = false; |
| 3242 |
|
| 3243 |
if (b.max_value() <= 0) |
| 3244 |
{ |
| 3245 |
negate = true; |
| 3246 |
} |
| 3247 |
if (negate) |
| 3248 |
{ |
| 3249 |
b = b * (-1); |
| 3250 |
} |
| 3251 |
bool debug = false; |
| 3252 |
RealType intercept = 0; |
| 3253 |
|
| 3254 |
VectorType r_k = (b - A.transpose() * (A * x_k)); |
| 3255 |
VectorType p_k = r_k; |
| 3256 |
double approxerr = 1.e22; |
| 3257 |
unsigned int ct = 0; |
| 3258 |
VectorType bestsol = x_k; |
| 3259 |
RealType starterr = r_k.two_norm(); |
| 3260 |
RealType minerr = starterr, deltaminerr = 1; |
| 3261 |
while (deltaminerr > 1.e-4 && minerr > convcrit && ct < maxits) |
| 3262 |
{ |
| 3263 |
RealType alpha_denom = inner_product(p_k, A.transpose() * (A * p_k)); |
| 3264 |
RealType iprk = inner_product(r_k, r_k); |
| 3265 |
RealType alpha_k = iprk / alpha_denom; |
| 3266 |
if (debug) |
| 3267 |
{ |
| 3268 |
if (!this->m_Silent) |
| 3269 |
std::cout << " alpha_k " << alpha_k << " iprk " << iprk << " alpha_denom " << alpha_denom << std::endl; |
| 3270 |
} |
| 3271 |
// HACK NOT USED RealType stepsize = alpha_k / 100; |
| 3272 |
// HACK NOT USED RealType minalph = stepsize; |
| 3273 |
// HACK NOT USED RealType maxalph = alpha_k * 2; |
| 3274 |
RealType best_alph = alpha_k; |
| 3275 |
// RealType best_alph = this->LineSearch( A, x_k, p_k, b, minalph, maxalph, keeppos ); |
| 3276 |
VectorType x_k1 = x_k + best_alph * p_k; |
| 3277 |
// this adds the scaled residual to the current solution |
| 3278 |
if (debug) |
| 3279 |
{ |
| 3280 |
if (!this->m_Silent) |
| 3281 |
std::cout << " xk12n " << x_k1.two_norm() << " alpha_k " << alpha_k << " pk2n " << p_k.two_norm() << " xk1-min " |
| 3282 |
<< x_k1.min_value() << std::endl; |
| 3283 |
} |
| 3284 |
if (hiorth > loorth) |
| 3285 |
{ |
| 3286 |
for (unsigned int wv = loorth; wv < hiorth; wv++) |
| 3287 |
{ |
| 3288 |
x_k1 = this->Orthogonalize(x_k1, this->m_VariatesP.get_column(wv)); |
| 3289 |
} |
| 3290 |
} |
| 3291 |
|
| 3292 |
this->SparsifyP(x_k1); |
| 3293 |
if (debug) |
| 3294 |
{ |
| 3295 |
if (!this->m_Silent) |
| 3296 |
std::cout << " xk12n " << x_k1.two_norm() << " alpha_k " << alpha_k << " pk2n " << p_k.two_norm() << std::endl; |
| 3297 |
} |
| 3298 |
VectorType proj = A.transpose() * (A * x_k1); |
| 3299 |
VectorType r_k1 = (b - proj); |
| 3300 |
if (makeprojsparse) |
| 3301 |
{ |
| 3302 |
this->SparsifyP(r_k1); |
| 3303 |
} |
| 3304 |
approxerr = r_k1.two_norm(); |
| 3305 |
RealType newminerr = minerr; |
| 3306 |
if (approxerr < newminerr || ct == 0) |
| 3307 |
{ |
| 3308 |
newminerr = approxerr; |
| 3309 |
bestsol = (x_k1); |
| 3310 |
this->m_Intercept = intercept; |
| 3311 |
} |
| 3312 |
deltaminerr = (minerr - newminerr); |
| 3313 |
if (newminerr < minerr) |
| 3314 |
{ |
| 3315 |
minerr = newminerr; |
| 3316 |
} |
| 3317 |
if (debug) |
| 3318 |
{ |
| 3319 |
if (!this->m_Silent) |
| 3320 |
std::cout << " r_k12n " << r_k1.two_norm() << " r_k2n " << r_k.two_norm() << std::endl; |
| 3321 |
} |
| 3322 |
if (debug) |
| 3323 |
{ |
| 3324 |
if (!this->m_Silent) |
| 3325 |
std::cout << " x_k2n " << x_k.two_norm() << " x_k12n " << x_k1.two_norm() << std::endl; |
| 3326 |
} |
| 3327 |
if (debug) |
| 3328 |
{ |
| 3329 |
if (!this->m_Silent) |
| 3330 |
std::cout << " r_k1 " << minerr << " derr " << deltaminerr << " ba " << best_alph / alpha_k << std::endl; |
| 3331 |
} |
| 3332 |
// measures the change in the residual --- this is the Fletcher-Reeves form for nonlinear CG |
| 3333 |
// see Table 1.1 \Beta^FR in A SURVEY OF NONLINEAR CONJUGATE GRADIENT METHODS |
| 3334 |
// in this paper's notation d => p, g => r , alpha, beta, x the same , so y = rk1 - rk |
| 3335 |
// RealType beta_k = inner_product( r_k1 , r_k1 ) / inner_product( r_k , r_k ); // classic cg |
| 3336 |
VectorType yk = r_k1 - r_k; |
| 3337 |
RealType bknd = inner_product(p_k, yk); |
| 3338 |
RealType beta_k = inner_product((yk - p_k * 2 * yk.two_norm() / bknd), r_k1 / bknd); // Hager and Zhang |
| 3339 |
// RealType beta_k = inner_product( r_k1 , r_k1 ) / inner_product( r_k , r_k ); // classic cg |
| 3340 |
VectorType p_k1 = r_k1 + beta_k * p_k; |
| 3341 |
// if ( makeprojsparse ) this->SparsifyP( p_k1, keeppos ); |
| 3342 |
r_k = r_k1; |
| 3343 |
p_k = p_k1; |
| 3344 |
x_k = x_k1; |
| 3345 |
ct++; |
| 3346 |
} |
| 3347 |
|
| 3348 |
x_k = bestsol; |
| 3349 |
if (negate) |
| 3350 |
{ |
| 3351 |
x_k = x_k * (-1); |
| 3352 |
b = b * (-1); |
| 3353 |
} |
| 3354 |
return minerr / A.rows(); |
| 3355 |
} |
| 3356 |
|
| 3357 |
template <typename TInputImage, typename TRealType> |
| 3358 |
TRealType |
| 3359 |
antsSCCANObject<TInputImage, TRealType>::SparseNLPreConjGrad( |
| 3360 |
typename antsSCCANObject<TInputImage, TRealType>::MatrixType & A, |
| 3361 |
typename antsSCCANObject<TInputImage, TRealType>::VectorType & x_k, |
| 3362 |
typename antsSCCANObject<TInputImage, TRealType>::VectorType b, |
| 3363 |
TRealType convcrit, |
| 3364 |
unsigned int maxits) |
| 3365 |
{ |
| 3366 |
if (this->m_PreC.size() <= 0) |
| 3367 |
{ |
| 3368 |
vnl_diag_matrix<TRealType> prec(A.cols(), 0); |
| 3369 |
this->m_PreC = prec; |
| 3370 |
for (unsigned int i = 0; i < A.cols(); i++) |
| 3371 |
{ |
| 3372 |
this->m_PreC(i, i) = inner_product(A.get_column(i), A.get_column(i)); |
| 3373 |
if (this->m_PreC(i, i) > 1.e-8) |
| 3374 |
{ |
| 3375 |
this->m_PreC(i, i) = 1 / this->m_PreC(i, i); |
| 3376 |
} |
| 3377 |
else |
| 3378 |
{ |
| 3379 |
this->m_PreC(i, i) = 0; |
| 3380 |
} |
| 3381 |
} |
| 3382 |
} |
| 3383 |
|
| 3384 |
RealType intercept = 0; |
| 3385 |
VectorType r_k = (b - A.transpose() * (A * x_k)); |
| 3386 |
VectorType z_k = this->m_PreC * r_k; |
| 3387 |
VectorType p_k = z_k; |
| 3388 |
double approxerr = 1.e22; |
| 3389 |
unsigned int ct = 0; |
| 3390 |
VectorType bestsol = x_k; |
| 3391 |
RealType starterr = 1.e99; |
| 3392 |
RealType minerr = starterr, deltaminerr = 1; |
| 3393 |
while ((ct < 5) || ((deltaminerr > 1.e-10) && (minerr > convcrit) && (ct < maxits))) |
| 3394 |
{ |
| 3395 |
// RealType spgoal = 100.0 * ( 1 - itk::Math::abs ( this->m_FractionNonZeroP ) ); |
| 3396 |
RealType alpha_denom = inner_product(p_k, A.transpose() * (A * p_k)); |
| 3397 |
RealType iprk = inner_product(r_k, z_k); |
| 3398 |
RealType alpha_k = iprk / alpha_denom; |
| 3399 |
RealType best_alph = alpha_k; |
| 3400 |
// RealType stepsize = alpha_k / 10; |
| 3401 |
// RealType minalph = stepsize; |
| 3402 |
// RealType maxalph = alpha_k * 4; |
| 3403 |
// best_alph = this->LineSearch( A, x_k, p_k, b, minalph, maxalph, false ); |
| 3404 |
// this->CurvatureSparseness( p_k , spgoal * 0.9 , A , b , 50 ); |
| 3405 |
VectorType x_k1 = x_k + best_alph * p_k; |
| 3406 |
this->SparsifyP(x_k1); |
| 3407 |
// VectorType x_k2( x_k1 ); |
| 3408 |
// this->CurvatureSparseness( x_k1 , spgoal , 500 ); |
| 3409 |
// x_k1 = x_k2; |
| 3410 |
VectorType proj = A.transpose() * (A * x_k1); |
| 3411 |
VectorType r_k1 = (b - proj) - x_k1 * 100; |
| 3412 |
// this->CurvatureSparseness( r_k1 , spgoal , A , b , 5 ); |
| 3413 |
approxerr = r_k1.two_norm(); |
| 3414 |
VectorType z_k1 = this->m_PreC * r_k1; |
| 3415 |
RealType newminerr = minerr; |
| 3416 |
if (approxerr < newminerr || ct == 0) |
| 3417 |
{ |
| 3418 |
newminerr = approxerr; |
| 3419 |
bestsol = (x_k1); |
| 3420 |
this->m_Intercept = intercept; |
| 3421 |
} |
| 3422 |
deltaminerr = (minerr - newminerr); |
| 3423 |
if (newminerr < minerr) |
| 3424 |
{ |
| 3425 |
minerr = newminerr; |
| 3426 |
} |
| 3427 |
VectorType yk = r_k1 - r_k; |
| 3428 |
RealType temp = inner_product(z_k, r_k); |
| 3429 |
if (itk::Math::abs(temp) < 1.e-9) |
| 3430 |
{ |
| 3431 |
temp = 1; |
| 3432 |
} |
| 3433 |
RealType beta_k = inner_product(z_k1, r_k1) / temp; |
| 3434 |
VectorType p_k1 = z_k1 + beta_k * p_k; |
| 3435 |
r_k = r_k1; |
| 3436 |
p_k = p_k1; |
| 3437 |
x_k = x_k1; |
| 3438 |
z_k = z_k1; // preconditioning |
| 3439 |
ct++; |
| 3440 |
if (!this->m_Silent) |
| 3441 |
std::cout << " approxerr " << approxerr << " ct " << ct << " nz " << this->CountNonZero(x_k) << " deltaminerr " |
| 3442 |
<< deltaminerr << std::endl; |
| 3443 |
} |
| 3444 |
|
| 3445 |
x_k = bestsol; |
| 3446 |
RealType finalerr = minerr / A.rows(); |
| 3447 |
// if ( ! this->m_Silent ) std::cout << " NZ1 " << this->CountNonZero( x_k ) << " err " << finalerr << std::endl; |
| 3448 |
return finalerr; |
| 3449 |
} |
| 3450 |
|
| 3451 |
template <typename TInputImage, typename TRealType> |
| 3452 |
TRealType |
| 3453 |
antsSCCANObject<TInputImage, TRealType>::PowerIteration( |
| 3454 |
typename antsSCCANObject<TInputImage, TRealType>::MatrixType & A, |
| 3455 |
typename antsSCCANObject<TInputImage, TRealType>::VectorType & evec, |
| 3456 |
unsigned int maxits, |
| 3457 |
bool makesparse) |
| 3458 |
{ |
| 3459 |
unsigned int n_vecs = this->m_VariatesP.cols(); |
| 3460 |
|
| 3461 |
for (unsigned int powerits = 0; powerits < maxits; powerits++) |
| 3462 |
{ |
| 3463 |
if (evec.two_norm() > 0) |
| 3464 |
{ |
| 3465 |
evec = evec / evec.two_norm(); |
| 3466 |
} |
| 3467 |
evec = (A.transpose()) * (A * evec); // power iteration |
| 3468 |
for (unsigned int orth = 0; orth < n_vecs; orth++) |
| 3469 |
{ |
| 3470 |
evec = this->Orthogonalize(evec, this->m_VariatesP.get_column(orth)); |
| 3471 |
} |
| 3472 |
} |
| 3473 |
// (void) this->CurvatureSparseness( evec , 95 , 10 ); |
| 3474 |
if (makesparse) |
| 3475 |
{ |
| 3476 |
this->SparsifyP(evec); |
| 3477 |
} |
| 3478 |
if (evec.two_norm() > 0) |
| 3479 |
{ |
| 3480 |
evec = evec / evec.two_norm(); |
| 3481 |
} |
| 3482 |
return inner_product(A * evec, A * evec); |
| 3483 |
} |
| 3484 |
|
| 3485 |
template <typename TInputImage, typename TRealType> |
| 3486 |
TRealType |
| 3487 |
antsSCCANObject<TInputImage, TRealType>::IHTPowerIteration( |
| 3488 |
typename antsSCCANObject<TInputImage, TRealType>::MatrixType & A, |
| 3489 |
typename antsSCCANObject<TInputImage, TRealType>::VectorType & evecin, |
| 3490 |
// typename antsSCCANObject<TInputImage, TRealType>::VectorType& y, |
| 3491 |
unsigned int maxits, |
| 3492 |
unsigned int maxorth) |
| 3493 |
{ |
| 3494 |
/** This computes a hard-thresholded gradient descent on the eigenvector criterion. |
| 3495 |
max x over x^t A^t A x s.t. x^t x = 1 |
| 3496 |
success of the optimization is measured by rayleigh quotient. derivative is: |
| 3497 |
d.dx ( x^t A^t A x ) = A^t A x , x \leftarrow x / \| x \| |
| 3498 |
we use a conjugate gradient version of this optimization. |
| 3499 |
*/ |
| 3500 |
A = A - A.min_value(); |
| 3501 |
VectorType evec = evecin; |
| 3502 |
unsigned int maxcoltoorth = (unsigned int)(1.0 / itk::Math::abs(this->m_FractionNonZeroP)) - 1; |
| 3503 |
if (itk::Math::FloatAlmostEqual(evecin.two_norm(), itk::NumericTraits<typename VectorType::abs_t>::ZeroValue())) |
| 3504 |
{ |
| 3505 |
evec = this->InitializeV(this->m_MatrixP, false); |
| 3506 |
} |
| 3507 |
VectorType proj = (A * evecin); |
| 3508 |
VectorType lastgrad = evecin; |
| 3509 |
RealType rayquo = 0; |
| 3510 |
RealType denom = inner_product(evecin, evecin); |
| 3511 |
if (denom > 0) |
| 3512 |
{ |
| 3513 |
rayquo = inner_product(proj, proj) / denom; |
| 3514 |
} |
| 3515 |
else if (!this->m_Silent) |
| 3516 |
std::cout << "Denom < 0: probable trouble." << std::endl; |
| 3517 |
RealType bestrayquo = 0; |
| 3518 |
MatrixType At = A.transpose(); |
| 3519 |
unsigned int powerits = 0; |
| 3520 |
bool conjgrad = true; |
| 3521 |
VectorType bestevec = evecin; |
| 3522 |
RealType relfac = 1.0; |
| 3523 |
VectorType di; |
| 3524 |
while (((rayquo > bestrayquo) && (powerits < maxits)) || powerits < 2) |
| 3525 |
// while ( ( relfac > 1.e-4 ) && ( powerits < maxits ) ) |
| 3526 |
{ |
| 3527 |
VectorType nvec = At * proj; |
| 3528 |
RealType gamma = 0.1; |
| 3529 |
// nvec = this->SpatiallySmoothVector( nvec, this->m_MaskImageP ); |
| 3530 |
if (powerits == 0) |
| 3531 |
di = nvec; |
| 3532 |
if ((lastgrad.two_norm() > 0) && (conjgrad)) |
| 3533 |
{ |
| 3534 |
gamma = inner_product(nvec, nvec) / inner_product(lastgrad, lastgrad); |
| 3535 |
} |
| 3536 |
lastgrad = nvec; |
| 3537 |
VectorType diplus1 = nvec + di * gamma; |
| 3538 |
evec = evec + diplus1 * relfac; |
| 3539 |
unsigned int startingm = 0; |
| 3540 |
if (maxorth > maxcoltoorth) |
| 3541 |
startingm = maxorth - maxcoltoorth; |
| 3542 |
for (unsigned int orth = startingm; orth < maxorth; orth++) |
| 3543 |
{ |
| 3544 |
VectorType zv = this->m_VariatesP.get_column(orth); |
| 3545 |
evec = this->Orthogonalize(evec, zv); |
| 3546 |
if (this->m_Covering == 1) |
| 3547 |
this->ZeroProduct(evec, zv); |
| 3548 |
/** alternative --- orthogonalize in n-space |
| 3549 |
VectorType v = A * this->m_VariatesP.get_column( orth ); |
| 3550 |
RealType ip1 = inner_product( A * evec, v ); |
| 3551 |
RealType ip2 = inner_product( v, v ); |
| 3552 |
// evec = evec - this->m_VariatesP.get_column( orth ) * ip1 / ip2;*/ |
| 3553 |
} |
| 3554 |
this->SparsifyP(evec); |
| 3555 |
if (evec.two_norm() > 0) |
| 3556 |
{ |
| 3557 |
evec = evec / evec.two_norm(); |
| 3558 |
} |
| 3559 |
proj = (A * evec); |
| 3560 |
denom = inner_product(evec, evec); |
| 3561 |
if (denom > 0) |
| 3562 |
{ |
| 3563 |
rayquo = inner_product(proj, proj) / denom; // - gradvec.two_norm() / gradvec.size() * 1.e2 ; |
| 3564 |
} |
| 3565 |
if (rayquo > bestrayquo) |
| 3566 |
{ |
| 3567 |
bestevec = evec; |
| 3568 |
bestrayquo = rayquo; |
| 3569 |
} |
| 3570 |
else |
| 3571 |
{ |
| 3572 |
relfac *= 0.9; |
| 3573 |
evec = bestevec; |
| 3574 |
} |
| 3575 |
powerits++; |
| 3576 |
} |
| 3577 |
if (!this->m_Silent) |
| 3578 |
std::cout << "rayleigh-quotient: " << bestrayquo << " in " << powerits << " num " << maxorth << " fnz " |
| 3579 |
<< this->CountNonZero(evec) << std::endl; |
| 3580 |
evecin = bestevec; |
| 3581 |
return bestrayquo; |
| 3582 |
} |
| 3583 |
|
| 3584 |
|
| 3585 |
template <typename TInputImage, typename TRealType> |
| 3586 |
TRealType |
| 3587 |
antsSCCANObject<TInputImage, TRealType>::IHTPowerIterationU( |
| 3588 |
typename antsSCCANObject<TInputImage, TRealType>::MatrixType & A, |
| 3589 |
typename antsSCCANObject<TInputImage, TRealType>::VectorType & evecin, |
| 3590 |
unsigned int maxits, |
| 3591 |
unsigned int maxorth) |
| 3592 |
{ |
| 3593 |
unsigned int maxcoltoorth = (unsigned int)(1.0 / itk::Math::abs(this->m_FractionNonZeroP)) - 1; |
| 3594 |
VectorType proj = evecin; |
| 3595 |
RealType rayquo = 0; |
| 3596 |
RealType bestrayquo = 0; |
| 3597 |
MatrixType At = A.transpose(); |
| 3598 |
VectorType lastgrad = At * proj; |
| 3599 |
VectorType evec = At * proj; |
| 3600 |
RealType denom = inner_product(evec, evec); |
| 3601 |
if (denom > 0) |
| 3602 |
{ |
| 3603 |
rayquo = inner_product(proj, proj) / denom; |
| 3604 |
} |
| 3605 |
else if (!this->m_Silent) |
| 3606 |
std::cout << "Denom < 0: probable trouble." << std::endl; |
| 3607 |
unsigned int powerits = 0; |
| 3608 |
bool conjgrad = true; |
| 3609 |
VectorType bestevec = evecin; |
| 3610 |
RealType relfac = 1.0; |
| 3611 |
VectorType di; |
| 3612 |
while (((rayquo > bestrayquo) && (powerits < maxits)) || powerits < 2) |
| 3613 |
{ |
| 3614 |
VectorType nvec = At * proj; |
| 3615 |
RealType gamma = 0.1; |
| 3616 |
if (powerits == 0) |
| 3617 |
di = nvec; |
| 3618 |
if ((lastgrad.two_norm() > 0) && (conjgrad)) |
| 3619 |
{ |
| 3620 |
gamma = inner_product(nvec, nvec) / inner_product(lastgrad, lastgrad); |
| 3621 |
} |
| 3622 |
lastgrad = nvec; |
| 3623 |
VectorType diplus1 = nvec + di * gamma; |
| 3624 |
evec = evec + diplus1 * relfac; |
| 3625 |
unsigned int startingm = 0; |
| 3626 |
if (maxorth > maxcoltoorth) |
| 3627 |
startingm = maxorth - maxcoltoorth; |
| 3628 |
for (unsigned int orth = startingm; orth < maxorth; orth++) |
| 3629 |
{ |
| 3630 |
VectorType zv = this->m_VariatesP.get_column(orth); |
| 3631 |
evec = this->Orthogonalize(evec, zv); |
| 3632 |
if (this->m_Covering == 1) |
| 3633 |
this->ZeroProduct(evec, zv); |
| 3634 |
} |
| 3635 |
this->SparsifyP(evec); |
| 3636 |
if (evec.two_norm() > 0) |
| 3637 |
{ |
| 3638 |
evec = evec / evec.two_norm(); |
| 3639 |
} |
| 3640 |
proj = (A * evec); |
| 3641 |
denom = inner_product(evec, evec); |
| 3642 |
if (denom > 0) |
| 3643 |
{ |
| 3644 |
rayquo = inner_product(proj, proj) / denom; // - gradvec.two_norm() / gradvec.size() * 1.e2 ; |
| 3645 |
} |
| 3646 |
powerits++; |
| 3647 |
if (rayquo > bestrayquo) |
| 3648 |
{ |
| 3649 |
bestevec = evec; |
| 3650 |
bestrayquo = rayquo; |
| 3651 |
} |
| 3652 |
else |
| 3653 |
{ |
| 3654 |
relfac *= 0.9; |
| 3655 |
evec = bestevec; |
| 3656 |
} |
| 3657 |
} |
| 3658 |
evecin = bestevec; |
| 3659 |
if (!this->m_Silent) |
| 3660 |
std::cout << "rayleigh-quotient: " << bestrayquo << " in " << powerits << " num " << maxorth << " fnz " |
| 3661 |
<< this->m_FractionNonZeroP << std::endl; |
| 3662 |
return bestrayquo; |
| 3663 |
} |
| 3664 |
|
| 3665 |
|
| 3666 |
template <typename TInputImage, typename TRealType> |
| 3667 |
TRealType |
| 3668 |
antsSCCANObject<TInputImage, TRealType>::RidgeCCA(unsigned int n_vecs) |
| 3669 |
{ |
| 3670 |
RealType taup = itk::Math::abs(this->m_FractionNonZeroP); |
| 3671 |
RealType tauq = itk::Math::abs(this->m_FractionNonZeroQ); |
| 3672 |
|
| 3673 |
if (!this->m_Silent) |
| 3674 |
std::cout << " ridge cca : taup " << taup << " tauq " << tauq << std::endl; |
| 3675 |
|
| 3676 |
this->m_CanonicalCorrelations.set_size(n_vecs); |
| 3677 |
this->m_MatrixP = this->NormalizeMatrix(this->m_OriginalMatrixP, false); |
| 3678 |
this->m_MatrixQ = this->NormalizeMatrix(this->m_OriginalMatrixQ, false); |
| 3679 |
|
| 3680 |
this->m_VariatesP.set_size(this->m_MatrixP.cols(), n_vecs); |
| 3681 |
this->m_VariatesQ.set_size(this->m_MatrixQ.cols(), n_vecs); |
| 3682 |
if (!this->m_Silent) |
| 3683 |
std::cout << " begin " << std::endl; |
| 3684 |
unsigned int maxloop = this->m_MaximumNumberOfIterations; |
| 3685 |
if (maxloop < 25) |
| 3686 |
{ |
| 3687 |
maxloop = 25; |
| 3688 |
} |
| 3689 |
RealType totalcorr = 0; |
| 3690 |
RealType bestcorr = 0; |
| 3691 |
unsigned int bestseed = 1; |
| 3692 |
for (unsigned int seeder = 1; seeder < 100; seeder++) |
| 3693 |
{ |
| 3694 |
totalcorr = this->InitializeSCCA(n_vecs, seeder); |
| 3695 |
if (totalcorr > bestcorr) |
| 3696 |
{ |
| 3697 |
bestseed = seeder; |
| 3698 |
bestcorr = totalcorr; |
| 3699 |
} |
| 3700 |
totalcorr = 0; |
| 3701 |
} |
| 3702 |
if (!this->m_Silent) |
| 3703 |
std::cout << " Best initial corr " << bestcorr << std::endl; |
| 3704 |
this->InitializeSCCA(n_vecs, bestseed); |
| 3705 |
unsigned int loop = 0; |
| 3706 |
bool energyincreases = true; |
| 3707 |
RealType energy = 0; |
| 3708 |
RealType lastenergy = 0; |
| 3709 |
while (((loop < maxloop) && (energyincreases)) || loop < 3) |
| 3710 |
{ |
| 3711 |
for (unsigned int k = 0; k < n_vecs; k++) |
| 3712 |
{ |
| 3713 |
VectorType ptemp = this->m_VariatesP.get_column(k); |
| 3714 |
VectorType qtemp = this->m_VariatesQ.get_column(k); |
| 3715 |
VectorType pveck = this->m_MatrixQ * qtemp; |
| 3716 |
VectorType qveck = this->m_MatrixP * ptemp; |
| 3717 |
pveck = pveck * this->m_MatrixP; |
| 3718 |
qveck = qveck * this->m_MatrixQ; |
| 3719 |
for (unsigned int j = 0; j < k; j++) |
| 3720 |
{ |
| 3721 |
VectorType qj = this->m_VariatesP.get_column(j); // this->ZeroProduct( pveck, qj ); |
| 3722 |
pveck = this->Orthogonalize(pveck, qj); |
| 3723 |
qj = this->m_VariatesQ.get_column(j); // this->ZeroProduct( qveck, qj ); |
| 3724 |
qveck = this->Orthogonalize(qveck, qj); |
| 3725 |
} |
| 3726 |
pveck = pveck / pveck.two_norm(); |
| 3727 |
qveck = qveck / qveck.two_norm(); |
| 3728 |
pveck = ptemp + pveck * 0.01; |
| 3729 |
qveck = qtemp + qveck * 0.01; |
| 3730 |
this->m_VariatesP.set_column(k, pveck); |
| 3731 |
this->m_VariatesQ.set_column(k, qveck); |
| 3732 |
this->NormalizeWeightsByCovariance(k, taup, tauq); |
| 3733 |
VectorType proj1 = this->m_MatrixP * this->m_VariatesP.get_column(k); |
| 3734 |
VectorType proj2 = this->m_MatrixQ * this->m_VariatesQ.get_column(k); |
| 3735 |
this->m_CanonicalCorrelations[k] = this->PearsonCorr(proj1, proj2); |
| 3736 |
} |
| 3737 |
this->SortResults(n_vecs); |
| 3738 |
lastenergy = energy; |
| 3739 |
energy = this->m_CanonicalCorrelations.one_norm() / n_vecs; |
| 3740 |
if (!m_Silent) |
| 3741 |
{ |
| 3742 |
if (!this->m_Silent) |
| 3743 |
std::cout << " Loop " << loop << " Corrs : " << this->m_CanonicalCorrelations << " CorrMean : " << energy |
| 3744 |
<< std::endl; |
| 3745 |
} |
| 3746 |
if (energy < lastenergy) |
| 3747 |
{ |
| 3748 |
energyincreases = false; |
| 3749 |
} |
| 3750 |
else |
| 3751 |
{ |
| 3752 |
energyincreases = true; |
| 3753 |
} |
| 3754 |
loop++; |
| 3755 |
} // outer loop |
| 3756 |
|
| 3757 |
double ccasum = 0; |
| 3758 |
for (unsigned int i = 0; i < this->m_CanonicalCorrelations.size(); i++) |
| 3759 |
{ |
| 3760 |
ccasum += fabs(this->m_CanonicalCorrelations[i]); |
| 3761 |
} |
| 3762 |
if (n_vecs > 1) |
| 3763 |
{ |
| 3764 |
return ccasum; |
| 3765 |
} |
| 3766 |
else |
| 3767 |
{ |
| 3768 |
return fabs(this->m_CanonicalCorrelations[0]); |
| 3769 |
} |
| 3770 |
return this->m_CanonicalCorrelations[0]; |
| 3771 |
} |
| 3772 |
|
| 3773 |
template <typename TInputImage, typename TRealType> |
| 3774 |
TRealType |
| 3775 |
antsSCCANObject<TInputImage, TRealType>::RidgeRegression( |
| 3776 |
typename antsSCCANObject<TInputImage, TRealType>::MatrixType & A, |
| 3777 |
typename antsSCCANObject<TInputImage, TRealType>::VectorType & x_k, |
| 3778 |
typename antsSCCANObject<TInputImage, TRealType>::VectorType b, |
| 3779 |
TRealType lambda, |
| 3780 |
unsigned int maxits, |
| 3781 |
bool makesparse) |
| 3782 |
{ |
| 3783 |
MatrixType At = A.transpose(); |
| 3784 |
VectorType r_k = At * (A * x_k); |
| 3785 |
|
| 3786 |
r_k = b - r_k - x_k * lambda; |
| 3787 |
VectorType p_k = r_k; |
| 3788 |
double approxerr = 1.e9; |
| 3789 |
unsigned int ct = 0; |
| 3790 |
VectorType bestsol = x_k; |
| 3791 |
RealType starterr = 1.e99; |
| 3792 |
RealType minerr = starterr, deltaminerr = 1, lasterr = starterr * 2; |
| 3793 |
while (deltaminerr > 1.e-4 && approxerr > 0 && ct < maxits) |
| 3794 |
{ |
| 3795 |
RealType alpha_denom = inner_product(p_k, At * (A * p_k)); |
| 3796 |
RealType iprk = inner_product(r_k, r_k); |
| 3797 |
if (alpha_denom < 1.e-12) |
| 3798 |
{ |
| 3799 |
alpha_denom = 1; |
| 3800 |
} |
| 3801 |
RealType alpha_k = iprk / alpha_denom; |
| 3802 |
VectorType x_k1 = x_k + alpha_k * p_k; // this adds the scaled residual to the current solution |
| 3803 |
x_k1 = x_k + alpha_k * p_k * .2; // this adds the scaled residual to the current solution |
| 3804 |
VectorType r_k1 = b - At * (A * x_k1) - x_k1 * lambda; |
| 3805 |
approxerr = (b - At * (A * x_k1)).two_norm() + x_k1.two_norm() * lambda; |
| 3806 |
if (approxerr < minerr) |
| 3807 |
{ |
| 3808 |
minerr = approxerr; |
| 3809 |
bestsol = (x_k1); |
| 3810 |
} |
| 3811 |
deltaminerr = (lasterr - approxerr); |
| 3812 |
lasterr = approxerr; |
| 3813 |
RealType bkdenom = inner_product(r_k, r_k); |
| 3814 |
if (bkdenom < 1.e-12) |
| 3815 |
{ |
| 3816 |
bkdenom = 1; |
| 3817 |
} |
| 3818 |
RealType beta_k = inner_product(r_k1, r_k1) / bkdenom; // classic cg |
| 3819 |
VectorType p_k1 = r_k1 + beta_k * p_k; |
| 3820 |
r_k = r_k1; |
| 3821 |
p_k = p_k1; |
| 3822 |
x_k = x_k1; |
| 3823 |
ct++; |
| 3824 |
} |
| 3825 |
|
| 3826 |
x_k = bestsol; |
| 3827 |
if (makesparse) |
| 3828 |
{ |
| 3829 |
this->SparsifyP(x_k); |
| 3830 |
} |
| 3831 |
return minerr; |
| 3832 |
} |
| 3833 |
|
| 3834 |
template <typename TInputImage, typename TRealType> |
| 3835 |
TRealType |
| 3836 |
antsSCCANObject<TInputImage, TRealType>::CGSPCA(unsigned int n_vecs) |
| 3837 |
{ |
| 3838 |
if (!this->m_Silent) |
| 3839 |
std::cout << " conjugate gradient sparse-pca approx to pca " << std::endl; |
| 3840 |
std::vector<RealType> vexlist; |
| 3841 |
|
| 3842 |
this->m_MatrixP = this->NormalizeMatrix(this->m_OriginalMatrixP); |
| 3843 |
this->m_MatrixQ = this->m_MatrixP; |
| 3844 |
if (this->m_OriginalMatrixR.size() > 0) |
| 3845 |
{ |
| 3846 |
this->m_MatrixRRt = this->ProjectionMatrix(this->m_OriginalMatrixR); |
| 3847 |
this->m_MatrixP = this->m_MatrixP - (this->m_MatrixRRt * this->m_MatrixP); |
| 3848 |
} |
| 3849 |
MatrixType nspaceevecs = this->GetCovMatEigenvectors(this->m_MatrixP); |
| 3850 |
VectorType nspaceevals = this->m_Eigenvalues; |
| 3851 |
this->BasicSVD(); |
| 3852 |
MatrixType variatesInit = this->m_VariatesP; |
| 3853 |
// unsigned int n_vecs = this->m_VariatesP.cols(); |
| 3854 |
VectorType evalInit(n_vecs); |
| 3855 |
unsigned int evct = 0; |
| 3856 |
for (unsigned int i = 0; i < this->m_Eigenvalues.size(); i++) |
| 3857 |
{ |
| 3858 |
if (evct < n_vecs) |
| 3859 |
{ |
| 3860 |
evalInit[evct] = this->m_Eigenvalues(i); |
| 3861 |
} |
| 3862 |
evct++; |
| 3863 |
if (evct < n_vecs) |
| 3864 |
{ |
| 3865 |
evalInit[evct] = this->m_Eigenvalues(i); |
| 3866 |
} |
| 3867 |
evct++; |
| 3868 |
} |
| 3869 |
unsigned int repspervec = this->m_MaximumNumberOfIterations; |
| 3870 |
this->m_VariatesP.set_size(this->m_MatrixP.cols(), repspervec * n_vecs); |
| 3871 |
this->m_VariatesP.fill(0); |
| 3872 |
RealType fnp = this->m_FractionNonZeroP; |
| 3873 |
for (unsigned int colind = 0; colind < n_vecs; colind++) |
| 3874 |
{ |
| 3875 |
if (!this->m_Silent) |
| 3876 |
std::cout << " colind " << colind << std::endl; |
| 3877 |
for (unsigned int whichevec = 0; whichevec < repspervec; whichevec++) |
| 3878 |
{ |
| 3879 |
VectorType b = this->m_Eigenvectors.get_column(colind); |
| 3880 |
unsigned int baseind = colind * repspervec; |
| 3881 |
// unsigned int lastbaseind = colind * repspervec; |
| 3882 |
// if( colind % 2 == 1 ) |
| 3883 |
// { |
| 3884 |
// lastbaseind = ( colind - 1 ) * repspervec; |
| 3885 |
// } |
| 3886 |
unsigned int locind = baseind + whichevec; |
| 3887 |
VectorType x_k = this->InitializeV(this->m_MatrixP, false); |
| 3888 |
this->m_FractionNonZeroP = fnp + fnp * whichevec; |
| 3889 |
RealType minerr1 = this->SparseNLConjGrad(this->m_MatrixP, x_k, b, 1.e-1, 10, true); // , 0 , colind ); |
| 3890 |
// // , baseind , |
| 3891 |
// locind |
| 3892 |
bool keepgoing = true; |
| 3893 |
unsigned int kgct = 0; |
| 3894 |
while (keepgoing && kgct < 100) |
| 3895 |
{ |
| 3896 |
VectorType x_k2 = x_k; |
| 3897 |
RealType minerr2 = this->SparseNLConjGrad(this->m_MatrixP, x_k2, b, 1.e-1, 10, true); // , 0 , colind |
| 3898 |
// ); |
| 3899 |
keepgoing = false; |
| 3900 |
// if ( fabs( minerr2 - minerr1 ) < 1.e-9 ) { x_k = x_k2; keepgoing = true; minerr1 = minerr2 ; } |
| 3901 |
if (minerr2 < minerr1) |
| 3902 |
{ |
| 3903 |
x_k = x_k2; |
| 3904 |
keepgoing = true; |
| 3905 |
minerr1 = minerr2; |
| 3906 |
} |
| 3907 |
// if ( ! this->m_Silent ) std::cout << " minerr1 " << minerr1 << " wev " << whichevec << std::endl; |
| 3908 |
kgct++; |
| 3909 |
} |
| 3910 |
for (unsigned int j = baseind; j < locind; j++) |
| 3911 |
{ |
| 3912 |
VectorType temp = this->m_VariatesP.get_column(j); |
| 3913 |
this->ZeroProduct(x_k, temp); |
| 3914 |
} |
| 3915 |
this->m_VariatesP.set_column(locind, x_k); |
| 3916 |
} // repspervec |
| 3917 |
} // colind |
| 3918 |
RealType reconstruction_error = this->ReconstructionError(this->m_MatrixP, this->m_VariatesP); |
| 3919 |
if (!this->m_Silent) |
| 3920 |
std::cout << "%Var " << reconstruction_error << std::endl; |
| 3921 |
for (unsigned int k = 0; k < this->m_VariatesP.cols(); k++) |
| 3922 |
{ |
| 3923 |
VectorType v = this->m_VariatesP.get_column(k); |
| 3924 |
this->m_VariatesP.set_column(k, v / v.sum()); |
| 3925 |
} |
| 3926 |
this->m_Eigenvalues = this->m_CanonicalCorrelations = evalInit; |
| 3927 |
return this->m_CanonicalCorrelations[0]; |
| 3928 |
} |
| 3929 |
|
| 3930 |
template <typename TInputImage, typename TRealType> |
| 3931 |
void |
| 3932 |
antsSCCANObject<TInputImage, TRealType>::DeleteRow(typename antsSCCANObject<TInputImage, TRealType>::MatrixType & p_in, |
| 3933 |
unsigned int row) |
| 3934 |
{ |
| 3935 |
unsigned int nrows = p_in.rows() - 1; |
| 3936 |
|
| 3937 |
if (row >= nrows) |
| 3938 |
{ |
| 3939 |
nrows = p_in.rows(); |
| 3940 |
} |
| 3941 |
MatrixType p(nrows, p_in.columns()); |
| 3942 |
unsigned int rowct = 0; |
| 3943 |
for (long i = 0; i < p.rows(); ++i) // loop over rows |
| 3944 |
{ |
| 3945 |
if (i != row) |
| 3946 |
{ |
| 3947 |
p.set_row(rowct, p_in.get_row(i)); |
| 3948 |
rowct++; |
| 3949 |
} |
| 3950 |
} |
| 3951 |
p_in = p; |
| 3952 |
return; |
| 3953 |
} |
| 3954 |
|
| 3955 |
template <typename TInputImage, typename TRealType> |
| 3956 |
void |
| 3957 |
antsSCCANObject<TInputImage, TRealType>::LASSO_alg( |
| 3958 |
typename antsSCCANObject<TInputImage, TRealType>::MatrixType & X, |
| 3959 |
typename antsSCCANObject<TInputImage, TRealType>::VectorType & y, |
| 3960 |
typename antsSCCANObject<TInputImage, TRealType>::VectorType & beta_lasso, |
| 3961 |
TRealType gamma, |
| 3962 |
unsigned int maxits) |
| 3963 |
{ |
| 3964 |
VectorType ypred; |
| 3965 |
VectorType yresid; |
| 3966 |
RealType besterr = 1.e20; |
| 3967 |
RealType curerr = besterr; |
| 3968 |
unsigned int added = 0; |
| 3969 |
|
| 3970 |
for (unsigned int its = 0; its < maxits; its++) |
| 3971 |
{ |
| 3972 |
added = 0; |
| 3973 |
ypred = X * beta_lasso; |
| 3974 |
for (unsigned int j = 0; j < beta_lasso.size(); j++) |
| 3975 |
{ |
| 3976 |
VectorType xj = X.get_column(j); |
| 3977 |
ypred = ypred - xj * beta_lasso(j); |
| 3978 |
|
| 3979 |
RealType regbeta = this->SimpleRegression(y, ypred); |
| 3980 |
yresid = y - (ypred * regbeta + this->m_Intercept); |
| 3981 |
|
| 3982 |
RealType beta = beta_lasso(j); |
| 3983 |
for (unsigned int i = 0; i < xj.size(); i++) |
| 3984 |
{ |
| 3985 |
beta += (xj(i) * yresid(i)); |
| 3986 |
} |
| 3987 |
beta = this->LASSOSoft(beta, gamma); |
| 3988 |
ypred = ypred + xj * beta; |
| 3989 |
regbeta = this->SimpleRegression(y, ypred); |
| 3990 |
yresid = y - (ypred * regbeta + this->m_Intercept); |
| 3991 |
|
| 3992 |
RealType sparseness = gamma * (beta_lasso.one_norm() + fabs(beta) - fabs(beta_lasso(j))); |
| 3993 |
curerr = yresid.two_norm() * 0.5 + sparseness; /** Eq 7 Friedman pathwise */ |
| 3994 |
// if ( ! this->m_Silent ) std::cout << "Err: " << yresid.one_norm() * n << " sp: " << sparseness << " cur " << |
| 3995 |
// curerr << " best " |
| 3996 |
// << besterr << std::endl; |
| 3997 |
if (curerr < besterr) |
| 3998 |
{ |
| 3999 |
beta_lasso(j) = beta; |
| 4000 |
besterr = curerr; |
| 4001 |
} |
| 4002 |
else |
| 4003 |
{ |
| 4004 |
ypred = ypred - xj * beta; |
| 4005 |
ypred = ypred + xj * beta_lasso(j); |
| 4006 |
} |
| 4007 |
if (fabs(beta_lasso(j)) > 0) |
| 4008 |
{ |
| 4009 |
added++; |
| 4010 |
} |
| 4011 |
if (j % 1000 == 0 && false) |
| 4012 |
{ |
| 4013 |
if (!this->m_Silent) |
| 4014 |
std::cout << "progress: " << 100 * j / X.cols() << " lasso-loop " << its << " : " << besterr |
| 4015 |
<< " num_vars_added " << added << std::endl; |
| 4016 |
} |
| 4017 |
} |
| 4018 |
// if ( ! this->m_Silent ) std::cout << " lasso-iteration: " << its << " minerr: " << besterr << " |
| 4019 |
// num_vars_added: " << added |
| 4020 |
// << std::endl; |
| 4021 |
} // iterations |
| 4022 |
} |
| 4023 |
|
| 4024 |
template <typename TInputImage, typename TRealType> |
| 4025 |
TRealType |
| 4026 |
antsSCCANObject<TInputImage, TRealType>::LASSO_Cross() |
| 4027 |
{ |
| 4028 |
RealType gamma = this->m_FractionNonZeroP; |
| 4029 |
|
| 4030 |
if (!this->m_Silent) |
| 4031 |
std::cout << " Cross-validation - LASSO " << std::endl; |
| 4032 |
|
| 4033 |
this->m_MatrixP = this->NormalizeMatrix(this->m_OriginalMatrixP); |
| 4034 |
this->m_MatrixR = this->NormalizeMatrix(this->m_OriginalMatrixR); |
| 4035 |
if (this->m_OriginalMatrixR.size() <= 0) |
| 4036 |
{ |
| 4037 |
if (!this->m_Silent) |
| 4038 |
std::cout << " You need to define a reference matrix " << std::endl; |
| 4039 |
std::exception(); |
| 4040 |
} |
| 4041 |
VectorType predictions(this->m_MatrixR.rows(), 0); |
| 4042 |
unsigned int foldnum = this->m_MaximumNumberOfIterations; |
| 4043 |
if (foldnum <= 1) |
| 4044 |
{ |
| 4045 |
foldnum = 1; |
| 4046 |
} |
| 4047 |
VectorType folds(this->m_MatrixP.rows(), 0); |
| 4048 |
for (unsigned int f = 0; f < this->m_MatrixP.rows(); f++) |
| 4049 |
{ |
| 4050 |
folds(f) = f % foldnum; |
| 4051 |
} |
| 4052 |
// Variates holds the average over all folds |
| 4053 |
this->m_VariatesP(this->m_MatrixP.cols(), 1); |
| 4054 |
this->m_VariatesP.fill(0); |
| 4055 |
this->m_VariatesQ = this->m_VariatesP; |
| 4056 |
for (unsigned int fold = 0; fold < foldnum; fold++) |
| 4057 |
{ |
| 4058 |
unsigned int foldct = 0; |
| 4059 |
for (unsigned int f = 0; f < this->m_MatrixP.rows(); f++) |
| 4060 |
{ |
| 4061 |
if (folds(f) == fold) |
| 4062 |
{ |
| 4063 |
foldct++; |
| 4064 |
} |
| 4065 |
} |
| 4066 |
MatrixType p_leave_out(foldct, this->m_MatrixP.cols(), 0); |
| 4067 |
MatrixType r_leave_out(foldct, this->m_MatrixR.cols(), 0); |
| 4068 |
unsigned int leftoutsize = this->m_MatrixP.rows() - foldct; |
| 4069 |
MatrixType matrixP(leftoutsize, this->m_MatrixP.cols()); |
| 4070 |
MatrixType matrixR(leftoutsize, this->m_MatrixR.cols()); |
| 4071 |
unsigned int leave_out = 0; |
| 4072 |
unsigned int dont_leave_out = 0; |
| 4073 |
for (unsigned int f = 0; f < this->m_MatrixP.rows(); f++) |
| 4074 |
{ |
| 4075 |
if (folds(f) == fold) |
| 4076 |
{ |
| 4077 |
p_leave_out.set_row(leave_out, this->m_MatrixP.get_row(f)); |
| 4078 |
r_leave_out.set_row(leave_out, this->m_OriginalMatrixR.get_row(f)); |
| 4079 |
leave_out++; |
| 4080 |
} |
| 4081 |
else |
| 4082 |
{ |
| 4083 |
matrixP.set_row(dont_leave_out, this->m_MatrixP.get_row(f)); |
| 4084 |
matrixR.set_row(dont_leave_out, this->m_MatrixR.get_row(f)); |
| 4085 |
dont_leave_out++; |
| 4086 |
} |
| 4087 |
} |
| 4088 |
if (foldnum <= 1) |
| 4089 |
{ |
| 4090 |
matrixP = p_leave_out; |
| 4091 |
matrixR = r_leave_out; |
| 4092 |
} |
| 4093 |
|
| 4094 |
VectorType rout = r_leave_out.get_column(0); |
| 4095 |
VectorType y = matrixR.get_column(0); |
| 4096 |
VectorType yreal = matrixR.get_column(0); |
| 4097 |
unsigned int fleave_out = 0; |
| 4098 |
for (unsigned int f = 0; f < this->m_MatrixP.rows(); f++) |
| 4099 |
{ |
| 4100 |
if (folds(f) != fold) |
| 4101 |
{ |
| 4102 |
yreal(fleave_out) = this->m_OriginalMatrixR(f, 0); |
| 4103 |
fleave_out++; |
| 4104 |
} |
| 4105 |
} |
| 4106 |
|
| 4107 |
/** train the lasso + regression model */ |
| 4108 |
VectorType beta_lasso(matrixP.cols(), 0); |
| 4109 |
this->LASSO_alg(matrixP, y, beta_lasso, gamma, 2); |
| 4110 |
VectorType ypred = matrixP * beta_lasso; |
| 4111 |
RealType regbeta = this->SimpleRegression(yreal, ypred); |
| 4112 |
RealType intercept = yreal.mean() - ypred.mean() * regbeta; |
| 4113 |
|
| 4114 |
/** test the lasso + regression model */ |
| 4115 |
VectorType localpredictions = p_leave_out * beta_lasso; |
| 4116 |
localpredictions = localpredictions * regbeta + intercept; |
| 4117 |
|
| 4118 |
fleave_out = 0; |
| 4119 |
for (unsigned int f = 0; f < this->m_MatrixP.rows(); f++) |
| 4120 |
{ |
| 4121 |
if (folds(f) == fold) |
| 4122 |
{ |
| 4123 |
predictions(f) = localpredictions(fleave_out); |
| 4124 |
fleave_out++; |
| 4125 |
} |
| 4126 |
} |
| 4127 |
this->m_VariatesP.set_column(0, this->m_VariatesP.get_column(0) + beta_lasso * regbeta); |
| 4128 |
if (!this->m_Silent) |
| 4129 |
std::cout << " fold " << fold << " mean-abs-error: " << (localpredictions - rout).one_norm() / foldct |
| 4130 |
<< std::endl; |
| 4131 |
} |
| 4132 |
|
| 4133 |
RealType regbeta = this->SimpleRegression(predictions, this->m_OriginalMatrixR.get_column(0)); |
| 4134 |
RealType intercept = this->m_OriginalMatrixR.get_column(0).mean() - predictions.mean() * regbeta; |
| 4135 |
RealType predictionerror = (predictions * regbeta + intercept - this->m_OriginalMatrixR.get_column(0)).one_norm() / |
| 4136 |
this->m_OriginalMatrixR.rows(); |
| 4137 |
if (!this->m_Silent) |
| 4138 |
std::cout << " overall-mean-abs-prediction-error: " << predictionerror << std::endl; |
| 4139 |
this->m_Eigenvalues = predictions * regbeta + intercept; |
| 4140 |
this->m_CanonicalCorrelations = predictions * regbeta + intercept; |
| 4141 |
return predictionerror; |
| 4142 |
} |
| 4143 |
|
| 4144 |
template <typename TInputImage, typename TRealType> |
| 4145 |
TRealType |
| 4146 |
antsSCCANObject<TInputImage, TRealType>::LASSO(unsigned int n_vecs) |
| 4147 |
{ |
| 4148 |
/** Based on Friedman's pathwise coordinate optimization */ |
| 4149 |
RealType gamma = this->m_FractionNonZeroP; |
| 4150 |
|
| 4151 |
if (!this->m_Silent) |
| 4152 |
std::cout << " Pathwise LASSO : penalty = " << gamma << " veclimit = " << n_vecs << std::endl; |
| 4153 |
|
| 4154 |
this->m_CanonicalCorrelations.set_size(1); |
| 4155 |
this->m_MatrixP = this->NormalizeMatrix(this->m_OriginalMatrixP); |
| 4156 |
this->m_MatrixR = this->NormalizeMatrix(this->m_OriginalMatrixR); |
| 4157 |
if (this->m_OriginalMatrixR.size() <= 0) |
| 4158 |
{ |
| 4159 |
if (!this->m_Silent) |
| 4160 |
std::cout << " You need to define a reference matrix " << std::endl; |
| 4161 |
std::exception(); |
| 4162 |
} |
| 4163 |
|
| 4164 |
VectorType y = this->m_MatrixR.get_column(0); |
| 4165 |
RealType n = 1.0 / static_cast<RealType>(y.size()); |
| 4166 |
VectorType beta_lasso(this->m_MatrixP.cols(), 0); |
| 4167 |
for (unsigned int i = 0; i < 1; i++) |
| 4168 |
{ |
| 4169 |
if (!this->m_Silent) |
| 4170 |
std::cout << i << std::endl; |
| 4171 |
this->LASSO_alg(this->m_MatrixP, y, beta_lasso, gamma, this->m_MaximumNumberOfIterations); |
| 4172 |
// RealType spgoal = ( RealType ) n_vecs ; |
| 4173 |
// this->CurvatureSparseness( beta_lasso , spgoal , 100 ); |
| 4174 |
} |
| 4175 |
/** post-process the result to select the largest n_vecs entries |
| 4176 |
std::vector<TRealType> post( beta_lasso.size() , 0 ); |
| 4177 |
for( unsigned long j = 0; j < beta_lasso.size(); ++j ) post[ j ] = fabs( beta_lasso( j ) ); |
| 4178 |
// sort and reindex the values |
| 4179 |
sort( post.begin(), post.end() ); |
| 4180 |
RealType thresh = 0; |
| 4181 |
for( unsigned long j = 0; ( ( j < beta_lasso.size() ) && ( j < n_vecs ) ); ++j ) |
| 4182 |
{ |
| 4183 |
thresh = post[j]; |
| 4184 |
} |
| 4185 |
unsigned int added = 0; |
| 4186 |
if ( thresh > 0 ) |
| 4187 |
{ |
| 4188 |
added = 0; |
| 4189 |
for( unsigned long j = 0; j < beta_lasso.size(); ++j ) |
| 4190 |
{ |
| 4191 |
if ( fabs( beta_lasso( j ) ) < thresh ) beta_lasso( j ) = 0; |
| 4192 |
else added++; |
| 4193 |
} |
| 4194 |
} |
| 4195 |
*/ |
| 4196 |
/** now get the original y value and result to find prediction error in true units */ |
| 4197 |
y = this->m_OriginalMatrixR.get_column(0); |
| 4198 |
this->m_Intercept = this->ComputeIntercept(this->m_MatrixP, beta_lasso, y); |
| 4199 |
VectorType ypred = this->m_MatrixP * beta_lasso + this->m_Intercept; |
| 4200 |
VectorType yresid = y - (ypred); |
| 4201 |
this->m_CanonicalCorrelations(0) = yresid.one_norm() * n; |
| 4202 |
this->m_VariatesP.set_size(this->m_MatrixP.cols(), 1); |
| 4203 |
this->m_VariatesP.set_column(0, beta_lasso); |
| 4204 |
if (!this->m_Silent) |
| 4205 |
std::cout << "final error " << this->m_CanonicalCorrelations(0) << std::endl; |
| 4206 |
return this->m_CanonicalCorrelations(0); |
| 4207 |
} |
| 4208 |
|
| 4209 |
template <typename TInputImage, typename TRealType> |
| 4210 |
TRealType |
| 4211 |
antsSCCANObject<TInputImage, TRealType>::SparseConjGradRidgeRegression( |
| 4212 |
typename antsSCCANObject<TInputImage, TRealType>::MatrixType & A, |
| 4213 |
typename antsSCCANObject<TInputImage, TRealType>::VectorType & x_k, |
| 4214 |
typename antsSCCANObject<TInputImage, TRealType>::VectorType b_in, |
| 4215 |
TRealType convcrit, |
| 4216 |
unsigned int maxits, |
| 4217 |
bool makesparse) |
| 4218 |
{ |
| 4219 |
x_k.fill(0); |
| 4220 |
RealType spgoal = 100.0 * (1 - itk::Math::abs(this->m_FractionNonZeroP)); |
| 4221 |
RealType lambda = 1.e2; |
| 4222 |
if (!this->m_Silent) |
| 4223 |
std::cout << "SparseConjGradRidgeRegression: lambda " << lambda << " Soft? " << this->m_UseL1 << " fnp " |
| 4224 |
<< this->m_FractionNonZeroP << std::endl; |
| 4225 |
MatrixType At = A.transpose(); |
| 4226 |
VectorType b(b_in); |
| 4227 |
/** The data term b = b_n * A may be noisy. here we optionally add a penalty |
| 4228 |
* \kappa \| \nabla b \|^2 |
| 4229 |
* which smooths the data term and eases optimization |
| 4230 |
*/ |
| 4231 |
this->CurvatureSparseness(b, spgoal, 10, this->m_MaskImageP); |
| 4232 |
VectorType r_k = At * (A * x_k); |
| 4233 |
/** this is the gradient of the objective : |
| 4234 |
* \| b - A^T A x \| + \lambda \| x \|^2 |
| 4235 |
*/ |
| 4236 |
r_k = b - r_k - x_k * lambda; |
| 4237 |
VectorType p_k = r_k; |
| 4238 |
double approxerr = 1.e9; |
| 4239 |
unsigned int ct = 0; |
| 4240 |
VectorType bestsol = x_k; |
| 4241 |
RealType starterr = 1.e99; |
| 4242 |
RealType minerr = starterr, deltaminerr = 1, lasterr = starterr * 2; |
| 4243 |
if (makesparse) |
| 4244 |
{ |
| 4245 |
this->SparsifyP(x_k); |
| 4246 |
} |
| 4247 |
/** Here, should be more careful to make sure energy improves, need better command line interface for specifying |
| 4248 |
objective */ |
| 4249 |
while (((deltaminerr > 0.1) && (approxerr > convcrit) && (ct < maxits)) || (ct < 3)) |
| 4250 |
{ |
| 4251 |
/** standard conjugate gradient defintion of \alpha_k and p_k |
| 4252 |
* see http://www.matematicas.unam.mx/gfgf/cg2010/HISTORY-conjugategradient.pdf |
| 4253 |
*/ |
| 4254 |
RealType alpha_denom = inner_product(p_k, At * (A * p_k)); |
| 4255 |
RealType iprk = inner_product(r_k, r_k); |
| 4256 |
if (alpha_denom < 1.e-12) |
| 4257 |
{ |
| 4258 |
alpha_denom = 1; |
| 4259 |
} |
| 4260 |
/** A line search on the objective term , given search direction p_k |
| 4261 |
* see http://en.wikipedia.org/wiki/Nonlinear_conjugate_gradient_method |
| 4262 |
*/ |
| 4263 |
RealType alpha_k = iprk / alpha_denom; |
| 4264 |
VectorType x_k1 = x_k + alpha_k * p_k; |
| 4265 |
/** To be careful (not necessary) orthogonalize with previous solutions |
| 4266 |
for( unsigned int col = 0; col < this->m_VariatesP.cols(); col++ ) |
| 4267 |
{ |
| 4268 |
x_k1 = this->Orthogonalize( x_k1, this->m_VariatesP.get_column( col ) ); |
| 4269 |
} |
| 4270 |
*/ |
| 4271 |
// VectorType kvec( x_k1 ); |
| 4272 |
// RealType gradnorm = this->ComputeVectorGradMag( x_k1 , this->m_MaskImageP ).two_norm() ; |
| 4273 |
this->CurvatureSparseness(x_k1, spgoal, 5, this->m_MaskImageP); |
| 4274 |
// VectorType gradvec = this->ComputeVectorLaplacian( x_k1 , this->m_MaskImageP ); |
| 4275 |
if (makesparse) |
| 4276 |
{ |
| 4277 |
/** use a hard thresholding on the update to enforce l_0 */ |
| 4278 |
this->SparsifyP(x_k1); |
| 4279 |
} |
| 4280 |
RealType dataterm = (b - At * (A * x_k1)).two_norm(); |
| 4281 |
// VectorType r_k1 = b - At * (A * x_k1 ) + gradvec * lambda; |
| 4282 |
// approxerr = dataterm + gradnorm * lambda; |
| 4283 |
/** compute the new gradient term */ |
| 4284 |
VectorType r_k1 = b - At * (A * x_k1) - x_k1 * lambda; // ridge |
| 4285 |
approxerr = dataterm + x_k1.two_norm() * lambda; |
| 4286 |
if (approxerr < minerr) |
| 4287 |
{ |
| 4288 |
minerr = approxerr; |
| 4289 |
bestsol = (x_k1); |
| 4290 |
} |
| 4291 |
if (approxerr > minerr || ct % 20 == 0) |
| 4292 |
{ |
| 4293 |
this->m_GoldenSectionCounter = 0; |
| 4294 |
RealType tau = 0.00001; |
| 4295 |
RealType loa = alpha_k * 0.25; |
| 4296 |
RealType hia = alpha_k * 1.e10; |
| 4297 |
RealType mida = (loa + hia) * 0.5; |
| 4298 |
alpha_k = this->GoldenSection(A, x_k, p_k, b, loa, mida, hia, tau, lambda); |
| 4299 |
x_k1 = x_k + alpha_k * p_k; |
| 4300 |
this->CurvatureSparseness(x_k1, spgoal, 5, this->m_MaskImageP); |
| 4301 |
this->SparsifyP(x_k1); |
| 4302 |
dataterm = (b - At * (A * x_k1)).two_norm(); |
| 4303 |
r_k1 = b - At * (A * x_k1) - x_k1 * lambda; // ridge |
| 4304 |
approxerr = dataterm + x_k1.two_norm() * lambda; |
| 4305 |
if (approxerr < minerr) |
| 4306 |
{ |
| 4307 |
minerr = approxerr; |
| 4308 |
bestsol = (x_k1); |
| 4309 |
} |
| 4310 |
} |
| 4311 |
deltaminerr = (lasterr - approxerr); |
| 4312 |
if (!this->m_Silent) |
| 4313 |
std::cout << " SparseRidgeConjGrad " << approxerr << " deltaminerr " << deltaminerr << " ct " << ct |
| 4314 |
<< " dataterm " << dataterm << std::endl; |
| 4315 |
lasterr = approxerr; |
| 4316 |
/** compute the new conjugate gradient term using standard approaches */ |
| 4317 |
RealType bkdenom = inner_product(r_k, r_k); |
| 4318 |
if (bkdenom < 1.e-12) |
| 4319 |
{ |
| 4320 |
bkdenom = 1; |
| 4321 |
} |
| 4322 |
RealType beta_k = inner_product(r_k1, r_k1) / bkdenom; // classic cg |
| 4323 |
VectorType p_k1 = r_k1 + beta_k * p_k; |
| 4324 |
r_k = r_k1; |
| 4325 |
p_k = p_k1; |
| 4326 |
x_k = x_k1; |
| 4327 |
ct++; |
| 4328 |
} |
| 4329 |
|
| 4330 |
return minerr; |
| 4331 |
} |
| 4332 |
|
| 4333 |
template <typename TInputImage, typename TRealType> |
| 4334 |
TRealType |
| 4335 |
antsSCCANObject<TInputImage, TRealType>::IHTRegression( |
| 4336 |
typename antsSCCANObject<TInputImage, TRealType>::MatrixType & A, |
| 4337 |
typename antsSCCANObject<TInputImage, TRealType>::VectorType & x_k, |
| 4338 |
typename antsSCCANObject<TInputImage, TRealType>::VectorType b_in, |
| 4339 |
TRealType itkNotUsed(convcrit), |
| 4340 |
unsigned int maxits, |
| 4341 |
TRealType mu, |
| 4342 |
bool isp, |
| 4343 |
bool verbose) |
| 4344 |
{ |
| 4345 |
if (b_in.size() != x_k.size()) |
| 4346 |
{ |
| 4347 |
if (!this->m_Silent) |
| 4348 |
std::cout << " ITHRegression error --- b_in is the wrong size " << std::endl; |
| 4349 |
return 1.e9; |
| 4350 |
} |
| 4351 |
vnl_diag_matrix<TRealType> mudiag(A.cols(), mu); |
| 4352 |
RealType lambda = 0.e2; |
| 4353 |
MatrixType At = A.transpose(); |
| 4354 |
VectorType b(b_in); |
| 4355 |
VectorType p_k = At * (A * x_k); |
| 4356 |
p_k = b - p_k - x_k * lambda; |
| 4357 |
VectorType curdir = p_k; |
| 4358 |
VectorType lastdir = p_k; |
| 4359 |
double approxerr = 1.e9; |
| 4360 |
unsigned int ct = 0; |
| 4361 |
VectorType bestsol = x_k; |
| 4362 |
RealType starterr = 1.e99; |
| 4363 |
RealType minerr = starterr, deltaminerr = 1, lasterr = starterr * 2; |
| 4364 |
if (isp) |
| 4365 |
{ |
| 4366 |
this->SparsifyP(x_k); |
| 4367 |
} |
| 4368 |
else |
| 4369 |
{ |
| 4370 |
this->SparsifyQ(x_k); |
| 4371 |
} |
| 4372 |
RealType alpha_k = 1.e-4 / A.frobenius_norm(); |
| 4373 |
if (verbose) |
| 4374 |
{ |
| 4375 |
if (!this->m_Silent) |
| 4376 |
std::cout << " alpha_k " << alpha_k << " xknorm " << x_k.two_norm() << " Anorm " << A.frobenius_norm() |
| 4377 |
<< std::endl; |
| 4378 |
} |
| 4379 |
RealType gamma = 0; |
| 4380 |
bool conjgrad = true; |
| 4381 |
VectorType lastgrad = x_k; |
| 4382 |
// while( ( ( deltaminerr > 0.1 ) && ( approxerr > convcrit ) && ( ct < maxits ) ) |
| 4383 |
// || ( ct < 4 ) ) |
| 4384 |
while ((ct < maxits) && alpha_k > 1.e-14) |
| 4385 |
{ |
| 4386 |
if ((lastgrad.two_norm() > 0) && (conjgrad) && (ct > 2)) |
| 4387 |
{ |
| 4388 |
gamma = inner_product(p_k, p_k) / inner_product(lastgrad, lastgrad); |
| 4389 |
} |
| 4390 |
lastdir = curdir; |
| 4391 |
curdir = (p_k + gamma * lastdir); |
| 4392 |
if (!conjgrad) |
| 4393 |
{ |
| 4394 |
curdir = p_k; |
| 4395 |
} |
| 4396 |
VectorType x_k1 = x_k + alpha_k * curdir; |
| 4397 |
for (unsigned int vp = 0; vp < this->m_VariatesP.cols(); vp++) |
| 4398 |
x_k1 = this->Orthogonalize(x_k1, this->m_VariatesP.get_column(vp)); |
| 4399 |
VectorType gradvec; |
| 4400 |
if (isp) |
| 4401 |
{ |
| 4402 |
// x_k1 = this->SpatiallySmoothVector( x_k1, this->m_MaskImageP ); |
| 4403 |
this->SparsifyP(x_k1); |
| 4404 |
gradvec = this->ComputeVectorLaplacian(x_k1, this->m_MaskImageP); |
| 4405 |
} |
| 4406 |
else |
| 4407 |
{ |
| 4408 |
// x_k1 = this->SpatiallySmoothVector( x_k1, this->m_MaskImageQ ); |
| 4409 |
this->SparsifyQ(x_k1); |
| 4410 |
gradvec = this->ComputeVectorLaplacian(x_k1, this->m_MaskImageQ); |
| 4411 |
} |
| 4412 |
VectorType update = At * (A * x_k1) - mudiag * x_k1; |
| 4413 |
RealType dataterm = (b - update).two_norm(); |
| 4414 |
lastgrad = p_k; |
| 4415 |
p_k = b - update - x_k1 * lambda + gradvec * 0.e4; // ridge |
| 4416 |
approxerr = dataterm + x_k1.two_norm() * lambda; |
| 4417 |
if (approxerr < minerr) |
| 4418 |
{ |
| 4419 |
minerr = approxerr; |
| 4420 |
bestsol = (x_k1); |
| 4421 |
} |
| 4422 |
else |
| 4423 |
{ |
| 4424 |
alpha_k *= 0.5; |
| 4425 |
} |
| 4426 |
deltaminerr = (lasterr - approxerr); |
| 4427 |
if (verbose) |
| 4428 |
{ |
| 4429 |
if (!this->m_Silent) |
| 4430 |
std::cout << " IHTRegression " << approxerr << " deltaminerr " << deltaminerr << " ct " << ct << " dataterm " |
| 4431 |
<< dataterm << " alpha_k " << alpha_k << std::endl; |
| 4432 |
} |
| 4433 |
lasterr = approxerr; |
| 4434 |
x_k = x_k1; |
| 4435 |
ct++; |
| 4436 |
} |
| 4437 |
|
| 4438 |
return minerr; |
| 4439 |
} |
| 4440 |
|
| 4441 |
template <typename TInputImage, typename TRealType> |
| 4442 |
TRealType |
| 4443 |
antsSCCANObject<TInputImage, TRealType>::MatchingPursuit( |
| 4444 |
typename antsSCCANObject<TInputImage, TRealType>::MatrixType & A, |
| 4445 |
typename antsSCCANObject<TInputImage, TRealType>::VectorType & x_k, |
| 4446 |
TRealType convcrit, |
| 4447 |
unsigned int maxits) |
| 4448 |
{ |
| 4449 |
double approxerr = 1.e9; |
| 4450 |
unsigned int ct = 0; |
| 4451 |
VectorType bestsol = x_k; |
| 4452 |
RealType starterr = 1.e99; |
| 4453 |
RealType minerr = starterr, deltaminerr = 1, lasterr = starterr * 2; |
| 4454 |
|
| 4455 |
this->m_Indicator.set_size(A.cols()); |
| 4456 |
this->m_Indicator.fill(0); |
| 4457 |
MatrixType Ai; |
| 4458 |
VectorType resid = this->m_OriginalB; |
| 4459 |
while (((deltaminerr > 1.e-3) && (approxerr > convcrit) && (ct < maxits)) || (ct < 10)) |
| 4460 |
{ |
| 4461 |
VectorType corrb(x_k); |
| 4462 |
for (unsigned int mm = 0; mm < A.cols(); mm++) |
| 4463 |
{ |
| 4464 |
corrb(mm) = itk::Math::abs(this->PearsonCorr(resid, A.get_column(mm))); |
| 4465 |
} |
| 4466 |
this->ReSoftThreshold(corrb, itk::Math::abs(this->m_FractionNonZeroP), true); |
| 4467 |
|
| 4468 |
/** this debiases the solution */ |
| 4469 |
unsigned int nzct = 0; |
| 4470 |
for (unsigned int mm = 0; mm < A.cols(); mm++) |
| 4471 |
{ |
| 4472 |
if (itk::Math::abs(corrb(mm)) > 0) |
| 4473 |
{ |
| 4474 |
this->m_Indicator(mm, mm) = 1; |
| 4475 |
} |
| 4476 |
} |
| 4477 |
this->GetSubMatrix(A, Ai); |
| 4478 |
VectorType lmsolv = this->InitializeV(Ai, true); |
| 4479 |
(void)this->ConjGrad(Ai, lmsolv, resid, 0, 10000); |
| 4480 |
x_k.fill(0); |
| 4481 |
for (unsigned int mm = 0; mm < A.cols(); mm++) |
| 4482 |
{ |
| 4483 |
if (this->m_Indicator(mm, mm) > 0) |
| 4484 |
{ |
| 4485 |
x_k(mm) = lmsolv(nzct); |
| 4486 |
nzct++; |
| 4487 |
} |
| 4488 |
} |
| 4489 |
this->ReSoftThreshold(x_k, itk::Math::abs(this->m_FractionNonZeroP), true); |
| 4490 |
this->ClusterThresholdVariate(x_k, this->m_MaskImageP, this->m_MinClusterSizeP); |
| 4491 |
|
| 4492 |
this->m_Intercept = this->ComputeIntercept(A, x_k, this->m_OriginalB); |
| 4493 |
VectorType soln = A * x_k + this->m_Intercept; |
| 4494 |
resid = (this->m_OriginalB - soln); |
| 4495 |
approxerr = resid.one_norm(); |
| 4496 |
if (approxerr < minerr) |
| 4497 |
{ |
| 4498 |
minerr = approxerr; |
| 4499 |
bestsol = (x_k); |
| 4500 |
} |
| 4501 |
deltaminerr = (lasterr - approxerr); |
| 4502 |
if (!this->m_Silent) |
| 4503 |
std::cout << " MatchingPursuit " << approxerr << " deltaminerr " << deltaminerr << " ct " << ct << " diag " |
| 4504 |
<< this->m_Indicator.diagonal().sum() << std::endl; |
| 4505 |
lasterr = approxerr; |
| 4506 |
ct++; |
| 4507 |
} |
| 4508 |
|
| 4509 |
return minerr; |
| 4510 |
} |
| 4511 |
|
| 4512 |
template <typename TInputImage, typename TRealType> |
| 4513 |
TRealType |
| 4514 |
antsSCCANObject<TInputImage, TRealType>::NetworkDecomposition(unsigned int n_vecs) |
| 4515 |
{ |
| 4516 |
// MatrixType rmat = this->m_OriginalMatrixR.extract( |
| 4517 |
// this->m_OriginalMatrixR.rows() , this->m_OriginalMatrixR.cols() - 1 , 0 , 1 ); |
| 4518 |
// rmat = this->NormalizeMatrix( rmat ); |
| 4519 |
/** Based on Golub CONJUGATE G R A D I E N T A N D LANCZOS HISTORY |
| 4520 |
* http://www.matematicas.unam.mx/gfgf/cg2010/HISTORY-conjugategradient.pdf |
| 4521 |
*/ |
| 4522 |
if (!this->m_Silent) |
| 4523 |
std::cout << " network decomposition using nlcg & normal equations " << std::endl; |
| 4524 |
|
| 4525 |
this->m_CanonicalCorrelations.set_size(this->m_OriginalMatrixP.rows()); |
| 4526 |
this->m_MatrixP = this->NormalizeMatrix(this->m_OriginalMatrixP); |
| 4527 |
this->m_MatrixR = this->NormalizeMatrix(this->m_OriginalMatrixR); |
| 4528 |
this->m_MatrixR = this->m_OriginalMatrixR; |
| 4529 |
if (this->m_OriginalMatrixR.size() <= 0) |
| 4530 |
{ |
| 4531 |
if (!this->m_Silent) |
| 4532 |
std::cout << " You need to define a reference matrix " << std::endl; |
| 4533 |
std::exception(); |
| 4534 |
} |
| 4535 |
|
| 4536 |
unsigned int foldnum = this->m_MaximumNumberOfIterations; |
| 4537 |
if (foldnum <= 1) |
| 4538 |
{ |
| 4539 |
foldnum = 1; |
| 4540 |
} |
| 4541 |
VectorType folds(this->m_MatrixP.rows(), 0); |
| 4542 |
for (unsigned int f = 0; f < this->m_MatrixP.rows(); f++) |
| 4543 |
{ |
| 4544 |
folds(f) = f % foldnum; |
| 4545 |
} |
| 4546 |
RealType avgprederr = 0.0; |
| 4547 |
RealType avgfiterr = 0.0; |
| 4548 |
unsigned int extra_cols = 0; |
| 4549 |
// VariatesQ holds the average over all folds |
| 4550 |
this->m_VariatesQ.set_size(this->m_MatrixP.cols(), n_vecs + extra_cols); |
| 4551 |
this->m_VariatesQ.fill(0); |
| 4552 |
RealType totalloerror = 0; |
| 4553 |
RealType loerror = 0; |
| 4554 |
unsigned int predct = 0; |
| 4555 |
for (unsigned int fold = 0; fold < foldnum; fold++) |
| 4556 |
{ |
| 4557 |
unsigned int foldct = 0; |
| 4558 |
for (unsigned int f = 0; f < this->m_MatrixP.rows(); f++) |
| 4559 |
{ |
| 4560 |
if (itk::Math::FloatAlmostEqual(folds(f), static_cast<RealType>(fold))) |
| 4561 |
{ |
| 4562 |
foldct++; |
| 4563 |
} |
| 4564 |
} |
| 4565 |
MatrixType p_leave_out(foldct, this->m_MatrixP.cols(), 0); |
| 4566 |
MatrixType r_leave_out(foldct, this->m_MatrixR.cols(), 0); |
| 4567 |
unsigned int leftoutsize = this->m_MatrixP.rows() - foldct; |
| 4568 |
MatrixType matrixP(leftoutsize, this->m_MatrixP.cols()); |
| 4569 |
MatrixType matrixR(leftoutsize, this->m_MatrixR.cols()); |
| 4570 |
unsigned int leave_out = 0; |
| 4571 |
unsigned int dont_leave_out = 0; |
| 4572 |
for (unsigned int f = 0; f < this->m_MatrixP.rows(); f++) |
| 4573 |
{ |
| 4574 |
if (itk::Math::FloatAlmostEqual(folds(f), static_cast<RealType>(fold))) |
| 4575 |
{ |
| 4576 |
p_leave_out.set_row(leave_out, this->m_MatrixP.get_row(f)); |
| 4577 |
r_leave_out.set_row(leave_out, this->m_MatrixR.get_row(f)); |
| 4578 |
leave_out++; |
| 4579 |
} |
| 4580 |
else |
| 4581 |
{ |
| 4582 |
matrixP.set_row(dont_leave_out, this->m_MatrixP.get_row(f)); |
| 4583 |
matrixR.set_row(dont_leave_out, this->m_MatrixR.get_row(f)); |
| 4584 |
dont_leave_out++; |
| 4585 |
} |
| 4586 |
} |
| 4587 |
if (foldnum <= 1) |
| 4588 |
{ |
| 4589 |
matrixP = p_leave_out; |
| 4590 |
matrixR = r_leave_out; |
| 4591 |
} |
| 4592 |
|
| 4593 |
if (matrixR.cols() > 1 && false) |
| 4594 |
{ |
| 4595 |
MatrixType m(matrixR.rows(), matrixR.cols() - 1, 0); |
| 4596 |
for (unsigned int mm = 1; mm < matrixR.cols(); mm++) |
| 4597 |
{ |
| 4598 |
m.set_row(mm, matrixR.get_column(mm)); |
| 4599 |
} |
| 4600 |
MatrixType projmat = this->ProjectionMatrix(m, 1.e-2); |
| 4601 |
matrixP = matrixP - projmat * matrixP; |
| 4602 |
|
| 4603 |
MatrixType m2(r_leave_out.rows(), r_leave_out.cols() - 1, 0); |
| 4604 |
for (unsigned int mm = 1; mm < r_leave_out.cols(); mm++) |
| 4605 |
{ |
| 4606 |
m2.set_row(mm, r_leave_out.get_column(mm)); |
| 4607 |
} |
| 4608 |
projmat = this->ProjectionMatrix(m2, 1.e-2); |
| 4609 |
p_leave_out = p_leave_out - projmat * p_leave_out; |
| 4610 |
} |
| 4611 |
|
| 4612 |
VectorType soln; |
| 4613 |
MatrixType A; |
| 4614 |
this->m_VariatesP.set_size(matrixP.cols(), n_vecs + extra_cols); |
| 4615 |
this->m_VariatesP.fill(0); |
| 4616 |
VectorType intercept(matrixP.cols(), 1); |
| 4617 |
if (extra_cols > 0) |
| 4618 |
{ |
| 4619 |
this->m_VariatesP.set_column(0, intercept); |
| 4620 |
} |
| 4621 |
VectorType original_b = matrixR.get_column(0); |
| 4622 |
unsigned int colind = extra_cols; |
| 4623 |
// NOT USED: RealType minerr1; |
| 4624 |
bool addcol = false; |
| 4625 |
matrixP = this->NormalizeMatrix(matrixP); |
| 4626 |
while (colind < this->m_VariatesP.cols()) |
| 4627 |
{ |
| 4628 |
VectorType b = original_b; |
| 4629 |
this->m_OriginalB = original_b; |
| 4630 |
VectorType x_k = this->m_VariatesP.get_column(colind); |
| 4631 |
/***************************************/ |
| 4632 |
if (colind > 0) |
| 4633 |
{ |
| 4634 |
A = matrixP * this->m_VariatesP.extract(matrixP.cols(), colind, 0, 0); |
| 4635 |
if (addcol) |
| 4636 |
{ |
| 4637 |
this->AddColumnsToMatrix(A, matrixR, 1, this->m_MatrixR.cols() - 1); |
| 4638 |
} |
| 4639 |
VectorType lmsolv(A.cols(), 1); |
| 4640 |
(void)this->ConjGrad(A, lmsolv, original_b, 0, 10000); |
| 4641 |
VectorType v = (A * lmsolv + this->m_Intercept); |
| 4642 |
b = this->Orthogonalize(b, v); |
| 4643 |
} |
| 4644 |
unsigned int adder = 0; |
| 4645 |
b = b - b.mean(); |
| 4646 |
for (unsigned int cl = colind; cl < colind + 2; cl++) |
| 4647 |
{ |
| 4648 |
VectorType randv = this->InitializeV(matrixP, 0); |
| 4649 |
VectorType bp = b * matrixP; |
| 4650 |
VectorType bpneg(bp); |
| 4651 |
this->PosNegVector(bp, true); |
| 4652 |
this->PosNegVector(bpneg, false); |
| 4653 |
if (cl % 2 != 0) |
| 4654 |
{ |
| 4655 |
bp = bpneg * (-1); |
| 4656 |
} |
| 4657 |
// minerr1 = this->SparseNLPreConjGrad( matrixP, randv, bp, 1.e-1, 100 ); /**/ |
| 4658 |
// minerr1 = this->SparseNLConjGrad( matrixP, randv, bp, 1.e-5, 500, true , true ); /** miccai good? */ |
| 4659 |
// minerr1 = this->MatchingPursuit( matrixP , randv, 0, 90 );/** bad */ |
| 4660 |
// minerr1 = this->SparseConjGradRidgeRegression( matrixP, randv, bp, 0, 150, true );/** good */ |
| 4661 |
// randv.fill( 0 ); |
| 4662 |
// minerr1 = this->RidgeRegression( matrixP , randv, bp, 1.e4 , 100 , false );/** biased but ok */ |
| 4663 |
// minerr1 = this->ConjGrad( matrixP , randv, matrixP * bp , 0, 10000 ); this->SparsifyP( randv ); |
| 4664 |
// NOT USED: minerr1 = |
| 4665 |
this->IHTRegression(matrixP, randv, bp, 0, 500, 0, true, true); /** good */ |
| 4666 |
if (cl < this->m_VariatesP.cols()) |
| 4667 |
{ |
| 4668 |
this->m_VariatesP.set_column(cl, randv); |
| 4669 |
} |
| 4670 |
adder++; |
| 4671 |
if (!this->m_Silent) |
| 4672 |
std::cout << " cl " << cl << " bp mean " << bp.mean() << " randvmean " << randv.mean() << std::endl; |
| 4673 |
} |
| 4674 |
colind = colind + adder; |
| 4675 |
/***************************************/ |
| 4676 |
/* Training : Now get the LSQ regression solution */ |
| 4677 |
/***************************************/ |
| 4678 |
A = matrixP * this->m_VariatesP.extract(matrixP.cols(), colind, 0, 0); |
| 4679 |
if (addcol) |
| 4680 |
{ |
| 4681 |
this->AddColumnsToMatrix(A, matrixR, 1, this->m_MatrixR.cols() - 1); |
| 4682 |
} |
| 4683 |
VectorType lmsolv(A.cols(), 1); |
| 4684 |
(void)this->ConjGrad(A, lmsolv, original_b, 0, 10000); |
| 4685 |
this->m_Intercept = this->ComputeIntercept(A, lmsolv, original_b); |
| 4686 |
soln = A * lmsolv + this->m_Intercept; |
| 4687 |
RealType fiterr = (original_b - soln).one_norm() / original_b.size(); |
| 4688 |
avgfiterr += (fiterr * (1.0 / (RealType)foldnum)); |
| 4689 |
/** Testing */ |
| 4690 |
p_leave_out = this->NormalizeMatrix(p_leave_out); |
| 4691 |
A = p_leave_out * this->m_VariatesP.extract(matrixP.cols(), colind, 0, 0); |
| 4692 |
if (addcol) |
| 4693 |
{ |
| 4694 |
this->AddColumnsToMatrix(A, r_leave_out, 1, this->m_MatrixR.cols() - 1); |
| 4695 |
} |
| 4696 |
soln = (A * lmsolv) + this->m_Intercept; |
| 4697 |
loerror = (soln - r_leave_out.get_column(0)).one_norm() / soln.size(); |
| 4698 |
unsigned int fleave_out = 0; |
| 4699 |
if (foldnum == 1) |
| 4700 |
{ |
| 4701 |
this->m_CanonicalCorrelations = soln; |
| 4702 |
} |
| 4703 |
else |
| 4704 |
{ |
| 4705 |
for (unsigned int f = 0; f < this->m_MatrixP.rows(); f++) |
| 4706 |
{ |
| 4707 |
if (itk::Math::FloatAlmostEqual(folds(f), static_cast<RealType>(fold))) |
| 4708 |
{ |
| 4709 |
this->m_CanonicalCorrelations(f) = soln(fleave_out); |
| 4710 |
RealType temp = fabs(soln(fleave_out) - r_leave_out.get_column(0)(fleave_out)); |
| 4711 |
if (colind == this->m_VariatesP.cols()) |
| 4712 |
{ |
| 4713 |
avgprederr += temp; |
| 4714 |
predct++; |
| 4715 |
} |
| 4716 |
fleave_out++; |
| 4717 |
} |
| 4718 |
} |
| 4719 |
} |
| 4720 |
if (predct > 0) |
| 4721 |
{ |
| 4722 |
if (!this->m_Silent) |
| 4723 |
std::cout << "Fold: " << fold << " minerr," << loerror << ",col," << colind << " totalpredictionerr " |
| 4724 |
<< avgprederr / predct << " ,fiterr, " << fiterr << " Gap: " << (avgprederr / predct) - fiterr |
| 4725 |
<< std::endl; |
| 4726 |
} |
| 4727 |
else |
| 4728 |
{ |
| 4729 |
if (!this->m_Silent) |
| 4730 |
std::cout << "Fold: " << fold << " minerr," << loerror << ",col," << colind << " ,fiterr, " << fiterr |
| 4731 |
<< std::endl; |
| 4732 |
} |
| 4733 |
} |
| 4734 |
|
| 4735 |
RealType wt = ((soln - r_leave_out.get_column(0)).one_norm() / soln.size()) / |
| 4736 |
((r_leave_out.get_column(0)).one_norm() / soln.size()); |
| 4737 |
this->m_VariatesQ = this->m_VariatesQ + this->m_VariatesP * (1 / wt); |
| 4738 |
totalloerror += (1 / wt); |
| 4739 |
} |
| 4740 |
this->m_VariatesQ = this->m_VariatesQ / totalloerror; |
| 4741 |
RealType regbeta = this->SimpleRegression(this->m_CanonicalCorrelations, this->m_OriginalMatrixR.get_column(0)); |
| 4742 |
RealType intercept = this->m_OriginalMatrixR.get_column(0).mean() - this->m_CanonicalCorrelations.mean() * regbeta; |
| 4743 |
VectorType predicted = this->m_CanonicalCorrelations * regbeta + intercept; |
| 4744 |
RealType predictionerror = |
| 4745 |
(predicted - this->m_OriginalMatrixR.get_column(0)).one_norm() / this->m_OriginalMatrixR.rows(); |
| 4746 |
if (!this->m_Silent) |
| 4747 |
std::cout << " overall-mean-abs-prediction-error: " << predictionerror << " correlation " |
| 4748 |
<< this->PearsonCorr(predicted, this->m_OriginalMatrixR.get_column(0)) << " Gap " |
| 4749 |
<< avgprederr / predct - avgfiterr << std::endl; |
| 4750 |
this->m_VariatesP = this->m_VariatesQ; |
| 4751 |
for (unsigned int i = 0; i < this->m_VariatesP.cols(); i++) |
| 4752 |
{ |
| 4753 |
VectorType col = this->m_VariatesP.get_column(i); |
| 4754 |
this->SparsifyP(col); // make averaged predictors sparse! |
| 4755 |
this->m_VariatesP.set_column(i, col); |
| 4756 |
} |
| 4757 |
|
| 4758 |
return avgprederr / predct; |
| 4759 |
} |
| 4760 |
|
| 4761 |
/* |
| 4762 |
|
| 4763 |
MatrixType pmod = this->m_MatrixP; |
| 4764 |
this->m_Indicator.set_size(this->m_MatrixP.cols()); |
| 4765 |
this->m_Indicator.fill(1); |
| 4766 |
if ( whichevec > 0 && false ) |
| 4767 |
{ |
| 4768 |
// zero out already used parts of matrix |
| 4769 |
for ( unsigned int mm = baseind; mm < locind; mm++ ) |
| 4770 |
{ |
| 4771 |
VectorType u = this->m_VariatesP.get_column( mm ); |
| 4772 |
for ( unsigned int j=0; j< u.size(); j++) |
| 4773 |
if ( fabs(u(j)) > 0 ) this->m_Indicator( j , j ) = 0; |
| 4774 |
} |
| 4775 |
pmod = pmod * this->m_Indicator; |
| 4776 |
b = b * this->m_Indicator; |
| 4777 |
} |
| 4778 |
|
| 4779 |
if( whichevec > 0 && false ) |
| 4780 |
{ |
| 4781 |
MatrixType m( this->m_MatrixP.rows(), locind - baseind , 0 ); |
| 4782 |
for( unsigned int mm = baseind; mm < locind; mm++ ) |
| 4783 |
{ |
| 4784 |
m.set_row( mm, this->m_MatrixP * this->m_VariatesP.get_column( mm ) ); |
| 4785 |
} |
| 4786 |
MatrixType projmat = this->ProjectionMatrix( m, 1.e-2 ); |
| 4787 |
pmod = pmod - projmat * pmod; |
| 4788 |
} |
| 4789 |
|
| 4790 |
|
| 4791 |
|
| 4792 |
if ( whichevec > 0 ) |
| 4793 |
{ |
| 4794 |
MatrixType A = this->m_MatrixP * this->m_VariatesP.get_n_columns( baseind , whichevec ); |
| 4795 |
VectorType lmsolv( A.cols() , 1 ); |
| 4796 |
VectorType bproj = this->m_MatrixP * b; |
| 4797 |
this->ConjGrad( A , lmsolv, bproj , 0, 10000 ); |
| 4798 |
bproj = bproj - A * lmsolv; |
| 4799 |
bp = bproj * this->m_MatrixP; |
| 4800 |
for( unsigned int vv = 0; vv < bp.size(); vv++ ) |
| 4801 |
if ( colind % 2 == 0 & bp( vv ) > 0 ) bp( vv ) = 0; |
| 4802 |
else if ( colind % 2 > 0 & bp( vv ) < 0 ) bp( vv ) = 0; |
| 4803 |
} |
| 4804 |
|
| 4805 |
VectorType bnspace = this->m_MatrixP * this->m_Eigenvectors.get_column( colind ); |
| 4806 |
if ( ! this->m_Silent ) std::cout << " vecerr-a " << this->PearsonCorr( this->m_MatrixP * x_k , bnspace )<< " norm |
| 4807 |
" << ( this->m_MatrixP * x_k - bnspace ).two_norm() << std::endl; if ( ! this->m_Silent ) std::cout << " |
| 4808 |
vecerr-a-init " << this->PearsonCorr( this->m_MatrixP * variatesInit.get_column( colind ) , bnspace ) << " norm " << |
| 4809 |
( this->m_MatrixP * variatesInit.get_column( colind ) - bnspace ).two_norm() << std::endl; if ( ! this->m_Silent ) |
| 4810 |
std::cout << " col " << colind << " of " << n_vecs << " nspaceevecssz " << nspaceevecs.cols() << " mnv " << |
| 4811 |
x_k.min_value() << " mxv " << x_k.max_value() << " colind " << colind << std::endl; |
| 4812 |
|
| 4813 |
this->m_Indicator.set_size(this->m_MatrixP.cols()); |
| 4814 |
this->m_Indicator.fill(1); |
| 4815 |
if ( colind > 0) |
| 4816 |
{ |
| 4817 |
// zero out already used parts of matrix |
| 4818 |
for ( unsigned int mm = 0; mm < colind; mm++ ) |
| 4819 |
{ |
| 4820 |
VectorType u = this->m_VariatesP.get_column( mm ); |
| 4821 |
for ( unsigned int j=0; j< u.size(); j++) |
| 4822 |
if ( fabs(u(j)) >= this->m_Epsilon ) this->m_Indicator( j , j ) = 0; |
| 4823 |
} |
| 4824 |
// pmod = pmod * this->m_Indicator; |
| 4825 |
} |
| 4826 |
*/ |
| 4827 |
|
| 4828 |
|
| 4829 |
template <typename TInputImage, typename TRealType> |
| 4830 |
TRealType |
| 4831 |
antsSCCANObject<TInputImage, TRealType>::SparseArnoldiSVD_Other( |
| 4832 |
typename antsSCCANObject<TInputImage, TRealType>::MatrixType & A) |
| 4833 |
{ |
| 4834 |
if (itk::Math::abs(this->m_RowSparseness) <= 1.e-9) |
| 4835 |
return 0; |
| 4836 |
unsigned int maxrowtoorth = (unsigned int)(1.0 / itk::Math::abs(this->m_RowSparseness)) - 0; |
| 4837 |
unsigned int maxloop = 10; |
| 4838 |
unsigned int loop = 0; |
| 4839 |
double convcrit = 1; |
| 4840 |
MatrixType Asparse(A); |
| 4841 |
while (loop < maxloop && convcrit > 1.e-8) |
| 4842 |
{ |
| 4843 |
for (unsigned int k = 0; k < A.columns(); k++) |
| 4844 |
{ |
| 4845 |
VectorType pveck = Asparse.get_column(k); |
| 4846 |
if (!this->m_GetSmall) |
| 4847 |
pveck = (pveck * A) * A.transpose(); |
| 4848 |
if (this->m_GetSmall) |
| 4849 |
{ |
| 4850 |
RealType traceest = 0; |
| 4851 |
for (unsigned int kcol = 0; kcol < Asparse.columns(); kcol++) |
| 4852 |
traceest += inner_product(Asparse.get_column(kcol), Asparse.get_column(kcol)); |
| 4853 |
traceest = traceest + 1; |
| 4854 |
VectorType p1 = (pveck * A) * A.transpose(); |
| 4855 |
vnl_diag_matrix<TRealType> trid(this->m_MatrixP.cols(), traceest); |
| 4856 |
VectorType p2 = (pveck * trid); |
| 4857 |
pveck = p2 - p1; |
| 4858 |
} |
| 4859 |
pveck = pveck / pveck.two_norm(); |
| 4860 |
unsigned int startingm = 0; |
| 4861 |
if (k > maxrowtoorth) |
| 4862 |
startingm = k - maxrowtoorth; |
| 4863 |
for (unsigned int m = startingm; m < k; m++) |
| 4864 |
{ |
| 4865 |
VectorType orthagainst = Asparse.get_column(m); |
| 4866 |
pveck = this->Orthogonalize(pveck, orthagainst); |
| 4867 |
// this->ZeroProduct( pveck, orthagainst ); |
| 4868 |
} |
| 4869 |
pveck = pveck / pveck.two_norm(); |
| 4870 |
this->SparsifyOther(pveck, false); |
| 4871 |
Asparse.set_column(k, pveck); |
| 4872 |
} |
| 4873 |
loop++; |
| 4874 |
} |
| 4875 |
for (unsigned int k = 0; k < A.columns(); k++) |
| 4876 |
{ |
| 4877 |
A.set_column(k, Asparse.get_column(k)); |
| 4878 |
} |
| 4879 |
return 0; |
| 4880 |
} |
| 4881 |
|
| 4882 |
|
| 4883 |
template <typename TInputImage, typename TRealType> |
| 4884 |
TRealType |
| 4885 |
antsSCCANObject<TInputImage, TRealType>::BasicSVD() |
| 4886 |
{ |
| 4887 |
unsigned int n_vecs = this->m_MatrixP.rows() - 1; |
| 4888 |
|
| 4889 |
if ((this->m_MatrixP.cols() - 1) < n_vecs) |
| 4890 |
{ |
| 4891 |
n_vecs = this->m_MatrixP.cols() - 1; |
| 4892 |
} |
| 4893 |
this->m_CanonicalCorrelations.set_size(n_vecs); |
| 4894 |
this->m_CanonicalCorrelations.fill(0); |
| 4895 |
if (!this->m_Silent) |
| 4896 |
std::cout << " basic svd " << std::endl; |
| 4897 |
std::vector<RealType> vexlist; |
| 4898 |
this->m_MatrixP = this->NormalizeMatrix(this->m_OriginalMatrixP); |
| 4899 |
this->m_MatrixQ = this->m_MatrixP; |
| 4900 |
if (this->m_OriginalMatrixR.size() > 0) |
| 4901 |
{ |
| 4902 |
this->m_MatrixRRt = this->ProjectionMatrix(this->m_OriginalMatrixR); |
| 4903 |
this->m_MatrixP = this->m_MatrixP - (this->m_MatrixRRt * this->m_MatrixP); |
| 4904 |
} |
| 4905 |
this->m_VariatesP.set_size(this->m_MatrixP.cols(), n_vecs * 2); |
| 4906 |
|
| 4907 |
MatrixType init = this->GetCovMatEigenvectors(this->m_MatrixP); |
| 4908 |
if (!this->m_Silent) |
| 4909 |
std::cout << "got initial svd " << std::endl; |
| 4910 |
m_Eigenvectors.set_size(this->m_MatrixP.cols(), n_vecs * 2); |
| 4911 |
unsigned int svdct = 0; |
| 4912 |
RealType fnp = this->m_FractionNonZeroP; |
| 4913 |
for (unsigned int kk = 0; kk < n_vecs; kk++) |
| 4914 |
{ |
| 4915 |
if (kk < init.columns()) |
| 4916 |
{ |
| 4917 |
VectorType initvpos = init.get_column(kk) * this->m_MatrixP; |
| 4918 |
VectorType initvneg = init.get_column(kk) * this->m_MatrixP; |
| 4919 |
if (true) |
| 4920 |
{ |
| 4921 |
for (unsigned int vv = 0; vv < initvneg.size(); vv++) |
| 4922 |
{ |
| 4923 |
if (initvneg(vv) > 0) |
| 4924 |
{ |
| 4925 |
initvneg(vv) = 0; |
| 4926 |
} |
| 4927 |
else |
| 4928 |
{ |
| 4929 |
initvneg(vv) = fabs(initvneg(vv)); |
| 4930 |
} |
| 4931 |
if (initvpos(vv) < 0) |
| 4932 |
{ |
| 4933 |
initvpos(vv) = 0; |
| 4934 |
} |
| 4935 |
} |
| 4936 |
m_Eigenvectors.set_column(svdct, initvneg * (-1)); |
| 4937 |
if (fnp < 1 && false) |
| 4938 |
{ |
| 4939 |
if (this->m_KeepPositiveP) |
| 4940 |
{ |
| 4941 |
this->ConstantProbabilityThreshold(initvneg, fnp, this->m_KeepPositiveP); |
| 4942 |
} |
| 4943 |
else |
| 4944 |
{ |
| 4945 |
this->ReSoftThreshold(initvneg, fnp, !this->m_KeepPositiveP); |
| 4946 |
} |
| 4947 |
this->ClusterThresholdVariate(initvneg, this->m_MaskImageP, this->m_MinClusterSizeP); |
| 4948 |
} |
| 4949 |
this->m_VariatesP.set_column(svdct, initvneg * (-1)); |
| 4950 |
svdct++; |
| 4951 |
m_Eigenvectors.set_column(svdct, initvpos); |
| 4952 |
if (fnp < 1 && false) |
| 4953 |
{ |
| 4954 |
if (this->m_KeepPositiveP) |
| 4955 |
{ |
| 4956 |
this->ConstantProbabilityThreshold(initvpos, fnp, this->m_KeepPositiveP); |
| 4957 |
} |
| 4958 |
else |
| 4959 |
{ |
| 4960 |
this->ReSoftThreshold(initvpos, fnp, !this->m_KeepPositiveP); |
| 4961 |
} |
| 4962 |
this->ClusterThresholdVariate(initvpos, this->m_MaskImageP, this->m_MinClusterSizeP); |
| 4963 |
} |
| 4964 |
this->m_VariatesP.set_column(svdct, initvpos); |
| 4965 |
} // separate the eigenvectors into + / - |
| 4966 |
else |
| 4967 |
{ |
| 4968 |
this->m_VariatesP.set_column(svdct, initvpos); |
| 4969 |
} |
| 4970 |
svdct++; |
| 4971 |
} |
| 4972 |
} |
| 4973 |
// double ktrace = vnl_trace<double>( this->m_MatrixP * this->m_MatrixP.transpose() ); |
| 4974 |
// RealType vex = this->ComputeSPCAEigenvalues( this->m_VariatesP.cols(), this->m_Eigenvalues.sum(), true ); |
| 4975 |
// if ( ! this->m_Silent ) std::cout << "original-vex : " << this->m_Eigenvalues.sum() / ktrace << " sparse-vex: " |
| 4976 |
// << vex << std::endl; |
| 4977 |
return this->m_CanonicalCorrelations[0]; |
| 4978 |
} |
| 4979 |
|
| 4980 |
template <typename TInputImage, typename TRealType> |
| 4981 |
TRealType |
| 4982 |
antsSCCANObject<TInputImage, TRealType>::SparseArnoldiSVD(unsigned int n_vecs) |
| 4983 |
{ |
| 4984 |
this->m_CanonicalCorrelations.set_size(n_vecs); |
| 4985 |
this->m_CanonicalCorrelations.fill(0); |
| 4986 |
if (!this->m_Silent) |
| 4987 |
std::cout << " arnoldi sparse svd : cg " << std::endl; |
| 4988 |
std::vector<RealType> vexlist; |
| 4989 |
this->m_MatrixP = this->NormalizeMatrix(this->m_OriginalMatrixP); |
| 4990 |
this->m_MatrixQ = this->m_MatrixP; |
| 4991 |
if (this->m_OriginalMatrixR.size() > 0) |
| 4992 |
{ |
| 4993 |
this->m_MatrixRRt = this->ProjectionMatrix(this->m_OriginalMatrixR); |
| 4994 |
this->m_MatrixP = this->m_MatrixP - (this->m_MatrixRRt * this->m_MatrixP); |
| 4995 |
} |
| 4996 |
this->m_ClusterSizes.set_size(n_vecs); |
| 4997 |
this->m_ClusterSizes.fill(0); |
| 4998 |
double trace = 0; |
| 4999 |
for (unsigned int i = 0; i < this->m_MatrixP.cols(); i++) |
| 5000 |
{ |
| 5001 |
trace += inner_product(this->m_MatrixP.get_column(i), this->m_MatrixP.get_column(i)); |
| 5002 |
} |
| 5003 |
this->m_VariatesP.set_size(this->m_MatrixP.cols(), n_vecs); |
| 5004 |
VariateType myGradients; |
| 5005 |
this->m_SparseVariatesP.set_size(this->m_MatrixP.cols(), n_vecs); |
| 5006 |
this->m_SparseVariatesP.fill(0); |
| 5007 |
myGradients.set_size(this->m_MatrixP.cols(), n_vecs); |
| 5008 |
myGradients.fill(0); |
| 5009 |
MatrixType init = this->GetCovMatEigenvectors(this->m_MatrixP); |
| 5010 |
for (unsigned int kk = 0; kk < n_vecs; kk++) |
| 5011 |
{ |
| 5012 |
this->m_VariatesP.set_column(kk, this->InitializeV(this->m_MatrixP)); |
| 5013 |
if (kk < init.columns()) |
| 5014 |
{ |
| 5015 |
VectorType initv = init.get_column(kk) * this->m_MatrixP; |
| 5016 |
this->m_VariatesP.set_column(kk, initv); |
| 5017 |
this->m_SparseVariatesP.set_column(kk, initv); |
| 5018 |
} |
| 5019 |
} |
| 5020 |
const unsigned int maxloop = this->m_MaximumNumberOfIterations; |
| 5021 |
// Arnoldi Iteration SVD/SPCA |
| 5022 |
unsigned int loop = 0; |
| 5023 |
bool debug = false; |
| 5024 |
double convcrit = 1; |
| 5025 |
RealType fnp = 1; |
| 5026 |
while (loop < maxloop && convcrit > 1.e-8) |
| 5027 |
{ |
| 5028 |
fnp = this->m_FractionNonZeroP; |
| 5029 |
for (unsigned int k = 0; k < n_vecs; k++) |
| 5030 |
{ |
| 5031 |
VectorType pveck = this->m_SparseVariatesP.get_column(k); |
| 5032 |
if (fnp < 1) |
| 5033 |
{ |
| 5034 |
if (this->m_KeepPositiveP) |
| 5035 |
{ |
| 5036 |
this->ConstantProbabilityThreshold(pveck, fnp, this->m_KeepPositiveP); |
| 5037 |
} |
| 5038 |
else |
| 5039 |
{ |
| 5040 |
this->ReSoftThreshold(pveck, fnp, !this->m_KeepPositiveP); |
| 5041 |
} |
| 5042 |
this->ClusterThresholdVariate(pveck, this->m_MaskImageP, this->m_MinClusterSizeP); |
| 5043 |
} |
| 5044 |
// for ( unsigned int i=0; i<k; i++) pveck = this->Orthogonalize(pveck, this->m_VariatesP.get_column(i) ); |
| 5045 |
RealType alpha = 0.1; |
| 5046 |
VectorType resid = init.get_column(k) - this->m_MatrixP * pveck; |
| 5047 |
VectorType newsol = this->m_SparseVariatesP.get_column(k) + this->m_MatrixP.transpose() * resid * alpha; |
| 5048 |
this->m_SparseVariatesP.set_column(k, newsol); |
| 5049 |
if (fnp < 1) |
| 5050 |
{ |
| 5051 |
if (this->m_KeepPositiveP) |
| 5052 |
{ |
| 5053 |
this->ConstantProbabilityThreshold(newsol, fnp, this->m_KeepPositiveP); |
| 5054 |
} |
| 5055 |
else |
| 5056 |
{ |
| 5057 |
this->ReSoftThreshold(newsol, fnp, !this->m_KeepPositiveP); |
| 5058 |
} |
| 5059 |
this->ClusterThresholdVariate(newsol, this->m_MaskImageP, this->m_MinClusterSizeP); |
| 5060 |
} |
| 5061 |
this->m_VariatesP.set_column(k, newsol); |
| 5062 |
} // kloop |
| 5063 |
|
| 5064 |
this->m_VariatesQ = this->m_VariatesP; |
| 5065 |
if (debug) |
| 5066 |
{ |
| 5067 |
if (!this->m_Silent) |
| 5068 |
std::cout << " get evecs " << std::endl; |
| 5069 |
} |
| 5070 |
RealType vex = this->ComputeSPCAEigenvalues(n_vecs, trace, true); |
| 5071 |
vexlist.push_back(vex); |
| 5072 |
this->SortResults(n_vecs); |
| 5073 |
convcrit = (this->ComputeEnergySlope(vexlist, 5)); |
| 5074 |
if (!this->m_Silent) |
| 5075 |
std::cout << "Iteration: " << loop << " Eigenval_0: " << this->m_CanonicalCorrelations[0] |
| 5076 |
<< " Eigenval_1: " << this->m_CanonicalCorrelations[1] |
| 5077 |
<< " Eigenval_N: " << this->m_CanonicalCorrelations[n_vecs - 1] << " Sparseness: " << fnp |
| 5078 |
<< " convergence-criterion: " << convcrit << " vex " << vex << std::endl; |
| 5079 |
loop++; |
| 5080 |
if (debug) |
| 5081 |
{ |
| 5082 |
if (!this->m_Silent) |
| 5083 |
std::cout << "wloopdone" << std::endl; |
| 5084 |
} |
| 5085 |
} // opt-loop |
| 5086 |
// if ( ! this->m_Silent ) std::cout << " cluster-sizes " << this->m_ClusterSizes << std::endl; |
| 5087 |
for (unsigned int i = 0; i < vexlist.size(); i++) |
| 5088 |
{ |
| 5089 |
if (!this->m_Silent) |
| 5090 |
std::cout << vexlist[i] << ","; |
| 5091 |
} |
| 5092 |
if (!this->m_Silent) |
| 5093 |
std::cout << std::endl; |
| 5094 |
return fabs(this->m_CanonicalCorrelations[0]); |
| 5095 |
} |
| 5096 |
|
| 5097 |
template <typename TInputImage, typename TRealType> |
| 5098 |
TRealType |
| 5099 |
antsSCCANObject<TInputImage, TRealType>::ComputeSPCAEigenvalues(unsigned int n_vecs, TRealType trace, bool orth) |
| 5100 |
{ |
| 5101 |
this->m_CanonicalCorrelations.set_size(n_vecs); |
| 5102 |
double evalsum = 0; |
| 5103 |
// we have variates P = X , Q = X^T , Cov \approx \sum_i eval_i E_i^t E_i |
| 5104 |
// where E_i - eigenvector, eval_i eigenvalue |
| 5105 |
unsigned long mind = this->m_MatrixP.rows(); |
| 5106 |
if (mind > this->m_MatrixP.cols()) |
| 5107 |
{ |
| 5108 |
mind = this->m_MatrixP.cols(); |
| 5109 |
} |
| 5110 |
// we estimate variance explained by \sum_i eigenvalue_i / trace(A) ... |
| 5111 |
// shen and huang |
| 5112 |
// MatrixType kcovmat=this->VNLPseudoInverse( this->m_VariatesP.transpose()*this->m_VariatesP |
| 5113 |
// )*this->m_VariatesP.transpose(); |
| 5114 |
// kcovmat=(this->m_MatrixP*this->m_VariatesP)*kcovmat; |
| 5115 |
// double ktrace=vnl_trace<double>( kcovmat ); |
| 5116 |
// if ( ! this->m_Silent ) std::cout <<" ktr " << ktrace << std::endl; |
| 5117 |
for (unsigned int i = 0; i < n_vecs; i++) |
| 5118 |
{ |
| 5119 |
VectorType u = this->m_VariatesP.get_column(i); |
| 5120 |
// vnl_diag_matrix<TRealType> indicator(this->m_MatrixP.cols(),1); |
| 5121 |
// for ( unsigned int j=0; j< u.size(); j++) if ( fabs(u(j)) <= this->m_Epsilon ) indicator(j,j)=0; |
| 5122 |
VectorType proj = this->m_MatrixP.transpose() * (this->m_MatrixP * u); |
| 5123 |
double eigenvalue_i = 0; |
| 5124 |
double p2n = proj.two_norm(); |
| 5125 |
double oeig = p2n; |
| 5126 |
proj = this->m_MatrixP * u; |
| 5127 |
eigenvalue_i = oeig; |
| 5128 |
u = this->m_MatrixP * u; |
| 5129 |
double unorm = u.two_norm(); |
| 5130 |
if (unorm > 0) |
| 5131 |
{ |
| 5132 |
u = u / u.two_norm(); |
| 5133 |
} |
| 5134 |
// factor out the % of the eigenvector that is not orthogonal to higher ranked eigenvectors |
| 5135 |
for (unsigned int j = 0; j < i; j++) |
| 5136 |
{ |
| 5137 |
VectorType v = this->m_MatrixP * this->m_VariatesP.get_column(j); |
| 5138 |
if (v.two_norm() > 0) |
| 5139 |
{ |
| 5140 |
v = v / v.two_norm(); |
| 5141 |
} |
| 5142 |
double ip = inner_product(u, v); |
| 5143 |
ip = 1 - fabs(ip); |
| 5144 |
if (orth) |
| 5145 |
{ |
| 5146 |
eigenvalue_i *= ip; |
| 5147 |
} |
| 5148 |
} |
| 5149 |
if (itk::Math::FloatAlmostEqual(eigenvalue_i, 0.0)) |
| 5150 |
{ |
| 5151 |
this->m_VariatesP.set_column(i, this->InitializeV(this->m_MatrixP)); |
| 5152 |
} |
| 5153 |
evalsum += oeig; // eigenvalue_i; |
| 5154 |
this->m_CanonicalCorrelations[i] = eigenvalue_i; |
| 5155 |
} |
| 5156 |
evalsum /= trace; |
| 5157 |
// if ( ! this->m_Silent ) std::cout << this->m_CanonicalCorrelations << std::endl; |
| 5158 |
double vex = this->m_CanonicalCorrelations.sum() / trace; |
| 5159 |
// if ( ! this->m_Silent ) std::cout<<" adjusted variance explained " << vex << std::endl; |
| 5160 |
// if ( ! this->m_Silent ) std::cout<<" raw variance explained " << evalsum << std::endl; |
| 5161 |
/* |
| 5162 |
MatrixType mmm = this->m_MatrixP * this->m_VariatesP ; |
| 5163 |
MatrixType projmat = this->ProjectionMatrix( mmm ); |
| 5164 |
projmat = projmat * this->m_MatrixP ; |
| 5165 |
projmat = projmat * this->m_MatrixP.frobenius_norm() / projmat.frobenius_norm(); |
| 5166 |
MatrixType resid = this->m_MatrixP - projmat; |
| 5167 |
double ktrace=vnl_trace<double>( resid ); |
| 5168 |
// if we factored out everything then ktrace will be small |
| 5169 |
if ( ! this->m_Silent ) std::cout <<" ktr " << ktrace << " trace " << this->m_Eigenvalues.sum() << std::endl; |
| 5170 |
*/ |
| 5171 |
return vex; |
| 5172 |
/*****************************************/ |
| 5173 |
MatrixType ptemp(this->m_MatrixP); |
| 5174 |
VectorType d_i(n_vecs, 0); |
| 5175 |
for (unsigned int i = 0; i < n_vecs; i++) |
| 5176 |
{ |
| 5177 |
// find d_i |
| 5178 |
MatrixType m = outer_product(this->m_VariatesQ.get_column(i), this->m_VariatesP.get_column(i)); |
| 5179 |
// now find d_i to bring m close to ptemp |
| 5180 |
RealType a = ptemp.frobenius_norm(); |
| 5181 |
RealType b = m.frobenius_norm(); |
| 5182 |
// m=m*(a/b); |
| 5183 |
RealType hypod = inner_product(this->m_VariatesQ.get_column(i), this->m_MatrixP * this->m_VariatesP.get_column(i)); |
| 5184 |
if (!this->m_Silent) |
| 5185 |
std::cout << " hypod " << hypod << " a " << a << " b " << b << " a/b " << a / b << " " << std::endl; |
| 5186 |
ptemp = ptemp + m * a; |
| 5187 |
} |
| 5188 |
return 0; |
| 5189 |
} |
| 5190 |
|
| 5191 |
template <typename TInputImage, typename TRealType> |
| 5192 |
typename antsSCCANObject<TInputImage, TRealType>::MatrixType |
| 5193 |
antsSCCANObject<TInputImage, TRealType>::GetCovMatEigenvectors( |
| 5194 |
typename antsSCCANObject<TInputImage, TRealType>::MatrixType rin) |
| 5195 |
{ |
| 5196 |
double pinvTolerance = this->m_PinvTolerance; |
| 5197 |
MatrixType dd = this->NormalizeMatrix(rin); |
| 5198 |
MatrixType cov = dd * dd.transpose(); |
| 5199 |
|
| 5200 |
cov.set_identity(); |
| 5201 |
TRealType regularization = 0; |
| 5202 |
cov = cov * regularization + rin * rin.transpose(); |
| 5203 |
vnl_svd<RealType> eig(cov, pinvTolerance); |
| 5204 |
VectorType vec1 = eig.U().get_column(0); |
| 5205 |
VectorType vec2 = eig.V().get_column(0); |
| 5206 |
double evalsum = 0; |
| 5207 |
this->m_Eigenvalues.set_size(cov.rows()); |
| 5208 |
this->m_Eigenvalues.fill(0); |
| 5209 |
for (unsigned int i = 0; i < cov.rows(); i++) |
| 5210 |
{ |
| 5211 |
this->m_Eigenvalues[i] = eig.W(i, i); |
| 5212 |
evalsum += eig.W(i, i); |
| 5213 |
// if ( ! this->m_Silent ) std::cout <<" variance-explained-eval " << i << " = " << evalsum/trace*100 << |
| 5214 |
// std::endl; |
| 5215 |
} |
| 5216 |
// if ( ! this->m_Silent ) std::cout <<" W 1 " << eig.W(0,0) << " W 2 " << eig.W(1,1) << std::endl; |
| 5217 |
if (vec2.size() == rin.rows()) |
| 5218 |
{ |
| 5219 |
this->m_Eigenvectors = eig.V(); |
| 5220 |
return eig.V(); |
| 5221 |
} |
| 5222 |
else |
| 5223 |
{ |
| 5224 |
this->m_Eigenvectors = eig.U(); |
| 5225 |
return eig.U(); |
| 5226 |
} |
| 5227 |
} |
| 5228 |
|
| 5229 |
template <typename TInputImage, typename TRealType> |
| 5230 |
bool |
| 5231 |
antsSCCANObject<TInputImage, TRealType>::CCAUpdate(unsigned int n_vecs, |
| 5232 |
bool allowchange, |
| 5233 |
bool normbycov, |
| 5234 |
unsigned int k) |
| 5235 |
{ |
| 5236 |
// srand (time(nullptr)); |
| 5237 |
RealType gsteP = this->m_GradStepP; |
| 5238 |
RealType gsteQ = this->m_GradStepQ; |
| 5239 |
this->m_FractionNonZeroP = this->m_SparsenessP(k); |
| 5240 |
this->m_FractionNonZeroQ = this->m_SparsenessQ(k); |
| 5241 |
VectorType pprior; |
| 5242 |
VectorType qprior; |
| 5243 |
if ((this->m_MatrixPriorROI.rows() > 0) && (this->m_MatrixPriorROI.cols() > 0)) |
| 5244 |
{ |
| 5245 |
pprior = this->m_MatrixPriorROI.transpose().get_column(k); |
| 5246 |
pprior = pprior / pprior.two_norm(); |
| 5247 |
} |
| 5248 |
if ((this->m_MatrixPriorROI2.rows() > 0) && (this->m_MatrixPriorROI2.cols() > 0)) |
| 5249 |
{ |
| 5250 |
qprior = this->m_MatrixPriorROI2.transpose().get_column(k); |
| 5251 |
qprior = qprior / qprior.two_norm(); |
| 5252 |
} |
| 5253 |
|
| 5254 |
this->m_Debug = false; |
| 5255 |
bool secondSO = true; |
| 5256 |
// for( unsigned int k = 0; k < n_vecs; k++ ) |
| 5257 |
{ |
| 5258 |
// residualize against previous vectors |
| 5259 |
// 0 - vox orth + resid, 1 - only vox orth , 2 - only resid |
| 5260 |
if ((k > 0) && (this->m_Covering == 0 || this->m_Covering == 2)) |
| 5261 |
{ |
| 5262 |
VectorType temp = this->m_MatrixP * this->m_VariatesP.get_column(k - 1); |
| 5263 |
this->SparsifyOther(temp); |
| 5264 |
if (k < (this->m_MatrixP.columns() - 1)) |
| 5265 |
this->m_MatrixP = this->OrthogonalizeMatrix(this->m_MatrixP, temp); |
| 5266 |
temp = this->m_MatrixQ * this->m_VariatesQ.get_column(k - 1); |
| 5267 |
this->SparsifyOther(temp); |
| 5268 |
if (n_vecs < this->m_MatrixQ.columns()) |
| 5269 |
this->m_MatrixQ = this->OrthogonalizeMatrix(this->m_MatrixQ, temp); |
| 5270 |
this->m_MatrixP = this->NormalizeMatrix(this->m_MatrixP, false); |
| 5271 |
this->m_MatrixQ = this->NormalizeMatrix(this->m_MatrixQ, false); |
| 5272 |
} |
| 5273 |
VectorType ptemp = this->m_VariatesP.get_column(k); |
| 5274 |
VectorType qtemp = this->m_VariatesQ.get_column(k); |
| 5275 |
VectorType pveck = this->m_MatrixQ * qtemp; |
| 5276 |
if (secondSO) |
| 5277 |
this->SparsifyOther(pveck); // zeromatch |
| 5278 |
VectorType qveck = this->m_MatrixP * ptemp; |
| 5279 |
if (secondSO) |
| 5280 |
this->SparsifyOther(qveck); // zeromatch |
| 5281 |
// get list of all zeroes |
| 5282 |
std::vector<TRealType> zeromatch(qveck.size(), 0); |
| 5283 |
// unsigned int zct = 0; |
| 5284 |
for (unsigned int zm = 0; zm < qveck.size(); zm++) |
| 5285 |
{ |
| 5286 |
if ((this->Close2Zero(pveck(zm)) || this->Close2Zero(qveck(zm))) && (false)) |
| 5287 |
{ |
| 5288 |
// zct++; |
| 5289 |
zeromatch[zm] = 1; |
| 5290 |
pveck(zm) = 0; |
| 5291 |
qveck(zm) = 0; |
| 5292 |
} |
| 5293 |
} |
| 5294 |
/** the gradient of ( x X , y Y ) * ( x X , x X )^{-1} * ( y Y , y Y )^{-1} |
| 5295 |
* where we constrain ( x X , x X ) = ( y Y , y Y ) = 1 |
| 5296 |
*/ |
| 5297 |
RealType ccafactor = inner_product(pveck, qveck) * 0.5; |
| 5298 |
pveck = pveck * this->m_MatrixP; |
| 5299 |
VectorType pproj = (this->m_MatrixP * ptemp); |
| 5300 |
if (secondSO) |
| 5301 |
this->SparsifyOther(pproj); // zeromatch |
| 5302 |
// for ( unsigned int zm = 0; zm < qveck.size(); zm++ ) |
| 5303 |
// if ( this->Close2Zero( zeromatch[ zm ] - 1 ) ) pproj( zm ) = 0; |
| 5304 |
pveck = pveck - this->m_MatrixP.transpose() * pproj * ccafactor; |
| 5305 |
qveck = qveck * this->m_MatrixQ; |
| 5306 |
VectorType qproj = (this->m_MatrixQ * qtemp); |
| 5307 |
if (secondSO) |
| 5308 |
this->SparsifyOther(qproj); // zeromatch |
| 5309 |
// for ( unsigned int zm = 0; zm < qveck.size(); zm++ ) |
| 5310 |
// if ( this->Close2Zero( zeromatch[ zm ] - 1 ) ) qproj( zm ) = 0; |
| 5311 |
qveck = qveck - this->m_MatrixQ.transpose() * (qproj)*ccafactor; |
| 5312 |
RealType sclp = (static_cast<RealType>(pveck.size()) * this->m_FractionNonZeroP); |
| 5313 |
RealType sclq = (static_cast<RealType>(qveck.size()) * this->m_FractionNonZeroQ); |
| 5314 |
bool genomics = false; |
| 5315 |
if (genomics) |
| 5316 |
{ |
| 5317 |
VectorType y = this->m_MatrixP * ptemp; |
| 5318 |
this->LASSO_alg(this->m_MatrixQ, y, qveck, 1.e-3, 2); |
| 5319 |
} |
| 5320 |
pveck = ptemp + pveck * (gsteP / sclp); |
| 5321 |
qveck = qtemp + qveck * (gsteQ / sclq); |
| 5322 |
if (this->m_Covering != 2) // 0 - vox orth + resid, 1 - only vox orth , 2 - only resid |
| 5323 |
for (unsigned int j = 0; j < k; j++) |
| 5324 |
{ |
| 5325 |
if (j != k) |
| 5326 |
{ |
| 5327 |
VectorType qj = this->m_VariatesP.get_column(j); |
| 5328 |
pproj = (this->m_MatrixP * pveck); |
| 5329 |
if (secondSO) |
| 5330 |
this->SparsifyOther(pproj); |
| 5331 |
pveck = this->Orthogonalize(pveck / (pproj).two_norm(), qj); |
| 5332 |
// pveck = this->Orthogonalize( pveck, qj, &this->m_MatrixP, &this->m_MatrixP); |
| 5333 |
|
| 5334 |
// if ( this->m_Covering ) this->ZeroProduct( pveck, qj ); |
| 5335 |
qj = this->m_VariatesQ.get_column(j); |
| 5336 |
qproj = (this->m_MatrixQ * qveck); |
| 5337 |
if (secondSO) |
| 5338 |
this->SparsifyOther(qproj); |
| 5339 |
qveck = this->Orthogonalize(qveck / (qproj).two_norm(), qj); |
| 5340 |
// qveck = this->Orthogonalize( qveck, qj, &this->m_MatrixQ, &this->m_MatrixQ); |
| 5341 |
// if ( this->m_Covering ) this->ZeroProduct( qveck, qj ); |
| 5342 |
} |
| 5343 |
} |
| 5344 |
if ((this->m_UseLongitudinalFormulation > 1.e-9) && (pveck.size() == qveck.size())) |
| 5345 |
{ |
| 5346 |
VectorType lpveck = pveck + (qveck - pveck) * this->m_UseLongitudinalFormulation; |
| 5347 |
VectorType lqveck = qveck + (pveck - qveck) * this->m_UseLongitudinalFormulation; |
| 5348 |
pveck = lpveck; |
| 5349 |
qveck = lqveck; |
| 5350 |
} |
| 5351 |
if (pprior.size() == pveck.size()) |
| 5352 |
{ |
| 5353 |
pveck = pveck / pveck.two_norm(); |
| 5354 |
pveck = pveck * (1.0 - this->m_PriorWeight) + pprior * this->m_PriorWeight; |
| 5355 |
} |
| 5356 |
if (qprior.size() == qveck.size()) |
| 5357 |
{ |
| 5358 |
qveck = qveck / qveck.two_norm(); |
| 5359 |
qveck = qveck * (1.0 - this->m_PriorWeight) + qprior * this->m_PriorWeight; |
| 5360 |
} |
| 5361 |
this->SparsifyP(pveck); |
| 5362 |
this->SparsifyQ(qveck); |
| 5363 |
if (this->m_UseLongitudinalFormulation > 1.e-9) |
| 5364 |
{ |
| 5365 |
if (k == 0 && (pveck.size() != qveck.size())) |
| 5366 |
{ |
| •5367 |
std::cout << "Cannot Use Longitudinal formulation if input matrix sizes don't match. "; |
| 5368 |
} |
| 5369 |
else if (k == 0) |
| 5370 |
{ |
| 5371 |
if (!this->m_Silent) |
| 5372 |
std::cout << "Longitudinal=>p=q: " << (pveck - qveck).one_norm(); |
| 5373 |
} |
| 5374 |
} |
| 5375 |
if (n_vecs == 0) |
| 5376 |
{ |
| 5377 |
RealType mup = inner_product(this->m_MatrixP * ptemp, this->m_MatrixP * ptemp) / ptemp.two_norm(); |
| 5378 |
RealType muq = inner_product(this->m_MatrixQ * qtemp, this->m_MatrixQ * qtemp) / qtemp.two_norm(); |
| 5379 |
if (!this->m_Silent) |
| 5380 |
std::cout << " USE-IHT FORMULATION " << mup << " " << muq << std::endl; |
| 5381 |
this->IHTRegression(this->m_MatrixP, ptemp, pveck, 0, 1, mup, true, false); |
| 5382 |
pveck = ptemp; |
| 5383 |
this->IHTRegression(this->m_MatrixQ, qtemp, qveck, 0, 1, muq, false, false); |
| 5384 |
qveck = qtemp; |
| 5385 |
} |
| 5386 |
|
| 5387 |
/* randomly drop-out some entries in pveck |
| 5388 |
for ( unsigned int i = 0; i < pveck.size(); i++ ) |
| 5389 |
if ( ( rand() % 100 ) < 2 ) pveck[i] = 0; |
| 5390 |
for ( unsigned int i = 0; i < qveck.size(); i++ ) |
| 5391 |
if ( ( rand() % 100 ) < 2 ) qveck[i] = 0; */ |
| 5392 |
|
| 5393 |
// test 4 cases of updates |
| 5394 |
pproj = this->m_MatrixP * ptemp; |
| 5395 |
if (secondSO) |
| 5396 |
this->SparsifyOther(pproj); |
| 5397 |
VectorType pproj2 = this->m_MatrixP * pveck; |
| 5398 |
if (secondSO) |
| 5399 |
this->SparsifyOther(pproj2); |
| 5400 |
qproj = this->m_MatrixQ * qtemp; |
| 5401 |
if (secondSO) |
| 5402 |
this->SparsifyOther(qproj); |
| 5403 |
VectorType qproj2 = this->m_MatrixQ * qveck; |
| 5404 |
if (secondSO) |
| 5405 |
this->SparsifyOther(qproj2); |
| 5406 |
RealType corr0 = this->PearsonCorr(pproj, qproj); |
| 5407 |
RealType corr1 = this->PearsonCorr(pproj2, qproj2); |
| 5408 |
RealType corr2 = this->PearsonCorr(pproj2, qproj); |
| 5409 |
RealType corr3 = this->PearsonCorr(pproj, qproj2); |
| 5410 |
this->m_Debug = true; |
| 5411 |
if (corr1 > corr0) |
| 5412 |
{ |
| 5413 |
this->m_VariatesP.set_column(k, pveck); |
| 5414 |
this->m_VariatesQ.set_column(k, qveck); |
| 5415 |
if (this->m_Debug) |
| 5416 |
{ |
| 5417 |
if (!this->m_Silent) |
| 5418 |
std::cout << " corr1 " << corr1 << " v " << corr2 << std::endl; |
| 5419 |
} |
| 5420 |
} |
| 5421 |
else if ((corr2 > corr0) && (corr2 > corr3)) |
| 5422 |
{ |
| 5423 |
this->m_VariatesP.set_column(k, pveck); |
| 5424 |
this->m_GradStepQ *= 0.5; |
| 5425 |
if (this->m_Debug) |
| 5426 |
{ |
| 5427 |
if (!this->m_Silent) |
| 5428 |
std::cout << " corr2 " << corr2 << " v " << corr1 << std::endl; |
| 5429 |
} |
| 5430 |
} |
| 5431 |
else if (corr3 > corr0) |
| 5432 |
{ |
| 5433 |
this->m_VariatesQ.set_column(k, qveck); |
| 5434 |
this->m_GradStepP *= 0.5; |
| 5435 |
if (this->m_Debug) |
| 5436 |
{ |
| 5437 |
if (!this->m_Silent) |
| 5438 |
std::cout << " corr3 " << corr3 << " v " << corr0 << std::endl; |
| 5439 |
} |
| 5440 |
} |
| 5441 |
else if (allowchange) |
| 5442 |
{ |
| 5443 |
this->m_GradStepP *= 0.5; |
| 5444 |
this->m_GradStepQ *= 0.5; |
| 5445 |
if (this->m_Debug) |
| 5446 |
{ |
| 5447 |
if (!this->m_Silent) |
| 5448 |
std::cout << " corr0 " << corr0 << " v " << corr1 << " NewGrad " << gsteP << " & " << gsteQ << std::endl; |
| 5449 |
} |
| 5450 |
} |
| 5451 |
this->m_Debug = false; |
| 5452 |
if (normbycov) |
| 5453 |
this->NormalizeWeightsByCovariance(k, 0, 0); |
| 5454 |
else |
| 5455 |
this->NormalizeWeights(k); |
| 5456 |
VectorType proj1 = this->m_MatrixP * this->m_VariatesP.get_column(k); |
| 5457 |
if (secondSO) |
| 5458 |
this->SparsifyOther(proj1); |
| 5459 |
VectorType proj2 = this->m_MatrixQ * this->m_VariatesQ.get_column(k); |
| 5460 |
if (secondSO) |
| 5461 |
this->SparsifyOther(proj2); |
| 5462 |
this->m_CanonicalCorrelations[k] = this->PearsonCorr(proj1, proj2); |
| 5463 |
} |
| 5464 |
// this->SortResults( n_vecs ); |
| 5465 |
return !itk::Math::FloatAlmostEqual(this->m_CanonicalCorrelations.mean(), 0.0); |
| 5466 |
} |
| 5467 |
|
| 5468 |
template <typename TInputImage, typename TRealType> |
| 5469 |
TRealType |
| 5470 |
antsSCCANObject<TInputImage, TRealType>::InitializeSCCA_simple(unsigned int n_vecs) |
| 5471 |
{ |
| 5472 |
/** get the row mean for each matrix , then correlate the other matrix with that, then sparsify |
| 5473 |
* for 2nd,3rd,etc evecs, orthogonalize initial vector against sparsified. |
| 5474 |
*/ |
| 5475 |
// this->InitializeSCCA( n_vecs , 20 );// arbitrary initialization |
| 5476 |
RealType totalcorr = 0; |
| 5477 |
unsigned int pmax = this->m_MatrixP.rows(); |
| 5478 |
unsigned int qmax = this->m_MatrixQ.rows(); |
| 5479 |
VectorType prowmean(pmax, 0); |
| 5480 |
for (unsigned long i = 0; i < pmax; i++) |
| 5481 |
{ |
| 5482 |
prowmean(i) = this->m_MatrixP.get_row(i).mean(); |
| 5483 |
} |
| 5484 |
VectorType qrowmean(qmax, 0); |
| 5485 |
for (unsigned long i = 0; i < qmax; i++) |
| 5486 |
{ |
| 5487 |
qrowmean(i) = this->m_MatrixQ.get_row(i).mean(); |
| 5488 |
} |
| 5489 |
if (prowmean.two_norm() > this->m_Epsilon) |
| 5490 |
prowmean = prowmean / prowmean.two_norm(); |
| 5491 |
if (qrowmean.two_norm() > this->m_Epsilon) |
| 5492 |
qrowmean = qrowmean / qrowmean.two_norm(); |
| 5493 |
this->SparsifyOther(prowmean); |
| 5494 |
this->SparsifyOther(qrowmean); |
| 5495 |
VectorType ipvec = (prowmean) * this->m_MatrixP; |
| 5496 |
VectorType iqvec = (qrowmean) * this->m_MatrixQ; |
| 5497 |
for (unsigned int kk = 0; kk < n_vecs; kk++) |
| 5498 |
{ |
| 5499 |
if ((kk > 0) && (this->m_Covering == 0 || this->m_Covering == 2)) |
| 5500 |
{ |
| 5501 |
VectorType temp = this->m_MatrixP * this->m_VariatesP.get_column(kk - 1); |
| 5502 |
this->SparsifyOther(temp); |
| 5503 |
if (n_vecs < this->m_MatrixP.columns()) |
| 5504 |
this->m_MatrixP = this->OrthogonalizeMatrix(this->m_MatrixP, temp); |
| 5505 |
temp = this->m_MatrixQ * this->m_VariatesQ.get_column(kk - 1); |
| 5506 |
this->SparsifyOther(temp); |
| 5507 |
if (n_vecs < this->m_MatrixQ.columns()) |
| 5508 |
this->m_MatrixQ = this->OrthogonalizeMatrix(this->m_MatrixQ, temp); |
| 5509 |
} |
| 5510 |
VectorType qvec = (this->m_MatrixP * ipvec); |
| 5511 |
this->SparsifyOther(qvec); |
| 5512 |
qvec = qvec * this->m_MatrixQ; |
| 5513 |
if (qvec.two_norm() > this->m_Epsilon) |
| 5514 |
qvec = qvec / qvec.two_norm(); |
| 5515 |
VectorType vec = (this->m_MatrixQ * qvec); |
| 5516 |
this->SparsifyOther(vec); |
| 5517 |
vec = vec * this->m_MatrixP; |
| 5518 |
if (vec.two_norm() > this->m_Epsilon) |
| 5519 |
vec = vec / vec.two_norm(); |
| 5520 |
VectorType vec2 = (this->m_MatrixQ * iqvec); |
| 5521 |
this->SparsifyOther(vec2); |
| 5522 |
vec2 = vec2 * this->m_MatrixP; |
| 5523 |
if (vec2.two_norm() > this->m_Epsilon) |
| 5524 |
vec2 = vec2 / vec2.two_norm(); |
| 5525 |
VectorType qvec2 = (this->m_MatrixP * vec2); |
| 5526 |
this->SparsifyOther(qvec2); |
| 5527 |
qvec2 = qvec2 * this->m_MatrixQ; |
| 5528 |
if (qvec2.two_norm() > this->m_Epsilon) |
| 5529 |
qvec2 = qvec2 / qvec2.two_norm(); |
| 5530 |
if (itk::Math::abs(this->PearsonCorr(this->m_MatrixP * vec2, this->m_MatrixQ * qvec2)) > |
| 5531 |
itk::Math::abs(this->PearsonCorr(this->m_MatrixP * vec, this->m_MatrixQ * qvec))) |
| 5532 |
{ |
| 5533 |
vec = vec2; |
| 5534 |
qvec = qvec2; |
| 5535 |
} |
| 5536 |
// if ( ! this->m_Silent ) std::cout << " vec-y " << vec.two_norm() << std::endl; |
| 5537 |
// if ( ! this->m_Silent ) std::cout << "qvec-y " << qvec.two_norm() << std::endl; |
| 5538 |
for (unsigned int j = 0; j < kk; j++) |
| 5539 |
{ |
| 5540 |
VectorType qj = this->m_VariatesP.get_column(j); |
| 5541 |
if (j < (this->m_MatrixP.cols() - 1)) |
| 5542 |
vec = this->Orthogonalize(vec, qj); |
| 5543 |
qj = this->m_VariatesQ.get_column(j); |
| 5544 |
if (n_vecs < this->m_MatrixQ.columns()) |
| 5545 |
qvec = this->Orthogonalize(qvec, qj); |
| 5546 |
} |
| 5547 |
// if ( ! this->m_Silent ) std::cout << " vec-z " << vec.two_norm() << std::endl; |
| 5548 |
// if ( ! this->m_Silent ) std::cout << "qvec-z " << qvec.two_norm() << std::endl; |
| 5549 |
vec = this->SpatiallySmoothVector(vec, this->m_MaskImageP); |
| 5550 |
qvec = this->SpatiallySmoothVector(qvec, this->m_MaskImageQ); |
| 5551 |
if (qvec.two_norm() > 0) |
| 5552 |
qvec = qvec / qvec.two_norm(); |
| 5553 |
if (vec.two_norm() > 0) |
| 5554 |
vec = vec / vec.two_norm(); |
| 5555 |
if (this->m_UseLongitudinalFormulation > 1.e-9) |
| 5556 |
{ |
| 5557 |
vec = (vec + qvec) * 0.5; |
| 5558 |
qvec = vec; |
| 5559 |
} |
| 5560 |
this->SparsifyP(vec); |
| 5561 |
this->SparsifyQ(qvec); |
| 5562 |
RealType locor = itk::Math::abs(this->PearsonCorr(this->m_MatrixP * vec, this->m_MatrixQ * qvec)); |
| 5563 |
if (std::isnan(qvec.two_norm())) |
| 5564 |
{ |
| 5565 |
qvec = this->m_VariatesQ.get_column(kk); |
| 5566 |
locor = itk::Math::abs(this->PearsonCorr(this->m_MatrixP * vec, this->m_MatrixQ * qvec)); |
| 5567 |
} |
| 5568 |
if (std::isnan(vec.two_norm())) |
| 5569 |
{ |
| 5570 |
vec = this->m_VariatesP.get_column(kk); |
| 5571 |
locor = itk::Math::abs(this->PearsonCorr(this->m_MatrixP * vec, this->m_MatrixQ * qvec)); |
| 5572 |
} |
| 5573 |
this->m_VariatesP.set_column(kk, vec); |
| 5574 |
this->m_VariatesQ.set_column(kk, qvec); |
| 5575 |
this->NormalizeWeightsByCovariance(kk, 1, 1); |
| 5576 |
totalcorr += locor; |
| 5577 |
} |
| 5578 |
return totalcorr; |
| 5579 |
} |
| 5580 |
|
| 5581 |
|
| 5582 |
template <typename TInputImage, typename TRealType> |
| 5583 |
TRealType |
| 5584 |
antsSCCANObject<TInputImage, TRealType>::InitializeSCCA(unsigned int n_vecs, unsigned int seeder) |
| 5585 |
{ |
| 5586 |
RealType totalcorr = 0; |
| 5587 |
|
| 5588 |
for (unsigned int kk = 0; kk < n_vecs; kk++) |
| 5589 |
{ |
| 5590 |
unsigned int theseed = (kk + 1) * seeder; |
| 5591 |
VectorType vec = this->InitializeV(this->m_MatrixP, theseed); |
| 5592 |
vec = this->m_MatrixP * vec; |
| 5593 |
this->SparsifyOther(vec); |
| 5594 |
vec = vec * this->m_MatrixP; |
| 5595 |
vec = vec / vec.two_norm(); |
| 5596 |
VectorType qvec = (this->m_MatrixP * vec); |
| 5597 |
this->SparsifyOther(qvec); |
| 5598 |
qvec = qvec * this->m_MatrixQ; |
| 5599 |
qvec = qvec / qvec.two_norm(); |
| 5600 |
vec = (this->m_MatrixQ * qvec) * this->m_MatrixP; |
| 5601 |
vec = vec / vec.two_norm(); |
| 5602 |
for (unsigned int j = 0; j < kk; j++) |
| 5603 |
{ |
| 5604 |
VectorType qj = this->m_VariatesP.get_column(j); |
| 5605 |
if (j < (this->m_MatrixP.columns() - 1)) |
| 5606 |
vec = this->Orthogonalize(vec, qj); |
| 5607 |
qj = this->m_VariatesQ.get_column(j); |
| 5608 |
if (n_vecs < this->m_MatrixQ.columns()) |
| 5609 |
qvec = this->Orthogonalize(qvec, qj); |
| 5610 |
} |
| 5611 |
vec = this->SpatiallySmoothVector(vec, this->m_MaskImageP); |
| 5612 |
qvec = this->SpatiallySmoothVector(qvec, this->m_MaskImageQ); |
| 5613 |
qvec = qvec / qvec.two_norm(); |
| 5614 |
vec = vec / vec.two_norm(); |
| 5615 |
this->SparsifyP(vec); |
| 5616 |
this->SparsifyQ(qvec); |
| 5617 |
this->m_VariatesP.set_column(kk, vec); |
| 5618 |
this->m_VariatesQ.set_column(kk, qvec); |
| 5619 |
this->NormalizeWeights(kk); |
| 5620 |
totalcorr += itk::Math::abs(this->PearsonCorr(this->m_MatrixP * vec, this->m_MatrixQ * qvec)); |
| 5621 |
} |
| 5622 |
return totalcorr * 0.5; |
| 5623 |
// this->CCAUpdate( n_vecs , false ); |
| 5624 |
return this->m_CanonicalCorrelations.sum(); |
| 5625 |
} |
| 5626 |
|
| 5627 |
|
| 5628 |
template <typename TInputImage, typename TRealType> |
| 5629 |
TRealType |
| 5630 |
antsSCCANObject<TInputImage, TRealType>::SparsePartialArnoldiCCA(unsigned int n_vecs_in) |
| 5631 |
{ |
| 5632 |
RealType basegradstep = this->m_GradStep; |
| 5633 |
this->m_Debug = false; |
| 5634 |
unsigned int n_vecs = n_vecs_in; |
| 5635 |
if (n_vecs < 1) |
| 5636 |
{ |
| 5637 |
n_vecs = 1; |
| 5638 |
} |
| 5639 |
this->m_CanonicalCorrelations.set_size(n_vecs); |
| 5640 |
this->m_CanonicalCorrelations.fill(0); |
| 5641 |
this->m_SparsenessP.set_size(n_vecs); |
| 5642 |
this->m_SparsenessP.fill(this->m_FractionNonZeroP); |
| 5643 |
this->m_SparsenessQ.set_size(n_vecs); |
| 5644 |
this->m_SparsenessQ.fill(this->m_FractionNonZeroQ); |
| 5645 |
if (!m_Silent) |
| 5646 |
{ |
| 5647 |
if (!this->m_Silent) |
| 5648 |
std::cout << " arnoldi sparse partial cca : L1?" << this->m_UseL1 << " GradStep " << this->m_GradStep << " p+ " |
| 5649 |
<< this->GetKeepPositiveP() << " q+ " << this->GetKeepPositiveQ() << " covering " << this->m_Covering |
| 5650 |
<< std::endl; |
| 5651 |
} |
| 5652 |
this->m_MatrixP = this->NormalizeMatrix(this->m_OriginalMatrixP, false); |
| 5653 |
this->m_MatrixQ = this->NormalizeMatrix(this->m_OriginalMatrixQ, false); |
| 5654 |
this->m_MatrixR = this->NormalizeMatrix(this->m_OriginalMatrixR, false); |
| 5655 |
|
| 5656 |
if (this->m_OriginalMatrixR.size() > 0) |
| 5657 |
{ |
| 5658 |
if (!this->m_Silent) |
| 5659 |
std::cout << "Partialing-Pre : -P-Norm " << this->m_MatrixP.frobenius_norm() << " -Q-Norm " |
| 5660 |
<< this->m_MatrixQ.frobenius_norm() << std::endl; |
| 5661 |
this->m_MatrixRRt = this->ProjectionMatrix(this->m_OriginalMatrixR); |
| 5662 |
if (this->m_SCCANFormulation == PminusRQ || this->m_SCCANFormulation == PminusRQminusR) |
| 5663 |
{ |
| 5664 |
if (!this->m_Silent) |
| 5665 |
std::cout << " Subtract R from P " << std::endl; |
| 5666 |
|
| 5667 |
this->m_MatrixP = this->m_MatrixP - (this->m_MatrixRRt * this->m_MatrixP); |
| 5668 |
if (!this->m_Silent) |
| 5669 |
std::cout << "Partialing-Post : -P-Norm " << this->m_MatrixP.frobenius_norm() << std::endl; |
| 5670 |
} |
| 5671 |
if (this->m_SCCANFormulation == PQminusR || this->m_SCCANFormulation == PminusRQminusR) |
| 5672 |
{ |
| 5673 |
if (!this->m_Silent) |
| 5674 |
std::cout << " Subtract R from Q " << std::endl; |
| 5675 |
this->m_MatrixQ = this->m_MatrixQ - this->m_MatrixRRt * this->m_MatrixQ; |
| 5676 |
if (!this->m_Silent) |
| 5677 |
std::cout << " -Q-Norm " << this->m_MatrixQ.frobenius_norm() << std::endl; |
| 5678 |
} |
| 5679 |
} |
| 5680 |
|
| 5681 |
this->m_VariatesP.set_size(this->m_MatrixP.cols(), n_vecs); |
| 5682 |
this->m_VariatesQ.set_size(this->m_MatrixQ.cols(), n_vecs); |
| 5683 |
RealType initReturn = this->InitializeSCCA_simple(n_vecs); |
| 5684 |
if (!m_Silent) |
| 5685 |
{ |
| 5686 |
if (!this->m_Silent) |
| 5687 |
std::cout << "Initialization: " << initReturn << std::endl; |
| 5688 |
} |
| 5689 |
/* |
| 5690 |
RealType bestcorr = 0; |
| 5691 |
RealType totalcorr = 0; |
| 5692 |
int bestseed = -1; |
| 5693 |
for( unsigned int seeder = 0; seeder < 100; seeder++ ) |
| 5694 |
{ |
| 5695 |
totalcorr = this->InitializeSCCA( n_vecs, seeder ); |
| 5696 |
if( totalcorr > bestcorr ) |
| 5697 |
{ |
| 5698 |
bestseed = seeder; |
| 5699 |
bestcorr = totalcorr; |
| 5700 |
if ( ! this->m_Silent ) std::cout << " seed " << seeder << " corr " << bestcorr << std::endl; |
| 5701 |
} |
| 5702 |
} |
| 5703 |
if( this->m_Debug ) |
| 5704 |
{ |
| 5705 |
if ( ! this->m_Silent ) std::cout << " Best initial corr " << bestcorr << std::endl; |
| 5706 |
} |
| 5707 |
if ( bestseed >= 0 ) totalcorr = this->InitializeSCCA( n_vecs, bestseed ); |
| 5708 |
if ( ! this->m_Silent ) std::cout << " seed " << bestseed << " corr " << totalcorr << std::endl; |
| 5709 |
*/ |
| 5710 |
|
| 5711 |
bool imagedriver = false; |
| 5712 |
if ((this->m_OriginalMatrixPriorROI.rows() > 0) && (this->m_OriginalMatrixPriorROI.cols() > 0)) |
| 5713 |
{ |
| 5714 |
imagedriver = true; |
| 5715 |
n_vecs = this->m_OriginalMatrixPriorROI.rows(); |
| 5716 |
this->m_VariatesP = this->m_MatrixPriorROI.transpose(); |
| 5717 |
for (unsigned int i = 0; i < n_vecs; i++) |
| 5718 |
{ |
| 5719 |
VectorType vec = this->m_VariatesP.get_column(i); |
| 5720 |
RealType spar = this->CountNonZero(vec); |
| 5721 |
if (fabs(this->m_FractionNonZeroP) < 1.e-11) // learn from prior |
| 5722 |
this->m_SparsenessP(i) = spar; |
| 5723 |
this->SparsifyP(vec); |
| 5724 |
vec = this->SpatiallySmoothVector(vec, this->m_MaskImageP); |
| 5725 |
vec = vec / vec.two_norm(); |
| 5726 |
this->m_VariatesP.set_column(i, vec); |
| 5727 |
} |
| 5728 |
} |
| 5729 |
if ((this->m_MatrixPriorROI2.rows() > 0) && (this->m_MatrixPriorROI2.cols() > 0)) |
| 5730 |
{ |
| 5731 |
imagedriver = true; |
| 5732 |
this->m_VariatesQ = this->m_MatrixPriorROI2.transpose(); |
| 5733 |
for (unsigned int i = 0; i < n_vecs; i++) |
| 5734 |
{ |
| 5735 |
VectorType vec = this->m_VariatesQ.get_column(i); |
| 5736 |
RealType spar = this->CountNonZero(vec); |
| 5737 |
if (fabs(this->m_FractionNonZeroQ) < 1.e-11) |
| 5738 |
this->m_SparsenessQ(i) = spar; |
| 5739 |
this->SparsifyQ(vec); |
| 5740 |
vec = this->SpatiallySmoothVector(vec, this->m_MaskImageQ); |
| 5741 |
vec = vec / vec.two_norm(); |
| 5742 |
this->m_VariatesQ.set_column(i, vec); |
| 5743 |
} |
| 5744 |
} |
| 5745 |
|
| 5746 |
if (!this->m_Silent) |
| 5747 |
{ |
| 5748 |
std::cout << " P-sparseness : " << this->m_SparsenessP << std::endl; |
| 5749 |
std::cout << " Q-sparseness : " << this->m_SparsenessQ << std::endl; |
| 5750 |
} |
| 5751 |
|
| 5752 |
unsigned int maxloop = this->m_MaximumNumberOfIterations; |
| 5753 |
unsigned int innerloop = 1; |
| 5754 |
unsigned int loop = 0; |
| 5755 |
RealType energy = 0; |
| 5756 |
// NOT USED: RealType lastenergy = 0; |
| 5757 |
VectorType gradstepsp(n_vecs, basegradstep); |
| 5758 |
VectorType gradstepsq(n_vecs, basegradstep); |
| 5759 |
if (this->m_Covering == 0 || this->m_Covering == 2) |
| 5760 |
{ |
| 5761 |
innerloop = maxloop; |
| 5762 |
maxloop = 1; |
| 5763 |
} |
| 5764 |
for (unsigned int oo = 0; oo < maxloop; oo++) |
| 5765 |
{ |
| 5766 |
for (unsigned int k = 0; k < n_vecs; k++) |
| 5767 |
{ |
| 5768 |
this->m_GradStepP = gradstepsp[k]; |
| 5769 |
this->m_GradStepQ = gradstepsq[k]; |
| 5770 |
if (k == 0) |
| 5771 |
this->m_MatrixP = this->NormalizeMatrix(this->m_OriginalMatrixP, false); |
| 5772 |
if (k == 0) |
| 5773 |
this->m_MatrixQ = this->NormalizeMatrix(this->m_OriginalMatrixQ, false); |
| 5774 |
loop = 0; |
| 5775 |
this->m_GradStep = basegradstep; |
| 5776 |
while (((loop < innerloop))) // && ( energyincreases ) ) ) |
| 5777 |
{ |
| 5778 |
// Arnoldi Iteration SCCA |
| 5779 |
bool normbycov = true; |
| 5780 |
bool changedgrad = this->CCAUpdate(n_vecs_in, true, normbycov, k); |
| 5781 |
// NOT USED: lastenergy = energy; |
| 5782 |
energy = static_cast<RealType>(this->m_CanonicalCorrelations.one_norm()) / static_cast<RealType>(n_vecs_in); |
| 5783 |
if (this->m_GradStep < |
| 5784 |
1.e-12) // || ( itk::Math::abs ( energy - lastenergy ) < this->m_Epsilon && !changedgrad ) ) |
| 5785 |
{ |
| 5786 |
if (!this->m_Silent) |
| 5787 |
std::cout << " this->m_GradStep : " << this->m_GradStep << " energy : " << energy |
| 5788 |
<< " changedgrad : " << changedgrad << std::endl; |
| 5789 |
} |
| 5790 |
gradstepsp[k] = this->m_GradStepP; |
| 5791 |
gradstepsq[k] = this->m_GradStepQ; |
| 5792 |
loop++; |
| 5793 |
} // outer loop |
| 5794 |
} |
| 5795 |
if (!imagedriver) |
| 5796 |
this->SortResults(n_vecs_in); |
| 5797 |
// if ( ! m_Silent ) |
| 5798 |
{ |
| 5799 |
if (!this->m_Silent) |
| 5800 |
std::cout << " Loop " << oo << " Corrs : " << this->m_CanonicalCorrelations << " CorrMean : " << energy |
| 5801 |
<< std::endl; |
| 5802 |
} |
| 5803 |
} // oo |
| 5804 |
// this->RunDiagnostics(n_vecs); |
| 5805 |
double ccasum = 0; |
| 5806 |
for (unsigned int i = 0; i < this->m_CanonicalCorrelations.size(); i++) |
| 5807 |
{ |
| 5808 |
if (!this->m_Silent) |
| 5809 |
std::cout << i << " P: " << this->CountNonZero(this->m_VariatesP.get_column(i)) |
| 5810 |
<< " Q: " << this->CountNonZero(this->m_VariatesQ.get_column(i)) << std::endl; |
| 5811 |
ccasum += fabs(this->m_CanonicalCorrelations[i]); |
| 5812 |
} |
| 5813 |
if (n_vecs_in > 1) |
| 5814 |
{ |
| 5815 |
return ccasum; |
| 5816 |
} |
| 5817 |
else |
| 5818 |
{ |
| 5819 |
return fabs(this->m_CanonicalCorrelations[0]); |
| 5820 |
} |
| 5821 |
|
| 5822 |
/* ************************************************************************************************** |
| 5823 |
|
| 5824 |
// now deal with covariates --- this could work but needs to be fixed. |
| 5825 |
for ( unsigned int j=0; j< this->m_MatrixR.cols(); j++) { |
| 5826 |
// FIXME is this really what qj should be? it should be the projection of the nuisance variable |
| 5827 |
// into the space of qj ... |
| 5828 |
VectorType cov=this->m_MatrixR.get_column(j); |
| 5829 |
VectorType qj=cov*this->m_MatrixP; |
| 5830 |
RealType hjk=inner_product(cov,this->m_MatrixP*pveck)/ |
| 5831 |
inner_product(cov,cov); |
| 5832 |
if ( this->m_SCCANFormulation == PminusRQ || this->m_SCCANFormulation == PminusRQminusR ) |
| 5833 |
for (unsigned int i=0; i<pveck.size(); i++) pveck(i)=pveck(i)-hjk*qj(i); |
| 5834 |
qj=cov*this->m_MatrixQ; |
| 5835 |
hjk=inner_product(cov,this->m_MatrixQ*qveck)/ |
| 5836 |
inner_product(cov,cov); |
| 5837 |
if ( this->m_SCCANFormulation == PQminusR || this->m_SCCANFormulation == PminusRQminusR ) |
| 5838 |
for (unsigned int i=0; i<qveck.size(); i++) qveck(i)=qveck(i)-hjk*qj(i); |
| 5839 |
} |
| 5840 |
|
| 5841 |
// evil code below .... |
| 5842 |
|
| 5843 |
if ( this->m_SCCANFormulation != PQ ) { |
| 5844 |
for ( unsigned int dd=0; dd<20 ; dd++) { |
| 5845 |
VectorType upp(this->m_MatrixP.cols(),0); |
| 5846 |
VectorType upq(this->m_MatrixQ.cols(),0); |
| 5847 |
for ( unsigned int rr=0; rr<this->m_MatrixR.cols(); rr++) { |
| 5848 |
pveck=this->m_MatrixQ*qtemp; |
| 5849 |
qveck=this->m_MatrixP*ptemp; |
| 5850 |
pveck=pveck/pveck.two_norm(); |
| 5851 |
qveck=qveck/qveck.two_norm(); |
| 5852 |
pveck=this->Orthogonalize(pveck,this->m_OriginalMatrixR.get_column(rr)); |
| 5853 |
qveck=this->Orthogonalize(qveck,this->m_OriginalMatrixR.get_column(rr)); |
| 5854 |
VectorType tempp=this->m_MatrixP.transpose()*pveck; |
| 5855 |
VectorType tempq=this->m_MatrixQ.transpose()*qveck; |
| 5856 |
pveck=pveck/pveck.two_norm(); |
| 5857 |
upp=upp+tempp/tempp.two_norm()*1.0/(this->m_MatrixR.cols()); |
| 5858 |
qveck=qveck/qveck.two_norm(); |
| 5859 |
upq=upq+tempq/tempq.two_norm()*1.0/(this->m_MatrixR.cols()); |
| 5860 |
} //rr |
| 5861 |
RealType eps=.01; |
| 5862 |
if ( dd % 2 == 0 ) |
| 5863 |
{ |
| 5864 |
qtemp=qtemp+eps*upq; |
| 5865 |
// RealType c1=this->PearsonCorr(this->m_OriginalMatrixR.get_column(rr),this->m_MatrixQ*qtemp); |
| 5866 |
// RealType c2=this->PearsonCorr(this->m_OriginalMatrixR.get_column(rr),this->m_MatrixQ*(qtemp+eps*upq)); |
| 5867 |
// RealType c3=this->PearsonCorr(this->m_OriginalMatrixR.get_column(rr),this->m_MatrixQ*(qtemp-eps*upq)); |
| 5868 |
// if ( fabs(c2) < fabs(c1) ) qtemp=qtemp+eps*upq; |
| 5869 |
// if ( fabs(c3) < fabs(c1) ) qtemp=qtemp-eps*upq; |
| 5870 |
} |
| 5871 |
else |
| 5872 |
{ |
| 5873 |
ptemp=ptemp+eps*upp; |
| 5874 |
// RealType c1=this->PearsonCorr(this->m_OriginalMatrixR.get_column(rr),this->m_MatrixP*ptemp); |
| 5875 |
// RealType c2=this->PearsonCorr(this->m_OriginalMatrixR.get_column(rr),this->m_MatrixP*(ptemp+eps*upp)); |
| 5876 |
// RealType c3=this->PearsonCorr(this->m_OriginalMatrixR.get_column(rr),this->m_MatrixP*(ptemp-eps*upp)); |
| 5877 |
// if ( fabs(c2) < fabs(c1) ) ptemp=ptemp+eps*upp; |
| 5878 |
// if ( fabs(c3) < fabs(c1) ) ptemp=ptemp-eps*upp; |
| 5879 |
} |
| 5880 |
ptemp=ptemp/ptemp.two_norm(); |
| 5881 |
qtemp=qtemp/qtemp.two_norm(); |
| 5882 |
pveck=this->m_MatrixQ*qtemp; |
| 5883 |
qveck=this->m_MatrixP*ptemp; |
| 5884 |
} //dd |
| 5885 |
} // if ( this->m_SCCANFormulation != PQ ) { |
| 5886 |
|
| 5887 |
|
| 5888 |
|
| 5889 |
VectorType proj=this->m_MatrixQ*this->m_WeightsQ; |
| 5890 |
if ( false && ( this->m_SCCANFormulation == PminusRQ || this->m_SCCANFormulation == PminusRQminusR ) ) |
| 5891 |
for (unsigned int kk=0; kk< this->m_OriginalMatrixR.cols(); kk++) |
| 5892 |
proj=this->Orthogonalize(proj,this->m_MatrixR.get_column(kk)); |
| 5893 |
this->m_WeightsP=this->m_MatrixP.transpose()*(proj); |
| 5894 |
*/ |
| 5895 |
} |
| 5896 |
|
| 5897 |
template <typename TInputImage, typename TRealType> |
| 5898 |
TRealType |
| 5899 |
antsSCCANObject<TInputImage, TRealType>::IHTCCA(unsigned int n_vecs_in) |
| 5900 |
{ |
| 5901 |
if (!this->m_Silent) |
| 5902 |
std::cout << " IHTCCA " << std::endl; |
| 5903 |
unsigned int n_vecs = n_vecs_in; |
| 5904 |
|
| 5905 |
if (n_vecs < 1) |
| 5906 |
{ |
| 5907 |
n_vecs = 1; |
| 5908 |
} |
| 5909 |
this->m_CanonicalCorrelations.set_size(n_vecs); |
| 5910 |
this->m_CanonicalCorrelations.fill(0); |
| 5911 |
if (!this->m_Silent) |
| 5912 |
std::cout << " iht cca " << std::endl; |
| 5913 |
if (!this->m_Silent) |
| 5914 |
std::cout << " pos-p " << this->GetKeepPositiveP() << " pos-q " << this->GetKeepPositiveQ() << std::endl; |
| 5915 |
this->m_MatrixP = this->NormalizeMatrix(this->m_OriginalMatrixP, false); |
| 5916 |
this->m_MatrixQ = this->NormalizeMatrix(this->m_OriginalMatrixQ, false); |
| 5917 |
this->m_MatrixR = this->NormalizeMatrix(this->m_OriginalMatrixR, false); |
| 5918 |
|
| 5919 |
if (this->m_OriginalMatrixR.size() > 0) |
| 5920 |
{ |
| 5921 |
if (!this->m_Silent) |
| 5922 |
std::cout << "Partialing-Pre : -P-Norm " << this->m_MatrixP.frobenius_norm() << " -Q-Norm " |
| 5923 |
<< this->m_MatrixQ.frobenius_norm() << std::endl; |
| 5924 |
this->m_MatrixRRt = this->ProjectionMatrix(this->m_OriginalMatrixR); |
| 5925 |
if (this->m_SCCANFormulation == PminusRQ || this->m_SCCANFormulation == PminusRQminusR) |
| 5926 |
{ |
| 5927 |
if (!this->m_Silent) |
| 5928 |
std::cout << " Subtract R from P " << std::endl; |
| 5929 |
this->m_MatrixP = this->m_MatrixP - (this->m_MatrixRRt * this->m_MatrixP); |
| 5930 |
if (!this->m_Silent) |
| 5931 |
std::cout << "Partialing-Post : -P-Norm " << this->m_MatrixP.frobenius_norm() << std::endl; |
| 5932 |
} |
| 5933 |
if (this->m_SCCANFormulation == PQminusR || this->m_SCCANFormulation == PminusRQminusR) |
| 5934 |
{ |
| 5935 |
if (!this->m_Silent) |
| 5936 |
std::cout << " Subtract R from Q " << std::endl; |
| 5937 |
this->m_MatrixQ = this->m_MatrixQ - this->m_MatrixRRt * this->m_MatrixQ; |
| 5938 |
if (!this->m_Silent) |
| 5939 |
std::cout << " -Q-Norm " << this->m_MatrixQ.frobenius_norm() << std::endl; |
| 5940 |
} |
| 5941 |
} |
| 5942 |
|
| 5943 |
this->m_VariatesP.set_size(this->m_MatrixP.cols(), n_vecs); |
| 5944 |
this->m_VariatesQ.set_size(this->m_MatrixQ.cols(), n_vecs); |
| 5945 |
RealType totalcorr = 0; |
| 5946 |
RealType bestcorr = 0; |
| 5947 |
unsigned int bestseed = 1; |
| 5948 |
for (unsigned int seeder = 1; seeder < 100; seeder++) |
| 5949 |
{ |
| 5950 |
totalcorr = this->InitializeSCCA(n_vecs, seeder); |
| 5951 |
if (totalcorr > bestcorr) |
| 5952 |
{ |
| 5953 |
bestseed = seeder; |
| 5954 |
bestcorr = totalcorr; |
| 5955 |
} |
| 5956 |
totalcorr = 0; |
| 5957 |
} |
| 5958 |
if (!this->m_Silent) |
| 5959 |
std::cout << " Best initial corr " << bestcorr << std::endl; |
| 5960 |
this->InitializeSCCA(n_vecs, bestseed); |
| 5961 |
const unsigned int maxloop = this->m_MaximumNumberOfIterations; |
| 5962 |
unsigned int loop = 0; |
| 5963 |
bool energyincreases = true; |
| 5964 |
RealType energy = 0, lastenergy = 0; |
| 5965 |
while (((loop < maxloop) && (energyincreases)) || loop < 1) // |
| 5966 |
{ |
| 5967 |
// Arnoldi Iteration SCCA |
| 5968 |
for (unsigned int k = 0; k < n_vecs; k++) |
| 5969 |
{ |
| 5970 |
VectorType ptemp = this->m_VariatesP.get_column(k); |
| 5971 |
VectorType qtemp = this->m_VariatesQ.get_column(k); |
| 5972 |
VectorType pveck = this->m_MatrixQ * qtemp; |
| 5973 |
VectorType qveck = this->m_MatrixP * ptemp; |
| 5974 |
pveck = this->m_MatrixP.transpose() * pveck; |
| 5975 |
qveck = this->m_MatrixQ.transpose() * qveck; |
| 5976 |
for (unsigned int j = 0; j < k; j++) |
| 5977 |
{ |
| 5978 |
VectorType qj = this->m_VariatesP.get_column(j); |
| 5979 |
pveck = this->Orthogonalize(pveck, qj); |
| 5980 |
qj = this->m_VariatesQ.get_column(j); |
| 5981 |
qveck = this->Orthogonalize(qveck, qj); |
| 5982 |
} |
| 5983 |
RealType mup = inner_product(this->m_MatrixP * pveck, this->m_MatrixP * pveck) / pveck.two_norm(); |
| 5984 |
RealType muq = inner_product(this->m_MatrixQ * qveck, this->m_MatrixQ * qveck) / qveck.two_norm(); |
| 5985 |
this->IHTRegression(this->m_MatrixP, ptemp, pveck, mup, 5, 0, true, false); |
| 5986 |
this->IHTRegression(this->m_MatrixQ, qtemp, qveck, muq, 5, 0, false, false); |
| 5987 |
this->m_VariatesP.set_column(k, ptemp); |
| 5988 |
this->m_VariatesQ.set_column(k, qtemp); |
| 5989 |
this->NormalizeWeightsByCovariance(k, 0.05, 0.05); |
| 5990 |
VectorType proj1 = this->m_MatrixP * this->m_VariatesP.get_column(k); |
| 5991 |
VectorType proj2 = this->m_MatrixQ * this->m_VariatesQ.get_column(k); |
| 5992 |
this->m_CanonicalCorrelations[k] = this->PearsonCorr(proj1, proj2); |
| 5993 |
} |
| 5994 |
this->SortResults(n_vecs); |
| 5995 |
lastenergy = energy; |
| 5996 |
energy = this->m_CanonicalCorrelations.one_norm() / n_vecs; |
| 5997 |
if (!this->m_Silent) |
| 5998 |
std::cout << " Loop " << loop << " Corrs : " << this->m_CanonicalCorrelations << " CorrMean : " << energy |
| 5999 |
<< std::endl; |
| 6000 |
if (itk::Math::abs(energy - lastenergy) < 1.e-8 || energy < lastenergy) |
| 6001 |
{ |
| 6002 |
energyincreases = false; |
| 6003 |
} |
| 6004 |
else |
| 6005 |
{ |
| 6006 |
energyincreases = true; |
| 6007 |
} |
| 6008 |
loop++; |
| 6009 |
} // outer loop |
| 6010 |
|
| 6011 |
this->SortResults(n_vecs); |
| 6012 |
// this->RunDiagnostics(n_vecs); |
| 6013 |
double ccasum = 0; |
| 6014 |
for (unsigned int i = 0; i < this->m_CanonicalCorrelations.size(); i++) |
| 6015 |
{ |
| 6016 |
ccasum += fabs(this->m_CanonicalCorrelations[i]); |
| 6017 |
} |
| 6018 |
if (n_vecs_in > 1) |
| 6019 |
{ |
| 6020 |
return ccasum; |
| 6021 |
} |
| 6022 |
else |
| 6023 |
{ |
| 6024 |
return fabs(this->m_CanonicalCorrelations[0]); |
| 6025 |
} |
| 6026 |
} |
| 6027 |
|
| 6028 |
template <typename TInputImage, typename TRealType> |
| 6029 |
void |
| 6030 |
antsSCCANObject<TInputImage, TRealType>::WhitenDataSetForRunSCCANMultiple(unsigned int nvecs) |
| 6031 |
{ |
| 6032 |
if (this->m_Debug) |
| 6033 |
{ |
| 6034 |
if (!this->m_Silent) |
| 6035 |
std::cout << " now whiten and apply R " << std::endl; |
| 6036 |
} |
| 6037 |
if (this->m_OriginalMatrixR.size() > 0 || nvecs > 0) |
| 6038 |
{ |
| 6039 |
this->m_MatrixP = this->NormalizeMatrix(this->m_OriginalMatrixP); |
| 6040 |
if (this->m_VariatesP.size() > 0) |
| 6041 |
{ |
| 6042 |
this->m_MatrixRp.set_size(this->m_MatrixP.rows(), this->m_OriginalMatrixR.cols() + nvecs); |
| 6043 |
this->m_MatrixRp.fill(0); |
| 6044 |
if (this->m_OriginalMatrixR.size() > 0 && |
| 6045 |
(this->m_SCCANFormulation == PminusRQ || this->m_SCCANFormulation == PminusRQminusR)) |
| 6046 |
{ |
| 6047 |
this->m_MatrixRp.set_columns(0, this->m_OriginalMatrixR); |
| 6048 |
this->m_MatrixRp.set_columns(this->m_OriginalMatrixR.cols(), |
| 6049 |
this->m_MatrixP * (this->m_VariatesP.get_n_columns(0, nvecs))); |
| 6050 |
} |
| 6051 |
else |
| 6052 |
{ |
| 6053 |
this->m_MatrixRp.set_columns(0, this->m_MatrixP * (this->m_VariatesP.get_n_columns(0, nvecs))); |
| 6054 |
} |
| 6055 |
} |
| 6056 |
else |
| 6057 |
{ |
| 6058 |
this->m_MatrixRp = this->NormalizeMatrix(this->m_OriginalMatrixR); |
| 6059 |
} |
| 6060 |
this->m_MatrixRp = ProjectionMatrix(this->m_MatrixRp); |
| 6061 |
if (this->m_Debug && this->m_VariatesP.cols() > 1 && !this->m_Silent) |
| 6062 |
{ |
| 6063 |
std::cout << " corr-pre " |
| 6064 |
<< this->PearsonCorr((this->m_OriginalMatrixP * this->m_VariatesP).get_column(0), |
| 6065 |
(this->m_OriginalMatrixP * this->m_VariatesP).get_column(1)) |
| 6066 |
<< std::endl; |
| 6067 |
std::cout << " corr-post " |
| 6068 |
<< this->PearsonCorr((this->m_MatrixP * this->m_VariatesP).get_column(0), |
| 6069 |
(this->m_MatrixP * this->m_VariatesP).get_column(1)) |
| 6070 |
<< std::endl; |
| 6071 |
} |
| 6072 |
|
| 6073 |
this->m_MatrixQ = this->NormalizeMatrix(this->m_OriginalMatrixQ); |
| 6074 |
if (this->m_VariatesQ.size() > 0) |
| 6075 |
{ |
| 6076 |
this->m_MatrixRq.set_size(this->m_MatrixQ.rows(), this->m_OriginalMatrixR.cols() + nvecs); |
| 6077 |
this->m_MatrixRq.fill(0); |
| 6078 |
if (this->m_OriginalMatrixR.size() > 0 && |
| 6079 |
(this->m_SCCANFormulation == PQminusR || this->m_SCCANFormulation == PminusRQminusR)) |
| 6080 |
{ |
| 6081 |
this->m_MatrixRq.set_columns(0, this->m_OriginalMatrixR); |
| 6082 |
this->m_MatrixRq.set_columns(this->m_OriginalMatrixR.cols(), |
| 6083 |
this->m_MatrixQ * (this->m_VariatesQ.get_n_columns(0, nvecs))); |
| 6084 |
} |
| 6085 |
else |
| 6086 |
{ |
| 6087 |
this->m_MatrixRq.set_columns(0, this->m_MatrixQ * (this->m_VariatesQ.get_n_columns(0, nvecs))); |
| 6088 |
} |
| 6089 |
} |
| 6090 |
else |
| 6091 |
{ |
| 6092 |
this->m_MatrixRq = this->NormalizeMatrix(this->m_OriginalMatrixR); |
| 6093 |
} |
| 6094 |
this->m_MatrixRq = ProjectionMatrix(this->m_MatrixRq); |
| 6095 |
} |
| 6096 |
else |
| 6097 |
{ |
| 6098 |
this->m_MatrixP = this->NormalizeMatrix(this->m_OriginalMatrixP); |
| 6099 |
this->m_MatrixQ = this->NormalizeMatrix(this->m_OriginalMatrixQ); |
| 6100 |
// this->m_MatrixP=this->WhitenMatrix(this->m_MatrixP); |
| 6101 |
// this->m_MatrixQ=this->WhitenMatrix(this->m_MatrixQ); |
| 6102 |
} |
| 6103 |
|
| 6104 |
if (this->m_Debug) |
| 6105 |
{ |
| 6106 |
if (!this->m_Silent) |
| 6107 |
std::cout << " whiten and apply R done " << std::endl; |
| 6108 |
} |
| 6109 |
} |
| 6110 |
|
| 6111 |
|
| 6112 |
template <typename TInputImage, typename TRealType> |
| 6113 |
void |
| 6114 |
antsSCCANObject<TInputImage, TRealType>::NormalizeWeights(const unsigned int k) |
| 6115 |
{ |
| 6116 |
this->m_WeightsP = this->m_VariatesP.get_column(k); |
| 6117 |
this->m_WeightsP = this->m_WeightsP / sqrt(this->m_WeightsP.two_norm()); // to scale gradient down |
| 6118 |
this->m_VariatesP.set_column(k, this->m_WeightsP); |
| 6119 |
this->m_WeightsQ = this->m_VariatesQ.get_column(k); |
| 6120 |
this->m_WeightsQ = this->m_WeightsQ / sqrt(this->m_WeightsQ.two_norm()); // to scale gradient down |
| 6121 |
this->m_VariatesQ.set_column(k, this->m_WeightsQ); |
| 6122 |
} |
| 6123 |
|
| 6124 |
template <typename TInputImage, typename TRealType> |
| 6125 |
void |
| 6126 |
antsSCCANObject<TInputImage, TRealType>::NormalizeWeightsByCovariance(const unsigned int k, |
| 6127 |
const TRealType taup, |
| 6128 |
const TRealType tauq) |
| 6129 |
{ |
| 6130 |
// for ( unsigned int k=0; k<this->m_VariatesP.cols(); k++) |
| 6131 |
{ |
| 6132 |
this->m_WeightsP = this->m_VariatesP.get_column(k); |
| 6133 |
this->m_WeightsQ = this->m_VariatesQ.get_column(k); |
| 6134 |
RealType normP = 0; |
| 6135 |
if (this->m_MatrixRp.size() > 0) |
| 6136 |
{ |
| 6137 |
VectorType w = this->m_MatrixP * this->m_WeightsP; |
| 6138 |
normP = inner_product(w, (this->m_MatrixP - this->m_MatrixRp * this->m_MatrixP) * this->m_WeightsP); |
| 6139 |
} |
| 6140 |
else |
| 6141 |
{ |
| 6142 |
// v^t ( X^t X + k * Id ) v = v^t ( X X^t v + k * Id * v ) |
| 6143 |
// = v^t ( X X^t v + k * Id * v ) |
| 6144 |
vnl_diag_matrix<double> regdiagp(this->m_MatrixP.cols(), taup); |
| 6145 |
VectorType w = this->m_MatrixP.transpose() * (this->m_MatrixP * this->m_WeightsP) + regdiagp * this->m_WeightsP; |
| 6146 |
normP = inner_product(this->m_WeightsP, w); |
| 6147 |
} |
| 6148 |
if (normP > 0) |
| 6149 |
{ |
| 6150 |
if (this->m_Debug) |
| 6151 |
if (!this->m_Silent) |
| 6152 |
std::cout << "normP " << normP; |
| 6153 |
this->m_WeightsP = this->m_WeightsP / sqrt(normP); |
| 6154 |
this->m_VariatesP.set_column(k, this->m_WeightsP); |
| 6155 |
} |
| 6156 |
RealType normQ = 0; |
| 6157 |
if (this->m_MatrixRq.size() > 0) |
| 6158 |
{ |
| 6159 |
VectorType w = this->m_MatrixQ * this->m_WeightsQ; |
| 6160 |
normQ = inner_product(w, (this->m_MatrixQ - this->m_MatrixRq * this->m_MatrixQ) * this->m_WeightsQ); |
| 6161 |
} |
| 6162 |
else |
| 6163 |
{ |
| 6164 |
vnl_diag_matrix<double> regdiagq(this->m_MatrixQ.cols(), tauq); |
| 6165 |
VectorType w = this->m_MatrixQ.transpose() * (this->m_MatrixQ * this->m_WeightsQ) + regdiagq * this->m_WeightsQ; |
| 6166 |
normQ = inner_product(this->m_WeightsQ, w); |
| 6167 |
} |
| 6168 |
if (normQ > 0) |
| 6169 |
{ |
| 6170 |
if (this->m_Debug) |
| 6171 |
if (!this->m_Silent) |
| 6172 |
std::cout << " normQ " << normQ << " "; |
| 6173 |
this->m_WeightsQ = this->m_WeightsQ / sqrt(normQ); |
| 6174 |
this->m_VariatesQ.set_column(k, this->m_WeightsQ); |
| 6175 |
} |
| 6176 |
} |
| 6177 |
} |
| 6178 |
|
| 6179 |
template <typename TInputImage, typename TRealType> |
| 6180 |
TRealType |
| 6181 |
antsSCCANObject<TInputImage, TRealType>::RunSCCAN2multiple(unsigned int n_vecs) |
| 6182 |
{ |
| 6183 |
this->m_Debug = false; |
| 6184 |
// this->m_Debug=true; |
| 6185 |
if (!this->m_Silent) |
| 6186 |
std::cout << " power iteration (partial) scca " << std::endl; |
| 6187 |
this->m_CanonicalCorrelations.set_size(n_vecs); |
| 6188 |
this->m_CanonicalCorrelations.fill(0); |
| 6189 |
RealType truecorr = 0; |
| 6190 |
unsigned int nr1 = this->m_MatrixP.rows(); |
| 6191 |
unsigned int nr2 = this->m_MatrixQ.rows(); |
| 6192 |
this->m_VariatesP.set_size(0, 0); |
| 6193 |
this->m_VariatesQ.set_size(0, 0); |
| 6194 |
if (nr1 != nr2) |
| 6195 |
{ |
| 6196 |
if (!this->m_Silent) |
| 6197 |
std::cout << " P rows " << this->m_MatrixP.rows() << " cols " << this->m_MatrixP.cols() << std::endl; |
| 6198 |
if (!this->m_Silent) |
| 6199 |
std::cout << " Q rows " << this->m_MatrixQ.rows() << " cols " << this->m_MatrixQ.cols() << std::endl; |
| 6200 |
if (!this->m_Silent) |
| 6201 |
std::cout << " R rows " << this->m_MatrixR.rows() << " cols " << this->m_MatrixR.cols() << std::endl; |
| 6202 |
if (!this->m_Silent) |
| 6203 |
std::cout << " N-rows for MatrixP does not equal N-rows for MatrixQ " << nr1 << " vs " << nr2 << std::endl; |
| 6204 |
std::exception(); |
| 6205 |
} |
| 6206 |
if (!this->m_AlreadyWhitened) |
| 6207 |
{ |
| 6208 |
if (this->m_Debug) |
| 6209 |
{ |
| 6210 |
if (!this->m_Silent) |
| 6211 |
std::cout << " whiten " << std::endl; |
| 6212 |
} |
| 6213 |
this->WhitenDataSetForRunSCCANMultiple(); |
| 6214 |
this->m_AlreadyWhitened = true; |
| 6215 |
if (this->m_Debug) |
| 6216 |
{ |
| 6217 |
if (!this->m_Silent) |
| 6218 |
std::cout << " whiten done " << std::endl; |
| 6219 |
} |
| 6220 |
} |
| 6221 |
this->m_VariatesP.set_size(this->m_MatrixP.cols(), n_vecs); |
| 6222 |
this->m_VariatesQ.set_size(this->m_MatrixQ.cols(), n_vecs); |
| 6223 |
for (unsigned int kk = 0; kk < n_vecs; kk++) |
| 6224 |
{ |
| 6225 |
this->m_VariatesP.set_column(kk, this->InitializeV(this->m_MatrixP)); |
| 6226 |
this->m_VariatesQ.set_column(kk, this->InitializeV(this->m_MatrixQ)); |
| 6227 |
} |
| 6228 |
// begin computing solution |
| 6229 |
for (unsigned int makesparse = 1; makesparse < 2; makesparse++) |
| 6230 |
{ |
| 6231 |
unsigned int which_e_vec = 0; |
| 6232 |
bool notdone = true; |
| 6233 |
while (notdone) |
| 6234 |
{ |
| 6235 |
if (this->m_Debug) |
| 6236 |
{ |
| 6237 |
if (!this->m_Silent) |
| 6238 |
std::cout << " get canonical variate number " << which_e_vec + 1 << std::endl; |
| 6239 |
} |
| 6240 |
double initcorr = 1.e-5; |
| 6241 |
truecorr = initcorr; |
| 6242 |
double deltacorr = 1, lastcorr = initcorr * 0.5; |
| 6243 |
this->m_WeightsP = this->m_VariatesP.get_column(which_e_vec); |
| 6244 |
this->m_WeightsQ = this->m_VariatesQ.get_column(which_e_vec); |
| 6245 |
unsigned long its = 0, min_its = 5; |
| 6246 |
if (this->m_Debug) |
| 6247 |
{ |
| 6248 |
if (!this->m_Silent) |
| 6249 |
std::cout << " Begin " << std::endl; |
| 6250 |
} |
| 6251 |
while ((its < this->m_MaximumNumberOfIterations && deltacorr > this->m_ConvergenceThreshold) || its < min_its) |
| 6252 |
{ |
| 6253 |
if (its == 0) |
| 6254 |
{ |
| 6255 |
this->WhitenDataSetForRunSCCANMultiple(which_e_vec); |
| 6256 |
} |
| 6257 |
bool doorth = true; // this->m_Debug=true; |
| 6258 |
{ |
| 6259 |
VectorType proj = this->m_MatrixQ * this->m_WeightsQ; |
| 6260 |
if (this->m_MatrixRp.size() > 0 && |
| 6261 |
(this->m_SCCANFormulation == PminusRQ || this->m_SCCANFormulation == PminusRQminusR)) |
| 6262 |
{ |
| 6263 |
this->m_WeightsP = this->m_MatrixP.transpose() * (proj - this->m_MatrixRp * proj); |
| 6264 |
} |
| 6265 |
else |
| 6266 |
{ |
| 6267 |
this->m_WeightsP = this->m_MatrixP.transpose() * (proj); |
| 6268 |
} |
| 6269 |
if (doorth) |
| 6270 |
{ |
| 6271 |
for (unsigned int kk = 0; kk < which_e_vec; kk++) |
| 6272 |
{ |
| 6273 |
this->m_WeightsP = this->Orthogonalize( |
| 6274 |
this->m_WeightsP, this->m_VariatesP.get_column(kk), &this->m_MatrixP, &this->m_MatrixP); |
| 6275 |
} |
| 6276 |
} |
| 6277 |
this->m_WeightsP = this->SoftThreshold(this->m_WeightsP, this->m_FractionNonZeroP, !this->m_KeepPositiveP); |
| 6278 |
if (its > 0) |
| 6279 |
{ |
| 6280 |
this->m_WeightsP = this->ClusterThresholdVariate(this->m_WeightsP, this->m_MaskImageP); |
| 6281 |
} |
| 6282 |
if (which_e_vec > 0 && this->m_Debug) |
| 6283 |
{ |
| 6284 |
if (!this->m_Silent) |
| 6285 |
std::cout << " p orth-b " |
| 6286 |
<< this->PearsonCorr(this->m_MatrixP * this->m_WeightsP, |
| 6287 |
this->m_MatrixP * this->m_VariatesP.get_column(0)) |
| 6288 |
<< std::endl; |
| 6289 |
} |
| 6290 |
} |
| 6291 |
VectorType projp = this->m_MatrixQ * this->m_WeightsQ; |
| 6292 |
VectorType projq = this->m_MatrixP * this->m_WeightsP; |
| 6293 |
|
| 6294 |
{ |
| 6295 |
VectorType proj = this->m_MatrixP * this->m_WeightsP; |
| 6296 |
if (this->m_MatrixRq.size() > 0 && |
| 6297 |
(this->m_SCCANFormulation == PQminusR || this->m_SCCANFormulation == PminusRQminusR)) |
| 6298 |
{ |
| 6299 |
this->m_WeightsQ = this->m_MatrixQ.transpose() * (proj - this->m_MatrixRq * proj); |
| 6300 |
} |
| 6301 |
else |
| 6302 |
{ |
| 6303 |
this->m_WeightsQ = this->m_MatrixQ.transpose() * (proj); |
| 6304 |
} |
| 6305 |
if (doorth) |
| 6306 |
{ |
| 6307 |
for (unsigned int kk = 0; kk < which_e_vec; kk++) |
| 6308 |
{ |
| 6309 |
this->m_WeightsQ = this->Orthogonalize( |
| 6310 |
this->m_WeightsQ, this->m_VariatesQ.get_column(kk), &this->m_MatrixQ, &this->m_MatrixQ); |
| 6311 |
} |
| 6312 |
} |
| 6313 |
this->m_WeightsQ = this->SoftThreshold(this->m_WeightsQ, this->m_FractionNonZeroQ, !this->m_KeepPositiveQ); |
| 6314 |
if (which_e_vec > 0 && this->m_Debug) |
| 6315 |
{ |
| 6316 |
if (!this->m_Silent) |
| 6317 |
std::cout << " q orth-b " |
| 6318 |
<< this->PearsonCorr(this->m_MatrixQ * this->m_WeightsQ, |
| 6319 |
this->m_MatrixQ * this->m_VariatesQ.get_column(0)) |
| 6320 |
<< std::endl; |
| 6321 |
} |
| 6322 |
} |
| 6323 |
this->m_WeightsP = this->m_MatrixP.transpose() * (projp); |
| 6324 |
this->m_WeightsQ = this->m_MatrixQ.transpose() * (projq); |
| 6325 |
this->ReSoftThreshold(this->m_WeightsP, this->m_FractionNonZeroP, this->m_KeepPositiveP); |
| 6326 |
this->ReSoftThreshold(this->m_WeightsQ, this->m_FractionNonZeroQ, this->m_KeepPositiveQ); |
| 6327 |
if (its > 1) |
| 6328 |
{ |
| 6329 |
this->m_WeightsP = |
| 6330 |
this->ClusterThresholdVariate(this->m_WeightsP, this->m_MaskImageP, this->m_MinClusterSizeP); |
| 6331 |
this->m_WeightsQ = |
| 6332 |
this->ClusterThresholdVariate(this->m_WeightsQ, this->m_MaskImageQ, this->m_MinClusterSizeQ); |
| 6333 |
} |
| 6334 |
for (unsigned int kk = 0; kk < which_e_vec; kk++) |
| 6335 |
{ |
| 6336 |
this->m_WeightsP = |
| 6337 |
this->Orthogonalize(this->m_WeightsP, this->m_VariatesP.get_column(kk), &this->m_MatrixP, &this->m_MatrixP); |
| 6338 |
} |
| 6339 |
if ((this->m_SCCANFormulation == PminusRQ || this->m_SCCANFormulation == PminusRQminusR)) |
| 6340 |
{ |
| 6341 |
for (unsigned int kk = 0; kk < this->m_OriginalMatrixR.cols(); kk++) |
| 6342 |
{ |
| 6343 |
this->m_WeightsP = this->Orthogonalize( |
| 6344 |
this->m_WeightsP, this->m_MatrixR.get_column(kk) * this->m_MatrixP, &this->m_MatrixP, &this->m_MatrixP); |
| 6345 |
} |
| 6346 |
} |
| 6347 |
for (unsigned int kk = 0; kk < which_e_vec; kk++) |
| 6348 |
{ |
| 6349 |
this->m_WeightsQ = |
| 6350 |
this->Orthogonalize(this->m_WeightsQ, this->m_VariatesQ.get_column(kk), &this->m_MatrixQ, &this->m_MatrixQ); |
| 6351 |
} |
| 6352 |
if ((this->m_SCCANFormulation == PQminusR || this->m_SCCANFormulation == PminusRQminusR)) |
| 6353 |
{ |
| 6354 |
for (unsigned int kk = 0; kk < this->m_OriginalMatrixR.cols(); kk++) |
| 6355 |
{ |
| 6356 |
this->m_WeightsQ = this->Orthogonalize( |
| 6357 |
this->m_WeightsQ, this->m_MatrixR.get_column(kk) * this->m_MatrixQ, &this->m_MatrixQ, &this->m_MatrixQ); |
| 6358 |
} |
| 6359 |
} |
| 6360 |
|
| 6361 |
this->NormalizeWeightsByCovariance(which_e_vec); |
| 6362 |
this->m_VariatesP.set_column(which_e_vec, this->m_WeightsP); |
| 6363 |
this->m_VariatesQ.set_column(which_e_vec, this->m_WeightsQ); |
| 6364 |
truecorr = this->PearsonCorr(this->m_MatrixP * this->m_WeightsP, this->m_MatrixQ * this->m_WeightsQ); |
| 6365 |
if (this->m_Debug) |
| 6366 |
{ |
| 6367 |
if (!this->m_Silent) |
| 6368 |
std::cout << " corr " << truecorr << " it " << its << std::endl; |
| 6369 |
} |
| 6370 |
deltacorr = fabs(truecorr - lastcorr); |
| 6371 |
lastcorr = truecorr; |
| 6372 |
++its; |
| 6373 |
this->m_CanonicalCorrelations[which_e_vec] = truecorr; |
| 6374 |
if (!this->m_Silent) |
| 6375 |
std::cout << " canonical variate number " << which_e_vec + 1 << " corr " |
| 6376 |
<< this->m_CanonicalCorrelations[which_e_vec] << " kept cluster " << this->m_KeptClusterSize |
| 6377 |
<< std::endl; |
| 6378 |
} // inner_it |
| 6379 |
|
| 6380 |
if (fabs(truecorr) < 1.e-2 || (which_e_vec + 1) == n_vecs) |
| 6381 |
{ |
| 6382 |
notdone = false; |
| 6383 |
} |
| 6384 |
else |
| 6385 |
{ |
| 6386 |
which_e_vec++; |
| 6387 |
} |
| 6388 |
} |
| 6389 |
|
| 6390 |
if (this->m_Debug) |
| 6391 |
{ |
| 6392 |
if (!this->m_Silent) |
| 6393 |
std::cout << " done with loop " << std::endl; |
| 6394 |
} |
| 6395 |
std::vector<TRealType> evals(n_vecs, 0); |
| 6396 |
std::vector<TRealType> oevals(n_vecs, 0); |
| 6397 |
for (unsigned long j = 0; j < n_vecs; ++j) |
| 6398 |
{ |
| 6399 |
RealType val = fabs(this->m_CanonicalCorrelations[j]); |
| 6400 |
evals[j] = val; |
| 6401 |
oevals[j] = val; |
| 6402 |
} |
| 6403 |
|
| 6404 |
// sort and reindex the eigenvectors/values |
| 6405 |
sort(evals.begin(), evals.end(), std::greater<TRealType>()); |
| 6406 |
std::vector<int> sorted_indices(n_vecs, -1); |
| 6407 |
for (unsigned int i = 0; i < evals.size(); i++) |
| 6408 |
{ |
| 6409 |
for (unsigned int j = 0; j < evals.size(); j++) |
| 6410 |
{ |
| 6411 |
if (evals[i] == oevals[j] && sorted_indices[i] == -1) |
| 6412 |
{ |
| 6413 |
sorted_indices[i] = j; |
| 6414 |
oevals[j] = 0; |
| 6415 |
} |
| 6416 |
} |
| 6417 |
} |
| 6418 |
|
| 6419 |
VectorType newcorrs(n_vecs, 0); |
| 6420 |
MatrixType varp(this->m_MatrixP.cols(), n_vecs, 0); |
| 6421 |
MatrixType varq(this->m_MatrixQ.cols(), n_vecs, 0); |
| 6422 |
for (unsigned int i = 0; i < n_vecs; i++) |
| 6423 |
{ |
| 6424 |
varp.set_column(i, this->m_VariatesP.get_column(sorted_indices[i])); |
| 6425 |
varq.set_column(i, this->m_VariatesQ.get_column(sorted_indices[i])); |
| 6426 |
newcorrs[i] = (this->m_CanonicalCorrelations[sorted_indices[i]]); |
| 6427 |
} |
| 6428 |
for (unsigned int i = 0; i < n_vecs; i++) |
| 6429 |
{ |
| 6430 |
this->m_VariatesP.set_column(i, varp.get_column(i)); |
| 6431 |
this->m_VariatesQ.set_column(i, varq.get_column(i)); |
| 6432 |
} |
| 6433 |
this->m_CanonicalCorrelations = newcorrs; |
| 6434 |
// this->RunDiagnostics(n_vecs); |
| 6435 |
} // makesparse |
| 6436 |
|
| 6437 |
RealType corrsum = 0; |
| 6438 |
for (unsigned int i = 0; i < this->m_CanonicalCorrelations.size(); i++) |
| 6439 |
{ |
| 6440 |
corrsum += fabs(this->m_CanonicalCorrelations[i]); |
| 6441 |
} |
| 6442 |
return this->m_CanonicalCorrelations[0]; // corrsum; |
| 6443 |
} |
| 6444 |
|
| 6445 |
template <typename TInputImage, typename TRealType> |
| 6446 |
TRealType |
| 6447 |
antsSCCANObject<TInputImage, TRealType>::RunSCCAN2() |
| 6448 |
{ |
| 6449 |
RealType truecorr = 0; |
| 6450 |
unsigned int nr1 = this->m_MatrixP.rows(); |
| 6451 |
unsigned int nr2 = this->m_MatrixQ.rows(); |
| 6452 |
|
| 6453 |
if (nr1 != nr2) |
| 6454 |
{ |
| 6455 |
if (!this->m_Silent) |
| 6456 |
std::cout << " P rows " << this->m_MatrixP.rows() << " cols " << this->m_MatrixP.cols() << std::endl; |
| 6457 |
if (!this->m_Silent) |
| 6458 |
std::cout << " Q rows " << this->m_MatrixQ.rows() << " cols " << this->m_MatrixQ.cols() << std::endl; |
| 6459 |
if (!this->m_Silent) |
| 6460 |
std::cout << " R rows " << this->m_MatrixR.rows() << " cols " << this->m_MatrixR.cols() << std::endl; |
| 6461 |
if (!this->m_Silent) |
| 6462 |
std::cout << " N-rows for MatrixP does not equal N-rows for MatrixQ " << nr1 << " vs " << nr2 << std::endl; |
| 6463 |
std::exception(); |
| 6464 |
} |
| 6465 |
else |
| 6466 |
{ |
| 6467 |
// if ( ! this->m_Silent ) std::cout << " P-positivity constraints? " << this->m_KeepPositiveP << " frac " << |
| 6468 |
// this->m_FractionNonZeroP |
| 6469 |
// << " |
| 6470 |
// Q-positivity constraints? " << m_KeepPositiveQ << " frac " << this->m_FractionNonZeroQ << std::endl; |
| 6471 |
} |
| 6472 |
this->m_WeightsP = this->InitializeV(this->m_MatrixP); |
| 6473 |
this->m_WeightsQ = this->InitializeV(this->m_MatrixQ); |
| 6474 |
|
| 6475 |
// if ( !this->m_AlreadyWhitened ) |
| 6476 |
{ |
| 6477 |
if (this->m_Debug) |
| 6478 |
{ |
| 6479 |
if (!this->m_Silent) |
| 6480 |
std::cout << " norm P " << std::endl; |
| 6481 |
} |
| 6482 |
this->m_MatrixP = this->NormalizeMatrix(this->m_MatrixP); |
| 6483 |
if (this->m_Debug) |
| 6484 |
{ |
| 6485 |
if (!this->m_Silent) |
| 6486 |
std::cout << " norm Q " << std::endl; |
| 6487 |
} |
| 6488 |
this->m_MatrixQ = this->NormalizeMatrix(this->m_MatrixQ); |
| 6489 |
if (this->m_OriginalMatrixR.size() > 0) |
| 6490 |
{ |
| 6491 |
this->m_MatrixR = this->NormalizeMatrix(this->m_OriginalMatrixR); |
| 6492 |
this->m_MatrixR = this->WhitenMatrix(this->m_MatrixR); |
| 6493 |
this->m_MatrixRRt = this->m_MatrixR * this->m_MatrixR.transpose(); |
| 6494 |
this->UpdatePandQbyR(); |
| 6495 |
} |
| 6496 |
this->m_MatrixP = this->WhitenMatrix(this->m_MatrixP); |
| 6497 |
this->m_MatrixQ = this->WhitenMatrix(this->m_MatrixQ); |
| 6498 |
this->m_AlreadyWhitened = true; |
| 6499 |
} |
| 6500 |
for (unsigned int outer_it = 0; outer_it < 2; outer_it++) |
| 6501 |
{ |
| 6502 |
truecorr = 0; |
| 6503 |
double deltacorr = 1, lastcorr = 1; |
| 6504 |
unsigned long its = 0; |
| 6505 |
while (its < this->m_MaximumNumberOfIterations && deltacorr > this->m_ConvergenceThreshold) |
| 6506 |
{ |
| 6507 |
this->m_WeightsP = this->TrueCCAPowerUpdate( |
| 6508 |
this->m_FractionNonZeroP, this->m_MatrixP, this->m_WeightsQ, this->m_MatrixQ, this->m_KeepPositiveP, false); |
| 6509 |
this->m_WeightsQ = this->TrueCCAPowerUpdate( |
| 6510 |
this->m_FractionNonZeroQ, this->m_MatrixQ, this->m_WeightsP, this->m_MatrixP, this->m_KeepPositiveQ, false); |
| 6511 |
truecorr = this->PearsonCorr(this->m_MatrixP * this->m_WeightsP, this->m_MatrixQ * this->m_WeightsQ); |
| 6512 |
deltacorr = fabs(truecorr - lastcorr); |
| 6513 |
lastcorr = truecorr; |
| 6514 |
++its; |
| 6515 |
} // inner_it |
| 6516 |
} // outer_it |
| 6517 |
this->m_CorrelationForSignificanceTest = truecorr; |
| 6518 |
return truecorr; |
| 6519 |
} |
| 6520 |
|
| 6521 |
template <typename TInputImage, typename TRealType> |
| 6522 |
TRealType |
| 6523 |
antsSCCANObject<TInputImage, TRealType>::RunSCCAN3() |
| 6524 |
{ |
| 6525 |
unsigned int nc1 = this->m_MatrixP.rows(); |
| 6526 |
unsigned int nc2 = this->m_MatrixQ.rows(); |
| 6527 |
unsigned int nc3 = this->m_MatrixR.rows(); |
| 6528 |
|
| 6529 |
if (nc1 != nc2 || nc1 != nc3 || nc3 != nc2) |
| 6530 |
{ |
| 6531 |
if (!this->m_Silent) |
| 6532 |
std::cout << " P rows " << this->m_MatrixP.rows() << " cols " << this->m_MatrixP.cols() << std::endl; |
| 6533 |
if (!this->m_Silent) |
| 6534 |
std::cout << " Q rows " << this->m_MatrixQ.rows() << " cols " << this->m_MatrixQ.cols() << std::endl; |
| 6535 |
if (!this->m_Silent) |
| 6536 |
std::cout << " R rows " << this->m_MatrixR.rows() << " cols " << this->m_MatrixR.cols() << std::endl; |
| 6537 |
if (!this->m_Silent) |
| 6538 |
std::cout << " N-rows do not match " << std::endl; |
| 6539 |
std::exception(); |
| 6540 |
} |
| 6541 |
|
| 6542 |
this->m_WeightsP = this->InitializeV(this->m_MatrixP); |
| 6543 |
this->m_WeightsQ = this->InitializeV(this->m_MatrixQ); |
| 6544 |
this->m_WeightsR = this->InitializeV(this->m_MatrixR); |
| 6545 |
if (!this->m_AlreadyWhitened) |
| 6546 |
{ |
| 6547 |
this->m_MatrixP = this->NormalizeMatrix(this->m_MatrixP); |
| 6548 |
this->m_MatrixP = this->WhitenMatrix(this->m_MatrixP); |
| 6549 |
this->m_MatrixQ = this->NormalizeMatrix(this->m_MatrixQ); |
| 6550 |
this->m_MatrixQ = this->WhitenMatrix(this->m_MatrixQ); |
| 6551 |
this->m_MatrixR = this->NormalizeMatrix(this->m_MatrixR); |
| 6552 |
this->m_MatrixR = this->WhitenMatrix(this->m_MatrixR); |
| 6553 |
this->m_AlreadyWhitened = true; |
| 6554 |
} |
| 6555 |
RealType truecorr = 0; |
| 6556 |
RealType norm = 0, deltacorr = 1, lastcorr = 1; |
| 6557 |
unsigned long its = 0; |
| 6558 |
while (its < this->m_MaximumNumberOfIterations && deltacorr > this->m_ConvergenceThreshold) |
| 6559 |
{ |
| 6560 |
/** for sparse mcca |
| 6561 |
* w_i \leftarrow \frac{ S( X_i^T ( \sum_{j \ne i} X_j w_j ) }{norm of above } |
| 6562 |
*/ |
| 6563 |
this->m_WeightsP = |
| 6564 |
this->m_MatrixP.transpose() * (this->m_MatrixQ * this->m_WeightsQ + this->m_MatrixR * this->m_WeightsR); |
| 6565 |
this->ReSoftThreshold(this->m_WeightsP, this->m_FractionNonZeroP, this->m_KeepPositiveP); |
| 6566 |
norm = this->m_WeightsP.two_norm(); |
| 6567 |
this->m_WeightsP = this->m_WeightsP / (norm); |
| 6568 |
|
| 6569 |
this->m_WeightsQ = |
| 6570 |
this->m_MatrixQ.transpose() * (this->m_MatrixP * this->m_WeightsP + this->m_MatrixR * this->m_WeightsR); |
| 6571 |
this->ReSoftThreshold(this->m_WeightsQ, this->m_FractionNonZeroQ, this->m_KeepPositiveQ); |
| 6572 |
norm = this->m_WeightsQ.two_norm(); |
| 6573 |
this->m_WeightsQ = this->m_WeightsQ / (norm); |
| 6574 |
|
| 6575 |
this->m_WeightsR = |
| 6576 |
this->m_MatrixR.transpose() * (this->m_MatrixP * this->m_WeightsP + this->m_MatrixQ * this->m_WeightsQ); |
| 6577 |
this->ReSoftThreshold(this->m_WeightsR, this->m_FractionNonZeroR, this->m_KeepPositiveR); |
| 6578 |
norm = this->m_WeightsR.two_norm(); |
| 6579 |
this->m_WeightsR = this->m_WeightsR / (norm); |
| 6580 |
|
| 6581 |
VectorType pvec = this->m_MatrixP * this->m_WeightsP; |
| 6582 |
VectorType qvec = this->m_MatrixQ * this->m_WeightsQ; |
| 6583 |
VectorType rvec = this->m_MatrixR * this->m_WeightsR; |
| 6584 |
|
| 6585 |
double corrpq = this->PearsonCorr(pvec, qvec); |
| 6586 |
double corrpr = this->PearsonCorr(pvec, rvec); |
| 6587 |
double corrqr = this->PearsonCorr(rvec, qvec); |
| 6588 |
truecorr = corrpq + corrpr + corrqr; |
| 6589 |
deltacorr = fabs(truecorr - lastcorr); |
| 6590 |
lastcorr = truecorr; |
| 6591 |
// if ( ! this->m_Silent ) std::cout << " correlation of projections: pq " << corrpq << " pr " << corrpr << " qr " |
| 6592 |
// << corrqr << " at-it " << its << std::endl; |
| 6593 |
its++; |
| 6594 |
} |
| 6595 |
|
| 6596 |
// if ( ! this->m_Silent ) std::cout << " PNZ-Frac " << this->CountNonZero(this->m_WeightsP) << std::endl; |
| 6597 |
// if ( ! this->m_Silent ) std::cout << " QNZ-Frac " << this->CountNonZero(this->m_WeightsQ) << std::endl; |
| 6598 |
// if ( ! this->m_Silent ) std::cout << " RNZ-Frac " << this->CountNonZero(this->m_WeightsR) << std::endl; |
| 6599 |
|
| 6600 |
this->m_CorrelationForSignificanceTest = truecorr; |
| 6601 |
return truecorr; |
| 6602 |
} |
| 6603 |
|
| 6604 |
template <typename TInputImage, typename TRealType> |
| 6605 |
void |
| 6606 |
antsSCCANObject<TInputImage, TRealType>::MRFFilterVariateMatrix() |
| 6607 |
{ |
| 6608 |
// 1. compute the label for each voxel --- the label is the col + 1 |
| 6609 |
// recall this->m_VariatesP.set_size(this->m_MatrixP.cols(), n_vecs); |
| 6610 |
VectorType labels(this->m_MatrixP.cols(), 0); |
| 6611 |
VectorType maxweight(this->m_MatrixP.cols(), 0); |
| 6612 |
unsigned int n_vecs = this->m_VariatesP.cols(); |
| 6613 |
|
| 6614 |
for (unsigned int i = 0; i < this->m_MatrixP.cols(); i++) |
| 6615 |
{ |
| 6616 |
RealType maxval = 0; |
| 6617 |
VectorType pvec = this->m_VariatesP.get_row(i); |
| 6618 |
for (unsigned int j = 0; j < pvec.size(); j++) |
| 6619 |
{ /** FIXME should this be fabs or not? */ |
| 6620 |
// RealType val = fabs( this->m_VariatesP( j , i ) ); |
| 6621 |
RealType val = pvec(j); |
| 6622 |
if (val > maxval) |
| 6623 |
{ |
| 6624 |
maxval = val; |
| 6625 |
labels(i) = (j + 1); |
| 6626 |
maxweight(i) = val; |
| 6627 |
} |
| 6628 |
} |
| 6629 |
} |
| 6630 |
typename TInputImage::Pointer flabelimage = this->ConvertVariateToSpatialImage(labels, this->m_MaskImageP, false); |
| 6631 |
typename TInputImage::Pointer maxweightimage = |
| 6632 |
this->ConvertVariateToSpatialImage(maxweight, this->m_MaskImageP, false); |
| 6633 |
|
| 6634 |
typedef unsigned int LabelType; |
| 6635 |
typedef itk::Image<LabelType, ImageDimension> LabelImageType; |
| 6636 |
typename LabelImageType::Pointer labelimage = LabelImageType::New(); |
| 6637 |
labelimage->SetRegions(flabelimage->GetRequestedRegion()); |
| 6638 |
labelimage->CopyInformation(flabelimage); |
| 6639 |
labelimage->AllocateInitialized(); |
| 6640 |
typename LabelImageType::Pointer maskimage = LabelImageType::New(); |
| 6641 |
maskimage->SetRegions(flabelimage->GetRequestedRegion()); |
| 6642 |
maskimage->CopyInformation(flabelimage); |
| 6643 |
maskimage->AllocateInitialized(); |
| 6644 |
itk::ImageRegionConstIterator<TInputImage> Itf(flabelimage, flabelimage->GetLargestPossibleRegion()); |
| 6645 |
for (Itf.GoToBegin(); !Itf.IsAtEnd(); ++Itf) |
| 6646 |
{ |
| 6647 |
if (this->m_MaskImageP->GetPixel(Itf.GetIndex()) > 0) |
| 6648 |
{ |
| 6649 |
labelimage->SetPixel(Itf.GetIndex(), static_cast<LabelType>(Itf.Get() + 0.5)); |
| 6650 |
maskimage->SetPixel(Itf.GetIndex(), 1); |
| 6651 |
} |
| 6652 |
} |
| 6653 |
// simple MRF style update |
| 6654 |
for (unsigned int mrfct = 0; mrfct < 2; mrfct++) |
| 6655 |
{ |
| 6656 |
typedef itk::NeighborhoodIterator<LabelImageType> iteratorType; |
| 6657 |
typename iteratorType::RadiusType rad; |
| 6658 |
rad.Fill(1); |
| 6659 |
iteratorType GHood(rad, labelimage, labelimage->GetLargestPossibleRegion()); |
| 6660 |
GHood.GoToBegin(); |
| 6661 |
while (!GHood.IsAtEnd()) |
| 6662 |
{ |
| 6663 |
VectorType countlabels(n_vecs, 0); |
| 6664 |
LabelType p = GHood.GetCenterPixel(); |
| 6665 |
if (p >= 0.5 && this->m_MaskImageP->GetPixel(GHood.GetIndex()) >= 0.5) |
| 6666 |
{ |
| 6667 |
for (unsigned int i = 0; i < GHood.Size(); i++) |
| 6668 |
{ |
| 6669 |
LabelType p2 = GHood.GetPixel(i); |
| 6670 |
if (p2 > 0) |
| 6671 |
{ |
| 6672 |
countlabels[p2 - 1] = countlabels[p2 - 1] + 1; |
| 6673 |
} |
| 6674 |
} |
| 6675 |
LabelType jj = 0; |
| 6676 |
unsigned long maxlabcount = 0; |
| 6677 |
for (unsigned int i = 0; i < countlabels.size(); i++) |
| 6678 |
{ |
| 6679 |
if (countlabels[i] > maxlabcount) |
| 6680 |
{ |
| 6681 |
jj = i + 1; |
| 6682 |
maxlabcount = countlabels[i]; |
| 6683 |
} |
| 6684 |
} |
| 6685 |
GHood.SetCenterPixel(jj); |
| 6686 |
} |
| 6687 |
++GHood; |
| 6688 |
} |
| 6689 |
} // mrfct |
| 6690 |
|
| 6691 |
unsigned int vecind = 0; |
| 6692 |
for (Itf.GoToBegin(); !Itf.IsAtEnd(); ++Itf) |
| 6693 |
{ |
| 6694 |
if (this->m_MaskImageP->GetPixel(Itf.GetIndex()) >= 0.5) |
| 6695 |
{ |
| 6696 |
labels(vecind) = labelimage->GetPixel(Itf.GetIndex()); |
| 6697 |
vecind++; |
| 6698 |
} |
| 6699 |
} |
| 6700 |
|
| 6701 |
{ |
| 6702 |
typedef itk::ImageFileWriter<LabelImageType> WriterType; |
| 6703 |
typename WriterType::Pointer writer = WriterType::New(); |
| 6704 |
writer->SetInput(labelimage); |
| 6705 |
writer->SetFileName("label1.nii.gz"); |
| 6706 |
writer->Update(); |
| 6707 |
} |
| 6708 |
{ |
| 6709 |
typedef itk::ImageFileWriter<TInputImage> WriterType; |
| 6710 |
typename WriterType::Pointer writer = WriterType::New(); |
| 6711 |
writer->SetInput(maxweightimage); |
| 6712 |
writer->SetFileName("weight.nii.gz"); |
| 6713 |
writer->Update(); |
| 6714 |
} |
| 6715 |
for (unsigned int i = 0; i < this->m_MatrixP.cols(); i++) |
| 6716 |
{ |
| 6717 |
VectorType pvec = this->m_VariatesP.get_row(i); |
| 6718 |
LabelType lab = static_cast<LabelType>(labels(i) + 0.5); |
| 6719 |
if (lab > 0) |
| 6720 |
{ |
| 6721 |
lab = lab - 1; |
| 6722 |
} |
| 6723 |
for (unsigned int j = 0; j < pvec.size(); j++) |
| 6724 |
{ |
| 6725 |
if (j != lab) |
| 6726 |
{ |
| 6727 |
pvec(j) = 0; |
| 6728 |
} |
| 6729 |
} |
| 6730 |
pvec = pvec / pvec.two_norm(); |
| 6731 |
this->m_VariatesP.set_row(i, pvec); |
| 6732 |
} |
| 6733 |
|
| 6734 |
/* |
| 6735 |
|
| 6736 |
// 2. now do a simple MRF style update |
| 6737 |
typedef itk::ants::AtroposSegmentationImageFilter |
| 6738 |
<TInputImage, LabelImageType> SegmentationFilterType; |
| 6739 |
typename SegmentationFilterType::Pointer segmenter |
| 6740 |
= SegmentationFilterType::New(); |
| 6741 |
typename SegmentationFilterType::ArrayType radius; |
| 6742 |
radius.Fill( 1 ); |
| 6743 |
segmenter->SetMinimizeMemoryUsage( false ); |
| 6744 |
segmenter->SetNumberOfTissueClasses( this->m_VariatesP.cols() ); |
| 6745 |
segmenter->SetInitializationStrategy( SegmentationFilterType::PriorLabelImage ); |
| 6746 |
segmenter->SetPriorProbabilityWeight( 0.5 ); |
| 6747 |
segmenter->SetPriorLabelImage( labelimage ); |
| 6748 |
segmenter->SetPosteriorProbabilityFormulation( |
| 6749 |
SegmentationFilterType::Socrates ); |
| 6750 |
segmenter->SetMaximumNumberOfIterations( 5 ); |
| 6751 |
segmenter->SetConvergenceThreshold( 0 ); |
| 6752 |
segmenter->SetMaskImage( maskimage ); |
| 6753 |
|
| 6754 |
// Check to see that the labels in the prior label image or the non-zero |
| 6755 |
// probability voxels in the prior probability images encompass the entire |
| 6756 |
// mask region. |
| 6757 |
|
| 6758 |
if( segmenter->GetInitializationStrategy() == |
| 6759 |
SegmentationFilterType::PriorLabelImage ) |
| 6760 |
{ |
| 6761 |
itk::ImageRegionConstIterator<LabelImageType> ItM( segmenter->GetMaskImage(), |
| 6762 |
segmenter->GetMaskImage()->GetLargestPossibleRegion() ); |
| 6763 |
itk::ImageRegionConstIterator<LabelImageType> ItP( segmenter->GetPriorLabelImage(), |
| 6764 |
segmenter->GetPriorLabelImage()->GetLargestPossibleRegion() ); |
| 6765 |
for( ItM.GoToBegin(), ItP.GoToBegin(); !ItM.IsAtEnd(); ++ItM, ++ItP ) |
| 6766 |
{ |
| 6767 |
if( ItM.Get() == segmenter->GetMaskLabel() && ItP.Get() == 0 ) |
| 6768 |
{ |
| 6769 |
if ( ! this->m_Silent ) std::cout << std::endl; |
| 6770 |
if ( ! this->m_Silent ) std::cout << "Warning: the labels in the the prior label image do " |
| 6771 |
<< "not encompass the entire mask region. As a result each unlabeled voxel will be " |
| 6772 |
<< "initially assigned a random label. The user might want to consider " |
| 6773 |
<< "various alternative strategies like assigning an additional " |
| 6774 |
<< "\"background\" label to the unlabeled voxels or propagating " |
| 6775 |
<< "the labels within the mask region." |
| 6776 |
<< std::endl; |
| 6777 |
if ( ! this->m_Silent ) std::cout << std::endl; |
| 6778 |
break; |
| 6779 |
} |
| 6780 |
} |
| 6781 |
} |
| 6782 |
|
| 6783 |
segmenter->SetIntensityImage( 0 , maxweightimage ); |
| 6784 |
segmenter->SetMRFSmoothingFactor( 0.2 ); |
| 6785 |
segmenter->SetMRFRadius( radius ); |
| 6786 |
typedef typename SegmentationFilterType::SampleType SampleType; |
| 6787 |
typedef itk::ants::Statistics::GaussianListSampleFunction |
| 6788 |
<SampleType, RealType, RealType> LikelihoodType; |
| 6789 |
for( unsigned int n = 0; n < segmenter->GetNumberOfTissueClasses(); n++ ) |
| 6790 |
{ |
| 6791 |
typename LikelihoodType::Pointer gaussianLikelihood = |
| 6792 |
LikelihoodType::New(); |
| 6793 |
segmenter->SetLikelihoodFunction( n, gaussianLikelihood ); |
| 6794 |
} |
| 6795 |
segmenter->SetUsePartialVolumeLikelihoods( false ); |
| 6796 |
segmenter->Update(); |
| 6797 |
typename LabelImageType::Pointer labelimage2 = segmenter->GetOutput(); |
| 6798 |
{ |
| 6799 |
typedef itk::ImageFileWriter<LabelImageType> WriterType; |
| 6800 |
typename WriterType::Pointer writer = WriterType::New(); |
| 6801 |
writer->SetInput( labelimage2 ); |
| 6802 |
writer->SetFileName( "label2.nii.gz" ); |
| 6803 |
writer->Update(); |
| 6804 |
} |
| 6805 |
*/ |
| 6806 |
} |
| 6807 |
} // namespace ants |
| 6808 |
} // namespace itk |
| 6809 |
|
| 6810 |
/* |
| 6811 |
|
| 6812 |
if (its == 0 ) |
| 6813 |
if ( which_e_vec > 0 && false ) |
| 6814 |
{ |
| 6815 |
// here, factor out previous evecs globally |
| 6816 |
MatrixType temp; |
| 6817 |
MatrixType pp; |
| 6818 |
unsigned int basect=this->m_MatrixR.columns(); |
| 6819 |
basect=0; |
| 6820 |
pp.set_size(this->m_MatrixP.rows(),which_e_vec+basect); |
| 6821 |
// for (unsigned int kk=0; kk<this->m_MatrixR.columns(); kk++) |
| 6822 |
// pp.set_column(kk,this->m_MatrixR.get_column(kk)); |
| 6823 |
unsigned int colcount=0; //this->m_MatrixR.columns(); |
| 6824 |
for (unsigned int kk=which_e_vec-1; kk<which_e_vec; kk++) { |
| 6825 |
pp.set_column(colcount,this->m_MatrixP*this->m_VariatesP.get_column(kk)); |
| 6826 |
colcount++; |
| 6827 |
} |
| 6828 |
temp=this->NormalizeMatrix(pp); |
| 6829 |
temp=this->WhitenMatrix(temp); |
| 6830 |
temp=temp*temp.transpose(); |
| 6831 |
this->m_MatrixP=(this->m_MatrixP-temp*this->m_MatrixP); |
| 6832 |
|
| 6833 |
MatrixType qq; |
| 6834 |
qq.set_size(this->m_MatrixQ.rows(),which_e_vec+this->m_MatrixR.columns()); |
| 6835 |
for (unsigned int kk=0; kk<this->m_MatrixR.columns(); kk++) |
| 6836 |
qq.set_column(kk,this->m_MatrixR.get_column(kk)); |
| 6837 |
colcount=this->m_MatrixR.columns(); |
| 6838 |
for (unsigned int kk=which_e_vec-1; kk<which_e_vec; kk++) { |
| 6839 |
qq.set_column(colcount,this->m_MatrixQ*this->m_VariatesQ.get_column(kk)); |
| 6840 |
colcount++; |
| 6841 |
} |
| 6842 |
temp=this->NormalizeMatrix(qq); |
| 6843 |
temp=this->WhitenMatrix(temp); |
| 6844 |
temp=temp*temp.transpose(); |
| 6845 |
this->m_MatrixQ=(this->m_MatrixQ-temp*this->m_MatrixQ); |
| 6846 |
} |
| 6847 |
|
| 6848 |
|
| 6849 |
|
| 6850 |
|
| 6851 |
//m_Debug=true; |
| 6852 |
double ip=1; unsigned long ct=0,max_ip_its=50; |
| 6853 |
double deltaip=1,lastip=0; |
| 6854 |
while ( (deltaip) > 1.e-3 && ct < max_ip_its && which_e_vec > 0 || ct < 4 ) { |
| 6855 |
ip=0; |
| 6856 |
this->ReSoftThreshold( this->m_WeightsP , this->m_FractionNonZeroP , this->m_KeepPositiveP ); |
| 6857 |
VectorType ptem=this->m_WeightsP; |
| 6858 |
if ( which_e_vec >= 1 ) |
| 6859 |
ptem=this->Orthogonalize(ptem,this->m_VariatesP.get_column(0),&this->m_MatrixP); |
| 6860 |
if ( which_e_vec >= 2 ) |
| 6861 |
ptem=this->Orthogonalize(ptem,this->m_VariatesP.get_column(1),&this->m_MatrixP); |
| 6862 |
this->m_WeightsP=ptem; |
| 6863 |
this->ReSoftThreshold( this->m_WeightsP , this->m_FractionNonZeroP , this->m_KeepPositiveP ); |
| 6864 |
ip+=this->PearsonCorr(this->m_MatrixP*this->m_WeightsP,this->m_MatrixP*this->m_VariatesP.get_column(0)); |
| 6865 |
if ( which_e_vec >= 2) |
| 6866 |
ip+=this->PearsonCorr(this->m_MatrixP*this->m_WeightsP,this->m_MatrixP*this->m_VariatesP.get_column(1)); |
| 6867 |
deltaip=fabs(lastip)-fabs(ip); |
| 6868 |
lastip=ip; |
| 6869 |
ct++; |
| 6870 |
if ( this->m_Debug ) if ( ! this->m_Silent ) std::cout << " pip-b " << ip << " delt " << deltaip << std::endl; |
| 6871 |
} |
| 6872 |
|
| 6873 |
ip=1; ct=0; |
| 6874 |
deltaip=1;lastip=0; |
| 6875 |
while ( (deltaip) > 1.e-3 && ct < max_ip_its && which_e_vec > 0 || ct < 4 ) { |
| 6876 |
ip=0; |
| 6877 |
this->ReSoftThreshold( this->m_WeightsQ , this->m_FractionNonZeroQ , this->m_KeepPositiveQ ); |
| 6878 |
VectorType ptem=this->m_WeightsQ; |
| 6879 |
if ( which_e_vec >= 1 ) |
| 6880 |
ptem=this->Orthogonalize(ptem,this->m_VariatesQ.get_column(0),&this->m_MatrixQ); |
| 6881 |
if ( which_e_vec >= 2 ) |
| 6882 |
ptem=this->Orthogonalize(ptem,this->m_VariatesQ.get_column(1),&this->m_MatrixQ); |
| 6883 |
this->m_WeightsQ=ptem; |
| 6884 |
this->ReSoftThreshold( this->m_WeightsQ , this->m_FractionNonZeroQ , this->m_KeepPositiveQ ); |
| 6885 |
ip+=this->PearsonCorr(this->m_MatrixQ*this->m_WeightsQ,this->m_MatrixQ*this->m_VariatesQ.get_column(0)); |
| 6886 |
if ( which_e_vec >= 2) |
| 6887 |
ip+=this->PearsonCorr(this->m_MatrixQ*this->m_WeightsQ,this->m_MatrixQ*this->m_VariatesQ.get_column(1)); |
| 6888 |
deltaip=fabs(lastip)-fabs(ip); |
| 6889 |
lastip=ip; |
| 6890 |
ct++; |
| 6891 |
if ( this->m_Debug ) if ( ! this->m_Silent ) std::cout << " qip-b " << ip << " delt " << deltaip << std::endl; |
| 6892 |
} |
| 6893 |
|
| 6894 |
|
| 6895 |
// alternative tools for factoring out evecs |
| 6896 |
// if ( its == 0 && which_e_vec > 0 ) { |
| 6897 |
// this->WhitenDataSetForRunSCCANMultiple(); |
| 6898 |
q_evecs_factor=this->m_MatrixQ*this->m_VariatesQ.get_n_columns(0,which_e_vec); |
| 6899 |
q_evecs_factor=this->NormalizeMatrix( q_evecs_factor ); |
| 6900 |
q_evecs_factor=this->WhitenMatrix(q_evecs_factor); |
| 6901 |
q_evecs_factor=q_evecs_factor*q_evecs_factor.transpose(); |
| 6902 |
// MatrixType temp=this->m_MatrixP-q_evecs_factor*this->m_MatrixP; |
| 6903 |
// temp=this->InverseCovarianceMatrix(temp,&this->m_MatrixP); |
| 6904 |
// this->m_MatrixP=temp; |
| 6905 |
|
| 6906 |
p_evecs_factor=this->m_MatrixP*this->m_VariatesP.get_n_columns(0,which_e_vec); |
| 6907 |
p_evecs_factor=this->NormalizeMatrix( p_evecs_factor ); |
| 6908 |
p_evecs_factor=this->WhitenMatrix(p_evecs_factor); |
| 6909 |
p_evecs_factor=p_evecs_factor*p_evecs_factor.transpose(); |
| 6910 |
// temp=this->m_MatrixQ-p_evecs_factor*this->m_MatrixQ; |
| 6911 |
// temp=this->InverseCovarianceMatrix(temp,&this->m_MatrixQ); |
| 6912 |
// this->m_MatrixQ=temp; |
| 6913 |
|
| 6914 |
} |
| 6915 |
|
| 6916 |
*/ |
| 6917 |
|
| 6918 |
/* |
| 6919 |
|
| 6920 |
bool precond = true; |
| 6921 |
MatrixType Cinv; |
| 6922 |
if ( precond ) |
| 6923 |
{ |
| 6924 |
// Preconditioned Conjugate Gradient Method |
| 6925 |
// Tuesday 25 July 2006, by Nadir SOUALEM |
| 6926 |
if ( ! this->m_Silent ) std::cout << " begin chol " << std::endl; |
| 6927 |
MatrixType AAt = A * A.transpose() ; |
| 6928 |
MatrixType kcovmat=this->VNLPseudoInverse( this->m_VariatesP.transpose()*this->m_VariatesP |
| 6929 |
vnl_ldl_cholesky chol( AAt ); |
| 6930 |
if ( ! this->m_Silent ) std::cout << " done chol " << std::endl; |
| 6931 |
MatrixType chollow = chol.lower_triangle(); |
| 6932 |
vnl_diag_matrix<double> diag( chol.diagonal() ); |
| 6933 |
vnl_diag_matrix<double> diaginv( chol.diagonal() ); |
| 6934 |
for ( unsigned int i = 0; i < diag.cols(); i++ ) |
| 6935 |
if ( diaginv(i,i) > 1.e-6 ) |
| 6936 |
{ |
| 6937 |
diaginv = 1.0 / diaginv( i , i ); |
| 6938 |
chollow( i , i ) = chollow( i , i ) + diag( i, i ); |
| 6939 |
} |
| 6940 |
else diaginv(i,i) = diag(i,i) = 0; |
| 6941 |
if ( ! this->m_Silent ) std::cout << " precon " << std::endl; |
| 6942 |
// preconditioner |
| 6943 |
MatrixType temp = ( chollow * diaginv ) * chollow.transpose(); |
| 6944 |
Cinv = vnl_matrix_inverse<double>( temp ); |
| 6945 |
if ( ! this->m_Silent ) std::cout << " precon done " << std::endl; |
| 6946 |
A = Cinv * A; |
| 6947 |
if ( ! this->m_Silent ) std::cout << " got A precond " << std::endl; |
| 6948 |
} |
| 6949 |
|
| 6950 |
|
| 6951 |
bool dosvdinit = true; |
| 6952 |
if ( n_vecs > ( this->m_MatrixP.rows() - 1 ) ) dosvdinit = false; |
| 6953 |
if ( dosvdinit ) |
| 6954 |
{ |
| 6955 |
MatrixType cov = this->m_MatrixP * this->m_MatrixP.transpose(); |
| 6956 |
vnl_svd<double> qr( cov ); |
| 6957 |
matrixB = qr.U().extract( this->m_MatrixP.rows(), n_vecs, 0, 0); |
| 6958 |
this->m_VariatesP = this->m_MatrixP.transpose( ) * matrixB; |
| 6959 |
for( unsigned int i = 0; i < n_vecs; i++ ) |
| 6960 |
{ |
| 6961 |
VectorType evec = this->m_VariatesP.get_column( i ); |
| 6962 |
this->SparsifyP( evec ); |
| 6963 |
this->m_VariatesP.set_column( i, evec ); |
| 6964 |
} |
| 6965 |
} |
| 6966 |
else |
| 6967 |
{ |
| 6968 |
|
| 6969 |
|
| 6970 |
*/ |