Skip to content
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

ot.dist crashes for hamming distance without weights #400

Closed
nklkhlr opened this issue Sep 20, 2022 · 0 comments · Fixed by #402
Closed

ot.dist crashes for hamming distance without weights #400

nklkhlr opened this issue Sep 20, 2022 · 0 comments · Fixed by #402

Comments

@nklkhlr
Copy link

nklkhlr commented Sep 20, 2022

Bug Description

When calling ot.dist with metric set to hamming an AttributeError is thrown from within scipy.cdist

To Reproduce

(tested using python 3.10, ot version 0.8.2 and scipy version 1.9.1 as well as 1.8.0):

import ot
import numpy as np

x = np.random.randint(0, 2, 20).reshape(4, 5)
y = np.random.randint(0, 2, 20).reshape(4, 5)

ot.dist(x, y, metric="hamming")

Environment

  • OS: Ubuntu 22.04
  • Python version: 3.10
  • Install: conda

Traceback:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "miniconda3/envs/mtda/lib/python3.10/site-packages/ot/utils.py", line 237, in dist
    return cdist(x1, x2, metric=metric, w=w)
  File "miniconda3/envs/mtda/lib/python3.10/site-packages/scipy/spatial/distance.py", line 2929, in cdist
    return cdist_fn(XA, XB, out=out, **kwargs)
  File "miniconda3/envs/mtda/lib/python3.10/site-packages/scipy/spatial/distance.py", line 1673, in __call__
    XA, XB, typ, kwargs = _validate_cdist_input(
  File "miniconda3/envs/mtda/lib/python3.10/site-packages/scipy/spatial/distance.py", line 205, in _validate_cdist_input
    kwargs = _validate_kwargs((XA, XB), mA + mB, n, **kwargs)
  File "miniconda3/envs/mtda/lib/python3.10/site-packages/scipy/spatial/distance.py", line 225, in _validate_hamming_kwargs
    if w.ndim != 1 or w.shape[0] != n:
AttributeError: 'NoneType' object has no attribute 'ndim'

The same error is raised when scipy.cdist is called with w=None

from scipy.spatial.distance import cdist

cdist(x, y, metric="hamming", w=None)

Hence it seems like not passing w here

POT/ot/utils.py

Line 237 in 951209a

return cdist(x1, x2, metric=metric, w=w)

when w is None should resolve the bug.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant