From 1bffe87459b60756549531c3a8b27f34b5e921e8 Mon Sep 17 00:00:00 2001 From: Arthit Suriyawongkul Date: Mon, 28 Sep 2026 14:42:04 +0100 Subject: [PATCH 1/2] fix: use np.asarray in meter curves for NumPy 2 - Replace np.array(..., copy=False), which raises on NumPy 2, with np.asarray (in score_vs_true and precision_recall_curve) - Add a regression test Signed-off-by: Arthit Suriyawongkul --- python/fasttext_module/fasttext/FastText.py | 4 +-- .../fasttext/tests/test_meter.py | 27 +++++++++++++++++++ 2 files changed, 29 insertions(+), 2 deletions(-) create mode 100644 python/fasttext_module/fasttext/tests/test_meter.py diff --git a/python/fasttext_module/fasttext/FastText.py b/python/fasttext_module/fasttext/FastText.py index c69311f..d4081b4 100644 --- a/python/fasttext_module/fasttext/FastText.py +++ b/python/fasttext_module/fasttext/FastText.py @@ -38,7 +38,7 @@ def score_vs_true(self, label): else: y_scores, y_true = ([], ()) - return np.array(y_scores, copy=False), np.array(y_true, copy=False) + return np.asarray(y_scores), np.asarray(y_true) def precision_recall_curve(self, label=None): """Return precision/recall curve""" @@ -53,7 +53,7 @@ def precision_recall_curve(self, label=None): else: precision, recall = ([], ()) - return np.array(precision, copy=False), np.array(recall, copy=False) + return np.asarray(precision), np.asarray(recall) def precision_at_recall(self, recall, label=None): """Return precision for a given recall""" diff --git a/python/fasttext_module/fasttext/tests/test_meter.py b/python/fasttext_module/fasttext/tests/test_meter.py new file mode 100644 index 0000000..394c051 --- /dev/null +++ b/python/fasttext_module/fasttext/tests/test_meter.py @@ -0,0 +1,27 @@ +# SPDX-FileContributor: Arthit Suriyawongkul +# SPDX-FileCopyrightText: 2026-present, fasttext-community +# SPDX-FileType: SOURCE +# SPDX-License-Identifier: MIT + +"""Meter curves must work with NumPy 2 (np.array copy=False raises there).""" + +import numpy as np + +from .helpers import build_supervised_model, get_random_data + + +def test_meter_returns_arrays(tmp_path): + data = get_random_data(300, max_vocab_size=100) + # thread=12: thread <= 10 leaves the input matrix partly uninitialized. + model = build_supervised_model(data, {"thread": 12, "dim": 16, "verbose": 0}) + path = tmp_path / "test.txt" + path.write_text("".join(f"__label__{line}\n" for line in data)) + meter = model.get_meter(str(path)) + label = model.labels[0] + for x, y in ( + meter.score_vs_true(label), + meter.precision_recall_curve(), + meter.precision_recall_curve(label), + ): + assert isinstance(x, np.ndarray) and isinstance(y, np.ndarray) + assert len(x) == len(y) > 0 From 69c6f311495ce563452d719184fc3992592ec9ee Mon Sep 17 00:00:00 2001 From: Arthit Suriyawongkul Date: Mon, 28 Sep 2026 14:46:08 +0100 Subject: [PATCH 2/2] Reduce test size Signed-off-by: Arthit Suriyawongkul --- .../fasttext_module/fasttext/tests/test_meter.py | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/python/fasttext_module/fasttext/tests/test_meter.py b/python/fasttext_module/fasttext/tests/test_meter.py index 394c051..1d0d42d 100644 --- a/python/fasttext_module/fasttext/tests/test_meter.py +++ b/python/fasttext_module/fasttext/tests/test_meter.py @@ -11,17 +11,11 @@ def test_meter_returns_arrays(tmp_path): - data = get_random_data(300, max_vocab_size=100) + data = get_random_data(100) # thread=12: thread <= 10 leaves the input matrix partly uninitialized. - model = build_supervised_model(data, {"thread": 12, "dim": 16, "verbose": 0}) + model = build_supervised_model(data, {"thread": 12}) path = tmp_path / "test.txt" path.write_text("".join(f"__label__{line}\n" for line in data)) meter = model.get_meter(str(path)) - label = model.labels[0] - for x, y in ( - meter.score_vs_true(label), - meter.precision_recall_curve(), - meter.precision_recall_curve(label), - ): - assert isinstance(x, np.ndarray) and isinstance(y, np.ndarray) - assert len(x) == len(y) > 0 + for pair in (meter.score_vs_true(model.labels[0]), meter.precision_recall_curve()): + assert all(isinstance(a, np.ndarray) for a in pair)