Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue with group registration #438

Open
boazmohar opened this issue Feb 5, 2023 · 6 comments
Open

Issue with group registration #438

boazmohar opened this issue Feb 5, 2023 · 6 comments
Labels
bug Something isn't working dependencies Issues/requests related to a dependency package
Milestone

Comments

@boazmohar
Copy link

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
Copy link
Collaborator

yoda-vid commented Feb 7, 2023

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 (SuperElastix/SimpleElastix#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:

magellanmapper/setup.py

Lines 76 to 89 in 430974b

"simpleitk==2.0.2rc2.dev785+g8ac4f ; python_version < '3.8'",
"simpleitk==2.3.0.dev117+g0640d ; python_version >= '3.8'",
"PyYAML",
"appdirs",
# part of stdlib in Python >= 3.8
"importlib-metadata >= 1.0 ; python_version < '3.8'",
"tifffile",
# required with tifffile >= 2022.7.28
"imagecodecs",
# part of stdlib in Python >= 3.7
"dataclasses ; python_version < '3.7'",
# BrainGlobe dependencies for access to cloud-hosted atlases
"bg-atlasapi @ https://github.com/brainglobe/bg-atlasapi/archive/refs/heads/master.zip",

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.

@yoda-vid yoda-vid added bug Something isn't working dependencies Issues/requests related to a dependency package labels Feb 7, 2023
@yoda-vid yoda-vid added this to the v1.6.0 milestone Feb 7, 2023
@boazmohar
Copy link
Author

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
Copy link
Collaborator

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
Copy link
Author

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
Copy link
Collaborator

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: SimpleITK/SimpleITK#1891 .

@yoda-vid
Copy link
Collaborator

@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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working dependencies Issues/requests related to a dependency package
Projects
None yet
Development

No branches or pull requests

2 participants