From c66a315fa390cf189568af2f59a83870724cebb8 Mon Sep 17 00:00:00 2001 From: Adam Tyson Date: Tue, 30 Apr 2024 18:37:57 +0100 Subject: [PATCH] Allow number of free CPU cores to be set in the GUI (#195) * expose n_free_cpus parameter to in GUI * Pass n_free_cpus to worker --- brainreg/core/cli.py | 2 +- brainreg/napari/register.py | 21 ++++++++++++++++----- brainreg/napari/util.py | 5 +++-- 3 files changed, 20 insertions(+), 8 deletions(-) diff --git a/brainreg/core/cli.py b/brainreg/core/cli.py index cf4738d..6214da9 100644 --- a/brainreg/core/cli.py +++ b/brainreg/core/cli.py @@ -98,7 +98,7 @@ def misc_parse(parser): "--n-free-cpus", dest="n_free_cpus", type=check_positive_int, - default=4, + default=2, help="The number of CPU cores on the machine to leave " "unused by the program to spare resources.", ) diff --git a/brainreg/napari/register.py b/brainreg/napari/register.py index 3d7bacf..f665f0d 100644 --- a/brainreg/napari/register.py +++ b/brainreg/napari/register.py @@ -132,6 +132,7 @@ def brainreg_register(): smoothing_sigma_floating=-1.0, histogram_n_bins_floating=128, histogram_n_bins_reference=128, + n_free_cpus=2, debug=False, ) @@ -216,6 +217,10 @@ def brainreg_register(): value=DEFAULT_PARAMETERS["histogram_n_bins_reference"], label="histogram_n_bins_reference", ), + n_free_cpus=dict( + value=DEFAULT_PARAMETERS["n_free_cpus"], + label="Free CPU cores", + ), debug=dict( value=DEFAULT_PARAMETERS["debug"], label="Debug mode", @@ -245,8 +250,9 @@ def widget( grid_spacing: int, smoothing_sigma_reference: float, smoothing_sigma_floating: float, - histogram_n_bins_floating: int, - histogram_n_bins_reference: int, + histogram_n_bins_floating: float, + histogram_n_bins_reference: float, + n_free_cpus: int, debug: bool, reset_button, check_orientation_button, @@ -339,6 +345,8 @@ def widget( Number of bins used for the generation of the histograms used for the calculation of Normalized Mutual Information on the reference image + n_free_cpus : int + How many CPU cores to leave free debug: bool Activate debug mode (save intermediate steps). check_orientation_button: @@ -401,7 +409,7 @@ def get_gui_logging_args(): ) @thread_worker - def run(): + def run(n_free_cpus): paths = Paths(pathlib.Path(registration_output_folder)) niftyreg_args = NiftyregArgs( @@ -438,7 +446,10 @@ def run(): scaling, load_parallel, ) = initialise_brainreg( - atlas_key.value, data_orientation, voxel_sizes + atlas_key.value, + data_orientation, + voxel_sizes, + n_free_cpus=n_free_cpus, ) additional_images_downsample = get_additional_images_downsample( @@ -491,7 +502,7 @@ def run(): f"{paths.registration_output_folder}" ) - worker = run() + worker = run(n_free_cpus) if not block: worker.returned.connect(load_registration_as_layers) diff --git a/brainreg/napari/util.py b/brainreg/napari/util.py index c8c4a52..cf04a69 100644 --- a/brainreg/napari/util.py +++ b/brainreg/napari/util.py @@ -9,9 +9,10 @@ from tqdm import tqdm -def initialise_brainreg(atlas_key, data_orientation_key, voxel_sizes): +def initialise_brainreg( + atlas_key, data_orientation_key, voxel_sizes, n_free_cpus=2 +): scaling_rounding_decimals = 5 - n_free_cpus = 2 atlas = BrainGlobeAtlas(atlas_key) source_space = bg.AnatomicalSpace(data_orientation_key)