question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

dict_learning and partial_dependence functions can be shadowed by deprecated module imports

See original GitHub issue

Since scikit-learn 0.22, the dict_learning function is defined in sklearn/decomposition/_dict_learning.py and exposed as sklearn.decomposition.dict_learning in the sklearn/decomposition/__init__.py file.

At the same time we also have a sklearn/decomposition/dict_learning.py deprecated module for backward compat. This module is not imported by default in sklearn/decomposition/__init__.py to avoid deprecation warning.

So far so good. The problem is that if the users or a tool later import sklearn.decomposition.dict_learning (the deprecated module), then the symbol sklearn.decomposition.dict_learning points to the module instead of the function:

>>> from sklearn.decomposition import dict_learning                                                                                                                             
>>> type(dict_learning)                                                                                                                                                         
<class 'function'>
>>> import sklearn.decomposition.dict_learning                                                                                                                                  
/home/ogrisel/code/scikit-learn/sklearn/utils/deprecation.py:144: FutureWarning: The sklearn.decomposition.dict_learning module is  deprecated in version 0.22 and will be removed in version 0.24. The corresponding classes / functions should instead be imported from sklearn.decomposition. Anything that cannot be imported from sklearn.decomposition is now part of the private API.
  warnings.warn(message, FutureWarning)
>>> from sklearn.decomposition import dict_learning                                                                                                                             
>>> type(dict_learning)                                                                                                                                                         
<class 'module'>

This can happen (semi-randomly?) when running test discovery with pytest --pyargs sklearn from a non-source folder (without the scikit-learn conftest.py file) or when calling all_estimators() from scikit-learn.

Note that in 0.21.3 we did not have the problem because once the sklearn.decomposition.dict_learning module is imported once, it is cached, and therefore, sklearn.decomposition.dict_learning is always referring to the function.

We have the same problem for the sklearn.inspection.partial_dependence function / module.

I will open a PR with a possible fix.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:10 (10 by maintainers)

github_iconTop GitHub Comments

1reaction
glemaitrecommented, Dec 9, 2019

If I recall correctly, it depends on the pytest version as well: https://github.com/scikit-learn/scikit-learn/blob/master/azure-pipelines.yml#L60

0reactions
ogriselcommented, Dec 18, 2019

I realized that I did not answer this question: in 0.21. Let me answer it now for future reference:

In 0.21, doing from sklearn.decomposition import dict_learning you actually trigger the import of the sklearn.decomposition.dict_learning module before setting the attribute sklearn.decomposition.dict_learning to the function.

If after you do from sklearn.decomposition.dict_learning import dict_learning, the sklearn.decomposition.dict_learning is already imported and in the sys.modules cache, therefore nothings happen and the sklearn.decomposition.dict_learning attribute still points to the function.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Deprecate or shadow a module or function from a third-party ...
In our project (a cabal project with executable and library sections), we sometimes implement "safer" or improved versions of standard functions ...
Read more >
Partial dependence plots (PDP) and individual conditional ...
Partial dependence plots (PDP) and individual conditional expectation (ICE) plots can be used to visualize and analyze interaction between the target ...
Read more >
What's New In Python 3.9
unparse() as a function in the ast module that can be used to unparse an ast.AST object and produce a string with code...
Read more >
Partial Dependence Plot
For a perturbation-based interpretability method, it is relatively quick. PDP assumes independence between the features, and can be misleading interpretability- ...
Read more >
iml: Interpretable Machine Learning
You can reuse the pdp object for other features: eff$set.feature("lstat") plot(eff). # Only plotting the aggregated partial dependence:.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found