def test_pickle_serializable_experiment_success(self): experiment = PickleOnlySerializableExeperiment() runner = ExperimentRunner() specification = {"test": "test"} runner.run("test", [specification], experiment, specification_runner=MainRunner()) self.assertIn("run.pkl", os.listdir(get_save_file_directory('test', specification))) self.assertIn("specification.json", os.listdir(get_save_file_directory('test', specification)))
def test_with_runner(self): experiment = SerializableExperiment() runner = ExperimentRunner() specification = {"test": "test"} runner.run("test", [specification], experiment, specification_runner=MainRunner()) self.assertEqual( 1, len(os.listdir(get_save_file_directory('test', specification))))
def save_run(name, experiment, specification, result, force_pickle, diff_namer, extended_keys=False): os.makedirs(get_save_file_directory(name, specification, diff_namer, extended_keys), exist_ok=True) output_dictionary = {"specification": specification, "result": result} json_serialize_was_successful = False # Try json serialization if not force_pickle: json_filename = get_json_file_location(name, specification, diff_namer, extended_keys) try: with open(json_filename, "w") as f: json.dump(output_dictionary, f) json_serialize_was_successful = True except Exception: logging.getLogger(experiment.get_logger_name()).warning( "Json serialization failed with exception", exc_info=True) os.remove(json_filename) # Try pickle serialization if force_pickle or not json_serialize_was_successful: pickle_file_location = get_pkl_file_location(name, specification, diff_namer, extended_keys) specification_file_location = get_specification_file_location( name, specification, diff_namer, extended_keys) try: with open(pickle_file_location, "wb") as f: dill.dump(output_dictionary, f) with open(specification_file_location, "w") as f: json.dump(specification, f) except Exception: logging.getLogger(experiment.get_logger_name()).critical( "Experiment results serialization failed!!!", exc_info=True) try: os.remove(pickle_file_location) except FileNotFoundError: pass try: os.remove(specification_file_location) except FileNotFoundError: pass
def test_un_serializable_experiment_failure(self): experiment = UnserializableExperiment() runner = ExperimentRunner() specification = {"test": "test"} runner.run("test", [specification], experiment, specification_runner=MainRunner()) self.assertEqual(0, len(os.listdir(get_save_file_directory('test', specification, runner.diff_namer))))