Draft
Conversation
Add a _LazyModule proxy class (econml/_lazy.py) that defers module
loading until first attribute access. This keeps lazy import
declarations at the top of each file alongside normal imports,
making the deferred loading explicit and avoiding scattered inline
imports inside function bodies.
Modules deferred:
- shap (+numba, sparse) in econml/_shap.py
- statsmodels.iolib.{table,summary} in econml/utilities.py
- statsmodels.{tools,api,robust} in econml/sklearn_extensions/linear_model.py
- statsmodels.tools.tools in econml/data/dynamic_panel_dgp.py
- statsmodels.{api,tools} in econml/validate/drtester.py
Measured improvement: single-test cold start ~12s -> ~7s (39% faster).
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Signed-off-by: Keith Battocchi <kebatt@microsoft.com>
Add a lightweight add_constant() to econml/utilities.py that handles the numpy-array case directly, with a guard that raises TypeError for pandas DataFrames. This eliminates the statsmodels dependency from dynamic_panel_dgp.py entirely, and removes the _statsmodels_tools lazy import from linear_model.py and drtester.py. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Signed-off-by: Keith Battocchi <kebatt@microsoft.com>
Replace 4 deferred imports that existed to avoid circular imports with top-level _LazyModule declarations. The lazy proxy defers the actual importlib.import_module() call until first attribute access, which happens inside function/method bodies after all modules have finished loading — so the circular dependency is still broken, but the import declaration lives at the top of the file. - econml/dml/causal_forest.py: econml.score (RScorer) - econml/inference/_bootstrap.py: econml._cate_estimator (BaseCateEstimator) - econml/sklearn_extensions/linear_model.py: econml.sklearn_extensions.model_selection - econml/_ortho_learner.py: econml.dml._rlearner (_ModelFinal) Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Signed-off-by: Keith Battocchi <kebatt@microsoft.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR adds a utility to lazily import modules, and uses it for the following use-cases: