Skip to content

Commit

Permalink
Fixed circular referencing in stats and file IO
Browse files Browse the repository at this point in the history
  • Loading branch information
mikefenton committed Mar 24, 2017
1 parent 35a4e25 commit 7ec8365
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 45 deletions.
14 changes: 7 additions & 7 deletions src/stats/stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down Expand Up @@ -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):
"""
Expand Down Expand Up @@ -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()
Expand Down
78 changes: 40 additions & 38 deletions src/utilities/stats/file_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
"""

Expand All @@ -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".
Expand Down Expand Up @@ -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()

0 comments on commit 7ec8365

Please sign in to comment.