Skip to content

Commit

Permalink
CI Adapt handling of discarded fused typed memoryview (#25425)
Browse files Browse the repository at this point in the history
Co-authored-by: Olivier Grisel <olivier.grisel@ensta.org>
  • Loading branch information
jjerphan and ogrisel committed Jan 18, 2023
1 parent d60af3d commit f1340c7
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
5 changes: 3 additions & 2 deletions sklearn/datasets/_svmlight_format_fast.pyx
Expand Up @@ -187,6 +187,7 @@ def _dump_svmlight_file(
bint y_is_sp,
):
cdef bint X_is_integral
cdef bint query_id_is_not_empty = query_id.size > 0
X_is_integral = X.dtype.kind == "i"
if X_is_integral:
value_pattern = "%d:%d"
Expand All @@ -198,7 +199,7 @@ def _dump_svmlight_file(
label_pattern = "%.16g"

line_pattern = "%s"
if query_id is not None:
if query_id_is_not_empty:
line_pattern += " qid:%d"
line_pattern += " %s\n"

Expand Down Expand Up @@ -246,7 +247,7 @@ def _dump_svmlight_file(
else:
labels_str = label_pattern % y[i,0]

if query_id is not None:
if query_id_is_not_empty:
feat = (labels_str, query_id[i], s)
else:
feat = (labels_str, s)
Expand Down
8 changes: 7 additions & 1 deletion sklearn/datasets/_svmlight_format_io.py
Expand Up @@ -506,7 +506,13 @@ def dump_svmlight_file(
if hasattr(X, "sort_indices"):
X.sort_indices()

if query_id is not None:
if query_id is None:
# NOTE: query_id is passed to Cython functions using a fused type on query_id.
# Yet as of Cython>=3.0, memory views can't be None otherwise the runtime
# would not known which concrete implementation to dispatch the Python call to.
# TODO: simplify interfaces and implementations in _svmlight_format_fast.pyx.
query_id = np.array([], dtype=np.int32)
else:
query_id = np.asarray(query_id)
if query_id.shape[0] != y.shape[0]:
raise ValueError(
Expand Down

0 comments on commit f1340c7

Please sign in to comment.