Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ENH: Suppress "Bad Rotation Matrix" warnings #1697

Merged
merged 2 commits into from
Mar 19, 2024

Conversation

dzenanz
Copy link
Contributor

@dzenanz dzenanz commented Mar 12, 2024

This warning show up a lot for me (e.g. from here). Should I be concerned?

Example of occurrence:

Progress  | 0 | 0.01 | 0.0345 | 0.059 | 0.0835 | 0.108 | 0.1325 | 0.157 | 0.1815 | 0.206
 | 0.2305 | 0.255WARNING: In C:\Dev\ANTsWasm\VS22py\_deps\ants_inner-src\ImageRegistration\itkANTSCenteredAffine2DTransform.hxx, line 138
ANTSCenteredAffine2DTransform (000002581E7CA7F0): Bad Rotation Matrix 1.02555 -0.00607706 0.00711725 1.02636

WARNING: In C:\Dev\ANTsWasm\VS22py\_deps\ants_inner-src\ImageRegistration\itkANTSCenteredAffine2DTransform.hxx, line 138 ANTSCenteredAffine2DTransform (000002581E930950): Bad Rotation Matrix 1.0038 0.000658097 0.0181835 1.0196

WARNING: In C:\Dev\ANTsWasm\VS22py\_deps\ants_inner-src\ImageRegistration\itkANTSCenteredAffine2DTransform.hxx, line 138 ANTSCenteredAffine2DTransform (000002581E8193D0): Bad Rotation Matrix 1.01296 -0.000485504 0.00252202 0.99968

WARNING: In C:\Dev\ANTsWasm\VS22py\_deps\ants_inner-src\ImageRegistration\itkANTSCenteredAffine2DTransform.hxx, line 138 ANTSCenteredAffine2DTransform (000002581F07ABF0): Bad Rotation Matrix 1.00139 -0.0353489 0.014181 0.954691

WARNING: In C:\Dev\ANTsWasm\VS22py\_deps\ants_inner-src\ImageRegistration\itkANTSCenteredAffine2DTransform.hxx, line 138 ANTSCenteredAffine2DTransform (000002581E8F5140): Bad Rotation Matrix 0.94323 0.00672548 -0.0293821 1.00311

 | 0.2795 | 0.304 | 0.3285 | 0.353 | 0.3775 | 0.402 | 0.4265 | 0.451
 | 0.4755 | 0.5WARNING: In C:\Dev\ANTsWasm\VS22py\_deps\ants_inner-src\ImageRegistration\itkANTSCenteredAffine2DTransform.hxx, line 138
ANTSCenteredAffine2DTransform (000002581EC262E0): Bad Rotation Matrix 1.01756 -0.00261415 0.0241831 1.00784

WARNING: In C:\Dev\ANTsWasm\VS22py\_deps\ants_inner-src\ImageRegistration\itkANTSCenteredAffine2DTransform.hxx, line 138 ANTSCenteredAffine2DTransform (000002581EC265A0): Bad Rotation Matrix 1.05521 -0.0124403 0.0215943 0.991163

WARNING: In C:\Dev\ANTsWasm\VS22py\_deps\ants_inner-src\ImageRegistration\itkANTSCenteredAffine2DTransform.hxx, line 138 ANTSCenteredAffine2DTransform (000002581F29B710): Bad Rotation Matrix 0.943761 -0.00148547 -0.0278611 1.00295

 | 0.5245 | 0.549 | 0.5735 | 0.598 | 0.6225 | 0.647 | 0.6715 | 0.696
 | 0.7205 | 0.745WARNING: In C:\Dev\ANTsWasm\VS22py\_deps\ants_inner-src\ImageRegistration\itkANTSCenteredAffine2DTransform.hxx, line 138
ANTSCenteredAffine2DTransform (000002581EF693A0): Bad Rotation Matrix 1.02405 -0.00516537 0.0336307 1.01107

WARNING: In C:\Dev\ANTsWasm\VS22py\_deps\ants_inner-src\ImageRegistration\itkANTSCenteredAffine2DTransform.hxx, line 138 ANTSCenteredAffine2DTransform (000002581EF69BE0): Bad Rotation Matrix 1.04952 -0.0166267 0.00826341 0.983876

WARNING: In C:\Dev\ANTsWasm\VS22py\_deps\ants_inner-src\ImageRegistration\itkANTSCenteredAffine2DTransform.hxx, line 138 ANTSCenteredAffine2DTransform (000002581EF69D40): Bad Rotation Matrix 0.946308 -0.0141908 -0.0155272 1.0036

 | 0.7695 | 0.794 | 0.8185 | 0.843 | 0.8675 | 0.892 | 0.9165 | 0.941
 | 0.9655 | 0.99WARNING: In C:\Dev\ANTsWasm\VS22py\_deps\ants_inner-src\ImageRegistration\itkANTSCenteredAffine2DTransform.hxx, line 138
