ANTsX / ANTs

Advanced Normalization Tools (ANTs)
Apache License 2.0
1.21k stars 381 forks source link

Refactor antsTransformInfo to add some error handling and compute determinant #1712

Closed gdevenyi closed 5 months ago

gdevenyi commented 7 months ago

I'd like to be able to easily extract the determinant of affine transforms and antsTransformInfo seems like the right place.

The function has been refactored to handle reading errors.

Based primarily off https://github.com/InsightSoftwareConsortium/ITK/blob/master/Examples/IO/TransformReadWrite.cxx

Things that aren't done:

cookpa commented 7 months ago

Once the transform is read, I think you can call GetInputSpaceDimension() on it to determine the dimensionality

cookpa commented 7 months ago

The program as written is not restricted to affine transforms, it will read any supported transform file. Assuming affine transforms would actually restrict its functionality.

Maybe the determinant computation would be better added to iMath or something?

gdevenyi commented 7 months ago

The program as written is not restricted to affine transforms,

Can you give an example? I tried this on Warpfields and it falls over, I don't know of any other transform types which couldn't be cast to an affine...

cookpa commented 7 months ago

If you run antsRegistration with --output-composite-transform, it will read the resulting transform file.

gdevenyi commented 7 months ago

Got it.

Looks like I need to read all transforms as composite transforms and then figure out how to unpack it back into a list and loop over the unpacked components, checking the type.

cookpa commented 7 months ago

From the discourse, seems like dynamic casting to MatrixOffsetTransformBase would be sufficient. If that's not null, you can call GetMatrix() on the result

gdevenyi commented 7 months ago

Ref: https://discourse.itk.org/t/reading-arbitrary-transforms-compute-something-if-linear/6553/2

gdevenyi commented 5 months ago

This now will unpack transform lists as well as CompositeTransforms and print the determinant in addition to the PrintSelf for every transform in the whole stack.

cookpa commented 5 months ago

Thanks - I'm going to be out of the office, so might be some time before I get to this

gdevenyi commented 5 months ago

Context of this work is I use affine determinants in steps of https://github.com/CoBrALab/optimized_antsMultivariateTemplateConstruction

And currently I have to do this by using antsApplyTransforms to produce a warp field from the affine and then a Jacobian file from that. This will allow me to just compute the the jacobian determinant with antsTransformInfo and extract it from the text output.

cookpa commented 5 months ago

I didn't verify the determinant values but it appears to work with the supported transform types

cookpa commented 5 months ago

Thanks for this @gdevenyi

gdevenyi commented 5 months ago

I have tested the output values using MINC affine transforms, where the I can generate arbitrary transforms with param2xfm (giving a known determinant) and then compute the determinant using both MINC tools and antsTransformInfo :+1: