1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352
|
/*=========================================================================
Program: Advanced Normalization Tools
Module: $RCSfile: antsMatrixUtilities.h,v $
Language: C++
Date: $Date: $
Version: $Revision: $
Copyright (c) ConsortiumOfANTS. All rights reserved.
See accompanying COPYING.txt or
http://sourceforge.net/projects/advants/files/ANTS/ANTSCopyright.txt
for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notices for more information.
=========================================================================*/
#ifndef __antsMatrixUtilities_h
#define __antsMatrixUtilities_h
#include <vnl/algo/vnl_matrix_inverse.h>
#include <vnl/algo/vnl_cholesky.h>
#include "itkImageToImageFilter.h"
namespace itk
{
namespace ants
{
template <class TInputImage, class TRealType = double>
class antsMatrixUtilities :
public ImageToImageFilter<TInputImage, TInputImage>
{
public:
/** Standard class typdedefs. */
typedef antsMatrixUtilities Self;
typedef ImageToImageFilter<TInputImage, TInputImage> Superclass;
typedef SmartPointer<Self> Pointer;
typedef SmartPointer<const Self> ConstPointer;
/** Method for creation through the object factory. */
itkNewMacro( Self );
/** Run-time type information (and related methods). */
itkTypeMacro( antsMatrixUtilities, ImageToImageFilter );
/** Dimension of the images. */
itkStaticConstMacro( ImageDimension, unsigned int,
TInputImage::ImageDimension );
itkStaticConstMacro( MatrixDimension, unsigned int, 2 );
/** Typedef support of input types. */
typedef TInputImage ImageType;
typedef typename ImageType::Pointer ImagePointer;
typedef typename ImageType::PixelType PixelType;
typedef typename ImageType::IndexType IndexType;
/** Some convenient typedefs. */
typedef TRealType RealType;
typedef Image<RealType,
itkGetStaticConstMacro( ImageDimension )> RealImageType;
/** note, eigen for pseudo-eigenvals */
typedef vnl_matrix<RealType> MatrixType;
typedef vnl_vector<RealType> VectorType;
typedef MatrixType VariateType;
typedef vnl_diag_matrix<RealType> DiagonalMatrixType;
void NormalizeWeightsByCovariance();
void SetPseudoInversePercentVariance( RealType p )
{
this->m_PercentVarianceForPseudoInverse = p;
}
MatrixType PseudoInverse( MatrixType p_in, bool take_sqrt = false )
{
return this->VNLPseudoInverse( p_in, take_sqrt );
}
MatrixType VNLPseudoInverse( MatrixType, bool take_sqrt = false );
VectorType Orthogonalize(VectorType Mvec, VectorType V, MatrixType* projecterM = NULL, MatrixType* projecterV =
NULL )
{
if( !projecterM && !projecterV )
{
double ratio = inner_product(Mvec, V) / inner_product(V, V);
VectorType ortho = Mvec - V * ratio;
return ortho;
}
else if( !projecterM && projecterV )
{
double ratio = inner_product(Mvec, *projecterV * V) / inner_product(*projecterV * V, *projecterV * V);
VectorType ortho = Mvec - V * ratio;
return ortho;
}
else if( !projecterV && projecterM )
{
double ratio = inner_product(*projecterM * Mvec, V) / inner_product(V, V);
VectorType ortho = (*projecterM * Mvec) - V * ratio;
return ortho;
}
else
{
double ratio = inner_product(*projecterM * Mvec, *projecterV * V) / inner_product(*projecterV * V,
*projecterV * V);
VectorType ortho = Mvec - V * ratio;
return ortho;
}
}
MatrixType OrthogonalizeMatrix(MatrixType M, VectorType V )
{
for( unsigned int j = 0; j < M.cols(); j++ )
{
VectorType Mvec = M.get_column(j);
double ratio = inner_product(Mvec, V) / inner_product(V, V);
VectorType ortho = Mvec - V * ratio;
M.set_column(j, ortho);
}
return M;
}
void SetMaskImageP( ImagePointer mask )
{
this->m_MaskImageP = mask;
}
void SetMatrixP( MatrixType matrix )
{
this->m_OriginalMatrixP.set_size(matrix.rows(), matrix.cols() ); this->m_MatrixP.set_size(
matrix.rows(), matrix.cols() ); this->m_OriginalMatrixP.update(matrix); this->m_MatrixP.update(matrix);
}
itkSetMacro( FractionNonZeroQ, RealType );
itkSetMacro( KeepPositiveQ, bool );
void SetMaskImageQ( ImagePointer mask )
{
this->m_MaskImageQ = mask;
}
void SetMatrixQ( MatrixType matrix )
{
this->m_OriginalMatrixQ.set_size(matrix.rows(), matrix.cols() ); this->m_MatrixQ.set_size(
matrix.rows(), matrix.cols() ); this->m_OriginalMatrixQ.update(matrix); this->m_MatrixQ.update(matrix);
}
itkSetMacro( FractionNonZeroR, RealType );
itkSetMacro( KeepPositiveR, bool );
void SetMaskImageR( ImagePointer mask )
{
this->m_MaskImageR = mask;
}
void SetMatrixR( MatrixType matrix )
{
this->m_OriginalMatrixR.set_size(matrix.rows(), matrix.cols() ); this->m_MatrixR.set_size(
matrix.rows(), matrix.cols() ); this->m_OriginalMatrixR.update(matrix); this->m_MatrixR.update(matrix);
}
MatrixType GetMatrixP()
{
return this->m_MatrixP;
}
MatrixType GetMatrixQ()
{
return this->m_MatrixQ;
}
MatrixType GetMatrixR()
{
return this->m_MatrixR;
}
MatrixType GetOriginalMatrixP()
{
return this->m_OriginalMatrixP;
}
MatrixType GetOriginalMatrixQ()
{
return this->m_OriginalMatrixQ;
}
MatrixType GetOriginalMatrixR()
{
return this->m_OriginalMatrixR;
}
VectorType InitializeV( MatrixType p );
MatrixType NormalizeMatrix(MatrixType p);
MatrixType CovarianceMatrix(MatrixType p, RealType regularization = 1.e-2 )
{
if( p.rows() < p.columns() )
{
MatrixType invcov = p * p.transpose();
invcov.set_identity();
invcov = invcov * regularization + p * p.transpose();
return invcov;
}
else
{
MatrixType invcov = p.transpose() * p;
invcov.set_identity();
invcov = invcov * regularization + p.transpose() * p;
return invcov;
}
}
VectorType GetCovMatEigenvector( MatrixType p, unsigned int evec );
MatrixType GetCovMatEigenvectors( MatrixType p );
VectorType AverageColumns( MatrixType p )
{
unsigned int ncol = p.columns();
VectorType v = p.get_column(0);
v.fill(0);
for( unsigned int i = 0; i < ncol; i++ )
{
v = v + p.get_column(i);
}
return v / (RealType)ncol;
}
MatrixType WhitenMatrix(MatrixType p, RealType regularization = 1.e-2 )
{
MatrixType invcov = this->CovarianceMatrix(p, regularization);
invcov = this->PseudoInverse( invcov, true );
if( p.rows() < p.columns() )
{
return invcov * p;
}
else
{
return p * invcov;
}
}
MatrixType WhitenMatrixByAnotherMatrix(MatrixType p, MatrixType op, RealType regularization = 1.e-2)
{
MatrixType invcov = this->CovarianceMatrix(op, regularization);
invcov = this->PseudoInverse( invcov, true );
if( p.rows() < p.columns() )
{
return invcov * p;
}
else
{
return p * invcov;
}
}
MatrixType ProjectionMatrix(MatrixType b)
{
b = this->NormalizeMatrix(b);
b = this->WhitenMatrix(b);
return b * b.transpose();
}
MatrixType DeleteCol( MatrixType p_in, unsigned int col)
{
unsigned int ncols = p_in.cols() - 1;
if( col >= ncols )
{
ncols = p_in.cols();
}
MatrixType p(p_in.rows(), ncols);
unsigned int colct = 0;
for( long i = 0; i < p.cols(); ++i ) // loop over cols
{
if( i != col )
{
p.set_column(colct, p_in.get_column(i) );
colct++;
}
}
return p;
}
RealType PearsonCorr(VectorType v1, VectorType v2 )
{
double xysum = 0;
for( unsigned int i = 0; i < v1.size(); i++ )
{
xysum += v1(i) * v2(i);
}
double frac = 1.0 / (double)v1.size();
double xsum = v1.sum(), ysum = v2.sum();
double xsqr = v1.squared_magnitude();
double ysqr = v2.squared_magnitude();
double numer = xysum - frac * xsum * ysum;
double denom = sqrt( ( xsqr - frac * xsum * xsum) * ( ysqr - frac * ysum * ysum) );
if( denom <= 0 )
{
return 0;
}
return numer / denom;
}
antsMatrixUtilities();
~antsMatrixUtilities()
{
}
void PrintSelf( std::ostream& os, Indent indent ) const ITK_OVERRIDE
{
os << indent;
}
private:
bool m_Debug;
MatrixType m_OriginalMatrixP;
MatrixType m_OriginalMatrixQ;
MatrixType m_OriginalMatrixR;
antsMatrixUtilities(const Self &); // purposely not implemented
void operator=(const Self &); // purposely not implemented
RealType m_PinvTolerance;
RealType m_PercentVarianceForPseudoInverse;
MatrixType m_MatrixP;
ImagePointer m_MaskImageP;
RealType m_FractionNonZeroP;
bool m_KeepPositiveP;
MatrixType m_MatrixQ;
ImagePointer m_MaskImageQ;
RealType m_FractionNonZeroQ;
bool m_KeepPositiveQ;
MatrixType m_MatrixR;
ImagePointer m_MaskImageR;
RealType m_FractionNonZeroR;
bool m_KeepPositiveR;
};
} // namespace ants
} // namespace itk
#ifndef ITK_MANUAL_INSTANTIATION
#include "antsMatrixUtilities.hxx"
#endif
#endif
|