SweepExpParallel#
- class sweepexp.sweepexp_parallel.SweepExpParallel(func: Callable, parameters: dict[str, list], save_path: Path | str | None = None, **kwargs: any)[source]#
Bases:
SweepExpRun a parameter sweep in parallel using multiprocessing.
Parameters#
- funcCallable
The experiment function to run. The function should take the parameters as keyword arguments and return a dictionary with the return values.
- parametersdict[str, list]
The parameters to sweep over. The keys are the parameter names and the values are lists of the parameter values.
- save_pathPath | str | None
The path to save the results to. Supported file formats are: ‘.zarr’, ‘.nc’, ‘.cdf’, ‘.pkl’. The ‘.zarr’ and ‘.nc’ formats only support numeric and boolean data. Only the ‘.pkl’ format supports saving data of any type.
Description#
The SweepExpParallel class can be used to run a custom experiment function with different parameter combinations. The results of the experiments are saved as an xarray dataset. The dataset can be saved to a file and loaded later to continue the experiments. All parameter combinations are run in parallel using multiprocessing. The number of workers can be specified using the ‘max_workers’ parameter in the ‘run’ method.
SweepExp supports the following additional features: - Custom arguments: Add custom arguments to the experiment function. - UUID: Pass a unique identifier to the experiment function. - Auto save: Automatically save the results after each experiment. - Timeit: Measure the duration of each experiment. - Priorities: Run experiments with higher priority first.
Examples#
from sweepexp import SweepExpParallel # Create a simple experiment function def my_experiment(x: int, y: float) -> dict: return {"sum": x + y, "product": x * y} # Initialize the SweepExp object sweep = SweepExpParallel( func=my_experiment, parameters={"x": [1, 2, 3], "y": [4, 5, 6]}, ) # Run the sweep sweep.run()
- __init__(func: Callable, parameters: dict[str, list], save_path: Path | str | None = None, **kwargs: any) None#
Methods
__init__(func, parameters[, save_path])add_custom_argument(name, default_value)Add custom arguments to the experiment function.
load(save_path)Load the xarray dataset from a file.
reset_status(states)Reset the status of experiments to 'N' (not started).
run([status, max_workers])Run all experiments with the status 'N' (not started).
save([mode])Save the xarray dataset to the save path.
Attributes
auto_saveWhether to automatically save the results after each finished experiment.
custom_argumentsCustom arguments of the experiment function.
dataThe data of the experiment.
durationThe duration of each experiment.
enable_prioritiesWhether to enable priorities for the experiments.
funcThe experiment function to run.
parametersThe parameters to sweep over.
pass_uuidWhether to pass the uuid to the experiment function.
priorityThe priority of each experiment.
save_pathPath to save the results to.
shapeThe shape of the parameter grid.
statusThe status of each parameter combination.
timeitWhether to measure the duration of each experiment.
uuidThe uuid of each parameter combination.
- run(status: str | list[str] | None = 'N', max_workers: int | None = None) xr.Dataset[source]#
Run all experiments with the status ‘N’ (not started).
Parameters#
- statusstr | list[str] | None
The status of the experiments to run. If None, all experiments with status ‘N’ (not started) are run.
- max_workersint | None
The maximum number of workers to run in parallel. If None, the number of workers is set to the number of CPUs available. Only relevant for parallel mode (‘mode=parallel’).
Returns#
- xr.Dataset
The xarray dataset with the results of the experiments.
Examples#
from sweepexp import SweepExp sweep = SweepExp(...) # Initialize the sweep # Run all experiments with status 'N' sweep.run() # Run all experiments with status 'C' sweep.run("C") # Run all experiments with status 'S' and 'F' sweep.run(["S", "F"])