ANTsX / ANTsPy

A fast medical imaging analysis library in Python with algorithms for registration, segmentation, and more.
https://antspyx.readthedocs.io
Apache License 2.0
625 stars 161 forks source link

inverse of a deformation field, build_template #125

Closed rueberger closed 4 years ago

rueberger commented 4 years ago

I tried to take the inverse of a deformation field and was met with a ValueError. Is there not a well defined notion of the inverse of a deformation field?

I'm doing this in the context of template building, something very much like ants.build_template. I'm still a novice at registration, but it's unclear to me that build_template works - I get a 'blurred' result.

I've been following Bogovic et. al 2018's prescription for creating a template from a set of brains:

Groupwise registration begins with an initial template - often a single individual or the average of the unregistered cohort of images is se- lected (we chose the latter). Next, every individual is registered to that initial template using a particular registration algorithm (transformation model, similarity measure, optimization scheme) and then transformed to the template space. Next, the set of transformed individuals are averaged and the transformations are averaged. This yields new mean intensities as well as a mean transformation from subject to the current template estimate. Finally, the new mean intensity image is transformed through the inverse of the mean transformation to obtain a new template. This procedure of registration-averaging-transformation is iterated to transport the initial template toward the mean intensity and shape.

And as far as I can tell build_template follows this prescription except for the last step - where it transforms the mean intensity image by the mean transformation (as opposed to the inverse of the mean transformation).

rueberger commented 4 years ago

I have consulted with ANTSdoc and see that ANT's transformation representation isn't necessarily invertible. That's unfortunate. But perhaps it would be possible to achieve approximate invertibility by somehow increasing the resolution of the forward transformation?

stnava commented 4 years ago

deformation fields are generally not invertible. velocity fields can be used to generate forward and (at least incremental) inverse maps which is what we usually do. syn provides fwd and inverse maps but these don't help you in the case of template building. see one of our older papers to understand this further.

I assure you that it works. one of the most used parts of ants. here is an example.

image = ants.image_read( ants.get_ants_data('r16')  )
image2 = ants.image_read( ants.get_ants_data('r27') )
image3 = ants.image_read( ants.get_ants_data('r85') )
timage = ants.build_template( image_list = ( image, image2, image3 ), iterations=3 )

and if you want something more complex, then see:

https://github.com/ntustison/TemplateBuildingExample

which you should be able to reproduce in python.

stnava commented 4 years ago

in general, one does not want to try to invert deformation fields directly. this is a losing proposition, for a variety of reasons.

rueberger commented 4 years ago

My impression was that deformations in ANTs are diffeomorphisms, which are definitionally invertible, and that invertibility problems arise out of limitations of the representation. Is that not the case?

How would you express the inverse (even approximately) of a deformation field in ANTs? Or am I just S.O.L.?

Well like I said, I am certainly no registration expert. But to my untrained eyes things don't look good.

Here are a few of the brains that I wish to construct a mean brain from:

brain_0 brain_1 brain_2

And here is the template I get from ants.build_template

template

Looks 'blurry', no?

I could imagine such a template being useful for the purposes of registering your original brains to it (although I was not impressed with the results I got when I did that) - but my main goal presently is to obtain the mean brain itself. And even to my untrained eyes, it's clear that the present result isn't suitable.

stnava commented 4 years ago

My impression was that deformations in ANTs are diffeomorphisms, which are definitionally invertible, and that invertibility problems arise out of limitations of the representation. Is that not the case?

that is the case. the deformation field representation is insufficient to generate the inverse mapping. for that, you need the velocity field. SyN returns the fwd and inverse maps which are computed in tandem throughout the optimization. so if you want both - and certain numerical guarantees on the accuracy of that inverse map - then use SyN. if you want the velocity fields, use an exponential map or the time-varying framework. then you can generate fwd and inverse maps from the velocity fields - not from the deformations.

. How would you express the inverse (even approximately) of a deformation field in ANTs? Or am I just S.O.L.?

you either use the inverse warp provided by SyN or use a different method (exponential or time varying) and use the velocity fields to generate fwd and inverse deformations the accuracy of which will depend on several details beyond the scope of this discussion.

Looks 'blurry', no?

I think your original data looks low quality (maybe blurry) and inconsistent ( potentially violating the theory of the template construction) and, as such, the template is roughly what you might expect. particularly given that you are using default parameters on a problem for which parameters have not been optimized. you may also be suffering from several other issues including inhomogeneity, poor overlap of anatomy across Subjects due to bounding box issues, boundary conditions and who knows what else ( perhaps bad initialization, bad affine mapping or sub-optimal parameter selection ).

some possible solutions

stnava commented 4 years ago

one more thing:

And as far as I can tell build_template follows this prescription except for the last step - where it transforms the mean intensity image by the mean transformation (as opposed to the inverse of the mean transformation).

the steps described by Bogovic are implemented in https://www.ncbi.nlm.nih.gov/pmc/articles/PMC2818274/ and https://www.ncbi.nlm.nih.gov/pubmed/15501083 and in the ANTs build template family of commands ie they are implement the same procedure (at least language wise) consistent with the description above.

stnava commented 4 years ago

also - if we were not inverting the mean transformation, nothing would work. we would in fact diverge quite disastrously.

rueberger commented 4 years ago

OK now I'm confused, can you help me understand where in build_template the mean transformation is inverted?

stnava commented 4 years ago

https://github.com/ANTsX/ANTsPy/blob/3989f6ef805924c49ca78b2682da3382ae057bce/ants/registration/build_template.py#L76-L77

we follow the negative gradient of the shape space which is defined by the average transport from the previous template to the whole population

rueberger commented 4 years ago

I don't follow that - will have to get cozy with your manuscript.

Quick q: you suggested earlier using an exponential map - what's the type_of_transform argument for that? The only mention of elastic I see is as a regularizer for SyN.