ANTSCenteredAffine2DTransform (000002581EDCFCE0): Bad Rotation Matrix 1.02332 -0.0042626 0.0304406 1.01458

WARNING: In C:\Dev\ANTsWasm\VS22py\_deps\ants_inner-src\ImageRegistration\itkANTSCenteredAffine2DTransform.hxx, line 138 ANTSCenteredAffine2DTransform (000002581EDD07E0): Bad Rotation Matrix 1.04445 -0.00741455 0.0214106 0.99242

WARNING: In C:\Dev\ANTsWasm\VS22py\_deps\ants_inner-src\ImageRegistration\itkANTSCenteredAffine2DTransform.hxx, line 138 ANTSCenteredAffine2DTransform (000002581EDD0680): Bad Rotation Matrix 0.955261 -0.0237769 -0.00559731 0.994758

 | 1
Filter took 147.606 seconds.

Should we try to retain these warnings in command-line tools?

Example of occurrence:

Progress  | 0 | 0.01 | 0.0345 | 0.059 | 0.0835 | 0.108 | 0.1325 | 0.157 | 0.1815 | 0.206
 | 0.2305 | 0.255WARNING: In C:\Dev\ANTsWasm\VS22py\_deps\ants_inner-src\ImageRegistration\itkANTSCenteredAffine2DTransform.hxx, line 138
ANTSCenteredAffine2DTransform (000002581E7CA7F0): Bad Rotation Matrix 1.02555 -0.00607706
0.00711725 1.02636

WARNING: In C:\Dev\ANTsWasm\VS22py\_deps\ants_inner-src\ImageRegistration\itkANTSCenteredAffine2DTransform.hxx, line 138
ANTSCenteredAffine2DTransform (000002581E930950): Bad Rotation Matrix 1.0038 0.000658097
0.0181835 1.0196

WARNING: In C:\Dev\ANTsWasm\VS22py\_deps\ants_inner-src\ImageRegistration\itkANTSCenteredAffine2DTransform.hxx, line 138
ANTSCenteredAffine2DTransform (000002581E8193D0): Bad Rotation Matrix 1.01296 -0.000485504
0.00252202 0.99968

WARNING: In C:\Dev\ANTsWasm\VS22py\_deps\ants_inner-src\ImageRegistration\itkANTSCenteredAffine2DTransform.hxx, line 138
ANTSCenteredAffine2DTransform (000002581F07ABF0): Bad Rotation Matrix 1.00139 -0.0353489
0.014181 0.954691

WARNING: In C:\Dev\ANTsWasm\VS22py\_deps\ants_inner-src\ImageRegistration\itkANTSCenteredAffine2DTransform.hxx, line 138
ANTSCenteredAffine2DTransform (000002581E8F5140): Bad Rotation Matrix 0.94323 0.00672548
-0.0293821 1.00311

 | 0.2795 | 0.304 | 0.3285 | 0.353 | 0.3775 | 0.402 | 0.4265 | 0.451
 | 0.4755 | 0.5WARNING: In C:\Dev\ANTsWasm\VS22py\_deps\ants_inner-src\ImageRegistration\itkANTSCenteredAffine2DTransform.hxx, line 138
ANTSCenteredAffine2DTransform (000002581EC262E0): Bad Rotation Matrix 1.01756 -0.00261415
0.0241831 1.00784

WARNING: In C:\Dev\ANTsWasm\VS22py\_deps\ants_inner-src\ImageRegistration\itkANTSCenteredAffine2DTransform.hxx, line 138
ANTSCenteredAffine2DTransform (000002581EC265A0): Bad Rotation Matrix 1.05521 -0.0124403
0.0215943 0.991163

WARNING: In C:\Dev\ANTsWasm\VS22py\_deps\ants_inner-src\ImageRegistration\itkANTSCenteredAffine2DTransform.hxx, line 138
ANTSCenteredAffine2DTransform (000002581F29B710): Bad Rotation Matrix 0.943761 -0.00148547
-0.0278611 1.00295

 | 0.5245 | 0.549 | 0.5735 | 0.598 | 0.6225 | 0.647 | 0.6715 | 0.696
 | 0.7205 | 0.745WARNING: In C:\Dev\ANTsWasm\VS22py\_deps\ants_inner-src\ImageRegistration\itkANTSCenteredAffine2DTransform.hxx, line 138
ANTSCenteredAffine2DTransform (000002581EF693A0): Bad Rotation Matrix 1.02405 -0.00516537
0.0336307 1.01107

