Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[project]
name = "cell-eval"
version = "0.7.1"
version = "0.7.2"
description = "Evaluation metrics for single-cell perturbation predictions"
readme = "README.md"
authors = [
Expand Down
18 changes: 17 additions & 1 deletion src/cell_eval/_evaluator.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,19 @@
logger = logging.getLogger(__name__)


def _available_cpus() -> int:
"""Return CPUs the current process is allowed to use.

Uses ``os.sched_getaffinity`` on Linux so SLURM/cgroup/taskset limits are
respected; falls back to ``mp.cpu_count`` on macOS/Windows where that API
is unavailable (those platforms typically run locally without cgroup caps).
"""
try:
return len(os.sched_getaffinity(0))
except AttributeError:
return mp.cpu_count()
Comment on lines +28 to +31
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

It is recommended to catch OSError in addition to AttributeError. In some restricted or containerized environments, os.sched_getaffinity might be present in the os module but blocked by security policies (e.g., seccomp), which would raise an OSError. Additionally, ensuring the returned value is at least 1 is a good defensive practice for downstream tools like Numba that require a positive thread count.

Suggested change
try:
return len(os.sched_getaffinity(0))
except AttributeError:
return mp.cpu_count()
try:
return max(1, len(os.sched_getaffinity(0)))
except (AttributeError, OSError):
return max(1, mp.cpu_count())

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that's overengineering and doesn't really add much benefit for four new lines of code.



class MetricsEvaluator:
"""
Evaluates benchmarking metrics of a predicted and real anndata object.
Expand Down Expand Up @@ -70,6 +83,9 @@ def __init__(
# Enable a global string cache for categorical columns
pl.enable_string_cache()

if num_threads == -1:
num_threads = _available_cpus()

if os.path.exists(outdir):
logger.warning(
f"Output directory {outdir} already exists, potential overwrite occurring"
Expand All @@ -91,7 +107,7 @@ def __init__(
anndata_pair=self.anndata_pair,
de_pred=de_pred,
de_real=de_real,
num_threads=num_threads if num_threads != -1 else mp.cpu_count(),
num_threads=num_threads,
allow_discrete=allow_discrete,
outdir=outdir,
prefix=prefix,
Expand Down
Loading