sanderslab / magellanmapper

MagellanMapper is a graphical interface for 3D bioimage annotation, atlas registration, and regional quantification
BSD 3-Clause "New" or "Revised" License
20 stars 3 forks source link

Issue with group registration #438

Open boazmohar opened 1 year ago

boazmohar commented 1 year ago

Hi,

I have an issue while I am trying to get an average volume of 6 brains I have imaged. I start with import and resize to the allen ccf:

python U:\magellanmapper\run.py --proc import_only -v --img "C:\Make_atlas\ANM39.tif" 
python U:\magellanmapper\run.py --img "C:\Make_atlas\ANM36.tif" --proc transform --atlas_profile abaccfv3

Then I tried to run the group reg:

python U:\magellanmapper\run.py -v  --register group  --atlas_profile groupwise --img "C:\Make_atlas\ANM36"  "C:\Make_atlas\ANM37"   "C:\Make_atlas\ANM39"  

My first issue is the assumption I have labels for one of the brains - line 1061 in register.py.

In my local copy I was trying to skip this and not crop in y. I can get thru to line 1116:

    transform_filter = elastix_img_filter.Execute()

And I get an error:

root - CRITICAL - Unhandled exception. Additional log saved to: C:\Users\moharb\AppData\Local\Temp\magellanmapper_error_varcqfqz.log
Traceback (most recent call last):
  File "U:\magellanmapper\run.py", line 23, in <module>
    load_env.main()
  File "U:\magellanmapper\magmap\io\load_env.py", line 185, in main
    launch_magmap()
  File "U:\magellanmapper\magmap\io\load_env.py", line 138, in launch_magmap
    cli.main()
  File "U:\magellanmapper\magmap\io\cli.py", line 994, in main
    process_tasks()
  File "U:\magellanmapper\magmap\io\cli.py", line 877, in process_tasks
    register.main()
  File "U:\magellanmapper\magmap\atlas\register.py", line 2047, in main
    register_group(
  File "U:\magellanmapper\magmap\atlas\register.py", line 1129, in register_group
    transform_filter = elastix_img_filter.Execute()
  File "C:\Users\moharb\Miniconda3\envs\mag\lib\site-packages\SimpleITK\SimpleITK.py", line 7081, in Execute
    return _SimpleITK.ElastixImageFilter_Execute(self)
RuntimeError: Exception thrown in SimpleITK ElastixImageFilter_Execute: Y:\SimpleElastix\Code\Elastix\src\sitkElastixImageFilterImpl.cxx:191:
sitk::ERROR: ElastixImageFilter does not support the combination of 4-dimensional 32-bit float fixed image and a 4-dimensional 32-bit float moving image.

I tried this minimal example:

vectorOfImages=sitk.VectorOfImage()
origin = None
spacing = None
for filename in l2:
    reader = sitk.ImageFileReader()
    reader.SetImageIO("TIFFImageIO")
    reader.SetFileName(filename)
    img = reader.Execute()
    if origin is None:
        origin = img.GetOrigin()
        spacing = img.GetSpacing()
    else:
        img.SetOrigin(origin)
        img.SetSpacing(spacing)
    vectorOfImages.push_back(img[:300,:300,:300])

label4d=sitk.JoinSeries(vectorOfImages)
elastixImageFilter=sitk.ElastixImageFilter()
elastixImageFilter.SetFixedImage(label4d)
elastixImageFilter.SetMovingImage(label4d)
elastixImageFilter.SetParameterMap(sitk.GetDefaultParameterMap('groupwise'))
resultImage=elastixImageFilter.Execute()

Still get the same error. Any idea what I have missing? I found this issue, maybe could be related.

Thanks! Boaz

yoda-vid commented 1 year ago

Thanks for asking about this, @boazmohar! I get this error as well when I test the groupwise reg.

Looks like it's related to this issue (https://github.com/SuperElastix/SimpleElastix/issues/468), where more recent versions of SimpleITK/SimpleElastix lost support for groupwise reg.

A workaround I just tested is to install an older version of SimpleElastix. I have an older build that works, though it's a bit complicated to install to avoid version conflicts and because it's only for Python 3.6. I got it to work by changing this section in setup.py: https://github.com/sanderslab/magellanmapper/blob/430974b4f1e8695c490be143c59f3443fdd7a9c7/setup.py#L76-L89

to install these simpleitk and bg-atlasapi (this package just bumped its Python req to >= 3.8) versions:

"simpleitk==1.1.0.dev362+gb3783 ; python_version < '3.8'",
"bg-atlasapi==1.0.2,"

and then rerunning setup:

pip install -e . --extra-index-url https://pypi.fury.io/dd8/

My first issue is the assumption I have labels for one of the brains - line 1061 in register.py.

Yeah, the groupwise reg function was originally for a more specialized case, and I'll need to remove this assumption--thanks for pointing it out! I also found a few more issues such as loading settings that I'll need to fix.

boazmohar commented 1 year ago

Thanks for addressing this. I saw that ANTS has a more well documented group registration function: antsMultivariateTemplateConstruction2.sh Any chance to incorporate that into this package?

yoda-vid commented 1 year ago

Thanks for sharing, will definitely consider this. I have not used ANTs extensively, and my understanding is that it can be very accurate but more computationally expensive (see https://ieeexplore.ieee.org/document/8512403). Have you found it to work well for your use cases?

Also, happy to work together with you to implement it here, if you are interested.

boazmohar commented 1 year ago

Yes, I do find it very accurate and slow. It can take many hours to make a template, but I on;y need to do it once (well correctly once, but incorrectly many time :). I am working from the command line currently, and I had to compile a bunch of things before it worked. I think there is a wrapperbut I haven't tried it. Could you take a look if that could be a dependency for your project? If so I can have a look at how to integrate it.

Thanks!! Boaz

yoda-vid commented 1 year ago

Thanks for pointing out that wrapper! It looks like a great way to implement ANTs here, and happy to add it as a dependency. I've been using SimpleITK here mainly just for I/O and registration, otherwise converting to NumPy arrays, and it looks like ANTsPy could at least function similarly. The extra registration accuracy would also be a definite plus! Please do feel free to integrate it if you'd like, and let me know anytime if questions come up, happy to help.

By the way, the folks at SimpleITK are fixing the groupwise reg, so hopefully that will be working again soon: https://github.com/SimpleITK/SimpleITK/issues/1891 .

yoda-vid commented 1 year ago

@boazmohar, I've added support for ITK-Elastix as an alternate to SimpleITK, where the groupwise registration works at least in my basic tests. It should also be easier to integrate other registration toolkits like ANTs if interested in the future.