Lazy loading of modules and exposed utility functions#246
Lazy loading of modules and exposed utility functions#246jlaehne wants to merge 9 commits intoLumiSpy:mainfrom
Conversation
ericpre
left a comment
There was a problem hiding this comment.
Is it worth adding test like in https://github.com/hyperspy/rosettasciio/blob/main/rsciio/tests/test_import.py?
Should lazy-loader be used to support typing and auto-completion in some development environment? This would add one more library but it is already a dependency of scikit-image and I also think that it will be good to use it in HyperSpy.
After hyperspy/hyperspy#3569 is merged, it will be possible to speed up signals import even more to 0.2-0.3s.
%time from lumispy.signals import CLSpectrumtakes 1.5 s on my laptop, while it should be possible to speed it up to ~0.3s. |
With or without hyperspy/hyperspy#3569 ? Maybe we first see what that brings before we go too far in the optimization here. We should though maybe also split out the lazy signal versions like you just did in HyperSpy. |
This needs hyperspy/hyperspy#3569. I think that splitting the lazy signal is useful to avoid importing dask. |
|
What do you think about using The main advantage is to support static type checkers. |
I was actually working on it already, but forgot to commit before pushing. |
|
Should now mirror approach from hyperspy/exspy#160
|
ericpre
left a comment
There was a problem hiding this comment.
See jlaehne#20 for suggestions.
| "join_spectra", | ||
| "to_array", | ||
| "savetxt", | ||
| "crop_edges", |
There was a problem hiding this comment.
This are now moved from lumispy.utils to lumispy, would it better to keep these in lumispy.utils?
>>> import lumispy
>>> dir(lumispy)
['__version__',
'components',
'crop_edges',
'eV2nm',
'invcm2nm',
'join_spectra',
'nm2eV',
'nm2invcm',
'savetxt',
'signals',
'to_array',
'utils']While we are at it, it may be worth tidy up lumispy.utils too by defined __all__ and __dir__, currently it shows:
>>> dir(lumispy.utils)
['__builtins__',
'__cached__',
'__doc__',
'__file__',
'__loader__',
'__name__',
'__package__',
'__path__',
'__spec__',
'axes',
'axis2eV',
'axis2invcm',
'com',
'crop_edges',
'data2eV',
'data2invcm',
'signals',
'solve_grating_equation',
'var2eV',
'var2invcm']There was a problem hiding this comment.
join_spectra etc. are currently imported in the __init__.py of the main module to make them directly available from there - though they are not part of the dir
There was a problem hiding this comment.
Okay, I see, then should it be tidied up? Currently, these can be imported from two different modules, should only one import be kept?
Description of the change
Import of LumiSpy is rather slow (
1.6 son linux) mainly due to the direct loading ofSignal1Dfrom HyperSpy. This PR uses__getattr__as proposed by GitHub Copilotlazy-loaderto load both the sub-modules and utility functions exposed at the top level of LumiSpy using a lazy approach.Splits off lazy signal modules in line with hyperspy/hyperspy#3569.
Progress of the PR
upcoming_changesfolder (seeupcoming_changes/README.rst),docs/readthedocs.org:lumispybuild of this PR (link in github checks),Minimal example of the bug fix or the new feature