From c1bc414cc4b93748eb22594a83fd82efba47bbd8 Mon Sep 17 00:00:00 2001 From: btraven Date: Tue, 1 Dec 2026 01:29:51 +0100 Subject: [PATCH] ensure num threads >= 1 --- lib/src/nclsoverlaps.cpp | 6 ++++++ lib/src/nclssearch.cpp | 12 ++++++++++++ tests/test_num_threads.py | 29 +++++++++++++++++++++++++++++ 3 files changed, 47 insertions(+) create mode 100644 tests/test_num_threads.py diff --git a/lib/src/nclsoverlaps.cpp b/lib/src/nclsoverlaps.cpp index 2c61a85..e2ac258 100644 --- a/lib/src/nclsoverlaps.cpp +++ b/lib/src/nclsoverlaps.cpp @@ -41,6 +41,9 @@ pybind11::tuple perform_find_overlaps( int min_overlap, int num_threads=1) { + if (num_threads < 1) { + throw std::invalid_argument("num_threads must be >= 1"); + } py::buffer_info q_starts_buf = query_starts.request(); py::buffer_info q_ends_buf = query_ends.request(); @@ -160,6 +163,9 @@ pybind11::tuple perform_find_overlaps_groups( int min_overlap, int num_threads=1) { + if (num_threads < 1) { + throw std::invalid_argument("num_threads must be >= 1"); + } auto s_starts_ptr = static_cast(self_starts.request().ptr); auto s_ends_ptr = static_cast(self_ends.request().ptr); auto q_starts_ptr = static_cast(query_starts.request().ptr); diff --git a/lib/src/nclssearch.cpp b/lib/src/nclssearch.cpp index 8bc90ca..253a44b 100644 --- a/lib/src/nclssearch.cpp +++ b/lib/src/nclssearch.cpp @@ -44,6 +44,9 @@ py::object perform_follow( const std::string& select, int num_threads = 1) { + if (num_threads < 1) { + throw std::invalid_argument("num_threads must be >= 1"); + } auto q_starts_ptr = static_cast(query_starts.request().ptr); Index n_queries = query_starts.request().shape[0]; @@ -111,6 +114,9 @@ py::object perform_precede( const std::string& select, int num_threads = 1) { + if (num_threads < 1) { + throw std::invalid_argument("num_threads must be >= 1"); + } auto q_ends_ptr = static_cast(query_ends.request().ptr); Index n_queries = query_ends.request().shape[0]; @@ -179,6 +185,9 @@ py::object perform_nearest( int num_threads=1, bool adjacent_equals_overlap=false) { + if (num_threads < 1) { + throw std::invalid_argument("num_threads must be >= 1"); + } auto q_starts_ptr = static_cast(query_starts.request().ptr); auto q_ends_ptr = static_cast(query_ends.request().ptr); Index n_queries = query_starts.request().shape[0]; @@ -275,6 +284,9 @@ pybind11::tuple perform_nearest_groups( int num_threads=1, bool adjacent_equals_overlap=false) { + if (num_threads < 1) { + throw std::invalid_argument("num_threads must be >= 1"); + } auto s_starts_ptr = static_cast(self_starts.request().ptr); auto s_ends_ptr = static_cast(self_ends.request().ptr); auto q_starts_ptr = static_cast(query_starts.request().ptr); diff --git a/tests/test_num_threads.py b/tests/test_num_threads.py new file mode 100644 index 0000000..155701f --- /dev/null +++ b/tests/test_num_threads.py @@ -0,0 +1,29 @@ +import numpy as np +import pytest + +from iranges import IRanges, lib_iranges + +__author__ = "btraven" +__copyright__ = "jkanche" +__license__ = "MIT" + +ir = IRanges([2, 10], [1, 3]) +starts, ends = np.array([2, 10], dtype=np.int32), np.array([3, 13], dtype=np.int32) +groups = [np.array([0, 1], dtype=np.int32)] + +CALLS = { + "find_overlaps": lambda: ir.find_overlaps(ir, num_threads=0), + "precede": lambda: ir.precede(ir, num_threads=0), + "follow": lambda: ir.follow(ir, num_threads=0), + "nearest": lambda: ir.nearest(ir, num_threads=0), + "find_overlaps_groups": lambda: lib_iranges.find_overlaps_groups( + starts, ends, groups, starts, ends, groups, num_threads=0 + ), + "nearest_groups": lambda: lib_iranges.nearest_groups(starts, ends, groups, starts, ends, groups, num_threads=0), +} + + +@pytest.mark.parametrize("name", CALLS) +def test_num_threads_zero_raises(name): + with pytest.raises(ValueError, match="num_threads must be >= 1"): + CALLS[name]()