Skip to content

Commit 55494bf

Browse files
committed
handle warnings and jobs
1 parent e6feb48 commit 55494bf

File tree

1 file changed

+12
-14
lines changed

1 file changed

+12
-14
lines changed

monte-cover/src/montecover/base.py

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -87,15 +87,14 @@ def run_simulation(self, n_jobs=None):
8787
n_jobs : int, optional (default=None)
8888
Number of jobs to run in parallel. If None, uses the value from the configuration file.
8989
If 1, runs sequentially. If > 1, runs in parallel with the specified number of workers.
90-
If -1, uses all available CPU cores (except one).
90+
If -1, uses all available CPU cores .
91+
If -2, uses all available CPU cores except one.
9192
"""
9293
# Use n_jobs from parameter, or fall back to config value
9394
n_jobs = n_jobs if n_jobs is not None else self.default_n_jobs
94-
if n_jobs == -1:
95-
n_jobs = os.cpu_count() - 1
9695
self._log_parameters(n_jobs=n_jobs)
9796

98-
if n_jobs <= 1:
97+
if n_jobs == 1:
9998
for i_rep in range(self.repetitions):
10099
rep_start_time = time.time()
101100
self.logger.info(f"Starting repetition {i_rep + 1}/{self.repetitions}")
@@ -109,15 +108,11 @@ def run_simulation(self, n_jobs=None):
109108
rep_end_time = time.time()
110109
rep_duration = rep_end_time - rep_start_time
111110
self.logger.info(f"Repetition {i_rep+1} completed in {rep_duration:.2f}s")
112-
else:
113111

114-
repetitions_to_run = list(range(self.repetitions))
115-
116-
self.logger.info(f"Starting parallel execution with {n_jobs} workers")
112+
else:
113+
self.logger.info(f"Starting parallel execution with n_jobs={n_jobs}")
117114
results = Parallel(n_jobs=n_jobs, verbose=10)(
118-
delayed(self._process_repetition)(i_rep)
119-
for i_rep in repetitions_to_run
120-
if not self._stop_simulation()
115+
delayed(self._process_repetition)(i_rep) for i_rep in range(self.repetitions) if not self._stop_simulation()
121116
)
122117

123118
# Process results from parallel execution
@@ -243,6 +238,9 @@ def _stop_simulation(self) -> bool:
243238

244239
def _process_repetition(self, i_rep):
245240
"""Process a single repetition with all parameter combinations."""
241+
if self.suppress_warnings:
242+
warnings.simplefilter(action="ignore", category=UserWarning)
243+
246244
i_param_comb = 0
247245
rep_results = {}
248246

@@ -262,7 +260,7 @@ def _process_repetition(self, i_rep):
262260
if result_name not in rep_results:
263261
rep_results[result_name] = []
264262
rep_results[result_name].extend(result_list)
265-
263+
266264
return rep_results
267265

268266
def _process_parameter_combination(self, i_rep, i_param_comb, dgp_params, dml_params, dml_data):
@@ -283,7 +281,7 @@ def _process_parameter_combination(self, i_rep, i_param_comb, dgp_params, dml_pa
283281
# Process results
284282
if repetition_results is None:
285283
return {}
286-
284+
287285
# Add metadata to results
288286
processed_results = {}
289287
for result_name, repetition_result in repetition_results.items():
@@ -292,7 +290,7 @@ def _process_parameter_combination(self, i_rep, i_param_comb, dgp_params, dml_pa
292290
res["repetition"] = i_rep
293291
res.update(dgp_params)
294292
processed_results[result_name].append(res)
295-
293+
296294
return processed_results
297295

298296
except Exception as e:

0 commit comments

Comments
 (0)