def print_trace(self, filename, directory='Trace', delete_existing_files=True): """ print the trace messages into a text file with the specified filename. It creates a sub directory where the python script is located. :param filename: filename of the text file where trace message should be exported to :param directory: directory (relative to the current root) where the trace files should be located :param delete_existing_files: set to True to delete the existing trace files in the directory """ if not self._on: return # create the directory if does not exist if not os.path.exists(directory): os.makedirs(directory) # delete existing files if delete_existing_files: io.delete_files(extension='.txt', path=os.getcwd() + '/' + directory) # create a new file filename = os.path.join(directory, filename) # open the file with a write access file = open(filename, 'w') # write the trace messages for message in self._messages: file.write('%s\n' % message) # close the file file.close()
def compare_trajectories(csv_file_1, csv_file_2, legends, figure_location): # clean the directory IO.delete_files('.png', figure_location) df1 = TrajsDataFrame(csv_file_1) df2 = TrajsDataFrame(csv_file_2) # for all trajectories i = 0 for key, trajs_one_outcome in df1.allTrajs.items(): fig, ax = plt.subplots(figsize=(5, 5)) # figure size ax.set(title=key) # title and labels # first (base) for traj in trajs_one_outcome.trajs: ax.plot(traj.times, traj.obss, 'g', alpha=1, zorder=2) # second (intervention) for traj in df2.allTrajs[key].trajs: ax.plot(traj.times, traj.obss, 'b', alpha=1, zorder=1) plt.legend(legends) #[::-1] file_name = figure_location + '/' + str(i) + ' ' + key output_figure(plt, file_name) i += 1
def clear_txt_files(path='..'): """ removes every .txt files inside the directory :param path: path (relative to the current root) where the .txt files are located (the folder should already exist) """ io.delete_files('.txt', path)
def plot_histograms(self, par_names=None, csv_file_name_prior=None, posterior_fig_loc='figures_national'): """ creates histograms of parameters specified by ids :param par_names: (list) of parameter names to plot :param csv_file_name_prior: (string) filename where parameter prior ranges are located :param posterior_fig_loc: (string) location where posterior figures_national should be located """ raise ValueError('Needs to be debugged.') # clean the directory IO.delete_files('.png', posterior_fig_loc) # read prior distributions dict_of_priors = None if csv_file_name_prior is not None: dict_of_priors = self.get_dict_of_priors(prior_info_csv_file=csv_file_name_prior) if par_names is None: par_names = self.get_all_parameter_names() # for all parameters, read sampled parameter values and create the histogram for par_name in par_names: # get values for this parameter par_values = self.dictOfParamValues[par_name] # get info of this parameter title, multiplier, x_range = self.get_title_multiplier_x_range_decimal_format( par_name=par_name, dict_of_priors=dict_of_priors) # adjust parameter values par_values = [v*multiplier for v in par_values] # find the filename the histogram should be saved as file_name = posterior_fig_loc + '/Par-' + par_name + ' ' + F.proper_file_name(par_name) # plot histogram Fig.plot_histogram( data=par_values, title=title.replace('!', '\n'), x_range=x_range, figure_size=HISTOGRAM_FIG_SIZE, file_name=file_name )
from apace import VisOptimClasses as Vis import SimPy.InOutFunctions as IO # delete existing figures_national IO.delete_files('.png', '../tests/VisualizeOptimization/optimization_figures/') Vis.plot_all_opt_itrs( csv_directory='../tests/VisualizeOptimization/optimization_csvfiles/', save_plots_directory='../tests/VisualizeOptimization/optimization_figures/', #f_range=[0, 5*10e7], #x_ranges=[[0, 0.3], [0, 0.2]], #y_axis_labels=[r'$f(\tau, \theta)$', r'$\tau$', r'$\theta$'] )
from apace import VisOptimClasses as Vis import SimPy.InOutFunctions as IO # delete existing figures_national IO.delete_files('.png', 'figures/optimization_figs/') Vis.plot_all_opt_itrs( csv_directory='csv_files/optimization_csvs/', n_vars=2, save_plots_directory='figures/optimization_figs/', show_titles=False, f_range=[-7500, -5000], #var_ranges=[[0, 5], [-1, 0], [0, 0.5], [0, 1000], [-1, 0], [0, 1]], y_axis_labels=[ r'$f$', r'$\tau_1$', r'$\tau_2$', r'$R: \rho$', r'$\%I: \tau_0$', r'$\%I: \tau_1$', r'$\%I: rho$' ], # , r'$\rho_1$' window=50)
def plot_histograms(self, ids=None, csv_file_name_prior=None, posterior_fig_loc='figures_national'): """ creates histograms of parameters specified by ids :param ids: (list) list of parameter ids :param csv_file_name_prior: (string) filename where parameter prior ranges are located :param posterior_fig_loc: (string) location where posterior figures_national should be located """ # clean the directory IO.delete_files('.png', posterior_fig_loc) # read prior distributions if csv_file_name_prior is not None: priors = IO.read_csv_rows(file_name=csv_file_name_prior, if_ignore_first_row=True, delimiter=',', if_convert_float=True) # for all parameters, read sampled parameter values and create the histogram par_id = 0 for key, par_values in self.dictOfParams.items(): # skip these columns if key in ['Simulation Replication', 'Random Seed']: continue # check if the histogram should be created for this parameter if_show = False if ids is None: if_show = True elif par_id in ids: if_show = True # create the histogram if if_show: # find prior range x_range = None if priors is not None: try: x_range = [ float(priors[par_id][Column.LB.value]), float(priors[par_id][Column.UB.value]) ] except: print( 'Could not convert string to float to find the prior distribution of parameter:', par_id) else: x_range = None # find the filename the histogram should be saved as file_name = posterior_fig_loc + '\Par-' + str( par_id) + ' ' + F.proper_file_name(key) # find title if priors[par_id][Column.TITLE.value] in ('', None): title = priors[par_id][Column.NAME.value] else: title = priors[par_id][Column.TITLE.value] # find multiplier if priors[par_id][Column.MULTIPLIER.value] in ('', None): multiplier = 1 else: multiplier = float(priors[par_id][Column.MULTIPLIER.value]) x_range = [x * multiplier for x in x_range] par_values = [v * multiplier for v in par_values] # plot histogram Fig.plot_histogram(data=par_values, title=title.replace('!', '\n'), x_range=x_range, figure_size=HISTOGRAM_FIG_SIZE, file_name=file_name) # move to the next parameter par_id += 1