WARNING: In C:\Dev\ANTsWasm\VS22py\_deps\ants_inner-src\ImageRegistration\itkANTSCenteredAffine2DTransform.hxx, line 138
ANTSCenteredAffine2DTransform (000002581EF69BE0): Bad Rotation Matrix 1.04952 -0.0166267
0.00826341 0.983876

WARNING: In C:\Dev\ANTsWasm\VS22py\_deps\ants_inner-src\ImageRegistration\itkANTSCenteredAffine2DTransform.hxx, line 138
ANTSCenteredAffine2DTransform (000002581EF69D40): Bad Rotation Matrix 0.946308 -0.0141908
-0.0155272 1.0036

 | 0.7695 | 0.794 | 0.8185 | 0.843 | 0.8675 | 0.892 | 0.9165 | 0.941
 | 0.9655 | 0.99WARNING: In C:\Dev\ANTsWasm\VS22py\_deps\ants_inner-src\ImageRegistration\itkANTSCenteredAffine2DTransform.hxx, line 138
ANTSCenteredAffine2DTransform (000002581EDCFCE0): Bad Rotation Matrix 1.02332 -0.0042626
0.0304406 1.01458

WARNING: In C:\Dev\ANTsWasm\VS22py\_deps\ants_inner-src\ImageRegistration\itkANTSCenteredAffine2DTransform.hxx, line 138
ANTSCenteredAffine2DTransform (000002581EDD07E0): Bad Rotation Matrix 1.04445 -0.00741455
0.0214106 0.99242

WARNING: In C:\Dev\ANTsWasm\VS22py\_deps\ants_inner-src\ImageRegistration\itkANTSCenteredAffine2DTransform.hxx, line 138
ANTSCenteredAffine2DTransform (000002581EDD0680): Bad Rotation Matrix 0.955261 -0.0237769
-0.00559731 0.994758

 | 1
Filter took 147.606 seconds.
@cookpa cookpa requested a review from ntustison March 12, 2024 20:56
@dzenanz
Copy link
Contributor Author

dzenanz commented Mar 15, 2024

Ping!

@cookpa
Copy link
Member

cookpa commented Mar 15, 2024

It looks like it's not able to get a pure rotation matrix from the transform, which seems bad but I don't know for sure why this is happening.

If you register the same data with antsRegistration, do you see these errors?

@dzenanz
Copy link
Contributor Author

dzenanz commented Mar 15, 2024

I am using Faces example. The warning gets triggered by the original script too:

dzenan@corista:~/ANTsWasm/TemplateBuildingExample/Faces$ ANTSPATH=/home/dzenan/ANTs/Scripts ./templateCommand.sh > run.log
WARNING: In /home/dzenan/ANTs/ImageRegistration/itkANTSCenteredAffine2DTransform.hxx, line 138
ANTSCenteredAffine2DTransform (0x55c8f7d4ffc0): Bad Rotation Matrix 1.02839 -0.0157299
0.0204534 1.0099


WARNING: In /home/dzenan/ANTs/ImageRegistration/itkANTSCenteredAffine2DTransform.hxx, line 138
ANTSCenteredAffine2DTransform (0x55c8f7d504f0): Bad Rotation Matrix 1.04678 0.00573631
0.00611402 0.981415


WARNING: In /home/dzenan/ANTs/ImageRegistration/itkANTSCenteredAffine2DTransform.hxx, line 138
ANTSCenteredAffine2DTransform (0x55c8f7d509d0): Bad Rotation Matrix 1.03557 -0.01256
0.0241544 1.02507


WARNING: In /home/dzenan/ANTs/ImageRegistration/itkANTSCenteredAffine2DTransform.hxx, line 138
ANTSCenteredAffine2DTransform (0x55c8f7d51120): Bad Rotation Matrix 0.972547 -0.00227736
-0.00214852 0.945171


WARNING: In /home/dzenan/ANTs/ImageRegistration/itkANTSCenteredAffine2DTransform.hxx, line 138
ANTSCenteredAffine2DTransform (0x55c8f7d51390): Bad Rotation Matrix 0.946277 0.00597829
-0.0209535 0.992051


WARNING: In /home/dzenan/ANTs/ImageRegistration/itkANTSCenteredAffine2DTransform.hxx, line 138
ANTSCenteredAffine2DTransform (0x55c3ff2f3fc0): Bad Rotation Matrix 1.02244 -0.0224988
0.0328645 1.02861


WARNING: In /home/dzenan/ANTs/ImageRegistration/itkANTSCenteredAffine2DTransform.hxx, line 138
ANTSCenteredAffine2DTransform (0x55c3ff2f5120): Bad Rotation Matrix 0.963474 0.0117549
-0.0205578 0.954667


