Skip to content

MAINT Replace stats.mode calls with fixes._mode #938

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 13 commits into from
Dec 2, 2022

Conversation

hayesall
Copy link
Member

@hayesall hayesall commented Nov 12, 2022

Reference Issue

Fix #937

What does this implement/fix? Explain your changes.

I caught up on scikit-learn/scikit-learn#23633 and replaced the scipy.stats.mode calls with sklearn.utils.fixes._mode calls.

  • Bump JOBLIB_MIN_VERSION = "1.1.1"
  • Bump SKLEARN_MIN_VERSION = "1.1.3"
  • Bump NUMPY_VERSION = "1.21.0" in py37_conda_defaults_openblas build (otherwise conda runs into a large number of incompatible packages).

Similar to the notes there, this should be replaced when scipy 1.9 is the minimum version:

https://github.com/scikit-learn/scikit-learn/blob/7ec1bfc2ab7425bcd984d631b5bfeb9082ce11bf/sklearn/utils/fixes.py#L168-L172

@codecov
Copy link

codecov bot commented Nov 12, 2022

Codecov Report

Base: 92.62% // Head: 93.87% // Increases project coverage by +1.24% 🎉

Coverage data is based on head (14fcd35) compared to base (2779327).
Patch coverage: 100.00% of modified lines in pull request are covered.

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #938      +/-   ##
==========================================
+ Coverage   92.62%   93.87%   +1.24%     
==========================================
  Files          94       95       +1     
  Lines        6361     6369       +8     
  Branches      886      887       +1     
==========================================
+ Hits         5892     5979      +87     
+ Misses        395      308      -87     
- Partials       74       82       +8     
Impacted Files Coverage Δ
imblearn/over_sampling/_smote/base.py 99.01% <100.00%> (ø)
..._prototype_selection/_edited_nearest_neighbours.py 99.18% <100.00%> (ø)
...rototype_selection/_neighbourhood_cleaning_rule.py 100.00% <100.00%> (ø)
imblearn/utils/fixes.py 100.00% <100.00%> (ø)
imblearn/ensemble/_forest.py 88.30% <0.00%> (+0.58%) ⬆️
imblearn/tests/test_docstring_parameters.py 87.05% <0.00%> (+56.11%) ⬆️

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

☔ View full report at Codecov.
📢 Do you have feedback about the report comment? Let us know in this issue.

@hayesall hayesall mentioned this pull request Nov 12, 2022
Copy link
Member

@glemaitre glemaitre left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the fix that I did as well in scikit-learn.
I will just postpone it until when all other tests are passed.

@hayesall
Copy link
Member Author

hayesall commented Dec 2, 2022

The lint failure is new. It looks like --diff got removed from flake8 in 6.0.0: https://flake8.pycqa.org/en/latest/release-notes/6.0.0.html#backwards-incompatible-changes

@glemaitre
Copy link
Member

I fixed it in my PR. Basically, we don't do diff anymore in scikit-learn.

Comment on lines 14 to 26
from sklearn.externals._packaging.version import parse as parse_version
import scipy
import scipy.stats


sp_version = parse_version(scipy.__version__)


# TODO: Remove when SciPy 1.9 is the minimum supported version
def _mode(a, axis=0):
if sp_version >= parse_version("1.9.0"):
return scipy.stats.mode(a, axis=axis, keepdims=True)
return scipy.stats.mode(a, axis=axis)
Copy link
Member Author

@hayesall hayesall Dec 2, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I found the earliest sklearn.utils.fixes._mode in scikit-learn==1.1.2:

pip install scikit-learn==1.1.2
python -c 'import sklearn; print(sklearn.utils.fixes._mode)'

We could add the checks for scikit-learn>=1.1.2 like follows (but I think the current fix is simpler and basically does the same thing):

from sklearn.externals._packaging.version import parse as parse_version
import scipy
import scipy.stats
import sklearn

sk_version = parse_version(sklearn.__version__)
sp_version = parse_version(scipy.__version__)


def _mode(a, axis=0):
    if sp_version >= parse_version("1.9.0"):
        # TODO: Remove when SciPy 1.9 is the minimum supported version
        return scipy.stats.mode(a, axis=axis, keepdims=True)
    elif sk_version >= parse_version("1.1.2"):
        # TODO: Remove when scikit-learn 1.2 is the minimum supported version
        return sklearn.utils.fixes._mode(a, axis=axis)
    return scipy.stats.mode(a, axis=axis)

@hayesall
Copy link
Member Author

hayesall commented Dec 2, 2022

Everything is green (e.g. ec50f5b) if we're more restrictive with minimum versions. These still fail:

SKLEARN_MIN_VERSION = "1.1.0"
JOBLIB_MIN_VERSION = "1.0.0"

... due to treating FutureWarning as errors in build_tools/azure/test_script.sh.

TEST_CMD="$TEST_CMD -Werror::DeprecationWarning -Werror::FutureWarning -Wignore:tostring:DeprecationWarning"

Because scikit-learn==1.1.0 still raises the FutureWarning because the scipy.stats.mode calls were not replaced with _mode yet.

A possible fix would be to drop the -Werror::FutureWarning (e.g. see my previous commit here)

MRE:

pip install scikit-learn==1.1.0 numpy==1.23.5 scipy==1.9.3 joblib==1.2.0 pytest
pytest -Werror::FutureWarning -k 'test_condensed_nearest_neighbour' imblearn/

@glemaitre glemaitre self-assigned this Dec 2, 2022
@glemaitre
Copy link
Member

I modify a couple of things that I merge in master. I will check if we can solve the FutureWarning.

@glemaitre glemaitre changed the title 👽️ Replace stats.mode calls with fixes._mode MAINT Replace stats.mode calls with fixes._mode Dec 2, 2022
@glemaitre glemaitre merged commit 436ee08 into scikit-learn-contrib:master Dec 2, 2022
@glemaitre
Copy link
Member

Thanks @hayesall. This should make master green. I will go forward to check the upcoming warnings.

@hayesall hayesall deleted the scipy-compat branch December 3, 2022 18:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[BUG] Upcoming changes to scipy.stats.mode in 1.11
2 participants