From 59ef7c44bbceed10191d3ad4e687aa78b3e5f70d Mon Sep 17 00:00:00 2001 From: Matej Hajnal Date: Mon, 10 May 2021 03:21:27 +0200 Subject: [PATCH] gui MHMH added --- performance_test/performance_test.py | 7 +- performance_test/performance_test_sampling.py | 3 +- src/gui.py | 91 +++++++++++-------- src/mhmh.py | 20 ++-- 4 files changed, 70 insertions(+), 51 deletions(-) diff --git a/performance_test/performance_test.py b/performance_test/performance_test.py index 3f48d86..eb66701 100644 --- a/performance_test/performance_test.py +++ b/performance_test/performance_test.py @@ -138,10 +138,9 @@ def load_functions(path, factorise=False, debug=False, source="any"): functions = list(map(str, functions)) if debug: - # print("functions", functions) - print("functions[0]", functions[0]) - else: - print("functions[0]", functions[0]) + print("Loaded functions", functions) + elif not silent: + print("First of the loaded functions: functions[0]", functions[0]) return functions diff --git a/performance_test/performance_test_sampling.py b/performance_test/performance_test_sampling.py index d34cabe..5ea6afa 100644 --- a/performance_test/performance_test_sampling.py +++ b/performance_test/performance_test_sampling.py @@ -80,7 +80,8 @@ else: functions = load_functions(f"bee/semisynchronous_{population_size}_bees", debug=debug, source=model_checker, factorise=factorise) - print("functions", functions) + if debug: + print("functions", functions) ## LOAD DATA if multiparam: diff --git a/src/gui.py b/src/gui.py index 356b27e..5ec116e 100755 --- a/src/gui.py +++ b/src/gui.py @@ -273,7 +273,7 @@ def __init__(self, *args, **kwargs): self.save.set(True) ## General Settings - self.version = "1.25.8" ## Version of the gui + self.version = "1.26.0" ## Version of the gui self.silent = BooleanVar() ## Sets the command line output to minimum self.debug = BooleanVar() ## Sets the command line output to maximum @@ -4077,7 +4077,7 @@ def mhmh(self): no_max_depth = False ## Getting values from entry boxes - self.max_depth = int(self.max_dept_entry.get()) + self.max_depth = int(self.max_depth_entry.get()) if self.max_depth < 0: no_max_depth = True self.max_depth = 3 @@ -4168,47 +4168,66 @@ def mhmh(self): self.delta = float(self.delta_entry.get()) if self.mh_results == "": - assert isinstance(self.mh_results, HastingsResults) rerun_mh = True else: rerun_mh = askyesno("MHMH", "Do you want to rerun Metropolis-Hasting?") - if rerun_mh: - self.create_window_to_load_param_point(parameters=self.parameters) + self.hastings() + + assert isinstance(self.mh_results, HastingsResults) + self.cursor_toggle_busy(True) + a, b = initialise_mhmh(self.parameters, self.parameter_domains, self.functions, self.constraints, self.data, + int(self.n_samples_entry.get()), int(self.MH_sampling_iterations_entry.get()), eps=0, + sd=float(self.sd_entry.get()), theta_init=self.parameter_point if rerun_mh else False, + is_probability=True, where_mh=[self.page6_figure2, self.page6_b], where_ref=None, + progress=False, burn_in=float(self.burn_in_entry.get()), debug=self.debug.get(), + bins=int(float(self.bins_entry.get())), mh_timeout=int(float(self.mh_timeout_entry.get())), + metadata=self.show_mh_metadata.get(), draw_plot=self.draw_plot_window, + save=False, silent=self.silent.get(), recursion_depth=10, epsilon=self.epsilon, + delta=self.delta, coverage=self.coverage, version=int(self.alg_entry.get()), + solver=str(self.solver_entry.get()), parallel=cores, + gui=self.update_progress_bar if self.show_progress else False, + ref_timeout=int(float(self.refinement_timeout_entry.get())), mh_result=self.mh_results) + + self.space = a + self.show_space(show_refinement=True, show_samples=self.show_samples, show_true_point=self.show_true_point, + prefer_unsafe=self.show_red_in_multidim_refinement.get(), show_all=show_all, + warnings=not (no_max_depth and self.space.get_coverage() < self.coverage), + is_sampling_guided=self.sampling_guided_refinement.get(), + is_parallel_refinement=int(self.refinement_cores_entry.get()) > 1) + self.page6_figure.tight_layout() ## By huypn + self.page6_figure.canvas.draw() + self.page6_figure.canvas.flush_events() - ## Clear figure - self.set_lower_figure(clear=True) - - a = initialise_mhmh(self.parameters, self.parameter_domains, self.functions, self.constraints, self.data, - int(self.n_samples_entry.get()), int(self.MH_sampling_iterations_entry.get()), eps=0, - sd=float(self.sd_entry.get()), theta_init=self.parameter_point, is_probability=True, - where_mh=[self.page6_figure2, self.page6_b], where_ref=[self.page6_figure, self.page6_a], - progress=False, burn_in=float(self.burn_in_entry.get()), - bins=int(float(self.bins_entry.get())), - mh_timeout=int(float(self.mh_timeout_entry.get())), debug=self.debug.get(), - metadata=self.show_mh_metadata.get(), draw_plot=self.draw_plot_window, - save=False, silent=self.silent.get(), recursion_depth=10, epsilon=self.epsilon, - delta=self.delta, coverage=self.coverage, - version=int(self.alg_entry.get()), solver=str(self.solver_entry.get()), - gui=self.update_progress_bar if self.show_progress else False, - parallel=cores, ref_timeout=int(float(self.refinement_timeout_entry.get())), mh_result=None) + ## Autosave figure + if self.save.get(): + time_stamp = str(strftime("%d-%b-%Y-%H-%M-%S", localtime())) + ".png" + self.page6_figure.savefig(os.path.join(self.refinement_results, "tmp", f"Space_refinement_{time_stamp}"), + bbox_inches='tight') + print("Figure stored here: ", + os.path.join(self.refinement_results, "tmp", f"Space_refinement_{time_stamp}")) + with open(os.path.join(self.refinement_results, "tmp", "figure_to_title.txt"), "a+") as f: + f.write(f"Space_refinement_{time_stamp} :\n") + f.write(f" constraints: {self.constraints_file.get()}\n") + + self.print_space() + + self.constraints_changed = False + self.space_changed = False + + ## Autosave + try: + self.save_space(os.path.join(self.tmp_dir, "space.p")) + except ValueError as err: + print(f"Space could not be loaded, {str(err)}") + messagebox.showwarning("Space could not be saved.", str(err)) + + if no_max_depth and self.space.get_coverage() < self.coverage: + self.refine_space() else: - assert isinstance(self.mh_results, HastingsResults) - a = initialise_mhmh(self.parameters, self.parameter_domains, self.functions, self.constraints, self.data, - int(self.n_samples_entry.get()), int(self.MH_sampling_iterations_entry.get()), eps=0, - sd=float(self.sd_entry.get()), theta_init=self.parameter_point, is_probability=True, - where_mh=[self.page6_figure2, self.page6_b], where_ref=[self.page6_figure, self.page6_a], - progress=False, burn_in=float(self.burn_in_entry.get()), - bins=int(float(self.bins_entry.get())), - mh_timeout=int(float(self.mh_timeout_entry.get())), debug=self.debug.get(), - metadata=self.show_mh_metadata.get(), draw_plot=self.draw_plot_window, - save=False, silent=self.silent.get(), recursion_depth=10, epsilon=self.epsilon, - delta=self.delta, coverage=self.coverage, version=int(self.alg_entry.get()), - solver=str(self.solver_entry.get()), parallel=cores, - gui=self.update_progress_bar if self.show_progress else False, - ref_timeout=int(float(self.refinement_timeout_entry.get())), - mh_result=self.mh_results.get_accepted()) + self.status_set("Space refinement finished.") + self.cursor_toggle_busy(False) def refine_space(self): """ Refines (Parameter) Space. Plots the results. """ diff --git a/src/mhmh.py b/src/mhmh.py index c481686..535dcd0 100644 --- a/src/mhmh.py +++ b/src/mhmh.py @@ -25,7 +25,7 @@ del config -def initialise_mhmh(params, parameter_intervals, functions, constraints, data, sample_size, mh_sampling_iterations, +def initialise_mhmh(params, parameter_domains, functions, constraints, data, sample_size, mh_sampling_iterations, eps=0, sd=0.15, theta_init=False, is_probability=None, where_mh=False, where_ref=False, progress=False, burn_in=False, bins=20, mh_timeout=False, debug=False, metadata=True, draw_plot=False, save=False, silent=True, recursion_depth=10, epsilon=0.001, delta=0.001, @@ -34,7 +34,7 @@ def initialise_mhmh(params, parameter_intervals, functions, constraints, data, s Args: params (list of strings): parameter names - parameter_intervals (list of tuples): domains of parameters + parameter_domains (list of tuples): domains of parameters functions (list of strings): expressions to be evaluated and compared with data constraints (list of strings): array of (in)equalities data (list of floats): measurement values @@ -64,11 +64,11 @@ def initialise_mhmh(params, parameter_intervals, functions, constraints, data, s gui (bool or Callable): called from the graphical user interface parallel (int): number of threads to use in parallel for refinement, when True (#cores - 1) is used ref_timeout (int): timeout of refinement in seconds (0 for no timeout) - mh_result (list or None): (optional) list of accepted points - if not None, this list of accepted points will be used as result of MH + mh_result (HastingsResults or None): (optional) list of accepted points - if not None, this list of accepted points will be used as result of MH """ if mh_result is None: ## Run MH - mh_result = init_mh(params, parameter_intervals, functions, data, sample_size, mh_sampling_iterations, eps=eps, + mh_result = init_mh(params, parameter_domains, functions, data, sample_size, mh_sampling_iterations, eps=eps, sd=sd, theta_init=theta_init, is_probability=is_probability, where=where_mh, progress=progress, burn_in=burn_in, bins=bins, timeout=mh_timeout, silent=silent, debug=debug, metadata=metadata, draw_plot=draw_plot) @@ -81,7 +81,7 @@ def initialise_mhmh(params, parameter_intervals, functions, constraints, data, s ## Create bins intervals = [] - for interval in parameter_intervals: + for interval in parameter_domains: add_this = [] for index in range(bins): add_this.append([Fraction(index, bins-1)*(interval[1] - interval[0]) + interval[0], Fraction(index+1, bins-1)*(interval[1] - interval[0]) + interval[0]]) @@ -133,12 +133,12 @@ def initialise_mhmh(params, parameter_intervals, functions, constraints, data, s ## print(point) indices = [] for index in range(len(point)): - interval_length = parameter_intervals[index][1] - parameter_intervals[index][0] - relative_position = (point[index] - parameter_intervals[index][0]) / interval_length + interval_length = Fraction(str(parameter_domains[index][1] - parameter_domains[index][0])) + relative_position = (point[index] - parameter_domains[index][0]) / interval_length relative_interval = relative_position * (bins-1) - indices.append((Fraction(interval_length, bins-1)*floor(relative_interval) + parameter_intervals[index][0], - Fraction(interval_length, bins-1)*(floor(relative_interval)+1) + parameter_intervals[index][0])) + indices.append((Fraction(interval_length, bins-1) * floor(relative_interval) + parameter_domains[index][0], + Fraction(interval_length, bins-1) * (floor(relative_interval)+1) + parameter_domains[index][0])) indices = tuple(indices) ## print("indices", indices) try: @@ -171,7 +171,7 @@ def initialise_mhmh(params, parameter_intervals, functions, constraints, data, s print("We expect to see ", expected_values, "per (hype)rectangle") if not silent else None ## Initialise and add only rectangles to be refined - space = RefinedSpace(parameter_intervals, params) + space = RefinedSpace(parameter_domains, params) space.rectangles_unknown = {} del rectangularised_space