bootphon / ABXpy

ABX discrimination task in python
https://docs.cognitive-ml.fr/ABXpy
MIT License
43 stars 16 forks source link

cosine.cosine_distance: ComplexWarning: Casting complex values to real discards the imaginary part #22

Open lemarcantoine opened 2 years ago

lemarcantoine commented 2 years ago

Hello, when running:

import numpy as np
import ABXpy.distances.metrics.cosine as cosine

a = np.array([[1.17004299, 0.85545695, 1.00981605, 1.16844952, 0.63780457, 0.86987048]])
cosine.cosine_distance(a, a)

I get the following warning:

/home/username/anaconda3/lib/python3.7/site-packages/ABXpy/distances/metrics/cosine.py:25: ComplexWarning: Casting complex values to real discards the imaginary part
  d = np.array([[np.float64(np.lib.scimath.arccos(d[0, 0]) / np.pi)]])

I believe its because the inner intermediate d var is slightly higher than 1 because of a float rounding.

lemarcantoine commented 2 years ago

I fixed it on my side by adding:

d = np.clip(d, -1, 1)

Right after the assignment of d.