Skip to content

Commit de639c8

Browse files
committed
less cores
1 parent b64f821 commit de639c8

1 file changed

Lines changed: 24 additions & 2 deletions

File tree

src/cell2sentence4longevity/explore.py

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -454,7 +454,12 @@ def extract(
454454
rendered_log = log_dir / "extract.log"
455455
to_nice_file(output_file=json_log, rendered_file=rendered_log)
456456
else:
457-
to_nice_stdout(output_file=Path("./extract.json"))
457+
# Default to ./logs directory
458+
default_log_dir = Path("./logs")
459+
default_log_dir.mkdir(parents=True, exist_ok=True)
460+
json_log = default_log_dir / "extract.json"
461+
rendered_log = default_log_dir / "extract.log"
462+
to_nice_file(output_file=json_log, rendered_file=rendered_log)
458463

459464
# Determine output path
460465
if output_path is None:
@@ -510,6 +515,7 @@ def batch(
510515
summary_format: str = typer.Option("csv", "--summary-format", help="Format for summary files: 'csv' or 'tsv' (default: csv)"),
511516
log_dir: Optional[Path] = typer.Option(None, "--log-dir", help="Directory for log files"),
512517
skip_existing: bool = typer.Option(True, "--skip-existing/--overwrite", help="Skip files that already have output"),
518+
max_threads: Optional[int] = typer.Option(None, "--max-threads", help="Maximum number of threads for Polars (default: 8, set to 1 for minimal memory)"),
513519
) -> None:
514520
"""Extract metadata fields from multiple h5ad files in batch.
515521
@@ -526,6 +532,17 @@ def batch(
526532
# Specify custom output directory
527533
explore batch ./data/input --output-dir ./custom/output
528534
"""
535+
# Limit Polars thread pool to prevent memory overload with many files
536+
# Use half of available cores by default to balance speed and memory
537+
import os
538+
if max_threads is not None:
539+
threads = max_threads
540+
else:
541+
cpu_count = os.cpu_count() or 4
542+
threads = max(1, cpu_count // 2) # Half of cores, minimum 1
543+
os.environ['POLARS_MAX_THREADS'] = str(threads)
544+
typer.echo(f"Using {threads} threads for Polars operations")
545+
529546
# Determine output directory
530547
if output_dir is None:
531548
output_dir = Path("data/output/meta")
@@ -537,7 +554,12 @@ def batch(
537554
rendered_log = log_dir / "batch_extract.log"
538555
to_nice_file(output_file=json_log, rendered_file=rendered_log)
539556
else:
540-
to_nice_stdout(output_file=Path("./batch_extract.json"))
557+
# Default to ./logs directory
558+
default_log_dir = Path("./logs")
559+
default_log_dir.mkdir(parents=True, exist_ok=True)
560+
json_log = default_log_dir / "batch_extract.json"
561+
rendered_log = default_log_dir / "batch_extract.log"
562+
to_nice_file(output_file=json_log, rendered_file=rendered_log)
541563

542564
with start_action(
543565
action_type="batch_extract_fields",

0 commit comments

Comments
 (0)