diff --git a/src/stats/stats.py b/src/stats/stats.py index 532bc7bf..1dd6d880 100755 --- a/src/stats/stats.py +++ b/src/stats/stats.py @@ -8,6 +8,9 @@ from utilities.algorithm.state import create_state from utilities.stats import trackers from utilities.stats.save_plots import save_best_fitness_plot +from utilities.stats.file_io import save_stats_to_file, save_stats_headers, \ + save_best_ind_to_file + """Algorithm statistics""" stats = { @@ -36,9 +39,6 @@ "time_adjust": 0 } -from utilities.stats.file_io import save_stats_to_file, save_stats_headers, \ - save_best_ind_to_file - def get_stats(individuals, end=False): """ @@ -95,12 +95,12 @@ def get_stats(individuals, end=False): # Save stats to file. if not params['DEBUG']: if stats['gen'] == 0: - save_stats_headers() - save_stats_to_file(end) + save_stats_headers(stats) + save_stats_to_file(stats, end) if params['SAVE_ALL']: - save_best_ind_to_file(end, stats['gen']) + save_best_ind_to_file(stats, end, stats['gen']) elif params['VERBOSE'] or end: - save_best_ind_to_file(end, "best") + save_best_ind_to_file(stats, end, "best") if end and not params['SILENT']: print_final_stats() diff --git a/src/utilities/stats/file_io.py b/src/utilities/stats/file_io.py index b86e49f9..c7277131 100644 --- a/src/utilities/stats/file_io.py +++ b/src/utilities/stats/file_io.py @@ -2,48 +2,14 @@ from os import path, getcwd, makedirs from algorithm.parameters import params -from stats.stats import stats from utilities.stats import trackers -def save_params_to_file(): - """ - Save evolutionary parameters in a parameters.txt file. Automatically - parse function and class names. - - :return: Nothing. - """ - - # Generate file path and name. - filename = path.join(params['FILE_PATH'], "parameters.txt") - savefile = open(filename, 'w') - - # Justify whitespaces for pretty printing/saving. - col_width = max(len(param) for param in params.keys()) - - for param in sorted(params.keys()): - savefile.write(str(param) + ": ") - spaces = [" " for _ in range(col_width - len(param))] - - if isinstance(params[param], types.FunctionType): - # Object is a function, save function name. - savefile.write("".join(spaces) + str(params[ - param].__name__) + "\n") - elif hasattr(params[param], '__call__'): - # Object is a class instance, save name of class instance. - savefile.write("".join(spaces) + str(params[ - param].__class__.__name__) + "\n") - else: - # Write object as normal. - savefile.write("".join(spaces) + str(params[param]) + "\n") - - savefile.close() - - -def save_stats_to_file(end=False): +def save_stats_to_file(stats, end=False): """ Write the results to a results file for later analysis + :param stats: The stats.stats.stats dictionary. :param end: A boolean flag indicating whether or not the evolutionary process has finished. :return: Nothing. @@ -67,10 +33,11 @@ def save_stats_to_file(end=False): savefile.close() -def save_stats_headers(): +def save_stats_headers(stats): """ Saves the headers for all stats in the stats dictionary. + :param stats: The stats.stats.stats dictionary. :return: Nothing. """ @@ -82,10 +49,11 @@ def save_stats_headers(): savefile.close() -def save_best_ind_to_file(end=False, name="best"): +def save_best_ind_to_file(stats, end=False, name="best"): """ Saves the best individual to a file. + :param stats: The stats.stats.stats dictionary. :param end: A boolean flag indicating whether or not the evolutionary process has finished. :param name: The name of the individual. Default set to "best". @@ -146,3 +114,37 @@ def generate_folders_and_files(): str(params['TIME_STAMP'])) save_params_to_file() + + +def save_params_to_file(): + """ + Save evolutionary parameters in a parameters.txt file. Automatically + parse function and class names. + + :return: Nothing. + """ + + # Generate file path and name. + filename = path.join(params['FILE_PATH'], "parameters.txt") + savefile = open(filename, 'w') + + # Justify whitespaces for pretty printing/saving. + col_width = max(len(param) for param in params.keys()) + + for param in sorted(params.keys()): + savefile.write(str(param) + ": ") + spaces = [" " for _ in range(col_width - len(param))] + + if isinstance(params[param], types.FunctionType): + # Object is a function, save function name. + savefile.write("".join(spaces) + str(params[ + param].__name__) + "\n") + elif hasattr(params[param], '__call__'): + # Object is a class instance, save name of class instance. + savefile.write("".join(spaces) + str(params[ + param].__class__.__name__) + "\n") + else: + # Write object as normal. + savefile.write("".join(spaces) + str(params[param]) + "\n") + + savefile.close()