WARNING: In /home/dzenan/ANTs/ImageRegistration/itkANTSCenteredAffine2DTransform.hxx, line 138
ANTSCenteredAffine2DTransform (0x55c3ff2f5390): Bad Rotation Matrix 0.948863 -0.0052806
-0.00569918 1.00631


WARNING: In /home/dzenan/ANTs/ImageRegistration/itkANTSCenteredAffine2DTransform.hxx, line 138
ANTSCenteredAffine2DTransform (0x562b801c4fc0): Bad Rotation Matrix 1.02179 -0.0224462
0.0431247 1.03127


WARNING: In /home/dzenan/ANTs/ImageRegistration/itkANTSCenteredAffine2DTransform.hxx, line 138
ANTSCenteredAffine2DTransform (0x562b801c6120): Bad Rotation Matrix 0.964812 0.00868409
-0.0161593 0.94557


WARNING: In /home/dzenan/ANTs/ImageRegistration/itkANTSCenteredAffine2DTransform.hxx, line 138
ANTSCenteredAffine2DTransform (0x562b801c6390): Bad Rotation Matrix 0.951257 0.00278727
-0.0111354 1.0092

@cookpa
Copy link
Member

cookpa commented Mar 18, 2024

@ntustison @stnava do you know if these warnings are harmless?

@stnava
Copy link
Member

stnava commented Mar 18, 2024

I dont know - are these matrices stored in .txt? my recollection is that the matrices will get projected to the closest orthogonal .... so potentially low impact if not harmless at least in this context

@dzenanz
Copy link
Contributor Author

dzenanz commented Mar 18, 2024

These are intermediate affine matrices which are getting averaged during groupwise registration.

@cookpa
Copy link
Member

cookpa commented Mar 18, 2024

Here's the function that produces the error

/** Compute the Angle from the Rotation Matrix */
template <typename TScalarType>
void
ANTSCenteredAffine2DTransform<TScalarType>::ComputeMatrixParameters(void)
{
typedef vnl_matrix_fixed<TScalarType, 2U, 2U> TMatrix;
const TMatrix A{ this->GetMatrix().GetVnlMatrix() };
vnl_qr<ScalarType> myqr(A.as_matrix());
TMatrix R = myqr.Q(); // Q() is the rotation
TMatrix Q = myqr.R(); // R() is the upper triangluar
TMatrix dq(itk::NumericTraits<TScalarType>::ZeroValue());
for (unsigned i = 0; i < 2; i++)
{
dq(i, i) = (Q(i, i) >= 0) ? 1 : -1;
}
R = R * dq;
Q = dq * Q;
// std::cout << "A=" << A << std::endl;
// std::cout << "Q=" << Q << std::endl;
// std::cout << "R=" << R << std::endl;
// std::cout << "dq=" << dq << std::endl;
m_Angle = std::acos(R[0][0]);
if (this->GetMatrix()[1][0] < itk::NumericTraits<TScalarType>::ZeroValue())
{
m_Angle = -m_Angle;
}
m_S1 = Q[0][0];
m_S2 = Q[1][1];
m_K = Q[0][1] / Q[0][0];
this->ComputeMatrix();
if (static_cast<double>(this->GetMatrix()[1][0]) - static_cast<double>(std::sin(m_Angle)) > 0.000001)
{
itkWarningMacro("Bad Rotation Matrix " << this->GetMatrix());
}
}

It does a QR decomposition on the matrix but then uses R to get m_Angle, which looks backwards to me.

Then it sets the full matrix and checks that matrix[1][0] == sin(m_Angle), but don't you need to do the QR decomposition again to extract the rotation?

@cookpa
Copy link
Member

cookpa commented Mar 18, 2024

OH!

TMatrix R = myqr.Q(); // Q() is the rotation
TMatrix Q = myqr.R(); // R() is the upper triangluar

OK now it all makes sense. So I think the answer is that the matrix is correct, but the check for "bad rotation" is not, because it's checking the full affine transform directly and not just the rotation part.

@dzenanz
Copy link
Contributor Author

dzenanz commented Mar 18, 2024

Feel free to take over this PR if you have an improvement!

@cookpa
Copy link
Member

cookpa commented Mar 18, 2024

Sure, I will change it to do the sanity check by redoing the decomposition, but only if in debug mode.

@cookpa
Copy link
Member

cookpa commented Mar 19, 2024

Will merge shortly. I re-ran the template example and don't see the warning now, even in debug mode. The rotations look correct, the warning was being triggered by a test on the composed affine transform, not the rotation component.

Copy link
Contributor Author

@dzenanz dzenanz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me

@cookpa cookpa merged commit fefb531 into ANTsX:master Mar 19, 2024
1 check passed
@dzenanz dzenanz deleted the badMatrixWarning branch March 19, 2024 17:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants