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

Welford method algo #88

Closed RyanHaniff closed 9 months ago

RyanHaniff commented 10 months ago

This PR uses welford algo to fix #78

There are a few branches that I have created for testing memory usage. You can see the results below, and see the branch with its respective code implementation.

For the welford library branch I use the pip Welford library.

The welford algo is an implementation based on this pseudocode.

Ram Usage from 10 MRI scans to get mean

Ram Usage (Max Ram - Init Ram)
Original Code 12.4GB - 6.04GB = 6.36GB
Welford Library 9.17GB - 5.8GB = 3.37GB
Welford Algo 7.81GB - 5.8GB = 2.01GB

Image Output

Original Code

ksnip_20230830-142618

Welford Algo

ksnip_20230830-142613

Welford Library

ksnip_20230830-142615

As mentioned by @gdevenyi, there should not be any difference between two mean image calculations. This was verified by running the original code and diffing the images.

However, the Welford library and Welford algo had differences when diffing from the original image. I suspect these differences are caused by different initial positions in the x an y axis.

Welford Library vs Original Code

ksnip_20230830-140944

Welford Algo vs Original Code

ksnip_20230830-140941

Original Code vs Original Code

ksnip_20230830-140939

Welford Algo vs Welford Library

ksnip_20230830-140933

Conclusion

If there is a way to verify that the original code and the Welford algo produce the same resultant image, the Welford algo is a good solution to reduce RAM usage.

gdevenyi commented 10 months ago

With regards to the differences, I expect these are numerical, but I'll examine again after you merge into a single welford function that we're not introducing any numerical instability.

As for benchmarking, in the future you can avoid instrumenting the code by running with:

/usr/bin/time -v <command> which gives you summaries of cpu/user/walltime and memory usage of a command.