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
586 stars 161 forks source link

very slow package import #610

Closed rueberger closed 1 month ago

rueberger commented 2 months ago

import ants can take several seconds, which is annoying. especially when ants is imported many times in a row as part of test suite.

consider moving away from star imports in __init__.py....

here is some profiling of the import:

Screenshot 2024-04-25 at 1 50 29 AM
ncullen93 commented 2 months ago

Thanks, yes we are working on this. The issue is with having to import the underlying ITK / ANTs compiled libraries and also with an inefficient way of mapping functions to the ANTsImage class (see below). The first part is being solved by improving the wrapping and the second part is something I am also working on.

I don't think using import * has any effect on the import time. If you've ever used the itk python library then you'll see that there also there is a very long import time.

https://github.com/ANTsX/ANTsPy/blob/d14dacdcdf7aeb3ae6e0da9d5e801bf6d060b3c7/ants/core/ants_image.py#L609C1-L634C1

rueberger commented 2 months ago

glad to hear that!

ncullen93 commented 1 month ago

Hi @rueberger, with the new C++ wrapping we just pushed the import time for me is ~1.7 seconds after a very initial import time of ~20 seconds. This is about as fast as it can be and is much much faster than ITK (5+ seconds import and then 3+ more seconds every time you use a function from a new module) and also pytorch, tensorflow, etc.

I will close this issue but still going to look into the idea of not importing everything at the start eventually. Thanks!