-
-
Notifications
You must be signed in to change notification settings - Fork 26k
[MRG] Raise exception on providing complex data to estimators #9551
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
Changes from all commits
9a9cbfe
709437e
09c5732
8d55a60
63ef59f
cd863c8
939d781
b84e1ee
7d977f2
e4b437f
1a3abb6
b26e99d
694b3b3
843230f
c065851
51d7f47
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,6 +13,7 @@ | |
|
||
import numpy as np | ||
import scipy.sparse as sp | ||
from numpy.core.numeric import ComplexWarning | ||
|
||
from ..externals import six | ||
from ..utils.fixes import signature | ||
|
@@ -276,6 +277,13 @@ def _ensure_sparse_format(spmatrix, accept_sparse, dtype, copy, | |
return spmatrix | ||
|
||
|
||
def _ensure_no_complex_data(array): | ||
if hasattr(array, 'dtype') and array.dtype is not None \ | ||
and hasattr(array.dtype, 'kind') and array.dtype.kind == "c": | ||
raise ValueError("Complex data not supported\n" | ||
"{}\n".format(array)) | ||
|
||
|
||
def check_array(array, accept_sparse=False, dtype="numeric", order=None, | ||
copy=False, force_all_finite=True, ensure_2d=True, | ||
allow_nd=False, ensure_min_samples=1, ensure_min_features=1, | ||
|
@@ -396,10 +404,28 @@ def check_array(array, accept_sparse=False, dtype="numeric", order=None, | |
context = " by %s" % estimator_name if estimator is not None else "" | ||
|
||
if sp.issparse(array): | ||
_ensure_no_complex_data(array) | ||
array = _ensure_sparse_format(array, accept_sparse, dtype, copy, | ||
force_all_finite) | ||
else: | ||
array = np.array(array, dtype=dtype, order=order, copy=copy) | ||
# If np.array(..) gives ComplexWarning, then we convert the warning | ||
# to an error. This is needed because specifying a non complex | ||
# dtype to the function converts complex to real dtype, | ||
# thereby passing the test made in the lines following the scope | ||
# of warnings context manager. | ||
with warnings.catch_warnings(): | ||
try: | ||
warnings.simplefilter('error', ComplexWarning) | ||
array = np.array(array, dtype=dtype, order=order, copy=copy) | ||
except ComplexWarning: | ||
raise ValueError("Complex data not supported\n" | ||
"{}\n".format(array)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unfortunately, at this point the array has been converted to float or int and the error message will be confusing. We need to keep a reference to the original input with complex values to report it in this error message (and There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Or maybe: with warnings.catch_warnings():
try:
warnings.simplefilter('error', ComplexWarning)
new_array = np.array(array, dtype=dtype, order=order, copy=copy)
except ComplexWarning:
raise ValueError("Complex data not supported\n"
"{}\n".format(array))
array = new_array
del new_array There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hum sorry my previous comments are wrong. Your code is fine. |
||
|
||
# It is possible that the np.array(..) gave no warning. This happens | ||
# when no dtype conversion happend, for example dtype = None. The | ||
# result is that np.array(..) produces an array of complex dtype | ||
# and we need to catch and raise exception for such cases. | ||
_ensure_no_complex_data(array) | ||
|
||
if ensure_2d: | ||
if array.ndim == 1: | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see that this branch is covered, but I don't see where. We're never passing
dtype
right?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Never passing dtype to check_array? Even if we don't pass it to check_array, the value of dtype changes within this function. For example lines 381-386. The ComplexWarning comes only if we are setting dtype to some one of the real types. However, if dtype is "None", no conversion takes place and no warning is produced, so we need to check that case again in subsequentl lines
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One example where dtype is explicitly passed to the function is that of svm decision function