-
Notifications
You must be signed in to change notification settings - Fork 135
Lazy loading
Various RAVEN features can use different libraries. Some of these libraries can take noticeable time to load. If they are only used for some inputs, they should be lazily loaded so they are not loaded for inputs that do not use them.
In the importerUtils, lazy loading methods can be used to do lazy imports. First the module is imported. It will be imported the first time that any attributes of the module are used.
Before (non lazy):
import tensorflow as tf
...
self.availLayer['dense'] = tf.keras.layers.Dense
After (lazy):
import utils.importerUtils
tf = utils.importerUtils.importModuleLazyRenamed("tf", globals(), "tensorflow")
...
self.availLayer['dense'] = tf.keras.layers.Dense
If renaming the module is not desired then the shorter form can be used:
Before (non-lazy):
import tensorflow
After (lazy):
import utils.importerUtils
tensorflow = utils.lazyImporterUtils.importModuleLazy("tensorflow", globals())
If the module is only used in one function, then the module can just be loaded that function.
Before (non-lazy):
import sklearn.neighbors
def someFunc():
nr = sklearn.neighbors.KNeighborsRegressor(...)
After (lazy):
def someFunc():
import sklearn.neighbors
nr = sklearn.neighbors.KNeighborsRegressor(...)
PluginFactory does not load the plugin immediately. In order to load the plugin, PluginFactory.finishLoadPlugin
needs to be called.
So something like:
#found something.stuff in input file
PluginFactory.finishLoadPlugin("something")
#then can check for something.stuff
These are modules that are time consuming to load and only sometimes needed, so they should only be loaded when the input requires them.
- tensorflow
- statsmodels
- scikit-learn (sklearn)
These are modules that practically every RAVEN input will need, so they can just be loaded directly.
- numpy
- pandas
- xarray