Skip to content

Commit 4ac081e

Browse files
franchuteriveracharlesfu4
authored andcommitted
Add exceptions to log file, not just stdout (automl#863)
* Add exceptions to log file, not just stdout * Removing dummy pred as trys is not needed
1 parent b7abdb8 commit 4ac081e

File tree

2 files changed

+56
-10
lines changed

2 files changed

+56
-10
lines changed

autosklearn/automl.py

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -506,16 +506,21 @@ def _fit(
506506
get_smac_object_callback=self._get_smac_object_callback,
507507
smac_scenario_args=self._smac_scenario_args,
508508
)
509-
self.runhistory_, self.trajectory_, self._budget_type = \
510-
_proc_smac.run_smbo()
511-
trajectory_filename = os.path.join(
512-
self._backend.get_smac_output_directory_for_run(self._seed),
513-
'trajectory.json')
514-
saveable_trajectory = \
515-
[list(entry[:2]) + [entry[2].get_dictionary()] + list(entry[3:])
516-
for entry in self.trajectory_]
517-
with open(trajectory_filename, 'w') as fh:
518-
json.dump(saveable_trajectory, fh)
509+
510+
try:
511+
self.runhistory_, self.trajectory_, self._budget_type = \
512+
_proc_smac.run_smbo()
513+
trajectory_filename = os.path.join(
514+
self._backend.get_smac_output_directory_for_run(self._seed),
515+
'trajectory.json')
516+
saveable_trajectory = \
517+
[list(entry[:2]) + [entry[2].get_dictionary()] + list(entry[3:])
518+
for entry in self.trajectory_]
519+
with open(trajectory_filename, 'w') as fh:
520+
json.dump(saveable_trajectory, fh)
521+
except Exception as e:
522+
self._logger.exception(e)
523+
raise
519524

520525
# Wait until the ensemble process is finished to avoid shutting down
521526
# while the ensemble builder tries to access the data

test/test_automl/test_automl.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,47 @@ def test_fail_if_dummy_prediction_fails(self, ta_run_mock):
393393
self._tearDown(backend_api.temporary_directory)
394394
self._tearDown(backend_api.output_directory)
395395

396+
@unittest.mock.patch('autosklearn.smbo.AutoMLSMBO.run_smbo')
397+
def test_exceptions_inside_log_in_smbo(self, smbo_run_mock):
398+
399+
# Make sure that any exception during the AutoML fit due to
400+
# SMAC are properly captured in a log file
401+
backend_api = self._create_backend('test_exceptions_inside_log')
402+
self._tearDown(backend_api.temporary_directory)
403+
self._tearDown(backend_api.output_directory)
404+
405+
automl = autosklearn.automl.AutoML(backend_api, 20, 5)
406+
407+
output_file = 'test_exceptions_inside_log.log'
408+
setup_logger(output_file=output_file)
409+
logger = get_logger('test_exceptions_inside_log')
410+
411+
# Create a custom exception to prevent other errors to slip in
412+
class MyException(Exception):
413+
pass
414+
415+
X_train, Y_train, X_test, Y_test = putil.get_dataset('iris')
416+
# The first call is on dummy predictor failure
417+
message = str(np.random.randint(100)) + '_run_smbo'
418+
smbo_run_mock.side_effect = MyException(message)
419+
420+
with unittest.mock.patch('autosklearn.automl.AutoML._get_logger') as mock:
421+
mock.return_value = logger
422+
with self.assertRaises(MyException):
423+
automl.fit(
424+
X_train,
425+
Y_train,
426+
metric=accuracy,
427+
task=MULTICLASS_CLASSIFICATION,
428+
)
429+
with open(output_file) as f:
430+
self.assertTrue(message in f.read())
431+
432+
# Cleanup
433+
os.unlink(output_file)
434+
self._tearDown(backend_api.temporary_directory)
435+
self._tearDown(backend_api.output_directory)
436+
396437

397438
if __name__ == "__main__":
398439
unittest.main()

0 commit comments

Comments
 (0)