@@ -704,29 +704,42 @@ def write_db_dict(self) -> None:
704704 pprint .pprint (spot_tuner_control )
705705 print ("surrogate_control:" )
706706 pprint .pprint (surrogate_control )
707- db_dict = {
708- str (ident ): {
709- "fun_control" : fun_control ,
710- "design_control" : design_control ,
711- "surrogate_control" : surrogate_control ,
712- "optimizer_control" : optimizer_control ,
713- "spot_tuner_control" : spot_tuner_control ,
707+ #
708+ # Generate a description of the results:
709+ # if spot_tuner_control['min_y'] exists:
710+ try :
711+ result = f"Results for { ident } : Finally, the best value is { spot_tuner_control ['min_y' ]} at { spot_tuner_control ['min_X' ]} ."
712+ #
713+ db_dict = {
714+ "data" : {
715+ "id" : str (ident ),
716+ "result" : result ,
717+ "fun_control" : fun_control ,
718+ "design_control" : design_control ,
719+ "surrogate_control" : surrogate_control ,
720+ "optimizer_control" : optimizer_control ,
721+ "spot_tuner_control" : spot_tuner_control ,
722+ }
714723 }
715- }
716-
717- # check if the directory "db_dicts" exists.
718- if not os .path .exists ("db_dicts" ):
719- try :
720- os .makedirs ("db_dicts" )
721- except OSError as e :
722- raise Exception (f"Error creating directory: { e } " )
723- if os .path .exists ("db_dicts" ):
724- try :
725- with open ("db_dicts/" + self .fun_control ["db_dict_name" ], "a" ) as f :
726- json .dump (db_dict , f , indent = 4 , cls = NumpyEncoder )
727- f .close ()
728- except OSError as e :
729- raise Exception (f"Error writing to file: { e } " )
724+ # Check if the directory "db_dicts" exists.
725+ if not os .path .exists ("db_dicts" ):
726+ try :
727+ os .makedirs ("db_dicts" )
728+ except OSError as e :
729+ raise Exception (f"Error creating directory: { e } " )
730+
731+ if os .path .exists ("db_dicts" ):
732+ try :
733+ # Open the file in append mode to add each new dict as a new line
734+ with open ("db_dicts/" + self .fun_control ["db_dict_name" ], "a" ) as f :
735+ # Using json.dumps to convert the dict to a JSON formatted string
736+ # We then write this string to the file followed by a newline character
737+ # This ensures that each dict is on its own line, conforming to the JSON Lines format
738+ f .write (json .dumps (db_dict , cls = NumpyEncoder ) + "\n " )
739+ except OSError as e :
740+ raise Exception (f"Error writing to file: { e } " )
741+ except KeyError :
742+ print ("No results to write." )
730743
731744 def run (self , X_start = None ) -> Spot :
732745 self .initialize_design (X_start )
0 commit comments