CoBrALab / optimized_antsMultivariateTemplateConstruction

A re-implementation of antsMultivariateTemplateConstruction2.sh using optimized image pyramid scale-space and qbatch support
Other
20 stars 8 forks source link

Affine Registration Failures #63

Closed araikes closed 1 year ago

araikes commented 1 year ago

Hi @gdevenyi

I was running an audit on my template building progress and using the resampled images in nlin/xxx/resample. Shown below is nlin/1/average/template_sharpen_shapeupdate, and then the resampled version of a single subject from nlin/0 (top right) and nlin/1 (bottom left).

Any idea why the resampling was so terrible and what issues will this cause down the line?

image

gdevenyi commented 1 year ago

This is an image registration failure, at the affine part of the nlin stage.

Consequences

If this subject is correct in a later stage, this finding is irrelevant, at worst the convergence was ever-so-slightly altered. In the future, you should probably just examine the final stage.

If these results were the final stage: if you goal is modelbuilding, this will hurt your average a bit, subject to how many inputs you actually have

if your goal is DBM, this subject will have meaningless measures.

How to fix this:

  1. Improve your target.
    • your padding is insufficient around the data, I suggest 10-20v around the foreground voxels
  2. --no-reuse-affines in case you are, this error will propagate forward, the default is --no-reuse-affines
  3. Work in isotropic space (I don't know if you are, but voxels should be isotropic, registration proceeds better in convergence and step size)

If you provide other runtime/file details, I may be able to provide a few more suggestions.

araikes commented 1 year ago

Thanks for the run-down... I was examining the earlier stages because I had a similar issue with the final stage and needed it for DBM so I removed both the affine and nlin stages to regenerate so I was checking as it went.

I did have a failure in the final step for a different mouse so I'm re-running that step.

I am curious about the padding here. I'm using an existing template as the starting target, but your suggestion would still be to pad the data?

I'm running this locally in a container with 42 mice: modelbuild.sh inputs/inputs.txt --masks masks/masks.txt --starting-target ${TEMPLATE_DIR}/starting_target.nii.gz --stages 'rigid[4],similarity[4],affine[4],nlin[6]' --mask-extract --sharpen-type laplacian --gradient-step 0.1 --average-type trimmed_mean --average-prog python

ANTs v. 2.4.2 My most recent pull of this package: fc5dd636808d3897a957b6f2f1b99bba86e6b43e`

gdevenyi commented 1 year ago

I am curious about the padding here. I'm using an existing template as the starting target, but your suggestion would still be to pad the data?

Its not the data that needs to be padded, but rather the starting target then. It will define the FOV for all subsequent registrations. Ideally we want the average to be in the middle of a padding of background, surrounded equally in all directions. This will ensure the isotropic smoothing which happens during registration gets a clean gradient to drive registration.

I'm running this locally in a container with 42 mice:

You may be interested in https://github.com/pipitone/qbatch environment variables to add more parallelism to the pipeline in the container for features not currently exposed on the command line.

I'm going to break down the command here, and comment, this is very helpful. --masks masks/masks.txt have you checked how the mask evolves? --starting-target ${TEMPLATE_DIR}/starting_target.nii.gz since you're using a starting target, do you have a starting target mask? --mask-extract this requires good masks, are make sure they are because otherwise you'll violate the 1:1 voxel matching assumption --average-type trimmed_mean --average-prog python this explains why your failed registration is minimally impacting things in the intensity average, its probably being outlier'd out.

--stages 'rigid[4],similarity[4],affine[4],nlin[6] -- my guess is the rigid and similarity iterations are overkill? my experience is 1-2 are enough, just to save you some time.

My most recent pull of this package: fc5dd63

Compared to master, the additions are a volgenmodel implementation, Lie-Algebra affine averaging, and affine scaling (disabled b/c I'm not sure its worth it)

You can see the exact commits and changes here: https://github.com/CoBrALab/optimized_antsMultivariateTemplateConstruction/compare/fc5dd636808d3897a957b6f2f1b99bba86e6b43e...master

araikes commented 1 year ago

In response here:

  1. I think I've left off the starting mask and that would definitely help.
  2. The mask looks reasonable as it goes and follows contours as expected. The input masks are decent with some marginal non-brain regions, but all in all not terrible.
  3. Thanks for the input about the rigid and similarity steps. That was a hold-over from when I wasn't using a starting template.
  4. I'll probably update to the current master just to make sure I'm up to date.
  5. I'll pad that starting template a bit as well.

Last question for qbatch: I'm running on a cluster where I've got 42 cores dedicated to the template building. Any options you'd suggest beyond:

export QBATCH_CORES=22
export QBATCH_SYSTEM="local"

Thanks, as always.

gdevenyi commented 1 year ago

The input masks are decent with some marginal non-brain regions, but all in all not terrible.

That should be fine, you don't want the other way of removing brain tissue, obviously.

I'll probably update to the current master just to make sure I'm up to date.

You should also update the minc-toolkit-extras to the latest HEAD. In the future I'm going to sync the versions, https://github.com/CoBrALab/optimized_antsMultivariateTemplateConstruction/pull/64

Any options you'd suggest beyond

Those are the only relevant ones for local parallelism :+1:

One more thing I'm always on the lookout for in these situations is "is the raw data that has issues materially different than the successful ones", this means things like bias field, mask quality, intensity artifacts, and other human-eye observables.

araikes commented 1 year ago

Went ahead and updated minc-toolkit-extras as well.

There is considerable heterogeneity in the raw data. Preprocessing has helped to mitigate much of it (bias correction, intensity normalization). And if it was a consistent mouse brain that had registration failures, I could understand that. The thing that gets me is that from iteration to iteration, there isn't consistency as to which (if any) have registration failures.

gdevenyi commented 1 year ago

Followup here, did the adjustments suggested resolve this?

araikes commented 1 year ago

No. I still have oddly resampled images for no obvious reason at times. However, I've gotten around it by generating the template (which works fine) and then running the "classical" dbm approach using that template as the target and this seems to work sufficiently for now.

As an aside, any progress toward multispectral templates?

gdevenyi commented 1 year ago

No. I still have oddly resampled images for no obvious reason at times.

I'm going to call this affine registration failures. The resampling is working as intended ;)

However, I've gotten around it by generating the template (which works fine) and then running the "classical" dbm approach using that template as the target and this seems to work sufficiently for now.

It's good that you worked around it, but frustratingly wasteful and less-than-fully automatic.

So, to confirm:

Here's some other thoughts:

As an aside, any progress toward multispectral templates?

I'm polishing off resampling of secondary spectra (or labels, or quantitative metrics) using the base-modelbuild, then I'll move on to multispectral registration.

gdevenyi commented 1 year ago

Original issue "resolved" as much as it can be.