Skip to content

MNT Import linear assignment from scipy#13465

Merged
jnothman merged 16 commits intoscikit-learn:masterfrom
praths007:import_linear_assignment_from_scipy
Apr 17, 2019
Merged

MNT Import linear assignment from scipy#13465
jnothman merged 16 commits intoscikit-learn:masterfrom
praths007:import_linear_assignment_from_scipy

Conversation

@praths007
Copy link
Copy Markdown
Contributor

@praths007 praths007 commented Mar 18, 2019

Reference Issues/PRs

linear_assignment can now be imported from scipy #13464

What does this implement/fix? Explain your changes.

Removed the use of sklearn.utils.linear_assignment_ and _hungarian and replaced it with scipy.optimize.linear_sum_assignment

Tweaked test case for _hungarian so that it works with scipy.optimize.linear_sum_assignment

Any other comments?

more info can be found here #13464

Copy link
Copy Markdown
Member

@jnothman jnothman left a comment

Choose a reason for hiding this comment

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

Thanks!

as the algorithm incorporated in SciPy will be used instead.

"""
# Based on original code by Brain Clapper, adapted to NumPy by Gael Varoquaux.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

please use the warnings module to raise a deprecation warning here (so that it appears whenever the module is imported)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

done

@@ -4,7 +4,7 @@
import numpy as np
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

we don't need this file. It can be removed entirely. Scipy tests their implementation.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Actually let's leave it in to maintain code coverage. Use the version in master, only changing it to ignore deprecation warnings during import. That way we will be sure to remove it in 0.23.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

done

# Deprecation warning for module
warnings.warn(
"This module is deprecated in 0.21 and will removed from 0.23 "
"as the algorithm incorporated in SciPy will be used instead.", ImportWarning)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Use a DeprecationWarning, rather

Copy link
Copy Markdown
Member

@jnothman jnothman left a comment

Choose a reason for hiding this comment

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

Sorry, that was in response to an old change.

import numpy as np

# XXX we should be testing the public API here
from sklearn.utils.linear_assignment_ import _hungarian
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

you need to ignore the warning here too.. or just put the import inside the test function

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

moved the import inside test function


import numpy as np

# XXX we should be testing the public API here
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Remove this and the spare blank lines

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

done

@jnothman jnothman changed the title Import linear assignment from scipy MNT Import linear assignment from scipy Mar 19, 2019
Copy link
Copy Markdown
Member

@NicolasHug NicolasHug left a comment

Choose a reason for hiding this comment

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

Some comments

# License: BSD

import numpy as np
# TODO: Remove this test module as the methods being tested
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Please add a line above with # 0.23 so we know when to remove this file

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

done

the original ordering.
"""
warnings.warn(
"This method is deprecated in 0.21 and will removed from 0.23 "
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Maybe indicate the function name and what to use instead:

The linear_assignment function is deprecated [...] Use scipy.optimize.linear_sum_assignment instead

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

done


# Deprecation warning for module
warnings.warn(
"This module is deprecated in 0.21 and will removed from 0.23 "
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
"This module is deprecated in 0.21 and will removed from 0.23 "
"The linear_assignment_ module is deprecated in 0.21 and will removed from 0.23 "

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

done

similarity = _jaccard
matrix = _pairwise_similarity(a, b, similarity)
indices = linear_assignment(1. - matrix)
row, col = linear_sum_assignment(1. - matrix)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
row, col = linear_sum_assignment(1. - matrix)
row_indices, col_indices = linear_sum_assignment(1. - matrix)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

done

Copy link
Copy Markdown
Member

@NicolasHug NicolasHug left a comment

Choose a reason for hiding this comment

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

Some comments


from sklearn.utils.linear_assignment_ import linear_assignment
from scipy.optimize import linear_sum_assignment
from sklearn.utils.validation import check_consistent_length, check_array
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

It's not you, but can you please change this toa relative import:

from ...utils.validation import check_consistent_length, check_array

Also Nitpick for pep8 compliance: the scipy import should be right below the numpy import, and there should be a blank line before the sklearn import:

import numpy as np
from scipy.optimize import linear_sum_assignment

from ...utils.validation import check_consistent_length, check_array

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

done

the original ordering.
"""
warnings.warn(
"The _hungarian function is deprecated in 0.21 "
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
"The _hungarian function is deprecated in 0.21 "
"The linear_assignment function is deprecated in 0.21 "

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

done

Copy link
Copy Markdown
Member

@NicolasHug NicolasHug left a comment

Choose a reason for hiding this comment

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

Last comment, LGTM, thanks @praths007


# Deprecation warning for module
warnings.warn(
"The linear_assignment function is deprecated in 0.21 "
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
"The linear_assignment function is deprecated in 0.21 "
"The linear_assignment_ module is deprecated in 0.21 "

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

done

@jnothman jnothman merged commit cfaf352 into scikit-learn:master Apr 17, 2019
@jnothman
Copy link
Copy Markdown
Member

Thanks @praths007

jeremiedbb pushed a commit to jeremiedbb/scikit-learn that referenced this pull request Apr 25, 2019
xhluca pushed a commit to xhluca/scikit-learn that referenced this pull request Apr 28, 2019
xhluca pushed a commit to xhluca/scikit-learn that referenced this pull request Apr 28, 2019
xhluca pushed a commit to xhluca/scikit-learn that referenced this pull request Apr 28, 2019
koenvandevelde pushed a commit to koenvandevelde/scikit-learn that referenced this pull request Jul 12, 2019
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.

3 participants