33import numpy as np
44from typing import Callable , Any , Union
55
6+
67def evaluate_row (row : Union [np .ndarray , list ], objective_function : Callable [[np .ndarray , Any ], Any ], fun_control : Any ) -> Any :
78 """
89 Evaluates a single row using the provided objective function.
@@ -15,7 +16,7 @@ def evaluate_row(row: Union[np.ndarray, list], objective_function: Callable[[np.
1516
1617 Returns:
1718 The result of the objective function applied to the row.
18-
19+
1920 Examples:
2021 >>> from spotpython.utils.parallel import evaluate_row
2122 >>> import numpy as np
@@ -28,10 +29,11 @@ def evaluate_row(row: Union[np.ndarray, list], objective_function: Callable[[np.
2829 """
2930 return objective_function (np .array ([row ]), fun_control )
3031
31- def parallel_objective_function (objective_function , X , num_cores , fun_control , method )-> np .ndarray :
32+
33+ def parallel_objective_function (objective_function , X , num_cores , fun_control , method ) -> np .ndarray :
3234 """
3335 Executes an objective function in parallel using either multiprocessing or joblib.
34-
36+
3537 Args:
3638 objective_function (callable): The function to be evaluated for each row in `X`.
3739 X (iterable): The input data, where each element represents a row to be processed.
@@ -40,13 +42,13 @@ def parallel_objective_function(objective_function, X, num_cores, fun_control, m
4042 method (str): The parallelization method to use. Options are:
4143 - 'mp': Use Python's multiprocessing module.
4244 - 'joblib': Use the joblib library.
43-
45+
4446 Returns:
4547 numpy.ndarray: A flattened array of results obtained by applying the objective function to each row in `X`.
46-
48+
4749 Raises:
4850 ValueError: If an unsupported `method` is provided.
49-
51+
5052 Examples:
5153 >>> from spotpython.utils.parallel import parallel_objective_function
5254 >>> import numpy as np
@@ -58,33 +60,34 @@ def parallel_objective_function(objective_function, X, num_cores, fun_control, m
5860 array([16, 25, 34])
5961 >>> parallel_objective_function(sample_objective, X, num_cores=2, fun_control=fun_control, method='joblib')
6062 array([16, 25, 34])
61- """
62- with Manager () as manager :
63+ """
64+ with Manager () as manager :
6365 shared_control = manager .dict (fun_control )
64- if method == 'mp' :
66+ if method == "mp" :
6567 with Pool (processes = num_cores ) as pool :
6668 results = pool .starmap (evaluate_row , [(row , objective_function , shared_control ) for row in X ])
67- elif method == ' joblib' :
69+ elif method == " joblib" :
6870 results = Parallel (n_jobs = num_cores )(delayed (evaluate_row )(row , objective_function , shared_control ) for row in X )
6971
7072 return np .array (results ).flatten ()
7173
72- def make_parallel (obj_func , num_cores , method = 'mp' )-> Callable :
74+
75+ def make_parallel (obj_func , num_cores , method = "mp" ) -> Callable :
7376 """
7477 Creates a parallelized wrapper function for the given objective function.
75-
78+
7679 Args:
77- obj_func (callable): The objective function to be parallelized.
80+ obj_func (callable): The objective function to be parallelized.
7881 It should accept the same arguments as the wrapper function.
7982 num_cores (int): The number of cores to use for parallel processing.
80- method (str, optional): The parallelization method to use.
81- Defaults to 'mp' (multiprocessing). Other methods may be supported
83+ method (str, optional): The parallelization method to use.
84+ Defaults to 'mp' (multiprocessing). Other methods may be supported
8285 depending on the implementation of `parallel_objective_function`.
83-
86+
8487 Returns:
85- callable: A wrapper function that executes the objective function
88+ callable: A wrapper function that executes the objective function
8689 in parallel using the specified number of cores and method.
87-
90+
8891 Examples:
8992 >>> from spotpython.utils.parallel import make_parallel
9093 >>> def sample_function(x):
@@ -96,7 +99,8 @@ def make_parallel(obj_func, num_cores, method='mp')->Callable:
9699 [1, 4, 9, 16]
97100 """
98101 global parallel_wrap
102+
99103 def parallel_wrap (X , fun_control = None ):
100104 return parallel_objective_function (obj_func , X , num_cores , fun_control , method )
101-
105+
102106 return parallel_wrap
0 commit comments